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