Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A way to pre-process parameters and post-process results #34

Open
nandana opened this issue Oct 14, 2015 · 1 comment
Open

A way to pre-process parameters and post-process results #34

nandana opened this issue Oct 14, 2015 · 1 comment
Labels

Comments

@nandana
Copy link

nandana commented Oct 14, 2015

This is more of a new feature for the wishlist.

Say for example, if I have a simple query such as

select distinct ?city where {
?city a <http://dbpedia.org/ontology/City> ;
<http://dbpedia.org/ontology/country> ?_name_iri .
}

I would like to pass just the local name in the API and concatenate it with a prefix on the fly.

e.g. Italy -> <http://dbpedia.org/resource/Italy>

Similarly it would be nice if some post processing results is possible, for example

select distinct ?country where {
?country a <http://dbpedia.org/ontology/City> .
}

So in this case, to convert <http://dbpedia.org/resource/Italy> to "Italy".

I was guessing may this what the scripts are for but I couldn't find the information in the documentation.

And one last minor request, will it be possible to preserve the variable names when your generate the Swagger specification?

@enridaga
Copy link
Contributor

For what concern param value pre-processing I couldn't find a use case so far that cannot be covered by SPARQL itself.
For example:

select distinct ?city where {
   BIND(iri(concat("http://dbpedia.org/resource/", ?_name)) as ?country) .
   ?city a <http://dbpedia.org/ontology/City> ;
   <http://dbpedia.org/ontology/country> ?country .
}

There might be use cases not covered by SPARQL, and we could think to provide scripts for input preparation. However, I like the idea to keep BASIL as simple as possible, and I could not found such cases so far.

Post-processing can be done with Scripts. At the moment we support the Mustache language (that I recommend for returning HTML) and Javascript.

Documentation of this feature is not yet ready (mea culpa), I am putting few examples taken from the current catalog here, as a basis to start from:

MUSTACHE
This is the very simple example, creating a list in HTML:

<ul>
{{#items}}
   <li><a href="{{id}}">{{label}}</a> ({{nationality}})<br/><small>{{id}}</small></li>
{{/items}}
</ul>

See it in action here:
http://basil.kmi.open.ac.uk/basil/qhq3k9v61eu9/api.list?year=1966

Javascript:
This snippet shows how to use Javascript (implemented via Rhino):

function(){
   while(this.items.hasNext()){
      var i = this.items.next();
      var c = (i.nationality.lastIndexOf('#') != -1) ? '#' : '/' ;
      var n = i.nationality.substring(i.nationality.lastIndexOf(c) + 1);
      this.print(i.label + " was born in " + n + "\n");
   }
   return this;
}

See it in action here:
http://basil.kmi.open.ac.uk/basil/qhq3k9v61eu9/api.jsres?year=1966

The important bits are:

  • wrap the script in a single function
  • iterate over values using the property this.items (it wraps a Java Iterator), as in the example
  • use this.print to spit to the output stream
  • always return this

Result sets are streamed to the script, so if you follow this pattern you shouldn't get into troubles with memory consumption on large result sets.

About the last question, I am not sure I got what you mean, can you show an example?

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants