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

时间:2013-07-04 11:21:01

标签: java rdf ontology owl owl-api

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

:Tom foaf:knows :Anna

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

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

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

2 个答案:

答案 0 :(得分:2)

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

以下代码:创建本体;宣告两个有名的人,汤姆和安娜;声明一个对象属性,喜欢;并声明一个数据属性,年龄。一旦宣布这些,我们打印本体只是为了确保它是我们所期望的。然后它创建一个OWLRDFConsumer。使用者构造函数需要一个本体,AnonymousNodeCheckerOWLOntologyLoaderConfiguration。对于配置,我只使用了由无参构造函数创建的一个,我认为没关系。对于节点检查器,唯一方便的实现者是TurtleParser,因此我创建了其中一个,将null作为Reader传递。我认为这样就行了,因为解析器不会被调用来读取任何东西。然后,消费者的handle(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 );
    }
}

答案 1 :(得分:0)

在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