Tutorial: Ontology Development
"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.
Related documents:
- Complete Graph File Format
- As ontology developer you must understand ontology version migration
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 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 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 www.acme.com and ontology name Movie, and the initial 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://www.acme.com> : 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://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.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: 0.1.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
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.HasYearOfBirth <R L0.HasProperty L0.HasDomain MO.Person L0.HasRange L0.Integer MO.HasAge <R L0.HasProperty L0.HasDomain MO.Character L0.HasRange L0.Integer MO.HasGender <R L0.HasProperty L0.HasRange MO.Gender MO.HasTitle <R L0.HasProperty // The english title L0.HasDomain MO.Movie 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 do it 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.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 Role is a valid instance only it has a single HasAge relation to an integer literal.
// Constrainting property cardinalities MO.Character @L0.singleProperty MO.HasAge @L0.singleProperty MO.HasGender MO.Person @L0.singleProperty MO.HasYearOfBirth @L0.singleProperty MO.HasGender
- Specify Relations
// 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.HasDomain MO.Cast L0.HasRange MO.Person L0.InverseOf MO.IsCastedTo <R L0.IsRelatedTo MO.OfRole <R L0.IsRelatedTo 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://com.acme/MovieLibrary. All movies, actors and other persons are linked to/from the library with L0.PartOf/L0.ConsistsOf relations.
db = <http://com.acme/MovieLibrary> : L0.Library @L0.new
You'll also find an example of the template in use:
db.tt0381061 : mo.Movie @mo.Casting db."James Bond" db."Daniel Craig" @mo.Casting db."M" db."Judi Dench"
Give your brains a break before proceeding to the next tutorial.
Tutorial 2: Project Development