简体   繁体   English

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

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

I'm writing a program in Java that exploits the OWL API version 3.1.0. 我正在编写一个利用OWL API 3.1.0版的Java程序。 I have a String that represents an axiom using the Manchester OWL Syntax, I would like to convert this string in a OWLAxiom object, because I need to add the resulting axiom into an ontology using the method addAxiom(OWLOntology owl, OWLAxiom axiom) (It's a method of OWLOntologyManager ). 我有一个String代表使用曼彻斯特OWL语法的公理,我想在OWLAxiom对象中转换这个字符串,因为我需要使用方法addAxiom(OWLOntology owl, OWLAxiom axiom)将生成的公理添加到本体中(它是一种OWLOntologyManager的方法。 How can I do that? 我怎样才能做到这一点?

How about something like the following Java code? 像下面的Java代码一样怎么样? Note that I'm parsing a complete, but small, ontology. 请注意,我正在解析一个完整但小巧的本体论。 If you're actually expecting just some Manchester text that won't be parsable as a complete ontology, you may need to prepend some standard prefix to everything. 如果您实际上只是期望一些曼彻斯特文本无法解析为完整的本体,您可能需要为所有内容添加一些标准前缀。 That's more of a concern for the particular application though. 尽管如此,这更像是对特定应用程序的关注。 You'll also need to make sure that you're getting the kinds of axioms that you're interested in. There will, necessarily, be declaration axioms (eg, that Person is a class), but you're more likely interested in TBox and ABox axioms, so I've added some notes about how you can get those. 你还需要确保你得到了你感兴趣的公理。必然会有宣言公理(例如,那个人是一个阶级),但你更有可能对TBox和ABox公理,所以我添加了一些关于如何获得这些公理的说明。

One point to note is that if you're only trying to add the axioms to an existing ontology, that's what the OWLParser methods do, although the Javadoc doesn't make this particularly clear (in my opinion). 需要注意的一点是,如果你只是试图公理添加到现有的本体中,那就是OWLParser方法的作用,尽管Javadoc没有特别清楚(在我看来)。 The documentation about OWLParser says that 关于OWLParser文档说明了这一点

An OWLParser parses an ontology document into an OWL API object representation of an ontology. OWLParser将本体文档解析为本体的OWL API对象表示。

and that's not strictly true. 这并不完全正确。 If the ontology argument to parse() already has content, and parse() doesn't remove it, then the ontology argument ends up being an object representation of a superset of the ontology document (it's the ontology document plus the prior content). 如果parse()的ontology参数已经有内容,并且parse()没有删除它,则本体论参数最终成为本体文档超集的对象表示(它是本体文档加上先前的内容)。 Fortunately, though, this is exactly what you want in your case: you want to read a snippet of text and add it to an existing ontology. 幸运的是,这正是您所希望的:您希望阅读一段文本并将其添加到现有本体中。

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 );
        }
    }
}

The output is: 输出是:

== 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