This discussion is archived
9 Replies Latest reply: Apr 20, 2011 4:26 PM by alwu RSS

SPARQL UPDATE with named graphs?

852770 Newbie
Currently Being Moderated
Is SPARQL UPDATE with Named graphs supported?

The SPARQL udpate Spec [1] says I can do the following to specify a named graph to update:

PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{ GRAPH <http://example/bookStore> { <http://example/book3>  dc:title  "Fundamentals of Compiler Design" } }

But the program below (a modification of program Test16.java at [2]) does not work.

--- begin program ---

package com.modusoperandi.orardf.datastore;

import oracle.spatial.rdf.client.jena.GraphOracleSem;
import oracle.spatial.rdf.client.jena.ModelOracleSem;
import oracle.spatial.rdf.client.jena.Oracle;
import oracle.spatial.rdf.client.jena.OracleUtils;

import com.hp.hpl.jena.graph.GraphUtil;
import com.hp.hpl.jena.update.UpdateAction;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class TestUpdateNamedGraph {

     public static void main(String[] args) throws Exception {
          String szJdbcURL = args[0];
          String szUser = args[1];
          String szPasswd = args[2];
          String szModelName = args[3];

          Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
          ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle,
                    szModelName);
          GraphOracleSem g = model.getGraph();

          // without named graph
          String insertString = " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
               + " INSERT DATA "
               + " { <http://example/book3> dc:title \"A new book\" ; "
               + " dc:creator \"A.N.Other\" . "
               + " } ";

          System.out.println(insertString);
          UpdateAction.parseExecute(insertString, model);
          ExtendedIterator ei = GraphUtil.findAll(g);
          while (ei.hasNext()) {
               System.out.println("Triple " + ei.next().toString());
          }
          
          // with named graph (http://www.w3.org/TR/sparql11-update/#t411 indicates
          // this should work)
          insertString = " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
               + " INSERT DATA "
               + " { GRAPH <http://example/bookStore>"
               + " { <http://example/book3> dc:title \"A new book\" ; "
               + " dc:creator \"A.N.Other\" . "
               + " } "
               + " } ";

          System.out.println(insertString);
          UpdateAction.parseExecute(insertString, model);
          ei = GraphUtil.findAll(g);
          while (ei.hasNext()) {
               System.out.println("Triple " + ei.next().toString());
          }
          
          model.close();
          OracleUtils.dropSemanticModel(oracle, szModelName);
          oracle.dispose();
     }
}

--- end program ---



Here is the program output showing the error:

--- begin program output ---

PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA { <http://example/book3> dc:title    "A new book" ;                          dc:creator  "A.N.Other" .  }
Triple http://example/book3 @dc:title "A new book"
Triple http://example/book3 @dc:creator "A.N.Other"
PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA { GRAPH <http://example/bookStore> { <http://example/book3> dc:title    "A new book" ;                          dc:creator  "A.N.Other" .  } }
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "graph" "GRAPH "" at line 1, column 64.
Was expecting one of:
<IRIref> ...
<PNAME_NS> ...
<PNAME_LN> ...
<BLANK_NODE_LABEL> ...
<VAR1> ...
<VAR2> ...
"true" ...
"false" ...
<INTEGER> ...
<DECIMAL> ...
<DOUBLE> ...
<INTEGER_POSITIVE> ...
<DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ...
<INTEGER_NEGATIVE> ...
<DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ...
<STRING_LITERAL1> ...
<STRING_LITERAL2> ...
<STRING_LITERAL_LONG1> ...
<STRING_LITERAL_LONG2> ...
"(" ...
<NIL> ...
"}" ...
"[" ...
<ANON> ...

     at com.hp.hpl.jena.sparql.lang.ParserARQUpdate._parse(ParserARQUpdate.java:59)
     at com.hp.hpl.jena.sparql.lang.ParserARQUpdate.parse(ParserARQUpdate.java:30)
     at com.hp.hpl.jena.update.UpdateFactory.create(UpdateFactory.java:34)
     at com.hp.hpl.jena.update.UpdateAction.parseExecute(UpdateAction.java:175)
     at com.hp.hpl.jena.update.UpdateAction.parseExecute(UpdateAction.java:143)
     at com.hp.hpl.jena.update.UpdateAction.parseExecute(UpdateAction.java:105)
     at com.modusoperandi.orardf.datastore.TestUpdateNamedGraph.main(TestUpdateNamedGraph.java:49)

--- end program output ---


References:
[1] http://www.w3.org/TR/sparql11-update/#t411
[2] http://download.oracle.com/docs/cd/E18283_01/appdev.112/e11828/sem_jena.htm


-Mark
  • 1. Re: SPARQL UPDATE with named graphs?
    alwu Pro
    Currently Being Moderated
    Hi,

    This is because SPARQL update Spec has been a moving target and Jena ARQ itself has been a moving target too.
    That particular syntax INSERT DATA { GRAPH ... } is not supported by arq 2.8.5.

    Thanks,

    Zhe Wu
  • 2. Re: SPARQL UPDATE with named graphs?
    852770 Newbie
    Currently Being Moderated
    Thanks. So how DO I insert into a named graph in Oracle using SPARQL UPDATE? :-)
  • 3. Re: SPARQL UPDATE with named graphs?
    alwu Pro
    Currently Being Moderated
    Hi,

    I am communicating with Jena dev team to get a recommended way to do this. Right now, ARQ 2.8.6 allows INSERT ... GRAPH syntax. However, ARQ 2.8.6 changes some package names and that break the integration with Jena Adapter for queries.

    Thanks,

    Zhe
  • 4. Re: SPARQL UPDATE with named graphs?
    alwu Pro
    Currently Being Moderated
    Hi,

    Please let me know if the following works with ARQ 2.8.5. Note that the latest Jena Adapter is certified against Jena 2.6.3 and ARQ 2.8.5.
    import com.hp.hpl.jena.sparql.modify.GraphStoreBasic;
    import com.hp.hpl.jena.update.GraphStore;
    ...

    ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName);
    ModelOracleSem model1 = ModelOracleSem.createOracleSemModel(oracle, szModelName+"1");
    ModelOracleSem model2 = ModelOracleSem.createOracleSemModel(oracle, szModelName+"2");

    insertString =
    " INSERT DATA <http://example.org/bob> {<urn:alice> <urn:loves> <urn:apples> } ";

    GraphStore gs = new GraphStoreBasic(model.getGraph());
    gs.addGraph(Node.createURI("http://example.org/alice"), model1.getGraph());
    gs.addGraph(Node.createURI("http://example.org/bob"), model2.getGraph());

    UpdateAction.parseExecute(insertString, gs);

    Thanks,

    Zhe
  • 5. Re: SPARQL UPDATE with named graphs?
    852770 Newbie
    Currently Being Moderated
    When I do that, I get this result:

    PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA <http://example/bookStore> { <http://example/book3> dc:title    "A new book" ;                          dc:creator  "A.N.Other" .  }
    com.hp.hpl.jena.update.UpdateException: No such graph: http://example/bookStore

    I tried preceding it with a CREATE GRAPH operation, but it did not seem to help.

    Here's the code I changed
    <pre>
              
              // with named graph alternative format
              insertString = "CREATE GRAPH <http://example/bookStore>";
              System.out.println(insertString);
              try {
                   UpdateAction.parseExecute(insertString, model);
              } catch (Exception e) {
                   System.err.println(e);
              }

              insertString = " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
                   + " INSERT DATA "
                   + " <http://example/bookStore>"
                   + " { <http://example/book3> dc:title \"A new book\" ; "
                   + " dc:creator \"A.N.Other\" . "
                   + " } ";

              System.out.println(insertString);
              try {
                   UpdateAction.parseExecute(insertString, model);
              } catch (Exception e) {
                   System.err.println(e);
              }
              ei = GraphUtil.findAll(g);
              while (ei.hasNext()) {
                   System.out.println("Triple " + ei.next().toString());
              }

    </pre>


    When I ran it I got this:


    CREATE GRAPH <http://example/bookStore>
    PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA <http://example/bookStore> { <http://example/book3> dc:title    "A new book" ;                          dc:creator  "A.N.Other" .  }
    com.hp.hpl.jena.update.UpdateException: No such graph: http://example/bookStore
  • 6. Re: SPARQL UPDATE with named graphs?
    alwu Pro
    Currently Being Moderated
    Hi,

    Could you construct GraphStore as follows and then run the update action?

    GraphStore gs = new GraphStoreBasic(model.getGraph());
    gs.addGraph(Node.createURI("http://example.org/alice"), model1.getGraph());
    gs.addGraph(Node.createURI("http://example.org/bob"), model2.getGraph());

    UpdateAction.parseExecute(insertString, gs);

    Thanks,

    Zhe
  • 7. Re: SPARQL UPDATE with named graphs?
    852770 Newbie
    Currently Being Moderated
    What are "model1" and "model2"? Are you suggesting I should create two additional empty ModelOracleSem objects?

    I tried this code

    <pre>
              ModelOracleSem model1 = ModelOracleSem.createOracleSemModel(oracle,
                        szModelName+"bookStore");

              String graphName = "http://example/bookStore";
              GraphStore gs = new GraphStoreBasic(model.getGraph());
              gs.addGraph(Node.createURI(graphName), model1.getGraph());

              insertString = " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
                   + " INSERT DATA "
                   + " <" + graphName + "> "
                   + " { <http://example/book3> dc:title \"A new book\" ; "
                   + " dc:creator \"A.N.Other\" . "
                   + " } ";

              System.out.println(insertString);
              try {
                   UpdateAction.parseExecute(insertString, model);
              } catch (Exception e) {
                   System.err.println(e);
              }
              ei = GraphUtil.findAll(g);
              while (ei.hasNext()) {
                   System.out.println("Triple " + ei.next().toString());
              }

              
    </pre>

    but still got the "No Such Graph" message below:
    PREFIX dc: <http://purl.org/dc/elements/1.1/> INSERT DATA <http://example/bookStore> { <http://example/book3> dc:title    "A new book" ;                          dc:creator  "A.N.Other" .  }
    com.hp.hpl.jena.update.UpdateException: No such graph: http://example/bookStore

    Edited by: 849767 on Apr 13, 2011 8:19 AM
  • 8. Re: SPARQL UPDATE with named graphs?
    alwu Pro
    Currently Being Moderated
    YES, I have a few models.

    ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName);
    // insert into model
    ModelOracleSem model1 = ModelOracleSem.createOracleSemModel(oracle, szModelName1);

    ...
    ModelOracleSem model2 = ModelOracleSem.createOracleSemModel(oracle, szModelName2);
    ...
    // insert into model2

    Thanks,

    Zhe
  • 9. Re: SPARQL UPDATE with named graphs?
    alwu Pro
    Currently Being Moderated
    Hi,

    Here is an update. If you don't want to use GraphStore or GraphStoreBasic directly, you can change the ARQ's GraphStoreBasic implementation (it is open source) and recompile/build the arq library.

    To do that, find the GraphStoreBasic.java in ARQ 2.8.5 distribution. Make the following changes to the constructor that takes Dataset as argument.

    public GraphStoreBasic(Dataset ds)
    {
    // super(ds.asDatasetGraph()) ; comment out.

    this(ds.getDefaultModel().getGraph());
    Iterator<String> it = ds.listNames();
    while (it.hasNext())
    {
    String name = it.next();
    Model m = ds.getNamedModel(name);
    addGraph(Node.createURI(name), m.getGraph());
    }
    }

    Thanks,

    Zhe

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points