02_inheritance.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # The following meta-model has an inheritance relation:
  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. # Note that we didn't give our inheritance link a name. A unique name will be auto-generated by the parser.
  14. # A (non-conforming) instance:
  15. m_nonconform_cs = """
  16. cc:MyConcreteClass
  17. z:Z
  18. """
  19. # Check conformance:
  20. from state.devstate import DevState
  21. from bootstrap.scd import bootstrap_scd
  22. from util import loader
  23. state = DevState()
  24. mmm = bootstrap_scd(state)
  25. mm = loader.parse_and_check(state, mm_cs, mmm, "mm")
  26. print("should be non-conform:")
  27. m_nonconform = loader.parse_and_check(state, m_nonconform_cs, mm, "m_nonconform")
  28. # The reason for the non-conformance is that all cardinalities and constraints are inherited. Therefore 'MyConcreteClass' must have at least one outgoing 'myZ' link as well.
  29. # We fix the non-conformance by adding this link:
  30. m_conform_cs = m_nonconform_cs + """
  31. :myZ (cc -> z)
  32. """
  33. # Now everything will be fine
  34. print("should be conform:")
  35. m_conform = loader.parse_and_check(state, m_conform_cs, mm, "m_conform")
  36. print("OK")
  37. # On to the next tutorial...