def print_combination_result(combined, A, B, A_name, B_name): print "State %s:" % A_name, A.state_index_sequence() print "State %s:" % B_name, B.state_index_sequence() #if True: # cmd_tree = CommandTree.from_AnalyzerState(combined) # print "".join(cmd_tree.shared_tail_db.get_tree_text()) print "Result:\n" TargetByStateKey.assign_scheme_ids(combined.transition_map) print_tm(combined.transition_map, combined.state_index_sequence()) print print_metric(combined.transition_map) print "\n"
def do(TheAnalyzer): """MegaState Analysis _____________________________________________________ Normal states are potentially absorbed by MegaState-s which represent more than one single state at once. The setting 'Setup.compression_type_list' defines what type of algorithms have to be executed in to construct MegaStates (if any at all). Consider 'core.py' in this directory for further reading. ___________________________________________________________________________ NOTE: MegaState-s apply some 'mechanics' for implementing the state which they represent. However, the TransitionMap-s of other states are not effected. They remain targetting the same DoorID-s. Example: ( 1 )--- 'a' -->[Door0]-->( 2 )--- 'b' ---> ( 3 ) / / ( 4 )--- 'c' -->[Door1]--' / / ( 5 )--- 'd' -->[Door2]-->( 6 )--- 'e' --' After implementing 2 and 6 in a MegaState: MegaState .--------------------. ( 1 )--- 'a' -->[Door0] ... [state=2?]--- 'b' ---> ( 3 ) | | / / ( 4 )--- 'c' -->[Door1] ... [state=2?]---' / | | / ( 5 )--- 'd' -->[Door2] ... [state=6?]--- 'e' --' '--------------------' So, from outside, there is no observable change in behavior. Other states do not 'feel' that there is a MegaState. ___________________________________________________________________________ """ assert len(Setup.compression_type_list) != 0 mega_state_list = [] # The 'remainder' keeps track of states which have not yet been # absorbed into a MegaState. remainder = set(TheAnalyzer.state_db.iterkeys()) remainder.remove(TheAnalyzer.init_state_index) for ctype in Setup.compression_type_list: # -- MegaState-s by Path-Compression if ctype in (E_Compression.PATH, E_Compression.PATH_UNIFORM): new_mega_state_list = path_analyzer.do(TheAnalyzer, ctype, remainder) # -- MegaState-s by Template-Compression elif ctype in (E_Compression.TEMPLATE, E_Compression.TEMPLATE_UNIFORM): new_mega_state_list = template_analyzer.do( TheAnalyzer, Setup.compression_template_min_gain, ctype, remainder) else: assert False for mega_state in new_mega_state_list: mega_state.finalize(TheAnalyzer, ctype) mega_state.assert_consistency(ctype, remainder, TheAnalyzer) # -- Track the remaining not-yet-absorbed states for mega_state in new_mega_state_list: remainder.difference_update( mega_state.implemented_state_index_set()) mega_state_list.extend(new_mega_state_list) # Only now: We enter the MegaState-s into the 'state_db'. If it was done before, # the MegaState-s might try to absorb each other. TheAnalyzer.add_mega_states(mega_state_list) for mega_state in mega_state_list: if isinstance(mega_state, TemplateState): ## TargetByStateKey.rejoin_uniform_schemes(mega_state.transition_map) TargetByStateKey.assign_scheme_ids(mega_state.transition_map) return
def do(TheAnalyzer): """MegaState Analysis _____________________________________________________ Normal states are potentially absorbed by MegaState-s which represent more than one single state at once. The setting 'Setup.compression_type_list' defines what type of algorithms have to be executed in to construct MegaStates (if any at all). Consider 'core.py' in this directory for further reading. ___________________________________________________________________________ NOTE: MegaState-s apply some 'mechanics' for implementing the state which they represent. However, the TransitionMap-s of other states are not effected. They remain targetting the same DoorID-s. Example: ( 1 )--- 'a' -->[Door0]-->( 2 )--- 'b' ---> ( 3 ) / / ( 4 )--- 'c' -->[Door1]--' / / ( 5 )--- 'd' -->[Door2]-->( 6 )--- 'e' --' After implementing 2 and 6 in a MegaState: MegaState .--------------------. ( 1 )--- 'a' -->[Door0] ... [state=2?]--- 'b' ---> ( 3 ) | | / / ( 4 )--- 'c' -->[Door1] ... [state=2?]---' / | | / ( 5 )--- 'd' -->[Door2] ... [state=6?]--- 'e' --' '--------------------' So, from outside, there is no observable change in behavior. Other states do not 'feel' that there is a MegaState. ___________________________________________________________________________ """ assert len(Setup.compression_type_list) != 0 mega_state_list = [] # The 'remainder' keeps track of states which have not yet been # absorbed into a MegaState. remainder = set(TheAnalyzer.state_db.iterkeys()) remainder.remove(TheAnalyzer.init_state_index) for ctype in Setup.compression_type_list: # -- MegaState-s by Path-Compression if ctype in (E_Compression.PATH, E_Compression.PATH_UNIFORM): new_mega_state_list = path_analyzer.do(TheAnalyzer, ctype, remainder) # -- MegaState-s by Template-Compression elif ctype in (E_Compression.TEMPLATE, E_Compression.TEMPLATE_UNIFORM): new_mega_state_list = template_analyzer.do(TheAnalyzer, Setup.compression_template_min_gain, ctype, remainder) else: assert False for mega_state in new_mega_state_list: mega_state.finalize(TheAnalyzer, ctype) mega_state.assert_consistency(ctype, remainder, TheAnalyzer) # -- Track the remaining not-yet-absorbed states for mega_state in new_mega_state_list: remainder.difference_update(mega_state.implemented_state_index_set()) mega_state_list.extend(new_mega_state_list) # Only now: We enter the MegaState-s into the 'state_db'. If it was done before, # the MegaState-s might try to absorb each other. TheAnalyzer.add_mega_states(mega_state_list) for mega_state in mega_state_list: if isinstance(mega_state, TemplateState): ## TargetByStateKey.rejoin_uniform_schemes(mega_state.transition_map) TargetByStateKey.assign_scheme_ids(mega_state.transition_map) return
def get_TargetByStateKey(TargetStateIndexList): scheme = [ get_TargetByStateKey_Element(target_state_index) for target_state_index in TargetStateIndexList ] return TargetByStateKey.from_scheme(scheme)
def combine_maps(TransitionMap_A, TransitionMap_B): """RETURNS: -- Transition map = combined transition map of StateA and StateB. -- List of target schemes that have been identified. NOTE: If the entries of both states are uniform, then a transition to itself of both states can be implemented as a recursion of the template state without knowing the particular states. EXPLANATION: This function combines two transition maps. A transition map is a list of tuples: [ ... (interval, target) ... ] Each tuple tells about a character range [interval.begin, interval.end) where the state triggers to the given target. In a normal AnalyzerState the target is the index of the target state. In a TemplateState, though, multiple states are combined. A TemplateState operates on behalf of a state which is identified by its 'state_key'. If two states (even TemplateStates) are combined the trigger maps are observed, e.g. Trigger Map A Trigger Map B [ [ ([0, 10), DropOut) ([0, 10), State_4) ([10, 15), State_0) ([10, 15), State_1) ([15, 20), DropOut) ([15, 20), State_0) ([20, 21), State_1) ([20, 21), DropOut) ([21, 255), DropOut) ([21, 255), State_0) ] ] For some intervals, the target is the same. But for some it is different. In a TemplateState, the intervals are associated with TargetByStateKey objects. A TargetByStateKey object tells the target state dependent on the 'state_key'. The above example may result in a transition map as below: Trigger Map A [ # intervals: target schemes: ( [0, 10), { A: DropOut, B: State_4, }, ( [10, 15), { A: State_0, B: State_1, }, ( [15, 20), { A: DropOut, B: State_0, }, ( [20, 21), { A: State_1, B: DropOut, }, ( [21, 255), { A: DropOut, B: State_0, }, ] Note, that the 'scheme' for interval [12, 20) and [21, 255) are identical. We try to profit from it by storing only it only once. A template scheme is associated with an 'index' for reference. TemplateStates may be combined with AnalyzerStates and other TemplateStates. Thus, TargetByStateKey objects must be combined with trigger targets and other TargetByStateKey objects. NOTE: The resulting target map results from the combination of both transition maps, which may introduce new borders, e.g. |----------------| (where A triggers to X) |---------------| (where B triggers to Y) becomes |----|-----------|---| 1 2 3 where: Domain: A triggers to: B triggers to: 1 X Nothing 2 X Y 3 Nothing Y ----------------------------------------------------------------------------- Transition maps of TemplateState-s function based on 'state_keys'. Those state keys are used as indices into TargetByStateKey-s. The 'state_key' of a given state relates to the 'state_index' by (1) self.state_index_sequence[state_key] == state_index where 'state_index' is the number by which the state is identified inside its state machine. Correspondingly, for a given TargetByStateKey T (2) T[state_key] gives the target of the template if it operates for 'state_index' determined from 'state_key' by relation (1). The state index list approach facilitates the computation of target schemes. For this reason no dictionary {state_index->target} is used. NOTE: To this point, there is no '.relate_to_DoorIDs()' required in the transition map. A transition map such as [INTERVAL] [TARGET] [-oo, 97] --> DropOut [98] --> Scheme((12, 32, DROP_OUT)) [99] --> Scheme((DROP_OUT, 13, 51)) [100, oo] --> DropOut lets find the transition '(source_state_index, to_state_index)' for each entry in a scheme. E.g. the second entry in the second scheme is the target state '32'. The 'state_index_sequence' might tell that the second entry in a scheme is to represent the transitions of state '57'. Then, it is clear that the door relating to transition '57->32' must be targetted. """ TransitionMap_A.assert_adjacency(TotalRangeF=True) TransitionMap_B.assert_adjacency(TotalRangeF=True) scheme_pair_db = {} result = TransitionMap.from_iterable( ((Interval(begin, end), TargetByStateKey.from_2_TargetByStateKeys(a_target, b_target, scheme_pair_db))) for begin, end, a_target, b_target in TransitionMap.izip(TransitionMap_A, TransitionMap_B) ) # Number of different target schemes: scheme_n = len(scheme_pair_db) return result, scheme_n
def combine_maps(TransitionMap_A, TransitionMap_B): """RETURNS: -- Transition map = combined transition map of StateA and StateB. -- List of target schemes that have been identified. NOTE: If the entries of both states are uniform, then a transition to itself of both states can be implemented as a recursion of the template state without knowing the particular states. EXPLANATION: This function combines two transition maps. A transition map is a list of tuples: [ ... (interval, target) ... ] Each tuple tells about a character range [interval.begin, interval.end) where the state triggers to the given target. In a normal AnalyzerState the target is the index of the target state. In a TemplateState, though, multiple states are combined. A TemplateState operates on behalf of a state which is identified by its 'state_key'. If two states (even TemplateStates) are combined the trigger maps are observed, e.g. Trigger Map A Trigger Map B [ [ ([0, 10), DropOut) ([0, 10), State_4) ([10, 15), State_0) ([10, 15), State_1) ([15, 20), DropOut) ([15, 20), State_0) ([20, 21), State_1) ([20, 21), DropOut) ([21, 255), DropOut) ([21, 255), State_0) ] ] For some intervals, the target is the same. But for some it is different. In a TemplateState, the intervals are associated with TargetByStateKey objects. A TargetByStateKey object tells the target state dependent on the 'state_key'. The above example may result in a transition map as below: Trigger Map A [ # intervals: target schemes: ( [0, 10), { A: DropOut, B: State_4, }, ( [10, 15), { A: State_0, B: State_1, }, ( [15, 20), { A: DropOut, B: State_0, }, ( [20, 21), { A: State_1, B: DropOut, }, ( [21, 255), { A: DropOut, B: State_0, }, ] Note, that the 'scheme' for interval [12, 20) and [21, 255) are identical. We try to profit from it by storing only it only once. A template scheme is associated with an 'index' for reference. TemplateStates may be combined with AnalyzerStates and other TemplateStates. Thus, TargetByStateKey objects must be combined with trigger targets and other TargetByStateKey objects. NOTE: The resulting target map results from the combination of both transition maps, which may introduce new borders, e.g. |----------------| (where A triggers to X) |---------------| (where B triggers to Y) becomes |----|-----------|---| 1 2 3 where: Domain: A triggers to: B triggers to: 1 X Nothing 2 X Y 3 Nothing Y ----------------------------------------------------------------------------- Transition maps of TemplateState-s function based on 'state_keys'. Those state keys are used as indices into TargetByStateKey-s. The 'state_key' of a given state relates to the 'state_index' by (1) self.state_index_sequence[state_key] == state_index where 'state_index' is the number by which the state is identified inside its state machine. Correspondingly, for a given TargetByStateKey T (2) T[state_key] gives the target of the template if it operates for 'state_index' determined from 'state_key' by relation (1). The state index list approach facilitates the computation of target schemes. For this reason no dictionary {state_index->target} is used. NOTE: To this point, there is no '.relate_to_DoorIDs()' required in the transition map. A transition map such as [INTERVAL] [TARGET] [-oo, 97] --> DropOut [98] --> Scheme((12, 32, DROP_OUT)) [99] --> Scheme((DROP_OUT, 13, 51)) [100, oo] --> DropOut lets find the transition '(source_state_index, to_state_index)' for each entry in a scheme. E.g. the second entry in the second scheme is the target state '32'. The 'state_index_sequence' might tell that the second entry in a scheme is to represent the transitions of state '57'. Then, it is clear that the door relating to transition '57->32' must be targetted. """ TransitionMap_A.assert_adjacency(TotalRangeF=True) TransitionMap_B.assert_adjacency(TotalRangeF=True) scheme_pair_db = {} result = TransitionMap.from_iterable( ((Interval(begin, end), TargetByStateKey.from_2_TargetByStateKeys(a_target, b_target, scheme_pair_db))) for begin, end, a_target, b_target in TransitionMap.izip( TransitionMap_A, TransitionMap_B)) # Number of different target schemes: scheme_n = len(scheme_pair_db) return result, scheme_n
def relate(TargetDoorId): transition_id = TransitionID(TargetDoorId.state_index, StateIndex, TriggerId=0) door_id = TargetDoorId return TargetByStateKey.from_transition(transition_id, door_id)