metamodels.alc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_if(model : Element, name : String):
  20. if (is_physical_action(model["model"][name])):
  21. if (cast_a2s(model["model"][name]) == "if"):
  22. return "OK"!
  23. else:
  24. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  25. else:
  26. return "Expected physical action value"!
  27. Element function constraint_while(model : Element, name : String):
  28. if (is_physical_action(model["model"][name])):
  29. if (cast_a2s(model["model"][name]) == "while"):
  30. return "OK"!
  31. else:
  32. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  33. else:
  34. return "Expected physical action value"!
  35. Element function constraint_break(model : Element, name : String):
  36. if (is_physical_action(model["model"][name])):
  37. if (cast_a2s(model["model"][name]) == "break"):
  38. return "OK"!
  39. else:
  40. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  41. else:
  42. return "Expected physical action value"!
  43. Element function constraint_continue(model : Element, name : String):
  44. if (is_physical_action(model["model"][name])):
  45. if (cast_a2s(model["model"][name]) == "continue"):
  46. return "OK"!
  47. else:
  48. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  49. else:
  50. return "Expected physical action value"!
  51. Element function constraint_assign(model : Element, name : String):
  52. if (is_physical_action(model["model"][name])):
  53. if (cast_a2s(model["model"][name]) == "assign"):
  54. return "OK"!
  55. else:
  56. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  57. else:
  58. return "Expected physical action value"!
  59. Element function constraint_return(model : Element, name : String):
  60. if (is_physical_action(model["model"][name])):
  61. if (cast_a2s(model["model"][name]) == "return"):
  62. return "OK"!
  63. else:
  64. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  65. else:
  66. return "Expected physical action value"!
  67. Element function constraint_output(model : Element, name : String):
  68. if (is_physical_action(model["model"][name])):
  69. if (cast_a2s(model["model"][name]) == "output"):
  70. return "OK"!
  71. else:
  72. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  73. else:
  74. return "Expected physical action value"!
  75. Element function constraint_input(model : Element, name : String):
  76. if (is_physical_action(model["model"][name])):
  77. if (cast_a2s(model["model"][name]) == "input"):
  78. return "OK"!
  79. else:
  80. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  81. else:
  82. return "Expected physical action value"!
  83. Element function constraint_declare(model : Element, name : String):
  84. if (is_physical_action(model["model"][name])):
  85. if (cast_a2s(model["model"][name]) == "declare"):
  86. return "OK"!
  87. else:
  88. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  89. else:
  90. return "Expected physical action value"!
  91. Element function constraint_global(model : Element, name : String):
  92. if (is_physical_action(model["model"][name])):
  93. if (cast_a2s(model["model"][name]) == "global"):
  94. return "OK"!
  95. else:
  96. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  97. else:
  98. return "Expected physical action value"!
  99. Element function constraint_access(model : Element, name : String):
  100. if (is_physical_action(model["model"][name])):
  101. if (cast_a2s(model["model"][name]) == "access"):
  102. return "OK"!
  103. else:
  104. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  105. else:
  106. return "Expected physical action value"!
  107. Element function constraint_constant(model : Element, name : String):
  108. if (is_physical_action(model["model"][name])):
  109. if (cast_a2s(model["model"][name]) == "constant"):
  110. return "OK"!
  111. else:
  112. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  113. else:
  114. return "Expected physical action value"!
  115. Element function constraint_resolve(model : Element, name : String):
  116. if (is_physical_action(model["model"][name])):
  117. if (cast_a2s(model["model"][name]) == "resolve"):
  118. return "OK"!
  119. else:
  120. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  121. else:
  122. return "Expected physical action value"!
  123. Element function constraint_call(model : Element, name : String):
  124. if (is_physical_action(model["model"][name])):
  125. if (cast_a2s(model["model"][name]) == "call"):
  126. return "OK"!
  127. else:
  128. return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
  129. else:
  130. return "Expected physical action value"!
  131. Element function initialize_SCD(location : String):
  132. if (element_neq(import_node(location), read_root())):
  133. return import_node(location)!
  134. Element scd
  135. scd = instantiate_bottom()
  136. model_add_node(scd, "Element")
  137. model_add_node(scd, "Class")
  138. model_add_node(scd, "AttributeValue")
  139. model_add_node(scd, "String")
  140. model_add_value(scd, "name_value", "name")
  141. model_add_edge(scd, "Association", "Class", "Class")
  142. model_add_edge(scd, "Inheritance", "Element", "Element")
  143. model_add_edge(scd, "Attribute", "Element", "AttributeValue")
  144. model_add_edge(scd, "attr_name", "Attribute", "String")
  145. model_add_edge(scd, "attr_name_name", "attr_name", "name_value")
  146. model_add_edge(scd, "class_inh_element", "Class", "Element")
  147. model_add_edge(scd, "attributevalue_inh_element", "AttributeValue", "Element")
  148. model_add_edge(scd, "association_inh_class", "Association", "Class")
  149. model_add_edge(scd, "attribute_inh_attributevalue", "Attribute", "AttributeValue")
  150. // Retype to self
  151. retype_model(scd, scd)
  152. retype(scd, "Element", "Class")
  153. retype(scd, "Class", "Class")
  154. retype(scd, "AttributeValue", "Class")
  155. retype(scd, "String", "AttributeValue")
  156. retype(scd, "name_value", "String")
  157. retype(scd, "Association", "Association")
  158. retype(scd, "Inheritance", "Association")
  159. retype(scd, "Attribute", "Association")
  160. retype(scd, "attr_name", "Attribute")
  161. retype(scd, "attr_name_name", "attr_name")
  162. retype(scd, "class_inh_element", "Inheritance")
  163. retype(scd, "attributevalue_inh_element", "Inheritance")
  164. retype(scd, "association_inh_class", "Inheritance")
  165. retype(scd, "attribute_inh_attributevalue", "Inheritance")
  166. // Add some attributes, now that it is an ordinary model
  167. instantiate_node(scd, "AttributeValue", "Natural")
  168. instantiate_node(scd, "AttributeValue", "Boolean")
  169. model_define_attribute(scd, "Attribute", "optional", False, "Boolean")
  170. model_define_attribute(scd, "Class", "lower_cardinality", False, "Natural")
  171. model_define_attribute(scd, "Class", "upper_cardinality", False, "Natural")
  172. model_define_attribute(scd, "Association", "source_lower_cardinality", False, "Natural")
  173. model_define_attribute(scd, "Association", "target_lower_cardinality", False, "Natural")
  174. model_define_attribute(scd, "Association", "source_upper_cardinality", False, "Natural")
  175. model_define_attribute(scd, "Association", "target_upper_cardinality", False, "Natural")
  176. // Add in the Action Language metamodel
  177. add_AL_to_MM(scd)
  178. // Define additional attributes that define functions
  179. model_define_attribute(scd, "Element", "constraint", True, "funcdef")
  180. model_define_attribute(scd, "AttributeValue", "to_string", False, "funcdef")
  181. // Define some constraints
  182. instantiate_attribute(scd, "Natural", "constraint", constraint_natural)
  183. instantiate_attribute(scd, "String", "constraint", constraint_string)
  184. // Add constraints to all primitive classes
  185. // TODO this is much too slow right now
  186. //add_constraint(scd, "if", constraint_if)
  187. //add_constraint(scd, "while", constraint_while)
  188. //add_constraint(scd, "break", constraint_break)
  189. //add_constraint(scd, "continue", constraint_continue)
  190. //add_constraint(scd, "assign", constraint_assign)
  191. //add_constraint(scd, "return", constraint_return)
  192. //add_constraint(scd, "output", constraint_output)
  193. //add_constraint(scd, "input", constraint_input)
  194. //add_constraint(scd, "declare", constraint_declare)
  195. //add_constraint(scd, "global", constraint_global)
  196. //add_constraint(scd, "access", constraint_access)
  197. //add_constraint(scd, "constant", constraint_constant)
  198. //add_constraint(scd, "resolve", constraint_resolve)
  199. //add_constraint(scd, "call", constraint_call)
  200. // Finally done, so export!
  201. export_node(location, scd)
  202. return scd!
  203. Element function initialize_PN(location_SCD : String, location_PN : String):
  204. Element pn
  205. Element scd
  206. scd = import_node(location_SCD)
  207. pn = instantiate_model(scd)
  208. instantiate_node(pn, "Class", "Place")
  209. instantiate_node(pn, "Class", "Transition")
  210. instantiate_node(pn, "AttributeValue", "Natural")
  211. instantiate_link(pn, "Association", "P2T", "Place", "Transition")
  212. instantiate_link(pn, "Association", "T2P", "Transition", "Place")
  213. model_define_attribute(pn, "Place", "tokens", False, "Natural")
  214. model_define_attribute(pn, "P2T", "weight", False, "Natural")
  215. model_define_attribute(pn, "T2P", "weight", False, "Natural")
  216. // Add constraint on the Natural
  217. instantiate_attribute(pn, "Natural", "constraint", constraint_natural)
  218. export_node(location_PN, pn)
  219. return pn!
  220. Element function initialize_bottom(location_bottom : String):
  221. Element ltm_bottom
  222. ltm_bottom = instantiate_bottom()
  223. model_add_node(ltm_bottom, "Node")
  224. model_add_edge(ltm_bottom, "Edge", "Node", "Node")
  225. model_add_edge(ltm_bottom, "inheritance", "Node", "Node")
  226. model_add_edge(ltm_bottom, "__inh", "Edge", "Node")
  227. retype_model(ltm_bottom, ltm_bottom)
  228. retype(ltm_bottom, "Node", "Node")
  229. retype(ltm_bottom, "Edge", "Edge")
  230. retype(ltm_bottom, "inheritance", "Edge")
  231. retype(ltm_bottom, "__inh", "inheritance")
  232. export_node(location_bottom, ltm_bottom)
  233. return ltm_bottom!
  234. Element function create_metamodels():
  235. String location_SCD
  236. String location_PN
  237. String location_bottom
  238. location_SCD = "models/SimpleClassDiagrams"
  239. location_PN = "models/PetriNets"
  240. location_bottom = "models/LTM_bottom"
  241. if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "SimpleClassDiagrams"))):
  242. initialize_SCD(location_SCD)
  243. if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "PetriNets"))):
  244. initialize_PN(location_SCD, location_PN)
  245. if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "LTM_bottom"))):
  246. initialize_bottom(location_bottom)
  247. return dict_read(dict_read(read_root(), "__hierarchy"), "models")!
  248. Void function add_AL_to_MM(model : Element):
  249. instantiate_node(model, "AttributeValue", "Action")
  250. instantiate_node(model, "AttributeValue", "Statement")
  251. instantiate_node(model, "AttributeValue", "Expression")
  252. instantiate_node(model, "AttributeValue", "funcdef")
  253. instantiate_node(model, "AttributeValue", "param")
  254. instantiate_node(model, "AttributeValue", "if")
  255. instantiate_node(model, "AttributeValue", "break")
  256. instantiate_node(model, "AttributeValue", "while")
  257. instantiate_node(model, "AttributeValue", "continue")
  258. instantiate_node(model, "AttributeValue", "assign")
  259. instantiate_node(model, "AttributeValue", "return")
  260. instantiate_node(model, "AttributeValue", "output")
  261. instantiate_node(model, "AttributeValue", "declare")
  262. instantiate_node(model, "AttributeValue", "global")
  263. instantiate_node(model, "AttributeValue", "access")
  264. instantiate_node(model, "AttributeValue", "constant")
  265. instantiate_node(model, "AttributeValue", "input")
  266. instantiate_node(model, "AttributeValue", "resolve")
  267. instantiate_node(model, "AttributeValue", "call")
  268. instantiate_link(model, "Attribute", "dict_link", "Action", "Element")
  269. instantiate_link(model, "Attribute", "to_str", "dict_link", "String")
  270. instantiate_attribute(model, "to_str", "name", "name")
  271. instantiate_link(model, "Inheritance", "", "Action", "Element")
  272. instantiate_link(model, "Inheritance", "", "funcdef", "Action")
  273. instantiate_link(model, "Inheritance", "", "param", "Action")
  274. instantiate_link(model, "Inheritance", "", "Statement", "Action")
  275. instantiate_link(model, "Inheritance", "", "Expression", "Action")
  276. instantiate_link(model, "Inheritance", "", "resolve", "Statement")
  277. instantiate_link(model, "Inheritance", "", "if", "Statement")
  278. instantiate_link(model, "Inheritance", "", "break", "Statement")
  279. instantiate_link(model, "Inheritance", "", "continue", "Statement")
  280. instantiate_link(model, "Inheritance", "", "global", "Statement")
  281. instantiate_link(model, "Inheritance", "", "while", "Statement")
  282. instantiate_link(model, "Inheritance", "", "assign", "Statement")
  283. instantiate_link(model, "Inheritance", "", "return", "Statement")
  284. instantiate_link(model, "Inheritance", "", "call", "Statement")
  285. instantiate_link(model, "Inheritance", "", "declare", "Statement")
  286. instantiate_link(model, "Inheritance", "", "call", "Expression")
  287. instantiate_link(model, "Inheritance", "", "access", "Expression")
  288. instantiate_link(model, "Inheritance", "", "constant", "Expression")
  289. instantiate_link(model, "Inheritance", "", "input", "Expression")
  290. instantiate_link(model, "Attribute", "statement_next", "Statement", "Statement")
  291. instantiate_link(model, "Attribute", "if_cond", "if", "Expression")
  292. instantiate_link(model, "Attribute", "if_then", "if", "Statement")
  293. instantiate_link(model, "Attribute", "if_else", "if", "Statement")
  294. instantiate_link(model, "Attribute", "while_cond", "while", "Expression")
  295. instantiate_link(model, "Attribute", "while_body", "while", "Statement")
  296. instantiate_link(model, "Attribute", "assign_var", "assign", "Element")
  297. instantiate_link(model, "Attribute", "assign_value", "assign", "Expression")
  298. instantiate_link(model, "Attribute", "break_while", "break", "while")
  299. instantiate_link(model, "Attribute", "continue_while", "continue", "while")
  300. instantiate_link(model, "Attribute", "return_value", "return", "Expression")
  301. instantiate_link(model, "Attribute", "resolve_var", "resolve", "Element")
  302. instantiate_link(model, "Attribute", "access_var", "access", "Element")
  303. instantiate_link(model, "Attribute", "constant_node", "constant", "Element")
  304. instantiate_link(model, "Attribute", "output_node", "output", "Expression")
  305. instantiate_link(model, "Attribute", "global_var", "global", "String")
  306. instantiate_link(model, "Attribute", "param_name", "param", "String")
  307. instantiate_link(model, "Attribute", "param_value", "param", "Expression")
  308. instantiate_link(model, "Attribute", "param_next_param", "param", "param")
  309. instantiate_link(model, "Attribute", "funcdef_body", "funcdef", "Statement")
  310. instantiate_link(model, "Attribute", "call_func", "call", "Expression")
  311. instantiate_link(model, "Attribute", "call_params", "call", "param")
  312. instantiate_link(model, "Attribute", "call_last_param", "call", "param")
  313. instantiate_link(model, "Inheritance", "", "statement_next", "dict_link")
  314. instantiate_link(model, "Inheritance", "", "if_cond", "dict_link")
  315. instantiate_link(model, "Inheritance", "", "if_then", "dict_link")
  316. instantiate_link(model, "Inheritance", "", "if_else", "dict_link")
  317. instantiate_link(model, "Inheritance", "", "while_cond", "dict_link")
  318. instantiate_link(model, "Inheritance", "", "while_body", "dict_link")
  319. instantiate_link(model, "Inheritance", "", "assign_var", "dict_link")
  320. instantiate_link(model, "Inheritance", "", "assign_value", "dict_link")
  321. instantiate_link(model, "Inheritance", "", "break_while", "dict_link")
  322. instantiate_link(model, "Inheritance", "", "continue_while", "dict_link")
  323. instantiate_link(model, "Inheritance", "", "return_value", "dict_link")
  324. instantiate_link(model, "Inheritance", "", "resolve_var", "dict_link")
  325. instantiate_link(model, "Inheritance", "", "access_var", "dict_link")
  326. instantiate_link(model, "Inheritance", "", "constant_node", "dict_link")
  327. instantiate_link(model, "Inheritance", "", "output_node", "dict_link")
  328. instantiate_link(model, "Inheritance", "", "global_var", "dict_link")
  329. instantiate_link(model, "Inheritance", "", "param_name", "dict_link")
  330. instantiate_link(model, "Inheritance", "", "param_value", "dict_link")
  331. instantiate_link(model, "Inheritance", "", "param_next_param", "dict_link")
  332. instantiate_link(model, "Inheritance", "", "funcdef_body", "dict_link")
  333. instantiate_link(model, "Inheritance", "", "call_func", "dict_link")
  334. instantiate_link(model, "Inheritance", "", "call_params", "dict_link")
  335. instantiate_link(model, "Inheritance", "", "call_last_param", "dict_link")
  336. // Add cardinalities on how many connections are allowed: one of each
  337. // instantiate_attribute(model, "statement_next", "target_upper_cardinality", 1)
  338. // instantiate_attribute(model, "if_cond", "target_lower_cardinality", 1)
  339. // instantiate_attribute(model, "if_cond", "target_upper_cardinality", 1)
  340. // instantiate_attribute(model, "if_then", "target_lower_cardinality", 1)
  341. // instantiate_attribute(model, "if_then", "target_upper_cardinality", 1)
  342. // instantiate_attribute(model, "if_else", "target_upper_cardinality", 1)
  343. // instantiate_attribute(model, "while_cond", "target_lower_cardinality", 1)
  344. // instantiate_attribute(model, "while_cond", "target_upper_cardinality", 1)
  345. // instantiate_attribute(model, "while_body", "target_lower_cardinality", 1)
  346. // instantiate_attribute(model, "while_body", "target_upper_cardinality", 1)
  347. // instantiate_attribute(model, "assign_var", "target_lower_cardinality", 1)
  348. // instantiate_attribute(model, "assign_var", "target_upper_cardinality", 1)
  349. // instantiate_attribute(model, "assign_value", "target_lower_cardinality", 1)
  350. // instantiate_attribute(model, "assign_value", "target_upper_cardinality", 1)
  351. // instantiate_attribute(model, "break_while", "target_lower_cardinality", 1)
  352. // instantiate_attribute(model, "break_while", "target_upper_cardinality", 1)
  353. // instantiate_attribute(model, "continue_while", "target_lower_cardinality", 1)
  354. // instantiate_attribute(model, "continue_while", "target_upper_cardinality", 1)
  355. // instantiate_attribute(model, "return_value", "target_upper_cardinality", 1)
  356. // instantiate_attribute(model, "resolve_var", "target_lower_cardinality", 1)
  357. // instantiate_attribute(model, "resolve_var", "target_upper_cardinality", 1)
  358. // instantiate_attribute(model, "access_var", "target_lower_cardinality", 1)
  359. // instantiate_attribute(model, "access_var", "target_upper_cardinality", 1)
  360. // instantiate_attribute(model, "constant_node", "target_lower_cardinality", 1)
  361. // instantiate_attribute(model, "constant_node", "target_upper_cardinality", 1)
  362. // instantiate_attribute(model, "output_node", "target_lower_cardinality", 1)
  363. // instantiate_attribute(model, "output_node", "target_upper_cardinality", 1)
  364. // instantiate_attribute(model, "global_var", "target_lower_cardinality", 1)
  365. // instantiate_attribute(model, "global_var", "target_upper_cardinality", 1)
  366. // instantiate_attribute(model, "param_name", "target_lower_cardinality", 1)
  367. // instantiate_attribute(model, "param_name", "target_upper_cardinality", 1)
  368. // instantiate_attribute(model, "param_value", "target_lower_cardinality", 1)
  369. // instantiate_attribute(model, "param_value", "target_upper_cardinality", 1)
  370. // instantiate_attribute(model, "param_next_param", "target_upper_cardinality", 1)
  371. // instantiate_attribute(model, "funcdef_body", "target_lower_cardinality", 1)
  372. // instantiate_attribute(model, "funcdef_body", "target_upper_cardinality", 1)
  373. // instantiate_attribute(model, "call_func", "target_lower_cardinality", 1)
  374. // instantiate_attribute(model, "call_func", "target_upper_cardinality", 1)
  375. // instantiate_attribute(model, "call_params", "target_upper_cardinality", 1)
  376. // instantiate_attribute(model, "call_last_param", "target_upper_cardinality", 1)
  377. return !