-
Notifications
You must be signed in to change notification settings - Fork 18
Hello World (Java RDF4J)
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 rdf4j 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).
- OLGA-CLI already compiled:
mvn clean install --projects OLGA-Core,OLGA-CLI -DskipTests=True
- Java IDE (eclipse or other)
- A Java Virtual Machine (version > 1.8) download JVM 1.8
- Maven with version > 3.5.0
- M2_HOME must be set as an environment variable name
- Protégé is an open source ontology editor provided by Stanford University. download Protégé.
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 java --library rdf4j --name DemoExample --path docs/example/ExampleDemoOntology.owl --out .
In case of errors, please check the file path format (\ or /) depending on your operating system: Windows, MacOS, Linux.
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.
- Create an empty Maven project in your Java IDE
- One created, open the pom.xml and add the dependency of the demoExample as follows:
<dependencies>
<dependency>
<groupId>OLGA-RDF4J</groupId>
<artifactId>demoexample-RDF4J</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies>
Once added, run mvn clean
, your project will add the dependency
The generated library demoexample-RDF4J-XXX.jar
contains already the generated interfaces, classes with their annotations. Therefore, the instantiation can take place:
public static void createTopology()
{
String ns = "http://www.example.com#";
//Create a floor 1
IFloor floor1 = new Floor(ns, "f1");
floor1.setName("floor 1");
//Create a floor 2
Floor floor2 = new Floor(ns, "f2");
floor2.setName("floor 2");
//Create a Building 1
Building building1 = new Building(ns, "b1");
building1.setDescription("North face Building");
building1.setName("b1");
building1.addFloors(floor1); //add floor 1
building1.addFloors(floor2);
//Create a Temperature measurement
ITemperature temp1 = new Temperature(ns, "t1");
temp1.setDescription("this is indoor temperature");
ITemperatureUnit celsius = new TemperatureUnit(ns, "Celsius");
temp1.addUnitOfMeasure(celsius);
temp1.setName("temp1");
temp1.setTimeStamp((new Date()));
temp1.setValue(32);
//Create a Temperature Sensor
ISensor temperatureSensor = new Sensor(ns, "s1");
temperatureSensor.setDescription("This is sensor s1");
temperatureSensor.setName("TempSensor1");
temperatureSensor.addMeasures(temp1);
temperatureSensor.addPhysicalLocation(floor1);
//Create a Humidity measurement
IHumidity humidity1 = new Humidity(ns, "h1");
humidity1.setDescription("this is indoor humidity");
IUnitOfMeasure relativeHumidity = new HumidityUnit(ns, "relativeHumidity");
humidity1.addUnitOfMeasure(relativeHumidity);
humidity1.setName("h1");
humidity1.setTimeStamp((new Date()));
humidity1.setValue(64);
//Create a Humidity Sensor
ISensor humiditySensor = new Sensor(ns, "hum1");
humiditySensor.setDescription("This is humidity sensor 1");
humiditySensor.setName("hum1");
humiditySensor.addMeasures(humidity1);
humiditySensor.addPhysicalLocation(floor2);
}
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.
public static void serialize()
{
Rio.write(GLOBAL.model, System.out, RDFFormat.TURTLE);
}
In the C# code generation example LINQ Queries, some high level queries based on LINQ were used to retrieve instantiated objects. However, the RDF4J library is low level oriented where statements and triples are used. Therefore, OLGA generates high level methods to retrieve objects such as:
public static void printAllFloors()
{
Set<IFloor> floors = Floor.getAllFloorsObjectsCreated();
for(IFloor floor : floors)
{
System.out.println(floor.getName());
}
}