use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method instantiateEnumLiteral.
@Test
public void instantiateEnumLiteral() {
VariableTerm enumTerm = Terms.newVariable("E");
VariableTerm idTerm = Terms.newVariable("X");
VariableTerm indexTerm = Terms.newVariable("I");
EnumerationAtom enumAtom = new EnumerationAtom(enumTerm, idTerm, indexTerm);
EnumerationLiteral lit = new EnumerationLiteral(enumAtom);
Substitution substitution = new BasicSubstitution();
substitution.put(enumTerm, Terms.newSymbolicConstant("enum1"));
substitution.put(idTerm, Terms.newSymbolicConstant("someElement"));
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(null));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
List<ImmutablePair<Substitution, AssignmentStatus>> resultSubstitutions = result.getSubstitutions();
assertEquals(1, resultSubstitutions.size());
assertEquals(AssignmentStatus.TRUE, resultSubstitutions.get(0).right);
assertTrue(resultSubstitutions.get(0).left.isVariableSet(indexTerm));
}
use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom in project Alpha by alpha-asp.
the class NoGoodGenerator method collectPosLiterals.
private List<Integer> collectPosLiterals(final CompiledRule nonGroundRule, final Substitution substitution) {
final List<Integer> bodyLiteralsPositive = new ArrayList<>();
for (Literal lit : nonGroundRule.getPositiveBody()) {
if (lit instanceof FixedInterpretationLiteral) {
// FixedInterpretationAtoms need not be shown to the solver, skip it.
continue;
}
final Atom atom = lit.getAtom();
// Skip the special enumeration atom.
if (atom instanceof EnumerationAtom) {
continue;
}
final Atom groundAtom = atom.substitute(substitution);
// Consider facts to eliminate ground atoms from the generated nogoods that are always true
// and eliminate nogoods that are always satisfied due to facts.
Set<Instance> factInstances = factsFromProgram.get(groundAtom.getPredicate());
if (factInstances != null && factInstances.contains(new Instance(groundAtom.getTerms()))) {
// Skip positive atoms that are always true.
continue;
}
if (!existsRuleWithPredicateInHead(groundAtom.getPredicate())) {
// Atom is no fact and no rule defines it, it cannot be derived (i.e., is always false), skip whole rule as it will never fire.
return null;
}
bodyLiteralsPositive.add(atomToLiteral(atomStore.putIfAbsent(groundAtom)));
}
return bodyLiteralsPositive;
}
use of at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom in project Alpha by alpha-asp.
the class EnumerationRewriting method rewriteRules.
private List<Rule<Head>> rewriteRules(List<Rule<Head>> srcRules, Predicate enumPredicate) {
List<Rule<Head>> rewrittenRules = new ArrayList<>();
for (Rule<Head> rule : srcRules) {
if (rule.getHead() != null && !(rule.getHead() instanceof NormalHead)) {
throw oops("Encountered rule whose head is not normal: " + rule);
}
if (rule.getHead() != null && ((NormalHead) rule.getHead()).getAtom().getPredicate().equals(enumPredicate)) {
throw oops("Atom declared as enumeration atom by directive occurs in head of the rule: " + rule);
}
List<Literal> modifiedBodyLiterals = new ArrayList<>(rule.getBody());
Iterator<Literal> rit = modifiedBodyLiterals.iterator();
LinkedList<Literal> rewrittenLiterals = new LinkedList<>();
while (rit.hasNext()) {
Literal literal = rit.next();
if (!(literal instanceof BasicLiteral)) {
continue;
}
BasicLiteral basicLiteral = (BasicLiteral) literal;
if (!basicLiteral.getPredicate().equals(enumPredicate)) {
continue;
}
// basicLiteral is an enumeration literal (i.e. predicate is marked as enum using directive)
rit.remove();
Term enumIdTerm = basicLiteral.getAtom().getTerms().get(0);
Term valueTerm = basicLiteral.getAtom().getTerms().get(1);
VariableTerm indexTerm = (VariableTerm) basicLiteral.getAtom().getTerms().get(2);
rewrittenLiterals.add(new EnumerationAtom(enumIdTerm, valueTerm, indexTerm).toLiteral());
}
modifiedBodyLiterals.addAll(rewrittenLiterals);
rewrittenRules.add(new BasicRule(rule.getHead(), modifiedBodyLiterals));
}
return rewrittenRules;
}
Aggregations