-
Notifications
You must be signed in to change notification settings - Fork 0
Abstract Sparql Query
Instances of AbstractSparqlQuery (ASQ) are abstractions of SPARQL queries. They can be renderered into SPARQL texts according to Settings values. The rendering can be specified by a template with parameters or according to the items in the ASQ.
Given an ASQ object Q, Q may be updated with additional graph
patterns, orderings, a template, and modifiers.
An ASQ also supports limited forms of geo-spatial queries and free-text searches. These are translated to SPARQL in ways which can be specified by Settings so the same abstract query can be run against different concrete back ends.
Set the single template object of
Qtot. A query starts off with its template equal to null. A template consists of a list of Elements which are either fixed text or a named parameter to be substituted later.
Translate
tto a Template and dosetTemplate()on that. Useful when the template is a string extracted, say, from a JSON value.
An ASQ holds three collections of graph patterns: an initial set of SPARQL bindings, the early graph patterns, and the later graph patterns. When rendering these are translated into SPARQL text in that order. Typically the "early" patterns will be those that extract properties from resources and "later" ones will filter those properties.
The allowed GraphPatterns are discussed elsewhere.
Projections are rendered as the
SELECTclause of a query. The obvious Projection is aVar(iable) but anything that satisfies the rendering interface will do.
Elements are rendered as the
DESCRIBEclause of a query. (Note that the same query can have aSELECTpart and aDESCRIBEpart; which one applies is chosen when rendering the query.) These elements will beVars orURIs.
Supplies a
Triplewhich will be part of aCONSTRUCTclause.
An ASQ can hold arbitrarily many orderings which may be upward (
o=ASC) or downward (o=DESC). Typeically the ordering expression e will be aVar(iable) but it can be any expression.
Set the
limitoroffsetthat will be applied to the generated query. If the value is negative (the default is-1) then no limit/offset is applied.
Sometimes the query modifiers are supplied not as specific Java objects but as a string to be inserted as-is in the modifier position of a query.
An ASQ is rendered to a SPARQL text using one of the rendering methods:
Create a String which is the appropriate kind of SPARQL query using all and only the elements appropriate to this query type.
If the query has a template then the result is the template text with its parameters filled in by rendering their values as found in the
Settings(). There are three reserved parameters that are added to theSettings:
_graphPattern_sort_modifiers
The
_graphPatternis filled in with the rendering of all of the graph patterns from the query, the_sortfrom the orderings of the query, and the_modifiersfrom the offset and limit.
The rendered text starts with PREFIX declarations, using only the ones that were usePrefix'd in the Settings.
An instance S of Settings manages prefixes, parameters, and renderers for geo-spatial and text search queries.
Associates the parameter
namewith the values.
Set the prefix
nameto abbreviate the stringURI.
Return a map of all the prefixes known to
S.
Shorten URI by applying the prefixes declared with setPrefix(). Record the prefix that is actually used.
Return a set of all the prefix names that have been used within
S.usePrefix().
Rendering a parameter value into a string is done by the
IsSparqler method toSparql(Settings s, StringBuffer sb),
where the settings s has been passed in by the top-level
call and sb is the string builder in which the complete
query is being assembled.
GraphPatterns form the major part of the query operations. The fundamental pattern is Basic, for triples and filters, and then there are several combinators and more complex patterns.
A Basic graph pattern is a sequence of Triple-or-Filter values. (Note that these triples are generalised; they may have variables within them. See below for more details on these classes.)
An And graph pattern allows multiple graph patterns in sequence.
Creates a graph pattern that renders as
BIND(_e AS ?_v)where _e is the rendering of e and _v is the rendering of v.
Empty is the occasionally-useful empty graph pattern; it renders as the empty string.
Creates a graph pattern that renders as
OPTIONAL {_operand}
where
_operandis the rendering ofoperand.
Creates a graph pattern that renders as
{_P1} UNION {_P2} ... {_Pn}
Where
_Pnis the rendering of then'th pattern in the union.
A Minus graph pattern renders as
{_A} MINUS {_B}
A Text graph pattern renders as the text. No checks are attempted; the developer is assumed to know that the text is legal at this point.
A Named graph pattern renders as
GRAPH _name {_p}
A Select graph pattern renders as
{_q}
where
_qis the rendering ofq, using the sameSettings.
A Values graph pattern renders as
VALUES (_vars) {_data1 _data2 ...}
_varsis the renderings of the elements ofvars; if there is only one element the round brackets () are omitted. The data elements_datanare the renderings of the expressions indata. When there are multiple variables, and hence any data element represents as many values as there are variables, then the data item must be a bracketed expression, ie of the form
new Call(Op.Tuple, V1 ... Vn)
An Exists graph pattern renders as
IF _exists {_p}
where
_existsis empty ifexistsistrueand isNOTifexistsis false.
A Triple has three TermAtomic components. A TermAtomic can be
a URI, a Literal, a Blank, or a Var.
A URI node has a string for the URI it represents. A Blank
holds a distinguishing string ID which may be specified
explicitly or generated (by Jena) as required. A Var has a
string Name (which does not start with a "?"). A Literal
has a lexical form, a URI type, and a language code; if
the type is ommitted use null, if the language is omitted
use the empty string "", and it is undefined to give a
Literal both a type and a language.
A Filter wraps an expression. When a Filter is rendered it
wraps FILTER() around the rendering of the expression. An
expression is something that satisfies the interface IsExpr,
which requires a rendering method toSparql(int, Settings, StringBuilder).
The int precedence is used to arrange that sufficiently many brackets
are inserted into the rendered expression.
Defined IsExprs are all the TermAtomics, function Calls, and Infix
operations.
An Infix represents a binary operator expression; it has two
(sub) expressions (L and R) and an Operator. The operator wraps
the name of a SPARQL operator.
A Call represents a function call; it has a name (the name of
a SPARQL function) and a list of (sub)expressions. The special
case where the name is the empty string is used to represent
expression lists in brackets.