Search in sources :

Example 16 with Predicate

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithFalseInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an assignment where q(a, b) is
 * assigned ThriceTruth.FALSE, so we expect an empty list from
 * {@link LiteralInstantiationStrategy#getAcceptedSubstitutions(Literal, Substitution)}.
 * Furthermore, we expect the atom q(a, b) to be added to the stale atom set.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithFalseInstance() {
    Predicate q = Predicates.getPredicate("q", 2);
    BasicAtom nonGroundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newVariable("X"));
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(q);
    workingMemory.addInstance(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b")), true);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    BasicAtom groundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"));
    atomStore.putIfAbsent(groundAtom);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(groundAtom), ThriceTruth.FALSE);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertTrue(result.isEmpty());
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(groundAtom));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 17 with Predicate

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

the class SubstitutionTest method specializeBasicAtom.

@Test
public void specializeBasicAtom() {
    Predicate p = Predicates.getPredicate("p", 2);
    BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
    Instance instance = new Instance(A, B);
    Substitution substitution = BasicSubstitution.specializeSubstitution(atom, instance, BasicSubstitution.EMPTY_SUBSTITUTION);
    BasicAtom substituted = atom.substitute(substitution);
    assertEquals(p, substituted.getPredicate());
    assertEquals(A, substituted.getTerms().get(0));
    assertEquals(B, substituted.getTerms().get(1));
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 18 with Predicate

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

the class SubstitutionTest method groundLiteralToString.

private void groundLiteralToString(boolean negated) {
    Predicate p = Predicates.getPredicate("p", 2);
    BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
    Substitution substitution1 = BasicSubstitution.specializeSubstitution(PX, PA, BasicSubstitution.EMPTY_SUBSTITUTION);
    Substitution substitution = BasicSubstitution.specializeSubstitution(PY, PB, substitution1);
    String printedString = SubstitutionTestUtil.groundLiteralToString(atom.toLiteral(!negated), substitution, true);
    assertEquals((negated ? "not " : "") + "p(a, b)", printedString);
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 19 with Predicate

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

the class TestUtils method basicAtomWithSymbolicTerms.

public static BasicAtom basicAtomWithSymbolicTerms(String predicate, String... constantSymbols) {
    Predicate pred = Predicates.getPredicate(predicate, constantSymbols.length);
    List<Term> trms = new ArrayList<>();
    for (String str : constantSymbols) {
        trms.add(Terms.newSymbolicConstant(str));
    }
    return Atoms.newBasicAtom(pred, trms);
}
Also used : ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 20 with Predicate

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

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