Norwegian University of Science and Technology

Global navigation


Using Javascript with Diamodl and Ecore

The Diamodl editor and runtime support using Javascript (by means of the GNU Rhino engine) for implementing functions and actions (e.g. enter and exit actions). The Javascript engine has been extended with support for Ecore, so Ecore classes and objects may be referred to and used in a natural way.

Javascript provides three means for navigating in data: in-scope variables, properties and indices. By utilising these, we can make Diamodl and Ecore model elements first-class citizens in Javascript.

Property access

Property access in Javascript uses dot or bracket syntax: .<property> or ['<property>'], e.g. person.name or person['name'] both access the 'name' property of the object referred to by the person variable. The bracket syntax is mostly used when the property name contains special characters, like space, or is a reserved word.

Property access on Ecore objects are interpreted in two ways. First, it is used to refer to attributes and references (associations in UML terminology), resulting in either a simple value or a List (usually EList). Second, if there is no attribute or reference, the content of an object is searched through, looking for one with a 'name' attribute value equal to the property. This is a very useful shortcut, since many common classes defines a 'name' attribute. Among others, Ecore package (EPackage) objects contains a set of classes (EClass objects) with a name attribute. This means that aPackage.aClass accesses the class named 'aClass' in the package referenced by the 'aPackage' variable. Similarly, all Diamodl flow nodes have a name attribute and may be referenced as properties of their parents. E.g. interactor.is accesses the input-send (named 'is') gate of interactor.

Index access

Index access in Javascript uses the bracket syntax: [n], e.g. persons[0] accesses the first object in the object referenced by the 'persons' variable. This works for arrays and lists, but may also be implemented for other kinds of objects.

Index access on Ecore objects accesses the content list of the object. E.g. if a mail folder ('MailFolder' object) contains messages ('Message' objects), folder[1] will access the second message in the object referenced by the 'folder' variable.

Pre-defined variables

The Ecore packages that are loaded are available as variables in the scope that Javascripts are executed in. By using property access, as described above, it is easy to locate specific Ecore classes.

Similarly, the top-level Diamodl interactor or diagram may be accessed by its name.

Ecore-specific functions

A small set of functions for operating on Ecore model element (e.g. classes) are defined.

The 'is' function tests whether the second argument is an instance of the first argument, which must be an Ecore class (EClass). In addition to the two-argument 'is' function, there is a one-argument 'is-<name>' function defined for each 'name' class, that tests whether the argument is of that class. E.g. the isPerson functions tests whether its argument is an instance of the 'Person' class.

The 'create' function creates an instance of the first argument, which must be an Ecore class, initialised with the properties defined in the second argument, which must be a Map. I.e. an instance of the class is created, and for each entry in the Map, the property named by the entry's key is set to the entry's value. In addition to the two-argument 'create' function, there is a one-argument 'create-<name>' function defined for each 'name' class, that creates an instance of that class. E.g. the createPerson functions creates an instance of the 'Person' class, initialised with the properties defined by the Map argument.




2007/11/20 11:27, Hallvard Trætteberg
NTNU, NO-7491 Trondheim. Telephone: +47 73 59 50 00. Contact us
Editorial responsibility: hal@idi.ntnu.no