Search in sources :

Example 1 with BasicAtom

use of at.ac.tuwien.kr.alpha.common.BasicAtom in project Alpha by alpha-asp.

the class ChoiceGrounder method assignmentToAnswerSet.

@Override
public AnswerSet assignmentToAnswerSet(Iterable<Integer> trueAtoms) {
    SortedSet<Predicate> trueAtomPredicates = new TreeSet<>();
    for (int trueAtom : trueAtoms) {
        BasicPredicate atomPredicate = new BasicPredicate(atomIdToString.get(trueAtom), 0);
        if (!filter.test(atomPredicate)) {
            continue;
        }
        if (atomPredicate.getPredicateName().startsWith("_")) {
            continue;
        }
        trueAtomPredicates.add(atomPredicate);
    }
    // Add the atom instances
    Map<Predicate, SortedSet<Atom>> predicateInstances = new HashMap<>();
    for (Predicate trueAtomPredicate : trueAtomPredicates) {
        BasicAtom basicAtom = new BasicAtom(trueAtomPredicate);
        predicateInstances.put(trueAtomPredicate, new TreeSet<>(singleton(basicAtom)));
    }
    // 	 only one predicate instance representing 0 terms.
    return new BasicAnswerSet(trueAtomPredicates, predicateInstances);
}
Also used : BasicAtom(at.ac.tuwien.kr.alpha.common.BasicAtom) Predicate(at.ac.tuwien.kr.alpha.common.Predicate)

Example 2 with BasicAtom

use of at.ac.tuwien.kr.alpha.common.BasicAtom in project Alpha by alpha-asp.

the class NonGroundRule method isSafe.

/**
	 * Checks whether a rule is safe. A rule is safe iff all negated variables and all variables occurring in the
	 * head also occur in the positive body).
	 * @return true if this rule is safe.
	 */
private boolean isSafe() {
    Set<VariableTerm> positiveVariables = new HashSet<>();
    Set<VariableTerm> builtinVariables = new HashSet<>();
    // Check that all negative variables occur in the positive body.
    for (Atom posAtom : bodyAtomsPositive) {
        // implementations of the Atom interface. Not nice.
        if (posAtom instanceof BasicAtom) {
            positiveVariables.addAll(posAtom.getOccurringVariables());
        } else if (posAtom instanceof BuiltinAtom) {
            builtinVariables.addAll(posAtom.getOccurringVariables());
        }
    }
    for (Atom negAtom : bodyAtomsNegative) {
        for (VariableTerm term : negAtom.getOccurringVariables()) {
            if (!positiveVariables.contains(term)) {
                return false;
            }
        }
    }
    for (VariableTerm builtinVariable : builtinVariables) {
        if (!positiveVariables.contains(builtinVariable)) {
            return false;
        }
    }
    // Constraint are safe at this point
    if (isConstraint()) {
        return true;
    }
    // Check that all variables of the head occur in the positive body.
    List<VariableTerm> headVariables = headAtom.getOccurringVariables();
    headVariables.removeAll(positiveVariables);
    return headVariables.isEmpty();
}
Also used : BuiltinAtom(at.ac.tuwien.kr.alpha.common.BuiltinAtom) VariableTerm(at.ac.tuwien.kr.alpha.common.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.common.BasicAtom) BuiltinAtom(at.ac.tuwien.kr.alpha.common.BuiltinAtom) ParsedAtom(at.ac.tuwien.kr.alpha.grounder.parser.ParsedAtom) BasicAtom(at.ac.tuwien.kr.alpha.common.BasicAtom) Atom(at.ac.tuwien.kr.alpha.common.Atom)

Aggregations

BasicAtom (at.ac.tuwien.kr.alpha.common.BasicAtom)2 Atom (at.ac.tuwien.kr.alpha.common.Atom)1 BuiltinAtom (at.ac.tuwien.kr.alpha.common.BuiltinAtom)1 Predicate (at.ac.tuwien.kr.alpha.common.Predicate)1 VariableTerm (at.ac.tuwien.kr.alpha.common.VariableTerm)1 ParsedAtom (at.ac.tuwien.kr.alpha.grounder.parser.ParsedAtom)1