Search in sources :

Example 26 with BasicAtom

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);
}
Also used : NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ArrayList(java.util.ArrayList) NormalRuleImpl(at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)

Example 27 with BasicAtom

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);
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 28 with BasicAtom

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());
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 29 with BasicAtom

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));
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Example 30 with BasicAtom

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);
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Aggregations

BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)30 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)16 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)15 Test (org.junit.jupiter.api.Test)15 LinkedHashSet (java.util.LinkedHashSet)12 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)11 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)10 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)9 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)9 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)9 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)8 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)7 ArrayList (java.util.ArrayList)6 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)3 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)3 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)3 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)2