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