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))));
}
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))));
}
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))));
}
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));
}
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());
}
Aggregations