use of at.ac.tuwien.kr.alpha.common.BuiltinAtom 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;
}
use of at.ac.tuwien.kr.alpha.common.BuiltinAtom 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();
}
Aggregations