Search in sources :

Example 51 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class RDF4jStore method termsByPredicatePosition.

@Override
public CloseableIterator<Term> termsByPredicatePosition(Predicate p, int position) throws AtomSetException {
    TupleQuery query = null;
    TupleQueryResult results = null;
    try {
        if (position == 0) {
            query = this.connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT DISTINCT ?x WHERE { ?x <" + utils.createURI(p) + "> ?y }");
        } else if (position == 1) {
            query = this.connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT DISTINCT ?x WHERE { ?y <" + utils.createURI(p) + "> ?x }");
        } else {
            throw new WrongArityException("Position should be 0 for subject or 1 for object.");
        }
        results = query.evaluate();
    } catch (RepositoryException e) {
        throw new AtomSetException(e);
    } catch (MalformedQueryException e) {
        throw new AtomSetException(e);
    } catch (QueryEvaluationException e) {
        throw new AtomSetException(e);
    }
    return new TermsIterator(results, "x", this.utils);
}
Also used : QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) WrongArityException(fr.lirmm.graphik.graal.api.store.WrongArityException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult)

Example 52 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class AbstractAtomSet method getTerms.

@Override
public Set<Term> getTerms() throws AtomSetException {
    Set<Term> terms = new HashSet<Term>();
    CloseableIterator<Atom> atomIt = this.iterator();
    try {
        while (atomIt.hasNext()) {
            Iterator<Term> termIt = atomIt.next().iterator();
            while (termIt.hasNext()) {
                terms.add(termIt.next());
            }
        }
    } catch (Exception e) {
        throw new AtomSetException(e);
    }
    return terms;
}
Also used : AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) HashSet(java.util.HashSet)

Example 53 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class AbstractAtomSet method getLiterals.

@Override
public Set<Literal> getLiterals() throws AtomSetException {
    Set<Literal> terms = new HashSet<Literal>();
    CloseableIterator<Atom> atomIt = this.iterator();
    try {
        while (atomIt.hasNext()) {
            Iterator<Term> termIt = atomIt.next().iterator();
            while (termIt.hasNext()) {
                Term t = termIt.next();
                if (t.isLiteral()) {
                    terms.add((Literal) t);
                }
            }
        }
    } catch (Exception e) {
        throw new AtomSetException(e);
    }
    return terms;
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) HashSet(java.util.HashSet)

Example 54 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class AbstractAtomSet method getVariables.

@Override
public Set<Variable> getVariables() throws AtomSetException {
    Set<Variable> terms = new HashSet<Variable>();
    CloseableIterator<Atom> atomIt = this.iterator();
    try {
        while (atomIt.hasNext()) {
            Iterator<Term> termIt = atomIt.next().iterator();
            while (termIt.hasNext()) {
                Term t = termIt.next();
                if (t.isVariable()) {
                    terms.add((Variable) t);
                }
            }
        }
    } catch (Exception e) {
        throw new AtomSetException(e);
    }
    return terms;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) HashSet(java.util.HashSet)

Example 55 with AtomSetException

use of fr.lirmm.graphik.graal.api.core.AtomSetException in project graal by graphik-team.

the class StarBootstrapper method exec.

// /////////////////////////////////////////////////////////////////////////
// 
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation compilation) throws BacktrackException {
    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 TreeSet<Term>(TermValueComparator.instance());
            for (Pair<Atom, Substitution> im : compilation.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) {
            it = postAtoms.iterator();
            while (it.hasNext()) {
                if (terms == null) {
                    terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
                } else {
                    terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
                }
            }
            it = preAtoms.iterator();
            while (it.hasNext()) {
                if (terms == null) {
                    terms = BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation);
                } else {
                    terms.retainAll(BootstrapperUtils.computeCandidatesOverRewritings(it.next(), v, data, compilation));
                }
            }
        }
        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) 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)

Aggregations

AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)69 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)26 SQLException (java.sql.SQLException)26 Atom (fr.lirmm.graphik.graal.api.core.Atom)25 Term (fr.lirmm.graphik.graal.api.core.Term)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)12 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)11 Statement (java.sql.Statement)11 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)10 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)9 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)7 TreeSet (java.util.TreeSet)7 SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)6 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)6 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)5 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)5 TreeMap (java.util.TreeMap)5 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)4 Variable (fr.lirmm.graphik.graal.api.core.Variable)4