Search in sources :

Example 1 with EnumerationLiteral

use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral 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 EnumerationLiteral

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

the class LiteralInstantiator method instantiateBasicLiteral.

/**
 * Calculates substitutions for a given literal that is not a {@link FixedInterpretationLiteral} or {@link EnumerationLiteral}.
 * If applying the given partial substitution to the literal already grounds the literal, the resulting ground literal is verified based on
 * this instantiators {@link LiteralInstantiationStrategy}. If the literal is only partially ground after applying the partial substitution,
 * ground substitutions are looked up using the instantiators {@link LiteralInstantiationStrategy}. This method assumes that the partial
 * substitution has <emph>not</emph> been applied to the passed literal.
 *
 * @param lit
 * @param partialSubstitution
 */
private LiteralInstantiationResult instantiateBasicLiteral(Literal lit, Substitution partialSubstitution) {
    LOGGER.trace("Instantiating basic literal: {}", lit);
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions;
    Literal substitutedLiteral = lit.substitute(partialSubstitution);
    LOGGER.trace("Substituted literal is {}", substitutedLiteral);
    if (substitutedLiteral.isGround()) {
        LOGGER.trace("Literal {} is already ground, checking truth", substitutedLiteral);
        // Lit seems to be a basic literal, so its satisfiability w.r.t.
        // partialSubstitution is decided based on knownInstances by the
        // instantiationStrategy.
        AssignmentStatus truthForLiteral = this.instantiationStrategy.getTruthForGroundLiteral(substitutedLiteral);
        if (truthForLiteral == AssignmentStatus.FALSE) {
            return LiteralInstantiationResult.stopBinding();
        } else {
            return LiteralInstantiationResult.continueBinding(partialSubstitution, truthForLiteral);
        }
    } else {
        LOGGER.trace("Handling non-ground literal {}", substitutedLiteral);
        if (substitutedLiteral.isNegated()) {
            return LiteralInstantiationResult.maybePushBack();
        }
        // Query instantiationStrategy for acceptable substitutions.
        // Note: getAcceptedSubstitutions will only give substitutions where the
        // resulting ground atom is true or unassigned, false atoms are internally
        // discarded.
        substitutions = this.instantiationStrategy.getAcceptedSubstitutions(substitutedLiteral, partialSubstitution);
        LOGGER.trace("Got {} substitutions from instantiation strategy for {}", substitutions.size(), substitutedLiteral);
        return substitutions.isEmpty() ? LiteralInstantiationResult.maybePushBack() : LiteralInstantiationResult.continueBinding(substitutions);
    }
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) IntervalLiteral(at.ac.tuwien.kr.alpha.core.atoms.IntervalLiteral) FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)

Aggregations

EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 ExternalLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral)1 FixedInterpretationLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 EnumerationAtom (at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom)1 IntervalLiteral (at.ac.tuwien.kr.alpha.core.atoms.IntervalLiteral)1 Test (org.junit.jupiter.api.Test)1