瀏覽代碼

Rename some fields, now that only bitmaps remain.

Joeri Exelmans 5 年之前
父節點
當前提交
6a4316ef4a
共有 2 個文件被更改,包括 16 次插入27 次删除
  1. 8 14
      src/sccd/statechart/dynamic/statechart_execution.py
  2. 8 13
      src/sccd/statechart/static/tree.py

+ 8 - 14
src/sccd/statechart/dynamic/statechart_execution.py

@@ -20,7 +20,7 @@ class StatechartExecution:
         self.raise_next_bs = None
 
         # set of current states
-        self.configuration_bitmap: Bitmap = Bitmap()
+        self.configuration: Bitmap = Bitmap()
 
         # mapping from history state id to states to enter if history is target of transition
         self.history_values: Dict[int, Bitmap] = {}
@@ -36,7 +36,7 @@ class StatechartExecution:
     # enter default states
     def initialize(self):
         states = self.statechart.tree.root.target_states(self)
-        self.configuration_bitmap = states
+        self.configuration = states
 
         ctx = EvalContext(current_state=self, events=[], memory=self.rhs_memory)
         if self.statechart.datamodel is not None:
@@ -62,14 +62,8 @@ class StatechartExecution:
 
             timer.start("exit states")
             # Sequence of exit states is the intersection between set of current states and the arena's descendants.
-            exit_ids = self.configuration_bitmap & t.opt.arena.opt.descendants_bitmap
+            exit_ids = self.configuration & t.opt.arena.opt.descendants
             exit_set = self._ids_to_states(exit_ids.reverse_items())
-
-            # Alternative implementation:
-            # if len(self.configuration) < len(t.opt.arena.opt.descendants):
-            #     exit_set = (s for s in self.configuration if s.opt.state_id_bitmap & t.opt.arena.opt.descendants_bitmap)
-            # else:
-            #     exit_set = (s for s in t.opt.arena.opt.descendants if s.opt.state_id_bitmap & self.configuration_bitmap)
             timer.stop("exit states")
 
 
@@ -78,7 +72,7 @@ class StatechartExecution:
             # Enter path is the intersection between:
             #   1) the transitions target and its ancestors, and
             #   2) the arena's descendants
-            enter_path = (t.targets[0].opt.state_id_bitmap | t.targets[0].opt.ancestors_bitmap) & t.opt.arena.opt.descendants_bitmap
+            enter_path = (t.targets[0].opt.state_id_bitmap | t.targets[0].opt.ancestors) & t.opt.arena.opt.descendants
             # Now, along the enter path, there may be AND-states whose children we don't explicitly enter, but should enter.
             # That's why we call 'additional_target_states' on every state on the path and join the results.
             # Finally, on the actual target itself, we call 'target_states' and add it to the results as well.
@@ -111,7 +105,7 @@ class StatechartExecution:
                     self.history_values[h.opt.state_id] = exit_ids & mask
                     # print(list(self._ids_to_states(self.history_values[h.opt.state_id].items())))
                 self._perform_actions(ctx, s.exit)
-                self.configuration_bitmap &= ~s.opt.state_id_bitmap
+                self.configuration &= ~s.opt.state_id_bitmap
             timer.stop("exit states")
 
             # execute transition action(s)
@@ -125,7 +119,7 @@ class StatechartExecution:
             # enter states...
             for s in enter_set:
                 print_debug(termcolor.colored('  ENTER %s' % s.opt.full_name, 'green'))
-                self.configuration_bitmap |= s.opt.state_id_bitmap
+                self.configuration |= s.opt.state_id_bitmap
                 self._perform_actions(ctx, s.enter)
                 self._start_timers(s.opt.after_triggers)
             timer.stop("enter states")
@@ -164,7 +158,7 @@ class StatechartExecution:
             raise
 
     def check_source(self, t) -> bool:
-        return self.configuration_bitmap & t.source.opt.state_id_bitmap
+        return self.configuration & t.source.opt.state_id_bitmap
 
     @staticmethod
     def _perform_actions(ctx: EvalContext, actions: List[Action]):
@@ -187,7 +181,7 @@ class StatechartExecution:
     # Return whether the current configuration includes ALL the states given.
     def in_state(self, state_strings: List[str]) -> bool:
         state_ids_bitmap = states_to_bitmap((self.statechart.tree.state_dict[state_string] for state_string in state_strings))
-        in_state = self.configuration_bitmap.has_all(state_ids_bitmap)
+        in_state = self.configuration.has_all(state_ids_bitmap)
         # if in_state:
         #     print_debug("in state"+str(state_strings))
         # else:

+ 8 - 13
src/sccd/statechart/static/tree.py

@@ -44,10 +44,8 @@ class StateOptimization:
     state_id: int = -1
     state_id_bitmap: Bitmap = Bitmap() # bitmap with only state_id-bit set
 
-    ancestors: List[State] = field(default_factory=list) # order: close to far away, i.e. first element is parent
-    ancestors_bitmap: Bitmap = Bitmap()
-    descendants: List[State] = field(default_factory=list)  # order: depth-first
-    descendants_bitmap: Bitmap = Bitmap()
+    ancestors: Bitmap = Bitmap()
+    descendants: Bitmap = Bitmap()
 
     history: List[Tuple[State, Bitmap]] = field(default_factory=list) # subset of children
     static_ts_bitmap: Bitmap = Bitmap() # Subset of descendants that are always entered when this state is the target of a transition
@@ -84,7 +82,7 @@ class DeepHistoryState(HistoryState):
 
     def history_mask(self) -> Bitmap:
         # All descendants of parent:
-        return self.parent.opt.descendants_bitmap
+        return self.parent.opt.descendants
 
     def __repr__(self):
         return "DeepHistoryState(\"%s\")" % (self.opt.full_name)
@@ -254,16 +252,13 @@ class StateTree:
                 self.stable_bitmap |= state.opt.state_id_bitmap
 
         def set_ancestors(state: State, ancestors=[]):
-            state.opt.ancestors = ancestors
-            state.opt.ancestors_bitmap = states_to_bitmap(ancestors)
+            state.opt.ancestors = states_to_bitmap(ancestors)
             return ancestors + [state]
 
         def set_descendants(state: State, children_descendants):
-            # flatten list of lists
-            descendants = list(itertools.chain.from_iterable(children_descendants))
+            descendants = reduce(lambda x,y: x|y, children_descendants, Bitmap())
             state.opt.descendants = descendants
-            state.opt.descendants_bitmap = states_to_bitmap(descendants)
-            return [state] + descendants
+            return state.opt.state_id_bitmap | descendants
 
         def set_static_target_states(state: State, _):
             if isinstance(state, ParallelState):
@@ -301,7 +296,7 @@ class StateTree:
 
         for t in self.transition_list:
             # intersection between source & target ancestors, last member in depth-first sorted state list.
-            lca_id = (t.source.opt.ancestors_bitmap & t.targets[0].opt.ancestors_bitmap).highest_bit()
+            lca_id = (t.source.opt.ancestors & t.targets[0].opt.ancestors).highest_bit()
             lca = self.state_list[lca_id]
             arena = lca
             while isinstance(arena, (ParallelState, HistoryState)):
@@ -309,6 +304,6 @@ class StateTree:
 
             t.opt = TransitionOptimization(
                 arena=arena,
-                arena_bitmap=arena.opt.descendants_bitmap | arena.opt.state_id_bitmap)
+                arena_bitmap=arena.opt.descendants | arena.opt.state_id_bitmap)
 
         timer.stop("optimize tree")