1

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?

giuseta
  • 117
  • 2
  • 15
  • [How to add RDF triples to an OWLOntology?](http://stackoverflow.com/q/17468984/1281433) might be relevant, though it's not quite an answer. – Joshua Taylor Jan 08 '14 at 21:26
  • Side note: 3.1.0 is very old and refers OWL 1.1, a transitional standard that has been superseded by OWL 2. I would recommend moving, if possible to OWLAPI 3.4.8, or 3.4.10 (which is due to be released by January 15th). – Ignazio Jan 09 '14 at 00:25
  • I can't move to later version of owlapi because I'm adding features to an application not written by me that uses a custom modified version of owlapi 3.1.0 and without documentation (sigh!). Moving from this modified version of 3.1.0 to later version will make me spend more time than writing useful code – giuseta Jan 09 '14 at 14:50
  • Interesting, is the application available on gitHub or similar? 3.1.0 was released as LGPL only, so a modified version of it would make it, and the application using it, GPL :-P but it's more of interest to me to know what the application is, in case it's worth helping with porting it to a more recent version. – Ignazio Jan 10 '14 at 17:18

1 Answers1

3

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>))
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • 2
    If there is only one axiom in the input string, this can be slightly simplified calling `ManchesterOWLSyntaxEditorParser.parseAxiom`, which simply starts the parsing from the axiom level rather than the ontology level. However care must be taken in setting the short form provider for the parser, since all entities used in the axiom must already be known to the short form provider. The `DLQueryExample` code has an example of how to set such a parser. https://github.com/owlcs/owlapi/wiki/DL-Queries-with-a-real-reasoner – Ignazio Jan 09 '14 at 00:22
  • @Ignazio Ah, thanks! That would make things much cleaner I think. – Joshua Taylor Jan 09 '14 at 00:25
  • Thanks to both, I think I will try what @Ignazio suggested. – giuseta Jan 09 '14 at 14:52
  • Do any of you have an example on how to use the ManchesterOWLSyntaxParser.parseAxiom() method? Docs only say "Parsing "Inline" Axioms.", but that does not help a lot. If I have a string with an axion, how am I supposed to pass the axiom to the parser? – Antonio Sesto Jul 28 '20 at 13:58