metamodels.alc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. include "primitives.alh"
  2. include "object_operations.alh"
  3. include "library.alh"
  4. include "conformance_scd.alh"
  5. include "modelling.alh"
  6. Element function constraint_natural(model : Element, name : String):
  7. if (is_physical_int(model["model"][name])):
  8. if (integer_gte(model["model"][name], 0)):
  9. return "OK"!
  10. else:
  11. return "Natural number not larger than or equal to zero"!
  12. else:
  13. return "Natural has non-integer instance"!
  14. Element function constraint_string(model : Element, name : String):
  15. if (is_physical_string(model["model"][name])):
  16. return "OK"!
  17. else:
  18. return "String has non-string instance"!
  19. Element function constraint_boolean(model : Element, name : String):
  20. if (is_physical_boolean(model["model"][name])):
  21. return "OK"!
  22. else:
  23. return "Boolean has non-boolean instance"!
  24. Element function constraint_if(model : Element, name : String):
  25. if (is_physical_action(model["model"][name])):
  26. if (cast_a2s(model["model"][name]) == "if"):
  27. return "OK"!
  28. else:
  29. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  30. else:
  31. return "Expected physical action value"!
  32. Element function constraint_while(model : Element, name : String):
  33. if (is_physical_action(model["model"][name])):
  34. if (cast_a2s(model["model"][name]) == "while"):
  35. return "OK"!
  36. else:
  37. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  38. else:
  39. return "Expected physical action value"!
  40. Element function constraint_break(model : Element, name : String):
  41. if (is_physical_action(model["model"][name])):
  42. if (cast_a2s(model["model"][name]) == "break"):
  43. return "OK"!
  44. else:
  45. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  46. else:
  47. return "Expected physical action value"!
  48. Element function constraint_continue(model : Element, name : String):
  49. if (is_physical_action(model["model"][name])):
  50. if (cast_a2s(model["model"][name]) == "continue"):
  51. return "OK"!
  52. else:
  53. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  54. else:
  55. return "Expected physical action value"!
  56. Element function constraint_assign(model : Element, name : String):
  57. if (is_physical_action(model["model"][name])):
  58. if (cast_a2s(model["model"][name]) == "assign"):
  59. return "OK"!
  60. else:
  61. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  62. else:
  63. return "Expected physical action value"!
  64. Element function constraint_return(model : Element, name : String):
  65. if (is_physical_action(model["model"][name])):
  66. if (cast_a2s(model["model"][name]) == "return"):
  67. return "OK"!
  68. else:
  69. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  70. else:
  71. return "Expected physical action value"!
  72. Element function constraint_output(model : Element, name : String):
  73. if (is_physical_action(model["model"][name])):
  74. if (cast_a2s(model["model"][name]) == "output"):
  75. return "OK"!
  76. else:
  77. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  78. else:
  79. return "Expected physical action value"!
  80. Element function constraint_input(model : Element, name : String):
  81. if (is_physical_action(model["model"][name])):
  82. if (cast_a2s(model["model"][name]) == "input"):
  83. return "OK"!
  84. else:
  85. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  86. else:
  87. return "Expected physical action value"!
  88. Element function constraint_declare(model : Element, name : String):
  89. if (is_physical_action(model["model"][name])):
  90. if (cast_a2s(model["model"][name]) == "declare"):
  91. return "OK"!
  92. else:
  93. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  94. else:
  95. return "Expected physical action value"!
  96. Element function constraint_global(model : Element, name : String):
  97. if (is_physical_action(model["model"][name])):
  98. if (cast_a2s(model["model"][name]) == "global"):
  99. return "OK"!
  100. else:
  101. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  102. else:
  103. return "Expected physical action value"!
  104. Element function constraint_access(model : Element, name : String):
  105. if (is_physical_action(model["model"][name])):
  106. if (cast_a2s(model["model"][name]) == "access"):
  107. return "OK"!
  108. else:
  109. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  110. else:
  111. return "Expected physical action value"!
  112. Element function constraint_constant(model : Element, name : String):
  113. if (is_physical_action(model["model"][name])):
  114. if (cast_a2s(model["model"][name]) == "constant"):
  115. return "OK"!
  116. else:
  117. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  118. else:
  119. return "Expected physical action value"!
  120. Element function constraint_resolve(model : Element, name : String):
  121. if (is_physical_action(model["model"][name])):
  122. if (cast_a2s(model["model"][name]) == "resolve"):
  123. return "OK"!
  124. else:
  125. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  126. else:
  127. return "Expected physical action value"!
  128. Element function constraint_call(model : Element, name : String):
  129. if (is_physical_action(model["model"][name])):
  130. if (cast_a2s(model["model"][name]) == "call"):
  131. return "OK"!
  132. else:
  133. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  134. else:
  135. return "Expected physical action value"!
  136. Element function initialize_SCD(location : String):
  137. if (element_neq(import_node(location), read_root())):
  138. return import_node(location)!
  139. Element scd
  140. scd = instantiate_bottom()
  141. model_add_node(scd, "Element")
  142. model_add_node(scd, "Class")
  143. model_add_node(scd, "AttributeValue")
  144. model_add_node(scd, "String")
  145. model_add_value(scd, "name_value", "name")
  146. model_add_edge(scd, "Association", "Class", "Class")
  147. model_add_edge(scd, "Inheritance", "Element", "Element")
  148. model_add_edge(scd, "Attribute", "Element", "AttributeValue")
  149. model_add_edge(scd, "attr_name", "Attribute", "String")
  150. model_add_edge(scd, "attr_name_name", "attr_name", "name_value")
  151. model_add_edge(scd, "class_inh_element", "Class", "Element")
  152. model_add_edge(scd, "attributevalue_inh_element", "AttributeValue", "Element")
  153. model_add_edge(scd, "association_inh_class", "Association", "Class")
  154. model_add_edge(scd, "attribute_inh_attributevalue", "Attribute", "AttributeValue")
  155. // Retype to self
  156. retype_model(scd, scd)
  157. retype(scd, "Element", "Class")
  158. retype(scd, "Class", "Class")
  159. retype(scd, "AttributeValue", "Class")
  160. retype(scd, "String", "AttributeValue")
  161. retype(scd, "name_value", "String")
  162. retype(scd, "Association", "Association")
  163. retype(scd, "Inheritance", "Association")
  164. retype(scd, "Attribute", "Association")
  165. retype(scd, "attr_name", "Attribute")
  166. retype(scd, "attr_name_name", "attr_name")
  167. retype(scd, "class_inh_element", "Inheritance")
  168. retype(scd, "attributevalue_inh_element", "Inheritance")
  169. retype(scd, "association_inh_class", "Inheritance")
  170. retype(scd, "attribute_inh_attributevalue", "Inheritance")
  171. // Add some attributes, now that it is an ordinary model
  172. instantiate_node(scd, "AttributeValue", "Natural")
  173. instantiate_node(scd, "AttributeValue", "Boolean")
  174. instantiate_link(scd, "Attribute", "attr_optional", "Attribute", "Boolean")
  175. instantiate_attribute(scd, "attr_optional", "name", "optional")
  176. instantiate_attribute(scd, "attr_optional", "optional", False)
  177. instantiate_attribute(scd, "attr_name", "optional", False)
  178. model_define_attribute(scd, "Class", "lower_cardinality", True, "Natural")
  179. model_define_attribute(scd, "Class", "upper_cardinality", True, "Natural")
  180. model_define_attribute(scd, "Association", "source_lower_cardinality", True, "Natural")
  181. model_define_attribute(scd, "Association", "target_lower_cardinality", True, "Natural")
  182. model_define_attribute(scd, "Association", "source_upper_cardinality", True, "Natural")
  183. model_define_attribute(scd, "Association", "target_upper_cardinality", True, "Natural")
  184. // Make all primitives inherit from primitive type
  185. instantiate_node(scd, "AttributeValue", "PrimitiveType")
  186. instantiate_link(scd, "Inheritance", "", "Natural", "PrimitiveType")
  187. instantiate_link(scd, "Inheritance", "", "String", "PrimitiveType")
  188. instantiate_link(scd, "Inheritance", "", "Boolean", "PrimitiveType")
  189. // Add in the Action Language metamodel
  190. add_AL_to_MM(scd)
  191. // Define additional attributes that define functions
  192. model_define_attribute(scd, "Element", "constraint", True, "funcdef")
  193. model_define_attribute(scd, "AttributeValue", "to_string", True, "funcdef")
  194. // Define some constraints
  195. instantiate_attribute(scd, "Natural", "constraint", constraint_natural)
  196. instantiate_attribute(scd, "String", "constraint", constraint_string)
  197. instantiate_attribute(scd, "Boolean", "constraint", constraint_boolean)
  198. // Add constraints to all primitive classes
  199. // TODO this is much too slow right now
  200. //add_constraint(scd, "if", constraint_if)
  201. //add_constraint(scd, "while", constraint_while)
  202. //add_constraint(scd, "break", constraint_break)
  203. //add_constraint(scd, "continue", constraint_continue)
  204. //add_constraint(scd, "assign", constraint_assign)
  205. //add_constraint(scd, "return", constraint_return)
  206. //add_constraint(scd, "output", constraint_output)
  207. //add_constraint(scd, "input", constraint_input)
  208. //add_constraint(scd, "declare", constraint_declare)
  209. //add_constraint(scd, "global", constraint_global)
  210. //add_constraint(scd, "access", constraint_access)
  211. //add_constraint(scd, "constant", constraint_constant)
  212. //add_constraint(scd, "resolve", constraint_resolve)
  213. //add_constraint(scd, "call", constraint_call)
  214. // Finally done, so export!
  215. export_node(location, scd)
  216. return scd!
  217. Element function initialize_PN(location_SCD : String, location_PN : String):
  218. Element pn
  219. Element scd
  220. scd = import_node(location_SCD)
  221. pn = instantiate_model(scd)
  222. instantiate_node(pn, "Class", "Place")
  223. instantiate_node(pn, "Class", "Transition")
  224. instantiate_node(pn, "AttributeValue", "Natural")
  225. instantiate_link(pn, "Association", "P2T", "Place", "Transition")
  226. instantiate_link(pn, "Association", "T2P", "Transition", "Place")
  227. model_define_attribute(pn, "Place", "tokens", False, "Natural")
  228. model_define_attribute(pn, "P2T", "weight", False, "Natural")
  229. model_define_attribute(pn, "T2P", "weight", False, "Natural")
  230. // Add constraint on the Natural
  231. instantiate_attribute(pn, "Natural", "constraint", constraint_natural)
  232. export_node(location_PN, pn)
  233. return pn!
  234. Element function initialize_bottom(location_bottom : String):
  235. Element ltm_bottom
  236. ltm_bottom = instantiate_bottom()
  237. model_add_node(ltm_bottom, "Node")
  238. model_add_edge(ltm_bottom, "Edge", "Node", "Node")
  239. model_add_edge(ltm_bottom, "inheritance", "Node", "Node")
  240. model_add_edge(ltm_bottom, "__inh", "Edge", "Node")
  241. retype_model(ltm_bottom, ltm_bottom)
  242. retype(ltm_bottom, "Node", "Node")
  243. retype(ltm_bottom, "Edge", "Edge")
  244. retype(ltm_bottom, "inheritance", "Edge")
  245. retype(ltm_bottom, "__inh", "inheritance")
  246. export_node(location_bottom, ltm_bottom)
  247. return ltm_bottom!
  248. Element function create_metamodels():
  249. String location_SCD
  250. String location_PN
  251. String location_bottom
  252. location_SCD = "models/SimpleClassDiagrams"
  253. location_PN = "models/PetriNets"
  254. location_bottom = "models/LTM_bottom"
  255. if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "SimpleClassDiagrams"))):
  256. initialize_SCD(location_SCD)
  257. if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "PetriNets"))):
  258. initialize_PN(location_SCD, location_PN)
  259. if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "LTM_bottom"))):
  260. initialize_bottom(location_bottom)
  261. return dict_read(dict_read(read_root(), "__hierarchy"), "models")!
  262. Void function add_AL_to_MM(model : Element):
  263. instantiate_node(model, "AttributeValue", "Action")
  264. instantiate_link(model, "Inheritance", "", "Action", "PrimitiveType")
  265. instantiate_node(model, "AttributeValue", "Statement")
  266. instantiate_node(model, "AttributeValue", "Expression")
  267. instantiate_node(model, "AttributeValue", "funcdef")
  268. instantiate_node(model, "AttributeValue", "param")
  269. instantiate_node(model, "AttributeValue", "if")
  270. instantiate_node(model, "AttributeValue", "break")
  271. instantiate_node(model, "AttributeValue", "while")
  272. instantiate_node(model, "AttributeValue", "continue")
  273. instantiate_node(model, "AttributeValue", "assign")
  274. instantiate_node(model, "AttributeValue", "return")
  275. instantiate_node(model, "AttributeValue", "output")
  276. instantiate_node(model, "AttributeValue", "declare")
  277. instantiate_node(model, "AttributeValue", "global")
  278. instantiate_node(model, "AttributeValue", "access")
  279. instantiate_node(model, "AttributeValue", "constant")
  280. instantiate_node(model, "AttributeValue", "input")
  281. instantiate_node(model, "AttributeValue", "resolve")
  282. instantiate_node(model, "AttributeValue", "call")
  283. instantiate_link(model, "Attribute", "dict_link", "Action", "PrimitiveType")
  284. instantiate_attribute(model, "dict_link", "name", "dictionary_link")
  285. instantiate_attribute(model, "dict_link", "optional", True)
  286. model_define_attribute(model, "dict_link", "name", False, "String")
  287. model_define_attribute(model, "Statement", "next", True, "Statement")
  288. model_define_attribute(model, "if", "cond", False, "Expression")
  289. model_define_attribute(model, "if", "true", False, "Statement")
  290. model_define_attribute(model, "if", "false", True, "Statement")
  291. model_define_attribute(model, "while", "cond", False, "Expression")
  292. model_define_attribute(model, "while", "body", False, "Statement")
  293. model_define_attribute(model, "assign", "var", False, "resolve")
  294. model_define_attribute(model, "assign", "value", False, "Expression")
  295. model_define_attribute(model, "break", "while", False, "while")
  296. model_define_attribute(model, "continue", "while", False, "while")
  297. model_define_attribute(model, "return", "value", True, "Expression")
  298. model_define_attribute(model, "resolve", "var", False, "String")
  299. model_define_attribute(model, "access", "var", False, "resolve")
  300. model_define_attribute(model, "constant", "node", False, "PrimitiveType")
  301. model_define_attribute(model, "output", "node", False, "Expression")
  302. model_define_attribute(model, "global", "var", False, "String")
  303. model_define_attribute(model, "param", "name", False, "String")
  304. model_define_attribute(model, "param", "value", False, "Expression")
  305. model_define_attribute(model, "param", "next_param", True, "param")
  306. model_define_attribute(model, "funcdef", "body", False, "Statement")
  307. model_define_attribute(model, "call", "func", False, "Statement")
  308. model_define_attribute(model, "call", "params", True, "param")
  309. model_define_attribute(model, "call", "last_param", True, "param")
  310. instantiate_link(model, "Inheritance", "", "statement_next", "dict_link")
  311. instantiate_link(model, "Inheritance", "", "if_cond", "dict_link")
  312. instantiate_link(model, "Inheritance", "", "if_true", "dict_link")
  313. instantiate_link(model, "Inheritance", "", "if_false", "dict_link")
  314. instantiate_link(model, "Inheritance", "", "while_cond", "dict_link")
  315. instantiate_link(model, "Inheritance", "", "while_body", "dict_link")
  316. instantiate_link(model, "Inheritance", "", "assign_var", "dict_link")
  317. instantiate_link(model, "Inheritance", "", "assign_value", "dict_link")
  318. instantiate_link(model, "Inheritance", "", "break_while", "dict_link")
  319. instantiate_link(model, "Inheritance", "", "continue_while", "dict_link")
  320. instantiate_link(model, "Inheritance", "", "return_value", "dict_link")
  321. instantiate_link(model, "Inheritance", "", "resolve_var", "dict_link")
  322. instantiate_link(model, "Inheritance", "", "access_var", "dict_link")
  323. instantiate_link(model, "Inheritance", "", "constant_node", "dict_link")
  324. instantiate_link(model, "Inheritance", "", "output_node", "dict_link")
  325. instantiate_link(model, "Inheritance", "", "global_var", "dict_link")
  326. instantiate_link(model, "Inheritance", "", "param_name", "dict_link")
  327. instantiate_link(model, "Inheritance", "", "param_value", "dict_link")
  328. instantiate_link(model, "Inheritance", "", "param_next_param", "dict_link")
  329. instantiate_link(model, "Inheritance", "", "funcdef_body", "dict_link")
  330. instantiate_link(model, "Inheritance", "", "call_func", "dict_link")
  331. instantiate_link(model, "Inheritance", "", "call_params", "dict_link")
  332. instantiate_link(model, "Inheritance", "", "call_last_param", "dict_link")
  333. return !