woods2.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_cd import parser as parser_cd
  5. from concrete_syntax.textual_od import parser as parser_od
  6. from concrete_syntax.textual_od import renderer as renderer_od
  7. from concrete_syntax.common import indent
  8. from concrete_syntax.plantuml import renderer as plantuml
  9. from util.prompt import yes_no, pause
  10. state = DevState()
  11. print("Loading meta-meta-model...")
  12. scd_mmm = bootstrap_scd(state)
  13. print("OK")
  14. print("Is our meta-meta-model a valid class diagram?")
  15. conf = Conformance(state, scd_mmm, scd_mmm)
  16. print(render_conformance_check_result(conf.check_nominal()))
  17. # If you are curious, you can serialize the meta-meta-model:
  18. # print("--------------")
  19. # print(indent(
  20. # renderer.render_od(state,
  21. # m_id=scd_mmm,
  22. # mm_id=scd_mmm),
  23. # 4))
  24. # print("--------------")
  25. # Change this:
  26. woods_mm_cs = """
  27. abstract class Animal
  28. class Bear (Animal) # Bear inherits Animal
  29. class Man [1..2] (Animal) {
  30. Integer weight `get_value(get_target(this)) > 20`; # <- constraint in context of attribute-link
  31. `get_value(get_slot(this, "weight")) > 20` # <- constraint in context of Man-object
  32. }
  33. association afraidOf [0..6] Man -> Animal [1..2]
  34. global total_weight_small_enough ```
  35. total_weight = 0
  36. for man_name, man_id in get_all_instances("Man"):
  37. total_weight += get_value(get_slot(man_id, "weight"))
  38. total_weight < 85
  39. ```
  40. """
  41. print()
  42. print("Parsing 'woods' meta-model...")
  43. woods_mm = parser_cd.parse_cd(
  44. state,
  45. m_text=woods_mm_cs, # the string of text to parse
  46. )
  47. print("OK")
  48. # We can serialize the class diagram to our object diagram syntax
  49. # (because the class diagram IS also an object diagram):
  50. print("--------------")
  51. print(indent(
  52. renderer_od.render_od(state,
  53. m_id=woods_mm,
  54. mm_id=scd_mmm),
  55. 4))
  56. print("--------------")
  57. print("Is our 'woods' meta-model a valid class diagram?")
  58. conf = Conformance(state, woods_mm, scd_mmm)
  59. print(render_conformance_check_result(conf.check_nominal()))
  60. # Change this:
  61. woods_m_cs = """
  62. george:Man {
  63. weight = 15;
  64. }
  65. billy:Man {
  66. weight = 100;
  67. }
  68. bear1:Bear
  69. bear2:Bear
  70. :afraidOf (george -> bear1)
  71. :afraidOf (george -> bear2)
  72. """
  73. print()
  74. print("Parsing 'woods' model...")
  75. woods_m = parser_od.parse_od(
  76. state,
  77. m_text=woods_m_cs,
  78. mm=woods_mm, # this time, the meta-model is the previous model we parsed
  79. )
  80. print("OK")
  81. # As a double-check, you can serialize the parsed model:
  82. # print("--------------")
  83. # print(indent(
  84. # renderer.render_od(state,
  85. # m_id=woods_m,
  86. # mm_id=woods_mm),
  87. # 4))
  88. # print("--------------")
  89. print("Is our model a valid woods-diagram?")
  90. conf = Conformance(state, woods_m, woods_mm)
  91. print(render_conformance_check_result(conf.check_nominal()))
  92. print()
  93. print("==================================")
  94. if yes_no("Print PlantUML?"):
  95. print_mm = yes_no(" ▸ Print meta-model?")
  96. print_m = yes_no(" ▸ Print model?")
  97. print_conf = print_mm and print_m and yes_no(" ▸ Print conformance links?")
  98. uml = ""
  99. if print_mm:
  100. uml += plantuml.render_package("Meta-model", plantuml.render_class_diagram(state, woods_mm))
  101. if print_m:
  102. uml += plantuml.render_package("Model", plantuml.render_object_diagram(state, woods_m, woods_mm))
  103. if print_conf:
  104. uml += plantuml.render_trace_conformance(state, woods_m, woods_mm)
  105. print("==================================")
  106. print(uml)
  107. print("==================================")
  108. print("Go to either:")
  109. print(" ▸ https://www.plantuml.com/plantuml/uml")
  110. print(" ▸ https://mstro.duckdns.org/plantuml/uml")
  111. print("and paste the above string.")