Skip to content

Creating BGPs using the Functional BGP API

Pieter Bonte edited this page Sep 14, 2021 · 3 revisions

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.

Definition of a BGP using the functional API

In the following example we will create a BGP using the functional API that joins two TPs: ?color rdf:type ?type and ?color test:hasName ?name. We first create the TPs with Vars for the variable and Terms for the fixed values. Once the TPs have been defined, we can create a BGP from a single TP and start joining other TPs 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 TPs can be joined in the same BGP:

       BGP bgp = BGP.createFrom(tp1)
                .join(tp2)
                .join(tp3)
                ...
                .join(tp_n)
                .create();

Defining the join algorithm

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());