4

我有一些来自 RabbitMQ 的数据。数据被格式化为三元组,因此来自队列的消息可能如下所示:

:Tom foaf:knows :Anna

哪里:是我想要导入数据的本体的标准命名空间,但导入的其他前缀也是可能的。三元组由主语、属性/谓词和宾语组成,我在每条消息中都知道哪个是哪个。

在接收端,我有一个 Java 程序,其中包含一个OWLOntology表示本体的对象,新到达的三元组应在其中临时存储以进行推理和其他内容。我有点设法将三元组放入耶拿OntModel,但这就是结束的地方。我尝试使用OWLRDFConsumer,但找不到有关如何应用它的任何信息。

我的函数看起来像这样:

public void addTriple(RDFTriple triple) {

    //OntModel model = ModelFactory.createOntologyModel();

    String subject = triple.getSubject().toString();
    subject = subject.substring(1,subject.length()-1);
    Resource s = ResourceFactory.createResource(subject);

    String predicate = triple.getPredicate().toString();
    predicate = predicate.substring(1,predicate.length()-1);
    Property p = ResourceFactory.createProperty(predicate);

    String object = triple.getObject().toString();
    object = object.substring(1,object.length()-1);
    RDFNode o = ResourceFactory.createResource(object);

    Statement statement = ResourceFactory.createStatement(s, p, o);
    //model.add(statement);

    System.out.println(statement.toString());
}

我进行了子字符串操作,因为 RDFTriple 类在三元组的参数周围添加了 <>,因此 Statement 的构造函数失败。

如果有人能指出我的例子,那就太好了。也许有一个更好的方法,我没有想过实现同样的事情?

4

2 回答 2

3

似乎 OWLRDFConsumer 通常用于将 RDF 解析器与支持 OWL 的处理器连接起来。但是,正如我在评论中指出的那样,以下代码似乎可以工作,有几个地方我需要一个参数并放入我能做的唯一可用的东西。

以下代码: 创建一个本体;宣布两个有名字的人,汤姆和安娜;声明一个对象属性,喜欢;并声明一个数据属性age。一旦声明了这些,我们打印本体只是为了确保它是我们所期望的。然后它创建一个 OWLRDFConsumer。消费者构造函数需要一个本体、一个AnonymousNodeChecker和一个OWLOntologyLoaderConfiguration。对于配置,我只是使用了一个由无参数构造函数创建的,我认为没关系。对于节点检查器,唯一方便的实现者是 TurtleParser,所以我创建了其中一个,null作为 Reader 传递。我认为这没问题,因为不会调用解析器来读取任何内容。然后消费者的句柄(IRI,IRI,IRI)handle(IRI,IRI,OWLLiteral)方法用于一次处理一个三元组。我们添加三元组

:Tom :likes :Anna
:Tom :age 35

然后再次打印出本体以确保添加了断言。由于您已经获得了 RDFTriples,您应该能够提取 handle() 需要的参数。在处理三元组之前,本体包含:

<NamedIndividual rdf:about="http://example.org/Tom"/>

然后是:

<NamedIndividual rdf:about="http://example.org/Tom">
  <example:age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">35</example:age>
  <example:likes rdf:resource="http://example.org/Anna"/>
</NamedIndividual>

这是代码:

import java.io.Reader;

import org.coode.owlapi.rdfxml.parser.OWLRDFConsumer;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

import uk.ac.manchester.cs.owl.owlapi.turtle.parser.TurtleParser;


public class ExampleOWLRDFConsumer {
    public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
        // Create an ontology.
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLDataFactory factory = manager.getOWLDataFactory();
        OWLOntology ontology = manager.createOntology();

        // Create some named individuals and an object property.
        String ns = "http://example.org/";
        OWLNamedIndividual tom = factory.getOWLNamedIndividual( IRI.create( ns+"Tom" ));
        OWLObjectProperty likes = factory.getOWLObjectProperty( IRI.create( ns+"likes" ));
        OWLDataProperty age = factory.getOWLDataProperty( IRI.create( ns+"age" ));
        OWLNamedIndividual anna = factory.getOWLNamedIndividual( IRI.create( ns+"Anna" ));

        // Add the declarations axioms to the ontology so that the triples involving
        // these are understood (otherwise the triples will be ignored).
        for ( OWLEntity entity : new OWLEntity[] { tom, likes, age, anna } ) {
            manager.addAxiom( ontology, factory.getOWLDeclarationAxiom( entity ));
        }

        // Print the the ontology to see that the entities are declared. 
        // The important result is
        //  <NamedIndividual rdf:about="http://example.org/Tom"/>
        // with no properties
        manager.saveOntology( ontology, System.out );

        // Create an OWLRDFConsumer for the ontology.  TurtleParser implements AnonymousNodeChecker, so 
        // it was a candidate for use here (but I make no guarantees about whether it's appropriate to 
        // do this).  Since it won't be reading anything, we pass it a null InputStream, and this doesn't
        // *seem* to cause any problem.  Hopefully the default OWLOntologyLoaderConfiguration is OK, too.
        OWLRDFConsumer consumer = new OWLRDFConsumer( ontology, new TurtleParser((Reader) null), new OWLOntologyLoaderConfiguration() );

        // The consumer handles (IRI,IRI,IRI) and (IRI,IRI,OWLLiteral) triples.
        consumer.handle( tom.getIRI(), likes.getIRI(), anna.getIRI() );
        consumer.handle( tom.getIRI(), age.getIRI(), factory.getOWLLiteral( 35 ));

        // Print the ontology to see the new object and data property assertions.  The import contents is
        // still Tom: 
        //   <NamedIndividual rdf:about="http://example.org/Tom">
        //     <example:age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">35</example:age>
        //     <example:likes rdf:resource="http://example.org/Anna"/>
        //  </NamedIndividual>
        manager.saveOntology( ontology, System.out );
    }
}
于 2013-07-04T14:59:53.000 回答
0

在 ONT-API 中,它是 OWL-API 的基于 Jena 的扩展实现,它非常简单:

    OWLOntologyManager manager = OntManagers.createONT();
    OWLOntology ontology = manager.createOntology(IRI.create("http://example.com#test"));
    ((Ontology)ontology).asGraphModel().createResource("http://example.com#clazz1").addProperty(RDF.type, OWL.Class);
    ontology.axioms(AxiomType.DECLARATION).forEach(System.out::println);

有关更多信息,请参阅ONT-API wiki、示例

于 2017-08-09T11:07:08.180 回答