Search in sources :

Example 1 with BasicSubstitution

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

the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralSatisfied.

@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralSatisfied() {
    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);
    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"));
    substitution.put(y, Terms.newSymbolicConstant("y"));
    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(1, substitutions.size());
    assertEquals(AssignmentStatus.TRUE, substitutions.get(0).right);
    Substitution resultSubstitution = substitutions.get(0).left;
    // With the given input substitution, lit is ground and satisfied -
    // we expect the instantiator to verify that.
    assertEquals(substitution, resultSubstitution);
}
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) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) 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) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 2 with BasicSubstitution

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

the class LiteralInstantiatorTest method workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied.

@Test
public void workingMemoryBasedVerifyPositiveGroundLiteralUnsatisfied() {
    Predicate p = Predicates.getPredicate("p", 2);
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(p);
    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"));
    substitution.put(y, Terms.newSymbolicConstant("y"));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(workingMemory));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    // With the given input substitution, lit is ground, but not satisfied -
    // we expect the instantiator to verify that and return an empty list of
    // substitutions.
    assertEquals(LiteralInstantiationResult.Type.STOP_BINDING, result.getType());
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) 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) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 3 with BasicSubstitution

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

the class LiteralInstantiatorTest method instantiateUnsatisfiedFixedInterpretationLiteral.

@Test
public void instantiateUnsatisfiedFixedInterpretationLiteral() {
    ComparisonAtom fiveEqualsThree = Atoms.newComparisonAtom(Terms.newVariable("FIVE"), Terms.newVariable("THREE"), ComparisonOperators.EQ);
    Literal lit = Literals.fromAtom(fiveEqualsThree, true);
    Substitution substitution = new BasicSubstitution();
    substitution.put(Terms.newVariable("FIVE"), Terms.newConstant(5));
    substitution.put(Terms.newVariable("THREE"), Terms.newConstant(3));
    LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(null));
    LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
    assertEquals(LiteralInstantiationResult.Type.STOP_BINDING, result.getType());
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) 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) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) Test(org.junit.jupiter.api.Test)

Example 4 with BasicSubstitution

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

the class LiteralInstantiatorTest method instantiateSatisfiedFixedInterpretationLiteral.

@Test
public void instantiateSatisfiedFixedInterpretationLiteral() {
    ComparisonAtom equalsThree = Atoms.newComparisonAtom(Terms.newConstant(3), Terms.newVariable("THREE"), ComparisonOperators.EQ);
    Literal lit = Literals.fromAtom(equalsThree, true);
    Substitution substitution = new BasicSubstitution();
    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);
    Substitution extendedSubstitution = resultSubstitutions.get(0).left;
    assertTrue(extendedSubstitution.isVariableSet(Terms.newVariable("THREE")));
    assertEquals(Terms.newConstant(3), extendedSubstitution.eval(Terms.newVariable("THREE")));
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) 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) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) Test(org.junit.jupiter.api.Test)

Example 5 with BasicSubstitution

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find ground
 * instances for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy does not have an assignment set (as
 * is the case when {@link NaiveGrounder} is in bootstrap), so we expect the
 * assignment status (i.e. assignment status of the found ground instance)
 * passed back with the substitution to be TRUE. Furthermore, the stale atom set
 * (used by {@link NaiveGrounder} to clean up atoms that should be deleted from
 * working memory) must stay empty.
 */
@Test
public void defaultLazyGroundingNoAssignmentSubstituteNonGroundLiteral() {
    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);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, new AtomStoreImpl(), Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(null);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertEquals(1, result.size());
    ImmutablePair<Substitution, AssignmentStatus> substitutionInfo = result.get(0);
    Substitution substitution = substitutionInfo.left;
    AssignmentStatus assignmentStatus = substitutionInfo.right;
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertTrue(staleSet.isEmpty());
}
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) 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) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) 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) 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