use of at.ac.tuwien.kr.alpha.api.rules.Rule in project Alpha by alpha-asp.
the class AbstractAggregateEncoder method encodeAggregateLiteral.
/**
* Encodes the aggregate literal referenced by the given {@link AggregateInfo}.
*
* @param aggregateToEncode
* @return
*/
public ASPCore2Program encodeAggregateLiteral(AggregateInfo aggregateToEncode) {
AggregateLiteral literalToEncode = aggregateToEncode.getLiteral();
if (literalToEncode.getAtom().getAggregateFunction() != this.aggregateFunctionToEncode) {
throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction());
}
if (!this.acceptedOperators.contains(literalToEncode.getAtom().getLowerBoundOperator())) {
throw new IllegalArgumentException("Encoder " + this.getClass().getSimpleName() + " cannot encode aggregate function " + literalToEncode.getAtom().getAggregateFunction() + " with operator " + literalToEncode.getAtom().getLowerBoundOperator());
}
String aggregateId = aggregateToEncode.getId();
ASPCore2Program literalEncoding = PredicateInternalizer.makePrefixedPredicatesInternal(encodeAggregateResult(aggregateToEncode), aggregateId);
List<Rule<Head>> elementEncodingRules = new ArrayList<>();
for (AggregateElement elementToEncode : literalToEncode.getAtom().getAggregateElements()) {
Rule<Head> elementRule = encodeAggregateElement(aggregateToEncode, elementToEncode);
elementEncodingRules.add(PredicateInternalizer.makePrefixedPredicatesInternal(elementRule, aggregateId));
}
return new InputProgram(ListUtils.union(literalEncoding.getRules(), elementEncodingRules), literalEncoding.getFacts(), new InlineDirectivesImpl());
}
use of at.ac.tuwien.kr.alpha.api.rules.Rule in project Alpha by alpha-asp.
the class ChoiceHeadToNormal method apply.
@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
InputProgram.Builder programBuilder = InputProgram.builder();
List<Rule<Head>> additionalRules = new ArrayList<>();
List<Rule<Head>> srcRules = new ArrayList<>(inputProgram.getRules());
Iterator<Rule<Head>> ruleIterator = srcRules.iterator();
while (ruleIterator.hasNext()) {
Rule<Head> rule = ruleIterator.next();
Head ruleHead = rule.getHead();
if (!(ruleHead instanceof ChoiceHead)) {
// Rule is constraint or without choice in the head. Leave as is.
continue;
}
// Remove this rule, as it will be transformed.
ruleIterator.remove();
ChoiceHead choiceHead = (ChoiceHead) ruleHead;
// Choice rules with boundaries are not yet supported.
if (choiceHead.getLowerBound() != null || choiceHead.getUpperBound() != null) {
throw new UnsupportedOperationException("Found choice rule with bounds, which are not yet supported. Rule is: " + rule);
}
// Only rewrite rules with a choice in their head.
for (ChoiceElement choiceElement : choiceHead.getChoiceElements()) {
// Create two guessing rules for each choiceElement.
// Construct common body to both rules.
BasicAtom head = choiceElement.getChoiceAtom();
List<Literal> ruleBody = new ArrayList<>(rule.getBody());
ruleBody.addAll(choiceElement.getConditionLiterals());
if (containsIntervalTerms(head)) {
throw new RuntimeException("Program contains a choice rule with interval terms in its head. This is not supported (yet).");
}
// Construct head atom for the choice.
Predicate headPredicate = head.getPredicate();
Predicate negPredicate = Predicates.getPredicate(PREDICATE_NEGATION_PREFIX + headPredicate.getName(), headPredicate.getArity() + 1, true);
List<Term> headTerms = new ArrayList<>(head.getTerms());
// FIXME: when introducing classical negation, this is 1 for classical positive atoms and 0 for
headTerms.add(0, Terms.newConstant("1"));
// classical negative atoms.
BasicAtom negHead = Atoms.newBasicAtom(negPredicate, headTerms);
// Construct two guessing rules.
List<Literal> guessingRuleBodyWithNegHead = new ArrayList<>(ruleBody);
guessingRuleBodyWithNegHead.add(Atoms.newBasicAtom(head.getPredicate(), head.getTerms()).toLiteral(false));
additionalRules.add(new BasicRule(Heads.newNormalHead(negHead), guessingRuleBodyWithNegHead));
List<Literal> guessingRuleBodyWithHead = new ArrayList<>(ruleBody);
guessingRuleBodyWithHead.add(Atoms.newBasicAtom(negPredicate, headTerms).toLiteral(false));
additionalRules.add(new BasicRule(Heads.newNormalHead(head), guessingRuleBodyWithHead));
// TODO: when cardinality constraints are possible, process the boundaries by adding a constraint with a cardinality check.
}
}
return programBuilder.addRules(srcRules).addRules(additionalRules).addFacts(inputProgram.getFacts()).addInlineDirectives(inputProgram.getInlineDirectives()).build();
}
use of at.ac.tuwien.kr.alpha.api.rules.Rule in project Alpha by alpha-asp.
the class EnumerationRewriting method rewriteRules.
private List<Rule<Head>> rewriteRules(List<Rule<Head>> srcRules, Predicate enumPredicate) {
List<Rule<Head>> rewrittenRules = new ArrayList<>();
for (Rule<Head> rule : srcRules) {
if (rule.getHead() != null && !(rule.getHead() instanceof NormalHead)) {
throw oops("Encountered rule whose head is not normal: " + rule);
}
if (rule.getHead() != null && ((NormalHead) rule.getHead()).getAtom().getPredicate().equals(enumPredicate)) {
throw oops("Atom declared as enumeration atom by directive occurs in head of the rule: " + rule);
}
List<Literal> modifiedBodyLiterals = new ArrayList<>(rule.getBody());
Iterator<Literal> rit = modifiedBodyLiterals.iterator();
LinkedList<Literal> rewrittenLiterals = new LinkedList<>();
while (rit.hasNext()) {
Literal literal = rit.next();
if (!(literal instanceof BasicLiteral)) {
continue;
}
BasicLiteral basicLiteral = (BasicLiteral) literal;
if (!basicLiteral.getPredicate().equals(enumPredicate)) {
continue;
}
// basicLiteral is an enumeration literal (i.e. predicate is marked as enum using directive)
rit.remove();
Term enumIdTerm = basicLiteral.getAtom().getTerms().get(0);
Term valueTerm = basicLiteral.getAtom().getTerms().get(1);
VariableTerm indexTerm = (VariableTerm) basicLiteral.getAtom().getTerms().get(2);
rewrittenLiterals.add(new EnumerationAtom(enumIdTerm, valueTerm, indexTerm).toLiteral());
}
modifiedBodyLiterals.addAll(rewrittenLiterals);
rewrittenRules.add(new BasicRule(rule.getHead(), modifiedBodyLiterals));
}
return rewrittenRules;
}
use of at.ac.tuwien.kr.alpha.api.rules.Rule in project Alpha by alpha-asp.
the class EnumerationRewriting method apply.
@Override
public ASPCore2Program apply(ASPCore2Program inputProgram) {
// Read enumeration predicate from directive.
String enumDirective = inputProgram.getInlineDirectives().getDirectiveValue(InlineDirectivesImpl.DIRECTIVE.enum_predicate_is);
if (enumDirective == null) {
// Directive not set, nothing to rewrite.
return inputProgram;
}
Predicate enumPredicate = Predicates.getPredicate(enumDirective, 3);
InputProgram.Builder programBuilder = InputProgram.builder().addInlineDirectives(inputProgram.getInlineDirectives());
checkFactsAreEnumerationFree(inputProgram.getFacts(), enumPredicate);
programBuilder.addFacts(inputProgram.getFacts());
List<Rule<Head>> srcRules = new ArrayList<>(inputProgram.getRules());
programBuilder.addRules(rewriteRules(srcRules, enumPredicate));
return programBuilder.build();
}
Aggregations