use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.
the class PredicateInternalizer method makePrefixedPredicatesInternal.
public static ASPCore2Program makePrefixedPredicatesInternal(ASPCore2Program program, String prefix) {
InputProgram.Builder prgBuilder = InputProgram.builder();
for (Atom atom : program.getFacts()) {
if (atom.getPredicate().getName().startsWith(prefix)) {
prgBuilder.addFact(PredicateInternalizer.makePredicateInternal((BasicAtom) atom));
} else {
prgBuilder.addFact(atom);
}
}
for (Rule<Head> rule : program.getRules()) {
prgBuilder.addRule(PredicateInternalizer.makePrefixedPredicatesInternal(rule, prefix));
}
prgBuilder.addInlineDirectives(program.getInlineDirectives());
return prgBuilder.build();
}
use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.
the class AggregateRewriting method rewriteRulesWithAggregates.
/**
* Transforms (restricted) aggregate literals of format "VAR OP #AGG_FN{...}" into literals of format
* "<result_predicate>(ARGS, VAR)" where ARGS is a function term wrapping the aggregate's global variables.
*
* @param ctx the {@link AggregateRewritingContext} containing information about all aggregates.
* @return for each rule, its rewritten version where aggregates are replaced with output atoms of the encoding.
*/
private static List<Rule<Head>> rewriteRulesWithAggregates(AggregateRewritingContext ctx) {
List<Rule<Head>> rewrittenRules = new ArrayList<>();
for (Rule<Head> rule : ctx.getRulesWithAggregates()) {
List<Literal> rewrittenBody = new ArrayList<>();
for (Literal lit : rule.getBody()) {
if (lit instanceof AggregateLiteral) {
AggregateInfo aggregateInfo = ctx.getAggregateInfo((AggregateLiteral) lit);
rewrittenBody.add(Literals.fromAtom(aggregateInfo.getOutputAtom(), !lit.isNegated()));
} else {
rewrittenBody.add(lit);
}
}
rewrittenRules.add(new BasicRule(rule.getHead(), rewrittenBody));
}
return rewrittenRules;
}
use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.
the class SubstitutionTest method substitutionFromString.
@Test
public void substitutionFromString() {
Rule<Head> rule = PARSER.parse("x :- p(X,Y), not q(X,Y).").getRules().get(0);
CompiledRule nonGroundRule = InternalRule.fromNormalRule(NormalRuleImpl.fromBasicRule(rule));
Substitution substitution1 = BasicSubstitution.specializeSubstitution(PX, PA, BasicSubstitution.EMPTY_SUBSTITUTION);
Substitution substitution = BasicSubstitution.specializeSubstitution(PY, PB, substitution1);
RuleAtom ruleAtom = new RuleAtom(nonGroundRule, substitution);
String substitutionString = (String) ((ConstantTerm<?>) ruleAtom.getTerms().get(1)).getObject();
Substitution fromString = Substitutions.fromString(substitutionString);
assertEquals(substitution, fromString);
}
use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.
the class LiteralBindingNonBindingVariablesTest method testPositiveExternalLiteral.
@Test
public void testPositiveExternalLiteral() {
externals.put("ext", new IntPredicateInterpretation(i -> i > 0));
Rule<Head> rule = parser.parse("p(X) :- q(Y), &ext[Y](X).", externals).getRules().get(0);
Literal literal = rule.getBody().stream().filter((lit) -> lit.getPredicate().getName().equals("ext")).findFirst().get();
assertEquals(false, literal.isNegated());
expectVariables(literal.getBindingVariables(), "X");
expectVariables(literal.getNonBindingVariables(), "Y");
}
use of at.ac.tuwien.kr.alpha.api.rules.heads.Head in project Alpha by alpha-asp.
the class RuleTest method testRulesEqual.
@Test
public void testRulesEqual() {
ASPCore2Program p1 = parser.parse("p(X, Y) :- bla(X), blub(Y), foo(X, Y), not bar(X).");
Rule<Head> r1 = p1.getRules().get(0);
ASPCore2Program p2 = parser.parse("p(X, Y) :- bla(X), blub(Y), foo(X, Y), not bar(X).");
Rule<Head> r2 = p2.getRules().get(0);
ASPCore2Program p3 = parser.parse("p(X, Y) :- bla(X), blub(X), foo(X, X), not bar(X).");
Rule<Head> r3 = p3.getRules().get(0);
assertTrue(r1.equals(r2));
assertTrue(r2.equals(r1));
assertTrue(r1.hashCode() == r2.hashCode());
assertFalse(r1.equals(r3));
assertFalse(r3.equals(r1));
assertTrue(r1.hashCode() != r3.hashCode());
assertFalse(r2.equals(r3));
assertFalse(r3.equals(r2));
assertTrue(r2.hashCode() != r3.hashCode());
}
Aggregations