Search in sources :

Example 21 with Predicate

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

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

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

the class DependencyGraphImplTest method edgesEqualTest.

@Test
public void edgesEqualTest() {
    Predicate testPredicate = Predicates.getPredicate("test", 2, false, false);
    EdgeImpl e1 = new EdgeImpl(new NodeImpl(testPredicate), true);
    EdgeImpl e2 = new EdgeImpl(new NodeImpl(testPredicate), true);
    assertEquals(e1, e2);
}
Also used : Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 24 with Predicate

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

the class LiteralInstantiatorTest method workingMemoryBasedInstantiatePositiveBasicLiteral.

@Test
public void workingMemoryBasedInstantiatePositiveBasicLiteral() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("z")), true);
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(x, Terms.newSymbolicConstant("x"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
    assertEquals(2, substitutions.size());
    boolean ySubstituted = false;
    boolean zSubstituted = false;
    for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : substitutions) {
        assertTrue(resultSubstitution.left.isVariableSet(y));
        assertEquals(AssignmentStatus.TRUE, resultSubstitution.right);
        if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("y"))) {
            ySubstituted = true;
        } else if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("z"))) {
            zSubstituted = true;
        } else {
            fail("Invalid substitution for variable Y");
        }
    }
    assertTrue(ySubstituted && zSubstituted);
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

Example 25 with Predicate

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

the class FixedInterpretationLiteralsTest method positiveExternalBindingOutput.

@Test
public void positiveExternalBindingOutput() {
    Optional<AnswerSet> answer = this.alpha.solve(this.alpha.readProgramString(TEST_PROG, this.externals)).findFirst();
    assertTrue(answer.isPresent());
    AnswerSet answerSet = answer.get();
    Predicate pred = Predicates.getPredicate("positive_external_binding_output", 2);
    assertTrue(answerSet.getPredicates().contains(pred));
    Set<Atom> instances = answerSet.getPredicateInstances(pred);
    assertEquals(3, instances.size());
    assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Klagenfurt"), Terms.newConstant("Villach"))));
    assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Klagenfurt"), Terms.newConstant("Graz"))));
    assertTrue(instances.contains(Atoms.newBasicAtom(pred, Terms.newConstant("Villach"), Terms.newConstant("Salzburg"))));
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

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