当前位置: 首页 > 知识库问答 >
问题:

在Java中使用OWLAPI3将曼彻斯特语法中的字符串转换为OWLAxiom对象

张承颜
2023-03-14

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

共有1个答案

夏侯枫
2023-03-14

下面的Java代码怎么样?请注意,我解析的是一个完整但很小的本体。如果您实际上只是期望一些曼彻斯特文本不能解析为一个完整的本体,那么您可能需要在所有内容前加一些标准前缀。不过,这是特定应用程序更关心的问题。您还需要确保您得到了您感兴趣的公理类型。一定会有声明公理(例如,那个人是一个类),但您更可能对TBox和ABox公理感兴趣,因此我添加了一些关于如何获得这些公理的说明。

需要注意的一点是,如果您只是试图将公理添加到现有的本体中,这就是OWLParser方法所做的,尽管Javadoc并没有特别清楚地说明这一点(在我看来)。关于OWLParser的文档说明

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

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>))
 类似资料:
  • 有没有办法将OWL公理转换成曼彻斯特语法?我知道OWL-API将允许您将曼彻斯特语法中的一个句子解析为OWL函数语法,但我需要做的正好相反。

  • 问题内容: 我有一个返回映射值(字符串)作为通用对象的函数。如何将其转换回字符串。我尝试了toString()但我得到的只是 收货结束 给我一个输出 问题答案: 恐怕您的地图包含物体以外的东西。如果调用String对象,则会获取字符串本身。 得到的结果表明您可能具有String数组。

  • 所以我尝试将int转换为string,然后再转换为charAt(0)、charAt(1)和charAt(2)。我这样做是为了把3位数的整数拆分成3个不同的整数。然后我想把这些整数转换成字符串。 我想做的是从101及以上的数字中提取并用文字打印出来。我有数百种、十种和一种方法。我试着把第一个整数应用到百分法,第二个整数应用到十分法,第三个整数应用到一分法。 这是一种 现在我正试着打印单词,我发现了3

  • 本文向大家介绍在Java中将StringBuilder转换为字符串,包括了在Java中将StringBuilder转换为字符串的使用技巧和注意事项,需要的朋友参考一下 StringBuilder类的toString()方法重新运行当前对象的String值。要将StringBuilder转换为String值,只需在其上调用toString()方法。 实例化StringBuilder类。 使用appe

  • 如何将Java中的字符串(如12.34)转换为双精度?

  • 问题内容: 我是Java的新手,通常使用PHP。 我正在尝试转换此字符串: 2011年3月14日星期一格林尼治标准时间16:02:37 放入Calendar对象,这样我就可以像这样轻松地提取Year和Month: 手动解析它不是一个好主意吗?使用子串方法? 任何建议都会有所帮助,谢谢! 问题答案: 注意:Locale根据你的环境/要求进行设置