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

The following examples show how to use org.apache.jena.sparql.syntax.ElementGroup. 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: 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 #2
Source File: ARQTest.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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 #3
Source File: labelSearch.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt)
{
    Var var2 = createNewVar() ; 
    // Triple patterns for   ?x rdfs:label ?hiddenVar
    ElementTriplesBlock elementBGP = new ElementTriplesBlock();
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2) ;
    elementBGP.addTriple(t) ;
    
    // Regular expression for  regex(?hiddenVar, "pattern", "i") 
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i") ;
    
    ElementGroup elementGroup = new ElementGroup() ;
    elementGroup.addElement(elementBGP) ;
    elementGroup.addElement(new ElementFilter(regex)) ;
    // Compile it.
    // The better design is to build the Op structure programmatically,
    Op op = Algebra.compile(elementGroup) ;
    op = Algebra.optimize(op, execCxt.getContext()) ;
    return QC.execute(op, input, execCxt) ;
}
 
Example #4
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 #5
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
protected List<Element> flatten(Element g) {
	List<Element> ret = new ArrayList<Element>();
	Stack<Element> stack = new Stack<Element>();
	stack.push(g);
	while (!stack.isEmpty()) {
		Element e = stack.pop();
		if (e instanceof ElementGroup) {
			ElementGroup eg = (ElementGroup) e;
			for (Element ne:eg.getElements()) {
				stack.push(ne);
			}
		} else {
			ret.add(e);
		}
	}
	return ret;
	
}
 
Example #6
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 #7
Source File: SelectExtractionVisitor.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visitQueryPattern(final Query query) {
    if (query.getQueryPattern() == null || ((ElementGroup) query.getQueryPattern()).isEmpty()) {
        output.setQueryPattern(new ElementGroup());
        return;
    }
	isDummyQuery = false;
    if (output.getQueryPattern() != null) {
        ElementGroup group = new ElementGroup();
        group.addElement(query.getQueryPattern());
        group.addElement(output.getQueryPattern());
        output.setQueryPattern(group);
    } else {
        Element el = query.getQueryPattern();
        output.setQueryPattern(el);
    }
}
 
Example #8
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 #9
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 #10
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
protected Triple getBindTriplePattern( String queryString ) {
	final ElementGroup eg = getElementGroup(queryString);
	assertEquals( 1, eg.size() );
	assertTrue( "unexpected type (" + eg.get(0).getClass() + ")", 
			    eg.get(0) instanceof ElementBind );

	final ElementBind eb = (ElementBind) eg.get(0);
	assertTrue( "unexpected type (" + eb.getExpr().getClass() + ")", 
		        eb.getExpr() instanceof NodeValue );

	final Node n = ( (NodeValue) eb.getExpr() ).getNode();
	assertTrue( "unexpected type (" + n.getClass() + ")", 
		        n instanceof Node_Triple );

	return ( (Node_Triple) n ).get();
}
 
Example #11
Source File: SelectPlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected void checkBgpAndBindOnlyQuery( String sparqlstarQueryString, String expectedResultQuery )
{
	final String baseIRI = null;
	final Query expectedQuery = QueryFactory.create(expectedResultQuery, baseIRI, Syntax.syntaxSPARQL);
	final ElementGroup expectedEG = (ElementGroup) expectedQuery.getQueryPattern();

	final Query convertedQuery = convert( sparqlstarQueryString );
	final ElementGroup convertedEG = (ElementGroup) convertedQuery.getQueryPattern();
	
	checkForEquivalence( expectedEG, mergeElementPathBlocks(convertedEG) );
}
 
Example #13
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 #14
Source File: FindAllVariables.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementGroup e) {
	for (Element ge : e.getElements()) {
		ge.visit(this);
	}
	
}
 
Example #15
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected ElementGroup  getElementGroup( String queryString ) {
	final SPARQLParser parser = new ParserSPARQLStar();
	final Query query = parser.parse(new Query(), queryString);

	assertTrue( query.isSelectType() );
	assertTrue( "unexpected type (" + query.getQueryPattern().getClass() + ")", 
		        query.getQueryPattern() instanceof ElementGroup );

	return (ElementGroup) query.getQueryPattern();
}
 
Example #16
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 #17
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementGroup group) {
	ElementGroup ret = new ElementGroup();
	for (Element elt: group.getElements() ) {
		elt.visit(this);
		ret.addElement(result);
	}
	result = ret;
}
 
Example #18
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 #19
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 #20
Source File: StatisticsVisitor.java    From IGUANA with GNU Affero General Public License v3.0 5 votes vote down vote up
public void startElement(ElementGroup el) {
	if (!started && el.equals(where)) {
		// root element found
		started = true;

	} 
}
 
Example #21
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public void visitAsGroup(Element el) {
    boolean needBraces = !((el instanceof ElementGroup) || (el instanceof ElementSubQuery));

    if (needBraces) {
        out.print("{ ");
        out.incIndent(INDENT);
    }

    el.visit(this);

    if (needBraces) {
        out.decIndent(INDENT);
        out.print("}");
    }
}
 
Example #22
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 #23
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 #24
Source File: SelectQueryPartialCopyVisitor.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visitQueryPattern(final Query query) {
    ElementGroup group = new ElementGroup();
    if (query.getQueryPattern() != null) {
        Element el = query.getQueryPattern();
        if(!(el instanceof ElementGroup)) {
            throw new SPARQLExtException("should not reach this point");
        }
        ((ElementGroup) el).getElements().forEach(group::addElement);
    }
    output.setQueryPattern(group);
}
 
Example #25
Source File: TriplePatternExtractor.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
public Set<Triple> extractTriplePattern(final ElementGroup group, final boolean ignoreOptionals) {
	triplePattern = new HashSet<>();
	optionalTriplePattern = new HashSet<>();

	group.visit(this);

	// postprocessing: triplepattern in OPTIONAL clause
	if (!ignoreOptionals) {
		for (Triple t : optionalTriplePattern) {
			triplePattern.add(t);
		}
	}

	return triplePattern;
}
 
Example #26
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 #27
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 #28
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 #29
Source File: QueryPatternNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(ElementGroup el) {
    final ElementGroup res = new ElementGroup();
    for (final Element element : el.getElements()) {
        element.visit(this);
        res.addElement(result);
    }
    result = res;
}
 
Example #30
Source File: QueryPatternNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void endVisit(Element nzed, NodeExprNormalizer nenzer) {
    if (!nenzer.hasBindings()) {
        result = nzed;
    } else {
        ElementGroup group = nenzer.getBindingsAsGroup();
        group.addElement(nzed);
        result = group;
    }
}