use of fr.lirmm.graphik.graal.api.core.Variable 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.Variable in project graal by graphik-team.
the class AbstractDlgpListener method endsConjunction.
@Override
public void endsConjunction(OBJECT_TYPE objectType) {
switch(objectType) {
case QUERY:
Set<Variable> bodyVars = this.atomSet.getVariables();
for (Term t : this.answerVars) {
if (t.isVariable() && !bodyVars.contains(t)) {
throw new ParseError("The variable [" + t + "] of the answer list does not appear in the query body.");
}
}
this.createQuery(DefaultConjunctiveQueryFactory.instance().create(this.label, this.atomSet, this.answerVars));
break;
case NEG_CONSTRAINT:
this.createNegConstraint(new DefaultNegativeConstraint(this.label, this.atomSet));
break;
case RULE:
if (this.atomSet2 == null) {
this.atomSet2 = this.atomSet;
this.atomSet = new LinkedListAtomSet();
} else {
this.createRule(DefaultRuleFactory.instance().create(this.label, this.atomSet, this.atomSet2));
}
break;
case FACT:
this.createAtomSet(this.atomSet);
break;
default:
break;
}
}
use of fr.lirmm.graphik.graal.api.core.Variable 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.Variable in project graal by graphik-team.
the class FixedOrderScheduler method execute.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) throws HomomorphismException {
Set<Variable> terms = h.getVariables();
VarSharedData[] vars = new VarSharedData[terms.size() + 2];
int level = 0;
vars[level] = new VarSharedData(level);
Set<Term> alreadyAffected = new TreeSet<Term>();
for (Variable v : this.order) {
if (!terms.contains(v)) {
throw new HomomorphismException("Try to schedule a variable which is not in the query :" + v);
}
if (alreadyAffected.contains(v)) {
throw new HomomorphismException("There is two occurences of the same variable in the specified order.");
}
++level;
vars[level] = new VarSharedData(level);
vars[level].value = v;
alreadyAffected.add(v);
}
terms.removeAll(alreadyAffected);
if (!terms.isEmpty()) {
throw new HomomorphismException("Some variables of the query are not scheduled :" + terms);
}
++level;
vars[level] = new VarSharedData(level);
vars[level].previousLevel = level - 1;
return vars;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class BacktrackIteratorData method computeAtomOrder.
/**
* The index 0 contains the fully instantiated atoms.
*
* @param atomset
* @param vars
*/
private static void computeAtomOrder(CloseableIterableWithoutException<Atom> atomset, VarSharedData[] vars, Map<Variable, Integer> index) {
int tmp, rank;
// initialisation preAtoms and postAtoms Collections
for (int i = 0; i < vars.length; ++i) {
vars[i].preAtoms = new HashSet<Atom>();
vars[i].postAtoms = new HashSet<Atom>();
vars[i].postVars = new HashSet<VarSharedData>();
vars[i].preVars = new TreeSet<VarSharedData>();
}
//
CloseableIteratorWithoutException<Atom> it = atomset.iterator();
while (it.hasNext()) {
Atom a = it.next();
rank = 0;
for (Variable t : a.getVariables()) {
Integer idx = index.get(t);
if (idx != null) {
tmp = vars[idx].level;
vars[tmp].postAtoms.add(a);
if (rank < tmp)
rank = tmp;
}
}
vars[rank].postAtoms.remove(a);
vars[rank].preAtoms.add(a);
}
for (int i = 0; i < vars.length; ++i) {
for (Atom a : vars[i].postAtoms) {
for (Variable t : a.getVariables()) {
Integer idx = index.get(t);
if (idx != null) {
if (vars[idx].level > i) {
vars[i].postVars.add(vars[idx]);
vars[idx].preVars.add(vars[i]);
}
}
}
}
}
}
Aggregations