Giter Club home page Giter Club logo

node-sparqling-star's Introduction

NPM version Build Status Coverage Status Dependencies

SPARQLing Star

node.js client for creating SPARQL queries and communicating with services like DBpedia

Introduction

This package allows you to create SPARQL calls via JavaScript object notation and to query a SPARQL endpoint and fetch the results.

What it is about?

Using the query language SPARQL (Simple Protocol and RDF Query Language), this module provides facilities to retrieve machine-readable data stored in RDF. RDF stands for Resource Description Framework and is a way to store data in triplets, following the syntactical structure of subject, predicate and noun. In a very practical sense, it gives an access point to semantic web technologies and databases. One of the prime examples is the DBpedia, which is the machine-readable form of Wikipedia. DBpedia allows you to understand the Wikipedia as a big, structured database from which valuable information can be extracted.

Getting Started

To install the package, call

npm install sparqling-star

from your working directory. To require the package in a *.js file, use the usual syntax:

var sparqler = require( 'sparqling-star' );

If you are interested in using the package not only on the server, but also client-side, you could use Browserify to convert your code into one single *.js file which can easily be embedded in an HTML document.

Quick Overview

To give you an idea on how the package works, we are going through the steps of creating a call to DBpedia and printing the fetched results to the console. Our objective is to load all the names of the music albums from Eminem. After loading the package, we can construct a query object as follows:

var myquery = new sparqls.Query();

This is an empty query object, so we have to add some information. Our myquery object expects JavaScript objects to perform the search. First, we create an album object which specifies the properties that we wish to retrieve.

var album = {
	'type': 'dbo:Album',
	'dbo:artist' : 'dbr:Eminem'
};

In this object, the keys represent the RDF predicates and the right-hand side our search values.

Then we have to register the query object in our query:

myquery.registerVariable( 'album', album );

Behind the scenes, this creates a valid SPARQL call. To retrieve the code, you can access the sparqlQuery property of the myquery object as in myquery.sparqlQuery, which will print out

SELECT * WHERE {
	?album a dbo:Album .
	?album dbo:artist dbr:Eminem .
}
LIMIT 100

To test the created code, you can use a web frontend of a SPARQL endpoint of DBpedia such as Virtuoso.

To fetch results in JSON format inside of JavaScript, we have to create a client object that communicates with a SPARQL endpoint. The constructor function Client has an optional string parameter to specify the endpoint. If you do not pass an argument when calling the function, it will default to http://dbpedia.org/sparql. Since we are fine with this in our current application, we can simply type

var sparqler = new sparqls.Client();

With this object, you can send a multitude of queries, even at the same time. One simply has to pass a Query object to the send method and provide as a second argument a callback function which has two parameters: error and data.

sparqler.send( myquery, function( error, data ) {
	console.log( data.results.bindings );
});

By default, a created SPARQL call will fetch all variables that have been registered. DBpedia provides a nice visual interface which gives you a preview of all the information stored for a certain entity. The URL for the DBpedia entry for Eminems second album, the Marshall Mathers LP, is http://dbpedia.org/page/The_Marshall_Mathers_LP. For example, we could extend our album object to also include the music genre and the record label:

var extendedAlbum = {
	'type': 'dbo:Album',
	'dbo:artist': 'dbr:Eminem',
	'dbo:genre': '?genre',
	'dbo:recordLabel': '?recordLabel'
};

As you can see here, this query differs from the previous one insofar as it introduces open variables that we want to retrieve but upon which we do not impose any restrictions. We use a starting "?" for such local variables. Using a new query, we can retrieve the results as follows:

var myquery2 = new sparqls.Query();
myquery2.registerVariable( 'extendedAlbum', extendedAlbum );

sparqler.send( myquery2, function( error, data ) {
	console.log( util.inspect( data.results.bindings ) );
});

Further Options

Selection

By default, all registered and other variables are returned in the result set. This corresponds to a "SELECT *" statement in SPARQL. However, you might want to reduce the returned data by specifying the result set explicitly. This is achieved by using the selection method of the query object. Assume that we only want to view the record labels of Eminems albums. We can do this by typing

myquery2.selection( 'recordLabel' );

Besides passing a string, it is also possible to supply a multitude of variables arranged in a JavaScript Array. As you may notice, the returned result set contains multiple instances of the different record labels. To only retrieve these instances once, we can use a modifier.

Modifiers

Distinct

When creating a query, we can use the distinct option to filter out duplicate instances. All modifiers are set when creating the query object.

var myquery3 = new sparqls.Query({
    'distinct': true
});

Reduced

While the distinct modifier ensures that duplicates are eliminated from the result set, reduced just allows them to be eliminated.

var myquery3 = new sparqls.Query({
    'reduced': true
});

Limit

To limit the size of the result set, we use the limit modifier.

var myquery3 = new sparqls.Query({
    'limit': 5
});

By default, a maximum of a hundred entries is returned.

Offset

To skip say the first five results, you can define an offset:

var myquery3 = new sparqls.Query({
    'offset': 5
});

You can also combine these modifiers, e.g. as in

var myquery3 = new sparqls.Query({
	'offset': 5,
	'limit': 20,
	'reduced': true,
	'distinct': false
});

Order By

The query object might be ordered by passing a regular SPARQL command to its order method.

myquery3.order( 'ASC(?extendedAlbum)' );

Filters

To refine your query, you can use filters. These again accept valid SPARQL filter expressions. For example, we could only retain results in which the city contains "New".

myquery.filter( 'regex(?city, \'New\')' );

Prefixes

Prefixes can be created as follows:

myquery.registerPrefix( 'dbres', '<http://dbpedia.org/resource/>' );

This important if you wish to combine ontologies and query other endpoints besides DBpedia.

Custom Triples

To create custom triples in which the subject is not equal to a query variable, use the registerTriple function, which expects an object with three keys subject, predicate and objects, all of which have to be strings.

var customQuery = new sparqls.Query();
var triple = {
	'subject': '<http://dbpedia.org/resource/Civil_engineering>',
	'predicate': 'dct:subject',
	'object': '?abstract'
};
customQuery.registerTriple( triple );

Method Chaining

One of the neat features of the SPARQLing star package is that it allows method chaining, that is you can build up your query in one rush like this:

myquery
	.registerVariable( 'company', company )
	.registerVariable( 'city', city )
	.registerPrefix( 'dbres', '<http://dbpedia.org/resource/>' )
	.selection( [ 'company', 'num' ] )
	.order( 'ASC(?num)' );

This sample code is taken from the companies.js file. You can find all example code in the examples subdirectory of this repository.

License

MIT © Philipp Burckhardt, 2014-2016

node-sparqling-star's People

Contributors

planeshifter avatar xantomen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

phalanstere

node-sparqling-star's Issues

Retrieve all triples for a specific subject

Hi,

I'm trying to figure out how to retrieve the predicates and objects of a specific subject, similar to the SPARQL query defined in the Abstract.js file in the examples folder. However the actual code does not seem to match the SPARQL query defined at the top of the file?

More specifically, my question is how do I write the code for a query such as the following:

SELECT ?p ?o
WHERE {
{ <http://some.uri> ?p ?o }
}

Thanks

Unions

Does this library have the ability to do Unions. For example, if I want to produce this query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT ?s WHERE {
  {
    <http://dbpedia.org/resource/Stephen_Curry_(basketball)> dbo:wikiPageRedirects ?s .  
    ?s a owl:Thing .       
  }
UNION
  {
    ?altName dbo:wikiPageRedirects "Stephen_Curry_(basketball)"@en ;
             dbo:wikiPageRedirects ?s .
  }

}

Query question

How would I go about creating this query using node-sparqling-star?

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT ?currentteam WHERE { dbpedia:Philip_Rivers dbp:currentteam ?currentteam}

SPARQL results

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.