On 31/05/13 00:16, Cindy A McMullen wrote:
Excellent, that helped.  Here's the code:

DatasetGraph dsg = DatasetGraphFactory.createMem();
Node g1 = Node.createURI("ns:Event1");
Triple e1 = new Triple(Node.createURI("w:Mike"), Node.createURI("w:follows"), 
Node.createURI("w:Dave"));
Quad quad1 = new Quad(g1, e1);
dsg.add(quad1);

I think I got hung up because I was starting my DatasetGraph by loading my 
ontology from file, then building it out from there.

Thanks --

On May 30, 2013, at 4:18 PM, Rob Vesse wrote:

Per the Javadoc for DatasetGraphFactory.create(Graph):

"New graphs must be explicitly added."

DatasetGraphFactory.create(Graph) isn't the general purpose DatasetGraph.

You get that from

    DatasetGraphFactory.create() ;

DatasetGraphFactory.create(Graph) is for the case where you have one graph and want it to be the default graph - so you can treat one graph as a dataset. Having it fixed means that the app has to be aware its going from one-default-graph to named graphs, such as when it's collecting graphs.

If you are going to be doing multi-graph work with quads, then use DatasetGraphFactory.create() as in your second code sample.

This could be changed - as long as there is a way to have a fixed set to support the usage with known fixed graphs and so catch errors of attempting to add stray data.

        Andy

PS You may wish to use URIs for Node names. w:follows is an absolute URI ... with scheme name "w". It's is not a prefixed name.


Thus you need to call add(Node, Graph) explicitly if you wish to introduce
quads into a graph that does not already exist.
Alternatively I would suggest creating a DatasetGraph with
DatasetGraphFactory.createMem() and simply adding to it as necessary I.e.
start from an empty dataset.


Generally I prefer to work with the Dataset abstraction and only go to the
lower level DatasetGraph where necessary.




Rob



On 5/30/13 3:10 PM, "Cindy A McMullen" <[email protected]> wrote:

How do I add a named graph to an in-memory Dataset?  This code works with
TDB, but the in-memory implementation throws:

Exception in thread "main" com.hp.hpl.jena.shared.JenaException: No such
graph: ns:Event1
at
com.hp.hpl.jena.sparql.core.DatasetGraphCollection.add(DatasetGraphCollect
ion.java:41)
    [java]      at
oracle.social.discovery.rdf.jena.QueryMemoryGraph.addData(QueryMemoryGraph
.java:94)
    [java]      at
oracle.social.discovery.rdf.jena.QueryMemoryGraph.main(QueryMemoryGraph.ja
va:33)

Here's the code:

String inputFileName  = "foo-ontology.ttl";
       Model model = ModelFactory.createDefaultModel();
       InputStream in = FileManager.get().open( inputFileName );
       model.read(in, "", "TTL");
       DatasetGraph dsg =  DatasetGraphFactory.create(model.getGraph());
ŠŠ

Node g1 = Node.createURI("ns:Event1");
       Triple e1 = new Triple(Node.createURI("w:Mike"),
Node.createURI("w:follows"), Node.createURI("w:Dave"));
       dsg.add(new Quad(g1, e1));

---

I'm using apache-jena-2.10.0



Reply via email to