use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.
the class UnifierTest method mergeUnifierIntoLeft.
@Test
public void mergeUnifierIntoLeft() {
VariableTerm varX = Terms.newVariable("X");
VariableTerm varY = Terms.newVariable("Y");
VariableTerm varZ = Terms.newVariable("Z");
Term constA = Terms.newConstant("a");
Unifier left = new Unifier();
left.put(varX, varY);
left.put(varZ, varY);
Unifier right = new Unifier();
right.put(varX, constA);
Unifier merged = Unifier.mergeIntoLeft(left, right);
assertEquals(constA, merged.eval(varY));
assertEquals(constA, merged.eval(varZ));
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.
the class LitSet method toString.
@Override
public String toString() {
StringBuilder sb = new StringBuilder("(" + atom + ",{");
for (Unifier complementSubstitution : complementSubstitutions) {
sb.append(atom.substitute(complementSubstitution));
sb.append(", ");
}
sb.append("})");
return sb.toString();
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.
the class LitSet method computeNormalizedSubstitutions.
private Set<Unifier> computeNormalizedSubstitutions() {
Set<Unifier> ret = new LinkedHashSet<>();
for (Unifier substitution : complementSubstitutions) {
Unifier preNormalizedSubstitution = normalizeSubstitution(atom, substitution, normalizedLiteral);
// Unifier may still contain variables in the right-hand side, those have to be normalized, too.
Atom appliedSub = normalizedLiteral.substitute(preNormalizedSubstitution);
// Apply substitution and normalize all remaining variables, i.e., those appearing at the right-hand side of the substitution.
Atom normalized = computeNormalized(appliedSub, "_X");
// Compute final substitution from normalized atom to the one where also variables are normalized.
Unifier normalizedSubstitution = new Unifier(Unification.instantiate(normalizedLiteral, normalized));
ret.add(normalizedSubstitution);
}
return ret;
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier 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);
}
use of at.ac.tuwien.kr.alpha.commons.substitutions.Unifier in project Alpha by alpha-asp.
the class AnalyzeUnjustified method unjustCover.
private Set<LitSet> unjustCover(List<Literal> vB, Set<Unifier> vY, Set<Unifier> vN, Assignment currentAssignment) {
padDepth += 2;
log("Begin UnjustCoverFixed()");
log("Finding unjustified body literals in: {} / {} excluded {}", vB, vY, vN);
Set<LitSet> ret = new LinkedHashSet<>();
if (vB.isEmpty() || vY.isEmpty()) {
log("End unjustCover().");
padDepth -= 2;
return Collections.emptySet();
}
int chosenLiteralPos = 0;
// Find a body literal that is not a ComparisonLiteral, because these do not generate/have atoms assigned.
for (int i = 0; i < vB.size(); i++) {
if (!(vB.get(i) instanceof ComparisonLiteral)) {
// TODO: Should this be FixedInterpretationLiteral instead??
chosenLiteralPos = i;
break;
}
}
Atom b = vB.get(chosenLiteralPos).getAtom();
log("Picked literal from body is: {}", b);
for (Unifier sigmaY : vY) {
Atom bSigmaY = b.substitute(sigmaY);
log("Treating substitution for: {}", bSigmaY);
Set<Unifier> vYp = new LinkedHashSet<>();
log("Checking atoms over predicate: {}", b.getPredicate());
AssignedAtomsIterator assignedAtomsOverPredicate = getAssignedAtomsOverPredicate(b.getPredicate());
atomLoop: while (assignedAtomsOverPredicate.hasNext()) {
Atom atom = assignedAtomsOverPredicate.next();
// Check that atom is justified/true.
log("Checking atom: {}", atom);
if (atomStore.contains(atom)) {
int atomId = atomStore.get(atom);
if (currentAssignment.getTruth(atomId) != ThriceTruth.TRUE) {
log("Atom is not TRUE. Skipping.");
continue;
}
}
// Note: in case the atom is not in the atomStore, it came from a fact and hence is true.
Unifier sigma = Unification.instantiate(b, atom);
if (sigma == null) {
log("Atom does not unify with picked body literal.");
continue;
}
Atom bSigma = b.substitute(sigma);
if (!bSigma.isGround()) {
throw oops("Resulting atom is not ground.");
}
Set<VariableTerm> variablesOccurringInSigma = sigma.getMappedVariables();
if (Unification.instantiate(bSigmaY, bSigma) != null) {
for (Unifier sigmaN : vN) {
ArrayList<Term> occurringVariables = new ArrayList<>(variablesOccurringInSigma);
occurringVariables.addAll(sigmaN.getMappedVariables());
BasicAtom genericAtom = Atoms.newBasicAtom(Predicates.getPredicate("_", occurringVariables.size(), true), occurringVariables);
Atom genericSubstituted = genericAtom.substitute(sigmaN).renameVariables("_analyzeTest");
if (Unification.instantiate(genericSubstituted, genericAtom.substitute(sigma)) != null) {
log("Atom {} is excluded by: {} via {}", genericSubstituted, sigmaN, sigma);
continue atomLoop;
}
}
log("Adding corresponding substitution to Y': {}", sigma);
vYp.add(sigma);
}
}
log("Unjustified body literals: {}", vYp);
Set<Unifier> vYpUN = new LinkedHashSet<>();
vYpUN.addAll(vYp);
vYpUN.addAll(vN);
LitSet toJustify = new LitSet(bSigmaY, vYpUN);
if (!toJustify.coversNothing()) {
log("New litset to do: {}", toJustify);
ret.add(toJustify);
} else {
log("Generated LitSet covers nothing. Ignoring: {}", toJustify);
}
ArrayList<Literal> newB = new ArrayList<>(vB);
newB.remove(chosenLiteralPos);
ret.addAll(unjustCover(newB, vYp, vN, currentAssignment));
log("Literal set(s) to treat: {}", ret);
}
log("End unjustCover().");
padDepth -= 2;
return ret;
}
Aggregations