use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class ArithmeticTermsRewriting method rewriteRule.
/**
* Takes a normal rule and rewrites it such that {@link ArithmeticTerm}s only appear inside {@link at.ac.tuwien.kr.alpha.common.atoms.ComparisonLiteral}s.
*
* @param inputProgramRule the rule to rewrite.
* @return the rewritten rule. Note that a new {@link NormalRule} is returned for every call of this method.
*/
private NormalRule rewriteRule(NormalRule inputProgramRule) {
// Reset number of introduced variables for each rule.
numArithmeticVariables = 0;
NormalHead rewrittenHead = null;
List<Literal> rewrittenBodyLiterals = new ArrayList<>();
// Rewrite head.
if (!inputProgramRule.isConstraint()) {
BasicAtom headAtom = inputProgramRule.getHeadAtom();
if (containsArithmeticTermsToRewrite(headAtom)) {
rewrittenHead = Heads.newNormalHead((BasicAtom) rewriteAtom(headAtom, rewrittenBodyLiterals));
} else {
rewrittenHead = inputProgramRule.getHead();
}
}
// Rewrite body.
for (Literal literal : inputProgramRule.getBody()) {
if (!containsArithmeticTermsToRewrite(literal.getAtom())) {
// Keep body literal as-is if no ArithmeticTerm occurs.
rewrittenBodyLiterals.add(literal);
continue;
}
rewrittenBodyLiterals.add(rewriteAtom(literal.getAtom(), rewrittenBodyLiterals).toLiteral(!literal.isNegated()));
}
return new NormalRuleImpl(rewrittenHead, rewrittenBodyLiterals);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class ChoiceGrounder method assignmentToAnswerSet.
@Override
public AnswerSet assignmentToAnswerSet(Iterable<Integer> trueAtoms) {
SortedSet<Predicate> trueAtomPredicates = new TreeSet<>();
for (int trueAtom : trueAtoms) {
Predicate atomPredicate = atomStore.get(trueAtom).getPredicate();
if (!filter.test(atomPredicate)) {
continue;
}
if (atomPredicate.isInternal()) {
continue;
}
trueAtomPredicates.add(atomPredicate);
}
// Add the atom instances
Map<Predicate, SortedSet<Atom>> predicateInstances = new HashMap<>();
for (Predicate trueAtomPredicate : trueAtomPredicates) {
BasicAtom basicAtom = Atoms.newBasicAtom(trueAtomPredicate);
predicateInstances.put(trueAtomPredicate, new TreeSet<>(singleton(basicAtom)));
}
// only one predicate instance representing 0 terms.
return AnswerSets.newAnswerSet(trueAtomPredicates, predicateInstances);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom 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.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class UnifierTest method extendUnifier.
@Test
public void extendUnifier() {
VariableTerm varX = Terms.newVariable("X");
VariableTerm varY = Terms.newVariable("Y");
Unifier sub1 = new Unifier();
sub1.put(varX, varY);
Unifier sub2 = new Unifier();
sub2.put(varY, Terms.newConstant("a"));
sub1.extendWith(sub2);
BasicAtom atom1 = parseAtom("p(X)");
Atom atomSubstituted = atom1.substitute(sub1);
assertEquals(Terms.newConstant("a"), atomSubstituted.getTerms().get(0));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom in project Alpha by alpha-asp.
the class DummyGrounder method assignmentToAnswerSet.
@Override
public AnswerSet assignmentToAnswerSet(Iterable<Integer> trueAtoms) {
// Note: This grounder only deals with 0-ary predicates, i.e., every atom is a predicate and there is
// only one predicate instance representing 0 terms.
SortedSet<Predicate> trueAtomPredicates = new TreeSet<>();
for (int trueAtom : trueAtoms) {
Predicate atomPredicate = atomStore.get(trueAtom).getPredicate();
if (!filter.test(atomPredicate)) {
continue;
}
if (atomPredicate.isInternal()) {
continue;
}
trueAtomPredicates.add(atomPredicate);
}
// Add the atom instances
Map<Predicate, SortedSet<Atom>> predicateInstances = new HashMap<>();
for (Predicate trueAtomPredicate : trueAtomPredicates) {
BasicAtom internalBasicAtom = Atoms.newBasicAtom(trueAtomPredicate);
predicateInstances.put(trueAtomPredicate, new TreeSet<>(singleton(internalBasicAtom)));
}
return AnswerSets.newAnswerSet(trueAtomPredicates, predicateInstances);
}
Aggregations