use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class HomomorphismTest method test6.
@Test
public void test6() throws HomomorphismException, IteratorException, AtomSetException {
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b), p(a,c), r(a,d), q(d), r(a,e)."));
Variable y = DefaultTermFactory.instance().createVariable("Y");
Variable z = DefaultTermFactory.instance().createVariable("Z");
InMemoryAtomSet positivePart = new LinkedListAtomSet();
positivePart.addAll(DlgpParser.parseAtomSet("p(X,Y),r(X,Z)."));
CloseableIteratorWithoutException<Atom> it = positivePart.iterator();
it.next().setTerm(1, y);
it.next().setTerm(1, z);
LinkedList<InMemoryAtomSet> parts = new LinkedList<InMemoryAtomSet>();
InMemoryAtomSet negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(Y)."));
negatedPart.iterator().next().setTerm(0, y);
parts.add(negatedPart);
negatedPart = new LinkedListAtomSet();
negatedPart.addAll(DlgpParser.parseAtomSet("q(Z)."));
negatedPart.iterator().next().setTerm(0, z);
parts.add(negatedPart);
DefaultConjunctiveQueryWithNegatedParts query = new DefaultConjunctiveQueryWithNegatedParts(positivePart, parts);
BacktrackHomomorphismWithNegatedParts h = new BacktrackHomomorphismWithNegatedParts();
CloseableIterator<Substitution> res = h.execute(query, data);
Assert.assertTrue(res.hasNext());
res.next();
Assert.assertFalse(res.hasNext());
res.close();
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method execute.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery query, AtomSet facts) throws HomomorphismException {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(query.toString());
}
if (profiler != null) {
profiler.start("preprocessing time");
}
Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(query);
List<Variable> orderedVars = order(pair.getLeft().getAtomSet().getVariables());
Collection<Atom>[] queryAtomRanked = getAtomRank(pair.getLeft().getAtomSet(), orderedVars);
if (profiler != null) {
profiler.stop("preprocessing time");
}
try {
this.domain = facts.getTerms();
if (profiler != null) {
profiler.start("backtracking time");
}
CloseableIterator<Substitution> results;
if (isHomomorphism(queryAtomRanked[0], facts, new HashMapSubstitution())) {
results = new CloseableIteratorAdapter<Substitution>(homomorphism(pair.getLeft(), queryAtomRanked, facts, new HashMapSubstitution(), orderedVars, 1).iterator());
} else {
// return false
results = new CloseableIteratorAdapter<Substitution>(Collections.<Substitution>emptyList().iterator());
}
if (profiler != null) {
profiler.stop("backtracking time");
}
if (!pair.getRight().getTerms().isEmpty()) {
results = new ConverterCloseableIterator<Substitution, Substitution>(results, new EqualityHandlerConverter(pair.getRight()));
}
return results;
} catch (Exception e) {
throw new HomomorphismException(e.getMessage(), e);
}
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class RecursiveBacktrackHomomorphism method homomorphism.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private Collection<Substitution> homomorphism(ConjunctiveQuery query, Collection<Atom>[] queryAtomRanked, AtomSet facts, Substitution substitution, List<Variable> orderedVars, int rank) throws Exception {
Collection<Substitution> substitutionList = new LinkedList<Substitution>();
if (orderedVars.size() == 0) {
Substitution filteredSub = new HashMapSubstitution();
for (Term var : query.getAnswerVariables()) {
if (var.isVariable()) {
filteredSub.put((Variable) var, substitution.createImageOf(var));
}
}
substitutionList.add(filteredSub);
} else {
Term var = orderedVars.remove(0);
if (var.isVariable()) {
for (Term substitut : domain) {
Substitution tmpSubstitution = new HashMapSubstitution(substitution);
tmpSubstitution.put((Variable) var, substitut);
// Test partial homomorphism
if (isHomomorphism(queryAtomRanked[rank], facts, tmpSubstitution))
substitutionList.addAll(homomorphism(query, queryAtomRanked, facts, tmpSubstitution, new LinkedList<Variable>(orderedVars), rank + 1));
}
}
}
return substitutionList;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class BCCScheduler method constructHyperGraph.
/**
* The HyperGraph of variables of h. There is an hyper edge between a set of
* variables if they appear in a same atom.
*
* @param h
* @return the HyperGraph of variables of h.
*/
protected static HyperGraph constructHyperGraph(InMemoryAtomSet h, int nbVariables, Term[] inverseMap, Map<Term, Integer> map, Iterable<Term> ans) {
HyperGraph graph = new DefaultHyperGraph(nbVariables + 1);
CloseableIteratorWithoutException<Atom> it = h.iterator();
while (it.hasNext()) {
Atom a = it.next();
DefaultHyperEdge edge = new DefaultHyperEdge();
int arity = 0;
for (Variable t : a.getVariables()) {
++arity;
edge.addVertice(map.get(t));
}
if (arity >= 2) {
graph.add(edge);
}
}
return graph;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class BCCScheduler method execute.
@Override
public VarSharedData[] execute(InMemoryAtomSet query, Set<Variable> preAffectedVars, List<Term> ans, AtomSet data, RulesCompilation rc) {
InMemoryAtomSet fixedQuery = (preAffectedVars.isEmpty()) ? query : computeFixedQuery(query, preAffectedVars);
// Term index
Set<Variable> variables = fixedQuery.getVariables();
Map<Term, Integer> map = new HashMap<Term, Integer>();
this.inverseMap = new Term[variables.size() + 1];
{
// init indexes
int i = 0;
for (Variable t : variables) {
inverseMap[++i] = t;
map.put(t, i);
}
}
HyperGraph graph = constructHyperGraph(fixedQuery, variables.size(), this.inverseMap, map, ans);
double[] proba;
if (data instanceof Store) {
proba = this.computeProba(fixedQuery, (Store) data, variables.size(), map, rc);
} else {
proba = new double[variables.size() + 1];
Arrays.fill(proba, 1);
}
// bias proba of answer variables
for (Term t : ans) {
if (t.isVariable()) {
int idx = map.get(t);
proba[idx] *= ansVariableFactor;
}
}
this.varComparator = new IntegerComparator(proba);
TmpData d = biconnect(graph, this.varComparator);
VarSharedData[] vars = new VarSharedData[variables.size() + 2];
this.BCC.varData = new VarData[variables.size() + 2];
vars[0] = new VarSharedData(0);
this.BCC.varData[0] = new VarData();
int lastAnswerVariable = -1;
for (int i = 1; i < d.vars.length; ++i) {
VarSharedData v = d.vars[i];
vars[v.level] = v;
this.BCC.varData[v.level] = d.ext[i];
v.value = (Variable) this.inverseMap[i];
v.nextLevel = v.level + 1;
v.previousLevel = v.level - 1;
if (this.withForbiddenCandidate && this.BCC.varData[v.level].isAccesseur) {
this.BCC.varData[v.level].forbidden = new HashSet<Term>();
}
if (ans.contains(v.value)) {
if (v.level > lastAnswerVariable)
lastAnswerVariable = v.level;
}
}
int level = variables.size() + 1;
vars[level] = new VarSharedData(level);
this.BCC.varData[level] = new VarData();
// if an homomorphism is found, go to the last answer variable
vars[level].previousLevel = lastAnswerVariable;
// Profiling
if (this.getProfiler().isProfilingEnabled()) {
StringBuilder sb = new StringBuilder();
for (VarSharedData v : vars) {
sb.append(v.value);
sb.append(" > ");
}
this.getProfiler().put("BCCOrder", sb.toString());
}
return vars;
}
Aggregations