如何将RDF三元组添加到OWLOntology?

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

:Tom foaf:knows :Anna 

其中:是我要导入数据的本体的标准命名空间,但也可以使用其他来自导入的前缀。 三元组由主语,属性/谓语和对象组成,我知道在每条消息中哪个是哪个。

在接收端,我有一个带有OWLOntology对象的Java程序,该对象表示应该临时存储新到达的三元组以用于推理和其他东西的本体。 我有点设法将三元组变成Jena 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的构造函数失败了。

如果有人能指出一个很好的例子。 也许有一个更好的方式,我没有想到实现同样的事情?

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

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

 :Tom :likes :Anna :Tom :age 35 

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

  

然后这个:

  35   

这是代码:

 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 //  // 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: //  // 35 //  //  manager.saveOntology( ontology, System.out ); } } 

在ONT-API(也是OWL-API)中它非常简单:

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

这是一个jena-way。 有关更多信息,请参阅ONT-API wiki