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

拜拜、爱过 提交于 2019-12-08 08:16:26

问题


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?


回答1:


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.

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

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

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

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


来源:https://stackoverflow.com/questions/21005908/convert-string-in-manchester-syntax-to-owlaxiom-object-using-owlapi-3-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!