From 0a73d07389984fc95dd018b84ad09942e2c0b921 Mon Sep 17 00:00:00 2001 From: mschmutzhart Date: Tue, 22 Mar 2022 15:48:26 +0100 Subject: [PATCH] added comments --- .../transformation/SimplePreprocessing.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/programs/transformation/SimplePreprocessing.java b/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/programs/transformation/SimplePreprocessing.java index 7626f2580..648f27194 100644 --- a/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/programs/transformation/SimplePreprocessing.java +++ b/alpha-core/src/main/java/at/ac/tuwien/kr/alpha/core/programs/transformation/SimplePreprocessing.java @@ -14,19 +14,26 @@ import java.util.*; /** - * Simplifies the input program by deleting redundant literals and rules, as well as adding rule heads that will - * always be true as facts. The approach is adopted from preprocessing techniques employed by traditional ground ASP - * solvers, as seen in: + * Simplifies the input program by deleting redundant literals and rules, as well as identifying rule heads that can + * always be derived from facts and adding them to the known facts. The approach is adopted from preprocessing + * techniques employed by traditional ground ASP solvers, as seen in: * @see doi:10.3233/978-1-58603-891-5-15 */ public class SimplePreprocessing extends ProgramTransformation { + + /** + * Evaluates all rules of the input program and tries to simplify them by applying a number of rule transformations. + * @param inputProgram a normalized ASP program to be preprocessed. + * @return the preprocessed program. + */ @Override public NormalProgram apply(NormalProgram inputProgram) { List srcRules = inputProgram.getRules(); Set newRules = new LinkedHashSet<>(); Set facts = new LinkedHashSet<>(inputProgram.getFacts()); - boolean canBePreprocessed = true; + + // Eliminate rules that will never fire, without regarding other rules. for (NormalRule rule: srcRules) { if (checkForConflictingBodyLiterals(rule.getPositiveBody(), rule.getNegativeBody())) { continue; @@ -37,6 +44,10 @@ public NormalProgram apply(NormalProgram inputProgram) { newRules.add(rule); } srcRules = new LinkedList<>(newRules); + + // Analyze every rule in regard to the other rules and facts and if applicable, simplify or remove it. Repeat + // until a fixpoint is reached. + boolean canBePreprocessed = true; while (canBePreprocessed) { newRules = new LinkedHashSet<>(); canBePreprocessed = false; @@ -111,10 +122,17 @@ private boolean checkForHeadInBody(Set body, Atom headAtom) { * modified, is a fact or will never fire. */ private RuleEvaluation evaluateRule(NormalRule rule, List rules, Set facts) { - Set redundantLiterals = new LinkedHashSet<>(); + // Check if the rule head is already a fact, making the rule redundant. if (facts.contains(rule.getHeadAtom())) { return RuleEvaluation.NO_FIRE; } + + // Collect all literals that can be removed from the rule. + Set redundantLiterals = new LinkedHashSet<>(); + + // Check if body literals can be derived from facts or other rules. If a body literal is not derivable, the + // rule can never fire. If a literal is already proven within the program context, the rule will be simplified + // by removing it. for (Literal literal : rule.getBody()) { if (literal instanceof BasicLiteral) { if (literal.isNegated()) { @@ -140,6 +158,7 @@ private RuleEvaluation evaluateRule(NormalRule rule, List rules, Set } } + // Checks if an atom cannot be derived from a rule or a fact. private boolean isNonDerivable(Atom atom, List rules, Set facts) { Atom tempAtom = atom.renameVariables("Prep"); for (NormalRule rule : rules) { @@ -158,7 +177,7 @@ private boolean isNonDerivable(Atom atom, List rules, Set fact } /** - * removes a set of given literals from the rule body. + * Removes a set of given literals from the rule body. * @param rule the rule from which literals should be removed. * @param literals The literals to remove. * @return the resulting rule or fact.