use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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.programs.literals.Literal in project Alpha by alpha-asp.
the class DefaultSolver method justifyMbtAndBacktrack.
private boolean justifyMbtAndBacktrack() {
mbtAtFixpoint++;
// Run justification only if enabled and possible.
if (disableJustifications || !(grounder instanceof ProgramAnalyzingGrounder)) {
if (!backtrack()) {
logStats();
return false;
}
return true;
}
ProgramAnalyzingGrounder analyzingGrounder = (ProgramAnalyzingGrounder) grounder;
// Justify one MBT assigned atom.
int atomToJustify = assignment.getBasicAtomAssignedMBT();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Searching for justification of {} / {}", atomToJustify, atomStore.atomToString(atomToJustify));
LOGGER.debug("Assignment is (TRUE part only): {}", translate(assignment.getTrueAssignments()));
}
Set<Literal> reasonsForUnjustified = analyzingGrounder.justifyAtom(atomToJustify, assignment);
NoGood noGood = noGoodFromJustificationReasons(atomToJustify, reasonsForUnjustified);
int noGoodID = grounder.register(noGood);
Map<Integer, NoGood> obtained = new LinkedHashMap<>();
obtained.put(noGoodID, noGood);
LOGGER.debug("Learned NoGood is: {}", atomStore.noGoodToString(noGood));
// Add NoGood and trigger backjumping.
if (!ingest(obtained)) {
logStats();
return false;
}
return true;
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal 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.api.programs.literals.Literal in project Alpha by alpha-asp.
the class DefaultSolver method noGoodFromJustificationReasons.
private NoGood noGoodFromJustificationReasons(int atomToJustify, Set<Literal> reasonsForUnjustified) {
// Turn the justification into a NoGood.
int[] reasons = new int[reasonsForUnjustified.size() + 1];
reasons[0] = atomToLiteral(atomToJustify);
int arrpos = 1;
for (Literal literal : reasonsForUnjustified) {
reasons[arrpos++] = atomToLiteral(atomStore.get(literal.getAtom()), !literal.isNegated());
}
return NoGood.learnt(reasons);
}
use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.
the class InternalRule method renameVariables.
/**
* Returns a new Rule that is equal to this one except that all variables are renamed to have the newVariablePostfix
* appended.
*
* @param newVariablePostfix
* @return
*/
@Override
public InternalRule renameVariables(String newVariablePostfix) {
List<VariableTerm> occurringVariables = new ArrayList<>();
BasicAtom headAtom = this.getHeadAtom();
occurringVariables.addAll(headAtom.getOccurringVariables());
for (Literal literal : this.getBody()) {
occurringVariables.addAll(literal.getOccurringVariables());
}
Unifier variableReplacement = new Unifier();
for (VariableTerm occurringVariable : occurringVariables) {
final String newVariableName = occurringVariable.toString() + newVariablePostfix;
variableReplacement.put(occurringVariable, Terms.newVariable(newVariableName));
}
BasicAtom renamedHeadAtom = headAtom.substitute(variableReplacement);
ArrayList<Literal> renamedBody = new ArrayList<>(this.getBody().size());
for (Literal literal : this.getBody()) {
renamedBody.add(literal.substitute(variableReplacement));
}
return new InternalRule(Heads.newNormalHead(renamedHeadAtom), renamedBody);
}
Aggregations