-
Notifications
You must be signed in to change notification settings - Fork 12
Creating BGPs using the Functional BGP API
RSP4J contains a functional API as part of the yasper package for the definition of Basic Graph Patterns (BGP)s. It allows the definition of multiple Triple Patterns (TP)s and join them together. Once a BGP
is created using the functional API it can be used to evaluated Streams
of Graphs.
In the following example we will create a BGP
using the functional API that joins two TP
s: ?color rdf:type ?type
and ?color test:hasName ?name
. We first create the TP
s with Var
s for the variable and Term
s for the fixed values. Once the TP
s have been defined, we can create a BGP
from a single TP
and start joining other TP
s as in the example below:
VarOrTerm s1 = new VarImpl("color");
VarOrTerm p1 = new TermImpl("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
VarOrTerm o1 = new VarImpl("type");
TP tp1 = new TP(s1,p1,o1);
VarOrTerm p2 = new TermImpl("http://test/hasName");
VarOrTerm o2 = new VarImpl("name");
TP tp2 = new TP(s1,p2,o2);
BGP bgp = BGP.createFrom(tp1)
.join(tp2)
.create();
Once the BGP
has been created we can evaluate it on a Stream
of Graphs
, producing a Stream
of Binding
as the result of the evaluation of the BGP
:
Stream<Graph> g = ...
Stream<Binding> bindings = bgp.eval(g);
Note that many TP
s can be joined in the same BGP
:
BGP bgp = BGP.createFrom(tp1)
.join(tp2)
.join(tp3)
...
.join(tp_n)
.create();
When evaluating the BGP
, a JoinAlgorithm
is used to perform the joins. In its standard configuration a Nested Join Loop
is used. However, other algorithms such as Hash Joins
can be configured as well:
BGP bgp = BGP.createFrom(tp1)
.join(tp2)
.create();
bgp.setJoinAlgorithm(new HashJoinAlgorithm());