Search in sources :

Example 16 with VariableTerm

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

the class InternalRule method renameVariables.

/**
 * Returns a new Rule that is equal to this one except that all variables are renamed to have the newVariablePostfix
 * appended.
 *
 * @param newVariablePostfix
 * @return
 */
@Override
public InternalRule renameVariables(String newVariablePostfix) {
    List<VariableTerm> occurringVariables = new ArrayList<>();
    BasicAtom headAtom = this.getHeadAtom();
    occurringVariables.addAll(headAtom.getOccurringVariables());
    for (Literal literal : this.getBody()) {
        occurringVariables.addAll(literal.getOccurringVariables());
    }
    Unifier variableReplacement = new Unifier();
    for (VariableTerm occurringVariable : occurringVariables) {
        final String newVariableName = occurringVariable.toString() + newVariablePostfix;
        variableReplacement.put(occurringVariable, Terms.newVariable(newVariableName));
    }
    BasicAtom renamedHeadAtom = headAtom.substitute(variableReplacement);
    ArrayList<Literal> renamedBody = new ArrayList<>(this.getBody().size());
    for (Literal literal : this.getBody()) {
        renamedBody.add(literal.substitute(variableReplacement));
    }
    return new InternalRule(Heads.newNormalHead(renamedHeadAtom), renamedBody);
}
Also used : 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) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 17 with VariableTerm

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

the class LiteralInstantiatorTest method instantiateEnumLiteral.

@Test
public void instantiateEnumLiteral() {
    VariableTerm enumTerm = Terms.newVariable("E");
    VariableTerm idTerm = Terms.newVariable("X");
    VariableTerm indexTerm = Terms.newVariable("I");
    EnumerationAtom enumAtom = new EnumerationAtom(enumTerm, idTerm, indexTerm);
    EnumerationLiteral lit = new EnumerationLiteral(enumAtom);
    Substitution substitution = new BasicSubstitution();
    substitution.put(enumTerm, Terms.newSymbolicConstant("enum1"));
    substitution.put(idTerm, Terms.newSymbolicConstant("someElement"));
    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);
    assertTrue(resultSubstitutions.get(0).left.isVariableSet(indexTerm));
}
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) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) Test(org.junit.jupiter.api.Test)

Example 18 with VariableTerm

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

the class LiteralInstantiatorTest method workingMemoryBasedInstantiatePositiveBasicLiteral.

@Test
public void workingMemoryBasedInstantiatePositiveBasicLiteral() {
    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);
    workingMemory.addInstance(Atoms.newBasicAtom(p, Terms.newSymbolicConstant("x"), Terms.newSymbolicConstant("z")), 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"));
    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(2, substitutions.size());
    boolean ySubstituted = false;
    boolean zSubstituted = false;
    for (ImmutablePair<Substitution, AssignmentStatus> resultSubstitution : substitutions) {
        assertTrue(resultSubstitution.left.isVariableSet(y));
        assertEquals(AssignmentStatus.TRUE, resultSubstitution.right);
        if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("y"))) {
            ySubstituted = true;
        } else if (resultSubstitution.left.eval(y).equals(Terms.newSymbolicConstant("z"))) {
            zSubstituted = true;
        } else {
            fail("Invalid substitution for variable Y");
        }
    }
    assertTrue(ySubstituted && zSubstituted);
}
Also used : WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) 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) 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) Test(org.junit.jupiter.api.Test)

Example 19 with VariableTerm

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

Example 20 with VariableTerm

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

the class ComparisonLiteralImpl method getSatisfyingSubstitutions.

@Override
public List<Substitution> getSatisfyingSubstitutions(Substitution partialSubstitution) {
    // Treat case where this is just comparison with all variables bound by partialSubstitution.
    final Term left = getAtom().getTerms().get(0).substitute(partialSubstitution);
    final Term right = getAtom().getTerms().get(1).substitute(partialSubstitution);
    final boolean leftAssigning = assignable(left);
    final boolean rightAssigning = assignable(right);
    if (!leftAssigning && !rightAssigning) {
        // No assignment (variables are bound by partialSubstitution), thus evaluate comparison only.
        Term leftEvaluatedSubstitute = evaluateTerm(left);
        if (leftEvaluatedSubstitute == null) {
            return Collections.emptyList();
        }
        Term rightEvaluatedSubstitute = evaluateTerm(right);
        if (rightEvaluatedSubstitute == null) {
            return Collections.emptyList();
        }
        if (compare(leftEvaluatedSubstitute, rightEvaluatedSubstitute)) {
            return Collections.singletonList(partialSubstitution);
        } else {
            return Collections.emptyList();
        }
    }
    // Treat case that this is X = t or t = X.
    VariableTerm variable = null;
    Term expression = null;
    if (leftAssigning) {
        variable = (VariableTerm) left;
        expression = right;
    }
    if (rightAssigning) {
        variable = (VariableTerm) right;
        expression = left;
    }
    Term groundTerm = expression.substitute(partialSubstitution);
    Term resultTerm = null;
    // Check if the groundTerm is an arithmetic expression and evaluate it if so.
    if (groundTerm instanceof ArithmeticTerm) {
        Integer result = Terms.evaluateGroundTerm(groundTerm);
        if (result == null) {
            return Collections.emptyList();
        }
        resultTerm = Terms.newConstant(result);
    } else {
        // Ground term is another term (constant, or function term).
        resultTerm = groundTerm;
    }
    BasicSubstitution extendedSubstitution = new BasicSubstitution(partialSubstitution);
    extendedSubstitution.put(variable, resultTerm);
    return Collections.singletonList(extendedSubstitution);
}
Also used : BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ArithmeticTerm(at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm) 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) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

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