use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class Utils method developpRewriting.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
// /////////////////////////////////////////////////////////////////////////
/**
* Add in the given rewriting set the rewrites that can be entailed from the
* predicate order ex: the rewrite A(x) can be entailed from the rewrite
* B(x) and the predicate order A > B
*
* @return a Collection of unfolded rewritings.
*/
private static Collection<ConjunctiveQuery> developpRewriting(Iterable<ConjunctiveQuery> rewritingSet, RulesCompilation compilation) {
Collection<ConjunctiveQuery> unfoldingRewritingSet = new LinkedList<ConjunctiveQuery>();
LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesBefore = new LinkedList<Pair<InMemoryAtomSet, Substitution>>();
LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesAfter = new LinkedList<Pair<InMemoryAtomSet, Substitution>>();
LinkedList<Pair<InMemoryAtomSet, Substitution>> newQueriesTmp;
Iterable<Pair<Atom, Substitution>> atomsRewritings;
InMemoryAtomSet copy;
for (ConjunctiveQuery originalQuery : rewritingSet) {
if (Thread.currentThread().isInterrupted()) {
break;
}
// q = query.getIrredondant(compilation);
// for all atom of the query we will build a list of all the
// rewriting
newQueriesBefore.clear();
newQueriesBefore.add(new ImmutablePair<InMemoryAtomSet, Substitution>(new LinkedListAtomSet(), DefaultSubstitutionFactory.instance().createSubstitution()));
// we will build all the possible fact from the rewriting of the
// atoms
CloseableIteratorWithoutException<Atom> it = originalQuery.iterator();
while (!Thread.currentThread().isInterrupted() && it.hasNext()) {
Atom a = it.next();
atomsRewritings = compilation.getRewritingOf(a);
for (Pair<InMemoryAtomSet, Substitution> before : newQueriesBefore) {
// query and add the atom
for (Pair<Atom, Substitution> rew : atomsRewritings) {
//
copy = new LinkedListAtomSet(before.getLeft());
copy.add(rew.getLeft());
Substitution newSub = Substitutions.aggregate(before.getRight(), rew.getRight());
if (newSub != null) {
newQueriesAfter.add(new ImmutablePair<InMemoryAtomSet, Substitution>(copy, newSub));
}
}
}
// switch list
newQueriesTmp = newQueriesBefore;
newQueriesBefore = newQueriesAfter;
newQueriesAfter = newQueriesTmp;
newQueriesAfter.clear();
}
for (Pair<InMemoryAtomSet, Substitution> before : newQueriesBefore) {
if (Thread.currentThread().isInterrupted()) {
break;
}
Substitution s = before.getRight();
InMemoryAtomSet atomset = before.getLeft();
atomset = s.createImageOf(atomset);
List<Term> ans = s.createImageOf(originalQuery.getAnswerVariables());
unfoldingRewritingSet.add(DefaultConjunctiveQueryFactory.instance().create(atomset, ans));
}
}
return unfoldingRewritingSet;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class ConjunctiveQueryWithFixedVariables method computeFixedQuery.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private static InMemoryAtomSet computeFixedQuery(InMemoryAtomSet atomset, Iterable<? extends Term> fixedTerms) {
// create a Substitution for fixed query
InMemoryAtomSet fixedQuery = DefaultAtomSetFactory.instance().create();
Substitution fixSub = DefaultSubstitutionFactory.instance().createSubstitution();
for (Term t : fixedTerms) {
if (t.isVariable()) {
fixSub.put((Variable) t, DefaultTermFactory.instance().createConstant(t.getLabel()));
}
}
// apply substitution
CloseableIteratorWithoutException<Atom> it = atomset.iterator();
while (it.hasNext()) {
Atom a = it.next();
fixedQuery.add(fixSub.createImageOf(a));
}
return fixedQuery;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class ProbaUtils method computeProba.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
* Compute the probability to have one specific instance of the specified
* atom over data
*
* @param atom
* @param data
* @param rc
* @return the probability to have the specified atom over specified data.
*/
public static double computeProba(Atom atom, Store data, RulesCompilation rc) {
int count = 0;
for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
count += data.size(im.getLeft().getPredicate());
}
double probaA;
if (count == 0) {
probaA = 0.0;
} else {
int nbCst = 0;
int nbVar = 0;
for (Term t : atom) {
if (t.isConstant()) {
++nbCst;
} else {
++nbVar;
}
}
int domSize = data.getDomainSize();
probaA = (count / (Math.pow(domSize, nbCst)) / Math.pow(data.getDomainSize(), nbVar));
}
return probaA;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class ForwardCheckingTest method simpleFCTest1.
@Test
public void simpleFCTest1() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
Profiler profiler = new CPUTimeProfiler();
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b,c)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(Y,Z).");
Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new SimpleFC());
h.setProfiler(profiler);
CloseableIterator<Substitution> results = h.execute(query, data);
while (results.hasNext()) {
results.next();
}
results.close();
Assert.assertEquals(7, profiler.get("#calls"));
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class ForwardCheckingTest method NFC2Test2.
@Test
public void NFC2Test2() throws HomomorphismException, IteratorException, ParseException {
Profiler profiler = new CPUTimeProfiler();
Predicate[] predicates = { new Predicate("p", 2), new Predicate("q", 2), new Predicate("r", 2) };
InMemoryAtomSet data = new DefaultInMemoryGraphStore();
TestUtil.addNAtoms(data, 32, predicates, 5, new Random(0));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(X,Z), r(Y,Z).");
Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new NFC2());
h.setProfiler(profiler);
CloseableIterator<Substitution> results = h.execute(query, data);
while (results.hasNext()) {
results.next();
}
results.close();
}
Aggregations