use of at.ac.tuwien.kr.alpha.core.rules.CompiledRule 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.core.rules.CompiledRule in project Alpha by alpha-asp.
the class DefaultSolver method treatConflictAfterClosing.
private boolean treatConflictAfterClosing(Antecedent violatedNoGood) {
if (disableJustificationAfterClosing || disableJustifications || !(grounder instanceof ProgramAnalyzingGrounder)) {
// Will not learn from violated NoGood, do simple backtrack.
LOGGER.debug("NoGood was violated after all unassigned atoms were assigned to false; will not learn from it; skipping.");
if (!backtrack()) {
logStats();
return false;
}
return true;
}
ProgramAnalyzingGrounder analyzingGrounder = (ProgramAnalyzingGrounder) grounder;
LOGGER.debug("Justifying atoms in violated nogood.");
LinkedHashSet<Integer> toJustify = new LinkedHashSet<>();
// Find those literals in violatedNoGood that were just assigned false.
for (Integer literal : violatedNoGood.getReasonLiterals()) {
if (assignment.getImpliedBy(atomOf(literal)) == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
toJustify.add(literal);
}
}
// Since the violatedNoGood may contain atoms other than BasicAtom, these have to be treated.
Map<Integer, NoGood> obtained = new LinkedHashMap<>();
Iterator<Integer> toJustifyIterator = toJustify.iterator();
ArrayList<Integer> ruleAtomReplacements = new ArrayList<>();
while (toJustifyIterator.hasNext()) {
Integer literal = toJustifyIterator.next();
Atom atom = atomStore.get(atomOf(literal));
if (atom instanceof BasicAtom) {
continue;
}
if (!(atom instanceof RuleAtom)) {
// Ignore atoms other than RuleAtom.
toJustifyIterator.remove();
continue;
}
// For RuleAtoms in toJustify the corresponding ground body contains BasicAtoms that have been assigned FALSE in the closing.
// First, translate RuleAtom back to NonGroundRule + Substitution.
String ruleId = (String) ((ConstantTerm<?>) atom.getTerms().get(0)).getObject();
CompiledRule nonGroundRule = analyzingGrounder.getNonGroundRule(Integer.parseInt(ruleId));
String substitution = (String) ((ConstantTerm<?>) atom.getTerms().get(1)).getObject();
Substitution groundingSubstitution = Substitutions.fromString(substitution);
// Find ground literals in the body that have been assigned false and justify those.
for (Literal bodyLiteral : nonGroundRule.getBody()) {
Atom groundAtom = bodyLiteral.getAtom().substitute(groundingSubstitution);
if (groundAtom instanceof ComparisonAtom || analyzingGrounder.isFact(groundAtom)) {
// Facts and ComparisonAtoms are always true, no justification needed.
continue;
}
int groundAtomId = atomStore.get(groundAtom);
Antecedent impliedBy = assignment.getImpliedBy(groundAtomId);
// Check if atom was assigned to FALSE during the closing.
if (impliedBy == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
ruleAtomReplacements.add(atomToNegatedLiteral(groundAtomId));
}
}
toJustifyIterator.remove();
}
toJustify.addAll(ruleAtomReplacements);
for (Integer literalToJustify : toJustify) {
LOGGER.debug("Searching for justification(s) of {} / {}", toJustify, atomStore.atomToString(atomOf(literalToJustify)));
Set<Literal> reasonsForUnjustified = analyzingGrounder.justifyAtom(atomOf(literalToJustify), assignment);
NoGood noGood = noGoodFromJustificationReasons(atomOf(literalToJustify), reasonsForUnjustified);
int noGoodID = grounder.register(noGood);
obtained.put(noGoodID, noGood);
LOGGER.debug("Learned NoGood is: {}", atomStore.noGoodToString(noGood));
}
// Backtrack to remove the violation.
if (!backtrack()) {
logStats();
return false;
}
// Add newly obtained noGoods.
if (!ingest(obtained)) {
logStats();
return false;
}
return true;
}
use of at.ac.tuwien.kr.alpha.core.rules.CompiledRule in project Alpha by alpha-asp.
the class NoGoodGeneratorTest method collectNeg_ContainsOnlyPositiveLiterals.
/**
* Calls {@link NoGoodGenerator#collectNegLiterals(InternalRule, Substitution)}, which puts the atom occurring
* negatively in a rule into the atom store. It is then checked whether the atom in the atom store is positive.
*/
@Test
public void collectNeg_ContainsOnlyPositiveLiterals() {
ASPCore2Program input = PARSER.parse("p(a,b). " + "q(a,b) :- not nq(a,b). " + "nq(a,b) :- not q(a,b).");
NormalProgram normal = NORMALIZE_TRANSFORM.apply(input);
CompiledProgram program = InternalProgram.fromNormalProgram(normal);
CompiledRule rule = program.getRules().get(1);
AtomStore atomStore = new AtomStoreImpl();
Grounder grounder = GrounderFactory.getInstance("naive", program, atomStore, true);
NoGoodGenerator noGoodGenerator = ((NaiveGrounder) grounder).noGoodGenerator;
Substitution substitution = new BasicSubstitution();
substitution.put(X, A);
substitution.put(Y, B);
List<Integer> collectedNeg = noGoodGenerator.collectNegLiterals(rule, substitution);
assertEquals(1, collectedNeg.size());
String negAtomString = atomStore.atomToString(Literals.atomOf(collectedNeg.get(0)));
assertEquals("q(a, b)", negAtomString);
}
use of at.ac.tuwien.kr.alpha.core.rules.CompiledRule in project Alpha by alpha-asp.
the class RuleToStringTest method constructNonGroundRuleAndCheckToString.
private void constructNonGroundRuleAndCheckToString(String textualRule) {
CompiledRule nonGroundRule = InternalRule.fromNormalRule(NormalRuleImpl.fromBasicRule(parseSingleRule(textualRule)));
assertEquals(textualRule, nonGroundRule.toString());
}
use of at.ac.tuwien.kr.alpha.core.rules.CompiledRule in project Alpha by alpha-asp.
the class RuleTest method renameVariables.
@Test
public void renameVariables() {
String originalRule = "p(X,Y) :- a, f(Z) = 1, q(X,g(Y),Z), dom(A).";
Rule<Head> rule = parser.parse(originalRule).getRules().get(0);
CompiledRule normalRule = InternalRule.fromNormalRule(NormalRuleImpl.fromBasicRule(rule));
CompiledRule renamedRule = normalRule.renameVariables("_13");
Rule<Head> expectedRenamedRule = parser.parse("p(X_13, Y_13) :- a, f(Z_13) = 1, q(X_13, g(Y_13), Z_13), dom(A_13).").getRules().get(0);
CompiledRule expectedRenamedNormalRule = InternalRule.fromNormalRule(NormalRuleImpl.fromBasicRule(expectedRenamedRule));
assertEquals(expectedRenamedNormalRule.toString(), renamedRule.toString());
}
Aggregations