models.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Design meta-model
  2. woods_mm_cs = """
  3. Animal:Class {
  4. abstract = True;
  5. }
  6. Bear:Class
  7. :Inheritance (Bear -> Animal)
  8. Man:Class {
  9. lower_cardinality = 1;
  10. upper_cardinality = 2;
  11. constraint = `get_value(get_slot(this, "weight")) > 20`;
  12. }
  13. :Inheritance (Man -> Animal)
  14. Man_weight:AttributeLink (Man -> Integer) {
  15. name = "weight";
  16. optional = False;
  17. }
  18. afraidOf:Association (Man -> Animal) {
  19. source_upper_cardinality = 6;
  20. target_lower_cardinality = 1;
  21. }
  22. """
  23. # Runtime meta-model
  24. woods_rt_mm_cs = woods_mm_cs + """
  25. AnimalState:Class {
  26. abstract = True;
  27. }
  28. AnimalState_dead:AttributeLink (AnimalState -> Boolean) {
  29. name = "dead";
  30. optional = False;
  31. }
  32. of:Association (AnimalState -> Animal) {
  33. source_lower_cardinality = 1;
  34. source_upper_cardinality = 1;
  35. target_lower_cardinality = 1;
  36. target_upper_cardinality = 1;
  37. }
  38. BearState:Class {
  39. constraint = `get_type_name(get_target(get_outgoing(this, "of")[0])) == "Bear"`;
  40. }
  41. :Inheritance (BearState -> AnimalState)
  42. BearState_hunger:AttributeLink (BearState -> Integer) {
  43. name = "hunger";
  44. optional = False;
  45. constraint = ```
  46. val = get_value(get_target(this))
  47. val >= 0 and val <= 100
  48. ```;
  49. }
  50. ManState:Class {
  51. constraint = `get_type_name(get_target(get_outgoing(this, "of")[0])) == "Man"`;
  52. }
  53. :Inheritance (ManState -> AnimalState)
  54. attacking:Association (AnimalState -> ManState) {
  55. # Animal can only attack one Man at a time
  56. target_upper_cardinality = 1;
  57. # Man can only be attacked by one Animal at a time
  58. source_upper_cardinality = 1;
  59. constraint = ```
  60. attacker = get_source(this)
  61. if get_type_name(attacker) == "BearState":
  62. # only BearState has 'hunger' attribute
  63. hunger = get_value(get_slot(attacker, "hunger"))
  64. else:
  65. hunger = 100 # Man can always attack
  66. attacker_dead = get_value(get_slot(attacker, "dead"))
  67. attacked_state = get_target(this)
  68. attacked_dead = get_value(get_slot(attacked_state, "dead"))
  69. (
  70. hunger >= 50
  71. and not attacker_dead # cannot attack while dead
  72. and not attacked_dead # cannot attack whoever is dead
  73. )
  74. ```;
  75. }
  76. attacking_starttime:AttributeLink (attacking -> Integer) {
  77. name = "starttime";
  78. optional = False;
  79. constraint = ```
  80. val = get_value(get_target(this))
  81. _, clock = get_all_instances("Clock")[0]
  82. current_time = get_slot_value(clock, "time")
  83. val >= 0 and val <= current_time
  84. ```;
  85. }
  86. # Just a clock singleton for keeping the time
  87. Clock:Class {
  88. lower_cardinality = 1;
  89. upper_cardinality = 1;
  90. }
  91. Clock_time:AttributeLink (Clock -> Integer) {
  92. name = "time";
  93. optional = False;
  94. constraint = `get_value(get_target(this)) >= 0`;
  95. }
  96. """
  97. # Our design model - the part that doesn't change
  98. woods_m_cs = """
  99. george:Man {
  100. weight = 80;
  101. }
  102. bill:Man {
  103. weight = 70;
  104. }
  105. teddy:Bear
  106. mrBrown:Bear
  107. # george is afraid of both bears
  108. :afraidOf (george -> teddy)
  109. :afraidOf (george -> mrBrown)
  110. # the men are afraid of each other
  111. :afraidOf (bill -> george)
  112. :afraidOf (george -> bill)
  113. """
  114. # Our runtime model - the part that changes with every execution step
  115. woods_rt_initial_m_cs = woods_m_cs + """
  116. georgeState:ManState {
  117. dead = False;
  118. }
  119. :of (georgeState -> george)
  120. billState:ManState {
  121. dead = False;
  122. }
  123. :of (billState -> bill)
  124. teddyState:BearState {
  125. dead = False;
  126. hunger = 40;
  127. }
  128. :of (teddyState -> teddy)
  129. mrBrownState:BearState {
  130. dead = False;
  131. hunger = 80;
  132. }
  133. :of (mrBrownState -> mrBrown)
  134. clock:Clock {
  135. time = 0;
  136. }
  137. """