Search in sources :

Example 1 with ComponentGraph

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);
}
Also used : InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Stack(java.util.Stack) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) LiteralInstantiationResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiationResult) Map(java.util.Map) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) SetUtils(org.apache.commons.collections4.SetUtils) LiteralInstantiator(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiator) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) LinkedHashSet(java.util.LinkedHashSet) AssignmentStatus(at.ac.tuwien.kr.alpha.core.grounder.instantiation.AssignmentStatus) Logger(org.slf4j.Logger) StratificationAlgorithm(at.ac.tuwien.kr.alpha.core.depgraph.StratificationAlgorithm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Set(java.util.Set) WorkingMemoryBasedInstantiationStrategy(at.ac.tuwien.kr.alpha.core.grounder.instantiation.WorkingMemoryBasedInstantiationStrategy) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) RuleGroundingOrder(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingOrder) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) IndexedInstanceStorage(at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage) Collections(java.util.Collections) RuleGroundingInfo(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingInfo) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) WorkingMemoryBasedInstantiationStrategy(at.ac.tuwien.kr.alpha.core.grounder.instantiation.WorkingMemoryBasedInstantiationStrategy) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LiteralInstantiator(at.ac.tuwien.kr.alpha.core.grounder.instantiation.LiteralInstantiator) ArrayList(java.util.ArrayList) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) LinkedHashMap(java.util.LinkedHashMap) ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with ComponentGraph

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));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 3 with ComponentGraph

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());
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 4 with ComponentGraph

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));
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) SCComponent(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 5 with ComponentGraph

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);
}
Also used : ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)10 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)9 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)9 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)8 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)8 Test (org.junit.jupiter.api.Test)8 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)7 SCComponent (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph.SCComponent)7 DebugSolvingContext (at.ac.tuwien.kr.alpha.api.DebugSolvingContext)2 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)1 Solver (at.ac.tuwien.kr.alpha.api.Solver)1 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 AlphaImpl (at.ac.tuwien.kr.alpha.api.impl.AlphaImpl)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 Atoms (at.ac.tuwien.kr.alpha.commons.atoms.Atoms)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)1 StratificationAlgorithm (at.ac.tuwien.kr.alpha.core.depgraph.StratificationAlgorithm)1 IndexedInstanceStorage (at.ac.tuwien.kr.alpha.core.grounder.IndexedInstanceStorage)1