-
Notifications
You must be signed in to change notification settings - Fork 6
Graph Patterns
Spanqit uses three classes to represent the SPARQL graph patterns, all of which implement the GraphPattern interface:
- The
TriplePatternclass represents triple patterns. - The
GraphPatternNotTripleclass represents collections of graph patterns. - The
SubSelectclass represents a SPARQL sub query.
Graph patterns are created by the more aptly named GraphPatterns class. Use GraphPatterns#tp() to create a TriplePattern instance:
Prefix dc = Spanqit.prefix("dc", iri("http://purl.org/dc/elements/1.1/"));
Variable book = Spanqit.var("book");
TriplePattern triple = GraphPatterns.tp(book, dc.iri("author"), literal("J.R.R. Tolkien"));
System.out.println(triple.getQueryString());
// ==> ?book dc:author "J.R.R. Tolkien"Three methods in GraphPatterns exist to create GraphPatternNotTriple instances. GraphPatterns#and() creates a group graph pattern, consisting of the GraphPattern instances passed as parameters:
Variable mbox = Spanqit.var("mbox"), x = Spanqit.var("x");
GraphPatternNotTriple groupPattern =
GraphPatterns.and(x.has(foaf.iri("name"), name), x.has(foaf.iri("mbox"), mbox);
System.out.println(groupPattern.getQueryString());
// ==> { ?x foaf:mbox ?mbox . ?x foaf:name ?name }GraphPatterns#union() creates an alternative graph pattern, taking the intersection of the provided GraphPattern instances:
Prefix dc10 = Spanqit.prefix("dc10", iri("http://purl.org/dc/elements/1.0/")),
dc11 = Spanqit.prefix("dc11", iri("http://purl.org/dc/elements/1.1/"));
Variable book = Spanqit.var("book"), title = Spanqit.var("title");
GraphPatternNotTriple union = GraphPatterns.union(book.has(dc10.iri("title"), title),
book.has(dc11.iri("title"), title);
System.out.println(union.getQueryString());
// ==> { ?book dc10:title ?title } UNION { ?book dc11:title ?title }GraphPatterns#optional() creates an optional group graph pattern, consisting of the passed in GraphPatterns:
GraphPatternNotTriple optionalPattern = GraphPatterns.optional(GraphPatterns.tp(x, foaf.iri("mbox"), mbox));
System.out.println(optionalPattern.getQueryString());
// ==> OPTIONAL { ?x foaf:mbox ?mbox }Finally, GraphPatterns#select() creates an instance of a SubSelect, which represents a SPARQL subquery:
SubSelect subQuery = GraphPatterns.select();