abstract_assoc.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from state.devstate import DevState
  2. from bootstrap.scd import bootstrap_scd
  3. from framework.conformance import Conformance, render_conformance_check_result
  4. from concrete_syntax.textual_od import parser, renderer
  5. from concrete_syntax.common import indent
  6. from concrete_syntax.plantuml import renderer as plantuml
  7. from util.prompt import yes_no, pause
  8. state = DevState()
  9. scd_mmm = bootstrap_scd(state)
  10. mm_cs = """
  11. BaseA:Class {
  12. abstract = True;
  13. }
  14. BaseB:Class {
  15. abstract = True;
  16. }
  17. baseAssoc:Association (BaseA -> BaseB) {
  18. abstract = True;
  19. target_lower_cardinality = 1;
  20. target_upper_cardinality = 2; # A has 1..2 B
  21. }
  22. A:Class
  23. B:Class
  24. assoc:Association (A -> B) {
  25. # we can further restrict cardinality from baseAssoc:
  26. target_upper_cardinality = 1;
  27. # relaxing cardinalities or constraints can be done (meaning: it will still be a valid meta-model), but will have no effect: for any instance of a type, the constraints defined on the type and its supertypes will be checked.
  28. }
  29. :Inheritance (A -> BaseA)
  30. :Inheritance (B -> BaseB)
  31. :Inheritance (assoc -> baseAssoc)
  32. """
  33. print()
  34. print("Parsing meta-model...")
  35. mm = parser.parse_od(
  36. state,
  37. m_text=mm_cs, # the string of text to parse
  38. mm=scd_mmm, # the meta-model of class diagrams (= our meta-meta-model)
  39. )
  40. print("OK")
  41. print("Is our meta-model a valid class diagram?")
  42. conf = Conformance(state, mm, scd_mmm)
  43. print(render_conformance_check_result(conf.check_nominal()))
  44. m_cs = """
  45. a0:A
  46. b0:B
  47. b1:B
  48. # error: assoc (A -> B) must have tgt card 0..1 (and we have 2 instead)
  49. :assoc (a0 -> b0)
  50. :assoc (a0 -> b1)
  51. # error: baseAssoc (A -> B) must have tgt card 1..2 (and we have 0 instead)
  52. a1:A
  53. """
  54. print()
  55. print("Parsing model...")
  56. m = parser.parse_od(
  57. state,
  58. m_text=m_cs,
  59. mm=mm, # this time, the meta-model is the previous model we parsed
  60. )
  61. print("OK")
  62. print("Is our model a valid woods-diagram?")
  63. conf = Conformance(state, m, mm)
  64. print(render_conformance_check_result(conf.check_nominal()))