tSPARQL is a trust-aware query language that has been developed within the tRDF project. tSPARQL extends the RDF query language SPARQL to query the trustworthiness of RDF data; it enables users to describe trust requirements in a declarative manner and to access the trust values associated to query solutions. tSPARQL adds two new clauses to SPARQL, namely the TRUST AS
clause and the ENSURE TRUST
clause. In the following you find a reference to the specification of tSPARQL, a brief introduction of the new clauses, and a description of the tSPARQL query engine.
To access trust values tSPARQL contains the TRUST AS
clause. Consider the following query which asks for names of students
and their courses. It contains a TRUST AS
clause
with a new variable ?t
. ?t
allows access to
the trust value associated to the triples that match the pattern in
line 7. Hence, the query additionally asks for the trustworthiness
of the fact that the student really takes the respective course.
1PREFIX ub: <http://www.lehigh.edu/.../univ-bench.owl#>
2PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
3SELECT ?n ?c ?t
4WHERE {
5 { ?s rdf:type ub:Student .
6 ?s ub:name ?n }
7 { ?s ub:takesCourse ?c .
8 TRUST AS ?t }
9}
The TRUST AS
clause offers the following novel features: i) the new variable may become part of the query result, ii) the variable may be used for sorting the results, iii) it may be associated with parts of the query pattern, and iv) two variables that represent trust values of different query pattern parts can be compared.
To express trust requirements tSPARQL contains the ENSURE TRUST
clause. The following query contains an ENSURE TRUST
clause with a pair of numbers in brackets; the first number denotes a lower bound and the second number denotes an upper bound. Again, the query asks for names of students and their courses; however, in this case only those solutions become part of the result where we highly trust that the student really takes the respective course.
1PREFIX ub: <http://www.lehigh.edu/.../univ-bench.owl#>
2PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
3SELECT ?n ?c
4WHERE {
5 { ?s rdf:type ub:Student .
6 ?s ub:name ?n }
7 { ?s ub:takesCourse ?c .
8 ENSURE TRUST (0.9,1.0) }
9}
The tSPARQL query engine is part of the tRDF4Jena package. The engine is an extension to the SPARQL query engine ARQ which is part of the Jena framework.
To use the tSPARQL query engine in order to process tSPARQL queries in your Jena-based application you first have to implement the following three functions. First, you need a trust function to determine trust values for RDF statements; second, you need a trust aggregation function to calculate an aggregated trust value for a set of RDF statements; and, third, you need a trust merge function to calculate a trust value merged from the trust values associated with two query solutions. By not prescribing a specific implementation of these functions the tSPARQL engine gives you the freedom to use functions that best fits the use case of your application.
With your implementations of the functions you register the tSPARQL query engine for ARQ as in the following sample code.
1import com.hp.hpl.jena.query.ARQ;
2import org.trdf.trdf4jena.TrustAggregationFunction;
3import org.trdf.trdf4jena.TrustFunction;
4import org.trdf.trdf4jena.TrustManager;
5import org.trdf.trdf4jena.tsparql.Constants;
6import org.trdf.trdf4jena.tsparql.algebra.TrustMergeFunction;
7import org.trdf.trdf4jena.tsparql.engine.QueryEngineTrust;
8import org.trdf.trdf4jena.tsparql.lang.ParserTSPARQL;
9
10// create your trust functions
11TrustFunction tFct = ...
12TrustAggregationFunction tAggFct = ...
13TrustMergeFunction tMrgFct = ...
14
15// create a trust manager for the tSPARQL engine
16TrustManager tMgr = new TrustManager( tFct, tAggFct );
17
18// register everything
19ARQ.getContext().setIfUndef( Constants.trustMgrSymbol, tMgr );
20ARQ.getContext().setIfUndef( TrustMergeFunction.contextSymbol, tMrgFct );
21
22ParserTSPARQL.register();
23QueryEngineTrust.register();
Now you can issue tSPARQL queries in the context of a specific information consumer as the following sample code illustrates. Notice, there are only two differences to the execution of ordinary SPARQL queries with ARQ. First, you must explicitly specify language of the given query (see line 19). Second, since tSPARQL queries are executed in the context of an information consumer you must create (line 13) and register (line 23) this consumer.
1import com.hp.hpl.jena.query.ARQ;
2import com.hp.hpl.jena.query.Query;
3import com.hp.hpl.jena.query.QueryFactory;
4import com.hp.hpl.jena.query.QueryExecution;
5import com.hp.hpl.jena.query.QueryExecutionFactory;
6import com.hp.hpl.jena.query.ResultSet;
7
8import org.trdf.trdf4jena.InformationConsumer;
9import org.trdf.trdf4jena.tsparql.Constants;
10import org.trdf.trdf4jena.tsparql.lang.ParserTSPARQL;
11
12// create the current information consumer
13InformationConsumer ic = ...
14
15// specify your tSPARQL query
16String qStr = ...
17
18// prepare query execution
19Query q = QueryFactory.create(qStr, ParserTSPARQL.tSPARQLSyntax );
20QueryExecution e = QueryExecutionFactory.create( q, dataset );
21
22// register the information consumer in the query execution context
23e.getContext().setIfUndef( Constants.consumerSymbol, ic );
24
25// execute the query
26ResultSet results = e.execSelect();
27
28// process the query result
29...