JRON-to-RDF (databank) conversion - rdfquery usage question

13 views
Skip to first unread message

gklyne

unread,
Jul 13, 2010, 11:24:15 AM7/13/10
to rdfQuery
I'm working on a package to allow mapping between JRON (http://
decentralyze.com/2010/06/04/from-json-to-rdf-in-six-easy-steps-with-
jron/#comment-308) and rdfquery databank objects.

The code below is incomplete, but works as far as it goes, but I'm not
sure if I've got the rdfquery usage idiom right - are there better
ways to assemble the nodes and statements that are added to the
databank. The rdfquery API seems to lean toward assembling each
statement as a string and adding those to the databank, but in this
case, starting from a Javascript object structure, that seems like a
slightly roundabout way to go.

Any thoughts or comments?

#g
--

{{{
jQuery.extend({
JRON_node:
/**
* Create an rdfquery node from a supplied JRON value and
* rdfquery options
*
* @param jronnode is a JRON value for a node: a URI, CURIE,
* bnode or literal
* @param options is an rdfquery options structure, in
particular
* containing a namespaces member with prefix
* definitions for expanding CURIES
* @return an rdfquery node value that can be used to
* construct a triple value, among other
things.
*/
function (jronnode, options)
{
if (typeof jronnode == "string")
{
return jQuery.rdf.literal('"'+jronnode+'"', options);
}
if (typeof jronnode == "object")
{
if (jronnode.__iri)
{
// CURIE or URI here
// { __iri: ... }
var uri = jronnode.__iri;
try
{
// Try for CURIE - more restrictive than JRON
proposal
curi = jQuery.curie(uri, options);
return jQuery.rdf.resource(curi, options);
}
catch (e)
{
log.debug("- not CURIE: "+e);
return jQuery.rdf.resource("<"+uri+">",
options);
}
};
if (jronnode.__text)
{
// Text literal, with or without language
// { "__text": "chat",
// "__lang": "fr" }
var opts = options;
if (jronnode.__lang)
{
opts = jQuery.extend({}, options, { lang:
jronnode.__lang });
}
return jQuery.rdf.literal(jronnode.__text, opts);
};
if (jronnode.__repr)
{
// Typed literal
// { "__repr": "2010-03-06",
// "__type": "http://www.w3.org/2001/
XMLSchema#date" }
var opts = jQuery.extend({}, options, { datatype:
jronnode.__type });
return jQuery.rdf.literal(jronnode.__text, opts);

};
// bnode, with or without __node_id value
// { "foaf_name": "Sandro Hawke",
// "foaf_knows: { "foaf_name": "Eric
Prud'hommeaux",
// "foaf_knows: { "__node_id":
"n334" },
// "__node_id": "n334" }
var nodeid = '[]';
if (jronnode.__node_id)
{
nodeid = "_:"+jronnode.__node_id;
}
return jQuery.rdf.blank(nodeid);
}
e = "JRON_node unrecognized node:
"+jQuery.toJSON(jronnode);
log.debug(e);
throw e;
},

JRON_pred:
/**
* Create an rdfquery node from a supplied JRON predicate
value and
* rdfquery options
*
* @param jronpred is a JRON value for a node: a string
containing a
* URI or CURIE
* @param options is an rdfquery options structure, in
particular
* containing a namespaces member with prefix
* definitions for expanding CURIES.
* @return an rdfquery node value that can be used to
* construct a triple value, among other
things.
*/
function (jronpred, options)
{
return jQuery.JRON_node( { __iri: jronpred }, options);
},

JRONtoRDF:
/**
* Create and return an rdfquery databank object containing
data from
* the supplied JRON object structure.
*
* @param jron is a javascript object containing RDF data
structured
* per JRON.
* @return an rdfquery databank object containing the
JRON data.
*/
function (jron)
{
log.debug("jQuery.JRONtoRDF");
var rdfdatabank = jQuery.rdf.databank()
.base("");
// Find and set base
if (jron.__base)
{
rdfdatabank.base(jron.__base);
};
// Find and set prefixes?
if (jron.__prefixes)
{
for (var pref in jron.__prefixes)
{
log.debug("- prefix "+pref);
//TODO: more robust way to strip spearator char(s)
from prefix
rdfdatabank.prefix(pref.slice(0,-1),
jron.__prefixes[pref]);
};
};
var opts = { namespaces: rdfdatabank.prefix(), base:
rdfdatabank.base() };
log.debug("- options "+jQuery.toJSON(opts));
subj = jQuery.JRON_node(jron, opts);
// Find and save statements
for (var pred in jron)
{
// Assume anything beginning with "__" is special
if (pred.slice(0,2) != "__")
try
{
var obj = jron[pred];
log.debug("- stmt "+subj+" "+pred+"
"+jQuery.toJSON(obj));
pred = jQuery.JRON_pred(pred, opts);
obj = jQuery.JRON_node(obj, opts);
var triple = jQuery.rdf.triple(subj, pred, obj,
opts);
rdfdatabank.add(triple);
}
catch (e)
{
log.debug("- error "+e);
};
}
return rdfdatabank;
}
});
}}}
Reply all
Reply to author
Forward
0 new messages