Search in sources :

Example 1 with EnumerationAtom

use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom in project Alpha by alpha-asp.

the class LiteralInstantiatorTest method instantiateEnumLiteral.

@Test
public void instantiateEnumLiteral() {
    VariableTerm enumTerm = Terms.newVariable("E");
    VariableTerm idTerm = Terms.newVariable("X");
    VariableTerm indexTerm = Terms.newVariable("I");
    EnumerationAtom enumAtom = new EnumerationAtom(enumTerm, idTerm, indexTerm);
    EnumerationLiteral lit = new EnumerationLiteral(enumAtom);
    Substitution substitution = new BasicSubstitution();
    substitution.put(enumTerm, Terms.newSymbolicConstant("enum1"));
    substitution.put(idTerm, Terms.newSymbolicConstant("someElement"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(null));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
    List<ImmutablePair<Substitution, AssignmentStatus>> resultSubstitutions = result.getSubstitutions();
    assertEquals(1, resultSubstitutions.size());
    assertEquals(AssignmentStatus.TRUE, resultSubstitutions.get(0).right);
    assertTrue(resultSubstitutions.get(0).left.isVariableSet(indexTerm));
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) Test(org.junit.jupiter.api.Test)

Example 2 with EnumerationAtom

use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom in project Alpha by alpha-asp.

the class NoGoodGenerator method collectPosLiterals.

private List<Integer> collectPosLiterals(final CompiledRule nonGroundRule, final Substitution substitution) {
    final List<Integer> bodyLiteralsPositive = new ArrayList<>();
    for (Literal lit : nonGroundRule.getPositiveBody()) {
        if (lit instanceof FixedInterpretationLiteral) {
            // FixedInterpretationAtoms need not be shown to the solver, skip it.
            continue;
        }
        final Atom atom = lit.getAtom();
        // Skip the special enumeration atom.
        if (atom instanceof EnumerationAtom) {
            continue;
        }
        final Atom groundAtom = atom.substitute(substitution);
        // Consider facts to eliminate ground atoms from the generated nogoods that are always true
        // and eliminate nogoods that are always satisfied due to facts.
        Set<Instance> factInstances = factsFromProgram.get(groundAtom.getPredicate());
        if (factInstances != null && factInstances.contains(new Instance(groundAtom.getTerms()))) {
            // Skip positive atoms that are always true.
            continue;
        }
        if (!existsRuleWithPredicateInHead(groundAtom.getPredicate())) {
            // Atom is no fact and no rule defines it, it cannot be derived (i.e., is always false), skip whole rule as it will never fire.
            return null;
        }
        bodyLiteralsPositive.add(atomToLiteral(atomStore.putIfAbsent(groundAtom)));
    }
    return bodyLiteralsPositive;
}
Also used : FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Literals.negateLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.negateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) ArrayList(java.util.ArrayList) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Example 3 with EnumerationAtom

use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom in project Alpha by alpha-asp.

the class EnumerationRewriting method rewriteRules.

private List<Rule<Head>> rewriteRules(List<Rule<Head>> srcRules, Predicate enumPredicate) {
    List<Rule<Head>> rewrittenRules = new ArrayList<>();
    for (Rule<Head> rule : srcRules) {
        if (rule.getHead() != null && !(rule.getHead() instanceof NormalHead)) {
            throw oops("Encountered rule whose head is not normal: " + rule);
        }
        if (rule.getHead() != null && ((NormalHead) rule.getHead()).getAtom().getPredicate().equals(enumPredicate)) {
            throw oops("Atom declared as enumeration atom by directive occurs in head of the rule: " + rule);
        }
        List<Literal> modifiedBodyLiterals = new ArrayList<>(rule.getBody());
        Iterator<Literal> rit = modifiedBodyLiterals.iterator();
        LinkedList<Literal> rewrittenLiterals = new LinkedList<>();
        while (rit.hasNext()) {
            Literal literal = rit.next();
            if (!(literal instanceof BasicLiteral)) {
                continue;
            }
            BasicLiteral basicLiteral = (BasicLiteral) literal;
            if (!basicLiteral.getPredicate().equals(enumPredicate)) {
                continue;
            }
            // basicLiteral is an enumeration literal (i.e. predicate is marked as enum using directive)
            rit.remove();
            Term enumIdTerm = basicLiteral.getAtom().getTerms().get(0);
            Term valueTerm = basicLiteral.getAtom().getTerms().get(1);
            VariableTerm indexTerm = (VariableTerm) basicLiteral.getAtom().getTerms().get(2);
            rewrittenLiterals.add(new EnumerationAtom(enumIdTerm, valueTerm, indexTerm).toLiteral());
        }
        modifiedBodyLiterals.addAll(rewrittenLiterals);
        rewrittenRules.add(new BasicRule(rule.getHead(), modifiedBodyLiterals));
    }
    return rewrittenRules;
}
Also used : BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) LinkedList(java.util.LinkedList) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) BasicLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) BasicRule(at.ac.tuwien.kr.alpha.core.rules.BasicRule)

Aggregations

EnumerationAtom (at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom)3 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)2 ArrayList (java.util.ArrayList)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)1 BasicLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.BasicLiteral)1 FixedInterpretationLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral)1 Rule (at.ac.tuwien.kr.alpha.api.rules.Rule)1 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)1 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)1 Term (at.ac.tuwien.kr.alpha.api.terms.Term)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)1 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)1 Literals.atomToLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral)1 Literals.negateLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.negateLiteral)1 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)1 BasicRule (at.ac.tuwien.kr.alpha.core.rules.BasicRule)1 LinkedList (java.util.LinkedList)1