use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class HomomorphismIteratorChecker method next.
@Override
public Term next() throws IteratorException {
this.hasNext();
Term t = this.next;
this.next = null;
return t;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class BacktrackUtils method createImageOf.
/**
* @param atom
* @param map
* @return an image of specified atom obtained by replacement variables contained in the map with the associated Var.image.
*/
public static Atom createImageOf(Atom atom, Substitution initialSubstitution, Map<Variable, Integer> map, Var[] varData) {
Term[] termsSubstitut = new Term[atom.getPredicate().getArity()];
int i = -1;
for (Term term : atom) {
if (term.isVariable()) {
Term t = initialSubstitution.createImageOf(term);
termsSubstitut[++i] = t.isVariable() ? imageOf((Variable) t, map, varData) : t;
} else {
termsSubstitut[++i] = term;
}
}
return new DefaultAtom(atom.getPredicate(), termsSubstitut);
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class StatBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation rc) throws BacktrackException {
if (!(data instanceof Store)) {
return fallback.exec(v, preAtoms, postAtoms, data, rc);
}
Store store = (Store) data;
Set<Term> terms = null;
if (this.getProfiler() != null) {
this.getProfiler().start("BootstrapTime");
this.getProfiler().start("BootstrapTimeFirstPart");
}
Iterator<Atom> it;
Collection<Constant> constants = null;
Atom aa = null;
it = postAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
try {
if (constants != null && !constants.isEmpty()) {
terms = new HashSet<Term>();
for (Pair<Atom, Substitution> im : rc.getRewritingOf(aa)) {
int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
CloseableIterator<Atom> match = data.match(im.getLeft());
while (match.hasNext()) {
terms.add(match.next().getTerm(pos));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTimeFirstPart");
}
if (terms == null) {
Atom a = null, tmp;
double probaA = 1.1;
it = postAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
terms = BootstrapperUtils.computeCandidatesOverRewritings(a, v, data, rc);
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTime");
}
if (terms == null) {
return data.termsIterator();
} else {
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
} catch (AtomSetException e) {
throw new BacktrackException(e);
} catch (IteratorException e) {
throw new BacktrackException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class DefaultScheduler method execute.
@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) {
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 (Term t : ans) {
if (t instanceof Variable && !alreadyAffected.contains(t)) {
++level;
vars[level] = new VarSharedData(level);
vars[level].value = (Variable) t;
alreadyAffected.add(t);
}
}
int lastAnswerVariable = level;
for (Term t : terms) {
if (!alreadyAffected.contains(t)) {
++level;
vars[level] = new VarSharedData(level);
vars[level].value = (Variable) t;
}
}
++level;
vars[level] = new VarSharedData(level);
vars[level].previousLevel = lastAnswerVariable;
return vars;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class TripleStoreTest method simpleTest.
@Theory
public void simpleTest(AtomSet store) throws AtomSetException, IteratorException {
Assume.assumeTrue(store instanceof TripleStore);
Term t1 = DefaultTermFactory.instance().createConstant("http://to.to/b");
Term t2 = DefaultTermFactory.instance().createConstant("http://to.to/a");
Predicate p = new Predicate("http://to.to/p", 2);
Atom atom1 = new DefaultAtom(p, t1, t2);
store.add(atom1);
int i = 0;
for (CloseableIterator<Atom> it = store.iterator(); it.hasNext(); it.next()) {
++i;
}
Assert.assertEquals(1, i);
}
Aggregations