Thanks for your reply Andy. What I'm trying to do is replace a named graph
with a newer version. The way I do this is by first removing the old graph
and then I add the new one. It seems that the "removing the old version"
part is causing the problem. I added some debug code to the method like
this:
protected Resource addOrReplaceResource(Dataset dataset, String
resourceURI, InputStream instream)
{
Resource resource = dataset.containsNamedModel(resourceURI) ?
dataset.getNamedModel(resourceURI).getResource(resourceURI) : null;
if (resource != null) {
System.out.print("before remove: ");
for (Iterator iter = dataset.listNames(); iter.hasNext(); ) {
System.out.println(" " + iter.next());
}
dataset.asDatasetGraph().removeGraph(resource.asNode());
System.out.print("after remove: ");
for (Iterator iter = dataset.listNames(); iter.hasNext(); ) {
System.out.println(" " + iter.next());
}
}
Model model = dataset.getNamedModel(resourceURI);
model.read(instream, null);
resource = model.getResource(resourceURI);
return resource;
}
When I ran the code with
resourceURI="https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/20",
I got this output:
before remove:
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/11
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/10
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/6
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/20
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/18
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/22
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/5
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/1
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/9
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/23
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/8
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/27
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/13
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/3
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/25
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/19
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/2
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/24
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/14
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/4
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/26
https://frankb-tp.torolab.ibm.com:9443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/21
after remove:
Notice that there are no names being returned from dataset.listNames()
after the call to removeGraph(). I was expecting it to only have removed
"...WorkItem/20", but it seems to have removed all the graphs.
Any idea why this is happening? Am I using the wrong API for this?
Thanks,
Frank
Andy Seaborne <[email protected]> wrote on 02/01/2011 04:20:58
PM:
> [image removed]
>
> Re: Problem replacing graph
>
> Andy Seaborne
>
> to:
>
> jena-users
>
> 02/01/2011 04:23 PM
>
> Please respond to jena-users
>
>
>
> On 01/02/11 14:47, Frank Budinsky wrote:
> >
> >
> > Hi All,
> >
> > I'm having trouble doing a fairly straightforward operation in Jena,
which
> > probably just means I'm doing something wrong. I'm trying to add or
replace
> > a resource (as a graph) in my dataset. The code I wrote looks something
> > like this:
> >
> > protected Resource addOrReplaceResource(Dataset dataset, String
> > resourceURI, InputStream instream)
> > {
> > Resource resource = dataset.containsNamedModel
(resourceURI) ?
> > dataset.getNamedModel(resourceURI).getResource
> > (resourceURI) : null;
> > if (resource != null)
> > dataset.asDatasetGraph().removeGraph(resource.asNode
());
> > Model model = dataset.getNamedModel(resourceURI);
> > model.read(instream, null);
> > resource = model.getResource(resourceURI);
> > return resource;
> > }
> >
> > When a resourceURI is added for the first time, it works fine but when
> > replacing a graph the call to removeGraph() seems to wipe out the
entire
> > dataset. The only thing left in the dataset after that is the single
> > resource which gets added after the removeGraph call.
>
> resources aren't added by getResource - in fact, it does not change the
> model and and RDF model does not have resources without being in a
> statement.
>
> >
> > Is there something obvious that I'm doing wrong here?
> >
> > Thanks,
> > Frank.
>
> removeGraph(String) removes the graph (all the statements).
>
> Then it depends what's in the RDF/XML in instream.
>
> What happens if you print the model, or print the dataset, just before
> the return?
>
> Andy