org.apache.jena.sparql.syntax.ElementPathBlock Java Examples

The following examples show how to use org.apache.jena.sparql.syntax.ElementPathBlock. 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: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Override
public Element transform( ElementBind eb, Var v, Expr expr2 )
{
	if (    ( eb.getExpr() instanceof NodeValue )
	     && ( ((NodeValue) eb.getExpr()).getNode() instanceof Node_Triple ) )
	{
		final NodeValue nv = (NodeValue) eb.getExpr();
		final Node_Triple nt = (Node_Triple) nv.getNode();
		final Triple tp = nt.get();

		final ElementPathBlock epb = new ElementPathBlock();
		unNestBindClause(tp, epb, v);
		return epb;
	}
	else
		return super.transform(eb, v, expr2);
}
 
Example #2
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public void setQueryPattern(Element graphPattern) {
	if (graphPattern instanceof ElementTriplesBlock) {
		query.setQueryPattern((ElementTriplesBlock)graphPattern);
	} else if (graphPattern instanceof ElementGroup) {
		ElementGroup group = (ElementGroup) graphPattern;
		for (Element e: flatten(group)) {
			if (e instanceof ElementPathBlock) {
				ElementPathBlock epb = (ElementPathBlock) e;
				for (TriplePath p:epb.getPattern().getList()) {
					if (!p.isTriple()) {
						throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns. No triple path:  "+ p+"\n"+graphPattern );	
					}
				}
			} else 	if (!(e instanceof ElementTriplesBlock)
			&&  !(e instanceof ElementFilter)) {
				throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns: "+ e+"\n"+graphPattern );	
			}
		}
		query.setQueryPattern(graphPattern);
	}
	else {
		throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns: "+ graphPattern);				
	}
	
}
 
Example #3
Source File: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
protected void checkForEquivalence( ElementGroup expectedEG, ElementGroup testEG )
{
	assertEquals( expectedEG.size(), testEG.size() );
	assertEquals( expectedEG.get(0).getClass(), testEG.get(0).getClass() );

	if ( expectedEG.get(0) instanceof ElementPathBlock ) {
		final ElementPathBlock expectedEPB = (ElementPathBlock) expectedEG.get(0);
		final ElementPathBlock convertedEPB = (ElementPathBlock) testEG.get(0);
		checkForEquivalence( expectedEPB, convertedEPB );
	}
	else if ( expectedEG.get(0) instanceof ElementBind ) {
		final ElementBind expectedEB = (ElementBind) expectedEG.get(0);
		final ElementBind convertedEB = (ElementBind) testEG.get(0);
		checkForEquivalence( expectedEB, convertedEB );
	}
	else
		fail();
}
 
Example #4
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 #5
Source File: SHACLPaths.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to parse a given string into a Jena Path.
 * Throws an Exception if the string cannot be parsed.
 * @param string  the string to parse
 * @param model  the Model to operate on (for prefixes)
 * @return a Path or a Resource if this is a URI
 */
public static Object getJenaPath(String string, Model model) throws QueryParseException {
	Query query = ARQFactory.get().createQuery(model, "ASK { ?a \n" + string + "\n ?b }");
	Element element = query.getQueryPattern();
	if(element instanceof ElementGroup) {
		Element e = ((ElementGroup)element).getElements().get(0);
		if(e instanceof ElementPathBlock) {
			Path path = ((ElementPathBlock) e).getPattern().get(0).getPath();
			if(path instanceof P_Link && ((P_Link)path).isForward()) {
				return model.asRDFNode(((P_Link)path).getNode());
			}
			else {
				return path;
			}
		}
		else if(e instanceof ElementTriplesBlock) {
			return model.asRDFNode(((ElementTriplesBlock) e).getPattern().get(0).getPredicate());
		}
	}
	throw new QueryParseException("Not a SPARQL 1.1 Path expression", 2, 1);
}
 
Example #6
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 #7
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 #8
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementPathBlock pb) {
	ElementPathBlock ret = new ElementPathBlock();
	for (Iterator<TriplePath> it= 	pb.patternElts();it.hasNext(); ) {
		TriplePath tp = it.next();
		ret.addTriplePath(copySubstitute(tp));
	}
	result = ret;
}
 
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 ?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 #10
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 #11
Source File: TriplePatternExtractor.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit(final ElementPathBlock el) {
	for (Iterator<TriplePath> iterator = el.patternElts(); iterator.hasNext();) {
		TriplePath tp = iterator.next();
		if (inOptionalClause) {
			optionalTriplePattern.add(tp.asTriple());
		} else {
			if (tp.isTriple()) {
				triplePattern.add(tp.asTriple());
			}
		}
	}
}
 
Example #12
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 #13
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 #14
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 #15
Source File: OCUtils.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static ElementTriplesBlock toTriples(ElementPathBlock pb) {
	ElementTriplesBlock ret = new ElementTriplesBlock(); 
	for (TriplePath tp :pb.getPattern().getList()) {
		if (tp.isTriple()) {
			ret.addTriple(tp.asTriple());
		} else {
			throw new RuntimeException("Path query not supported");
		}
	} 
	return ret;
}
 
Example #16
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementGroup group) {
	//remove nested groups
	ElementGroup ret = new ElementGroup();
	ElementPathBlock pathBlock = new ElementPathBlock();
	for (Element elt: group.getElements()) {
		elt.visit(this);
		if (result instanceof ElementGroup) {
			ElementGroup subgroup = (ElementGroup) result;
			// add the subgroup content directly
			add(ret, pathBlock, subgroup);
		} else if (result instanceof ElementPathBlock) {
			if (pathBlock.isEmpty()) {
				ret.addElement(pathBlock);
			}
			add(pathBlock, (ElementPathBlock) result);
		} else if (result instanceof ElementTriplesBlock) {
			if (pathBlock.isEmpty()) {
				ret.addElement(pathBlock);
			}
			add(pathBlock, (ElementTriplesBlock) result);
			
		} else {
			ret.addElement(result);
		}
		
	}
	if (ret.getElements().size() == 1) {
		result = ret.getElements().get(0);
	} else {
		result = ret;
	}
}
 
Example #17
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected void add(ElementGroup targetGroup,ElementPathBlock targetGroupPathBlock, ElementGroup newGroup) {
	// add the subgroup content directly
	for (Element subelt: newGroup.getElements()) {
		if (subelt instanceof ElementPathBlock) {
			add(targetGroupPathBlock, (ElementPathBlock) subelt);
		} else if (subelt instanceof ElementTriplesBlock) {
			add(targetGroupPathBlock, (ElementTriplesBlock) subelt);
		} else if (subelt instanceof ElementGroup) {
			assert false : subelt+"\n"+newGroup ;
			//add(targetGroup, targetGroupPathBlock, (ElementGroup) subelt);
		} else {
			targetGroup.addElement(subelt);
		}
	}
}
 
Example #18
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 #19
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 #20
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected Triple getTriplePattern( String queryString ) {
	final ElementPathBlock epb = getElementPathBlock(queryString);
	assertEquals( 1, epb.getPattern().size() );
	assertTrue( "unexpected type (" + epb.getPattern().get(0).getClass() + ")", 
		        epb.getPattern().get(0) instanceof TriplePath );

	final TriplePath tp = epb.getPattern().get(0);
	assertTrue( tp.isTriple() );

	return tp.asTriple();
}
 
Example #21
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected ElementPathBlock getElementPathBlock( String queryString ) {
	final ElementGroup eg = getElementGroup(queryString);
	assertEquals( 1, eg.size() );
	assertTrue( "unexpected type (" + eg.get(0).getClass() + ")", 
			    eg.get(0) instanceof ElementPathBlock );

	return (ElementPathBlock) eg.get(0);
}
 
Example #22
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void parseNestedTPsOneOfTwoObjects2() {
	final String queryString = "SELECT * WHERE { ?s1 ?p1 ?o1 , <<?s ?p ?o>> }";

	final ElementPathBlock epb = getElementPathBlock(queryString);
	assertEquals( 2, epb.getPattern().size() );
	assertTrue( epb.getPattern().get(0) instanceof TriplePath );
	assertTrue( epb.getPattern().get(1) instanceof TriplePath );

	final TriplePath tp1 = epb.getPattern().get(0);
	assertTrue( tp1.isTriple() );
	final Triple t1 = tp1.asTriple();

	final TriplePath tp2 = epb.getPattern().get(1);
	assertTrue( tp2.isTriple() );
	final Triple t2 = tp2.asTriple();

	assertFalse( t1.getSubject()   instanceof  Node_Triple );
	assertFalse( t1.getPredicate() instanceof  Node_Triple );
	assertFalse( t1.getObject()    instanceof  Node_Triple );

	assertFalse( t2.getSubject()   instanceof  Node_Triple );
	assertFalse( t2.getPredicate() instanceof  Node_Triple );
	assertTrue(  t2.getObject()    instanceof  Node_Triple );

	assertTrue(  t1.getSubject().equals(t2.getSubject()) );
	assertTrue(  t1.getPredicate().equals(t2.getPredicate()) );
	assertFalse( t1.getObject().equals(t2.getObject()) );
}
 
Example #23
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void parseNestedTPsOneOfTwoObjects1() {
	final String queryString = "SELECT * WHERE { ?s1 ?p1 ?o1 ; ?p2 <<?s ?p ?o>> }";

	final ElementPathBlock epb = getElementPathBlock(queryString);
	assertEquals( 2, epb.getPattern().size() );
	assertTrue( epb.getPattern().get(0) instanceof TriplePath );
	assertTrue( epb.getPattern().get(1) instanceof TriplePath );

	final TriplePath tp1 = epb.getPattern().get(0);
	assertTrue( tp1.isTriple() );
	final Triple t1 = tp1.asTriple();

	final TriplePath tp2 = epb.getPattern().get(1);
	assertTrue( tp2.isTriple() );
	final Triple t2 = tp2.asTriple();

	assertFalse( t1.getSubject()   instanceof  Node_Triple );
	assertFalse( t1.getPredicate() instanceof  Node_Triple );
	assertFalse( t1.getObject()    instanceof  Node_Triple );

	assertFalse( t2.getSubject()   instanceof  Node_Triple );
	assertFalse( t2.getPredicate() instanceof  Node_Triple );
	assertTrue(  t2.getObject()    instanceof  Node_Triple );

	assertTrue(  t1.getSubject().equals(t2.getSubject()) );
	assertFalse( t1.getPredicate().equals(t2.getPredicate()) );
	assertFalse( t1.getObject().equals(t2.getObject()) );
}
 
Example #24
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void parseNestedTPsSameSubject2() {
	final String queryString = "SELECT * WHERE { <<?s ?p ?o>> ?p2 ?o2 , ?o3 }";

	final ElementPathBlock epb = getElementPathBlock(queryString);
	assertEquals( 2, epb.getPattern().size() );
	assertTrue( epb.getPattern().get(0) instanceof TriplePath );
	assertTrue( epb.getPattern().get(1) instanceof TriplePath );

	final TriplePath tp1 = epb.getPattern().get(0);
	assertTrue( tp1.isTriple() );
	final Triple t1 = tp1.asTriple();

	final TriplePath tp2 = epb.getPattern().get(1);
	assertTrue( tp2.isTriple() );
	final Triple t2 = tp2.asTriple();

	assertTrue(  t1.getSubject()   instanceof  Node_Triple );
	assertFalse( t1.getPredicate() instanceof  Node_Triple );
	assertFalse( t1.getObject()    instanceof  Node_Triple );

	assertTrue(  t2.getSubject()   instanceof  Node_Triple );
	assertFalse( t2.getPredicate() instanceof  Node_Triple );
	assertFalse( t2.getObject()    instanceof  Node_Triple );

	assertTrue(  t1.getSubject().equals(t2.getSubject()) );
	assertTrue(  t1.getPredicate().equals(t2.getPredicate()) );
	assertFalse( t1.getObject().equals(t2.getObject()) );
}
 
Example #25
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 #26
Source File: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Override
public Element transform( ElementPathBlock el )
{
	final ElementPathBlock epb = new ElementPathBlock();

	for ( TriplePath tp : el.getPattern() ) {
		unNestTriplePattern(tp.asTriple(), epb, false);
	}

	return epb;
}
 
Example #27
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Test
public void parseNestedTPsSameSubject1() {
	final String queryString = "SELECT * WHERE { <<?s ?p ?o>> ?p2 ?o2 ; ?p3 ?o3 }";

	final ElementPathBlock epb = getElementPathBlock(queryString);
	assertEquals( 2, epb.getPattern().size() );
	assertTrue( epb.getPattern().get(0) instanceof TriplePath );
	assertTrue( epb.getPattern().get(1) instanceof TriplePath );

	final TriplePath tp1 = epb.getPattern().get(0);
	assertTrue( tp1.isTriple() );
	final Triple t1 = tp1.asTriple();

	final TriplePath tp2 = epb.getPattern().get(1);
	assertTrue( tp2.isTriple() );
	final Triple t2 = tp2.asTriple();

	assertTrue(  t1.getSubject()   instanceof  Node_Triple );
	assertFalse( t1.getPredicate() instanceof  Node_Triple );
	assertFalse( t1.getObject()    instanceof  Node_Triple );

	assertTrue(  t2.getSubject()   instanceof  Node_Triple );
	assertFalse( t2.getPredicate() instanceof  Node_Triple );
	assertFalse( t2.getObject()    instanceof  Node_Triple );

	assertTrue(  t1.getSubject().equals(t2.getSubject()) );
	assertFalse( t1.getPredicate().equals(t2.getPredicate()) );
	assertFalse( t1.getObject().equals(t2.getObject()) );
}
 
Example #28
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 #29
Source File: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected ElementGroup mergeElementPathBlocks( ElementGroup eg )
{
	if ( eg.size() == 1 )
		return eg;

	final ElementGroup result = new ElementGroup();

	final Iterator<Element> it = eg.getElements().iterator();
	Element prevEl = it.next();

	while ( it.hasNext() )
	{
		final Element currEl = it.next();
		if (    (currEl instanceof ElementPathBlock)
		     && (prevEl instanceof ElementPathBlock) )
		{
			final ElementPathBlock currPB = (ElementPathBlock) currEl;
			final ElementPathBlock prevPB = (ElementPathBlock) prevEl;
			prevPB.getPattern().addAll( currPB.getPattern() );
		}
		else {
			result.addElement(prevEl);
			prevEl = currEl;
		}
	}

	result.addElement(prevEl);

	return result;
}
 
Example #30
Source File: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected void checkForEquivalence( ElementPathBlock expectedEPB, ElementPathBlock testEPB )
{
	final IsomorphismMap isoMap = new IsomorphismMap();

	final Iterator<TriplePath> eit = expectedEPB.getPattern().iterator();
	while ( eit.hasNext() )
	{
		final Triple expcTP = eit.next().asTriple();

		boolean found = false;
		final Iterator<TriplePath> rit = testEPB.getPattern().iterator();
		while ( rit.hasNext() )
		{
			final Triple testTP = rit.next().asTriple();

			if (    isoMap.canBeIsomorphic(expcTP.getSubject(),   testTP.getSubject())
			     && isoMap.canBeIsomorphic(expcTP.getPredicate(), testTP.getPredicate())
			     && isoMap.canBeIsomorphic(expcTP.getObject(),    testTP.getObject()) )
				found = true;
		}

		if ( ! found )
			System.err.println( "Expected triple pattern not found (" + expcTP.toString() + ")" );

		assertTrue(found);
	}
}