繁体   English   中英

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

[英]Convert String in manchester syntax to OWLAxiom object using owlapi 3 in Java

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

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

需要注意的一点是,如果你只是试图公理添加到现有的本体中,那就是OWLParser方法的作用,尽管Javadoc没有特别清楚(在我看来)。 关于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>))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM