Search in sources :

Example 61 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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));
}
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 62 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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));
}
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 63 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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));
}
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 64 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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));
}
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 65 with Predicate

use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.

the class ChoiceGrounder method assignmentToAnswerSet.

@Override
public AnswerSet assignmentToAnswerSet(Iterable<Integer> trueAtoms) {
    SortedSet<Predicate> trueAtomPredicates = new TreeSet<>();
    for (int trueAtom : trueAtoms) {
        Predicate atomPredicate = atomStore.get(trueAtom).getPredicate();
        if (!filter.test(atomPredicate)) {
            continue;
        }
        if (atomPredicate.isInternal()) {
            continue;
        }
        trueAtomPredicates.add(atomPredicate);
    }
    // Add the atom instances
    Map<Predicate, SortedSet<Atom>> predicateInstances = new HashMap<>();
    for (Predicate trueAtomPredicate : trueAtomPredicates) {
        BasicAtom basicAtom = Atoms.newBasicAtom(trueAtomPredicate);
        predicateInstances.put(trueAtomPredicate, new TreeSet<>(singleton(basicAtom)));
    }
    // only one predicate instance representing 0 terms.
    return AnswerSets.newAnswerSet(trueAtomPredicates, predicateInstances);
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Aggregations

Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)69 Test (org.junit.jupiter.api.Test)37 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)27 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)19 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)15 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)13 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 LinkedHashSet (java.util.LinkedHashSet)13 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)11 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)10 ArrayList (java.util.ArrayList)10 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)9 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)8 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)8 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)8 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)8 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)8 HashMap (java.util.HashMap)8