使用shell中的简单命令行在RDF / XML中转换OWL / XML

时间:2015-05-18 06:14:49

标签: java shell rdf owl owl-api

我正在请求您帮助创建转换器以将OWL / XML转换为RDF / XML。我的目的是通过一个带有bash的简单shell命令来使用OWLapi 2。 我的文件是OWL / XML,但我必须将它们转换为RDF / XML,以便将它们发送到我的fuseki数据库中。感谢Protégé或在线提供的转换器,我可以转换每个文件,但我要转换的文件超过一千个。

查看我当前的java文件(但我不知道如何使用它):

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));
    }
}

当我执行此代码时,相对于我根本不理解的异常,我有很多错误,但我发现这是一个常见的错误:

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

1 个答案:

答案 0 :(得分:0)

将OWL / XML文件的整个存储库更改为RDF / XML文件:

1-创建你的软件包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-感谢Eclipse等Java-IDE,管理所有依赖项(repo Maven,下载jar,classplath等)

3-创建你的bash脚本my-scrip.sh;这里绝对没有优化

#!/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-执行你的脚本 $ chmod +x my-script.sh;./my-script

5-哈哈时刻:所有的OWL / XML都是用RDF / XML转换的。例如,您可以将它们导入fuseki或芝麻数据库。