03_api.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # We reuse our (meta-)model from the previous tutorial. For this tutorial, it doesn't really matter what the models look like.
  2. mm_cs = """
  3. MyAbstractClass:Class {
  4. abstract = True;
  5. }
  6. MyConcreteClass:Class
  7. :Inheritance (MyConcreteClass -> MyAbstractClass)
  8. Z:Class
  9. myZ:Association (MyAbstractClass -> Z) {
  10. target_lower_cardinality = 1;
  11. }
  12. """
  13. m_cs = """
  14. cc:MyConcreteClass
  15. z:Z
  16. :myZ (cc -> z)
  17. """
  18. # We parse everything:
  19. from state.devstate import DevState
  20. from bootstrap.scd import bootstrap_scd
  21. from util import loader
  22. state = DevState()
  23. mmm = bootstrap_scd(state)
  24. mm = loader.parse_and_check(state, mm_cs, mmm, "mm")
  25. m = loader.parse_and_check(state, m_cs, mm, "m")
  26. # We can query the model via an API called ODAPI (Object Diagram API):
  27. from api.od import ODAPI
  28. odapi = ODAPI(state, m, mm)
  29. ls = odapi.get_all_instances("MyAbstractClass", include_subtypes=True)
  30. print("result of get_all_instances:")
  31. print(ls)
  32. # Observing the output above, we see that we got a list of tuples (object_name, UUID).
  33. # We can also modify the model via the same API:
  34. (cc_name, cc_id) = ls[0]
  35. z2 = odapi.create_object("z2", "Z")
  36. odapi.create_link("lnk", "myZ", cc_id, z2)
  37. # And we can observe the modified model:
  38. from concrete_syntax.textual_od.renderer import render_od
  39. from concrete_syntax.common import indent
  40. print()
  41. print("the modified model:")
  42. print(indent(render_od(state, m, mm, hide_names=False), 2))
  43. # BTW, notice that the anonymous link of type 'myZ' from the original model was automatically given a unique name (starting with two underscores).
  44. # The full ODAPI is documented on page 6 of this PDF:
  45. # http://msdl.uantwerpen.be/people/hv/teaching/MSBDesign/202425/assignments/assignment6.pdf
  46. # On to the next tutorial...