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

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

我请求你帮助创建一个转换器,将 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

要将 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- 感谢 Java-IDE,例如 Eclipse 或其他东西,管理所有依赖项(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 或 sesame 数据库。