SPARQL and OWL on the command line—of my phone!

Termux and rdflib on my Android phone.

I recently wondered “could I run a Python script that includes the rdflib library on my Samsung Android phone?” Five minutes later, I was doing it, and about three of those minutes were spent installing Python.

The termux terminal emulator lets you treat your Android phone as a regular linux machine. (I even have a Bluetooth keyboard for when I’m getting super geeky with termux on my phone by running Emacs or command line git.) From the termux command line, I installed Python and rdflib the same way I would on any machine:

pkg install python
pip install rdflib

I pasted the Python script in “A tiny example” from the rdflib getting started page into a text file and ran it with no problem five minutes after wondering if all this would work.

To point my phone’s Python scripts at the interesting place that pkg install put my Python executable, I did have to put this as my first line:

#!/data/data/com.termux/files/usr/bin/python

To try a SPARQL query, I took the first example on the rdflib documentation’s Querying with SPARQL page, substituted the URL of my own ancient FOAF file http://snee.com/bob/foaf.rdf as the parameter for the demo script’s g.parse() call, and the Python script ran fine with the expected output of the script’s SPARQL query.

Then I got ambitious and tried some OWL inferencing. After I did the pip install owlrl command shown at the top of the owlrl home page in termux, everything from my blog entry My command line OWL processor then worked fine. I took the script shown at the end of that blog entry and only had to make one change (not to run on my phone, but I’m guessing because the library has evolved a bit): I removed .decode() from the last line.

To review the goal of the “command line OWL processor” demo, it started with Turtle data about musicians, their instruments, and the states that they were from, such as:

d:m2 rdfs:label "Charlie Christian" ;
     dm:plays d:Guitar ;
     dm:stateOfBirth d:TX .

d:m4 rdfs:label "Kim Gordon" ;
     dm:plays d:Bass ;
     dm:stateOfBirth d:NY .

It also declared three OWL restriction classes:

  • dm:Guitarist as resources that have a d:Guitar value for their dm:plays property.

  • dm:Texan as resources that have a value of d:TX for their dm:stateOfBirth property.

  • dm:TexasGuitarPlayer as the intersection of the dm:Guitarist and dm:Texan classes.

The sample data did not identify any of the musicians as instances of any classes; finding this out required OWL inferencing, and the owlrl library made this possible. Below you can see the script being invoked:

Running a Python OWL script on Termux

In this excerpt from its output, you can see that the OWL inferencing happened, and that resource http://learningsparql.com/ns/m2 (Charlie Christian) is an instance of all three restriction classes:

Output of command from previous image

In Converting sqlite browser cookies to Turtle and querying them with SPARQL I wrote that most of your computing devices probably have some SQLite data on them, and I showed that converting this data to RDF is pretty easy. sqlite3 was as easy to install with termux as the other packages above; using it to reach the SQLite data on my phone was another matter. As you might guess from where pkg install put my Python executable, termux has its own storage section on my phone. From there I can access music files, downloads, and other files on my phone from the termux command line, but I couldn’t find any SQLite data in my phone’s termux storage area. Rooting my phone would give me access to more, so that’s something to consider.

Still, as the first two examples above show, a Python script with rdflib can retrieve data from the Internet and run SPARQL queries on that, so that provides some interesting possibilities. The most pleasant surprise for me about all this was just how easy it was to use this set of tools on my phone.


Comments? Reply to my tweet (or even better, my Mastodon message) announcing this blog entry.