Hands On Sessions

From Linked Building Data Community Group
Jump to: navigation, search

Convert OWL to JSON-LD

Short description: This tutorial will try to link all the concepts presented in the previous two tutorials. More specifically, the process of transforming ontologies (presented in the second tutorial) including building and construction metadata to JSON-LD format (described in the first tutorial).

Tutorial description: In this tutorial you will learn how building information and metadata represented in .owl format can be transformed into JSON-LD format and vice versa.

Resources:

owlapi-jsonld: https://github.com/stain/owlapi-jsonld

IFC4_ADD1.owl: http://linkedbuildingdata.net/resources/IFC4_ADD1.owl

RDF Translator: http://rdf-translator.appspot.com/

Reading and Writing RDF in Apache Jena: https://jena.apache.org/documentation/io/

Prerequisites:

Go to https://www.w3.org/community/lbd/2014/12/12/ifcowl-ontology-file-added-for-ifc4_add1/ and download http://linkedbuildingdata.net/resources/IFC4_ADD1.owl

Download and install Protege: http://protege.stanford.edu/

Download and install JDK & Netbeans IDE:

Download and install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html (version Java SE Development Kit 7u80 is suggested)

Download and install Netbeans IDE: https://netbeans.org/downloads/

OR

Donwload and install the distribution of the JDK including the Java SE bundle of NetBeans IDE: http://www.oracle.com/technetwork/java/javase/downloads/jdk-7-netbeans-download-432126.html

Download java dependencies:

Donwload OWL API v.3.5.0: http://sourceforge.net/projects/owlapi/files/OWL%20API%20%28for%20OWL%202.0%29/3.5.0/owlapi-distribution-3.5.0.jar/download

Download JSON-LD Java v.0.4.1: http://central.maven.org/maven2/com/github/jsonld-java/jsonld-java/0.4.1/jsonld-java-0.4.1.jar

Donwload jackson-core v2.3.3: http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.3.3/jackson-core-2.3.3.jar

Download jackson-databind v2.3.3: http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.3.3/jackson-databind-2.3.3.jar

Download jackson-annotations v.2.3.3: http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.3.3/jackson-annotations-2.3.3.jar

Download Apache HttpClient v4.2.5: http://central.maven.org/maven2/org/apache/httpcomponents/httpclient/4.2.5/httpclient-4.2.5.jar

Download Apache HttpClient-cache v4.2.5: http://central.maven.org/maven2/org/apache/httpcomponents/httpclient-cache/4.2.5/httpclient-cache-4.2.5.jar

Download slf4j api module v1.7.7: http://central.maven.org/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar

Download Apache http-core v4.2.4: http://central.maven.org/maven2/org/apache/httpcomponents/httpcore/4.2.4/httpcore-4.2.4.jar

Download “JSON-LD parser and writer for OWL API” (Go to https://github.com/stain/owlapi-jsonld and select “Download ZIP”). Then, extract the contents of the .zip file in a directory of your choice of your hard drive.

Step 1: Browse the ontology (IFC4_ADD1.owl) using Protege [Menu] File -> Open… -> Select the filepath to “IFC4_ADD1.owl” “Entities” tab -> Class hierarchy -> Double click to “Thing” to see the classes of the ontology

Step 2: Build “JSON-LD parser and writer for OWL API” In NetBeans: [Menu] File -> File -> Open Project… -> Select the file path to “JSON-LD parser and writer for OWL API” where you extracted the corresponding .zip file [Menu] Run -> Clean and Build Project this downloads all the dependencies defined in the pom.xml file and builds the project finally, “owlapi-jsonld-0.2.0-SNAPSHOT.jar” is generated. We will use this as a dependency in the project that we will create during our next step.

Step 3: Create a new Java project to convert .owl to JSON-LD In NetBeans: File -> New Project... -> Java -> Java Application New Java Application -> provide “OwlToJsonLDConverter” as Project Name

     -     -> provide a Project Location
     -     -> click Finish

Create a JFrame container In the Projects window, right-click the OwlToJsonLDConverter node and choose New > Other. In the New File dialog box, choose the Swing GUI Forms category and the JFrame Form file type. Click Next. Enter "myConverter" in class name. Select the only package appearing in the combobox Click Finish

Add project dependencies In the Projects window on the left, right click on the project -> Properties -> Libraries -> Compile tab

   -> Add JAR/Folder -> Select "owlapi-distribution-3.5.0.jar"
   -> Add JAR/Folder -> Select "jsonld-java-0.4.1.jar"
   -> Add JAR/Folder -> Select "jackson-core-2.3.3.jar"
   -> Add JAR/Folder -> Select "jackson-databind-2.3.3.jar"
   -> Add JAR/Folder -> Select "jackson-annotations-2.3.3.jar"
   -> Add JAR/Folder -> Select "httpclient-4.2.5.jar"
   -> Add JAR/Folder -> Select "httpcore-4.2.4.jar"
   -> Add JAR/Folder -> Select "httpclient-cache-4.2.5.jar"
   -> Add JAR/Folder -> Select "slf4j-api-1.7.7.jar"
   -> Add JAR/Folder -> Select "owlapi-jsonld-0.2.0-SNAPSHOT.jar" -> OK

Select the main class In Projects window on the left - Right click on the project -> Properties -> Run -> Main Class -> write "owltojsonldconverter_test.myConverter" -> OK

- After class declaration we add the following line

       JFileChooser fc;
   String owlFilepath;

- click on the error sign on the left and click "Add import for javax.swing.JFileChooser" - In class constructor ("public myConverter()") after "initComponents();" we add the following:

       "fc = new JFileChooser();"

Add a button for selecting the .owl file to be converted to JSON-LD - drag & drop a Button - right click on the button -> Edit text -> Write "Select .owl file" - on the right, at the Properties window -> Code tab -> Variable Name -> Write "selectOwlButton" - Double click on the button, in order to write the action of the button and write the following:

       int returnVal = fc.showOpenDialog(myConverter.this);
   if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fc.getSelectedFile();
       System.out.println("Opening: " + file.getAbsolutePath());
       owlFilepath = file.getAbsolutePath();
   } 
   else 
   {
       System.out.println("Open command cancelled by user.");
   }

- click on the error sign on the left and click "Add import java.io.File"

Add a button for selecting the filepath of the generated JSON-LD file - drag & drop a Button - right click on the button -> Edit text -> Write "Transform .owl to JSON-LD" - on the right, at the Properties window -> Code tab -> Variable Name -> Write "transformButton" - Double click on the button, in order to write the action of the button and write the following:

       int returnVal = fc.showOpenDialog(myConverter.this);
   if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fc.getSelectedFile();
       System.out.println("Opening: " + file.getAbsolutePath());
       System.out.println("Please wait until conversion is completed. This is a time-consuming process. A pop-up window will be shown when conversion is completed.");
       OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
       JsonLdStorer.register(ontologyManager);    
       try
       {
           OWLOntology ontology = ontologyManager.loadOntologyFromOntologyDocument(new File(owlFilepath));
           ontologyManager.saveOntology(ontology, new JsonLdOntologyFormat(), IRI.create(file.toURI()));
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
       JOptionPane.showMessageDialog(this, "Conversion completed!");
   } 
   else 
   {
       System.out.println("Open command cancelled by user.");
   }
   

- click on the error sign on the left and click "Add import for org.semanticweb.owlapi.apibinding.OWLManager" - click on the error sign on the left and click "Add import for org.semanticweb.owlapi.model.OWLOntologyManager" - click on the error sign on the left and click "Add import for org.semanticweb.owlapi.model.OWLOntology" - click on the error sign on the left and click "Add import for org.semanticweb.owlapi.model.IRI" - click on the error sign on the left and click "Add import for javax.swing.JOptionPane"

[Menu] Run -> Clean and Build Project [Menu] Run -> Run Main Project Click on “Select .owl file” Select “IFC4_ADD1.owl” and click “Open” Click on “Transform .owl to JSON-LD” Put a filename for the generated file (e.g. generatedJSONFile.json) and click “Open” The following message is printed in the console: “Please wait until conversion is completed. This is a time-consuming process. A pop-up window will be shown when conversion is completed.”


Step 4: Browse the contents of the generated JSON-LD file