Merging two RDF graphs?

28 views
Skip to first unread message

mhausenblas

unread,
Mar 4, 2009, 1:53:22 AM3/4/09
to rdfQuery
Jeni,

Got a quick question ( as I was unable to find it in the doc): how to
merge two RDF graphs? I'm using rdfquery for pushback [1] and have the
following code:

var bugformrdf = $('#bugform').rdf();
var jsoningraph = $.toJSON(bugformrdf.databank.dump());
// go through all updateable fields and update with current values
(set by user) - this is our output graph
var outgraph = null;

bugformrdf
.prefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
.prefix('pb', 'http://ld2sd.deri.org/pb/ns#')
.prefix('dcterms', 'http://purl.org/dc/terms/')
.where('?rdform rdf:type pb:RDForm')
.where('?rdform pb:field ?field')
.where('?field rdf:type pb:UpateableField')
.where('?field pb:key ?key')
.where('?field dcterms:title ?title')
.where('?field pb:value ?val')
.where('?val rdf:value ?fval')
.each(function () {
//for example for @about='http://ld2sd.deri.org/pb/demo#fo1.f2.val'
var newval = $("[about=" + this.val.value + "]").val();
if(debug) alert('Gonna pushback value of field with label "' +
this.title.value + '" and key=<' + this.key.value +"> with value={" +
newval + "}");
// create the new graph here:
outgraph = $.rdf.databank()
.base('http://ld2sd.deri.org/pb/demo#')
.prefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
.prefix('pb', 'http://ld2sd.deri.org/pb/ns#')
.prefix('dcterms', 'http://purl.org/dc/terms/')
.add('<' + this.field.value + '> rdf:type pb:UpateableField .')
.add('<' + this.field.value + '> pb:key <' + this.key.value +
'> .')
.add('<' + this.field.value + '> pb:value <' + this.val.value +
'> .')
.add('<' + this.val.value + '> rdf:value ' + newval + ' .');
//outgraph.add(newgraph.dump());
}
);

So, I was expecting something like outgraph.add(newgraph.dump()) to
work, but doesn't. Any hints?

Let me say that rdfquery is an awesome thing and makes a lot of things
easier and possible, actually ;) I'll try to contribute with some
examples and documentation ...

Cheers,
Michael

[1] http://esw.w3.org/topic/PushBackDataToLegacySources

Jeni Tennison

unread,
Mar 4, 2009, 8:08:23 AM3/4/09
to mhausenblas, rdfq...@googlegroups.com
Michael,

> Got a quick question ( as I was unable to find it in the doc): how to
> merge two RDF graphs?

I'm trying to figure out what you're aiming to do, because there are a
few different nuances on what 'merge' might mean here.

I think you probably want to union two rdfQuery objects or databanks
together, which you can do by adding them to each other. Perhaps
you're after:

merged = outgraph.add(newgraph);

Note that merged is a separate object from outgraph and newgraph (a
separate databank that is the union of the two other databanks), and
that any further addition to it causes the triples to be added to both
original databanks.

You could also manually iterate over the triples in one of the
databanks and add them to the other, if you wanted, but that would
actually change the original databank (and the results of any queries
on it), obviously.

I haven't yet implemented an 'import' of JSON (or any other format),
but that's something that I do intend to do.

I'm really happy to accept suggestions for ways things could be made
easier or more intuitive, or other developments that would be useful.
And if you want to donate some documentation, that would be great! Let
me know your Google mail address and I can add you as a member of the
project.

Cheers,

Jeni
--
Jeni Tennison
http://www.jenitennison.com

mhausenblas

unread,
Mar 4, 2009, 9:20:07 AM3/4/09
to rdfQuery
Jeni,

Thanks a lot, will try ... merge I mean merge as in RDF graph merge
[1].

Btw, have started now to use rdfjquery for RDForms, see [2]

Cheers,
Michael

[1] http://www.w3.org/TR/rdf-mt/#entail
[2] http://esw.w3.org/topic/PushBackDataToLegacySourcesRDForms

mhausenblas

unread,
Mar 4, 2009, 9:27:37 AM3/4/09
to rdfQuery
Ok, now, I know. My problem is that I have

var tmpgraph = $.rdf.databank()
.add('<' + fieldURI + '> rdf:type pb:UpateableField .')
.add('<' + fieldURI + '> pb:key <' + keyURI + '> .')
.add('<' + fieldURI + '> pb:value <' + valURI + '> .')
.add('<' + valURI + '> rdf:value ' + val + ' .');


and want to do

outgraph.add(tmpgraph);

but that doesn't work as tmpgraph is a databank .... any ideas?

Cheers,
Michael

Jeni Tennison

unread,
Mar 4, 2009, 9:48:53 AM3/4/09
to rdfq...@googlegroups.com
Michael,

If outgraph is a databank too, then it should work (remember to
capture the result of the .add() method and use that as the merged
result, as it's a new databank rather than an update to the old one).

If outgraph is a rdfQuery object, then you need to do:

merged = outgraph.add($.rdf({databank: tmpgraph }));

to create a new merged rdfQuery object, or:

merged = outgraph.databank.add(tmpgraph);

to create a new merged databank object.

mhausenblas

unread,
Mar 4, 2009, 10:17:18 AM3/4/09
to rdfQuery

Jeni,

Dunno, maybe I'm too stupid (I know I am ;) - I'm close to giving up,
don't grok the difference between rdfQuery and databank, etc.

I very much appreciate your help here Jeni! Can you just, to make the
thing easier (and I really have to finalise this) tell me wtf I have
to change in [1], please, please, please? ;)

Cheers,
Michael

[1] http://pastebin.com/f873d9ab


On Mar 4, 2:20 pm, mhausenblas <michael.hausenb...@deri.org> wrote:

Jeni Tennison

unread,
Mar 4, 2009, 10:41:49 AM3/4/09
to mhausenblas, rdfq...@googlegroups.com
Michael,

You don't seem to be capturing the result of adding the tmpgraph to
the outgraph. As I said, it's a different object, so if you want to do
something with it, you need to capture it. Perhaps doing something
like the following on line 66:

outgraph = outgraph.add(createDiffFieldGraph(this.field.value,
this.key.value, this.val.value, newval));

or you might want to replace that line with something like:

addFieldGraph(outgraph, this.field.value, this.key.value,
this.val.value, newval);

and then have:

function addFieldGraph(databank, fieldURI, keyURI, valURI, val) {
databank
.add('<' + fieldURI + '> rdf:type pb:UpateableField .')
.add('<' + fieldURI + '> pb:key <' + keyURI + '> .')
.add('<' + fieldURI + '> pb:value <' + valURI + '> .')


.add('<' + valURI + '> rdf:value ' + val + ' .');

return databank;
}

which actually adds the triples to the databank without going to the
trouble of creating a separate databank for them.

A databank is a triplestore. A rdfQuery object is an object that
represents a query over a triplestore.

Hope that helps,

mhausenblas

unread,
Mar 4, 2009, 11:01:15 AM3/4/09
to rdfQuery
That was it. Thank you sooo much!!! Credits will be added, for sure ;)

Cheers,
Michael

Jeni Tennison

unread,
Mar 4, 2009, 2:39:17 PM3/4/09
to rdfq...@googlegroups.com, mhausenblas
Michael,

On 4 Mar 2009, at 16:01, mhausenblas wrote:
> That was it. Thank you sooo much!!! Credits will be added, for sure ;)


Glad you got it working!

By the way, if you do want to do diffs, the best I can offer is the
except() method, either on two databanks or two queries. Something like:

added = after.except(before);
removed = before.except(after);

I'm going to be doing a presentation at OXON SWIG next week about
rdfQuery; if you get something visible running, I'd love to demo it if
that would be OK?

Michael Hausenblas

unread,
Mar 8, 2009, 1:46:37 AM3/8/09
to Jeni Tennison, rdfq...@googlegroups.com

Jeni,

Again, thanks a lot for your great first-level support - just as brilliant
as rdfquery itself :)

I've now got a first demo of what we call RDForms (RDFa marked-up + HTML
form) that has rdfquery as it's core [1]. I'm still improving and adding
stuff, but you may wanna have a look at it and demo it, if you think it's
not too premature.

The vision of RDForms/pushback [2] is to turn the read-only Semantic Web/Web
of Data into a read/write one. The starting point is always the 'semantic
space', that is, some RDF graph, such as [3] (RDFised version of the Twitter
statuses of a demo account) . What I did so far is parsing this RDF/XML
(unfortunately this is currently not possible with rdfquery, right?) and, by
using a predefined RDForm, pushing back the changes (add/delete) to a Web
2.0 source (Twitter, in this very first and very simple example).

My Goolge email, btw, is Michael.H...@gmail.com ;)

Cheers,
Michael

[1]
http://esw.w3.org/topic/PushBackDataToLegacySourcesRDForms#RDForms_Examples
[2] http://esw.w3.org/topic/PushBackDataToLegacySources
[3]
http://linkeddata.uriburner.com/about/rdf/http://twitter.com/pushback_demo

--
Dr. Michael Hausenblas
DERI - Digital Enterprise Research Institute
National University of Ireland, Lower Dangan,
Galway, Ireland, Europe
Tel. +353 91 495730
http://sw-app.org/about.html
http://webofdata.wordpress.com/

Reply all
Reply to author
Forward
0 new messages