Search in sources :

Example 21 with BasicSubstitution

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;
}
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) ArrayList(java.util.ArrayList) RuleGroundingOrder(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingOrder) RuleGroundingInfo(at.ac.tuwien.kr.alpha.core.grounder.RuleGroundingInfo)

Example 22 with BasicSubstitution

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());
}
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 23 with BasicSubstitution

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));
}
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) Test(org.junit.jupiter.api.Test)

Example 24 with BasicSubstitution

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;
}
Also used : BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term)

Aggregations

BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)24 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)18 Test (org.junit.jupiter.api.Test)13 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)9 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)9 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)8 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)7 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)7 Term (at.ac.tuwien.kr.alpha.api.terms.Term)6 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)6 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)5 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)4 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)4 LinkedHashSet (java.util.LinkedHashSet)4 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)3 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)3 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)3 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)3 ArrayList (java.util.ArrayList)3