use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralSatisfied.
@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralSatisfied() {
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);
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"));
substitution.put(y, Terms.newSymbolicConstant("y"));
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(1, substitutions.size());
assertEquals(AssignmentStatus.TRUE, substitutions.get(0).right);
Substitution resultSubstitution = substitutions.get(0).left;
// With the given input substitution, lit is ground and satisfied -
// we expect the instantiator to verify that.
assertEquals(substitution, resultSubstitution);
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied.
@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied() {
Predicate p = Predicates.getPredicate("p", 2);
WorkingMemory workingMemory = new WorkingMemory();
workingMemory.initialize(p);
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"));
substitution.put(y, Terms.newSymbolicConstant("y"));
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
// With the given input substitution, lit is ground, but not satisfied -
// we expect the instantiator to verify that and return an empty list of
// substitutions.
assertEquals(LiteralInstantiationResult.Type.STOP_BINDING, result.getType());
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testRecursiveRanking.
@Test
public void testRecursiveRanking() {
// @formatter:off
String asp = "thing(a).\n" + "thing(b).\n" + "thing(c).\n" + "thing_before(a, b).\n" + "thing_before(b, c).\n" + "has_prev_thing(X) :- thing(X), thing_succ(_, X).\n" + "first_thing(X) :- thing(X), not has_prev_thing(X).\n" + "thing_not_succ(X, Y) :-\n" + " thing(X),\n" + " thing(Y),\n" + " thing(INTM),\n" + " thing_before(X, Y),\n" + " thing_before(X, INTM),\n" + " thing_before(INTM, X).\n" + "thing_succ(X, Y) :-\n" + " thing(X),\n" + " thing(Y),\n" + " thing_before(X, Y),\n" + " not thing_not_succ(X, Y).\n" + "thing_rank(X, 1) :- first_thing(X).\n" + "thing_rank(X, R) :-\n" + " thing(X),\n" + " thing_succ(Y, X),\n" + " thing_rank(Y, K),\n" + " R = K + 1.";
// @formatter:on
CompiledProgram evaluated = parseAndEvaluate.apply(asp);
Predicate rank = Predicates.getPredicate("thing_rank", 2);
BasicAtom rank1 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("a"), Terms.newConstant(1));
BasicAtom rank2 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("b"), Terms.newConstant(2));
BasicAtom rank3 = Atoms.newBasicAtom(rank, Terms.newSymbolicConstant("c"), Terms.newConstant(3));
List<Atom> evaluatedFacts = evaluated.getFacts();
assertTrue(evaluatedFacts.contains(rank1));
assertTrue(evaluatedFacts.contains(rank2));
assertTrue(evaluatedFacts.contains(rank3));
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class StratifiedEvaluation method apply.
@Override
public // memories created here rather than re-initialize everything.
InternalProgram apply(AnalyzedProgram inputProgram) {
// Calculate a stratification and initialize the working memory.
ComponentGraph componentGraph = inputProgram.getComponentGraph();
List<ComponentGraph.SCComponent> strata = StratificationAlgorithm.calculateStratification(componentGraph);
predicateDefiningRules = inputProgram.getPredicateDefiningRules();
// Set up list of atoms which are known to be true - these will be expand by the evaluation.
Map<Predicate, Set<Instance>> knownFacts = new LinkedHashMap<>(inputProgram.getFactsByPredicate());
for (Map.Entry<Predicate, Set<Instance>> entry : knownFacts.entrySet()) {
workingMemory.initialize(entry.getKey());
workingMemory.addInstances(entry.getKey(), true, entry.getValue());
}
// Create working memories for all predicates occurring in each rule.
for (CompiledRule nonGroundRule : inputProgram.getRulesById().values()) {
for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
workingMemory.initialize(predicate);
}
}
workingMemory.reset();
// Set up literal instantiator.
literalInstantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
// Evaluate the program part covered by the calculated stratification.
for (ComponentGraph.SCComponent currComponent : strata) {
evaluateComponent(currComponent);
}
// Build the program resulting from evaluating the stratified part.
// Add original input facts to newly derived ones.
additionalFacts.addAll(inputProgram.getFacts());
List<CompiledRule> outputRules = new ArrayList<>();
inputProgram.getRulesById().entrySet().stream().filter((entry) -> !solvedRuleIds.contains(entry.getKey())).forEach((entry) -> outputRules.add(entry.getValue()));
// NOTE: if InternalProgram requires solved rules, they should be added here.
return new InternalProgram(outputRules, additionalFacts);
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class StratifiedEvaluation method getRulesToEvaluate.
private ComponentEvaluationInfo getRulesToEvaluate(ComponentGraph.SCComponent comp) {
Set<CompiledRule> nonRecursiveRules = new HashSet<>();
Set<CompiledRule> recursiveRules = new HashSet<>();
// Collect all predicates occurring in heads of rules of the given component.
Set<Predicate> headPredicates = new HashSet<>();
for (DependencyGraph.Node node : comp.getNodes()) {
headPredicates.add(node.getPredicate());
}
// cycle.
for (Predicate headPredicate : headPredicates) {
HashSet<CompiledRule> definingRules = predicateDefiningRules.get(headPredicate);
if (definingRules == null) {
// Predicate only occurs in facts, skip.
continue;
}
// Note: here we assume that all rules defining a predicate belong to the same SC component.
for (CompiledRule rule : definingRules) {
boolean isRuleRecursive = false;
for (Literal lit : rule.getPositiveBody()) {
if (headPredicates.contains(lit.getPredicate())) {
// The rule body contains a predicate that is defined in the same
// component, the rule is therefore part of a cyclic dependency within
// this component.
isRuleRecursive = true;
}
}
if (isRuleRecursive) {
recursiveRules.add(rule);
} else {
nonRecursiveRules.add(rule);
}
}
}
return new ComponentEvaluationInfo(nonRecursiveRules, recursiveRules);
}
Aggregations