Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Created December 5, 2012 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bollwyvl/4211863 to your computer and use it in GitHub Desktop.
Save bollwyvl/4211863 to your computer and use it in GitHub Desktop.
d3-style RDF API experiments
// mock classes, no state
var rdfstore = {
Store: function(){}
},
d3 = {
rdf: function(store){}
};
d3.rdf.query = function(){
var query = function(){ return query; };
query.select = function(){
var select = function(){return select;};
arguments.map(function(bound_name){
var binding,
usage = function(content){
return usage;
};
binding = query[bound_name] = function(_){
// what does it mean if actually called? guess: dereference
if(!arguments.length) return "?" + bound_name;
/*
if there is an argument, this is a graph pattern, which is
chainable... so this is more of a "usage"
*/
return usage(_);
};
});
return select;
};
query.prefix = function(prefix, uri){
query[prefix] = function(){ return query[prefix]; };
arguments.slice(2).map(function(term)){
query[prefix][term] = function(_){
// more turtles... probably...
if(!arguments.length) return [prefix, term];
};
});
return query;
};
query.where = function(){
var where = function(){return where;};
where.optional = function(){}
return where;
};
query.toString = function(){
return "SPARQL, bitches.";
};
return query;
}
/*
This is a list of the different kind of queries currently implemented:
SELECT queries
[5] SelectQuery ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( Var+ | '*' )
DatasetClause* WhereClause SolutionModifier
UNION, OPTIONAL clauses
NAMED GRAPH identifiers
LIMIT, OFFSET
ORDER BY clauses
SPARQL 1.0 filters and builtin functions
variable aliases
variable aggregation: MAX, MIN, COUNT, AVG, SUM functions
GROUP BY clauses
DISTINCT query modifier
CONSTRUCT queries
ASK queries
INSERT DATA queries
DELETE DATA queries
DELETE WHERE queries
WITH/DELETE/INSERT/WHERE queries
LOAD queries
CREATE GRAPH clauses
DROP DEFAULT/NAMED/ALL/GRAPH clauses
CLEAR DEFAULT/NAMED/ALL/Graph clauses
FILTER EXISTS / NOT EXISTS operators
*/
rdf = d3.rdf(new rdfstore.Store());
function test_select(){
var target = [
'PREFIX foaf: <http://xmlns.com/foaf/0.1/>',
'SELECT ?nameX ?nameY ?nickY',
'WHERE',
' { ?x foaf:knows ?y ;',
' foaf:name ?nameX .',
' ?y foaf:name ?nameY .',
' OPTIONAL { ?y foaf:nick ?nickY }',
' }'].join("\n");
var q = rdf.query()
.prefix("foaf", // no other way to achieve
"http://xmlns.com/foaf/0.1/", // q.foaf.XYZ?
"name", "knows");
s = q.select("nameX", "nameY", "nickY") // my kingdom for __getattr__
.extra("x", "y") // not selected...
s.where(
q.x(q.foaf.knows(q.y)) // dubious... but works
(q.foaf.name(q.nameX)), // scope will be gross here
q.y(q.foaf.name(q.nickY))
)
.optional(q.y(q.foaf.nick, q.nickY)); // modifies the `where`
console.log("SQL Matches", q.sparql() == target);
var entries = s.execute()
/* a list of objects
[
{nameX: <a name>, nameY: <another name>, nickY: <>},
...
]
*/
var tree = d3.nest()
.key(function(d) { return d.nameX; })
.entries(entries)
/* returns a tree of object-y things
[
{key: <a name>, values: [
{nameX: <a name>, nameY: <another name>, nickY: <>}
]},
...
]
this is pretty useful, i guess...
*/
// but maybe I'd like something like this...
var obj = d3.treeish()
.root(function(d){ return d.nameX; })
.leaf("name", function(d){ return d.nameX; })
.branch("knows", function(d){ return d.nameY; })
.leaf("name", function(d){ return d.nameY; })
/* that made this...
[
{
name: <a name>,
knows: [
{name: <another name>},
...
]
}
]
*/
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment