metamodels.alc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. // Temporarily disable during tests
  140. return create_node()!
  141. Element scd
  142. scd = instantiate_bottom()
  143. model_add_node(scd, "Element")
  144. model_add_node(scd, "Class")
  145. model_add_node(scd, "Attribute")
  146. model_add_node(scd, "SimpleAttribute")
  147. model_add_node(scd, "String")
  148. model_add_value(scd, "name_value", "name")
  149. model_add_edge(scd, "Association", "Class", "Class")
  150. model_add_edge(scd, "Inheritance", "Element", "Element")
  151. model_add_edge(scd, "AttributeLink", "Element", "Attribute")
  152. model_add_edge(scd, "attr_name", "AttributeLink", "String")
  153. model_add_edge(scd, "attr_name_name", "attr_name", "name_value")
  154. model_add_edge(scd, "class_inh_element", "Class", "Element")
  155. model_add_edge(scd, "attribute_inh_element", "Attribute", "Element")
  156. model_add_edge(scd, "association_inh_element", "Association", "Element")
  157. model_add_edge(scd, "attributelink_inh_element", "AttributeLink", "Element")
  158. // Retype to self
  159. retype_model(scd, scd)
  160. retype(scd, "Element", "Class")
  161. retype(scd, "Class", "Class")
  162. retype(scd, "Attribute", "Class")
  163. retype(scd, "SimpleAttribute", "Class")
  164. retype(scd, "String", "Attribute")
  165. retype(scd, "name_value", "String")
  166. retype(scd, "Association", "Association")
  167. retype(scd, "Inheritance", "Association")
  168. retype(scd, "AttributeLink", "Association")
  169. retype(scd, "attr_name", "Attribute")
  170. retype(scd, "attr_name_name", "attr_name")
  171. retype(scd, "class_inh_element", "Inheritance")
  172. retype(scd, "attribute_inh_element", "Inheritance")
  173. retype(scd, "association_inh_element", "Inheritance")
  174. retype(scd, "attributelink_inh_element", "Inheritance")
  175. // Add some attributes, now that it is an ordinary model
  176. instantiate_node(scd, "SimpleAttribute", "Location")
  177. instantiate_node(scd, "SimpleAttribute", "Natural")
  178. instantiate_node(scd, "SimpleAttribute", "Boolean")
  179. instantiate_link(scd, "AttributeLink", "attr_optional", "Attribute", "Boolean")
  180. instantiate_attribute(scd, "attr_optional", "name", "optional")
  181. instantiate_attribute(scd, "attr_optional", "optional", False)
  182. instantiate_attribute(scd, "attr_name", "optional", False)
  183. model_define_attribute(scd, "Class", "lower_cardinality", True, "Natural")
  184. model_define_attribute(scd, "Class", "upper_cardinality", True, "Natural")
  185. model_define_attribute(scd, "Association", "source_lower_cardinality", True, "Natural")
  186. model_define_attribute(scd, "Association", "target_lower_cardinality", True, "Natural")
  187. model_define_attribute(scd, "Association", "source_upper_cardinality", True, "Natural")
  188. model_define_attribute(scd, "Association", "target_upper_cardinality", True, "Natural")
  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. //instantiate_attribute_code(scd, "if", "constraint", constraint_if)
  201. //instantiate_attribute_code(scd, "while", "constraint", constraint_while)
  202. //instantiate_attribute_code(scd, "break", "constraint", constraint_break)
  203. //instantiate_attribute_code(scd, "continue", "constraint", constraint_continue)
  204. //instantiate_attribute_code(scd, "assign", "constraint", constraint_assign)
  205. //instantiate_attribute_code(scd, "return", "constraint", constraint_return)
  206. //instantiate_attribute_code(scd, "output", "constraint", constraint_output)
  207. //instantiate_attribute_code(scd, "input", "constraint", constraint_input)
  208. //instantiate_attribute_code(scd, "declare", "constraint", constraint_declare)
  209. //instantiate_attribute_code(scd, "global", "constraint", constraint_global)
  210. //instantiate_attribute_code(scd, "access", "constraint", constraint_access)
  211. //instantiate_attribute_code(scd, "constant", "constraint", constraint_constant)
  212. //instantiate_attribute_code(scd, "resolve", "constraint", constraint_resolve)
  213. //instantiate_attribute_code(scd, "call", "constraint", 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", "PrimitiveType")
  264. instantiate_node(model, "AttributeValue", "Action")
  265. instantiate_node(model, "AttributeValue", "String")
  266. instantiate_node(model, "AttributeValue", "Natural")
  267. instantiate_link(model, "Inheritance", "", "Natural", "PrimitiveType")
  268. instantiate_link(model, "Inheritance", "", "String", "PrimitiveType")
  269. instantiate_link(model, "Inheritance", "", "Action", "PrimitiveType")
  270. instantiate_link(model, "Inheritance", "", "Boolean", "PrimitiveType")
  271. instantiate_attribute(model, "Natural", "constraint", constraint_natural)
  272. instantiate_attribute(model, "String", "constraint", constraint_string)
  273. instantiate_attribute(model, "Boolean", "constraint", constraint_boolean)
  274. instantiate_node(model, "AttributeValue", "Boolean")
  275. instantiate_link(model, "Inheritance", "", "Action", "PrimitiveType")
  276. instantiate_node(model, "AttributeValue", "Statement")
  277. instantiate_node(model, "AttributeValue", "Expression")
  278. instantiate_node(model, "AttributeValue", "funcdef")
  279. instantiate_node(model, "AttributeValue", "param")
  280. instantiate_node(model, "AttributeValue", "if")
  281. instantiate_node(model, "AttributeValue", "break")
  282. instantiate_node(model, "AttributeValue", "while")
  283. instantiate_node(model, "AttributeValue", "continue")
  284. instantiate_node(model, "AttributeValue", "assign")
  285. instantiate_node(model, "AttributeValue", "return")
  286. instantiate_node(model, "AttributeValue", "output")
  287. instantiate_node(model, "AttributeValue", "declare")
  288. instantiate_node(model, "AttributeValue", "global")
  289. instantiate_node(model, "AttributeValue", "access")
  290. instantiate_node(model, "AttributeValue", "constant")
  291. instantiate_node(model, "AttributeValue", "input")
  292. instantiate_node(model, "AttributeValue", "resolve")
  293. instantiate_node(model, "AttributeValue", "call")
  294. instantiate_link(model, "Inheritance", "", "Action", "Element")
  295. instantiate_link(model, "Inheritance", "", "funcdef", "Action")
  296. instantiate_link(model, "Inheritance", "", "param", "Action")
  297. instantiate_link(model, "Inheritance", "", "Statement", "Action")
  298. instantiate_link(model, "Inheritance", "", "Expression", "Action")
  299. instantiate_link(model, "Inheritance", "", "resolve", "Statement")
  300. instantiate_link(model, "Inheritance", "", "if", "Statement")
  301. instantiate_link(model, "Inheritance", "", "break", "Statement")
  302. instantiate_link(model, "Inheritance", "", "continue", "Statement")
  303. instantiate_link(model, "Inheritance", "", "global", "Statement")
  304. instantiate_link(model, "Inheritance", "", "declare", "Statement")
  305. instantiate_link(model, "Inheritance", "", "while", "Statement")
  306. instantiate_link(model, "Inheritance", "", "assign", "Statement")
  307. instantiate_link(model, "Inheritance", "", "return", "Statement")
  308. instantiate_link(model, "Inheritance", "", "call", "Statement")
  309. instantiate_link(model, "Inheritance", "", "access", "Expression")
  310. instantiate_link(model, "Inheritance", "", "constant", "Expression")
  311. instantiate_link(model, "Inheritance", "", "input", "Expression")
  312. model_define_attribute(model, "Statement", "next", True, "Statement")
  313. model_define_attribute(model, "if", "cond", False, "Expression")
  314. model_define_attribute(model, "if", "then", False, "Statement")
  315. model_define_attribute(model, "if", "else", True, "Statement")
  316. model_define_attribute(model, "while", "cond", False, "Expression")
  317. model_define_attribute(model, "while", "body", False, "Statement")
  318. model_define_attribute(model, "assign", "var", False, "resolve")
  319. model_define_attribute(model, "assign", "value", False, "Expression")
  320. model_define_attribute(model, "break", "while", False, "while")
  321. model_define_attribute(model, "continue", "while", False, "while")
  322. model_define_attribute(model, "return", "value", True, "Expression")
  323. model_define_attribute(model, "resolve", "var", False, "PrimitiveType")
  324. model_define_attribute(model, "access", "var", False, "resolve")
  325. model_define_attribute(model, "constant", "node", False, "PrimitiveType")
  326. model_define_attribute(model, "output", "node", False, "Expression")
  327. model_define_attribute(model, "global", "var", False, "String")
  328. model_define_attribute(model, "param", "name", False, "String")
  329. model_define_attribute(model, "param", "value", False, "Expression")
  330. model_define_attribute(model, "param", "next_param", True, "param")
  331. model_define_attribute(model, "funcdef", "body", False, "Statement")
  332. model_define_attribute(model, "call", "func", False, "Statement")
  333. model_define_attribute(model, "call", "params", True, "param")
  334. model_define_attribute(model, "call", "last_param", True, "param")
  335. model_define_attribute(model, "Statement_next", "name", False, "String")
  336. model_define_attribute(model, "if_cond", "name", False, "String")
  337. model_define_attribute(model, "if_then", "name", False, "String")
  338. model_define_attribute(model, "if_else", "name", False, "String")
  339. model_define_attribute(model, "while_cond", "name", False, "String")
  340. model_define_attribute(model, "while_body", "name", False, "String")
  341. model_define_attribute(model, "assign_var", "name", False, "String")
  342. model_define_attribute(model, "assign_value", "name", False, "String")
  343. model_define_attribute(model, "break_while", "name", False, "String")
  344. model_define_attribute(model, "continue_while", "name", False, "String")
  345. model_define_attribute(model, "return_value", "name", False, "String")
  346. model_define_attribute(model, "resolve_var", "name", False, "String")
  347. model_define_attribute(model, "access_var", "name", False, "String")
  348. model_define_attribute(model, "constant_node", "name", False, "String")
  349. model_define_attribute(model, "output_node", "name", False, "String")
  350. model_define_attribute(model, "global_var", "name", False, "String")
  351. model_define_attribute(model, "param_name", "name", False, "String")
  352. model_define_attribute(model, "param_value", "name", False, "String")
  353. model_define_attribute(model, "param_next_param", "name", False, "String")
  354. model_define_attribute(model, "funcdef_body", "name", False, "String")
  355. model_define_attribute(model, "call_func", "name", False, "String")
  356. model_define_attribute(model, "call_params", "name", False, "String")
  357. model_define_attribute(model, "call_last_param", "name", False, "String")
  358. return !