use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class StratifiedEvaluation method calculateSatisfyingSubstitutionsForRule.
private List<Substitution> calculateSatisfyingSubstitutionsForRule(CompiledRule rule, boolean checkAllStartingLiterals) {
LOGGER.debug("Grounding rule {}", rule);
RuleGroundingInfo groundingOrders = rule.getGroundingInfo();
// Treat rules with fixed instantiation first.
LOGGER.debug("Is fixed rule? {}", rule.getGroundingInfo().hasFixedInstantiation());
if (groundingOrders.hasFixedInstantiation()) {
RuleGroundingOrder fixedGroundingOrder = groundingOrders.getFixedGroundingOrder();
return calcSubstitutionsWithGroundingOrder(fixedGroundingOrder, Collections.singletonList(new BasicSubstitution()));
}
List<Literal> startingLiterals = groundingOrders.getStartingLiterals();
// Check only one starting literal if indicated by the parameter.
if (!checkAllStartingLiterals) {
// If this is the first evaluation run, it suffices to start from the first starting literal only.
Literal lit = startingLiterals.get(0);
return calcSubstitutionsWithGroundingOrder(groundingOrders.orderStartingFrom(lit), substituteFromRecentlyAddedInstances(lit));
}
// Ground from all starting literals.
// Collection of full ground substitutions for the given rule.
List<Substitution> groundSubstitutions = new ArrayList<>();
for (Literal lit : startingLiterals) {
List<Substitution> substitutionsForStartingLiteral = calcSubstitutionsWithGroundingOrder(groundingOrders.orderStartingFrom(lit), substituteFromRecentlyAddedInstances(lit));
groundSubstitutions.addAll(substitutionsForStartingLiteral);
}
return groundSubstitutions;
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class SubstitutionTest method substituteBasicAtomLiteral.
private void substituteBasicAtomLiteral(boolean negated) {
Predicate p = Predicates.getPredicate("p", 2);
BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
Literal literal = Literals.fromAtom(atom, !negated);
Substitution substitution = new BasicSubstitution();
substitution.put(X, A);
substitution.put(Y, B);
literal = literal.substitute(substitution);
assertEquals(p, literal.getPredicate());
assertEquals(A, literal.getTerms().get(0));
assertEquals(B, literal.getTerms().get(1));
assertEquals(negated, literal.isNegated());
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class SubstitutionTest method putSimpleBinding.
@Test
public void putSimpleBinding() {
Substitution substitution = new BasicSubstitution();
substitution.put(Y, A);
assertEquals(A, substitution.eval(Y));
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.
the class Substitutions method fromString.
public static Substitution fromString(String str) {
String bare = str.substring(1, str.length() - 1);
String[] assignments = bare.split(",");
BasicSubstitution ret = new BasicSubstitution();
for (String assignment : assignments) {
String[] keyVal = assignment.split("->");
VariableTerm variable = Terms.newVariable(keyVal[0]);
Term assignedTerm = PROGRAM_PART_PARSER.parseTerm(keyVal[1]);
ret.put(variable, assignedTerm);
}
return ret;
}
Aggregations