modellanguage.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 mostly 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. Import
  13. ^^^^^^
  14. An import pulls in a model and makes it accessible using a simpler identifier.
  15. The structure is as follows::
  16. import path/in/modelverse as my_model
  17. After this import, the model that was previously exported to *path/in/modelverse* becomes available as *my_model*.
  18. Include
  19. ^^^^^^^
  20. When combined with action language, the action language might require some includes of header files.
  21. To do this, the includes can be placed at the top of the hierarchy.
  22. Model
  23. ^^^^^
  24. Every model is defined by first specifying the type of model to instantiate, followed by the name of the model to create.
  25. The name of the model can then later on be referenced in other models (as type, or for exporting).
  26. Within the model, all names of the type model can be used as types again.
  27. Instance
  28. ^^^^^^^^
  29. A model consists of some instances.
  30. These instances are instances of types specified by the model that is the metaclass of the current model.
  31. Attribute
  32. ^^^^^^^^^
  33. Each model instance can contain attributes.
  34. There are two kinds of attributes: defining attributes, or value attributes.
  35. For defining attributes, the structure is as follows::
  36. Class A{
  37. my_parameter : ParameterType
  38. }
  39. This defines a parameter called *my_parameter* which is typed by *ParameterType*.
  40. *ParameterType* must always be a type defined in the type that is being instantiated.
  41. Even if this is a primitive, such as *Integer*, the metamodel must define what it calls an *Integer*.
  42. While this might seem bothersome, this clearly defines the notion of what a type means, without having to resort to the implementation.
  43. Value attributes are similar, but have a different syntax, and contain an actual value.
  44. Their structure is as follows::
  45. A a{
  46. my_parameter = 1
  47. }
  48. They assign a value to a previously defined attribute.
  49. In this case, it is important that the value conforms to the type specified in the defining attribute.
  50. Merge with Action Language
  51. --------------------------
  52. It is of course possible to incorporate action language inside of a model.
  53. A typical use case for this is the definition of constraints or actions.
  54. Action language is surrounded by dollar signs ($), which causes the parser to redirect the text between dollar signs to the action language parser.
  55. All includes necessary for the compilation of the action code, must be provided at the top of the document.
  56. Examples
  57. --------
  58. Some simple examples of models are provided below.
  59. 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.
  60. Petri Net metamodel
  61. ^^^^^^^^^^^^^^^^^^^
  62. A simple Petri Net metamodel can be created, based on the SimpleClassDiagrams metamodel.
  63. This looks like this::
  64. import models/SimpleClassDiagrams as SCD
  65. SCD PetriNets{
  66. SimpleAttribute Natural {}
  67. Class Place{
  68. tokens : Natural
  69. }
  70. Class Transition{}
  71. Association P2T (Place, Transition) {
  72. weight : Natural
  73. }
  74. Association T2P (Transition, Place) {
  75. weight : Natural
  76. }
  77. }
  78. export PetriNets to models/PetriNets
  79. Note that in this metamodel, there is no constraint placed on the value of a Natural: it can literaly be anything.
  80. That is why usually, you want to place constraints on the value.
  81. In this case, the value needs to be an integer, and it must be larger than or equal to zero.
  82. Such constraints are written in the action language, surrounded by dollar signs::
  83. import models/SimpleClassDiagrams as SCD
  84. include "primitives.alh"
  85. SCD PetriNets{
  86. SimpleAttribute Natural {
  87. $
  88. if (bool_not(is_physical_int(self))):
  89. return "Natural has no integer value at " + name!
  90. elif (integer_lt(self, 0)):
  91. return "Natural does not have a positive or zero value at " + name!
  92. else:
  93. return "OK"!
  94. $
  95. }
  96. Class Place{
  97. tokens : Natural {
  98. target_lower_cardinality = 1
  99. target_upper_cardinality = 1
  100. }
  101. }
  102. Class Transition{}
  103. Association P2T (Place, Transition) {
  104. weight : Natural {
  105. target_lower_cardinality = 1
  106. target_upper_cardinality = 1
  107. }
  108. }
  109. Association T2P (Transition, Place) {
  110. weight : Natural {
  111. target_lower_cardinality = 1
  112. target_upper_cardinality = 1
  113. }
  114. }
  115. }
  116. Petri Net instance
  117. ^^^^^^^^^^^^^^^^^^
  118. The previous metamodel can then be instantiated::
  119. import models/PetriNets as PetriNets
  120. PetriNets my_petrinet {
  121. Place p1 {
  122. tokens = 1
  123. }
  124. Place p2 {
  125. tokens = 3
  126. }
  127. Transition t1 {}
  128. P2T (p1, t1) {
  129. weight = 1
  130. }
  131. T2P (t1, p2) {
  132. weight = 2
  133. }
  134. }
  135. export my_petrinet to models/my_petrinet
  136. Use in interactive interface
  137. ----------------------------
  138. The interactive interface, such as the *prompt.py* script, can also invoke the model compiler.
  139. 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.