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);
}
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));
}
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);
}
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));
}
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);
}
Aggregations