Search in sources :

Example 11 with Term

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

the class ParseTreeVisitor method visitChoice.

@Override
public Head visitChoice(ASPCore2Parser.ChoiceContext ctx) {
    // choice : (lt=term lop=binop)? CURLY_OPEN choice_elements? CURLY_CLOSE (uop=binop ut=term)?;
    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);
    }
    return Heads.newChoiceHead(visitChoice_elements(ctx.choice_elements()), lt, lop, ut, uop);
}
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)

Example 12 with Term

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

the class Unification method unifyTerms.

private static boolean unifyTerms(Term left, Term right, Unifier currentSubstitution, boolean keepLeftAsIs) {
    final Term leftSubs = left.substitute(currentSubstitution);
    final Term rightSubs = right.substitute(currentSubstitution);
    if (leftSubs == rightSubs) {
        return true;
    }
    if (!keepLeftAsIs && leftSubs instanceof VariableTerm && !currentSubstitution.isVariableSet((VariableTerm) leftSubs)) {
        currentSubstitution.put((VariableTerm) leftSubs, rightSubs);
        return true;
    }
    if (rightSubs instanceof VariableTerm && !currentSubstitution.isVariableSet((VariableTerm) rightSubs)) {
        currentSubstitution.put((VariableTerm) rightSubs, leftSubs);
        return true;
    }
    if (leftSubs instanceof FunctionTerm && rightSubs instanceof FunctionTerm) {
        final FunctionTerm leftFunction = (FunctionTerm) leftSubs;
        final FunctionTerm rightFunction = (FunctionTerm) rightSubs;
        if (!leftFunction.getSymbol().equals(rightFunction.getSymbol()) || leftFunction.getTerms().size() != rightFunction.getTerms().size()) {
            return false;
        }
        for (int i = 0; i < leftFunction.getTerms().size(); i++) {
            final Term leftTerm = leftFunction.getTerms().get(i);
            final Term rightTerm = rightFunction.getTerms().get(i);
            if (!unifyTerms(leftTerm, rightTerm, currentSubstitution, keepLeftAsIs)) {
                return false;
            }
        }
        return true;
    }
    if (leftSubs instanceof ArithmeticTerm && rightSubs instanceof ArithmeticTerm) {
        // ArithmeticTerms are similar to FunctionTerms, i.e. if the operator is the same and its subterms unify, the ArithmeticTerms unify.
        final ArithmeticTerm leftArithmeticTerm = (ArithmeticTerm) leftSubs;
        final ArithmeticTerm rightArithmeticTerm = (ArithmeticTerm) rightSubs;
        if (!leftArithmeticTerm.getOperator().equals(rightArithmeticTerm.getOperator())) {
            return false;
        }
        final Term leftTermLeftSubterm = leftArithmeticTerm.getLeftOperand();
        final Term rightTermLeftSubterm = rightArithmeticTerm.getLeftOperand();
        if (!unifyTerms(leftTermLeftSubterm, rightTermLeftSubterm, currentSubstitution, keepLeftAsIs)) {
            return false;
        }
        final Term leftTermRightSubterm = leftArithmeticTerm.getRightOperand();
        final Term rightTermRightSubterm = rightArithmeticTerm.getRightOperand();
        if (!unifyTerms(leftTermRightSubterm, rightTermRightSubterm, currentSubstitution, keepLeftAsIs)) {
            return false;
        }
        return true;
    }
    return false;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) 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) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)

Example 13 with Term

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

the class TestUtils method basicAtomWithSymbolicTerms.

public static BasicAtom basicAtomWithSymbolicTerms(String predicate, String... constantSymbols) {
    Predicate pred = Predicates.getPredicate(predicate, constantSymbols.length);
    List<Term> trms = new ArrayList<>();
    for (String str : constantSymbols) {
        trms.add(Terms.newSymbolicConstant(str));
    }
    return Atoms.newBasicAtom(pred, trms);
}
Also used : ArrayList(java.util.ArrayList) Term(at.ac.tuwien.kr.alpha.api.terms.Term) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate)

Example 14 with Term

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

the class AggregateOperatorNormalizationTest method assertAggregateBoundIncremented.

private static void assertAggregateBoundIncremented(Rule<Head> sourceRule, Rule<Head> rewrittenRule) {
    AggregateLiteral sourceAggregate = null;
    for (Literal lit : sourceRule.getBody()) {
        if (lit instanceof AggregateLiteral) {
            sourceAggregate = (AggregateLiteral) lit;
        }
    }
    AggregateLiteral rewrittenAggregate = null;
    ComparisonLiteral addedComparisonLiteral = null;
    for (Literal lit : rewrittenRule.getBody()) {
        if (lit instanceof AggregateLiteral) {
            rewrittenAggregate = (AggregateLiteral) lit;
        } else if (lit instanceof ComparisonLiteral) {
            addedComparisonLiteral = (ComparisonLiteral) lit;
        }
    }
    assertNotNull(addedComparisonLiteral);
    assertEquals(addedComparisonLiteral.getAtom().getTerms().get(0), rewrittenAggregate.getAtom().getLowerBoundTerm());
    Term comparisonRightHandTerm = addedComparisonLiteral.getAtom().getTerms().get(1);
    assertTrue(comparisonRightHandTerm instanceof ArithmeticTerm);
    ArithmeticTerm incrementTerm = (ArithmeticTerm) comparisonRightHandTerm;
    assertEquals(ArithmeticOperator.PLUS, incrementTerm.getOperator());
    assertEquals(Terms.newConstant(1), incrementTerm.getRightOperand());
    Term sourceBound = sourceAggregate.getAtom().getLowerBoundTerm() != null ? sourceAggregate.getAtom().getLowerBoundTerm() : sourceAggregate.getAtom().getUpperBoundTerm();
    assertEquals(sourceBound, incrementTerm.getLeftOperand());
}
Also used : AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)

Example 15 with Term

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

the class AggregateRewritingRuleAnalysisTest method bindingAggregateWithGlobals2.

@Test
public void bindingAggregateWithGlobals2() {
    AggregateRewritingRuleAnalysis analysis = analyze(BINDING_AGGREGATE_WITH_GLOBALS_2);
    assertEquals(2, analysis.globalVariablesPerAggregate.size());
    assertEquals(2, analysis.dependenciesPerAggregate.size());
    // Verify correct analysis of max aggregate
    List<Term> vertexDegreeTerms = Collections.singletonList(Terms.newVariable("DV"));
    Literal vertexDegreeLiteral = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex_degree", 3), Terms.newVariable("G"), Terms.newVariable("V"), Terms.newVariable("DV")), true);
    List<Literal> vertexDegreeLiterals = Collections.singletonList(vertexDegreeLiteral);
    AggregateElement vertexDegree = Atoms.newAggregateElement(vertexDegreeTerms, vertexDegreeLiterals);
    AggregateLiteral maxAggregate = Literals.fromAtom(Atoms.newAggregateAtom(ComparisonOperators.EQ, Terms.newVariable("DMAX"), AggregateFunctionSymbol.MAX, Collections.singletonList(vertexDegree)), true);
    assertTrue(analysis.globalVariablesPerAggregate.containsKey(maxAggregate));
    Set<VariableTerm> maxAggrGlobalVars = analysis.globalVariablesPerAggregate.get(maxAggregate);
    assertEquals(1, maxAggrGlobalVars.size());
    assertTrue(maxAggrGlobalVars.contains(Terms.newVariable("G")));
    assertTrue(analysis.dependenciesPerAggregate.containsKey(maxAggregate));
    Set<Literal> maxAggrDependencies = analysis.dependenciesPerAggregate.get(maxAggregate);
    assertEquals(1, maxAggrDependencies.size());
    Literal graph = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph", 1), Terms.newVariable("G")), true);
    assertTrue(maxAggrDependencies.contains(graph));
    // Verify correct analysis of count aggregate
    List<Term> maxVertexDegreeTerms = Collections.singletonList(Terms.newVariable("V"));
    Literal maxVertexDegreeLiteral = Literals.fromAtom(Atoms.newBasicAtom(Predicates.getPredicate("graph_vertex_degree", 3), Terms.newVariable("G"), Terms.newVariable("V"), Terms.newVariable("DMAX")), true);
    List<Literal> maxVertexDegreeLiterals = Collections.singletonList(maxVertexDegreeLiteral);
    AggregateElement maxVertexDegree = Atoms.newAggregateElement(maxVertexDegreeTerms, maxVertexDegreeLiterals);
    AggregateLiteral countAggregate = Literals.fromAtom(Atoms.newAggregateAtom(ComparisonOperators.EQ, Terms.newVariable("N"), AggregateFunctionSymbol.COUNT, Collections.singletonList(maxVertexDegree)), true);
    assertTrue(analysis.globalVariablesPerAggregate.containsKey(countAggregate));
    Set<VariableTerm> cntAggrGlobalVars = analysis.globalVariablesPerAggregate.get(countAggregate);
    assertEquals(2, cntAggrGlobalVars.size());
    assertTrue(cntAggrGlobalVars.contains(Terms.newVariable("G")));
    assertTrue(cntAggrGlobalVars.contains(Terms.newVariable("DMAX")));
    assertTrue(analysis.dependenciesPerAggregate.containsKey(countAggregate));
    Set<Literal> cntAggrDependencies = analysis.dependenciesPerAggregate.get(countAggregate);
    assertEquals(2, cntAggrDependencies.size());
    assertTrue(cntAggrDependencies.contains(graph));
    assertTrue(cntAggrDependencies.contains(maxAggregate));
}
Also used : AggregateElement(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateElement) 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) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Test(org.junit.jupiter.api.Test)

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