1.4. Action Language : A model-oriented language

[Warning]Text not verified for kermeta 2

As explained in the Preface and in Architecture , Kermeta extends the MOF. It provides useful means to manipulate models. The support of model introduces the main difference with "more" traditional programming languages.

1.4.1. Associations : toward a first concrete example of a Kermeta model

Association is one of the key concepts when using and defining models. It is obviously part of Kermeta.

MOF defines the concept of "Property" which generalizes the notions of attributes, and associations (composite or not) that you can find in UML. Kermeta syntax also distinguishes these two notions as introduced in Section 2.16, “ Class properties ” .

As a reminder, the attribute keyword defines a link with containment (a composite association) whereas the reference keyword just defines an association. As you can see, property declarations are very close to variable declarations introduced in Section 2.6, “ Using Variables ). Each reference may be explicitly linked to another reference (it is the opposite concept in MOF terminology – see also section Section 2.16.1, “ Attributes (attribute) , references (reference) ).

class Library
{
    attribute books : set Book[0..*]
}

class Book
{
    attribute title : String
    attribute subtitle : String

    reference authors : oset Author[1..*]#works
}


class Author
{
    attribute name : String
    attribute lastName : String
    
    reference works : set Book[0..*]#authors
}

If we represent our Kermeta model in a graphical syntax we obtain the following class diagram ( Figure 1.2, “A concrete example : a library” ).

A concrete example : a library

Figure 1.2. A concrete example : a library


1.4.2. Loading an existing model

Using Eclipse Modeling Framework (EMF), Kermeta can load and save models done with other EMF tools.

/* Initialize the EMF repository */
var repository : EMFRepository init EMFRepository.new

/* Create an EMF Resource, given model and metamodel URIs as String */
var resource : Resource init repository.createResource(myModelURI, itsMetamodelURI)

/* Load the resource */
resource.load

// get elements from the resource
// in this sample, you know that your root element is always a Library, 
// so you can directly get the first one
var aLibrary : Library
aLibrary ?= resource.one // note the conditional assignment using the ?=, if not a Library you'll get Void

In the same way, you can serialize a model, or load, change and save an existing model.

[Caution]Caution

Your model URI MUST be of the form "platform:/resource/myProject/myModel" or "platform:/plugin/myProject/myModel".

Your metamodel URI MUST be of the form "platform:/resource/myProject/myModel" or "platform:/plugin/myProject/myModel" or an URI registered in the EMF registry.

[Caution]Caution

Be aware that you CANNOT load kermeta text files (*.kmt). Only xmi files are allowed to be loaded. Parsing and obtaining a model from a textual syntax is not part of Kermeta. This is the role of other tools (like sintaks). Technically, it is possible to create some Kermeta operation that will hide this step, however, this is not the goal of this manual to explain this procedure.

1.4.3. Navigation in a model

Actually, navigating in a model is as simple as using objects in an object-oriented program. However, several features have been added in order to ease this activity.

For example, thanks to the lambda expressions, the collections of the language are easily manipulated using lexical closure (select, collect, each, etc). This applies to all the collections of the language, the one you may define directly but also the one which are used when an Attribute or Reference has a multiplicity greater than 1.

Example (based on the library sample of Section 1.4.1, “Associations : toward a first concrete example of a Kermeta model ” ):

var smithBooks : Set<Book> init Set<Book>.new
smithBooks.addAll(
	lib.books.select{aLibraryBook | 
		aLibraryBook.authors.exists{aBookAuthor | aBookAuthor.lastName == "Smith"}})
		

In the example above, lib is an instance of Library. It searchs in the books, select the books where the author last name is "Smith".

1.4.4. Model type

In order to improve reuse of existing code between metamodel variants, the language introduces the notion of ModelType. It is based on the notion of conformance between two metamodels. This allows to write behavior that is valid for a given metamodel and that will also work for any conformant metamodel.

TODO write a small illustrative example of a simple printer based on a ModelType : a subset of class diagram of UML

1.4.5. Kermeta model reflexively available

Kermeta has been developed, using MDE principles so it also provides its own metamodel (reflectiveley available). Details of Kermeta metamodel is available in Chapter 3, Kermeta Metamodel

1.4.6. Corresponding sections in the Reference chapter

You can get more informations about all Kermeta model-oriented features in the Chapter 2, Reference . More precisely in