Search code examples
javashellrdfowlowl-api

Convert OWL/XML in RDF/XML with a simple command line in shell


I'm asking your help to create a converter to transform OWL/XML into RDF/XML. My purpose is to use OWLapi 2 through a simple shell command with bash. My files are in OWL/XML but I have to transform them into RDF/XML to send them in my fuseki database. I could transform each file thanks to Protégé or a converter available online, but I've more than one thousand files to convert.

See my current java file (but I don't know how to use it) :

package owl2rdf;

import java.io.File;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

@SuppressWarnings("deprecation")
public class owl2rdf {

public static void main(String[] args) throws Exception {

    // Get hold of an ontology manager
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

    // Load the ontology from a local files
    File file = new File(args[0]);
    System.out.println("Loaded ontology: " + file);
    OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);

    // Get the ontology format ; in our case it's normally OWL/XML
    OWLOntologyFormat format = manager.OWLOntologyFormat(file);
    System.out.println("    format: " + format);

    // save the file into RDF/XML format
    RDFXMLOntologyFormat rdfxmlFormat = new RDFXMLOntologyFormat();
    manager.saveOntology(ontology, rdfxmlFormat, IRI.create(file));
    }
}

When I execute this code, I've many errors relative to exceptions I don't understand at all, but I saw it's a common error:

Exception in thread "main" java.lang.reflect.InvocationTargetException
Caused by: java.lang.NoClassDefFoundError: com/google/inject/Provider
Caused by: java.lang.ClassNotFoundException: com.google.inject.Provider

Solution

  • To change a entire repository of an OWL/XML file into RDF/XML file:

    1- create your package owl2rdf.java

    package owl2rdf;
    
    //import all necessary classes
    import java.io.File;
    import org.semanticweb.owlapi.apibinding.OWLManager;
    import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
    import org.semanticweb.owlapi.model.IRI;
    import org.semanticweb.owlapi.model.OWLOntology;
    import org.semanticweb.owlapi.model.OWLOntologyManager;
    
    @SuppressWarnings("deprecation")
    public class owl2rdf {
    
    #create a main() function to take an argument; here in the example one argument only
        public static void main(String[] args) throws Exception {
    
            // Get hold of an ontology manager
            OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    
            // Load the ontology from a local files
            File file = new File(args[0]);
            System.out.println("Loaded ontology: " + file);
            OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
    
            // save the file into RDF/XML format
            //in this case, my ontology file and format already manage prefix
            RDFXMLOntologyFormat rdfxmlFormat = new RDFXMLOntologyFormat();
            manager.saveOntology(ontology, rdfxmlFormat, IRI.create(file));
        }
    }
    

    2- Thanks to a Java-IDE such as Eclipse or something else, manages all dependencies (repo Maven, downloads jar, classplath, etc.)

    3- create your bash scrip my-scrip.sh; here absolutely not optimized

    #!/bin/bash
    cd your-dir
    for i in *
    do
    #get the absolute path; be careful, realpath comes with the latest coreutils package
        r=$(realpath "$i")
        #to be not disturb by relative path with java -jar, I put the .jar in the parent directory
        cd ..
        java -jar owl2rdf.jar "$r"
        cd your-dir
    done
    echo "Conversion finished, see below if there are errors."
    

    4- Execute your script $ chmod +x my-script.sh;./my-script

    5- haha moment: all your OWL/XML are converted in RDF/XML. You can for example, import them into fuseki or sesame database.