globals.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from typing import *
  2. from sccd.util.namespace import *
  3. from sccd.util.duration import *
  4. from sccd.util.debug import *
  5. from sccd.common.exceptions import *
  6. # Global values for all statecharts in a class diagram.
  7. class Globals:
  8. def __init__(self):
  9. # All the event names in the model
  10. # self.events = Namespace()
  11. # self.out_events = Namespace()
  12. self.inports = Namespace()
  13. self.outports = Namespace()
  14. # All the duration literals occuring in action code expressions in the class diagram.
  15. self.durations: List[SCDurationLiteral] = []
  16. # The smallest unit for all durations in the model.
  17. # Upon simulation, all timestamps are multiples of this value.
  18. # Calculated after all expressions have been parsed, based on all DurationLiterals.
  19. self.delta: Optional[Duration] = None
  20. # parameter delta: if set, this will be the model delta.
  21. # otherwise, model delta will be the GCD of all durations registered.
  22. # typically, a 'delta' of 100us to 1ms is desirable because this will also be the 'precision' of input event timestamps.
  23. def init_durations(self, delta: Optional[Duration] = None):
  24. gcd_delta = gcd(*(d.d for d in self.durations))
  25. # Ensure delta not too big
  26. if delta:
  27. if duration(0) < gcd_delta < delta:
  28. raise ModelStaticError("Model contains duration deltas (smallest = %s) not representable with delta of %s." % (str(self.delta), str(delta)))
  29. else:
  30. self.delta = delta
  31. else:
  32. self.delta = gcd_delta
  33. if self.delta != duration(0):
  34. # Secretly convert all durations to integers of the same unit...
  35. for d in self.durations:
  36. d.opt = d.d // self.delta
  37. else:
  38. for d in self.durations:
  39. d.opt = 0
  40. def assert_ready(self):
  41. assert self.delta is not None # init_durations() not yet called