use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyWithPositiveCycleTest.
@Test
public void stratifyWithPositiveCycleTest() {
StringBuilder bld = new StringBuilder();
bld.append("ancestor_of(X, Y) :- parent_of(X, Y).");
bld.append("ancestor_of(X, Z) :- parent_of(X, Y), ancestor_of(Y, Z).");
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 ancestorOf = Predicates.getPredicate("ancestor_of", 2);
Predicate parentOf = Predicates.getPredicate("parent_of", 2);
assertEquals(2, strata.size());
assertTrue(predicateIsBeforePredicateInOrder(parentOf, ancestorOf, strata));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyAvoidDuplicatesTest.
@Test
public void stratifyAvoidDuplicatesTest() {
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);
assertEquals(7, strata.size());
assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, h, strata));
assertTrue(predicateIsBeforePredicateInOrder(c, d, strata));
assertTrue(predicateIsBeforePredicateInOrder(d, e, strata));
assertTrue(predicateIsBeforePredicateInOrder(e, f, strata));
assertTrue(predicateIsBeforePredicateInOrder(d, f, strata));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class DependencyGraphImplTest method reachabilityCheckSimpleTest.
@Test
public void reachabilityCheckSimpleTest() {
ASPCore2Program prog = parser.parse("b :- a.");
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));
NodeImpl nonExistent = new NodeImpl(Predicates.getPredicate("notHere", 0));
assertTrue(DependencyGraphUtils.isReachableFrom(a, a, dg));
assertTrue(DependencyGraphUtils.isReachableFrom(b, a, dg));
assertFalse(DependencyGraphUtils.isReachableFrom(a, b, dg));
assertFalse(DependencyGraphUtils.isReachableFrom(nonExistent, a, dg));
assertFalse(DependencyGraphUtils.isReachableFrom(nonExistent, b, dg));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph in project Alpha by alpha-asp.
the class AlphaImpl method prepareDebugSolve.
@Override
public DebugSolvingContext prepareDebugSolve(final NormalProgram program, java.util.function.Predicate<Predicate> filter) {
final DependencyGraph depGraph;
final ComponentGraph compGraph;
final AnalyzedProgram analyzed = AnalyzedProgram.analyzeNormalProgram(program);
final NormalProgram preprocessed;
if (this.config.isEvaluateStratifiedPart()) {
preprocessed = new StratifiedEvaluation().apply(analyzed).toNormalProgram();
} else {
preprocessed = program;
}
depGraph = analyzed.getDependencyGraph();
compGraph = analyzed.getComponentGraph();
final Solver solver = prepareSolverFor(analyzed, filter);
return new DebugSolvingContext() {
@Override
public Solver getSolver() {
return solver;
}
@Override
public NormalProgram getPreprocessedProgram() {
return preprocessed;
}
@Override
public NormalProgram getNormalizedProgram() {
return program;
}
@Override
public DependencyGraph getDependencyGraph() {
return depGraph;
}
@Override
public ComponentGraph getComponentGraph() {
return compGraph;
}
};
}
Aggregations