Search in sources :

Example 6 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithFalseInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an assignment where q(a, b) is
 * assigned ThriceTruth.FALSE, so we expect an empty list from
 * {@link LiteralInstantiationStrategy#getAcceptedSubstitutions(Literal, Substitution)}.
 * Furthermore, we expect the atom q(a, b) to be added to the stale atom set.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithFalseInstance() {
    Predicate q = Predicates.getPredicate("q", 2);
    BasicAtom nonGroundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newVariable("X"));
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(q);
    workingMemory.addInstance(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b")), true);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    BasicAtom groundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"));
    atomStore.putIfAbsent(groundAtom);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(groundAtom), ThriceTruth.FALSE);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertTrue(result.isEmpty());
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(groundAtom));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 7 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class SubstitutionTest method specializeTermsFunctionTermBinding.

@Test
public void specializeTermsFunctionTermBinding() {
    Substitution substitution = new BasicSubstitution();
    substitution.put(Y, A);
    FunctionTerm groundFunctionTerm = Terms.newFunctionTerm("f", B, C);
    Instance qfBC = new Instance(groundFunctionTerm);
    Term nongroundFunctionTerm = Terms.newFunctionTerm("f", B, X);
    BasicAtom qfBX = Atoms.newBasicAtom(Predicates.getPredicate("q", 1), nongroundFunctionTerm);
    Substitution substitution1 = BasicSubstitution.specializeSubstitution(qfBX, qfBC, substitution);
    assertEquals(C, substitution1.eval(X));
    assertEquals(A, substitution1.eval(Y));
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 8 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution 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 9 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution 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 10 with BasicSubstitution

use of at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution in project Alpha by alpha-asp.

the class LiteralInstantiatorTest method workingMemoryBasedInstantiatePositiveBasicLiteral.

@Test
public void workingMemoryBasedInstantiatePositiveBasicLiteral() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("y")), true);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("z")), true);
    VariableTerm x = Terms.newVariable("X");
    VariableTerm y = Terms.newVariable("Y");
    Literal lit = Literals.fromAtom(Atoms.newBasicAtom(p, x, y), true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(x, Terms.newSymbolicConstant("x"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
    List<ImmutablePair<Substitution, AssignmentStatus>> substitutions = result.getSubstitutions();
    assertEquals(2, substitutions.size());
    boolean ySubstituted = false;
    boolean zSubstituted = false;
    for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : substitutions) {
        assertTrue(resultSubstitution.left.isVariableSet(y));
        assertEquals(AssignmentStatus.TRUE, resultSubstitution.right);
        if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("y"))) {
            ySubstituted = true;
        } else if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("z"))) {
            zSubstituted = true;
        } else {
            fail("Invalid substitution for variable Y");
        }
    }
    assertTrue(ySubstituted && zSubstituted);
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) 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) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

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