===== Advanced ontology engineering ===== * Last verification: **20220909** * Tools required for this lab: * [[https://protege.stanford.edu/software.php#desktop-protege|Protégé Desktop 5]] * [[http://oops.linkeddata.es/|OOPS! (OntOlogy Pitfall Scanner!)]] (web service) ==== Prepare yourself for the lab ==== * [[https://cambridgesemantics.com/blog/semantic-university/learn-owl-rdfs/owl-references-humans/|OWL References for Humans @Cambridge Semantics]] ==== Lab instructions ==== === 1. Let's start: Protégé Desktop === During this lab we will continue the development of the ontology started in the [[.:lab_onto101|previous classes]]. \\ During the previous lab you did a lot of conceptual work -- you thought about the purpose of an ontology, its scope and basic concepts. \\ Now, we will focus on the more technical aspects, introducing selected constructs of the OWL language. **NOTES:** - As previously, the lab is divided into several sections. **Each of them should take approx. 10 minutes!** - The following tasks will sometimes involve specific situations. Try to implement them analogously in your knowledge graph to best understand the particular aspects. - The key classes are: * ''RiversideBandMusicCD'' (in //Primitive class hierarchy// section), * ''BaroqueOrganMusicCD'' (in //Properties and Restrictions// section), * ''GuitarMusicCD'' (in //Defined Classes// section), * ''ACapellaMusic'' (in //Union in OWL// section), * ''ACapellaMixMusicCD'' (in //Universal Restriction and Open World Assumption// section). - The [[http://protege.stanford.edu/publications/ontology_development/ontology101-noy-mcguinness.html|Build your first ontology]] tutorial studied in previous classes may be useful as a reference. Let's start! - Download your ontology from WebProtégé - Open the projects list: [[https://webprotege.stanford.edu/#projects/list]] - From //hamburger menu// (on the right) select Download - Download and start [[https://protege.stanford.edu/software.php#desktop-protege|Protégé Desktop 5]] - Load your ontology in Protégé Desktop: ''File'' -> ''Open...'' - You can change the prefix for your ontology using: ''File'' -> ''Preferences'' -> ''New entities'' -> ''Entry IRI'', e.g. ''https://your-name/game-of-thrones''. * This will be a prefix for all concepts/relations/individuals that we will define in our ontology. * This will **not** change the existing URIs (generated, e.g., by WebProtégé) - You can also specify URI for your ontology (ontology is abstract thing in universe we describe, so it also has URI ;) ) using: ''Refactor'' -> ''Change ontology IRI...'' (it CAN be the prefix, e.g. ''https://your-name/game-of-thrones'') === 2. Primitive class hierarchy === - Do you have a simple hierarchy of classes and subclasses in your model? If not, complete it now! * Use ''Classes'' -> ''Class hierarchy'' tab. * In OWL, it always starts with ''owl:Thing'' as a root node. \\ {{.:protege5_class_hierarchy.png?direct}} * New subclasses can be added using the ''Add subclass'' button on the ''Class hierarchy'' tab or by specifying the superclass in ''SubClass of'' field in ''Description'' panel. \\ {{.:protege5_add_superclass.png?direct}} - Create a class that is a subclass of several classes. In media ontology, we declare the ''RiversideBandMusicCD'' class (//[[wp>Riverside_(band)|Riverside]]//) as a subclass of ''BandMusicCD'', ''PolishMusicCD'' and ''ProgressiveRockMusicCD'' (:!: create the class __once__, then choose the class, click on the ''SubClass of'' button in the class description, and choose appropriate classes from ''Class hierarchy'' tab). \\ {{.:protege5_multiple_superclasses.png?direct&w500}} - You should now have a small hierarchy of classes. === 3. Disjoints === - To make sure a Book cannot be a MusicCD at the same time, add disjoints into your primitive tree. - Select one of your top level concepts (e.g., Book) - Select ''Make primitive siblings disjoint'' in the ''Edit'' Menu - Repeat this where appropriate, for each level of your ontology, selecting a class and making all of its siblings disjoint. \\ {{.:protege5_disjoint_classes.png?direct&w500}} - In ''Reasoner'' menu choose ''HermiT''. Start the reasoner. Once your reasoner is running, periodically classify your ontology by pressing the ''Synchronize reasoner'' button in the ''Reasoner'' menu. Inferred hierarchy can be seen in "Class hierarchy (inferred)" tab: \\ {{.:protege5_inferred_hierarchy.png?direct}} * If you get inconsistencies (some classes will become red in the hierarchy), see what is wrong with ''Explain inference'' button (question mark in ''Description'' tab; see picture in //Defined Classes// section below) and repair the ontology. === 4. Properties and Restrictions === In order to describe our classes we need properties, which are used to relate members of a class. We then add restrictions on the class to state logically how these properties are used. \\ At this stage we are creating **Primitive Classes**, which only have **necessary conditions** (in this case, restrictions) on them –- these are conditions that must be satisfied by all members of this class. - During this exercise, we will use class hierarchy as shown in the picture: \\ {{.:protege5_class_hierarchy3.png?direct|}} * The important thing is that we have a class (''MusicInstrument'') and a set of its subclasses. * We will also define another hierarchies, which will be related to the one in the figure (with ''Artist''/''MusicCD'' as a superclass). * In your ontology, these hierarchies can be anything, e.g., different areas and the associated hierarchy of rulers or classes of wizards and the associated hierarchy of schools of magic. - Step 1: Create Properties - Select the ''Object Properties'' tab - Create a new Object Property, ''containsMusicPerformedBy'' \\ {{.:protege5_objectproperty.png?direct&w400|}} - Create 2 more properties: ''containsMusic'' and ''containsMusicPerformedOn'' - Step 2: Create restrictions - Select the Classes Tab - Select the MusicCD class - In the right panel for this class, select ''SubClass of'' (click the circle with + to add new superclass), then choose ''Object restriction creator'' tab - In the left window choose ''containsMusicPerformedBy'' and in the right choose the ''Artist'' class. Make sure that Restriction Type is set to ''Some (existential)''. This means that for every MusicCD there exist an artist who performs the music in this MusicCD. \\ {{.:protege5_restrictions.png?direct&w500|}} - Step 3: Define a ''BaroqueOrganMusicCD'' class - Create a new subclass of ''MusicCD'': ''BaroqueOrganMusicCD'' - Select ''BaroqueOrganMusicCD'' (note the superclass inherited from the MusicCD) \\ {{.:protege5_inheritedsuperclass.png?direct&w500|}} - Select ''SubClass of'' -> ''Object restriction creator'' - Using Existential restriction choose ''containsMusicPerformedOn'' in the left window and ''OrganInstrument'' in the class hierarchy in the right window. Set another restriction saying that ''BaroqueOrganMusicCD'' - ''containsMusic'' - ''BaroqueMusicGenre''. Eventually you should have a primitive class defined as follows: \\ {{.:protege5_namedclass.png?direct&w500|}} \\ - Create a class ''JimiHendrixMusicCD'' as a subclass of the MusicCD, define that it contains music performed on an electric guitar. - Create some other kinds of MusicCD, books or movies (//if you haven't exceeded your 10 minutes/section yet//) === 5. Defined Classes === Creating a **Defined Class** is similar to creating a //primitive class// (as in previous step), but a defined class has one or more **Necessary & Sufficient Condition**. Classes can easily be migrated between primitive and defined. - In this exercise, we wish to model the statement: "Any MusicCD that contains music played on any kind of guitar is a GuitarMusicCD". * What would an analogous statement look like for your ontology? Maybe "Any person who practices fire magic is a fire mage" or "Any person who rules a kingdom is a king"? - Create a new subclass of ''MusicCD'' called ''GuitarMusicCD''. In general, defined classes are **not** disjoint from their siblings - Create a restriction on ''GuitarMusicCD'' to state that it contains music performed on guitar. - Select both conditions (the new restriction and the superclass, MusicCD) and in the context menu choose ''Convert selected rows to defined class''. \\ {{.:protege_definedclass.png?direct|}} - They should move to the ''Equivalent to'' section and the class icon (see class hierarchy) should change to contain an equivalence symbol. You can also create restrictions under this heading automatically by pressing the circle with + next to ''Equivalent to''. \\ The result should be as below: \\ {{.:protege_definedclass1.png?direct|}} - Classify your ontology (Synchronize the reasoner) and check the ''Class hierarchy (inferred)'' to see what has been classified under the new class. - All MusicCDs that are set to contain music performed on any kind of guitar should now be subclasses of GuitarMusicCD.\\ {{.:protege5_inferredclasshierarchy.png?direct|}} * Click on the JimiHendrixMusicCD which should be classified as a GuitarMusicCD. Choose ''Explain inference'' button (with question mark) and analyze the inference the reasoner did to classify the subclass relationship. * This option may be unavailable if you use a reasoner older than HermiT 1.3.4 :-( \\ {{.:protege5_inferenceexpl.png?direct|}} \\ - If you have time, create several other defined classes and classify each time to see if their definitions are correct. === 6. Union in OWL === In order to define an ACappellaMusicCD, we are going to model the idea of a music performed without instruments. This class is going to be covered by all of the Music that are not InstrumentalMusic. GregorianChant, JewishCantillation, MuslimAnaseheed, etc. are all to be classified as ACapellaMusic. - Create a ''Music'' class and the following subclasses of the ''Music'': ''GregorianChant'', ''JewishCantillation'', ''MuslimAnaseheed''. - Create a new subclass of ''Music'' called ''ACapellaMusic'' - Click on the ''Equivalent to'' add button - Select the ''Class expression editor'' - In the expression editor that appears type a list of the Music you would like to be considered ACapella, separated by the union symbol (lowercase "or") \\ {{.:protege5_union.png?direct|}} - Click OK to accept the changes. This can only be done when the expression is correct (e.g., the class names are valid) - Classify your ontology –- check that all the expected Music classes are now subsumed by ACapellaMusic. \\ {{.:protege5_inferredclasshierarchy1.png?direct|}} === 7. Universal Restriction and Open World Assumption === To state that members of a class can only have a specific relationship with individuals from a specific class we use a Universal (AllValuesFrom) restriction. We need to model the fact that: \\ ''Any MusicCD that **only** has music from ACapellaMusic is a ACapellaMusicCD'' - Create a new subclass of ''MusicCD'' called ''ACapellaMusicCD'' - (With the Add button in ''SubClass of'') create an ''Only (universal)'' restriction on this class along the containsMusic property with a filler of ACapellaMusic. \\ {{.:protege5_universal.png?direct|}} - Convert this class into a defined class by selecting the conditions and choosing ''Convert selected rows to defined class'' - Create a ''ACapellaMixMusicCD'' as a subclass of the ''MusicCD''. - Add restrictions as in the picture: the CD should contain some GregorianChant, JewishCantillation and some MuslimAnasheed music. \\ {{.:protege_universal-example.png?direct|}} - Classify your ontology. - What happens? Do you get the expected results? Is the ''ACapellaMixMusicCD'' classified as an ''ACapellaMusicCD''? === 8. Closing the classes === Why the ACapellaMixMusicCD wasn't classified as a ACapellaMusicCD, although it has only ACapellaMusic on it? \\ Because reasoning in OWL-DL is based on the **Open World Assumption**: * Non-existence cannot be assumed, it has to be stated explicitly * CD could have additional kinds of music on it * Definitions in OWL-DL need closure axiom (to __explicitly__ say class has only the referred restrictions) Therefore, we need to close our MusicCD descriptions in order for them to classify correctly. - Select the ''ACapellaMixMusicCD'' - Create a new ''Only (universal)'' restriction along the containsMusic property. The filler will be a union of all of the music kinds on the CD – ie the union should match all of the music kinds of the existing ''Some (existential)'' restrictions using the contains property. Select Add ''SubClass of'' button, then Class expression editor and type the following: \\ {{.:protege5_closing.png?direct|}} - Classify your ontology to check if this class classifies under ''ACapellaMusicCD''. === 9. Data Properties === Data Properties are relations that connect objects and __literals__. \\ As a **range**, you can specify any **datatype** (most often we use the XSD Datatypes; you have seen them in //RDF: Datatypes// section of [[lab_rdf|Modeling with RDF and RDFS]] lab. \\ Setting a **domain** on a property may lead to unexpected classifier results if the property is misused (e.g., if the domain has been over-constrained; or we use the property dedicated to other class). We will explore this further in this exercise. - Create a ''publishedIn'' data property in the ''Data Properties'' tab. - Set the property's domain to ''MusicCD'', range to ''dateTime'' (or other appropriate datatype if ''dateTime'' isn't available), set the property as "functional" \\ {{.:protege5-dataproperty1.png?direct&w500|}} - Go to the ''Entities'' tab and add a new existential restriction for the class ''Book'' saying that it has to be published somewhen (yes, we will use the property that was defined for MusicCD to see if it explodes ;-) ) \\ {{.:protege5-dataproperty2.png?direct&w550|}} - Run the reasoner: what happens? why? \\ {{.:protege5-nothing.png?direct&w550|}} - Remove the disjoint axioms and run the reasoner again. What happens now? \\ {{.:protege5-wrongsubclass.png?direct&w550|}} - Solve the problem so both MusicCDs and Books can be ''published'' in the ontology. === 10. Finish the ontology === - Finally, we will check our ontology against the most common pitfalls. We can do this using the [[http://oops.linkeddata.es/|OOPS! (OntOlogy Pitfall Scanner!)]]. - Export the ontology to RDF/XML format (used by OOPS!): ''File'' -> ''Save as'' -> ''RDF/XML'' - Paste your RDF/XML file in the OOPS! and click ''Scanner by RDF'' - Fix your ontology accordingly to the guidelines. * Note: as we are learning how to develop ontologies, there is no need to fix all the issues. Correct the most important errors and see what else should be fixed in ready-to-publish ontology (e.g., //Missing annotations// or //No license declared// may be omitted). - Save the final version of the ontology in a safe place! We will use it during the next two labs. ==== Learn more! ==== {{section>.:lab_onto101#learn_more&noheader&nofooter}}