Java Code Examples for org.apache.jena.query.Query#setQueryPattern()
The following examples show how to use
org.apache.jena.query.Query#setQueryPattern() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TriplestoreResource.java From trellis with Apache License 2.0 | 6 votes |
/** * This code is equivalent to the SPARQL query below. * * <p><pre><code> * SELECT ?subject ?predicate ?object * WHERE { GRAPH fromGraphName { ?subject ?predicate ?object } } * </code></pre> */ private Stream<Quad> fetchAllFromGraph(final String fromGraphName, final IRI toGraphName) { final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(SUBJECT); q.addResultVar(PREDICATE); q.addResultVar(OBJECT); final ElementPathBlock epb = new ElementPathBlock(); epb.addTriple(create(SUBJECT, PREDICATE, OBJECT)); final ElementGroup elg = new ElementGroup(); elg.addElement(new ElementNamedGraph(createURI(fromGraphName), epb)); q.setQueryPattern(elg); final Stream.Builder<Quad> builder = builder(); rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(toGraphName, getSubject(qs), getPredicate(qs), getObject(qs)))); return builder.build(); }
Example 2
Source File: OWLQLSPARQLCompiler.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private Query primCompile(Query query, Set<String> allVars) { // query must already be in dnf Element e = query.getQueryPattern(); Element newelt; /*if (e instanceof ElementUnion) { ElementUnion union= new ElementUnion(); for (Element ge : ((ElementUnion) e).getElements()) { Set<String> distinguishedVars = getMultipleOccurrenceVars(getVisibleVarsToOccurrences(ge)); distinguishedVars.addAll(query.getResultVars()); ExpandBasicGraphPatterns ebgp = new ExpandBasicGraphPatterns(); Element newge = ebgp.expand(query.getQueryPattern(), new LinkedList<String>(distinguishedVars), allVars); union.addElement(newge); } newelt = union; } else { Set<String> distinguishedVars = getMultipleOccurrenceVars(getVisibleVarsToOccurrences(e));*/ ExpandBasicGraphPatterns ebgp = new ExpandBasicGraphPatterns(); //distinguishedVars.addAll(query.getResultVars()); newelt = ebgp.expand(query.getQueryPattern(), /* new LinkedList<String>(distinguishedVars),*/ allVars); //} Query ret = query.cloneQuery(); ret.setQueryPattern(newelt); return ret; }
Example 3
Source File: ARQTest.java From tarql with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void testQueryAddValuesInQueryPattern() { List<Var> header = vars("a", "b"); Binding b1 = binding(header, "1", "2"); Binding b2 = binding(header, "3", "4"); Query q = QueryFactory.create("SELECT * {}"); ElementGroup group = new ElementGroup(); ElementData table = new ElementData(); table.add(Var.alloc("a")); table.add(Var.alloc("b")); table.add(b1); table.add(b2); group.addElement(q.getQueryPattern()); group.addElement(table); q.setQueryPattern(group); ResultSet rs = QueryExecutionFactory.create(q, ModelFactory.createDefaultModel()).execSelect(); assertEquals(Arrays.asList(new String[]{"a","b"}), rs.getResultVars()); assertTrue(rs.hasNext()); assertEquals(b1, rs.nextBinding()); assertTrue(rs.hasNext()); assertEquals(b2, rs.nextBinding()); assertFalse(rs.hasNext()); }
Example 4
Source File: TriplestoreResource.java From trellis with Apache License 2.0 | 5 votes |
/** * Fetch data for this resource. * * <p>This is equivalent to the following SPARQL query: * <pre><code> * SELECT ?predicate ?object ?binarySubject ?binaryPredicate ?binaryObject * WHERE { * GRAPH trellis:PreferServerManaged { * IDENTIFIER ?predicate ?object * OPTIONAL { * IDENTIFIER dc:hasPart ?binarySubject . * ?binarySubject ?binaryPredicate ?binaryObject * } * } * } * </code></pre> */ protected void fetchData() { LOGGER.debug("Fetching data from RDF datastore for: {}", identifier); final Var binarySubject = Var.alloc("binarySubject"); final Var binaryPredicate = Var.alloc("binaryPredicate"); final Var binaryObject = Var.alloc("binaryObject"); final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(PREDICATE); q.addResultVar(OBJECT); q.addResultVar(binarySubject); q.addResultVar(binaryPredicate); q.addResultVar(binaryObject); final ElementPathBlock epb1 = new ElementPathBlock(); epb1.addTriple(create(toJena(identifier), PREDICATE, OBJECT)); final ElementPathBlock epb2 = new ElementPathBlock(); epb2.addTriple(create(toJena(identifier), toJena(DC.hasPart), binarySubject)); epb2.addTriple(create(toJena(identifier), type.asNode(), toJena(LDP.NonRDFSource))); epb2.addTriple(create(binarySubject, binaryPredicate, binaryObject)); final ElementGroup elg = new ElementGroup(); elg.addElement(epb1); elg.addElement(new ElementOptional(epb2)); q.setQueryPattern(new ElementNamedGraph(toJena(Trellis.PreferServerManaged), elg)); rdfConnection.querySelect(q, qs -> { final RDFNode s = qs.get("binarySubject"); final RDFNode p = qs.get("binaryPredicate"); final RDFNode o = qs.get("binaryObject"); nodesToTriple(s, p, o).ifPresent(t -> data.put(t.getPredicate(), t.getObject())); data.put(getPredicate(qs), getObject(qs)); }); }
Example 5
Source File: TriplestoreResource.java From trellis with Apache License 2.0 | 5 votes |
/** * This code is equivalent to the SPARQL query below. * * <p><pre><code> * SELECT ?subject ?predicate ?object * WHERE { * GRAPH trellis:PreferServerManaged { * ?s ldp:member IDENTIFIER . * ?s ldp:membershipResource ?subject . * ?s rdf:type ldp:IndirectContainer . * ?s ldp:membershipRelation ?predicate . * ?s ldp:insertedContentRelation ?o . * ?res dc:isPartOf ?s . * } * GRAPH ?res { ?res ?o ?object } * } * </code></pre> */ private Stream<Quad> fetchIndirectMemberQuads() { final Var s = Var.alloc("s"); final Var o = Var.alloc("o"); final Var res = Var.alloc("res"); final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(SUBJECT); q.addResultVar(PREDICATE); q.addResultVar(OBJECT); final ElementPathBlock epb1 = new ElementPathBlock(); epb1.addTriple(create(s, toJena(LDP.member), toJena(identifier))); epb1.addTriple(create(s, toJena(LDP.membershipResource), SUBJECT)); epb1.addTriple(create(s, type.asNode(), toJena(LDP.IndirectContainer))); epb1.addTriple(create(s, toJena(LDP.hasMemberRelation), PREDICATE)); epb1.addTriple(create(s, toJena(LDP.insertedContentRelation), o)); epb1.addTriple(create(res, toJena(DC.isPartOf), s)); final ElementPathBlock epb2 = new ElementPathBlock(); epb2.addTriple(create(res, o, OBJECT)); final ElementGroup elg = new ElementGroup(); elg.addElement(new ElementNamedGraph(toJena(Trellis.PreferServerManaged), epb1)); elg.addElement(new ElementNamedGraph(res, epb2)); q.setQueryPattern(elg); final Stream.Builder<Quad> builder = builder(); rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(LDP.PreferMembership, getSubject(qs), getPredicate(qs), getObject(qs)))); return builder.build(); }
Example 6
Source File: TriplestoreResource.java From trellis with Apache License 2.0 | 5 votes |
/** * This code is equivalent to the SPARQL query below. * * <p><pre><code> * SELECT ?subject ?predicate ?object ?type * WHERE { * GRAPH trellis:PreferServerManaged { * ?s ldp:member IDENTIFIER * ?s ldp:membershipResource ?subject * ?s ldp:hasMemberRelation ?predicate * ?s ldp:insertedContentRelation ldp:MemberSubject * ?object dc:isPartOf ?s * ?object rdf:type ?type . * } * } * </code></pre> */ private Stream<Quad> fetchDirectMemberQuads() { final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(SUBJECT); q.addResultVar(PREDICATE); q.addResultVar(OBJECT); q.addResultVar(TYPE); final Var s = Var.alloc("s"); final ElementPathBlock epb = new ElementPathBlock(); epb.addTriple(create(s, toJena(LDP.member), toJena(identifier))); epb.addTriple(create(s, toJena(LDP.membershipResource), SUBJECT)); epb.addTriple(create(s, toJena(LDP.hasMemberRelation), PREDICATE)); epb.addTriple(create(s, toJena(LDP.insertedContentRelation), toJena(LDP.MemberSubject))); epb.addTriple(create(OBJECT, toJena(DC.isPartOf), s)); epb.addTriple(create(OBJECT, type.asNode(), TYPE)); final ElementNamedGraph ng = new ElementNamedGraph(toJena(Trellis.PreferServerManaged), epb); final ElementGroup elg = new ElementGroup(); elg.addElement(ng); q.setQueryPattern(elg); final Stream.Builder<Quad> builder = builder(); rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(LDP.PreferMembership, getSubject(qs), getPredicate(qs), adjustIdentifier((IRI) getObject(qs), getType(qs))))); return builder.build(); }
Example 7
Source File: TriplestoreResource.java From trellis with Apache License 2.0 | 5 votes |
/** * This code is equivalent to the SPARQL query below. * * <p><pre><code> * SELECT ?predicate ?object ?type * WHERE { * GRAPH trellis:PreferServerManaged { * IDENTIFIER dc:isPartOf ?subject . * ?subject ldp:isMemberOfRelation ?predicate . * ?subject ldp:membershipResource ?object . * ?subject ldp:insertedContentRelation ldp:MemberSubject . * ?object rdf:type ?type . * } * } * </code></pre> */ private Stream<Quad> fetchDirectMemberQuadsInverse() { final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(PREDICATE); q.addResultVar(OBJECT); q.addResultVar(TYPE); final ElementPathBlock epb = new ElementPathBlock(); epb.addTriple(create(toJena(identifier), toJena(DC.isPartOf), SUBJECT)); epb.addTriple(create(SUBJECT, toJena(LDP.isMemberOfRelation), PREDICATE)); epb.addTriple(create(SUBJECT, toJena(LDP.membershipResource), OBJECT)); epb.addTriple(create(SUBJECT, toJena(LDP.insertedContentRelation), toJena(LDP.MemberSubject))); epb.addTriple(create(OBJECT, type.asNode(), TYPE)); final ElementNamedGraph ng = new ElementNamedGraph(toJena(Trellis.PreferServerManaged), epb); final ElementGroup elg = new ElementGroup(); elg.addElement(ng); q.setQueryPattern(elg); final Stream.Builder<Quad> builder = builder(); final IRI ixnModel = getInteractionModel(); final IRI subject = adjustIdentifier(identifier, ixnModel); rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(LDP.PreferMembership, subject, getPredicate(qs), getObject(qs)))); return builder.build(); }
Example 8
Source File: TriplestoreResource.java From trellis with Apache License 2.0 | 5 votes |
/** * This code is equivalent to the SPARQL query below. * * <p><pre><code> * SELECT ?object ?type * WHERE { * GRAPH trellis:PreferServerManaged { * ?object dc:isPartOf IDENTIFIER . * ?object rdf:type ?type . * } * } * </code></pre> */ private Stream<Quad> fetchContainmentQuads() { if (getInteractionModel().getIRIString().endsWith("Container")) { final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(OBJECT); q.addResultVar(TYPE); final ElementPathBlock epb = new ElementPathBlock(); epb.addTriple(create(OBJECT, toJena(DC.isPartOf), toJena(identifier))); epb.addTriple(create(OBJECT, type.asNode(), TYPE)); final ElementNamedGraph ng = new ElementNamedGraph(toJena(Trellis.PreferServerManaged), epb); final ElementGroup elg = new ElementGroup(); elg.addElement(ng); q.setQueryPattern(elg); final Stream.Builder<Quad> builder = builder(); final IRI ixnModel = getInteractionModel(); final IRI subject = adjustIdentifier(identifier, ixnModel); rdfConnection.querySelect(q, qs -> builder.accept(rdf.createQuad(LDP.PreferContainment, subject, LDP.contains, adjustIdentifier((IRI) getObject(qs), getType(qs))))); return builder.build(); } return Stream.empty(); }
Example 9
Source File: QueryXExprNormalizer.java From sparql-generate with Apache License 2.0 | 5 votes |
@Override public void visitQueryPattern(Query query) { if (query.getQueryPattern() == null) { return; } final QueryPatternNormalizer nzer = new QueryPatternNormalizer(); query.getQueryPattern().visit(nzer); query.setQueryPattern(nzer.getResult()); }
Example 10
Source File: SelectPlan.java From sparql-generate with Apache License 2.0 | 5 votes |
private void augmentQuery(final Query q, final List<Var> variables, final List<Binding> values) { if (variables.isEmpty()) { return; } ElementGroup old = (ElementGroup) q.getQueryPattern(); ElementGroup newQueryPattern = new ElementGroup(); q.setQueryPattern(newQueryPattern); if (old.size() >= 1 && old.get(0) instanceof ElementData) { ElementData qData = (ElementData) old.get(0); int oldSize = qData.getRows().size(); qData = mergeValues(qData, variables, values); newQueryPattern.addElement(qData); for (int i = 1; i < old.size(); i++) { newQueryPattern.addElement(old.get(i)); } LOG.debug("New query has " + qData.getRows().size() + " initial values. It had " + oldSize + " values before"); } else { ElementData data = new ElementData(); variables.forEach(data::add); values.forEach(data::add); newQueryPattern.addElement(data); old.getElements().forEach(newQueryPattern::addElement); // unexplainable, but did happen check(data, values); } }
Example 11
Source File: QueryPatternSimplification.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(ElementSubQuery e) { Query sq = e.getQuery(); QueryPatternSimplification qps = new QueryPatternSimplification(); sq.getQueryPattern().visit(qps); Element newelt = qps.getResult(); Query newsq = sq.cloneQuery(); newsq.setQueryPattern(newelt); result = new ElementSubQuery(newsq); }
Example 12
Source File: QueryPatternSimplification.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public Query simplify(Query q) { QueryPatternSimplification qps = new QueryPatternSimplification(); q.getQueryPattern().visit(qps); Element newelt = qps.getResult(); Query ret = q.cloneQuery(); ret.setQueryPattern(newelt); return ret; }
Example 13
Source File: RuleSystemToUnionQuery.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(ElementSubQuery e) { Query sq = e.getQuery(); VariableSubstitutionElementVisitor qps = new VariableSubstitutionElementVisitor(oldVar2NewValue); sq.getQueryPattern().visit(qps); Element newelt = qps.getResult(); Query newsq = sq.cloneQuery(); newsq.setQueryPattern(newelt); result = new ElementSubQuery(newsq); }
Example 14
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 5 votes |
/** * <p>Replaces every non-recursive property path expression by its equivalent expression containing * no property path constructs. For recursive property path expressions are left unchanged if * bestEffort is set to <code>false</code>; otherwise, only their non-recursive part is rewritten. * </p> * <p> It returns <code>true</code> iff. the resulting rewritten query has no property path expressions. * @param query * @param bestEffort * @return */ public boolean rewrite(Query query, boolean bestEffort) { Set<Var> vars = FindAllVariables.getAllVariables(query); Set<String> varNames = OCUtils.getVars(vars); String varPrefix = "intermV"; int startSuffix = OCUtils.nextAvailableSuffixVariable(varNames, varPrefix)+1; NewVariableGenerator vargen = new NewVariableGenerator(varPrefix, startSuffix); ElementRewrite er = new ElementRewrite(bestEffort, vargen); Element newElt = ElementTransformer.transform(query.getQueryPattern(), er); query.setQueryPattern(newElt); return !er.isResultWithPropertyPaths(); }
Example 15
Source File: TriplestoreResourceService.java From trellis with Apache License 2.0 | 4 votes |
/** * This code is equivalent to the SPARQL queries below. * * <pre><code> * SELECT ?object WHERE { * GRAPH trellis:PreferServerManaged { IDENTIFIER rdf:type ?object } * } * </code></pre> * * <pre><code> * INSERT DATA { * GRAPH trellis:PreferServerManaged { * IDENTIFIER rdf:type ldp:Container ; * dc:modified "NOW"^^xsd:dateTime } * GRAPH IDENTIFIER?ext=audit { * IDENTIFIER prov:wasGeneratedBy [ * rdf:type prov:Activity , as:Create ; * prov:wasAssociatedWith trellis:AdministorAgent ; * prov:atTime "TIME"^^xsd:dateTime ] } * GRAPH IDENTIFIER?ext=acl { * IDENTIFIER acl:mode acl.Read , acl:Write , acl:Control ; * acl:agentClass foaf:Agent ; * acl:accessTo IDENTIFIER } * } * * </code></pre> */ @PostConstruct public void initialize() { final IRI root = rdf.createIRI(TRELLIS_DATA_PREFIX); final Query q = new Query(); q.setQuerySelectType(); q.addResultVar(OBJECT); final ElementPathBlock epb = new ElementPathBlock(); epb.addTriple(triple(toJena(root), toJena(type), OBJECT)); final ElementNamedGraph ng = new ElementNamedGraph(toJena(PreferServerManaged), epb); final ElementGroup elg = new ElementGroup(); elg.addElement(ng); q.setQueryPattern(elg); final Stream.Builder<RDFTerm> builder = builder(); rdfConnection.querySelect(q, qs -> builder.accept(getObject(qs))); if (!builder.build().findFirst().isPresent()) { final Literal time = rdf.createLiteral(now().toString(), XSD.dateTime); final IRI auth = rdf.createIRI(TRELLIS_DATA_PREFIX + "#auth"); final UpdateRequest update = new UpdateRequest(); final QuadDataAcc sink = new QuadDataAcc(); sink.addQuad(new Quad(toJena(PreferServerManaged), triple(toJena(root), toJena(type), toJena(LDP.BasicContainer)))); sink.addQuad(new Quad(toJena(PreferServerManaged), triple(toJena(root), toJena(DC.modified), toJena(time)))); sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.mode), toJena(ACL.Read)))); sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.mode), toJena(ACL.Write)))); sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.mode), toJena(ACL.Control)))); sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.agentClass), toJena(FOAF.Agent)))); sink.addQuad(new Quad(getExtIRI(root, ACL_EXT), triple(toJena(auth), toJena(ACL.accessTo), toJena(root)))); update.add(new UpdateDataInsert(sink)); rdfConnection.update(update); } LOGGER.info("Initialized Trellis Triplestore Resource Service"); }
Example 16
Source File: TarqlQueryExecution.java From tarql with BSD 2-Clause "Simplified" License | 4 votes |
/** * Modifies a query so that it operates onto a table. This is achieved * by appending the table as a VALUES block to the end of the main * query pattern. * * @param query Original query; will be modified in place * @param table Data table to be added into the query */ private void modifyQuery(Query query, final Table table) { ElementData tableElement = new ElementData() { @Override public Table getTable() { return table; } }; for (Var var: table.getVars()) { // Skip ?ROWNUM for "SELECT *" queries -- see further below if (query.isSelectType() && query.isQueryResultStar() && var.equals(TarqlQuery.ROWNUM)) continue; tableElement.add(var); } ElementGroup groupElement = new ElementGroup(); groupElement.addElement(tableElement); if (query.getQueryPattern() instanceof ElementGroup) { for (Element element: ((ElementGroup) query.getQueryPattern()).getElements()) { groupElement.addElement(element); } } else { groupElement.addElement(query.getQueryPattern()); } query.setQueryPattern(groupElement); // For SELECT * queries, we don't want to include pseudo // columns such as ?ROWNUM that may exist in the table. // That's why we skipped ?ROWNUM further up. if (query.isSelectType() && query.isQueryResultStar()) { // Force expansion of "SELECT *" to actual projection list query.setResultVars(); // Tell ARQ that it actually needs to pay attention to // the projection list query.setQueryResultStar(false); // And now we can add ?ROWNUM to the table, as the "*" // has already been expanded. tableElement.add(TarqlQuery.ROWNUM); } // Data can only be added to table after we've finished the // ?ROWNUM shenangians /*for (Binding binding: table.getRows()) { tableElement.add(binding); }*/ }