예제 #1
0
    def get(self, TA, TB):
        assert isinstance(TA, MegaState_Target) 
        assert isinstance(TB, MegaState_Target) 

        if TA.drop_out_f:
            if TB.drop_out_f:
                return TA
            TA_scheme = self.__drop_out_scheme_a

        elif TA.target_state_index is not None:
            if TB.target_state_index is not None and TA.target_state_index == TB.target_state_index:
                return TA
            TA_scheme = (TA.target_state_index,) * self.__length_a

        else:
            TA_scheme = TA.scheme

        if TB.drop_out_f:
            # TA was not drop-out, otherwise we would have returned earlier
            TB_scheme = self.__drop_out_scheme_b

        elif TB.target_state_index is not None:
            # TA was not the same door, otherwise we would have returned earlier
            TB_scheme = (TB.target_state_index,) * self.__length_b

        else:
            TB_scheme = TB.scheme

        return MegaState_Target.create(TA_scheme + TB_scheme)
예제 #2
0
    def prepare_transition_map(TM):
        """Character Path objects contain transition maps of the form

                list of (interval, DoorID)

        which has now to be transformed into the form

                list of (interval, MegaState_Target)

        This is the form that code generation requires for MegaState-s.
        """
        def adapt(Target):
            if isinstance(Target, DoorID): return Target.state_index
            return Target
        return [ (interval, MegaState_Target.create(adapt(target))) \
                 for interval, target in TM ]
예제 #3
0
파일: candidate.py 프로젝트: liancheng/rose
    def get(self, TA, TB):
        """RETURNS:
        
        A MegaState_Target which represents the combination of target A and
        target B. If both are equal the MegaState_Target may have the
        '.target_state_index' set. If not a 'scheme' is developped is
        developed, which determines a target based on a state key, i.e.
        'target_state_index = scheme[state_key]'.
        """
        assert isinstance(TA, MegaState_Target) 
        assert isinstance(TB, MegaState_Target) 

        if TA.drop_out_f:
            if TB.drop_out_f:
                return TA
            TA_scheme = self.__drop_out_scheme_a

        elif TA.target_state_index is not None:
            if TB.target_state_index is not None and TA.target_state_index == TB.target_state_index:
                return TA
            TA_scheme = (TA.target_state_index,) * self.__length_a

        else:
            TA_scheme = TA.scheme

        if TB.drop_out_f:
            # TA was not drop-out, otherwise we would have returned earlier
            TB_scheme = self.__drop_out_scheme_b

        elif TB.target_state_index is not None:
            # TA was not the same door, otherwise we would have returned earlier
            TB_scheme = (TB.target_state_index,) * self.__length_b

        else:
            TB_scheme = TB.scheme

        return MegaState_Target.create(TA_scheme + TB_scheme)
예제 #4
0
    def get(self, TA, TB):
        """RETURNS:
        
        A MegaState_Target which represents the combination of target A and
        target B. If both are equal the MegaState_Target may have the
        '.target_state_index' set. If not a 'scheme' is developped is
        developed, which determines a target based on a state key, i.e.
        'target_state_index = scheme[state_key]'.
        """
        assert isinstance(TA, MegaState_Target)
        assert isinstance(TB, MegaState_Target)

        if TA.drop_out_f:
            if TB.drop_out_f:
                return TA
            TA_scheme = self.__drop_out_scheme_a

        elif TA.target_state_index is not None:
            if TB.target_state_index is not None and TA.target_state_index == TB.target_state_index:
                return TA
            TA_scheme = (TA.target_state_index, ) * self.__length_a

        else:
            TA_scheme = TA.scheme

        if TB.drop_out_f:
            # TA was not drop-out, otherwise we would have returned earlier
            TB_scheme = self.__drop_out_scheme_b

        elif TB.target_state_index is not None:
            # TA was not the same door, otherwise we would have returned earlier
            TB_scheme = (TB.target_state_index, ) * self.__length_b

        else:
            TB_scheme = TB.scheme

        return MegaState_Target.create(TA_scheme + TB_scheme)
예제 #5
0
파일: state.py 프로젝트: liancheng/rose
def combine_maps(StateA, StateB):
    """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 MegaState_Target 
       objects. A MegaState_Target 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, MegaState_Targets must be combined with trigger targets
       and other MegaState_Targets.

       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 TemplateMegaState_Targets. 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 TemplateMegaState_Target 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.
    """
    transition_map_tools.assert_adjacency(StateA.transition_map,
                                          TotalRangeF=True)
    transition_map_tools.assert_adjacency(StateB.transition_map,
                                          TotalRangeF=True)

    MegaState_Target.init(
    )  # Initialize the tracking of generated MegaState_Target-s
    factory = TargetFactory(StateA, StateB)
    result = []
    for begin, end, a_target, b_target in transition_map_tools.zipped_iterable(
            StateA.transition_map, StateB.transition_map):
        target = factory.get(a_target, b_target)
        result.append((Interval(begin, end), target))

    # Return the database of generated MegaState_Target objects
    mega_state_target_db = MegaState_Target.disconnect_object_db()
    # Number of different target schemes:
    scheme_n = 0
    for x in (key for key in mega_state_target_db.iterkeys()
              if isinstance(key, tuple)):
        scheme_n += 1
    return result, scheme_n
예제 #6
0
파일: state.py 프로젝트: liancheng/rose
def combine_maps(StateA, StateB):
    """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 MegaState_Target 
       objects. A MegaState_Target 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, MegaState_Targets must be combined with trigger targets
       and other MegaState_Targets.

       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 TemplateMegaState_Targets. 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 TemplateMegaState_Target 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.
    """
    transition_map_tools.assert_adjacency(StateA.transition_map, TotalRangeF=True)
    transition_map_tools.assert_adjacency(StateB.transition_map, TotalRangeF=True)

    MegaState_Target.init() # Initialize the tracking of generated MegaState_Target-s
    factory = TargetFactory(StateA, StateB)
    result  = []
    for begin, end, a_target, b_target in transition_map_tools.zipped_iterable(StateA.transition_map, 
                                                                               StateB.transition_map):
        target = factory.get(a_target, b_target)
        result.append((Interval(begin, end), target))

    # Return the database of generated MegaState_Target objects
    mega_state_target_db = MegaState_Target.disconnect_object_db()
    # Number of different target schemes:
    scheme_n = 0
    for x in (key for key in mega_state_target_db.iterkeys() if isinstance(key, tuple)):
        scheme_n += 1
    return result, scheme_n