Search in sources :

Example 1 with VariableTerm

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

the class NonGroundRule method sortAtoms.

/**
	 * Sorts atoms such that the join-order of the atoms is improved (= cannot degenerate into cross-product).
	 * Note that the below sorting can be improved to yield smaller joins.
	 */
private List<Atom> sortAtoms(List<Atom> atoms) {
    final Set<SortingBodyComponent> components = new LinkedHashSet<>();
    final Set<BuiltinAtom> builtinAtoms = new LinkedHashSet<>();
    for (Atom atom : atoms) {
        if (atom instanceof BuiltinAtom) {
            // Sort out builtin atoms (we consider them as not creating new bindings)
            builtinAtoms.add((BuiltinAtom) atom);
            continue;
        }
        final Set<SortingBodyComponent> hits = new LinkedHashSet<>();
        // For each variable
        for (VariableTerm variableTerm : atom.getOccurringVariables()) {
            // Find all components it also occurs and record in hitting components
            for (SortingBodyComponent component : components) {
                if (component.occurringVariables.contains(variableTerm)) {
                    hits.add(component);
                }
            }
        }
        // If no components were hit, create new component, else merge components
        if (hits.isEmpty()) {
            components.add(new SortingBodyComponent(atom));
            continue;
        }
        // If only one component hit, add atom to it
        if (hits.size() == 1) {
            hits.iterator().next().add(atom);
            continue;
        }
        // Merge all components that are hit by the current atom
        SortingBodyComponent firstComponent = hits.iterator().next();
        firstComponent.add(atom);
        for (SortingBodyComponent hitComponent : hits) {
            if (hitComponent != firstComponent) {
                firstComponent.merge(hitComponent);
                // Remove merged component from the set of available components
                components.remove(hitComponent);
            }
        }
    }
    // Components now contains all components that are internally connected but not connected to another component
    List<Atom> sortedPositiveBodyAtoms = new ArrayList<>(components.size());
    for (SortingBodyComponent component : components) {
        sortedPositiveBodyAtoms.addAll(component.atomSequence);
    }
    // Put builtin atoms after positive literals and before negative ones.
    sortedPositiveBodyAtoms.addAll(builtinAtoms);
    return sortedPositiveBodyAtoms;
}
Also used : BuiltinAtom(at.ac.tuwien.kr.alpha.common.BuiltinAtom) VariableTerm(at.ac.tuwien.kr.alpha.common.VariableTerm) 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)

Example 2 with VariableTerm

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

the class Substitution method toUniformString.

/**
	 * Prints the variable substitution in a uniform way (sorted by variable names).
	 *
	 * @return
	 */
public String toUniformString() {
    List<VariableTerm> variablesInSubstitution = new ArrayList<>(substitution.size());
    variablesInSubstitution.addAll(substitution.keySet());
    // Hint: Maybe this is a performance issue later, better have sorted/well-defined insertion into Substitution.
    Collections.sort(variablesInSubstitution);
    StringBuilder ret = new StringBuilder();
    for (VariableTerm variableTerm : variablesInSubstitution) {
        ret.append("_").append(variableTerm).append(":").append(substitution.get(variableTerm));
    }
    return ret.toString();
}
Also used : ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.common.VariableTerm)

Example 3 with VariableTerm

use of at.ac.tuwien.kr.alpha.common.VariableTerm 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

VariableTerm (at.ac.tuwien.kr.alpha.common.VariableTerm)3 Atom (at.ac.tuwien.kr.alpha.common.Atom)2 BasicAtom (at.ac.tuwien.kr.alpha.common.BasicAtom)2 BuiltinAtom (at.ac.tuwien.kr.alpha.common.BuiltinAtom)2 ParsedAtom (at.ac.tuwien.kr.alpha.grounder.parser.ParsedAtom)2 ArrayList (java.util.ArrayList)1