Search in sources :

Example 6 with Term

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

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

the class UnifierTest method mergeUnifierIntoLeft.

@Test
public void mergeUnifierIntoLeft() {
    VariableTerm varX = Terms.newVariable("X");
    VariableTerm varY = Terms.newVariable("Y");
    VariableTerm varZ = Terms.newVariable("Z");
    Term constA = Terms.newConstant("a");
    Unifier left = new Unifier();
    left.put(varX, varY);
    left.put(varZ, varY);
    Unifier right = new Unifier();
    right.put(varX, constA);
    Unifier merged = Unifier.mergeIntoLeft(left, right);
    assertEquals(constA, merged.eval(varY));
    assertEquals(constA, merged.eval(varZ));
}
Also used : VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier) Test(org.junit.jupiter.api.Test)

Example 8 with Term

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

the class ParseTreeVisitor method visitTerm_interval.

@Override
public IntervalTerm visitTerm_interval(ASPCore2Parser.Term_intervalContext ctx) {
    // interval : lower = (NUMBER | VARIABLE) DOT DOT upper = (NUMBER | VARIABLE);
    ASPCore2Parser.IntervalContext ictx = ctx.interval();
    String lowerText = ictx.lower.getText();
    String upperText = ictx.upper.getText();
    Term lower = ictx.lower.getType() == ASPCore2Lexer.NUMBER ? Terms.newConstant(Integer.parseInt(lowerText)) : Terms.newVariable(lowerText);
    Term upper = ictx.upper.getType() == ASPCore2Lexer.NUMBER ? Terms.newConstant(Integer.parseInt(upperText)) : Terms.newVariable(upperText);
    return IntervalTerm.getInstance(lower, upper);
}
Also used : IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) 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) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)

Example 9 with Term

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

the class ParseTreeVisitor method visitAggregate.

@Override
public AggregateLiteral visitAggregate(ASPCore2Parser.AggregateContext ctx) {
    // aggregate : NAF? (lt=term lop=binop)? aggregate_function CURLY_OPEN aggregate_elements CURLY_CLOSE (uop=binop ut=term)?;
    boolean isPositive = ctx.NAF() == null;
    Term lt = null;
    ComparisonOperator lop = null;
    Term ut = null;
    ComparisonOperator uop = null;
    if (ctx.lt != null) {
        lt = (Term) visit(ctx.lt);
        lop = visitBinop(ctx.lop);
    }
    if (ctx.ut != null) {
        ut = (Term) visit(ctx.ut);
        uop = visitBinop(ctx.uop);
    }
    AggregateAtom.AggregateFunctionSymbol aggregateFunction = visitAggregate_function(ctx.aggregate_function());
    List<AggregateAtom.AggregateElement> aggregateElements = visitAggregate_elements(ctx.aggregate_elements());
    return Atoms.newAggregateAtom(lop, lt, uop, ut, aggregateFunction, aggregateElements).toLiteral(isPositive);
}
Also used : ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) 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) AggregateAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom)

Example 10 with Term

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

the class ParseTreeVisitor method visitExternal_atom.

@Override
public ExternalAtom visitExternal_atom(ASPCore2Parser.External_atomContext ctx) {
    if (ctx.MINUS() != null) {
        throw notSupported(ctx);
    }
    final String predicateName = ctx.ID().getText();
    final PredicateInterpretation interpretation = externals.get(predicateName);
    if (interpretation == null) {
        throw new IllegalArgumentException("Unknown interpretation name encountered: " + predicateName);
    }
    List<Term> outputTerms = visitTerms(ctx.output);
    return Atoms.newExternalAtom(Predicates.getPredicate(predicateName, outputTerms.size()), interpretation, visitTerms(ctx.input), outputTerms);
}
Also used : PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) 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)

Aggregations

Term (at.ac.tuwien.kr.alpha.api.terms.Term)52 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)36 ArrayList (java.util.ArrayList)16 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)15 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)15 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)12 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)9 Test (org.junit.jupiter.api.Test)8 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)6 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)6 HashSet (java.util.HashSet)6 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)4 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)4 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)3 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)3 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)3 Unifier (at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)3 MinusTerm (at.ac.tuwien.kr.alpha.commons.terms.ArithmeticTermImpl.MinusTerm)3