Search in sources :

Example 61 with Term

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;
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term)

Example 62 with Term

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);
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 63 with Term

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);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Constant(fr.lirmm.graphik.graal.api.core.Constant) Store(fr.lirmm.graphik.graal.api.store.Store) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 64 with Term

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) TreeSet(java.util.TreeSet) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Example 65 with Term

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);
}
Also used : TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Aggregations

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8