use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class DependencyGraphImplTest method stronglyConnectedComponentsMultipleComponentsTest.
@Test
public void stronglyConnectedComponentsMultipleComponentsTest() {
String inputProgram = "f0.\n" + "f1.\n" + "f2.\n" + "f3.\n" + "a :- f0, f1, not b.\n" + "b :- f0, f1, not a.\n" + "c :- f2, f3, not d.\n" + "d :- f2, f3, not c.\n" + "x :- a, c, y.\n" + "y :- b, d, x.\n" + "z :- x, y, z.";
ASPCore2Program prog = parser.parse(inputProgram);
NormalProgram normalProg = normalizeTransform.apply(prog);
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
DependencyGraph dg = analyzed.getDependencyGraph();
Node f0 = dg.getNodeForPredicate(Predicates.getPredicate("f0", 0));
Node f1 = dg.getNodeForPredicate(Predicates.getPredicate("f1", 0));
Node f2 = dg.getNodeForPredicate(Predicates.getPredicate("f2", 0));
Node f3 = dg.getNodeForPredicate(Predicates.getPredicate("f3", 0));
Node a = dg.getNodeForPredicate(Predicates.getPredicate("a", 0));
Node b = dg.getNodeForPredicate(Predicates.getPredicate("b", 0));
Node c = dg.getNodeForPredicate(Predicates.getPredicate("c", 0));
Node d = dg.getNodeForPredicate(Predicates.getPredicate("d", 0));
Node x = dg.getNodeForPredicate(Predicates.getPredicate("x", 0));
Node y = dg.getNodeForPredicate(Predicates.getPredicate("y", 0));
Node z = dg.getNodeForPredicate(Predicates.getPredicate("z", 0));
StronglyConnectedComponentsAlgorithm.SccResult sccResult = StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg);
Map<Node, Integer> nodesByComponent = sccResult.nodesByComponentId;
List<List<Node>> stronglyConnectedComponents = sccResult.stronglyConnectedComponents;
assertEquals(8, stronglyConnectedComponents.size());
for (int i = 0; i < stronglyConnectedComponents.size(); i++) {
List<Node> stronglyConnectedComponent = stronglyConnectedComponents.get(i);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(stronglyConnectedComponent, dg));
for (Node node : stronglyConnectedComponent) {
assertEquals(Integer.valueOf(i), nodesByComponent.get(node));
}
}
List<Node> c1 = new ArrayList<>();
c1.add(a);
c1.add(b);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c1, dg));
assertEquals(nodesByComponent.get(a), nodesByComponent.get(b));
List<Node> c2 = new ArrayList<>();
c2.add(c);
c2.add(d);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c2, dg));
assertEquals(nodesByComponent.get(c), nodesByComponent.get(d));
List<Node> c3 = new ArrayList<>();
c3.add(x);
c3.add(y);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c3, dg));
assertEquals(nodesByComponent.get(x), nodesByComponent.get(y));
List<Node> c4 = new ArrayList<>();
c4.add(z);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c4, dg));
List<Node> c5 = new ArrayList<>();
c5.add(f0);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c5, dg));
List<Node> c6 = new ArrayList<>();
c6.add(f1);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c6, dg));
List<Node> c7 = new ArrayList<>();
c7.add(f2);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c7, dg));
List<Node> c8 = new ArrayList<>();
c8.add(f3);
assertTrue(DependencyGraphUtils.isStronglyConnectedComponent(c8, dg));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class DependencyGraphImplTest method reachabilityCheckWithHopsTest.
@Test
public void reachabilityCheckWithHopsTest() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.").append("\n");
bld.append("c :- b.").append("\n");
bld.append("d :- c.").append("\n");
ASPCore2Program prog = parser.parse(bld.toString());
NormalProgram normalProg = normalizeTransform.apply(prog);
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
DependencyGraph dg = analyzed.getDependencyGraph();
Node a = dg.getNodeForPredicate(Predicates.getPredicate("a", 0));
Node b = dg.getNodeForPredicate(Predicates.getPredicate("b", 0));
Node c = dg.getNodeForPredicate(Predicates.getPredicate("c", 0));
Node d = dg.getNodeForPredicate(Predicates.getPredicate("d", 0));
assertTrue(DependencyGraphUtils.isReachableFrom(d, a, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(c, a, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(b, a, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(a, a, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(d, b, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(c, b, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(b, b, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(d, c, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(c, c, dg));
assertFalse(DependencyGraphUtils.isReachableFrom(a, d, dg));
assertFalse(DependencyGraphUtils.isReachableFrom(a, c, dg));
assertFalse(DependencyGraphUtils.isReachableFrom(a, b, dg));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class DependencyGraphWriterTest method smokeTest.
@Test
public void smokeTest() {
// Note: rather than testing correct implementation of dot file format,
// (which would be a lot of work), just test correct dot code generation
// for one dependency graph that has all possible "special" node constellations,
// i.e. positive and negative dependencies, cycle through negation, constraints.
String asp = "p(X) :- q(X), r(X)." + LS + "s(X) :- p(X), q(X), not r(X)." + LS + "t(X) :- p(X), not u(X)." + LS + "u(X) :- p(X), not t(X)." + LS + ":- p(X), not q(X), not r(X).";
String expectedGraph = "digraph dependencyGraph" + LS + "{" + LS + "splines=false;" + LS + "ranksep=4.0;" + LS + "n0 [label = \"r/1\"]" + LS + "n1 [label = \"q/1\"]" + LS + "n2 [label = \"t/1\"]" + LS + "n3 [label = \"s/1\"]" + LS + "n4 [label = \"u/1\"]" + LS + "n5 [label = \"[constr_1]/0\"]" + LS + "n6 [label = \"p/1\"]" + LS + "n0 -> n6 [xlabel=\"+\" labeldistance=0.1]" + LS + "n0 -> n3 [xlabel=\"-\" labeldistance=0.1]" + LS + "n0 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n1 -> n6 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n3 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n4 [xlabel=\"-\" labeldistance=0.1]" + LS + "n4 -> n2 [xlabel=\"-\" labeldistance=0.1]" + LS + "n5 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n6 -> n3 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n2 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n4 [xlabel=\"+\" labeldistance=0.1]" + LS + "n6 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "}" + LS;
Alpha alpha = new AlphaImpl();
DebugSolvingContext dbgResult = alpha.prepareDebugSolve(alpha.readProgramString(asp));
DependencyGraph depgraph = dbgResult.getDependencyGraph();
DependencyGraphWriter writer = new DependencyGraphWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeAsDot(depgraph, out);
String actualGraph = out.toString();
assertEquals(expectedGraph, actualGraph);
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyLargeGraphTest.
@Test
public void stratifyLargeGraphTest() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.");
bld.append("c :- b.");
bld.append("d :- c.");
bld.append("e :- d.");
bld.append("f :- not e.");
bld.append("g :- d, j, not f.");
bld.append("h :- not c.");
bld.append("i :- h, not j.");
bld.append("j :- h, not i.");
bld.append("k :- g, not l.");
bld.append("l :- g, not k.");
bld.append("m :- not k, not l.");
bld.append("n :- m, not i, not j.");
bld.append("p :- not m, not n.");
ASPCore2Program prog = parser.parse(bld.toString());
NormalProgram normalProg = normalizeTransform.apply(prog);
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
DependencyGraph dg = analyzed.getDependencyGraph();
ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
Predicate a = Predicates.getPredicate("a", 0);
Predicate b = Predicates.getPredicate("b", 0);
Predicate c = Predicates.getPredicate("c", 0);
Predicate d = Predicates.getPredicate("d", 0);
Predicate e = Predicates.getPredicate("e", 0);
Predicate f = Predicates.getPredicate("f", 0);
Predicate h = Predicates.getPredicate("h", 0);
assertTrue(predicateIsBeforePredicateInOrder(a, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(a, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(d, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(e, f, strata));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyWithNegativeDependencyTest.
@Test
public void stratifyWithNegativeDependencyTest() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.").append("\n");
bld.append("c :- b.").append("\n");
bld.append("d :- not c.").append("\n");
bld.append("e :- d.").append("\n");
ASPCore2Program prog = parser.parse(bld.toString());
NormalProgram normalProg = normalizeTransform.apply(prog);
AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(normalProg);
DependencyGraph dg = analyzed.getDependencyGraph();
ComponentGraph cg = ComponentGraphImpl.buildComponentGraph(dg, StronglyConnectedComponentsAlgorithm.findStronglyConnectedComponents(dg));
List<SCComponent> strata = StratificationAlgorithm.calculateStratification(cg);
Predicate a = Predicates.getPredicate("a", 0);
Predicate b = Predicates.getPredicate("b", 0);
Predicate c = Predicates.getPredicate("c", 0);
Predicate d = Predicates.getPredicate("d", 0);
Predicate e = Predicates.getPredicate("e", 0);
assertEquals(5, strata.size());
assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, d, strata));
assertTrue(predicateIsBeforePredicateInOrder(d, e, strata));
}
Aggregations