use of at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph in project Alpha by alpha-asp.
the class StratifiedEvaluation method apply.
@Override
public // memories created here rather than re-initialize everything.
InternalProgram apply(AnalyzedProgram inputProgram) {
// Calculate a stratification and initialize the working memory.
ComponentGraph componentGraph = inputProgram.getComponentGraph();
List<ComponentGraph.SCComponent> strata = StratificationAlgorithm.calculateStratification(componentGraph);
predicateDefiningRules = inputProgram.getPredicateDefiningRules();
// Set up list of atoms which are known to be true - these will be expand by the evaluation.
Map<Predicate, Set<Instance>> knownFacts = new LinkedHashMap<>(inputProgram.getFactsByPredicate());
for (Map.Entry<Predicate, Set<Instance>> entry : knownFacts.entrySet()) {
workingMemory.initialize(entry.getKey());
workingMemory.addInstances(entry.getKey(), true, entry.getValue());
}
// Create working memories for all predicates occurring in each rule.
for (CompiledRule nonGroundRule : inputProgram.getRulesById().values()) {
for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
workingMemory.initialize(predicate);
}
}
workingMemory.reset();
// Set up literal instantiator.
literalInstantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
// Evaluate the program part covered by the calculated stratification.
for (ComponentGraph.SCComponent currComponent : strata) {
evaluateComponent(currComponent);
}
// Build the program resulting from evaluating the stratified part.
// Add original input facts to newly derived ones.
additionalFacts.addAll(inputProgram.getFacts());
List<CompiledRule> outputRules = new ArrayList<>();
inputProgram.getRulesById().entrySet().stream().filter((entry) -> !solvedRuleIds.contains(entry.getKey())).forEach((entry) -> outputRules.add(entry.getValue()));
// NOTE: if InternalProgram requires solved rules, they should be added here.
return new InternalProgram(outputRules, additionalFacts);
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyTwoRulesTest.
@Test
public void stratifyTwoRulesTest() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.").append("\n");
bld.append("c :- b.").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);
assertEquals(3, strata.size());
assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
assertTrue(predicateIsBeforePredicateInOrder(a, c, strata));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method avoidDuplicatesTest1.
@Test
public void avoidDuplicatesTest1() {
StringBuilder bld = new StringBuilder();
bld.append("b :- a.");
bld.append("c :- b.");
bld.append("c :- a.");
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);
assertTrue(predicateIsBeforePredicateInOrder(a, b, strata));
assertTrue(predicateIsBeforePredicateInOrder(b, c, strata));
assertTrue(predicateIsBeforePredicateInOrder(a, c, strata));
assertEquals(3, strata.size());
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph in project Alpha by alpha-asp.
the class StratificationAlgorithmTest method stratifyOneRuleTest.
@Test
public void stratifyOneRuleTest() {
ASPCore2Program prog = parser.parse("a :- b.");
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);
assertEquals(2, strata.size());
assertTrue(predicateIsBeforePredicateInOrder(b, a, strata));
}
use of at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph in project Alpha by alpha-asp.
the class ComponentGraphWriterTest 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 component 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 componentGraph" + LS + "{" + LS + "splines=false;" + LS + "ranksep=4.0;" + LS + "label = <" + LS + " <table border = '1' cellborder = '0'>" + LS + " <tr><td>Component Id</td><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>" + LS + " <tr><td>Predicates</td><td>q/1<br/></td><td>r/1<br/></td><td>p/1<br/></td><td>[constr_1]/0<br/></td><td>t/1<br/>u/1<br/></td><td>s/1<br/></td></tr>" + LS + " </table>" + LS + ">" + LS + "" + LS + "n0 [label = C0]" + LS + "n1 [label = C1]" + LS + "n2 [label = C2]" + LS + "n0 -> n2 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n2 [xlabel=\"+\" labeldistance=0.1]" + LS + "n3 [label = C3]" + LS + "n0 -> n3 [xlabel=\"-\" labeldistance=0.1]" + LS + "n1 -> n3 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n3 [xlabel=\"+\" labeldistance=0.1]" + LS + "n4 [label = C4]" + LS + "n2 -> n4 [xlabel=\"+\" labeldistance=0.1]" + LS + "n5 [label = C5]" + LS + "n0 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "n1 -> n5 [xlabel=\"-\" labeldistance=0.1]" + LS + "n2 -> n5 [xlabel=\"+\" labeldistance=0.1]" + LS + "}" + LS;
Alpha alpha = new AlphaImpl();
DebugSolvingContext dbgResult = alpha.prepareDebugSolve(alpha.readProgramString(asp));
ComponentGraph compgraph = dbgResult.getComponentGraph();
ComponentGraphWriter writer = new ComponentGraphWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeAsDot(compgraph, out);
String actualGraph = out.toString();
System.out.println(actualGraph);
assertEquals(expectedGraph, actualGraph);
}
Aggregations