modellanguage.rst 5.5 KB

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