Search in sources :

Example 11 with VariableTerm

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

the class ArithmeticTermsRewritingTest method rewriteExternalAtom.

@Test
public void rewriteExternalAtom() {
    NormalProgram inputProgram = NormalProgramImpl.fromInputProgram(parser.parse("p :- Y = 13, &extArithTest[Y*5](Y-4)."));
    assertEquals(1, inputProgram.getRules().size());
    ArithmeticTermsRewriting arithmeticTermsRewriting = new ArithmeticTermsRewriting();
    NormalProgram rewrittenProgram = arithmeticTermsRewriting.apply(inputProgram);
    assertEquals(1, rewrittenProgram.getRules().size());
    NormalRule rewrittenRule = rewrittenProgram.getRules().get(0);
    assertEquals(4, rewrittenRule.getBody().size());
    List<Literal> externalLiterals = rewrittenRule.getBody().stream().filter(lit -> lit instanceof ExternalLiteral).collect(toList());
    assertEquals(1, externalLiterals.size());
    ExternalAtom rewrittenExternal = ((ExternalLiteral) externalLiterals.get(0)).getAtom();
    assertEquals(1, rewrittenExternal.getInput().size());
    assertTrue(rewrittenExternal.getInput().get(0) instanceof VariableTerm);
    assertEquals(1, rewrittenExternal.getOutput().size());
    assertTrue(rewrittenExternal.getOutput().get(0) instanceof VariableTerm);
}
Also used : NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) Set(java.util.Set) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Externals(at.ac.tuwien.kr.alpha.core.externals.Externals) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) NormalProgramImpl(at.ac.tuwien.kr.alpha.core.programs.NormalProgramImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) NormalRule(at.ac.tuwien.kr.alpha.api.rules.NormalRule) ExternalAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom) ExternalLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral) Test(org.junit.jupiter.api.Test)

Example 12 with VariableTerm

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

the class AggregateRewritingContextTest method countEqMaxEqGlobalVars.

@Test
public void countEqMaxEqGlobalVars() {
    AggregateRewritingContext ctx = rewritingContextForAspString(CTX_TEST_GRAPH_ASP);
    Map<ImmutablePair<AggregateFunctionSymbol, ComparisonOperator>, Set<AggregateInfo>> functionsToRewrite = ctx.getAggregateFunctionsToRewrite();
    assertEquals(2, functionsToRewrite.size());
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> cntEq = new ImmutablePair<>(AggregateFunctionSymbol.COUNT, ComparisonOperators.EQ);
    ImmutablePair<AggregateFunctionSymbol, ComparisonOperator> maxEq = new ImmutablePair<>(AggregateFunctionSymbol.MAX, ComparisonOperators.EQ);
    assertTrue(functionsToRewrite.containsKey(cntEq));
    assertTrue(functionsToRewrite.containsKey(maxEq));
    Set<AggregateInfo> cntEqIds = functionsToRewrite.get(cntEq);
    Set<AggregateInfo> maxEqIds = functionsToRewrite.get(maxEq);
    assertEquals(2, cntEqIds.size());
    assertEquals(1, maxEqIds.size());
    Predicate<AggregateInfo> vertexDegreeCount = (info) -> {
        if (info.getLiteral().getAtom().getAggregateElements().size() != 2) {
            return false;
        }
        Set<VariableTerm> globalVars = info.getGlobalVariables();
        if (globalVars.size() != 2) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("G"))) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("V"))) {
            return false;
        }
        return true;
    };
    Predicate<AggregateInfo> maxDegreeVerticesCount = (info) -> {
        if (info.getLiteral().getAtom().getAggregateElements().size() != 1) {
            return false;
        }
        Set<VariableTerm> globalVars = info.getGlobalVariables();
        if (globalVars.size() != 2) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("G"))) {
            return false;
        }
        if (!globalVars.contains(Terms.newVariable("DMAX"))) {
            return false;
        }
        return true;
    };
    boolean verifiedDegreeCount = false;
    boolean verifiedMaxDegreeVerticesCount = false;
    for (AggregateInfo id : cntEqIds) {
        if (vertexDegreeCount.test(id)) {
            verifiedDegreeCount = true;
        } else if (maxDegreeVerticesCount.test(id)) {
            verifiedMaxDegreeVerticesCount = true;
        }
    }
    assertTrue(verifiedDegreeCount);
    assertTrue(verifiedMaxDegreeVerticesCount);
}
Also used : AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) Predicate(java.util.function.Predicate) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Test(org.junit.jupiter.api.Test) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) ComparisonOperator(at.ac.tuwien.kr.alpha.api.ComparisonOperator) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) AggregateFunctionSymbol(at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom.AggregateFunctionSymbol) AggregateInfo(at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext.AggregateInfo) Test(org.junit.jupiter.api.Test)

Example 13 with VariableTerm

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

the class IntervalTermToIntervalAtom method rewriteIntervalSpecifications.

/**
 * Rewrites intervals into a new variable and special IntervalAtom.
 *
 * @return true if some interval occurs in the rule.
 */
private static NormalRule rewriteIntervalSpecifications(NormalRule rule) {
    // Collect all intervals and replace them with variables.
    Map<VariableTerm, IntervalTerm> intervalReplacements = new LinkedHashMap<>();
    List<Literal> rewrittenBody = new ArrayList<>();
    for (Literal literal : rule.getBody()) {
        Literal rewrittenLiteral = rewriteLiteral(literal, intervalReplacements);
        if (rewrittenLiteral != null) {
            rewrittenBody.add(rewrittenLiteral);
        }
    }
    // Note that this cast is safe: NormalHead can only have a BasicAtom, so literalizing and getting back the Atom destroys type information,
    // but should never yield anything other than a BasicAtom
    NormalHead rewrittenHead = rule.isConstraint() ? null : Heads.newNormalHead((BasicAtom) rewriteLiteral(rule.getHead().getAtom().toLiteral(), intervalReplacements).getAtom());
    // If intervalReplacements is empty, no IntervalTerms have been found, keep rule as is.
    if (intervalReplacements.isEmpty()) {
        return rule;
    }
    // Add new IntervalAtoms representing the interval specifications.
    for (Map.Entry<VariableTerm, IntervalTerm> interval : intervalReplacements.entrySet()) {
        rewrittenBody.add(new IntervalAtom(interval.getValue(), interval.getKey()).toLiteral());
    }
    return new NormalRuleImpl(rewrittenHead, rewrittenBody);
}
Also used : IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) NormalRuleImpl(at.ac.tuwien.kr.alpha.core.rules.NormalRuleImpl) LinkedHashMap(java.util.LinkedHashMap) NormalHead(at.ac.tuwien.kr.alpha.api.rules.heads.NormalHead) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with VariableTerm

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

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

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