Java Code Examples for org.apache.jena.query.Query#addResultVar()
The following examples show how to use
org.apache.jena.query.Query#addResultVar() .
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: 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 3
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 4
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 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 ?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 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 ?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 7
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"); }