Takis
Takis

Reputation: 8705

How to use the Jena query builder for RDF-star?

How to convert this simple query to Jena java query builder?

SELECT ?city 
WHERE { <<:athens :connected ?city >>  :distance  500}

I tried this in Clojure and it didn't work. No need to write Clojure i just need the Java code.
I created a TipleNode and gave it as subject to a a Triple that i added to Where, but it complains about the subject.

Error : 
Caused by: java.lang.IllegalArgumentException: Subject (<< https://squery.org/joy/athens @https://squery.org/joy/connected ?city >>) 
must be a URI, blank, variable, or a wildcard. 
Is a prefix missing?  Prefix must be defined before use. 
(->
  (SelectBuilder.)
  (.addVar "?city")
  (.addWhere
    (Triple/create
      (NodeFactory/createTripleNode
        (.asNode (ResourceFactory/createResource "https://squery.org/joy/athens"))
        (.asNode (ResourceFactory/createProperty "https://squery.org/joy/connected"))
        (.asNode (Var/alloc "city")))
      (.asNode (ResourceFactory/createProperty "https://squery.org/joy/distance"))
      (.asNode (ResourceFactory/createTypedLiteral 500))))
  .build
  .toString
  println)

Upvotes: 2

Views: 138

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62653

This is possible only since Apache Jena 5.0.0. In Java, you can do:

    final Triple triple1 = Triple.create(
            NodeFactory.createURI("https://squery.org/joy/athens"),
            NodeFactory.createURI("https://squery.org/joy/connected"),
            Var.alloc("city")
    );
    final Triple triple2 = Triple.create(
            NodeFactory.createTripleNode(triple1),
            NodeFactory.createURI("https://squery.org/joy/distance"),
            ResourceFactory.createTypedLiteral(500).asNode()
    );
    final SelectBuilder sb = new SelectBuilder();
    sb.addVar("?city");
    sb.addWhere(triple2);
    Query query = sb.build();
    System.out.println(query);

Upvotes: 1

Related Questions