An AngularJS module to formulate SPARQL 1.1 queries in a more JavaScript'ish way. It's currently integrated with the RESTful Webservice of BrightstarDB. Has functions to store and retreive simple JavaScript objects in the RDF database.
Note: The language binding is in no ways complete. It covers only basic triple based operations like:
- select, where, order by
- select distinct
- where with optional triples
- prefixes and resources
- strings and numbers as literals
To store and retrieve JS objects:
- only flat property/literal-or-resource-value like JS objects
- with a rdf:type
- array properties are stored and retrieved as sets (no order, no duplicates!)
Please remember: RDF is all about resources and their respective links, so you have to make them explicit with store and retrieve. If you need to store complex object graphs without thinking, consider using JSON.stringify.
My work, i.e. the language binding and the sample app are provided under the MIT license. They are free to use for both commercial and non-commercial purposes. The Markdown parser and the BrightstarDB Server have their own licensing. At the time of creation of this repository, i.e. mid 2015 they were both open source.
Documentation is included only as comments in the file, but see also the sample application for an example.
This is a sample HTML5 application that uses the library.
- create articles
- add some metadata like abstract and title
- tag them
- markdown as body language
- save them in a BrightstarDB RDF store
- list and filter them by tags or metadata content
To run the application locally you need to do some things first:
-
Download and install BrightstarDB version at least 1.11
-
Create a store named "Article"
-
Store ArticleApp.htm, rdf.angular.js and marked.min.js in the same folder.
-
Fire off ArticleApp.htm in your preferred html5 capable browser
-
Hit "Metadata", "Edit Content", fill out the fields and hit "Save"
This library is used by the sample application to transform markdown source code to proper html.
A brief description of the three provided Angular services:
- sparql
- sparql$http
- brightstardb
Main service to construct SPARQL query functions.
Create promises to
- Query a b* store
- Store a flat JS object to the RDF store
- retrieve a flat JS object from the RDF store
A configuration point to build URLs to access BrightstarDB's Query and Update endpoints for a particular store.
Code fragments showing the use of the library
brightstardb.config.server = "bs.somedomain.com";
brightstardb.config.store = "Article";
// a prefix for Object IDs
var idPrefix = sparql.prefix("o", "http://www.brightstardb.com/example/article/");
// a prefix for their properties
var propertyPrefix = sparql.prefix("", "http://www.brightstardb.com/example/article#",
["title", "abstr", "tags"])
var a = sparql.a; // standard abbreviation for rdf:type
State prefixes, variables and triples to form a query function. Retrieve a promise to an array of the results.
Unbounds can and must be provided later when you post the query to the REST service. The query function (here: articlelistQY) can be stored for later reuse. Triples in the .where() function are stated as arrays with three elements. Optional triples are enclosed with the sparql.optional() function (not shown here).
var unboundTag = sparql.unbound("TAG");
var articlelist = sparql.vars("res", "title", "abstr"); // define the variables
var articlelistQY = sparql(propertyPrefix).select(sparql.distinct, articlelist).where(
[articlelist.res, a, idPrefix._asResource], // type triple
[articlelist.res, propertyPrefix.title, articlelist.title], // standard s p o triple
[articlelist.res, propertyPrefix.abstr, articlelist.abstr], // standard s p o triple
[articlelist.res, propertyPrefix.tags, unboundTag] // the o position is bound later
).orderBy(articlelist.res.desc); // order by res descending ;-)
$scope.tagfilter is either a sparql variable or a sparql.literal() here.
sparql$http(articlelistQY({TAG: $scope.tagfilter})).then(
function(data){ $scope.L = data; },
function(reason){ $scope.L = null; stderr(reason);});
Store a flat JS object under an ID and with a type in the RDF store. IDs and the type are of course RDF resources. The JS object property names are combined with the propertyPrefix to form proper predicate resources.
You need an RDF prefix for the properties to be stored and an RDF type resource for the object, which is the second parameter here. The resulting update function can be reused.
var update = sparql.update(propertyPrefix, idPrefix._asResource);
The object to store is $scope.A and it is stored under the ID $scope.A.$ID witch is a sparql.resource() (not shown here).
sparql$http(update($scope.A.$ID, $scope.A)).then(
function(rdf) { /* OK */ },
function(reason) { stderr(reason); }
The reverse operation of Store
The retrieve function can be reused. Prefix and type parameters as in Store.
var retrieve = sparql.retrieve(propertyPrefix, idPrefix._asResource);
res is the ID of the article as a string so it has to be converted to an RDF resource first.
var articleResource = sparql.resource(res);
sparql$http(retrieve(articleResource)).then(
function(art) { $scope.A = art; },
function (reason) { $scope.A = null; stderr(reason); }
);