metamodelling.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. Metamodelling
  2. =============
  3. Defining languages in the Modelverse is done through metamodelling.
  4. With metamodelling, a language is itself a model, which can be created through the usual operations.
  5. This explains why language engineers need to have the same knowledge of the Modelverse as modellers: language engineering is modelling in a domain-specific language for languages.
  6. Creating a metamodel
  7. --------------------
  8. To create a metamodel, you have to instantiate the *SimpleClassDiagrams* metamodel.
  9. While there is no restriction that this has to be *SimpleClassDiagrams*, for now we assume that this is the only possible meta-language.
  10. Therefore, to create the PetriNets language, we execute::
  11. >>> model_add("formalisms/PetriNets", "models/SimpleClassDiagrams")
  12. From then on, we can simply use the PetriNets metamodel as if it were a model.
  13. Again, the *model_add* operation can take an optional third parameter, specifying the textual representation of the model.
  14. We will go deeper into this in one of the next sections.
  15. Instantiating a metamodel
  16. -------------------------
  17. To actually define a metamodel, we need to add elements to it.
  18. These elements are tightly related to the metamodel of the model, being most likely *SimpleClassDiagrams*.
  19. In this language, there exist four major components: *Class*, *Association*, *Inheritance*, and attributes.
  20. Class
  21. ^^^^^
  22. Creating a class is easy, and very similar to usual modelling.
  23. To define the *Place* concept, which can later on be used in models, we merely define this as a new class::
  24. >>> instantiate("formalisms/PetriNets", "Class", ID="Place")
  25. Note that here we must define an ID to get a sensible name to use.
  26. Of course, this ID does not need to be used, and the returned (randomized) ID can just as well be used.
  27. Nonetheless, if it is used, it becomes non-intuitive to use in future requests.
  28. In the instance of this newly created language, we can immediately use the new concept::
  29. >>> instantiate("formalisms/PetriNets", "Class", ID="Place")
  30. >>> instantiate("my_pn", "Place")
  31. If the ID was not used, we must remember the ID that was provided to us::
  32. >>> pn_place = instantiate("formalisms/PetriNets", "Class")
  33. >>> instantiate("models/my_pn", pn_place)
  34. While this is more reliable, it becomes difficult to refer to the concept in the future without any clue as to what it resembles.
  35. Later on, this can be solved with concrete syntax, in which case modellers can identify the concepts based on their visual representation.
  36. Association
  37. ^^^^^^^^^^^
  38. Creating a link between two concepts is very similar.
  39. For example, to create the *P2T* association, merely create an instance of association, using the same remarks as before::
  40. >>> instantiate("formalisms/PetriNets", "Association", edge=("Place", "Transition"), ID="P2T")
  41. Inheritance
  42. ^^^^^^^^^^^
  43. A useful concept is the use of inheritance, whereby the inheriting class can take on the responsibilities of the inherited class.
  44. To create this, simply instantiate the *Inheritance* association as usual.
  45. For example, it is possible to structure the *PetriNets* model (in an admittedly strange way) such that there is a generic concept of *NamedElement* and *Arc*::
  46. >>> instantiate("formalisms/PetriNets", "Class", ID="NamedElement")
  47. >>> instantiate("formalisms/PetriNets", "Class", ID="Place")
  48. >>> instantiate("formalisms/PetriNets", "Class", ID="Transition")
  49. >>> instantiate("formalisms/PetriNets", "Inheritance", edge=("Place", "NamedElement"))
  50. >>> instantiate("formalisms/PetriNets", "Inheritance", edge=("Transition", "NamedElement"))
  51. In the Modelverse, inheritance is possible between any two instances, including associations.
  52. To define the *Arc* relation, we act similar::
  53. >>> instantiate("formalisms/PetriNets", "Association", edge=("NamedElement", "NamedElement"), ID="Arc")
  54. >>> instantiate("formalisms/PetriNets", "Association", edge=("Place", "Transition"), ID="P2T")
  55. >>> instantiate("formalisms/PetriNets", "Association", edge=("Transition", "Place"), ID="T2P")
  56. >>> instantiate("formalisms/PetriNets", "Inheritance", edge=("P2T", "Arc"))
  57. >>> instantiate("formalisms/PetriNets", "Inheritance", edge=("T2P", "Arc"))
  58. Instantiating attributes
  59. ^^^^^^^^^^^^^^^^^^^^^^^^
  60. Instantiating attributes is similar as it was before::
  61. >>> attr_assign("formalisms/PetriNets", "NamedElement", "name", "NamedElement")
  62. >>> attr_assign("formalisms/PetriNets", "Transition", "name", "Transition")
  63. >>> attr_assign("formalisms/PetriNets", "Place", "name", "Place")
  64. Note that this *name* attribute is in no way related to the *name* attributes at the *my_pn* level.
  65. Indeed, the *name* in *PetriNets* specifies the name of the class.
  66. Other attributes that can be set on classes are:
  67. 1. *lower_cardinality* specifies the number (integer) of instances that must at least exist of this element.
  68. 2. *upper_cardinality* specifies the number (integer) of instances that must at most exist of this element.
  69. 3. *constraint* specifies additional constraints on the instances of this class.
  70. 4. *abstract* specifies whether the class is to be abstract or not.
  71. For associations, we have the same three attributes, augmented with:
  72. 5. *source_lower_cardinality* specifies the number (integer) of incoming edges that are at least required for the target.
  73. 6. *source_upper_cardinality* specifies the number (integer) of incoming edges that are at most required for the target.
  74. 7. *target_lower_cardinality* specifies the number (integer) of outgoing edges that are at least required for the source.
  75. 8. *target_upper_cardinality* specifies the number (integer) of outgoing edges that are at most required for the source.
  76. Constraints
  77. ^^^^^^^^^^^
  78. A constraint specifies additional constraints on the instances of this model, which could not be expressed before.
  79. We distinguish between two types of constraints: local and global constraints.
  80. Local constraints are defined at the level of a single class or association.
  81. For example, if the number of tokens must always be non-negative, this can be specified as follows::
  82. >>> attr_assign_code("formalisms/PetriNets", "Place", "constraint", \
  83. ... """
  84. ... String function constraint(model : Element, name : String):
  85. ... if (integer_gte(read_attribute(model, name, "tokens"), 0)):
  86. ... return "OK"!
  87. ... else:
  88. ... return "Negative number of tokens specified"!
  89. Global constraints operate over the complete model, and are therefore not attached to a single element.
  90. Instead, a specific *GlobalConstraint* element must be instantiated.
  91. This element has a *constraint* attribute, which specifies the constraint to evaluate in the global context.
  92. For example, when, for some reason, the number of places must always be larger than the number of transitions::
  93. >>> constraint = instantiate("formalisms/PetriNets", "GlobalConstraint")
  94. >>> attr_assign_code("formalisms/PetriNets", constraint, "constraint", \
  95. ... """
  96. ... String function constraint(model : Element):
  97. ... if (set_len(allInstances(model, "Place")) > set_len(allInstances(model, "Transition"))):
  98. ... return "OK"!
  99. ... else:
  100. ... return "Needs more places than transitions"!
  101. While all constraints can be specified as global constraints, using local constraints is preferred.
  102. .. note::
  103. Currently, all constraints are only evaluated upon explicit user request.
  104. More specifically: during the *verify* operation.
  105. Defining attributes
  106. ^^^^^^^^^^^^^^^^^^^
  107. Defining attributes is an important consideration in a metamodel.
  108. Up to now, our *Place* and *Transition* were just nodes without any attributes.
  109. A meaningful (marked) petrinet has tokens in places, and names for the places and transitions.
  110. To define these attributes, we must take two steps:
  111. 1. Define the type that will be used (e.g., String, Integer)
  112. 2. Define the name and type of the attribute (e.g., *tokens* is an Integer, *name* is a String)
  113. To define the type that we want to use, we must manually define it.
  114. While many other tools provide a set of primitives, which cannot be altered in any way, the Modelverse leaves all this up to the language engineer.
  115. If the language engineer wants the concepts of a *Natural*, for example, it is not required that the attribute is specified as an Integer, with an additional constraint on each and every attribute that is defined this way.
  116. It is possible to define a new attribute type as follows::
  117. >>> instantiate("formalisms/PetriNets", "SimpleAttribute", ID="Natural")
  118. But now, the attribute is constrained in no way: it is merely called Natural, but can even contain a string.
  119. The attribute type must be constrained::
  120. >>> attr_assign_code("formalisms/PetriNets", "Natural", "constraint", \
  121. ... """
  122. ... String function constraint(value : Element):
  123. ... if (is_physical_integer(value)):
  124. ... if (integer_gte(value, 0)):
  125. ... return "OK"!
  126. ... else:
  127. ... return "Not a positive value"!
  128. ... else:
  129. ... return "Not a numeric value!"!
  130. Now, the *Natural* is correctly specified and can be used throughout the model.
  131. All uses of *Natural* will now make sure that the value is positive.
  132. This offers additional possibilities, such as defining complex attributes as if they were primitives, for example complex numbers.
  133. The next step is to define the attribute itself.
  134. This can be easily done as follows::
  135. >>> define_attribute("formalisms/PetriNets", "Place", "tokens", "Natural")
  136. For the name, *String* can be defined similarly (using *is_physical_string* and omitting the check greater than 0).