use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class BacktrackUtils method isHomomorphism.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
* @param atomsFrom
* @param atomsTo
* @param index
* @param rc
* @return true if there is a homomorphism, false otherwise.
* @throws AtomSetException
*/
public static boolean isHomomorphism(Iterable<Atom> atomsFrom, AtomSet atomsTo, Substitution initialSubstitution, Map<Variable, Integer> index, Var[] varData, RulesCompilation rc) throws AtomSetException {
for (Atom atom : atomsFrom) {
Atom image = BacktrackUtils.createImageOf(atom, initialSubstitution, index, varData);
boolean contains = false;
for (Pair<Atom, Substitution> p : rc.getRewritingOf(image)) {
if (atomsTo.contains(p.getLeft())) {
contains = true;
break;
}
}
if (!contains)
return false;
}
return true;
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class EqualityUtils method processEquality.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
* This method produces a conjunctive query based on the specified one where
* equality atoms are removed and affected variables are replaced or merged.
* It returns the new query and a substitution which allow to rebuild
* answers to the original query based on answers to the returned one by
* composition of each answer with the returned substitution.
*
* @param q
* a conjunctive query
* @return a pair composed of the computed conjunctive query and the
* substitution which allow to rebuild answers.
*/
public static Pair<ConjunctiveQuery, Substitution> processEquality(ConjunctiveQuery q) {
LinkedList<Atom> toRemove = new LinkedList<Atom>();
Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
while (it.hasNext()) {
Atom a = it.next();
if (Predicate.EQUALITY.equals(a.getPredicate())) {
if (a.getTerm(0).isVariable()) {
if (!updateSubstitution(s, (Variable) a.getTerm(0), a.getTerm(1))) {
return generateBottomResult();
}
toRemove.add(a);
} else if (a.getTerm(1).isVariable()) {
if (!updateSubstitution(s, (Variable) a.getTerm(1), a.getTerm(0))) {
return generateBottomResult();
}
toRemove.add(a);
} else {
return generateBottomResult();
}
}
}
return new ImmutablePair<ConjunctiveQuery, Substitution>(generateQuery(q, s, toRemove), s);
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class DlgpListener method createAtomSet.
@Override
protected void createAtomSet(InMemoryAtomSet atomset) {
FreshVarSubstitution s = new FreshVarSubstitution(DlgpParser.freeVarGen);
CloseableIteratorWithoutException<Atom> it = atomset.iterator();
while (it.hasNext()) {
Atom a = it.next();
this.set.write(s.createImageOf(a));
}
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class AbstractNFC method init.
@Override
public void init(VarSharedData[] vars, Map<Variable, Integer> map) {
this.data = new VarData[vars.length];
for (int i = 0; i < vars.length; ++i) {
this.data[vars[i].level] = new VarData();
this.data[vars[i].level].candidats = new AcceptableCandidats[vars.length];
this.data[vars[i].level].tmp = new HashSet<Term>();
this.data[vars[i].level].toCheckAfterAssignment = new LinkedList<Atom>();
for (Atom a : vars[i].preAtoms) {
int cpt = 0;
boolean toAdd = true;
for (Variable t : a.getVariables()) {
if (map.containsKey(t)) {
if (t.equals(vars[i].value))
++cpt;
else
toAdd = false;
}
}
if (toAdd || cpt > 1) {
this.data[vars[i].level].toCheckAfterAssignment.add(a);
}
}
AcceptableCandidats previous = new AcceptableCandidats();
for (VarSharedData z : vars[i].preVars) {
AcceptableCandidats ac = new AcceptableCandidats();
ac.candidats = new TreeSet<Term>();
ac.previous = previous;
previous = ac;
this.data[vars[i].level].candidats[z.level] = ac;
}
this.data[vars[i].level].last = previous;
}
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class AbstractNFC method check.
protected boolean check(Atom atom, VarSharedData currentVar, VarSharedData varToCompute, AtomSet g, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData, RulesCompilation rc) throws AtomSetException {
Substitution s = BacktrackUtils.createSubstitution(varData);
s.put(initialSubstitution);
Atom im = s.createImageOf(atom);
this.data[varToCompute.level].tmp.clear();
Set<Term> candidats = this.data[varToCompute.level].candidats[currentVar.level].candidats;
// FIXMEÂ bug with p(X,Y,Z) -> q(X,Y) in the compilation
for (Pair<Atom, Substitution> rew : rc.getRewritingOf(im)) {
Atom a = rew.getLeft();
Iterator<Term> it = candidats.iterator();
while (it.hasNext()) {
Term t = it.next();
Atom fullInstantiatedAtom = Substitutions.createImageOf(a, varToCompute.value, t);
Profiler profiler = this.getProfiler();
if (profiler != null) {
profiler.incr("#check", 1);
profiler.start("checkTime");
}
if (g.contains(fullInstantiatedAtom)) {
this.data[varToCompute.level].tmp.add(t);
}
if (profiler != null) {
profiler.stop("checkTime");
}
}
}
candidats.retainAll(this.data[varToCompute.level].tmp);
this.data[varToCompute.level].tmp.clear();
if (candidats.isEmpty()) {
this.bj.addNeighborhoodToBackjumpSet(varToCompute, currentVar);
return false;
} else {
return true;
}
}
Aggregations