使用Java中的owlapi 3将曼彻斯特语法中的String转换为OWLAxiom对象 [英] Convert String in manchester syntax to OWLAxiom object using owlapi 3 in Java

查看:181
本文介绍了使用Java中的owlapi 3将曼彻斯特语法中的String转换为OWLAxiom对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a program in Java that exploits the OWL API version 3.1.0. 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). How can I do that?

推荐答案

类似下面的Java代码如何?请注意,我正在解析一个完整的但很小的本体.如果您实际上只是希望某些曼彻斯特文本无法作为完整的本体进行解析,则可能需要在所有内容前添加一些标准前缀.但是,对于特定的应用程序,这更值得关注.您还需要确保获得自己感兴趣的公理.肯定会有声明公理(例如, Person 是一个类),但是您更可能对TBox和ABox公理感兴趣,所以我添加了一些有关如何获取它们的说明.

How about something like the following Java code? 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 (e.g., 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.

需要注意的一点是,如果您只是想将公理添加到现有本体中,这就是OWLParser方法的工作,尽管Javadoc并未对此做特别明确的说明(在我的想法). 有关OWLParser的文档表示

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). The documentation about OWLParser says that

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

An OWLParser parses an ontology document into an OWL API object representation of an ontology.

并非完全正确.如果parse()的本体论参数已经包含内容,而parse()并未将其删除,则本体论参数最终将成为本体文档超集的对象表示(本体文档加上先前的内容).幸运的是,这正是您所需要的:您想阅读一段文本并将其添加到现有的本体中.

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

输出为:

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

这篇关于使用Java中的owlapi 3将曼彻斯特语法中的String转换为OWLAxiom对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆