Hi,

did you try addGraph method on the SelectBuilder object with another SelectBuilder object on which you add the VALUES clause and the triple pattern?

In your case with the UNION I think the flow should be something like


SelectBuilder sb = ...

SelectBuilder sb_g1 = ... // first graph pattern
sb_g1.addValueVar(

SelectBuilder sb_g2 = ... // second graph pattern

sb.addUnion(sb_g1)
sb.addUnion(sb_g2)


But, I think there might be some bug or limitation:

While

SelectBuilder sb1 = new SelectBuilder()
                .addWhere("?s", "?p", "?o")
                .addWhereValueVar("?s", "foo1")
                .addWhereValueVar("?p", "foo1")
                .addWhereValueVar("?o", "foo1");
System.out.println(sb1.buildString());


Returns the expected query

SELECT  *
WHERE
  { ?s  ?p  ?o
    VALUES ( ?s ?p ?o ) {
      ( "foo1" "foo1" "foo1" )
    }
  }

passing that builder to another builder where it is supposed to make the graph

Node g1 = NodeFactory.createURI("http://example/g1";);
SelectBuilder sb2 = new SelectBuilder().addGraph(g1,
                new SelectBuilder()
                        .addWhere("?s", "?p", "?o")
                        .addWhereValueVar("?s", "foo1")
                        .addWhereValueVar("?p", "foo1")
                        .addWhereValueVar("?o", "foo1"));
System.out.println(sb2.buildString());


leads to only

SELECT  *
WHERE
  { GRAPH <http://example/g1>
      { ?s  ?p  ?o}}


From the code I'd say the issue is that the build() method isn't being called before adding the query pattern as element to the graph. But that's just a guess.


So what works is to force a build() on the WhereHandler explicitly before passing it to the graph:


Node g1 = NodeFactory.createURI("http://example/g1";);
SelectBuilder sbG1 = new SelectBuilder()
                .addWhere("?s", "?p", "?o")
                .addWhereValueVar("?s", "foo1")
                .addWhereValueVar("?p", "foo1")
                .addWhereValueVar("?o", "foo1");
sbG1.getWhereHandler().build(); // the important line
SelectBuilder sb2 = new SelectBuilder().addGraph(g1,
                sbG1);
System.out.println(sb2.buildString());


then we get


SELECT  *
WHERE
  { GRAPH <http://example/g1>
      { ?s  ?p  ?o
        VALUES ( ?s ?p ?o ) {
          ( "foo1" "foo1" "foo1" )
        }
      }}


It don't know if this is intended or if you want to open an issue or wait for Claude Warren which is the principle maintainer of the query builder code and he'll provide a better answer than me.


I have one question, any reason for using Jena 3.5.0 which is 6 years old?

On 06.12.23 01:33, Dhamotharan, Kishan wrote:
Hi All,

I have been trying to construct the below query using Jena query builder. I 
have tried multiple different ways to build it, nothing seems to work. Adding 
values block inside Graph seems to be not possible. We are using Jena 3.5.


SELECT ?subject ?predicate ?object ?graph
WHERE {

{
   GRAPH <G1> {
     ?subject ?predicate ?object

       VALUES (?subject ?predicate ?object) {
       (<S1> <P1> <O1>)
       (<S2> <P2> <O2>)
       (<S3> <P3> <O3>)
      }

     BIND(<G1> AS ?graph)
   }
}

UNION

{
   GRAPH <G2> {
     ?subject ?predicate ?object

       VALUES (?subject ?predicate ?object) {
       (<S4> <P4> <O4>)
       (<S5> <P4> <O5>)
       (<S6> <P4> <O6>)
    }

     BIND(<G2> AS ?graph)
   }
}

Can anyone suggest on how this can be done ? Any help is appreciated 😊


Thanks
Kishan Dhamotharan

--
Lorenz Bühmann
Research Associate/Scientific Developer

Email [email protected]

Institute for Applied Informatics e.V. (InfAI) | Goerdelerring 9 | 04109 
Leipzig | Germany

Reply via email to