owlcs / owlapi

OWL API main repository
822 stars 315 forks source link

Question about Manchester Syntax Parser #1008

Closed shyamaW closed 3 years ago

shyamaW commented 3 years ago

Hi I tried to parse the expression which is in manchester OWL syntax format. I tested Example 01 mentioned below and it works successfully.

However, my requirement is different as I want to parse statements (1,2,3) like follows when the ontology does not include the entity Car, Vehicle, Person, isOwnerOf. For that, I followed the program provided in "https://stackoverflow.com/questions/21005908/convert-string-in-manchester-syntax-to-owlaxiom-object-using-owlapi-3-in-java". Unfortunately, the code line "parser.parse( new StreamDocumentSource( in ), ontology );" does not work for OWL API version 5. As I think, there is no parse() function in OWL API v5. Thus, I would appreciate it if you could assist me to write a similar code using OWL version 5. I use OWL API 5.1.17.

  1. "Class: Car SubClassOf: Vehicle"
  2. "Class: Person"
  3. "ObjectProperty: isOwnerof Domain: Person Range: Car " Thank You Shyama

//Example 01 -------------------------------------------------------------------------------------------- OWLOntologyManager manager=OWLManager.createOWLOntologyManager();

    IRI IOR = IRI.create("http://www.sln4mop.org/ontologies/2021/testV1");

    OWLOntology rootOntology = manager.createOntology(IOR);
    OWLDataFactory df = rootOntology.getOWLOntologyManager().getOWLDataFactory();
    OWLClass Car = df.getOWLClass(IOR+"#Car");
    OWLDeclarationAxiom da1 = df.getOWLDeclarationAxiom(Car);
    rootOntology.add(da1);
    OWLClass vehicle = df.getOWLClass(IOR+"#Vehicle");
    OWLDeclarationAxiom da2 = df.getOWLDeclarationAxiom(vehicle);
    rootOntology.add(da2);

    Set<OWLOntology> importsClosure = rootOntology.getImportsClosure();
// Create a bidirectional short form provider to do the actual mapping.
// It will generate names using the input
// short form provider

OWLEntityChecker entityChecker = new ShortFormEntityChecker(
    new BidirectionalShortFormProviderAdapter(manager, importsClosure,
        new SimpleShortFormProvider()));
ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
parser.setDefaultOntology(rootOntology);
parser.setOWLEntityChecker(entityChecker);
String input1 = "Car SubClassOf Vehicle";
parser.setStringToParse(input1);
OWLAxiom axiom = parser.parseAxiom();
System.out.println("CheckManchesterSyntax.main() " + axiom);

//--------------------------------------------------------------------------------

ignazio1977 commented 3 years ago

In OWLAPI 5, look at ManchesterOWLSyntaxParser

shyamaW commented 3 years ago

Really appreciate your prompt response.

Sorry to bother you again, I want to add new axioms using Manchester syntax expressions that are already not in the ontology, Thus, can I parse an expression, if the expression contains an undeclared class name/ object property name.

Doesn't parser automatically detect the undeclared class name/ object property name/ etc. and add them automatically?

This is the code that I tried and got an exception.

// code snippet----------------------------------------------------------------------------------- final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); final OWLOntology ontology = manager.createOntology(); OWLDataFactory df = ontology.getOWLOntologyManager().getOWLDataFactory(); ManchesterOWLSyntaxParser parser = new ManchesterOWLSyntaxParserImpl( new OntologyConfigurator(), df);

    // A small OWL ontology in the Manchester syntax.
    String input1 = "Prefix: so: <http://stackoverflow.com/q/21005908/1281433/>\n" + 
            "Class: so:Car SubClassOf: so:Vehicle";

    parser.setStringToParse(input1);
    parser.parseAxiom(); // exceptions occured

//------------------------------------------------------------------------------------- Exception:

Exception in thread "main" org.semanticweb.owlapi.manchestersyntax.renderer.ParserException: Encountered Prefix: at line 1 column 1. Expected one of: Class name Object property name Data property name inv Functional inverse InverseFunctional ( Asymmetric

ignazio1977 commented 3 years ago

The parser won't add entities that are unknown - most of the time there isn't enough information in a manchester syntax axiom to disambiguate what is a class, a property or a datatype. You should add the declarations beforehand, or you can try parsing a declaration axiom, although that's untested, so I can't be sure it will work.

If you're parsing a large number of manchester syntax axioms, are you taking them from an ontology file? It would be easier to just parse the whole ontology instead of doing the parser's work by hand.

shyamaW commented 3 years ago

Greate! Your assistance is very helpful for me.

Thanks and appreciate your support. cheers!