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

我正在用Java编写一个利用OWL API 3.1.0版的程序。 我有一个使用曼彻斯特OWL语法表示公理的String ,我想在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: \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( ObjectIntersectionOf( )) Declaration(Class()) Declaration(Class()) Declaration(Class()) == ABox Axioms: == SubClassOf( ObjectIntersectionOf( ))