use of at.ac.tuwien.kr.alpha.api.programs.Predicate 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.programs.Predicate 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();
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class AnalyzeUnjustified method rulesHeadUnifyingWith.
private List<RuleAndUnifier> rulesHeadUnifyingWith(Atom p) {
List<RuleAndUnifier> rulesWithUnifier = new ArrayList<>();
Predicate predicate = p.getPredicate();
ArrayList<FactOrNonGroundRule> definingRulesAndFacts = new ArrayList<>();
// Get facts over the same predicate.
LinkedHashSet<Instance> factInstances = factsFromProgram.get(predicate);
if (factInstances != null) {
for (Instance factInstance : factInstances) {
definingRulesAndFacts.add(new FactOrNonGroundRule(factInstance));
}
}
HashSet<CompiledRule> rulesDefiningPredicate = programAnalysis.getPredicateDefiningRules().get(predicate);
if (rulesDefiningPredicate != null) {
for (CompiledRule nonGroundRule : rulesDefiningPredicate) {
definingRulesAndFacts.add(new FactOrNonGroundRule(nonGroundRule));
}
}
for (FactOrNonGroundRule factOrNonGroundRule : definingRulesAndFacts) {
boolean isNonGroundRule = factOrNonGroundRule.nonGroundRule != null;
Set<Literal> renamedBody;
Atom headAtom;
if (isNonGroundRule) {
// First rename all variables in the rule.
CompiledRule rule = factOrNonGroundRule.nonGroundRule.renameVariables("_" + renamingCounter++);
renamedBody = rule.getBody();
headAtom = rule.getHeadAtom();
} else {
// Create atom and empty rule body out of instance.
headAtom = Atoms.newBasicAtom(p.getPredicate(), factOrNonGroundRule.factInstance.terms);
renamedBody = Collections.emptySet();
}
// Unify rule head with literal to justify.
Unifier unifier = Unification.unifyAtoms(p, headAtom);
// Skip if unification failed.
if (unifier == null) {
continue;
}
rulesWithUnifier.add(new RuleAndUnifier(renamedBody, unifier, headAtom));
}
return rulesWithUnifier;
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class ParseTreeVisitor method visitAnswer_set.
@Override
public AnswerSet visitAnswer_set(ASPCore2Parser.Answer_setContext ctx) {
SortedSet<Predicate> predicates = new TreeSet<>();
Map<Predicate, SortedSet<Atom>> predicateInstances = new TreeMap<>();
for (ASPCore2Parser.Classical_literalContext classicalLiteralContext : ctx.classical_literal()) {
Atom atom = visitClassical_literal(classicalLiteralContext);
predicates.add(atom.getPredicate());
predicateInstances.compute(atom.getPredicate(), (k, v) -> {
if (v == null) {
v = new TreeSet<>();
}
v.add(atom);
return v;
});
}
return AnswerSets.newAnswerSet(predicates, predicateInstances);
}
use of at.ac.tuwien.kr.alpha.api.programs.Predicate in project Alpha by alpha-asp.
the class NaiveGrounder method initializeFactsAndRules.
private void initializeFactsAndRules() {
// Initialize all facts.
for (Atom fact : program.getFacts()) {
final Predicate predicate = fact.getPredicate();
// Record predicate
workingMemory.initialize(predicate);
}
// Register internal atoms.
workingMemory.initialize(RuleAtom.PREDICATE);
workingMemory.initialize(ChoiceAtom.OFF);
workingMemory.initialize(ChoiceAtom.ON);
// Initialize rules and constraints in working memory.
for (CompiledRule nonGroundRule : program.getRulesById().values()) {
// Create working memories for all predicates occurring in the rule.
for (Predicate predicate : nonGroundRule.getOccurringPredicates()) {
// FIXME: this also contains interval/builtin predicates that are not needed.
workingMemory.initialize(predicate);
}
// If the rule has fixed ground instantiations, it is not registered but grounded once like facts.
if (nonGroundRule.getGroundingInfo().hasFixedInstantiation()) {
fixedRules.add(nonGroundRule);
continue;
}
// Register each starting literal at the corresponding working memory.
for (Literal literal : nonGroundRule.getGroundingInfo().getStartingLiterals()) {
registerLiteralAtWorkingMemory(literal, nonGroundRule);
}
}
}
Aggregations