use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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);
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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());
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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());
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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")));
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class ParserTest method cardinalityAggregate.
@Test
public void cardinalityAggregate() {
ASPCore2Program parsedProgram = parser.parse("num(K) :- K <= #count {X,Y,Z : p(X,Y,Z) }, dom(K).");
Optional<Literal> optionalBodyElement = parsedProgram.getRules().get(0).getBody().stream().filter((lit) -> lit instanceof AggregateLiteral).findFirst();
assertTrue(optionalBodyElement.isPresent());
Literal bodyElement = optionalBodyElement.get();
AggregateLiteral parsedAggregate = (AggregateLiteral) bodyElement;
VariableTerm x = Terms.newVariable("X");
VariableTerm y = Terms.newVariable("Y");
VariableTerm z = Terms.newVariable("Z");
List<Term> basicTerms = Arrays.asList(x, y, z);
AggregateAtom.AggregateElement aggregateElement = Atoms.newAggregateElement(basicTerms, Collections.singletonList(Atoms.newBasicAtom(Predicates.getPredicate("p", 3), x, y, z).toLiteral()));
AggregateAtom expectedAggregate = Atoms.newAggregateAtom(ComparisonOperators.LE, Terms.newVariable("K"), null, null, AggregateAtom.AggregateFunctionSymbol.COUNT, Collections.singletonList(aggregateElement));
assertEquals(expectedAggregate, parsedAggregate.getAtom());
}
Aggregations