SPARQL templating for fun and profit

tldr: SPARQL with a simple templating language gets incredibly powerful.

Hateful WordPress, must get my own host back up + silly danny code up again soon (will be hyperdata.it this time around, remind me to make that live soon).

Lost half of this doc because WP doesn’t do Job One. Oh sorry, wrong link, but I believe DanC is a good church-goer, so not too far off the mark. This is the more practical Job One (ymmv).

So Mr. DuCharme, take browser stuff, pump it through templating.

GRAPH ~{here}~

No sure that’s the best syntax, but it’s just a little config thing to make Mustache templating (presumably others too) recognise a slightly different marker for placeholders. I chose that to save me getting confused because of SPARQL’s big use of Mustaches {} .

I was able to make an editable site without even thinking, a Wiki that we’ve been using internally, it just works. Just use SPARQL Update, use Mustache templating on the queries/updates. Can do anything from the browser, little bit of jQuery for shinies to help, it’s Ajax bits don’t take a lot of thought. Fuseki is a very nice bit of code (thanks AndyS, Jena team). I’m sure the OpenLink stuff is too, but I get a bit scared because that seems to try to do too much. Nice that it uses the same endpoint for query and update, Kingsley or one of his awesome crew will no doubt correct me there. We all know it’s not SPARQL stores aren’t quite RESTful, but these things do solve a lot of problems in a straightforward fashion. At least it’s not Ajax with the last letter standing for XML, been there, done that, nice idea but it doesn’t simplify things. Can only be hypermedia to do this kind of stuff, webbishness. I think that Turtle gets there, opening the door to hyperdata, but even that can be a struggle sometimes. Can only be hypermedia to do this kind of stuff. It does seem like an uphill struggle, but all you really need is links in there, even PDFs can do that, despite the painful proprietary angle. Konwabueno innit, need to make sure i18n is in there too. RDF tends to be pretty good on that front, is pretty much native, timbl ftw. I haven’t even tried some of the other graph data store, the name/pointer kinda stuff, worries me that, like JSON, they don’t have URLs as native (JSON-LD ftw there, though I did wish they’d have let me flip the syntax over, put the simple JSON first, schema stuff, disambiguation after, more likely to engage the poor old API builders, IMHO).

Beware that despite it being a Recc, different stores have slightly different conventions for query/update URLs, not all support simple POSTing/PUTing of an RDF format (Fuseki does, and it’s even now got an Apache security mechanism available, thanks small gods).

With the project I was , am, working on I wound up doing rather a crude regex hack to shift the prefixes to the top, drop the @ from prefixes and add a dot (or the other way around, I forget which, another slight non-intuitive thing I’m afraid, just have to get over it, there was probably a good reason, like most W3C specs, ymmv again, but I couldn’t be arsed looking at the proper syntax def, sorry, not entirely i18n’d that phrase, but one does need simple answers, not that huge hornet buzzing around in autumn).

Only really posting to point Bob DuCharme at this idea, hoping to prompt maybe another book from him… (mine Gott, looks like you can even rent the pages, though I I suspect Bob’s got most of the material online somewhere anyway).

So, I love the SPARQL Protocol and RDF Query Language (naming recursion pretty much halts at RDF, the Resource Description Framework) . If you do code anywhere near the web, you should know it.

Before SPARQL arrived, I hated it. I’d had a few years in a sysadmin job which involved lots of SQL. Anything that looked like that quasi-relational nonsense, run a mile. (Looks like the Database Debunkers have gone offline, but check out anything by a guy called Date, or just that original paper which tells proper relational, I forget it’s author, but that makes sense, even though it’s a bit mathsy).

Thing is, RDF is a graph structure, so you can project it in every kinda way. Can look like a graph, a tree or even tables. Or just triple after triple. All the way down.

But when it did arrive, and I played, it turned out to be awesome. So much so I named a lovely cat for it (ok, not exactly, SPARQL is SPARQL, the lovely moggie was Sparql, see what I did there, syntax monsters?).

I can’t remember if there was actually a vote on it, but I know I favoured the approach to a query language as proposed by Uche & co. Pointerish things, taking you through the graph. But what was accepted seemed a monstrosity that looked painfully like SQL :

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE {
   ?person foaf:name ?name .
}

But please take it from me, it’s a lot better than it looks.

Sorry I ran out of steam on this post. But if you got this far I’m sure you can think it through. Create SPARQL queries using a simple, well-tested templating thingy (like Mustache) to fill in the blanks, like graph name (just use the namespace, no?). Is very easy to get creative with simple queries and updates (SPARQL 1.1). These templating thingys support conditionals and basic loops etc. I did wind up with loads of cruft, because it was so easy to try things. And I nearly didn’t get remotely near the requirement spec when I got confused over FROM and FROM NAMED (isn’t very intuitive, IMHO agin, get the wrong SPARQL store config (easily done) and it just stops working).

Yeah, take that, and hopefully you can run with it, yet another IMHO.I have been around this stuff a long while, so there’s at least a 50/50 chance of it working

Advertisements

2 thoughts on “SPARQL templating for fun and profit

  1. bobdc

    Looks cool! I’m unfamiliar with Mustache, and that looks cool too. “just use the namespace, no?” Depends on the triple store.

    At TopQuadrant we have our own SPARQL-web templating language that you can read about at http://uispin.org/ui.html, but that’s not supported by the free version of TopBraid Composer. (If I got too excited about any alternatives to it, I’d have to tiptoe around the home office when I’m there.)

    Reply
    1. Danny Ayers Post author

      Hi Bob,
      Late response – this WP blog doesn’t feel like my own somehow, keep forgetting it’s here. Will be much better once I’ve got my own stuff back online (in progress..).

      One thing I just realised after a few email exchanges with AndyS (who is joining TopQuadrant?) re. templating SPARQL – the bit I’ve been delighted by might not be obvious. Sure, fire off a query and render the results through a template. I do have a bit of that going on in my UI.
      But what I’ve found really powerful is using templates for the queries/updates themselves.

      You no doubt got that right away – if I remember correctly SPIN can be SPARQL queries expressed in RDF, so I guess you can fill in placeholders easily that way.

      Anyhow, although the mustache is only operating at the string level, it does make it easy to do pretty neat things easily. The usual delims are {{}} but I was getting confused with SPARQL’s plenty of {} so tweaked the config to use ~{}~.

      e.g.

      INSERT DATA {
      GRAPH <~{graphURI}~> {
      <~{resource}~> a <~{rdfType}~> .
      ...
      }
      }

      What I like about mustache specifically is that it doesn’t try to do too much. They call it ‘logic free’, but it does understand simple things like conditionals, lists. (I’ll probably use is for the next rev of my blog software – should be easy enough to render a list of items).

      Incidentally, after years of hopping around different languages/formats/toolkits, I seem to be settling down to a handful to do 90+% of my own stuff : HTML+CSS, Javascript (browser & node), Turtle, SPARQL. Things like JSON, XML only if unavoidable. HTTP everywhere! And recently added templating… Oh yeah, also I’ve started liking markdown for typing stuff up. The github flavour seems to translate cleanly, much more reliably than my HTML-by-hand 🙂
      Toolwise, I’m kinda in the process of knocking together a little bit of browser-based kit to cover these.

      Heh, after having said all that, I’ve also got Python back in my intray. Have used it in the past but very rusty. But I want to play with some machine learning stuff, have got an external GPU card (“Jetson”) to play with when time permits. It looks like Python is the best supported for using with that (C++ is probably the biggest alternative, but I really don’t want to go there). Once I’ve got my head around the current conventional ways of expressing neural net topologies & parameters, I want to get RDF+HTTP in on the act. Although the processing itself will need to be passed to the optimised hardare, the configs look to me just like directed graphs. Modularity obviously desirable, async comms useful in places – I’m think little RDF-based agents/microservices. (If I get on to this by next year, I’ll be a happy man 🙂

      Heh #2, also been dusting of my Java a bit for figuring out how to make Android apps. Seems pretty straightforward – Java with some custom libs + XML, easy-hackable in Eclipse. But I’m think I’ll take a step back from that, check out the tools that take HTML5 as input and spit out semi-native mobile apps.

      Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s