Difference between revisions of "Tutorial: Ontology Development"

From Developer Documents
Jump to navigation Jump to search
 
(102 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== Step 1: Ontology Plug-in ==
+
NEEDS TO BE REVISED TO MATCH CURRENT CONVENTIONS
Ontologies are encapsulated in OSGi bundles. In eclipse we develop them as equinox plug-ins. It contains ontology sources (graph/*.pgraph), compiled graph file (graph.tg), and Java resource files (src and bin).
+
 
 +
__NOTOC__
 +
''"What's up folks."'' This tutorial series is composed of four parts. As it is a trail, you should study them in order. Today we are going to create a movie database ontology for a client of your Acme company. Even though Simantics is intended for simulator integration platform, we decided to use movies as the theme. This basic level tutorial doesn't go into simulation specifics, and the subject is more familiar for any developer to relate to than a simulation specific one. Before starting make sure you have installed [[Development_Environment_Setup_Guide#Install_Simantics_target_platform|Target Platform]] and [[Graph_Compiler|Graph Compiler]] to your Eclipse as instructed in [[Development Environment Setup Guide]].
 +
 
 +
 
 +
Related documents:
 +
* Complete [[Graph File Format]]
 +
* As ontology developer you must understand [[Version Migration#Ontology migration|ontology version migration]]
 +
 
 +
== Step 1: Setup Ontology Plug-in ==
 +
Ontologies are encapsulated in OSGi bundles. In Eclipse, we develop them as Eclipse plug-ins.  An ontology bundle is a specialized package. It has the following content: graph source files (graph/*.pgraph), a compiled graph file (graph.tg), and Java files (src/ and bin/).
  
 
Open a New Plug-in Project wizard:
 
Open a New Plug-in Project wizard:
* '' '''Select File''' >> '''New''' >> '''Plug-in Development''' >> '''Plug-in Project''' ''
+
* Open Plug-in Development Perspective
 +
* Select'' '''File''' >> '''New''' >> '''Other'''
 +
* Select'' '''Plug-in Development''' >> '''Plug-in Project''' ''
  
Our organization is Acme Software ltd and from there is our plug-in name derived, ''com.acme.movie.ontology''. Insert that as project name. In the following page you should know that this plugin does not contribute to UI, does not need activator or isn't a rich client application.
+
A ''New Plug-in Project'' wizard will open.
 +
* Our organization is Acme Software ltd and from there is our plug-in name derived, ''com.acme.movie.ontology''. Insert that as project name. '''Next >'''
 +
* Set Version to ''0.1.0.qualifier''. In this page you should know that this plugin does not contribute to UI, does not need an activator and is not a rich client application. '''Next >'''
 +
* Use template ''Plug-in with a Simantics Ontology''. '''Next >'''
 +
* The domain of your company is ''www.acme.com'' and the name of the ontology ''Movie''. The initial version is 1.0. '''Finish'''
  
===Setup Graph Building Nature===
+
You have now a new project in Package Explorer. You will find META-INF/MANIFEST.MF, graph/Movie.pgraph, and graph.tg in it.
First, Make sure you have installed [[Graph Compiler]].
 
Next, add Graph builder into the project nature.  
 
  
* '' "com.acme.movie.ontology"' >> '''Context Menu''' >> '''Configure''' >> '''Add graph compilation''' ''
+
== Step 2: Initialize Ontology ==
 +
Open Movie.pgraph and start editing.
  
[[File:graph_nature.png]]
+
You will be using Layer0 concepts. You can avoid writing full URIs for all references by defining an abbreviation. It is a good idea to declare all ontologies you will use in this way:
 +
  L0 = <nowiki><http://www.simantics.org/Layer0-1.0></nowiki>
  
Also create directory called graphs, and there a file Movie.pgraph.
+
Your acme company has a domain that has to be declared. Because this is the first time the domain is used, you have to mark it "new" so that the graph compiler does not try to find it from dependencies.
 +
  <nowiki><http://www.acme.com></nowiki> : L0.Library
 +
      @L0.new
  
=== Configure MANIFEST.MF ===
+
The ontology has a representing resource, an instance of L0.Ontology. The second line tells the compiler to create a Resource file as well.
Your ontology well depend on Layer0, so open META-INF/MANIFEST.MF in a editor and add org.simantics.layer0 to plug-in dependencies.  
+
  MO = <nowiki><http://www.acme.com/Movie-1.0></nowiki> : L0.Ontology
 +
      L0.HasResourceClass "com.acme.movie.ontology.Movie"
  
Also, you are going to  
+
== Step 3: Export package ==
*Export package com.acme.movie.ontology
+
Now that you have some content, graph compiler has created a Resource class ''com.acme.movie.ontology.Movie'' under ''src/'' folder. When developing database code you can gain access to your ontology concepts using this class. In OSGi plugin environment, the access to other plugins' classes is denied by default. To share, you must export its Java package.
 +
* '' Open '''META-INF/MANIFEST.MF'''
 +
* ''Add '''Exported Packages''' >> '''com.acme.ontology''' ''
  
 
Your MANIFEST.MF should look like this.
 
Your MANIFEST.MF should look like this.
Line 27: Line 48:
 
Manifest-Version: 1.0
 
Manifest-Version: 1.0
 
Bundle-ManifestVersion: 2
 
Bundle-ManifestVersion: 2
Bundle-Name: Ontology
+
Bundle-Name: http://www.acme.com/Movie
 
Bundle-SymbolicName: com.acme.movie.ontology
 
Bundle-SymbolicName: com.acme.movie.ontology
 
Bundle-Version: 1.0.0.qualifier
 
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: ACME
+
Bundle-Vendor: Acme Software ltd
 
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 
Require-Bundle: org.simantics.layer0;bundle-version="1.0.0"
 
Require-Bundle: org.simantics.layer0;bundle-version="1.0.0"
Export-Package: com.acme.ontology
+
Export-Package: com.acme.movie.ontology
 +
</pre>
 +
 
 +
In this case the ontology's namespace root URI is '''<nowiki>http://www.acme.com/Movie-1.0</nowiki>'''.
 +
Due to ontology versioning support in the platform
 +
# '''Bundle-Name''' must match URI without the version number, i.e. '''<nowiki>http://www.acme.com/Movie</nowiki>'''
 +
# '''Bundle-Version''' first two digits (major.minor) must match the URI's version part, i.e. '''1.0'''.
 +
 
 +
Notice, in particular, that the ontology depends on Layer0. If other ontologies are needed, they have to be added to MANIFEST.MF.
 +
 
 +
== Step 4: Write your Movie Ontology ==
 +
In movie database, we are going to store objects such as movies, actors, directors and producers. There is going to be a few properties about them, and a couple of relations. At this point it is a good idea to take a look at the [[Graph File Format|graph file format]] documentation. Here is all we are going to define in one diagram:
 +
[[File:MovieOntology.png|center]]
 +
 
 +
Open Movie.pgraph again.
 +
 
 +
* Specify Types
 +
First we need to specify the types ''Movie'', ''Character'', ''Cast'' and ''Person''. Actors, directors and producers are modelled as ''Persons''. A person becomes a representative of a classification, say actor, once he/she has acted in a motion picture (modeled as interrelation statement). Cast is a anonymous resource that links together Person (actor) and Character in one Movie. There is no inheritance between the concepts in our ontology, however every type in Simantics has to inherit the Entity type.
 +
 
 +
// Types
 +
MO.Movie <T L0.Entity
 +
MO.Character <T L0.Entity
 +
MO.Person <T L0.Entity
 +
MO.Cast <T L0.Entity
 +
 
 +
* Specify Properties
 +
The following relations define the basic properties that can be given to the types of our ontology. All properties are functional, i.e. every resource may have at most one statement of the relation.
 +
 
 +
// Properties
 +
MO.HasYearOfBirth <R L0.HasProperty : L0.FunctionalRelation
 +
    L0.HasDomain MO.Person
 +
    L0.HasRange L0.Integer 
 +
MO.HasAge <R L0.HasProperty : L0.FunctionalRelation
 +
    L0.HasDescription "The age of the character during the movie."
 +
    L0.HasDomain MO.Character
 +
    L0.HasRange L0.Integer 
 +
MO.HasGender <R L0.HasProperty : L0.FunctionalRelation
 +
    L0.HasDescription "The gender of a person or a character."
 +
    L0.HasRange MO.Gender
 +
MO.HasTitle <R L0.HasProperty : L0.FunctionalRelation
 +
    L0.HasDescription "The English title of the movie."
 +
    L0.HasDomain MO.Movie
 +
    L0.HasRange L0.String
 +
MO.HasName <R L0.HasProperty : L0.FunctionalRelation
 +
    L0.HasDescription "The name of a person or a character."
 +
    L0.HasRange L0.String
 +
 
 +
* Specify Gender enumeration
 +
Enumerations are special kind of types: They have a fixed number of instances that are defined in the ontology. Gender is a prototypical example:
 +
 
 +
// Enumeration
 +
MO.Gender <T L0.Property
 +
    @L0.tag L0.Enumeration
 +
MO.Gender.Male : MO.Gender
 +
MO.Gender.Female : MO.Gender
 +
 
 +
* Add the properties to your types
 +
Add properties with ''@L0.singleProperty'' or ''@L0.optionalProperty''. They are actually restrictions. The former means that an instance of Character is a valid instance only it has exactly one HasName statement. Optional property requires only that the number of such statements is at most one.
 +
 
 +
// Constrainting property cardinalities
 +
MO.Character
 +
    @L0.singleProperty MO.HasName
 +
    @L0.optionalProperty MO.HasGender
 +
    @L0.optionalProperty MO.HasAge   
 +
MO.Person
 +
    @L0.singleProperty MO.HasName
 +
    @L0.singleProperty MO.HasGender
 +
    @L0.singleProperty MO.HasYearOfBirth
 +
MO.Cast
 +
    @L0.singleProperty MO.OfRole
 +
    @L0.singleProperty MO.IsPlayedBy
 +
 
 +
* Specify Relations
 +
Specify then the relations of the ontology with their domains, ranges and inverses.
 +
 
 +
// Relations
 +
MO.IsDirectedBy <R L0.IsRelatedTo
 +
    L0.HasDomain MO.Movie
 +
    L0.HasRange MO.Person
 +
    L0.InverseOf MO.IsDirectorOf <R L0.IsRelatedTo
 +
MO.IsProducedBy <R L0.IsRelatedTo
 +
    L0.HasDomain MO.Movie
 +
    L0.HasRange MO.Person
 +
    L0.InverseOf MO.IsProducerOf <R L0.IsRelatedTo
 +
MO.HasCast <R L0.IsRelatedTo
 +
    L0.HasDomain MO.Movie
 +
    L0.HasRange MO.Cast
 +
    L0.InverseOf MO.IsCastOf <T L0.IsRelatedTo
 +
MO.IsPlayedBy <R L0.IsRelatedTo : L0.FunctionalRelation
 +
    L0.HasDomain MO.Cast
 +
    L0.HasRange MO.Person
 +
    L0.InverseOf MO.IsCastedTo <R L0.IsRelatedTo
 +
MO.OfRole <R L0.IsRelatedTo : L0.FunctionalRelation
 +
    L0.HasDomain MO.Cast
 +
    L0.HasRange MO.Character
 +
    L0.InverseOf MO.HasCasting <R L0.IsRelatedTo
 +
 
 +
* Create a template
 +
Casting is a binding between a movie, a character and an actor. To state a casting in a database three statements and one instance is required. To make the code more readable, define a template.
 +
MO.Casting : L0.Template
 +
    @template %movie %character %actor
 +
        %movie
 +
            MO.HasCast _ : MO.Cast
 +
                MO.IsPlayedBy %actor
 +
                MO.OfRole %character
 +
 
 +
Save file. Your Movie.java resource file should now contain a list of resources and URIs. Go on and take a look.
 +
 
 +
== Step 5: Setup Build Properties ==
 +
''build.properties'' determines which files are included with the binary and the source release. A new ontology plug-in needs some fixing, you must add graph.tg to the binary build and ''graph''-folder to source build. You can make the modification with the form editor.
 +
 
 +
* Open ''build.properties'' and go to ''build''-page.
 +
* Check ''graph.tg'' under ''Binary Build''.
 +
* Check ''graph''-folder under ''Source Build''.
 +
 
 +
If you open the ''build.properties'' page, it should look like this.
 +
<pre>
 +
source.. = src/
 +
output.. = bin/
 +
bin.includes = META-INF/,\
 +
              .,\
 +
              graph.tg
 +
src.includes = graph/
 
</pre>
 
</pre>
  
=== Developing the ontology ===
+
== Step 6: Download MovieLibrary ==
Open Movie.pgraph and start editing.
+
We made a Movie Library for initial data. Download: [[file:MovieLibrary.pgraph]] and put in graph folder.
 +
 
 +
If you take a look inside you'll find a root resource at <nowiki>http://www.acme.com/MovieLibrary</nowiki>. All movies, actors and other persons are linked to/from the library with L0.PartOf/L0.ConsistsOf relations.
 +
ML = <nowiki><http://www.acme.com/MovieLibrary></nowiki> : L0.Library
 +
    @L0.new
 +
 
 +
You'll also find an example of the template in use:
 +
ML.tt0830515 : MO.Movie
 +
    @MO.Casting      ML.JamesBond        ML.DanielCraig
 +
    @MO.Casting      ML.M                ML.JudiDench
 +
 
 +
== Exercises ==
 +
 
 +
There are some [[Ontology_Development_Exercises|exercises]] available related to this tutorial.
 +
 
  
As you are using Layer0 concepts, it is a good idea to capture its namespace:
+
<hr />
  L0 = <http://www.simantics.org/Layer0-1.0>
+
{{handshake| '''Congratulations''' You have now set-up an ontology.}}
  
Your acme company has a namespace which is used . You are creating a ''new'' one so it must be stated.
+
Give your brains a break before proceeding to the next tutorial.<br/>
  <http://com.acme> : L0.Library
+
[[file:next.png]] [[Tutorial: Project Development|Tutorial 2: Project Development]]
      @L0.new
 
  
The ontology has a representing resource, an instance of L0.Ontology. Don't forget to add stub-compiler metadata L0.HasResourceClass.
+
[[Category: Ontology Development]]
  Movie = <http://com.acme/Movie-1.0> : L0.Ontology
+
[[Category: Tutorials]]
      L0.HasResourceClass "com.acme.ontology.Movie"
 

Latest revision as of 06:19, 19 March 2014

NEEDS TO BE REVISED TO MATCH CURRENT CONVENTIONS


"What's up folks." This tutorial series is composed of four parts. As it is a trail, you should study them in order. Today we are going to create a movie database ontology for a client of your Acme company. Even though Simantics is intended for simulator integration platform, we decided to use movies as the theme. This basic level tutorial doesn't go into simulation specifics, and the subject is more familiar for any developer to relate to than a simulation specific one. Before starting make sure you have installed Target Platform and Graph Compiler to your Eclipse as instructed in Development Environment Setup Guide.


Related documents:

Step 1: Setup Ontology Plug-in

Ontologies are encapsulated in OSGi bundles. In Eclipse, we develop them as Eclipse plug-ins. An ontology bundle is a specialized package. It has the following content: graph source files (graph/*.pgraph), a compiled graph file (graph.tg), and Java files (src/ and bin/).

Open a New Plug-in Project wizard:

  • Open Plug-in Development Perspective
  • Select File >> New >> Other
  • Select Plug-in Development >> Plug-in Project

A New Plug-in Project wizard will open.

  • Our organization is Acme Software ltd and from there is our plug-in name derived, com.acme.movie.ontology. Insert that as project name. Next >
  • Set Version to 0.1.0.qualifier. In this page you should know that this plugin does not contribute to UI, does not need an activator and is not a rich client application. Next >
  • Use template Plug-in with a Simantics Ontology. Next >
  • The domain of your company is www.acme.com and the name of the ontology Movie. The initial version is 1.0. Finish

You have now a new project in Package Explorer. You will find META-INF/MANIFEST.MF, graph/Movie.pgraph, and graph.tg in it.

Step 2: Initialize Ontology

Open Movie.pgraph and start editing.

You will be using Layer0 concepts. You can avoid writing full URIs for all references by defining an abbreviation. It is a good idea to declare all ontologies you will use in this way:

 L0 = <http://www.simantics.org/Layer0-1.0>

Your acme company has a domain that has to be declared. Because this is the first time the domain is used, you have to mark it "new" so that the graph compiler does not try to find it from dependencies.

 <http://www.acme.com> : L0.Library
     @L0.new

The ontology has a representing resource, an instance of L0.Ontology. The second line tells the compiler to create a Resource file as well.

 MO = <http://www.acme.com/Movie-1.0> : L0.Ontology
     L0.HasResourceClass "com.acme.movie.ontology.Movie"

Step 3: Export package

Now that you have some content, graph compiler has created a Resource class com.acme.movie.ontology.Movie under src/ folder. When developing database code you can gain access to your ontology concepts using this class. In OSGi plugin environment, the access to other plugins' classes is denied by default. To share, you must export its Java package.

  • Open META-INF/MANIFEST.MF
  • Add Exported Packages >> com.acme.ontology

Your MANIFEST.MF should look like this.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: http://www.acme.com/Movie
Bundle-SymbolicName: com.acme.movie.ontology
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Acme Software ltd
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.simantics.layer0;bundle-version="1.0.0"
Export-Package: com.acme.movie.ontology

In this case the ontology's namespace root URI is http://www.acme.com/Movie-1.0. Due to ontology versioning support in the platform

  1. Bundle-Name must match URI without the version number, i.e. http://www.acme.com/Movie
  2. Bundle-Version first two digits (major.minor) must match the URI's version part, i.e. 1.0.

Notice, in particular, that the ontology depends on Layer0. If other ontologies are needed, they have to be added to MANIFEST.MF.

Step 4: Write your Movie Ontology

In movie database, we are going to store objects such as movies, actors, directors and producers. There is going to be a few properties about them, and a couple of relations. At this point it is a good idea to take a look at the graph file format documentation. Here is all we are going to define in one diagram:

MovieOntology.png

Open Movie.pgraph again.

  • Specify Types

First we need to specify the types Movie, Character, Cast and Person. Actors, directors and producers are modelled as Persons. A person becomes a representative of a classification, say actor, once he/she has acted in a motion picture (modeled as interrelation statement). Cast is a anonymous resource that links together Person (actor) and Character in one Movie. There is no inheritance between the concepts in our ontology, however every type in Simantics has to inherit the Entity type.

// Types
MO.Movie <T L0.Entity
MO.Character <T L0.Entity
MO.Person <T L0.Entity
MO.Cast <T L0.Entity
  • Specify Properties

The following relations define the basic properties that can be given to the types of our ontology. All properties are functional, i.e. every resource may have at most one statement of the relation.

// Properties
MO.HasYearOfBirth <R L0.HasProperty : L0.FunctionalRelation
    L0.HasDomain MO.Person
    L0.HasRange L0.Integer  
MO.HasAge <R L0.HasProperty : L0.FunctionalRelation
    L0.HasDescription "The age of the character during the movie."
    L0.HasDomain MO.Character
    L0.HasRange L0.Integer  
MO.HasGender <R L0.HasProperty : L0.FunctionalRelation
    L0.HasDescription "The gender of a person or a character."
    L0.HasRange MO.Gender
MO.HasTitle <R L0.HasProperty : L0.FunctionalRelation 
    L0.HasDescription "The English title of the movie."
    L0.HasDomain MO.Movie
    L0.HasRange L0.String
MO.HasName <R L0.HasProperty : L0.FunctionalRelation
    L0.HasDescription "The name of a person or a character."
    L0.HasRange L0.String
  • Specify Gender enumeration

Enumerations are special kind of types: They have a fixed number of instances that are defined in the ontology. Gender is a prototypical example:

// Enumeration
MO.Gender <T L0.Property
    @L0.tag L0.Enumeration
MO.Gender.Male : MO.Gender
MO.Gender.Female : MO.Gender
  • Add the properties to your types

Add properties with @L0.singleProperty or @L0.optionalProperty. They are actually restrictions. The former means that an instance of Character is a valid instance only it has exactly one HasName statement. Optional property requires only that the number of such statements is at most one.

// Constrainting property cardinalities
MO.Character
    @L0.singleProperty MO.HasName
    @L0.optionalProperty MO.HasGender
    @L0.optionalProperty MO.HasAge    
MO.Person
    @L0.singleProperty MO.HasName
    @L0.singleProperty MO.HasGender
    @L0.singleProperty MO.HasYearOfBirth
MO.Cast 
    @L0.singleProperty MO.OfRole
    @L0.singleProperty MO.IsPlayedBy
  • Specify Relations

Specify then the relations of the ontology with their domains, ranges and inverses.

// Relations
MO.IsDirectedBy <R L0.IsRelatedTo
    L0.HasDomain MO.Movie
    L0.HasRange MO.Person
    L0.InverseOf MO.IsDirectorOf <R L0.IsRelatedTo
MO.IsProducedBy <R L0.IsRelatedTo
    L0.HasDomain MO.Movie
    L0.HasRange MO.Person
    L0.InverseOf MO.IsProducerOf <R L0.IsRelatedTo
MO.HasCast <R L0.IsRelatedTo
    L0.HasDomain MO.Movie
    L0.HasRange MO.Cast
    L0.InverseOf MO.IsCastOf <T L0.IsRelatedTo
MO.IsPlayedBy <R L0.IsRelatedTo : L0.FunctionalRelation
    L0.HasDomain MO.Cast
    L0.HasRange MO.Person
    L0.InverseOf MO.IsCastedTo <R L0.IsRelatedTo
MO.OfRole <R L0.IsRelatedTo : L0.FunctionalRelation
    L0.HasDomain MO.Cast
    L0.HasRange MO.Character
    L0.InverseOf MO.HasCasting <R L0.IsRelatedTo
  • Create a template

Casting is a binding between a movie, a character and an actor. To state a casting in a database three statements and one instance is required. To make the code more readable, define a template.

MO.Casting : L0.Template 
    @template %movie %character %actor
        %movie
            MO.HasCast _ : MO.Cast 
                MO.IsPlayedBy %actor
                MO.OfRole %character

Save file. Your Movie.java resource file should now contain a list of resources and URIs. Go on and take a look.

Step 5: Setup Build Properties

build.properties determines which files are included with the binary and the source release. A new ontology plug-in needs some fixing, you must add graph.tg to the binary build and graph-folder to source build. You can make the modification with the form editor.

  • Open build.properties and go to build-page.
  • Check graph.tg under Binary Build.
  • Check graph-folder under Source Build.

If you open the build.properties page, it should look like this.

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\
               graph.tg
src.includes = graph/

Step 6: Download MovieLibrary

We made a Movie Library for initial data. Download: File:MovieLibrary.pgraph and put in graph folder.

If you take a look inside you'll find a root resource at http://www.acme.com/MovieLibrary. All movies, actors and other persons are linked to/from the library with L0.PartOf/L0.ConsistsOf relations.

ML = <http://www.acme.com/MovieLibrary> : L0.Library
    @L0.new

You'll also find an example of the template in use:

ML.tt0830515 : MO.Movie
    @MO.Casting      ML.JamesBond        ML.DanielCraig 
    @MO.Casting      ML.M                ML.JudiDench

Exercises

There are some exercises available related to this tutorial.



Handshake.png Congratulations You have now set-up an ontology.

Give your brains a break before proceeding to the next tutorial.
Next.png Tutorial 2: Project Development