use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class SolverTests method instanceEnumerationArbitraryTerms.
@RegressionTest
public void instanceEnumerationArbitraryTerms(RegressionTestConfig cfg) {
Set<AnswerSet> answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(a). dom(f(a,b)). dom(d)." + "p(X) :- dom(X)." + "q(Y) :- p(Y)." + "unique_position(Term,Pos) :- q(Term), enum(id0,Term,Pos)." + "wrong_double_occurrence :- unique_position(T1,P), unique_position(T2,P), T1 != T2.", cfg).collectSet();
// Since enumeration depends on evaluation, we do not know which unique_position is actually assigned.
// Check manually that there is one answer set, wrong_double_occurrence has not been derived, and enum yielded a unique position for each term.
assertEquals(1, answerSets.size());
AnswerSet answerSet = answerSets.iterator().next();
assertPropositionalPredicateFalse(answerSet, Predicates.getPredicate("wrong_double_occurrence", 0));
SortedSet<Atom> positions = answerSet.getPredicateInstances(Predicates.getPredicate("unique_position", 2));
assertEnumerationPositions(positions, 3);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class StratifiedEvaluation method fireRule.
private void fireRule(CompiledRule rule, Substitution substitution) {
Atom newAtom = rule.getHeadAtom().substitute(substitution);
if (!newAtom.isGround()) {
throw new IllegalStateException("Trying to fire rule " + rule.toString() + " with incompatible substitution " + substitution.toString());
}
LOGGER.debug("Firing rule - got head atom: {}", newAtom);
workingMemory.addInstance(newAtom, true);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class InternalProgram method recordFacts.
private void recordFacts(List<Atom> facts) {
for (Atom fact : facts) {
List<Instance> tmpInstances = FactIntervalEvaluator.constructFactInstances(fact);
Predicate tmpPredicate = fact.getPredicate();
factsByPredicate.putIfAbsent(tmpPredicate, new LinkedHashSet<>());
factsByPredicate.get(tmpPredicate).addAll(tmpInstances);
}
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class VariableEqualityRemoval method findAndReplaceVariableEquality.
private Rule<Head> findAndReplaceVariableEquality(Rule<Head> rule) {
// Collect all equal variables.
HashMap<VariableTerm, HashSet<VariableTerm>> variableToEqualVariables = new LinkedHashMap<>();
HashSet<Literal> equalitiesToRemove = new HashSet<>();
for (Literal bodyElement : rule.getBody()) {
if (!(bodyElement instanceof ComparisonLiteral)) {
continue;
}
ComparisonLiteral comparisonLiteral = (ComparisonLiteral) bodyElement;
if (!comparisonLiteral.isNormalizedEquality()) {
continue;
}
if (comparisonLiteral.getTerms().get(0) instanceof VariableTerm && comparisonLiteral.getTerms().get(1) instanceof VariableTerm) {
VariableTerm leftVariable = (VariableTerm) comparisonLiteral.getTerms().get(0);
VariableTerm rightVariable = (VariableTerm) comparisonLiteral.getTerms().get(1);
HashSet<VariableTerm> leftEqualVariables = variableToEqualVariables.get(leftVariable);
HashSet<VariableTerm> rightEqualVariables = variableToEqualVariables.get(rightVariable);
if (leftEqualVariables == null && rightEqualVariables == null) {
HashSet<VariableTerm> equalVariables = new LinkedHashSet<>(Arrays.asList(leftVariable, rightVariable));
variableToEqualVariables.put(leftVariable, equalVariables);
variableToEqualVariables.put(rightVariable, equalVariables);
}
if (leftEqualVariables == null && rightEqualVariables != null) {
rightEqualVariables.add(leftVariable);
variableToEqualVariables.put(leftVariable, rightEqualVariables);
}
if (leftEqualVariables != null && rightEqualVariables == null) {
leftEqualVariables.add(rightVariable);
variableToEqualVariables.put(rightVariable, leftEqualVariables);
}
if (leftEqualVariables != null && rightEqualVariables != null) {
leftEqualVariables.addAll(rightEqualVariables);
for (VariableTerm rightEqualVariable : rightEqualVariables) {
variableToEqualVariables.put(rightEqualVariable, leftEqualVariables);
}
}
equalitiesToRemove.add(comparisonLiteral);
}
}
if (variableToEqualVariables.isEmpty()) {
// Skip rule if there is no equality between variables.
return rule;
}
List<Literal> rewrittenBody = new ArrayList<>(rule.getBody());
if (!rule.isConstraint() && rule.getHead() instanceof DisjunctiveHead) {
throw new UnsupportedOperationException("VariableEqualityRemoval cannot be applied to rule with DisjunctiveHead, yet.");
}
NormalHead rewrittenHead = rule.isConstraint() ? null : Heads.newNormalHead(((NormalHead) rule.getHead()).getAtom());
// Use substitution for actual replacement.
Unifier replacementSubstitution = new Unifier();
// For each set of equal variables, take the first variable and replace all others by it.
for (Map.Entry<VariableTerm, HashSet<VariableTerm>> variableEqualityEntry : variableToEqualVariables.entrySet()) {
VariableTerm variableToReplace = variableEqualityEntry.getKey();
VariableTerm replacementVariable = variableEqualityEntry.getValue().iterator().next();
if (variableToReplace == replacementVariable) {
continue;
}
replacementSubstitution.put(variableToReplace, replacementVariable);
}
// Replace/Substitute in each literal every term where one of the common variables occurs.
Iterator<Literal> bodyIterator = rewrittenBody.iterator();
while (bodyIterator.hasNext()) {
Literal literal = bodyIterator.next();
if (equalitiesToRemove.contains(literal)) {
bodyIterator.remove();
}
for (int i = 0; i < literal.getTerms().size(); i++) {
Term replaced = literal.getTerms().get(i).substitute(replacementSubstitution);
literal.getTerms().set(i, replaced);
}
}
// Replace variables in head.
if (rewrittenHead != null) {
Atom headAtom = rewrittenHead.getAtom();
for (int i = 0; i < headAtom.getTerms().size(); i++) {
Term replaced = headAtom.getTerms().get(i).substitute(replacementSubstitution);
headAtom.getTerms().set(i, replaced);
}
}
return new BasicRule(rewrittenHead, rewrittenBody);
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.Atom in project Alpha by alpha-asp.
the class AbstractLiteralInstantiationStrategy method getAcceptedSubstitutions.
/**
* See {@link LiteralInstantiationStrategy#getAcceptedSubstitutions(Literal, BasicSubstitution)}.
*
* A very general implementation of the basic steps needed to obtain ground substitutions for a positive literal.
* Potentially valid ground instances are obtained using {@link AbstractLiteralInstantiationStrategy#computeCandidateInstances(Atom)}, then
* checked, such that each candidate instance unifies with the given partial substitution and has a "valid" {@link AssignmentStatus},
* where "validity" of an {@link AssignmentStatus} is determined using the abstract method
* {@link AbstractLiteralInstantiationStrategy#assignmentStatusAccepted(AssignmentStatus)}.
*/
@Override
public final List<ImmutablePair<Substitution, AssignmentStatus>> getAcceptedSubstitutions(Literal lit, Substitution partialSubstitution) {
Atom atom = lit.getAtom();
Iterable<Instance> groundInstances = this.computeCandidateInstances(atom);
return this.buildSubstitutionsFromInstances(atom, groundInstances, partialSubstitution);
}
Aggregations