Search in sources :

Example 11 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class SubstitutionTest method substitutionFromString.

@Test
public void substitutionFromString() {
    Rule<Head> rule = PARSER.parse("x :- p(X,Y), not q(X,Y).").getRules().get(0);
    CompiledRule nonGroundRule = InternalRule.fromNormalRule(NormalRuleImpl.fromBasicRule(rule));
    Substitution substitution1 = BasicSubstitution.specializeSubstitution(PX, PA, BasicSubstitution.EMPTY_SUBSTITUTION);
    Substitution substitution = BasicSubstitution.specializeSubstitution(PY, PB, substitution1);
    RuleAtom ruleAtom = new RuleAtom(nonGroundRule, substitution);
    String substitutionString = (String) ((ConstantTerm<?>) ruleAtom.getTerms().get(1)).getObject();
    Substitution fromString = Substitutions.fromString(substitutionString);
    assertEquals(substitution, fromString);
}
Also used : Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) Test(org.junit.jupiter.api.Test)

Example 12 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class SubstitutionTest method groundLiteralToString.

private void groundLiteralToString(boolean negated) {
    Predicate p = Predicates.getPredicate("p", 2);
    BasicAtom atom = Atoms.newBasicAtom(p, Arrays.asList(X, Y));
    Substitution substitution1 = BasicSubstitution.specializeSubstitution(PX, PA, BasicSubstitution.EMPTY_SUBSTITUTION);
    Substitution substitution = BasicSubstitution.specializeSubstitution(PY, PB, substitution1);
    String printedString = SubstitutionTestUtil.groundLiteralToString(atom.toLiteral(!negated), substitution, true);
    assertEquals((negated ? "not " : "") + "p(a, b)", printedString);
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 13 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class DefaultSolver method treatConflictAfterClosing.

private boolean treatConflictAfterClosing(Antecedent violatedNoGood) {
    if (disableJustificationAfterClosing || disableJustifications || !(grounder instanceof ProgramAnalyzingGrounder)) {
        // Will not learn from violated NoGood, do simple backtrack.
        LOGGER.debug("NoGood was violated after all unassigned atoms were assigned to false; will not learn from it; skipping.");
        if (!backtrack()) {
            logStats();
            return false;
        }
        return true;
    }
    ProgramAnalyzingGrounder analyzingGrounder = (ProgramAnalyzingGrounder) grounder;
    LOGGER.debug("Justifying atoms in violated nogood.");
    LinkedHashSet<Integer> toJustify = new LinkedHashSet<>();
    // Find those literals in violatedNoGood that were just assigned false.
    for (Integer literal : violatedNoGood.getReasonLiterals()) {
        if (assignment.getImpliedBy(atomOf(literal)) == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
            toJustify.add(literal);
        }
    }
    // Since the violatedNoGood may contain atoms other than BasicAtom, these have to be treated.
    Map<Integer, NoGood> obtained = new LinkedHashMap<>();
    Iterator<Integer> toJustifyIterator = toJustify.iterator();
    ArrayList<Integer> ruleAtomReplacements = new ArrayList<>();
    while (toJustifyIterator.hasNext()) {
        Integer literal = toJustifyIterator.next();
        Atom atom = atomStore.get(atomOf(literal));
        if (atom instanceof BasicAtom) {
            continue;
        }
        if (!(atom instanceof RuleAtom)) {
            // Ignore atoms other than RuleAtom.
            toJustifyIterator.remove();
            continue;
        }
        // For RuleAtoms in toJustify the corresponding ground body contains BasicAtoms that have been assigned FALSE in the closing.
        // First, translate RuleAtom back to NonGroundRule + Substitution.
        String ruleId = (String) ((ConstantTerm<?>) atom.getTerms().get(0)).getObject();
        CompiledRule nonGroundRule = analyzingGrounder.getNonGroundRule(Integer.parseInt(ruleId));
        String substitution = (String) ((ConstantTerm<?>) atom.getTerms().get(1)).getObject();
        Substitution groundingSubstitution = Substitutions.fromString(substitution);
        // Find ground literals in the body that have been assigned false and justify those.
        for (Literal bodyLiteral : nonGroundRule.getBody()) {
            Atom groundAtom = bodyLiteral.getAtom().substitute(groundingSubstitution);
            if (groundAtom instanceof ComparisonAtom || analyzingGrounder.isFact(groundAtom)) {
                // Facts and ComparisonAtoms are always true, no justification needed.
                continue;
            }
            int groundAtomId = atomStore.get(groundAtom);
            Antecedent impliedBy = assignment.getImpliedBy(groundAtomId);
            // Check if atom was assigned to FALSE during the closing.
            if (impliedBy == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
                ruleAtomReplacements.add(atomToNegatedLiteral(groundAtomId));
            }
        }
        toJustifyIterator.remove();
    }
    toJustify.addAll(ruleAtomReplacements);
    for (Integer literalToJustify : toJustify) {
        LOGGER.debug("Searching for justification(s) of {} / {}", toJustify, atomStore.atomToString(atomOf(literalToJustify)));
        Set<Literal> reasonsForUnjustified = analyzingGrounder.justifyAtom(atomOf(literalToJustify), assignment);
        NoGood noGood = noGoodFromJustificationReasons(atomOf(literalToJustify), reasonsForUnjustified);
        int noGoodID = grounder.register(noGood);
        obtained.put(noGoodID, noGood);
        LOGGER.debug("Learned NoGood is: {}", atomStore.noGoodToString(noGood));
    }
    // Backtrack to remove the violation.
    if (!backtrack()) {
        logStats();
        return false;
    }
    // Add newly obtained noGoods.
    if (!ingest(obtained)) {
        logStats();
        return false;
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) ProgramAnalyzingGrounder(at.ac.tuwien.kr.alpha.core.grounder.ProgramAnalyzingGrounder) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) ArrayList(java.util.ArrayList) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) LinkedHashMap(java.util.LinkedHashMap) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToNegatedLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Example 14 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution in project Alpha by alpha-asp.

the class NoGoodGeneratorTest method collectNeg_ContainsOnlyPositiveLiterals.

/**
 * Calls {@link NoGoodGenerator#collectNegLiterals(InternalRule, Substitution)}, which puts the atom occurring
 * negatively in a rule into the atom store. It is then checked whether the atom in the atom store is positive.
 */
@Test
public void collectNeg_ContainsOnlyPositiveLiterals() {
    ASPCore2Program input = PARSER.parse("p(a,b). " + "q(a,b) :- not nq(a,b). " + "nq(a,b) :- not q(a,b).");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(input);
    CompiledProgram program = InternalProgram.fromNormalProgram(normal);
    CompiledRule rule = program.getRules().get(1);
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", program, atomStore, true);
    NoGoodGenerator noGoodGenerator = ((NaiveGrounder) grounder).noGoodGenerator;
    Substitution substitution = new BasicSubstitution();
    substitution.put(X, A);
    substitution.put(Y, B);
    List<Integer> collectedNeg = noGoodGenerator.collectNegLiterals(rule, substitution);
    assertEquals(1, collectedNeg.size());
    String negAtomString = atomStore.atomToString(Literals.atomOf(collectedNeg.get(0)));
    assertEquals("q(a, b)", negAtomString);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) Test(org.junit.jupiter.api.Test)

Example 15 with Substitution

use of at.ac.tuwien.kr.alpha.api.grounder.Substitution 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)

Aggregations

Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)34 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)33 Test (org.junit.jupiter.api.Test)19 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)13 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)10 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)8 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)8 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)8 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)7 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)7 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)6 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)6 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)6 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)6 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)6 ArrayList (java.util.ArrayList)6 BindingResult (at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult)5 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)4 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)4