def test(Name, TM_A, TM_B):
    tm_a = TransitionMap.from_iterable((Interval(x[0], x[1]), x[2]) for x in TM_A)
    tm_b = TransitionMap.from_iterable((Interval(x[0], x[1]), x[2]) for x in TM_B)
    print "____________________________________________________________________"
    print
    print "Transition Map A:"
    print tm_a.get_string("dec", IntervalF=False),
    print
    print "Transition Map B:"
    print tm_b.get_string("dec", IntervalF=False),
    print
    print "Result:"
    for begin, end, a, b in TransitionMap.izip(tm_a, tm_b):
        x = "%i" % begin if begin != -sys.maxint else "-oo"
        y = "%i" % end   if end   != sys.maxint else "+oo"
        print "  [%3s:%3s)   %s  %s" % (x, y, a, b)
    print
    print "Result (switched):"
    for begin, end, a, b in TransitionMap.izip(tm_b, tm_a):
        x = "%i" % begin if begin != -sys.maxint else "-oo"
        y = "%i" % end   if end   != sys.maxint else "+oo"
        print "  [%3s:%3s)   %s  %s" % (x, y, a, b)
def test(Name, TM_A, TM_B):
    tm_a = TransitionMap.from_iterable((Interval(x[0], x[1]), x[2]) for x in TM_A)
    tm_b = TransitionMap.from_iterable((Interval(x[0], x[1]), x[2]) for x in TM_B)
    print "____________________________________________________________________"
    print
    print "Transition Map A:"
    print tm_a.get_string("dec", IntervalF=False),
    print
    print "Transition Map B:"
    print tm_b.get_string("dec", IntervalF=False),
    print
    print "Result:"
    for begin, end, a, b in TransitionMap.izip(tm_a, tm_b):
        x = "%i" % begin if begin != -INTEGER_MAX else "-oo"
        y = "%i" % end   if end   != INTEGER_MAX else "+oo"
        print "  [%3s:%3s)   %s  %s" % (x, y, a, b)
    print
    print "Result (switched):"
    for begin, end, a, b in TransitionMap.izip(tm_b, tm_a):
        x = "%i" % begin if begin != -INTEGER_MAX else "-oo"
        y = "%i" % end   if end   != INTEGER_MAX else "+oo"
        print "  [%3s:%3s)   %s  %s" % (x, y, a, b)
Пример #3
0
def _transition_cost_combined(TM_A, TM_B, ImplementedStateN):
    """Computes the storage consumption of a transition map.
    """
    # Count the number of unique schemes and the total interval number
    scheme_set       = set()
    uniform_target_n = 0
    interval_n       = 0
    for begin, end, a_target, b_target in TransitionMap.izip(TM_A, TM_B):
        interval_n += 1
        if     a_target.uniform_door_id is not None \
           and a_target.uniform_door_id == a_target.uniform_door_id:
            uniform_target_n += 1
        else:
            update_scheme_set(scheme_set, a_target, b_target)

    # The number of different schemes:
    scheme_n = len(scheme_set)

    return __transition_map_cost(ImplementedStateN, interval_n, scheme_n)
Пример #4
0
    def verify_transition_map(self, TheAnalyzer):
        """Each state which is implemented by this MegaState has a transition map.
        Given the state key of the represented state the TemplateState must know
        the exact transition map. Exceptions are transitions to DoorID-s which 
        have been replaced.

        This function relies on '._get_target_by_state_key()' being implemented
        by the derived class.
        """
        # Exceptions: replaced DoorID-s. No assumptions made on those targets.
        replaced_door_id_set = set(self.entry.transition_reassignment_db.itervalues())
        self_DoorID_drop_out = TheAnalyzer.drop_out_DoorID(self.index)

        # Iterate over all represented states of the MegaState
        for state_index in self.implemented_state_index_set():
            state_key = self.map_state_index_to_state_key(state_index)

            # TransitionMap of the represented state.
            original_tm = TheAnalyzer.state_db[state_index].transition_map
            # Compare for each interval original target and target(state_key)
            for begin, end, target, target_scheme in TransitionMap.izip(original_tm, self.transition_map):
                target_by_state_key = self._get_target_by_state_key(begin, end, target_scheme, state_key)
                if   target_by_state_key == target:               continue # The target must be the same, or
                elif target_by_state_key in replaced_door_id_set: continue # be a 'replaced one'.
                elif target.state_index == TheAnalyzer.reload_state.index: continue
                # A MegaState-s DropOut may represent any DropOut
                #elif     target_by_state_key == self_DoorID_drop_out  \
                elif target.drop_out_f():                         continue 

                print "#original:\n"    + original_tm.get_string("hex")
                print "#scheme:\n"      + self.transition_map.get_string("hex")
                print "#selfDropOut:\n" + str(self_DoorID_drop_out)
                print "#StateKey:        ", state_key
                print "#siseq:           ", self.ski_db.state_index_sequence
                print "#siseq[StateKey]: ", self.ski_db.state_index_sequence[state_key]
                print "# %s: tm -> %s; scheme[%s] -> %s;" % (Interval(begin, end).get_string("hex"), 
                                                             target, 
                                                             state_key, target_by_state_key)
                return False
        return True
Пример #5
0
    def verify_transition_map(self, TheAnalyzer):
        """Each state which is implemented by this MegaState has a transition map.
        Given the state key of the represented state the TemplateState must know
        the exact transition map. Exceptions are transitions to DoorID-s which 
        have been replaced.

        This function relies on '._get_target_by_state_key()' being implemented
        by the derived class.
        """
        # Exceptions: replaced DoorID-s. No assumptions made on those targets.
        replaced_door_id_set = set(self.entry.transition_reassignment_db.itervalues())
        self_DoorID_drop_out = TheAnalyzer.drop_out_DoorID(self.index)

        # Iterate over all represented states of the MegaState
        for state_index in self.implemented_state_index_set():
            state_key = self.map_state_index_to_state_key(state_index)

            # TransitionMap of the represented state.
            original_tm = TheAnalyzer.state_db[state_index].transition_map
            # Compare for each interval original target and target(state_key)
            for begin, end, target, target_scheme in TransitionMap.izip(original_tm, self.transition_map):
                target_by_state_key = self._get_target_by_state_key(begin, end, target_scheme, state_key)
                if   target_by_state_key == target:               continue # The target must be the same, or
                elif target_by_state_key in replaced_door_id_set: continue # be a 'replaced one'.
                elif target.state_index == TheAnalyzer.reload_state.index: continue
                # A MegaState-s DropOut may represent any DropOut
                #elif     target_by_state_key == self_DoorID_drop_out  \
                elif target.drop_out_f():                         continue 

                print "#original:\n"    + original_tm.get_string("hex")
                print "#scheme:\n"      + self.transition_map.get_string("hex")
                print "#selfDropOut:\n" + str(self_DoorID_drop_out)
                print "#StateKey:        ", state_key
                print "#siseq:           ", self.ski_db.state_index_sequence
                print "#siseq[StateKey]: ", self.ski_db.state_index_sequence[state_key]
                print "# %s: tm -> %s; scheme[%s] -> %s;" % (Interval(begin, end).get_string("hex"), 
                                                             target, 
                                                             state_key, target_by_state_key)
                return False
        return True
Пример #6
0
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
Пример #7
0
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