introduction into the owl api tasks
play

Introduction into the OWL API: Tasks Basic Project Setup 1. Create a - PDF document

Introduction into the OWL API: Tasks Basic Project Setup 1. Create a new Java project using your favourite IDE (ideally Eclipse) 2. Create a new package owl.cs.man.ac.uk.owltutorial 3. Create a new Class inside that package OWLAPITutorial, include


  1. Introduction into the OWL API: Tasks Basic Project Setup 1. Create a new Java project using your favourite IDE (ideally Eclipse) 2. Create a new package owl.cs.man.ac.uk.owltutorial 3. Create a new Class inside that package OWLAPITutorial, include a main method 4. Add the OWLAPI version 3.4.8 (owlapi-distribution-3.4.8.jar) to your class path 5. In the Build Path Dialogue, you may want to add the source jar to the respective entry underneath the library. That way you can inspect the OWLAPI classes when using them (see following 2 screenshots) 6. To check whether all went well, write the following code inside the main method: OWLOntologyManager man = OWLManager. createOWLOntologyManager (); IRI ontologyIRI = IRI. create ("http://130.88.198.11/co-ode- files/ontologies/pizza.owl"); try {

  2. OWLOntology ontology = man.loadOntology(ontologyIRI); System. out .println(ontology.getLogicalAxiomCount()); } catch (OWLOntologyCreationException e) { e.printStackTrace(); } 7. When you execute the program, you should be able to see the axiom count: 712 . Congratulations. You have just conquered mount Olympus. 8. For every task in the following, we recommend you to create a new class with some sensible name (OWLAPITutorialTask1) , copy the test code mentioned in the Basic Project Setup into it and go from there. Task 1: Loading ontology from the file system / first glimpse of entities You know now how to load an ontology from the web and extract a simple piece of information about it (the logical axiom count). Your first assignment will be to play with the OWLOntologyManager and find a way to load your own Sushi Ontology that you have created as part of last weeks coursework into the OWL API and print a list of all the classes you have in your ontology onto the console (System.out). When you are done with this exercise, play with the OWLOntology interface and print other interesting bits of information (Ontology IRI, Object Property Count, print all TBox axioms etc) . Tip: make heavy use of your IDEs autocomplete features. In Eclipse you can trigger it by using CTRL + SPACE. Task 2: Creating entities and asserting axioms We have been looking at the properties of an existing ontology. Let’s try to create something for a change. We have seen in the demo how to create a simple class declaration. In this task, you are asked to create a second class , Person, and make Student a subclass of it. To ensure that you have done the right thing, print the axioms in the ontology to the console (you should know how to do this by now). If you are done with this, create two or three students . For that, find out how to create a named individual, and assert it to be a member of the student class. Make sure you add this assertion to the ontology again and check whether it worked in the known style. IRI ontologyIRI = IRI. create ("http://owl.cs.man.ac.uk/ontology"); OWLOntologyManager man = OWLManager. createOWLOntologyManager (); OWLDataFactory factory = man.getOWLDataFactory(); PrefixManager pm = new DefaultPrefixManager(ontologyIRI+"#"); try { OWLOntology ontology = man.createOntology(ontologyIRI); System. out .println("Created: "+ontology.getOntologyID().getOntologyIRI()); OWLClass student = factory.getOWLClass(":Student",pm); OWLDeclarationAxiom declaration = factory.getOWLDeclarationAxiom(student); man.addAxiom(ontology, declaration); System. out .println(student); } catch (OWLOntologyCreationException e) { e.printStackTrace(); }

  3. Task 3: Saving the ontology Copy the final version of your Task2 Java Class, and attempt the following: 1) Save the ontology somewhere with a sensible name. 2) Adapt your Task1 class so that you can see whether you can load the ontology you just saved again. 3) Save the ontology in another format (functional syntax for example) 4) Open the two serialisations in a text editor, identify the axioms you added and look at the differences. OutputStream os = new FileOutputStream( new File("D:\\ruvinsgang.owl")); man.saveOntology(ontology, new OWLXMLOntologyFormat(), os); Task 4: Class Expressions and more ABox assertions The axioms we added so far are nice, but not very complicated. What if we wanted to say that “a student is a Person that is enrolled at a University and attends some course?” 1) Copy the Task2 class and create a definition (equivalent class axiom) reflecting the sentence above. You might have to create some more entities (classes and object properties) in order to do that. I t’s a complicated task , so take your time. 2) Create a named individual (like yourself, we will call her/him person X from now on), assert her or him as a Person ( not Student!), and a named individual ManchesterUniversity, asserted as an instance of the class University. 3) Assert that the person X you just created is enrolled in the ManchesterUniversity . This task requires you to create an object property assertion! 4) Assert that the person you just created attends a course, but without specifying which one. In Manchester syntax that looks like this: Individual: Nico Types: attendsCourse some Course, Person I t’s a bit tricky to wrap your head around that one, but you have the tools already to succeed at this. 5) Print all axioms in your ontology to check whether they look complete according to the task. You might even want to save the ontology like we discussed in the previous exercise, and inspect the result in Protégé. This is all you need to conquer next week’s coursework. But let ’ s look at some advanced things. Task 5: Reasoning I We have discussed reasoners in class. Let ’ s see what the reasoner can tell us about our ontology. Copy the Java class from Task2 ideally, and go from there. 1) Create a new ontology with at least two satisfiable classes and one unsatisfiable class. Tip: the easiest way to do that is to make a class a subclass of the intersection of two disjoint classes. One suggestion (the thing that will be demonstrated later on) is to use a Student and

  4. Teacher class, make them disjoint and make a class like Demonstrator a subclass of their intersection. 2) Instantiate a reasoner and determine whether your ontology is consistent. Should it be? 3) Print the list of unsatisfiable classes to the console. 4) Create an individual and make it an instance of the unsatisfiable class in the way we discussed it before. 5) Check whether the ontology is consistent and explain why. Task 6: Reasoning II Now that we know how to instantiate the reasoner, let ’ s see if we can do something useful with it. 1) Copy the Task4 class to a new Task6 class. 2) Instantiate the reasoner again in the same way as we did in Task 5. 3) The question is: Is the person X you created a student or not? a. Try to find this out using the OWLOntology interface alone. Don ’ t spend more than 7 minutes on this and move on. b. Ask the reasoner the same question (use the OWLReasoner interface) i . 4) If you are bored, play with the reasoner interface and see what other cool things you can do with it (queries similar to the once in the DLQuery tab in Protégé, print the reasoner name, and so on). Task 7: Annotations A last task for those that have finished all the rest is the following. It is a bit tricky, I do not really expect anyone to solve it in the short amount of time left: 1) Load your Sushi ontology in the same way as discussed in Task 1. 2) Instantiate a reasoner 3) Get all the subclasses of your Sushi class (using the reasoner) 4) For all those subclasses, create a RDFSLabel by taking the class name, introducing space where there are uppercase letters, and then making the entire string lower case. For example “ BlackPepperSearedBeef ” becomes “ black pepper seared beef ” . Add the respective annotation assertion to the ontology. 5) Save the ontology and inspect your results. i Should this not work, run reasoner.precomputeInferences() and ask the reasoner again.

Recommend


More recommend