I have a program where I need to re-use the data returned from sparql
commands in others sparql commands. However, I can't figure out how can I
do this when blank nodes are returned. How can I make reference to a blank
node returned by a sparql command in other sparql command? I tried to
simplify my problem in example listed below (is only a short example that
is not my real case). I want to get:
:s01;:o01
:s21;:o22
:s11;:o12

(I need the code line to replace "//How should construct here the sparql
command?" to put the code returning the desire results)

Miguel

public class Test15 {

    public static void main(String[] args) {
        String myData =
            "@prefix : <http://www.example.org/test#> . \n" +
            ":s01 :p1 [:p2 :o02] .\n" +
            ":s11 :p1 :os11 .\n" +
            ":os11 :p2 :o12 . \n" +
            ":s21 :p1 :os21 .\n" +
            ":os21 :p2 :o22 . ";

        Model m = ModelFactory.createDefaultModel();

        m.read( new ByteArrayInputStream( myData.getBytes() ), null, "TTL"
);

        String sparql1 =
            "prefix : <http://www.example.org/test#> " +
            "Select ?p ?o where {?p :p1 ?o}";

        String sparql2 =
            "prefix : <http://www.example.org/test#> " +
            "Select ?o where {_P1_ :p2 ?o}";

        QueryExecution qe = QueryExecutionFactory.create(sparql1, m);

        ResultSet rs = qe.execSelect();
        while(rs.hasNext()) {
            QuerySolution qs = rs.next();
            String inner_sparql2 = "";
            if(qs.get("o").isAnon()) {
                "//How should construct here the sparql command?"
            }
            else {
                inner_sparql2 = sparql2.replace("_P1_", "<"+ qs.get("o") +
">");
            }
            if(inner_sparql2.length()>0) {
                QueryExecution qe2 =
QueryExecutionFactory.create(inner_sparql2, m);

                ResultSet rs2 = qe2.execSelect();

                while(rs2.hasNext()) {
                    QuerySolution qs2 = rs2.next();
                    System.out.println(qs.get("p") +";"+qs2.get("o"));
                }
            }
        }
    }
}

Reply via email to