Making Queries
There's no point to making ontologies if you don't implement them to grab data from them. In this case we make use of queries to grab information that can be of use for us.
W3C made a recommended language to make consults in our ontologies called SPARQL. This language is very similar to SQL syntax on relational databases and can be used to grab data. In this case we're going to make a consult using two very famous tools, Jena for the Java platform and RDFLib for the Python language.
W3C made a recommended language to make consults in our ontologies called SPARQL. This language is very similar to SQL syntax on relational databases and can be used to grab data. In this case we're going to make a consult using two very famous tools, Jena for the Java platform and RDFLib for the Python language.
Java + Jena + SPARQL
SPARQL (called sparkle) is an ontology processing framework developed for the Java language that allows the making of queries from the ontology. In this tutorial I'll show you how to make consults to the ontology using SPARQL.
Printing the Ontology using Jena.
This code allows to print the ontology contents using Java and the Jena API. In order to use it you must link the Jena libraries to the Eclipse IDE (or your chosen IDE) and call the ontology file (in RDF format) through the code.
|
SPARQL Consults
anime.java | |
File Size: | 1 kb |
File Type: | java |
SPARQL in RDFLib
RDFLib also provides with a SPARQL binder to make consults in our ontology using the Python programming language. Personally this is my favorite way to work given the advantages Python provides in ease of prototyping and which makes it ideal for web applications.
Of course if real time and resource allocation are vital (such as embedded systems) then other option like C, Assembly or even VHDL/Verilog may be preferable.
To begin we create a new file with .py extension to indicate our interpreter that we want to create a Python application. In this case I called my application Taki.py based on my otaku knowledge. Save in on the same folder as your ontology.
First of all we must import the libraries we're going to use. In this case we'll make use of RDFLib and as such must add the following lines in the header of our application.
from rdflib import Graph, URIRef, Namespace, OWL
import pprint
Next we must create a Graph object that will hold the values of our ontology in memory. In this case its called g.
g = Graph()
After that we must define a namespace which is the NAME we gave our ontology and add a # at the end. If you don't know what is your namespace you can open your ontology file and check it. Its in the header at the end in a field called owl:Ontology. In this case its:
<owl:Ontology rdf:about="http://www.semanticweb.org/arthur/ontologies/2016/4/anime"/>
We add the namespace to a variable:
n = Namespace("http://www.semanticweb.org/arthur/ontologies/2016/4/anime#")
And load the ontology with the command:
g.load("/path/to/your/ontology.rdf")
Now we can finally make our query. We shall use the SELECT method which works in a very similar way to SQL's SELECT. Syntax:
SELECT <output_variables> WHERE { <CONDITIONS> }
In this case we want to grab the name of the animes whose genre is drama which we know is shared between the 3 animes we stored in the ontology. We also know that our desired output is the field name, which we specify adding the ? sign before the variable name. It will be shown as:
qres = g.query("SELECT ?Series ?name WHERE { ?Series n:isGenre n:Drama . ?Series n:name ?name }", initNs={"n":n})
From the acquired values we will iterate printing each of the names using an iterator and finally close the graph as shown in the figure.
Of course if real time and resource allocation are vital (such as embedded systems) then other option like C, Assembly or even VHDL/Verilog may be preferable.
To begin we create a new file with .py extension to indicate our interpreter that we want to create a Python application. In this case I called my application Taki.py based on my otaku knowledge. Save in on the same folder as your ontology.
First of all we must import the libraries we're going to use. In this case we'll make use of RDFLib and as such must add the following lines in the header of our application.
from rdflib import Graph, URIRef, Namespace, OWL
import pprint
Next we must create a Graph object that will hold the values of our ontology in memory. In this case its called g.
g = Graph()
After that we must define a namespace which is the NAME we gave our ontology and add a # at the end. If you don't know what is your namespace you can open your ontology file and check it. Its in the header at the end in a field called owl:Ontology. In this case its:
<owl:Ontology rdf:about="http://www.semanticweb.org/arthur/ontologies/2016/4/anime"/>
We add the namespace to a variable:
n = Namespace("http://www.semanticweb.org/arthur/ontologies/2016/4/anime#")
And load the ontology with the command:
g.load("/path/to/your/ontology.rdf")
Now we can finally make our query. We shall use the SELECT method which works in a very similar way to SQL's SELECT. Syntax:
SELECT <output_variables> WHERE { <CONDITIONS> }
In this case we want to grab the name of the animes whose genre is drama which we know is shared between the 3 animes we stored in the ontology. We also know that our desired output is the field name, which we specify adding the ? sign before the variable name. It will be shown as:
qres = g.query("SELECT ?Series ?name WHERE { ?Series n:isGenre n:Drama . ?Series n:name ?name }", initNs={"n":n})
From the acquired values we will iterate printing each of the names using an iterator and finally close the graph as shown in the figure.
Executing our application we see that it successfully retrieves the three series that have Drama as their Genre or rather, in semantic programming vocabulary, those instances of the Series class whose Object property links to the Drama class.
taki.py | |
File Size: | 0 kb |
File Type: | py |
References
[1] https://rdflib.github.io/sparqlwrapper/
[2] https://jena.apache.org/getting_started/
[3] https://jena.apache.org/tutorials/
[4] https://www.youtube.com/watch?v=nUdHneViLp4&list=PLr_TipxhoDryHmYOZWH9LrZmB2G0DJ2IU&index=4
[2] https://jena.apache.org/getting_started/
[3] https://jena.apache.org/tutorials/
[4] https://www.youtube.com/watch?v=nUdHneViLp4&list=PLr_TipxhoDryHmYOZWH9LrZmB2G0DJ2IU&index=4