Difference between revisions of "Tutorial: Ontology Development"

From Developer Documents
Jump to navigation Jump to search
m
m
Line 29: Line 29:
  
 
The ontology has a representing resource, an instance of L0.Ontology. Don't forget to tell the compiler to create Resource file aswell.
 
The ontology has a representing resource, an instance of L0.Ontology. Don't forget to tell the compiler to create Resource file aswell.
   Movie = <http://com.acme/Movie-1.0> : L0.Ontology
+
   mo = <http://com.acme/Movie-1.0> : L0.Ontology
 
       L0.HasResourceClass "com.acme.ontology.Movie"
 
       L0.HasResourceClass "com.acme.ontology.Movie"
  
Line 54: Line 54:
  
 
* Specify Types
 
* Specify Types
First we need to specify object types for the concepts ''Movie'', ''Role'' 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).
+
First we need to specify object types for the concepts ''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 between Person (actor) and Character.  
  
 
  // Types
 
  // Types
  Movie.Movie <T L0.Entity
+
  mo.Movie <T L0.Entity
  Movie.Role <T L0.Entity
+
  mo.Character <T L0.Entity
  Movie.Person <T L0.Entity
+
  mo.Person <T L0.Entity
 +
mo.Cast <T L0.Entity
  
 
* Specify Properties
 
* Specify Properties
 
 
  // Properties
 
  // Properties
  Movie.HasDobYear <R L0.HasProperty
+
  mo.HasDobYear <R L0.HasProperty
 +
    L0.HasRange L0.Integer 
 +
mo.HasAge <R L0.HasProperty
 
     L0.HasRange L0.Integer   
 
     L0.HasRange L0.Integer   
  Movie.HasAge <R L0.HasProperty
+
  mo.HasGender <R L0.HasProperty
    L0.HasRange L0.Double 
+
     L0.HasRange mo.Gender
Movie.HasGender <R L0.HasProperty
+
  mo.HasTitle <R L0.HasProperty // The english title
     L0.HasRange Movie.Gender
 
  Movie.HasTitle <R L0.HasProperty // The english title
 
 
     L0.HasRange L0.String
 
     L0.HasRange L0.String
 
  
 
* Specify Literals
 
* Specify Literals
Gender is a special case, since it is not defined in Layer0 you must define yourself. The syntax is defined in [[Databoard_Specification#Datatypes|Datatype documentation]]. In it you will find out the format for your enumeration is ''|Male|Female''.
+
Numberic primitives are defined in Layer0, eg. L0.Integer. Gender is a special case, since it is not defined in Layer0 you must define yourself. The syntax is defined in [[Databoard_Specification#Datatypes|Datatype documentation]]. In it you will find out the format for your enumeration is ''|Male|Female''.
  
 
  // Literals
 
  // Literals
  Movie.Gender <T L0.Literal
+
  mo.Gender <T L0.Literal
 
     @L0.assert L0.HasDataType $(|Male|Female)
 
     @L0.assert L0.HasDataType $(|Male|Female)
  
Line 86: Line 85:
 
Name property of a movie will be its  (''L0.HasName'') and therefore does not need explicit statement.
 
Name property of a movie will be its  (''L0.HasName'') and therefore does not need explicit statement.
  
  Movie.Role
+
  // Add properties
     @L0.singleProperty Movie.HasAge
+
mo.Character
     @L0.singleProperty Movie.HasGender
+
     @L0.singleProperty mo.HasAge
  Movie.Person
+
     @L0.singleProperty mo.HasGender
     @L0.singleProperty Movie.HasDobYear
+
  mo.Person
     @L0.singleProperty Movie.HasGender
+
     @L0.singleProperty mo.HasDobYear
 +
     @L0.singleProperty mo.HasGender
  
  
 
* Specify Relations
 
* Specify Relations
 
  // Relations
 
  // Relations
  Movie.IsDirectedBy <R L0.IsRelatedTo
+
  mo.IsDirectedBy <R L0.IsRelatedTo
     L0.HasDomain Movie.Movie
+
     L0.HasDomain mo.Movie
     L0.HasRange Movie.Person
+
     L0.HasRange mo.Person
  Movie.IsDirectorOf <R L0.IsRelatedTo
+
  mo.IsDirectorOf <R L0.IsRelatedTo
     L0.InverseOf Movie.IsDirectedBy
+
    L0.HasDomain mo.Person
     L0.HasDomain Movie.Person
+
    L0.HasRange mo.Movie
     L0.HasRange Movie.Movie
+
     L0.InverseOf mo.IsDirectedBy
 +
 +
mo.IsProducedBy <R L0.IsRelatedTo
 +
     L0.HasDomain mo.Movie
 +
    L0.HasRange mo.Person
 +
mo.IsProducerOf <R L0.IsRelatedTo
 +
    L0.HasDomain mo.Movie
 +
     L0.HasRange mo.Person
 +
    L0.InverseOf mo.IsProducedBy
 
   
 
   
  Movie.IsWrittenBy <R L0.IsRelatedTo
+
  mo.HasCast <R L0.IsRelatedTo
     L0.HasDomain Movie.Movie
+
     L0.HasDomain mo.Movie
    L0.HasRange Movie.Person
+
     L0.HasRange mo.Cast
Movie.HasWritten <R L0.IsRelatedTo
 
    L0.InverseOf Movie.IsWrittenBy
 
    L0.HasDomain Movie.Person
 
     L0.HasRange Movie.Movie
 
 
   
 
   
  Movie.IsProducedBy <R L0.IsRelatedTo
+
  mo.IsPlayedBy <R L0.IsRelatedTo
     L0.HasDomain Movie.Movie
+
     L0.HasDomain mo.Cast
     L0.HasRange Movie.Person
+
     L0.HasRange mo.Person
  Movie.IsProducerOf <R L0.IsRelatedTo
+
  mo.IsCastedTo <R L0.IsRelatedTo
     L0.InverseOf Movie.IsProducedBy
+
     L0.HasDomain mo.Person
     L0.HasDomain Movie.Movie
+
     L0.HasRange mo.Cast
     L0.HasRange Movie.Person
+
     L0.InverseOf mo.IsPlayedBy
 
   
 
   
  Movie.Played <R L0.IsRelatedTo
+
  mo.OfRole <R L0.IsRelatedTo
     L0.HasDomain Movie.Person
+
     L0.HasDomain mo.Cast
     L0.HasRange Movie.Role
+
     L0.HasRange mo.Character
  Movie.IsPlayedBy <R L0.IsRelatedTo
+
  mo.HasCasting <R L0.IsRelatedTo
     L0.InverseOf Movie.Played
+
     L0.InverseOf mo.OfRole
     L0.HasDomain Movie.Role
+
     L0.HasDomain mo.Character
     L0.HasRange Movie.Person
+
     L0.HasRange mo.Cast
  
* Create Library for movie database
+
* Create Library for movie, actor and director database
 
  // Library - Consists of Movies and Persons
 
  // Library - Consists of Movies and Persons
 
  Movie.Library : L0.Library
 
  Movie.Library : L0.Library
  
 +
* Create 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.  
 
Save file. Your Movie.java resource file should now contain a list of resources and URIs. Go on and take a look.  

Revision as of 13:47, 20 October 2010

"What's up folks." Today we are going to create a movie database ontology for a client of your Acme company.

Step 1: Setup Ontology Plug-in

Ontologies are encapsulated in OSGi bundles. In eclipse we develop them as equinox plug-ins. An ontology bundle is a package that contains sources (graph/*.pgraph), compiled graph file (graph.tg), and Java resource 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 >
  • In the following 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 >
  • Your company domain is com.acme.movie and ontology name Movie, and the inital version is 1.0. Finish

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

Step 2: Initialize Ontology

Open Movie.pgraph and start editing.

As you are using Layer0 concepts, it is a good idea to capture its namespace:

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

Your acme company has a namespace. You are creating a new one so it must be stated.

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

The ontology has a representing resource, an instance of L0.Ontology. Don't forget to tell the compiler to create Resource file aswell.

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

Step 3: Export package

Now that you have some content, graph compiler has created a Resource class com.acme.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: Ontology
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.ontology

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. Good, open Movie.pgraph again.

  • Specify Types

First we need to specify object types for the concepts 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 between Person (actor) and Character.

// Types
mo.Movie <T L0.Entity
mo.Character <T L0.Entity
mo.Person <T L0.Entity
mo.Cast <T L0.Entity
  • Specify Properties
// Properties
mo.HasDobYear <R L0.HasProperty
    L0.HasRange L0.Integer  
mo.HasAge <R L0.HasProperty
    L0.HasRange L0.Integer  
mo.HasGender <R L0.HasProperty
    L0.HasRange mo.Gender
mo.HasTitle <R L0.HasProperty // The english title
    L0.HasRange L0.String
  • Specify Literals

Numberic primitives are defined in Layer0, eg. L0.Integer. Gender is a special case, since it is not defined in Layer0 you must define yourself. The syntax is defined in Datatype documentation. In it you will find out the format for your enumeration is |Male|Female.

// Literals
mo.Gender <T L0.Literal
    @L0.assert L0.HasDataType $(|Male|Female)
  • 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 Role is a valid instance only it has a single HasAge relation to an integer literal.

Name property of a movie will be its (L0.HasName) and therefore does not need explicit statement.

// Add properties
mo.Character
    @L0.singleProperty mo.HasAge
    @L0.singleProperty mo.HasGender
mo.Person
    @L0.singleProperty mo.HasDobYear
    @L0.singleProperty mo.HasGender


  • Specify Relations
// Relations
mo.IsDirectedBy <R L0.IsRelatedTo
    L0.HasDomain mo.Movie
    L0.HasRange mo.Person
mo.IsDirectorOf <R L0.IsRelatedTo
    L0.HasDomain mo.Person
    L0.HasRange mo.Movie
    L0.InverseOf mo.IsDirectedBy

mo.IsProducedBy <R L0.IsRelatedTo
    L0.HasDomain mo.Movie
    L0.HasRange mo.Person
mo.IsProducerOf <R L0.IsRelatedTo
    L0.HasDomain mo.Movie
    L0.HasRange mo.Person
    L0.InverseOf mo.IsProducedBy

mo.HasCast <R L0.IsRelatedTo
    L0.HasDomain mo.Movie
    L0.HasRange mo.Cast

mo.IsPlayedBy <R L0.IsRelatedTo
    L0.HasDomain mo.Cast
    L0.HasRange mo.Person
mo.IsCastedTo <R L0.IsRelatedTo
    L0.HasDomain mo.Person
    L0.HasRange mo.Cast
    L0.InverseOf mo.IsPlayedBy

mo.OfRole <R L0.IsRelatedTo
    L0.HasDomain mo.Cast
    L0.HasRange mo.Character
mo.HasCasting <R L0.IsRelatedTo
    L0.InverseOf mo.OfRole
    L0.HasDomain mo.Character
    L0.HasRange mo.Cast
  • Create Library for movie, actor and director database
// Library - Consists of Movies and Persons
Movie.Library : L0.Library
  • Create 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 what files are released with the. 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/



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