Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用shell中的简单命令行在RDF/XML中转换OWL/XML_Java_Shell_Rdf_Owl_Owl Api - Fatal编程技术网

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

Java 使用shell中的简单命令行在RDF/XML中转换OWL/XML,java,shell,rdf,owl,owl-api,Java,Shell,Rdf,Owl,Owl Api,我请求您帮助创建一个转换器,将OWL/XML转换为RDF/XML。我的目的是通过一个简单的shell命令与bash一起使用owlapi2。 我的文件是OWL/XML格式的,但我必须将它们转换为RDF/XML,才能将它们发送到fuseki数据库中。多亏了Protégé或在线可用的转换器,我可以转换每个文件,但我有1000多个文件要转换 查看我当前的java文件,但我不知道如何使用它: package owl2rdf; import java.io.File; import org.semanti

我请求您帮助创建一个转换器,将OWL/XML转换为RDF/XML。我的目的是通过一个简单的shell命令与bash一起使用owlapi2。 我的文件是OWL/XML格式的,但我必须将它们转换为RDF/XML,才能将它们发送到fuseki数据库中。多亏了Protégé或在线可用的转换器,我可以转换每个文件,但我有1000多个文件要转换

查看我当前的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 scrip 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/我的剧本


5-哈哈:所有OWL/XML都转换成RDF/XML。例如,您可以将它们导入fuseki或sesame数据库。

您可以通过输入接收路径吗?代码就像一个符咒,你只需要提供文件链接。我看不出有什么问题。你能详细说明一下吗?谢谢你的回复。由于bash中的realpath和java代码中的main函数,我将路径发送到jar:r=$realpath$I;java-jar owl2rdf.jar$r但问题是我在OWLapi的java类中找不到rdf ontolongy管理器,因此我有以下错误:类型OWLOntologyManager中的GetOntologyFormatToLonTology不适用于参数文件saveOntologyOWLOntology,OWLDocumentFormat,OWLOntologyManager类型中的IRI不适用于参数文件RDFXMLOntologyFormat,IRI使用本体管理器解决我的问题。这只是因为本体不是一个本体对象。但我现在有很多错误,除了我不太理解的例外。我编辑我的第一条消息来解释。代码在没有从命令行调用的情况下工作吗?我的意思是你能测试代码来转换一个本体并单独运行吗?我解决了很多问题!!我终于明白了如何使用maven存储库。但是我有一个找不到的类的问题:java.lang.NoClassDefFoundError:org/openrdf/rio/RDFFormat。但是问题是我已经安装了org.openrdf.rio包,它通常提供org.openrdf.rio.rdfformat类。它工作得非常好!!但将OWL/XML转换为RDF/XML非常缓慢,而且显然没有经过优化:500KO文件需要4秒钟才能转换,想象一下,当您的存储库中有1Go或更多文件时。我认为Xslt方法可能更有效,但需要花很多时间来设计。如果可以直接用RDF/XML编写本体文件,那么就这样做。但很明显,OWL/XML更易于使用Xslt进行操作,并且更易于人类阅读,无需到处使用复杂的前缀和名称空间。