Search in sources :

Example 1 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm 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 VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm 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 VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm 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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) InlineDirectives(at.ac.tuwien.kr.alpha.api.programs.InlineDirectives) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) CharStreams(org.antlr.v4.runtime.CharStreams) CharStream(org.antlr.v4.runtime.CharStream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ChoiceHead(at.ac.tuwien.kr.alpha.api.rules.heads.ChoiceHead) IOException(java.io.IOException) Atoms(at.ac.tuwien.kr.alpha.commons.atoms.Atoms) Test(org.junit.jupiter.api.Test) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) List(java.util.List) Predicates(at.ac.tuwien.kr.alpha.commons.Predicates) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Util(at.ac.tuwien.kr.alpha.commons.util.Util) Optional(java.util.Optional) Collections(java.util.Collections) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) 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) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom) Test(org.junit.jupiter.api.Test)

Example 4 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm in project Alpha by alpha-asp.

the class ArithmeticTermsRewriting method rewriteArithmeticSubterms.

private Term rewriteArithmeticSubterms(Term term, List<Literal> bodyLiterals) {
    // Keep term as-is if it contains no ArithmeticTerm.
    if (!containsArithmeticTerm(term)) {
        return term;
    }
    // Switch on term type.
    if (term instanceof ArithmeticTerm) {
        VariableTerm replacementVariable = Terms.newVariable(ARITHMETIC_VARIABLES_PREFIX + numArithmeticVariables++);
        bodyLiterals.add(Atoms.newComparisonAtom(replacementVariable, term, ComparisonOperators.EQ).toLiteral());
        return replacementVariable;
    } else if (term instanceof VariableTerm || term instanceof ConstantTerm) {
        return term;
    } else if (term instanceof FunctionTerm) {
        List<Term> termList = ((FunctionTerm) term).getTerms();
        List<Term> rewrittenTermList = new ArrayList<>();
        for (Term subterm : termList) {
            rewrittenTermList.add(rewriteArithmeticSubterms(subterm, bodyLiterals));
        }
        return Terms.newFunctionTerm(((FunctionTerm) term).getSymbol(), rewrittenTermList);
    } else {
        throw Util.oops("Rewriting unknown Term type: " + term.getClass());
    }
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 5 with VariableTerm

use of at.ac.tuwien.kr.alpha.api.terms.VariableTerm in project Alpha by alpha-asp.

the class AggregateOperatorNormalization method rewriteAggregateOperator.

private static List<Literal> rewriteAggregateOperator(AggregateLiteral lit) {
    AggregateAtom atom = lit.getAtom();
    if (atom.getLowerBoundOperator() == null && atom.getUpperBoundOperator() != null) {
        return rewriteAggregateOperator(convertToLeftHandComparison(lit));
    }
    if (lit.getAtom().getAggregateFunction() == AggregateFunctionSymbol.MIN || lit.getAtom().getAggregateFunction() == AggregateFunctionSymbol.MAX) {
        // No operator normalization needed for #min/#max aggregates.
        return Collections.singletonList(lit);
    }
    if (atom.getLowerBoundOperator().equals(ComparisonOperators.EQ) || atom.getLowerBoundOperator().equals(ComparisonOperators.LE)) {
        // Nothing to do for operator "=" or "<=".
        return Collections.singletonList(lit);
    } else {
        List<Literal> retVal = new ArrayList<>();
        VariableTerm decrementedBound;
        ComparisonOperator lowerBoundOp = atom.getLowerBoundOperator();
        if (lowerBoundOp.equals(ComparisonOperators.LT)) {
            decrementedBound = Terms.newAnonymousVariable();
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.LE, decrementedBound, atom, !lit.isNegated()));
            retVal.add(createPlusOneTerm(atom.getLowerBoundTerm(), decrementedBound));
        } else if (lowerBoundOp.equals(ComparisonOperators.NE)) {
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.EQ, atom.getLowerBoundTerm(), atom, lit.isNegated()));
        } else if (lowerBoundOp.equals(ComparisonOperators.GT)) {
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.LE, atom.getLowerBoundTerm(), atom, lit.isNegated()));
        } else if (lowerBoundOp.equals(ComparisonOperators.GE)) {
            decrementedBound = Terms.newAnonymousVariable();
            retVal.add(createLowerBoundedAggregateLiteral(ComparisonOperators.LE, decrementedBound, atom, lit.isNegated()));
            retVal.add(createPlusOneTerm(atom.getLowerBoundTerm(), decrementedBound));
        } else {
            throw new IllegalStateException("No operator rewriting logic available for literal: " + lit);
        }
        return retVal;
    }
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Aggregations

VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)45 Term (at.ac.tuwien.kr.alpha.api.terms.Term)25 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)18 Test (org.junit.jupiter.api.Test)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)10 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)9 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)8 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)6 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)6 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)6 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)6 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)6 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)6 Map (java.util.Map)6 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)5 NormalHead (at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead)5 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)4 AggregateAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)4 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)4