cnetwork enables you to manage constraints by providing a simple Java API for constraint network construction and constraint network manipulation.
For the time being, because there are no stable releases yet, there are only Maven snapshots available that can be easily integrated into your application.
<dependency>
<groupId>com.github.hycos</groupId>
<artifactId>cnetwork</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
The example below illustrates how to use the cnetwork API. The
ConstraintNetworkBuilder
manages the constraint network construction. The
addOperand()
, addOperation()
and addConstraint()
methods add
variables/operands, operations (i.e., a function with a non-boolean return
value) and constraints to the network, respectively. All of these methods
create Node
instances in the constraint network. The addOperand()
method
takes as argument the type and the identifier as input, whereas the
addOperation()
and addConstraint()
methods takes as input the kind of
operation/constraint and the involved parameter nodes.
ConstraintNetworkBuilder cn = new ConstraintNetworkBuilder();
Node zero = cn.addOperand(NodeKind.NUMLIT, "0");
Node one = cn.addOperand(NodeKind.NUMLIT, "1");
Node three = cn.addOperand(NodeKind.NUMLIT, "3");
Node dot = cn.addOperand(NodeKind.STRLIT, ".");
Node add = cn.addOperation(NodeKind.ADD, one, zero);
cn.addConstraint(NodeKind.EQUALS, add, zero);
In case you would like to do an impact analysis in a constraint network, you
can use the CnetworkSlicerForward
object or CnetworkSlicerBackward
classes.
The slicer takes as input a constraint network instance. After invoking the
slice()
method with the slicing criterion as input, the method returns a set
of the nodes which appear in the slice. The following code snippet illustrates
a backward slicing example where the set nodes
contains the two constraint
network nodes tostr
and var
.
Node five = cn.addOperand(NodeKind.NUMLIT, "5");
Node s = cn.addOperand(NodeKind.STRLIT, "s");
Node var = cn.addOperand(NodeKind.NUMVAR, "v");
Node tostr = cn.addOperation(NodeKind.TOSTR, var);
Node concat = cn.addOperation(NodeKind.CONCAT, tostr, s);
CnetworkSlicerBackward b = new CnetworkSlicerBackward(cn
.getConstraintNetwork());
You can visualize a constraint network by invoking the .toDot()
method which
serializes the constraint network in the .dot
output format. From this input
format, you print a graph by means of graphviz (for
example by invoking dot -T pdf <infile> -o <ofile>
). In case you would like
to automate this task, this
script
might be useful.
ConstraintNetworkBuilder cb = new ConstraintNetworkBuilder();
// ...
System.out.println(cb.getConstraintNetwork().toDot());
The constraint network for the first example would look as follows.
Copyright (C) 2017 Julian Thome [email protected]
cnetwork is licensed under the EUPL, Version 1.1 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in compliance with the Licence. You may obtain a copy of the Licence at:
https://joinup.ec.europa.eu/sites/default/files/eupl1.1.-licence-en_0.pdf
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licence for the specific language governing permissions and limitations under the Licence.