Skip to content

Hello World (Python RDFAlchemy)

AlberTadrous edited this page Oct 5, 2018 · 6 revisions

Hello World Example in Python (rdfAlchemy)

In this example, we will go through the instantiation of an ontology model (classes and relationships) programmatically. We will rely on a set of annotated classes compatible with the RDFAlchemy library to generate a topology instance specific to a site. The generated topology will be serialized in an rdf file or other standard formats (jsonld, n3, etc).

Pre-requisites:

  1. OLGA-CLI already compiled: mvn clean install --projects OLGA-Core,OLGA-CLI -DskipTests=True

  2. Java IDE (eclipse or other)

  3. A Java Virtual Machine (version > 1.8) download JVM 1.8

  4. Maven with version > 3.5.0

  5. M2_HOME must be set as an environment variable name

  6. Protégé is an open source ontology editor provided by Stanford University. download Protégé.

  7. Python with (version == 2.7) installed, RDFAlchemy is still not python 3 compliant.

  8. Pip installed

  9. pip install twine

  10. pip install wheel

  11. pip install --upgrade setuptools setuptools

  12. Download RDFAlchemy.tar.gz and pip install RDFAlchemy.tar.gz.

    Or download the source code from RDFAlchemy github and install it python setup.py install

RDFAchemy is a Python based serializer, it is an extension for RDFAlchemy. It is very easy to use. It relies on code decoration.

1. Generate a Library from the Demo Ontology

First, retrieve the ExampleDemoOntology.owl and generate a library to a given ${Path Of Your Choice} or "." for the current directory:

java -jar OLGA-Cli/target/OLGA-Cli-0.0.4-with-dependencies.jar --code python --library rdfalchemy --name DemoExample --path docs/example/ExampleDemoOntology.owl --out .

The following sections will go through the steps need to instantiate an ontology, and serialize it in a file. The source code for these steps can be found here.

2. Install the generated library from the Demo Ontology

Install the DemoExample library (.whl) which is available in the generated **dist **folder.

pip install ${yourGenerationFolder}\DemoExample-0.0.1a1-py2.py3-none-any.whl

3. Instantiate the Ontology Model

The generated library demoexample-XXX.whl contains already the generated interfaces, classes with their annotations. Therefore, the instantiation can take place:

import datetime
import time

from Simple.Rdf.Model.Floor import Floor
from Simple.Rdf.Model.Humidity import Humidity
from Simple.Rdf.Model.HumidityUnit import HumidityUnit
from Simple.Rdf.Model.Sensor import Sensor
from Simple.Rdf.Model.Temperature import Temperature
from simple.Simple.Rdf.Model.Building import Building
from simple.Simple.Rdf.Model.TemperatureUnit import TemperatureUnit
from rdflib import Namespace, Graph, URIRef
from rdfalchemy.rdfSubject import rdfSubject


if __name__ == '__main__':
    g = rdfSubject.db = Graph()
    ns = Namespace("http://www.example.com/ex/")
    ts = time.time()

    floor1 = Floor(URIRef(ns + "f1"))
    floor1.setName = "floor 1"

    floor2 = Floor(URIRef(ns + "f2"))
    floor2.setName = "floor 2"

    building1 = Building(URIRef(ns + "b1"))
    building1.setDescription = "North face Building"
    building1.setName = "b1"
    building1.addFloors(floor1)
    building1.addFloors(floor2)

    temp1 = Temperature(URIRef(ns + "t1"))
    temp1.setDescription = "this is indoor temperature"

    celsius = TemperatureUnit(URIRef(ns + "Celsius"))

    temp1.addUnitOfMeasure(celsius)
    temp1.setName = "temp1"
    temp1.setTimeStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    temp1.setValue = 32

    temperatureSensor = Sensor(URIRef(ns + "s1"))
    temperatureSensor.setDescription = "This is sensor s1"
    temperatureSensor.setName = "TempSensor1"
    temperatureSensor.addMeasures(temp1)
    temperatureSensor.addPhysicalLocation(floor1)

    humidity1 = Humidity(URIRef(ns + "hum1"))
    humidity1.setDescription = "this is indoor humidity"
    relativeHumidity = HumidityUnit(URIRef(ns, "relativeHumidity"))
    humidity1.addUnitOfMeasure(relativeHumidity)
    humidity1.setName = "h1"
    humidity1.setTimeStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    humidity1.setValue = 64

    humiditySensor = Sensor(URIRef(ns, "hum1"))
    humiditySensor.setDescription = "This is humidity sensor 1"
    humiditySensor.setName = "hum1"
    humiditySensor.addMeasures(humidity1)
    humiditySensor.addPhysicalLocation(floor2)

4. Serialize

The output of the topology (ontology instances) can be serialized and saved in a jsonld file. The following, code snippet, serializes the content in an rdf file which can be found here.

 ## this 'g' is the rdfSubject.db which be created in the previous code snippet 
 g.serialize(destination='./output.ttl', format='turtle')