languageengineer_modellanguage.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. Modelling Language
  2. ==================
  3. Apart from the action language, the Modelverse also has a modelling language.
  4. With this language, models (*i.e.*, data) can be defined in addition to the algorithms.
  5. This language is still work in progress, but should be decent enough to construct simple metamodels and instantiate them.
  6. All models have the extension \*.mvc, indicating that they are models and should be compiled as such.
  7. If you want to create models interactively, such as with another tool, it is strongly recommended to use the interactive interface to do this, as the modelling language is completely static.
  8. Language description
  9. --------------------
  10. The modelling language defines data structures, which will be formed as models in the Modelverse.
  11. Several constructs are supported.
  12. Include
  13. ^^^^^^^
  14. When combined with action language, for example with code fragments, the action language might require some includes of header files.
  15. To do this, the includes can be placed at the top of the hierarchy.
  16. The structure is as follows::
  17. include "primitives.alh"
  18. Model
  19. ^^^^^
  20. Every model is defined by first specifying the type of model to instantiate, followed by the name of the model to create.
  21. The name of the model can then later on be referenced in other models (as type, or for exporting).
  22. Within the model, all names of the type model can be used as types again.
  23. This is only used internally for bootstrapping purposes, so feel free to write whatever you want here.
  24. The structure is as follows::
  25. SimpleClassDiagrams PetriNets{
  26. ...
  27. }
  28. All further operations happen inside such a definition.
  29. Instance
  30. ^^^^^^^^
  31. A model consists of some instances.
  32. These instances are instances of types specified by the model that is the metaclass of the current model.
  33. The structure is as follows::
  34. SimpleClassDiagrams PetriNets {
  35. Class Place {
  36. ...
  37. }
  38. }
  39. Attribute
  40. ^^^^^^^^^
  41. Each model instance can contain attributes.
  42. There are two kinds of attributes: defining attributes, or value attributes.
  43. For defining attributes, the structure is as follows::
  44. Class A{
  45. my_parameter : ParameterType
  46. }
  47. This defines a parameter called *my_parameter* which is typed by *ParameterType*.
  48. *ParameterType* must always be a type defined in the type that is being instantiated.
  49. Even if this is a primitive, such as *Integer*, the metamodel must define what it calls an *Integer*.
  50. While this might seem bothersome, this clearly defines the notion of what a type means, without having to resort to the implementation.
  51. Value attributes are similar, but have a different syntax, and contain an actual value.
  52. Their structure is as follows::
  53. A a{
  54. my_parameter = 1
  55. }
  56. They assign a value to a previously defined attribute.
  57. In this case, it is important that the value conforms to the type specified in the defining attribute.
  58. Merge with Action Language
  59. --------------------------
  60. It is of course possible to incorporate action language inside of a model.
  61. A typical use case for this is the definition of constraints or actions.
  62. Action language is surrounded by dollar signs ($), which causes the parser to redirect the text between dollar signs to the action language parser.
  63. All includes necessary for the compilation of the action code, must be provided at the top of the document.
  64. Examples
  65. --------
  66. Some simple examples of models are provided below.
  67. This code only makes the specified models available in code; to do something with these models, an algorithms has to read out the model and operate on it.
  68. Petri Net metamodel
  69. ^^^^^^^^^^^^^^^^^^^
  70. A simple Petri Net metamodel can be created, based on the SimpleClassDiagrams metamodel.
  71. This looks like this::
  72. import models/SimpleClassDiagrams as SCD
  73. SCD PetriNets{
  74. SimpleAttribute Natural {}
  75. Class Place{
  76. tokens : Natural
  77. }
  78. Class Transition{}
  79. Association P2T (Place, Transition) {
  80. weight : Natural
  81. }
  82. Association T2P (Transition, Place) {
  83. weight : Natural
  84. }
  85. }
  86. export PetriNets to models/PetriNets
  87. Note that in this metamodel, there is no constraint placed on the value of a Natural: it can literaly be anything.
  88. That is why usually, you want to place constraints on the value.
  89. In this case, the value needs to be an integer, and it must be larger than or equal to zero.
  90. Such constraints are written in the action language, surrounded by dollar signs::
  91. import models/SimpleClassDiagrams as SCD
  92. include "primitives.alh"
  93. SCD PetriNets{
  94. SimpleAttribute Natural {
  95. $
  96. if (bool_not(is_physical_int(self))):
  97. return "Natural has no integer value at " + name!
  98. elif (integer_lt(self, 0)):
  99. return "Natural does not have a positive or zero value at " + name!
  100. else:
  101. return "OK"!
  102. $
  103. }
  104. Class Place{
  105. tokens : Natural {
  106. target_lower_cardinality = 1
  107. target_upper_cardinality = 1
  108. }
  109. }
  110. Class Transition{}
  111. Association P2T (Place, Transition) {
  112. weight : Natural {
  113. target_lower_cardinality = 1
  114. target_upper_cardinality = 1
  115. }
  116. }
  117. Association T2P (Transition, Place) {
  118. weight : Natural {
  119. target_lower_cardinality = 1
  120. target_upper_cardinality = 1
  121. }
  122. }
  123. }
  124. Petri Net instance
  125. ^^^^^^^^^^^^^^^^^^
  126. The previous metamodel can then be instantiated::
  127. import models/PetriNets as PetriNets
  128. PetriNets my_petrinet {
  129. Place p1 {
  130. tokens = 1
  131. }
  132. Place p2 {
  133. tokens = 3
  134. }
  135. Transition t1 {}
  136. P2T (p1, t1) {
  137. weight = 1
  138. }
  139. T2P (t1, p2) {
  140. weight = 2
  141. }
  142. }
  143. export my_petrinet to models/my_petrinet
  144. Use in interactive interface
  145. ----------------------------
  146. The interactive interface, such as the *prompt.py* script, can also invoke the model compiler.
  147. In this case, however, the import, export, and model type and model name parameters are ignored as they have to be defined and checked in the bigger context of the MvC.
  148. Internal use only
  149. -----------------
  150. Some constructs only make sense in the context of the bootstrap generation.
  151. These constructs can still be used at all times, but are meaningless.
  152. Import
  153. ^^^^^^
  154. An import pulls in a model and makes it accessible using a simpler identifier.
  155. The structure is as follows::
  156. import path/in/modelverse as my_model
  157. After this import, the model that was previously exported to *path/in/modelverse* becomes available as *my_model*.