使用Java中的owlapi 3将manchester语法中的String转换为OWLAxiom对象

时间:2014-01-08 20:33:16

标签: java owl owl-api

我正在编写一个利用OWL API 3.1.0版的Java程序。我有一个String表示使用曼彻斯特OWL语法的公理,我想在OWLAxiom对象中转换此字符串,因为我需要使用方法{{}将生成的公理添加到本体中1}}(这是addAxiom(OWLOntology owl, OWLAxiom axiom)的方法)。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

以下Java代码如何?请注意,我正在解析一个完整但小巧的本体论。如果您实际上只是期望一些曼彻斯特文本无法解析为完整的本体,您可能需要为所有内容添加一些标准前缀。尽管如此,这更像是对特定应用程序的关注。你还需要确保你得到了你感兴趣的各种公理。必然会有声明公理(例如, Person 是一个类),但是你更有可能对TBox和ABox公理感兴趣,所以我添加了一些关于你如何获得这些公理的笔记。

需要注意的一点是,如果您只是尝试方法添加到现有本体中,那就是OWLParser方法所做的,尽管Javadoc并没有特别明确这一点(在我的看法)。 documentation about OWLParser表示

  

OWLParser将本体文档解析为本体的OWL API对象表示。

并不完全正确。如果parse()的ontology参数已经有内容,并且parse()没有删除它,则本体论参数最终成为本体文档超集的对象表示(它是本体文档加上先前的内容)。幸运的是,这正是您所希望的:您希望阅读一段文本并将其添加到现有本体中。

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxParserFactory;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.OWLParser;
import org.semanticweb.owlapi.io.StreamDocumentSource;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class ReadManchesterString {
    public static void main(String[] args) throws OWLOntologyCreationException, IOException {
        // Get a manager and create an empty ontology, and a parser that 
        // can read Manchester syntax.
        final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        final OWLOntology ontology = manager.createOntology();
        final OWLParser parser = new ManchesterOWLSyntaxParserFactory().createParser( manager );

        // A small OWL ontology in the Manchester syntax.
        final String content = "" +
                "Prefix: so: <http://stackoverflow.com/q/21005908/1281433/>\n" +
                "Class: so:Person\n" +
                "Class: so:Young\n" +
                "\n" +
                "Class: so:Teenager\n" +
                "  SubClassOf: (so:Person and so:Young)\n" +
                "";

        // Create an input stream from the ontology, and use the parser to read its 
        // contents into the ontology.
        try ( final InputStream in = new ByteArrayInputStream( content.getBytes() ) ) {
            parser.parse( new StreamDocumentSource( in ), ontology );
        }

        // Iterate over the axioms of the ontology. There are more than just the subclass
        // axiom, because the class declarations are also axioms.  All in all, there are
        // four:  the subclass axiom and three declarations of named classes.
        System.out.println( "== All Axioms: ==" );
        for ( final OWLAxiom axiom : ontology.getAxioms() ) {
            System.out.println( axiom );
        }

        // You can iterate over more specific axiom types, though.  For instance, 
        // you could just iterate over the TBox axioms, in which case you'll just
        // get the one subclass axiom. You could also iterate over
        // ontology.getABoxAxioms() to get ABox axioms.
        System.out.println( "== ABox Axioms: ==" );
        for ( final OWLAxiom axiom : ontology.getTBoxAxioms( false ) ) {
            System.out.println( axiom );
        }
    }
}

输出结果为:

== All Axioms: ==
SubClassOf(<http://stackoverflow.com/q/21005908/1281433/Teenager> ObjectIntersectionOf(<http://stackoverflow.com/q/21005908/1281433/Person> <http://stackoverflow.com/q/21005908/1281433/Young>))
Declaration(Class(<http://stackoverflow.com/q/21005908/1281433/Person>))
Declaration(Class(<http://stackoverflow.com/q/21005908/1281433/Young>))
Declaration(Class(<http://stackoverflow.com/q/21005908/1281433/Teenager>))
== ABox Axioms: ==
SubClassOf(<http://stackoverflow.com/q/21005908/1281433/Teenager> ObjectIntersectionOf(<http://stackoverflow.com/q/21005908/1281433/Person> <http://stackoverflow.com/q/21005908/1281433/Young>))