protected DefaultConjunctiveQueryFactory() { super(); BOOLEAN_BOTTOM_QUERY = this.create( DefaultAtomFactory.instance().create(Predicate.BOTTOM, DefaultTermFactory.instance().createVariable("X")), Collections.<Term>emptyList()); }
@Override public ConjunctiveQuery map(ConjunctiveQuery cq) { return DefaultConjunctiveQueryFactory.instance().create(cq.getLabel(), this.map(cq.getAtomSet()), cq.getAnswerVariables()); }
@Override public ConjunctiveQuery unmap(ConjunctiveQuery cq) { return DefaultConjunctiveQueryFactory.instance().create(cq.getLabel(), this.unmap(cq.getAtomSet()), cq.getAnswerVariables()); }
@Override public CloseableIterator<Atom> match(Atom atom) throws AtomSetException { ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atom); StringWriter s = new StringWriter(); SparqlConjunctiveQueryWriter w = new SparqlConjunctiveQueryWriter(s, this.utils.getURIzer()); try { w.write(query); w.close(); } catch (IOException e1) { throw new AtomSetException("Error while converting to SPARQL " + atom, e1); } TupleQuery sparqlQuery = this.connection.prepareTupleQuery(s.toString()); TupleQueryResult result = sparqlQuery.evaluate(); return new TupleQueryResultAtomIterator(result, atom, utils); }
@Override public CloseableIterator<Atom> match(Atom atom) throws AtomSetException { ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom)); SqlHomomorphism solver = SqlHomomorphism.instance(); try { return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this)); } catch (HomomorphismException e) { throw new AtomSetException(e); } }
private void execute(String queryString) { this.prefixes = new LinkedList<Prefix>(); List<Term> ans = new LinkedList<Term>(); Query sparql = QueryFactory.create(queryString); for (Map.Entry<String, String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) { this.prefixes.add(new Prefix(e.getKey(), e.getValue())); } if (sparql.isSelectType()) { for (String v : sparql.getResultVars()) { ans.add(DefaultTermFactory.instance().createVariable(v)); } } ElementVisitorImpl visitor = new ElementVisitorImpl(DefaultAtomSetFactory.instance().create()); sparql.getQueryPattern().visit(visitor); // check if answer variables appear in the query body Set<Variable> bodyVars = visitor.getAtomSet().getVariables(); for(Term t : ans) { if(t.isVariable() && !bodyVars.contains(t)) { throw new ParseError("The variable ["+ t +"] of the answer list does not appear in the query body."); } } this.query = DefaultConjunctiveQueryFactory.instance().create(visitor.getAtomSet(), ans); }
@Override public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException { List<Term> terms = new LinkedList<Term>(); for (int i = 0; i < p.getArity(); ++i) { terms.add(DefaultTermFactory.instance().createVariable("X" + i)); } Atom atom = DefaultAtomFactory.instance().create(p, terms); ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom)); SqlHomomorphism solver = SqlHomomorphism.instance(); try { return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this)); } catch (HomomorphismException e) { throw new AtomSetException(e); } }
@Override public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException { if (!this.check(p)) { return Iterators.<Atom> emptyIterator(); } List<Term> terms = new LinkedList<Term>(); for (int i = 0; i < p.getArity(); ++i) { terms.add(DefaultTermFactory.instance().createVariable("X" + i)); } Atom atom = DefaultAtomFactory.instance().create(p, terms); ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom)); SqlHomomorphism solver = SqlHomomorphism.instance(); try { return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this)); } catch (HomomorphismException e) { throw new AtomSetException(e); } }
@Override public CloseableIterator<Atom> match(Atom atom) throws AtomSetException { if (!this.check(atom)) { return Iterators.<Atom> emptyIterator(); } ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(new LinkedListAtomSet(atom)); SqlHomomorphism solver = SqlHomomorphism.instance(); try { return new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this)); } catch (HomomorphismException e) { throw new AtomSetException(e); } }
@Override public Iterator<SQLQuery> translate(Rule rangeRestrictedRule) throws AtomSetException { Collection<SQLQuery> queries = new LinkedList<SQLQuery>(); InMemoryAtomSet body = rangeRestrictedRule.getBody(); CloseableIterator<Atom> it = rangeRestrictedRule.getHead().iterator(); try { while (it.hasNext()) { Atom headAtom = it.next(); DBTable table = this.store.createPredicateTableIfNotExist(headAtom.getPredicate()); List<Term> terms = headAtom.getTerms(); ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(body, terms); SQLQuery selectQuery = this.translate(query); if (!selectQuery.hasSchemaError()) { queries.add(new SQLQuery(store.getDriver().getInsertOrIgnoreQuery(table, selectQuery.toString()))); } } } catch (IteratorException e) { throw new AtomSetException(e); } catch (SQLException e) { throw new AtomSetException(e); } return queries.iterator(); }
@Override public boolean hasNext() throws IteratorException { if (!this.hasNextCallDone) { this.hasNextCallDone = true; while (this.predicateIt.hasNext() && (this.atomIt == null || !this.atomIt.hasNext())) { Predicate p = predicateIt.next(); List<Term> terms = new LinkedList<Term>(); VariableGenerator gen = new DefaultVariableGenerator("X"); for(int i=0; i<p.getArity(); ++i) { terms.add(gen.getFreshSymbol()); } InMemoryAtomSet atomSet = new LinkedListAtomSet(); Atom atom = new DefaultAtom(p, terms); atomSet.add(atom); ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atomSet); SqlHomomorphism solver = SqlHomomorphism.instance(); try { this.atomIt = new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this.store)); } catch (HomomorphismException e) { throw new IteratorException(e); } } } return this.atomIt != null && this.atomIt.hasNext(); }
DefaultConjunctiveQueryFactory.instance().create(this.label, this.atomSet, this.answerVars)); break;