Java Code Examples for org.apache.jena.sparql.syntax.ElementPathBlock#addTriple()

The following examples show how to use org.apache.jena.sparql.syntax.ElementPathBlock#addTriple() . 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 vote down vote up
/**
 * 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: TriplestoreResourceService.java    From trellis with Apache License 2.0 6 votes vote down vote up
/**
 * This code is equivalent to the SPARQL query below.
 *
 * <p><pre><code>
 * WITH trellis:PreferServerManaged
 *   DELETE { IDENTIFIER dc:modified ?time }
 *   INSERT { IDENTIFIER dc:modified TIME }
 *   WHERE { IDENTIFIER dc:modified ?time } .
 * </code></pre></p>
 */
private UpdateRequest buildUpdateModificationRequest(final IRI identifier, final Literal time) {
    final UpdateRequest req = new UpdateRequest();
    final Var modified = Var.alloc(MODIFIED);
    final UpdateDeleteInsert modify = new UpdateDeleteInsert();
    modify.setWithIRI(toJena(PreferServerManaged));
    modify.getDeleteAcc().addTriple(triple(toJena(identifier), toJena(DC.modified), modified));
    modify.getInsertAcc().addTriple(triple(toJena(identifier), toJena(DC.modified), toJena(time)));
    final ElementGroup eg = new ElementGroup();
    final ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(triple(toJena(identifier), toJena(DC.modified), modified));
    eg.addElement(epb);
    modify.setElement(eg);
    req.add(modify);
    return req;
}
 
Example 3
Source File: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected void unNestBindClause( Triple tp, ElementPathBlock epb, Var var )
{
	Node s = tp.getSubject();
	Node p = tp.getPredicate();
	Node o = tp.getObject();

	if ( s instanceof Node_Triple )
	{
		final Triple sTP = ( (Node_Triple) s ).get();
		s = unNestTriplePattern(sTP, epb, true);
	}

	if ( o instanceof Node_Triple )
	{
		final Triple oTP = ( (Node_Triple) o ).get();
		o = unNestTriplePattern(oTP, epb, true);
	}

	final Triple nonnestedTP = new Triple(s, p, o);

	if ( ! doneNested.containsKey(nonnestedTP) )
	{
		epb.addTriple(nonnestedTP);
		epb.addTriple( new Triple(var, RDF.Nodes.type,      RDF.Nodes.Statement) );
		epb.addTriple( new Triple(var, RDF.Nodes.subject,   s) );
		epb.addTriple( new Triple(var, RDF.Nodes.predicate, p) );
		epb.addTriple( new Triple(var, RDF.Nodes.object,    o) );
		doneNested.put(nonnestedTP, var);
	}
}
 
Example 4
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(P_ZeroOrMoreN p) {
	if (bestEffort) {
		ElementPathBlock e = new ElementPathBlock();
		e.addTriple(new TriplePath(subject, p, object));
		result = e;
		resultHasPropertyPaths = true;
	} else {
		result = null;
	}
	
}
 
Example 5
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(P_ZeroOrMore1 p) {
	if (bestEffort) {
		ElementPathBlock e = new ElementPathBlock();
		e.addTriple(new TriplePath(subject, p, object));
		result = e;
		resultHasPropertyPaths = true;
	} else {
		result = null;
	}
	
}
 
Example 6
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(P_OneOrMoreN p) {
	if (bestEffort) {
		ElementPathBlock e = new ElementPathBlock();
		e.addTriple(new TriplePath(subject, p, object));
		result = e;
		resultHasPropertyPaths = true;
	} else {
		result = null;
	}
	
}
 
Example 7
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(P_OneOrMore1 p) {
	if (bestEffort) {
		ElementPathBlock e = new ElementPathBlock();
		e.addTriple(new TriplePath(subject, p, object));
		result = e;
		resultHasPropertyPaths = true;
	} else {
		result = null;
	}
	
}
 
Example 8
Source File: TriplestoreResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
/**
 * 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: TriplestoreResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
/**
 * 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 10
Source File: TriplestoreResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
/**
 * 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 11
Source File: TriplestoreResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
/**
 * 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 12
Source File: TriplestoreResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
/**
 * 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 13
Source File: TriplestoreResourceService.java    From trellis with Apache License 2.0 4 votes vote down vote up
/**
 * 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 14
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected void add(ElementPathBlock targetPathBlock, ElementPathBlock newPaths) {
	for (TriplePath tp:newPaths.getPattern().getList()) {
		targetPathBlock.addTriple(tp);
	}
}
 
Example 15
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected void add(ElementPathBlock targetPathBlock, ElementTriplesBlock newTriples) {
	for (Triple t:newTriples.getPattern().getList()) {
		targetPathBlock.addTriple(t);
	}
}
 
Example 16
Source File: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
protected Node unNestTriplePattern( Triple tp, ElementPathBlock epb, boolean hasParent )
{	
	Node s = tp.getSubject();
	Node p = tp.getPredicate();
	Node o = tp.getObject();

	if ( s instanceof Node_Triple )
	{
		final Triple sTP = ( (Node_Triple) s ).get();
		s = unNestTriplePattern(sTP, epb, true);
	}

	if ( o instanceof Node_Triple )
	{
		final Triple oTP = ( (Node_Triple) o ).get();
		o = unNestTriplePattern(oTP, epb, true);
	}

	final Triple nonnestedTP = new Triple(s, p, o);
	final boolean seenBefore = doneNested.containsKey(nonnestedTP);

	final Node var;
	if ( seenBefore ) {
		var = doneNested.get(nonnestedTP);
	}
	else
	{
		var = createFreshAnonVarForReifiedTriple();
		epb.addTriple(nonnestedTP);

		if ( hasParent ) {
			epb.addTriple( new Triple(var, RDF.Nodes.type,      RDF.Nodes.Statement) );
			epb.addTriple( new Triple(var, RDF.Nodes.subject,   s) );
			epb.addTriple( new Triple(var, RDF.Nodes.predicate, p) );
			epb.addTriple( new Triple(var, RDF.Nodes.object,    o) );
			doneNested.put(nonnestedTP, var);
		}
	}

	return var;
}
 
Example 17
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(P_ZeroOrOne p) {
	if (bestEffort) {
		ElementPathBlock e = new ElementPathBlock();
		e.addTriple(new TriplePath(subject, p, object));
		result = e;
		resultHasPropertyPaths = true;
	} else {
		result = null;
	}
	
	// (subj p? obj) is semantically equivalent to a union of
	// 1) subj == obj
	// 2) subj p obj
	/*TriplePath newtp = new TriplePath(subject, p.getSubPath(), object);
	boolean[] resHasPropertyPaths = new boolean[]{false};
	Element newElt = transform(newtp, bestEffort, vargen, resHasPropertyPaths);
	if (!subject.isVariable() && !object.isVariable()) {
		if (subject.sameValueAs(object)) {
			// trivially satisfied
			result = new ElementGroup();
		} else {
			// the zero branch cannot be satified
			// so we test only the one branch
			result = newElt;
			if (!resultHasPropertyPaths) {
				resultHasPropertyPaths = resHasPropertyPaths[0];
			}
		}
	} else {
		if (!resultHasPropertyPaths) {
			resultHasPropertyPaths = resHasPropertyPaths[0];
		}
		ElementUnion union = new ElementUnion();
		union.addElement(newElt);
		ElementGroup eqGroup = new ElementGroup();
		ElementBind bind; 
		if (subject.isVariable()) {
			bind = new ElementBind( Var.alloc(subject.getName()), getExpr(object)); 
		} else {
			assert object.isVariable() : object;
			bind = new ElementBind(Var.alloc(object.getName()), getExpr(subject));
		}
		eqGroup.addElement(bind);
		union.addElement(eqGroup);
		result = union;
		
	}
	*/
	
}