Search in sources :

Example 11 with Predicate

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

the class AggregateRewritingTest method sumEquals1.

@Test
public void sumEquals1() {
    List<AnswerSet> answerSets = NORMALIZE_AND_SOLVE.apply(SUM_EQ1_ASP);
    assertEquals(1, answerSets.size());
    AnswerSet answerSet = answerSets.get(0);
    Predicate sumThings = Predicates.getPredicate("sum_things", 1);
    // System.out.println(new SimpleAnswerSetFormatter("\n").format(answerSet));
    assertTrue(answerSet.getPredicates().contains(sumThings));
    assertEquals(1, answerSet.getPredicateInstances(sumThings).size());
    assertTrue(answerSet.getPredicateInstances(sumThings).contains(Atoms.newBasicAtom(sumThings, Terms.newConstant(12))));
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 12 with Predicate

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

the class AggregateRewritingTest method countEqSimple.

@Test
public void countEqSimple() {
    List<AnswerSet> answerSets = NORMALIZE_AND_SOLVE.apply(CNT_EQ1_ASP);
    assertEquals(1, answerSets.size());
    AnswerSet answerSet = answerSets.get(0);
    Predicate thing = Predicates.getPredicate("thing", 1);
    Predicate cntThings = Predicates.getPredicate("cnt_things", 1);
    // System.out.println(new SimpleAnswerSetFormatter("\n").format(answerSet));
    assertTrue(answerSet.getPredicateInstances(thing).contains(Atoms.newBasicAtom(thing, Terms.newConstant(4))));
    assertTrue(answerSet.getPredicateInstances(thing).contains(Atoms.newBasicAtom(thing, Terms.newConstant(5))));
    assertTrue(answerSet.getPredicateInstances(thing).contains(Atoms.newBasicAtom(thing, Terms.newConstant(6))));
    assertTrue(answerSet.getPredicates().contains(cntThings));
    assertTrue(answerSet.getPredicateInstances(cntThings).contains(Atoms.newBasicAtom(cntThings, Terms.newConstant(3))));
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 13 with Predicate

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

the class AggregateRewritingTest method countLeCountingGridSimple.

@Test
public void countLeCountingGridSimple() {
    List<AnswerSet> answerSets = NORMALIZE_AND_SOLVE.apply(CNT_LE1_ASP);
    assertEquals(1, answerSets.size());
    AnswerSet answerSet = answerSets.get(0);
    Predicate thing = Predicates.getPredicate("thing", 1);
    Predicate candidate = Predicates.getPredicate("candidate", 1);
    Predicate cntLe = Predicates.getPredicate("cnt_le", 1);
    // System.out.println(new SimpleAnswerSetFormatter("\n").format(answerSet));
    assertTrue(answerSet.getPredicateInstances(thing).contains(Atoms.newBasicAtom(thing, Terms.newConstant(75))));
    assertTrue(answerSet.getPredicateInstances(thing).contains(Atoms.newBasicAtom(thing, Terms.newConstant(76))));
    assertTrue(answerSet.getPredicateInstances(candidate).contains(Atoms.newBasicAtom(candidate, Terms.newConstant(2))));
    assertTrue(answerSet.getPredicateInstances(candidate).contains(Atoms.newBasicAtom(candidate, Terms.newConstant(3))));
    assertTrue(answerSet.getPredicateInstances(candidate).contains(Atoms.newBasicAtom(candidate, Terms.newConstant(4))));
    assertTrue(answerSet.getPredicates().contains(cntLe));
    assertTrue(answerSet.getPredicateInstances(cntLe).contains(Atoms.newBasicAtom(cntLe, Terms.newConstant(2))));
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 14 with Predicate

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckFalseGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to check the truth
 * (i.e. {@link AssignmentStatus}) of the positive ground literal "p(a)".
 *
 * In this case, the instantiation strategy has an assignment where the atom
 * "p(a)" is assigned ThriceTruth.FALSE, so we expect the instantiation strategy
 * to determine that p(a) is FALSE. Since UNASSIGNED and FALSE atoms are
 * (potentially) stale in working memory, we expect the atom "p(a)" to be added
 * to the stale set by the instantiation strategy.
 */
@Test
public void defaultLazyGroundingCheckFalseGroundLiteral() {
    Predicate p = Predicates.getPredicate("p", 1);
    BasicAtom pOfA = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a"));
    WorkingMemory workingMemory = new WorkingMemory();
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    atomStore.putIfAbsent(pOfA);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(pOfA), ThriceTruth.FALSE);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    AssignmentStatus assignmentStatus = strategy.getTruthForGroundLiteral(Literals.fromAtom(pOfA, true));
    assertEquals(AssignmentStatus.FALSE, assignmentStatus);
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(pOfA));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) 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) Test(org.junit.jupiter.api.Test)

Example 15 with Predicate

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find ground
 * instances for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy does not have an assignment set (as
 * is the case when {@link NaiveGrounder} is in bootstrap), so we expect the
 * assignment status (i.e. assignment status of the found ground instance)
 * passed back with the substitution to be TRUE. Furthermore, the stale atom set
 * (used by {@link NaiveGrounder} to clean up atoms that should be deleted from
 * working memory) must stay empty.
 */
@Test
public void defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral() {
    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);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, new AtomStoreImpl(), Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(null);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertEquals(1, result.size());
    ImmutablePair<Substitution, AssignmentStatus> substitutionInfo = result.get(0);
    Substitution substitution = substitutionInfo.left;
    AssignmentStatus assignmentStatus = substitutionInfo.right;
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertTrue(staleSet.isEmpty());
}
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) 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) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) 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) 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