예제 #1
0
def get_Analyzer(StatesDescription):
    """StatesDescription: List of pairs:
         
           (state index, transition map)

       That is, it tells what state of a given state index has what transition
       map. The transition map is a list of pairs

           (interval, target state index)
    """
    # Use 'BACKWARD_PRE_CONTEXT' so that the drop-out objects are created
    # without larger analysis.
    init_state_index = 7777L
    analyzer = Analyzer(engine.BACKWARD_PRE_CONTEXT, init_state_index)
    all_state_index_set = set()
    for state_index, transition_map in StatesDescription:
        assert isinstance(state_index, long)
        assert isinstance(transition_map, list)
        tm = TransitionMap.from_iterable(transition_map)
        tm.fill_gaps(E_StateIndices.DROP_OUT,
                     Setup.buffer_codec.drain_set.minimum(),
                     Setup.buffer_codec.drain_set.supremum())
        analyzer.state_db[state_index] = get_AnalyzerState(state_index, tm)
        all_state_index_set.update(x[1] for x in transition_map)

    # 'Dummy' transitions from init state to all
    analyzer.state_db[init_state_index] = get_AnalyzerState_Init(
        init_state_index, [x[0] for x in StatesDescription])

    # Make sure, that all states mentioned in the transition map really exist.
    for i in all_state_index_set:
        if i in analyzer.state_db: continue
        analyzer.state_db[i] = get_AnalyzerState(
            i, TransitionMap.from_iterable([]))

    # Make sure, that the transitions appear in the 'entry' member of the
    # states. Collect transition information.
    for state_index, transition_map in StatesDescription:
        for interval, target_index in transition_map:
            if not isinstance(target_index, (long, int)): continue
            analyzer.state_db[target_index].entry.enter(
                target_index, state_index, TransitionAction())
        analyzer.state_db[state_index].entry.enter(state_index,
                                                   init_state_index,
                                                   TransitionAction())

    for state in analyzer.state_db.itervalues():
        state.entry.categorize(state.index)

    # Make sure that every state has its entry into drop-out
    empty_cl = OpList()
    for i in analyzer.state_db.iterkeys():
        analyzer.drop_out.entry.enter_OpList(E_StateIndices.DROP_OUT, i,
                                             copy(empty_cl))
    analyzer.drop_out.entry.categorize(E_StateIndices.DROP_OUT)

    analyzer.prepare_DoorIDs()

    return analyzer
예제 #2
0
def get_Analyzer(StatesDescription):
    """StatesDescription: List of pairs:
         
           (state index, transition map)

       That is, it tells what state of a given state index has what transition
       map. The transition map is a list of pairs

           (interval, target state index)
    """
    # Use 'BACKWARD_PRE_CONTEXT' so that the drop-out objects are created
    # without larger analysis.
    init_state_index = 7777L
    analyzer = Analyzer(engine.BACKWARD_PRE_CONTEXT, init_state_index)
    all_state_index_set = set()
    for state_index, transition_map in StatesDescription:
        assert isinstance(state_index, long)
        assert isinstance(transition_map, list)
        tm = TransitionMap.from_iterable(transition_map)
        tm.fill_gaps(E_StateIndices.DROP_OUT,
                     Setup.buffer_codec.drain_set.minimum(), 
                     Setup.buffer_codec.drain_set.supremum())
        analyzer.state_db[state_index] = get_AnalyzerState(state_index, tm)
        all_state_index_set.update(x[1] for x in transition_map)

    # 'Dummy' transitions from init state to all
    analyzer.state_db[init_state_index] = get_AnalyzerState_Init(init_state_index, 
                                                                 [ x[0] for x in StatesDescription ])

    # Make sure, that all states mentioned in the transition map really exist.
    for i in all_state_index_set:
        if i in analyzer.state_db: continue
        analyzer.state_db[i] = get_AnalyzerState(i, TransitionMap.from_iterable([]))

    # Make sure, that the transitions appear in the 'entry' member of the
    # states. Collect transition information.
    for state_index, transition_map in StatesDescription:
        for interval, target_index in transition_map:
            if not isinstance(target_index, (long, int)): continue
            analyzer.state_db[target_index].entry.enter(target_index, state_index, TransitionAction())
        analyzer.state_db[state_index].entry.enter(state_index, init_state_index, TransitionAction())

    for state in analyzer.state_db.itervalues():
        state.entry.categorize(state.index)

    # Make sure that every state has its entry into drop-out
    empty_cl = OpList()
    for i in analyzer.state_db.iterkeys():
        analyzer.drop_out.entry.enter_OpList(E_StateIndices.DROP_OUT, i, copy(empty_cl))
    analyzer.drop_out.entry.categorize(E_StateIndices.DROP_OUT)

    analyzer.prepare_DoorIDs()

    return analyzer
예제 #3
0
파일: code.py 프로젝트: xxyzzzq/quex
def relate_to_TransitionCode(tm):
    assert tm is not None
    tm.assert_continuity()
    tm.assert_adjacency()
    tm.assert_boundary(Setup.buffer_codec.lexatom_range.begin,
                       Setup.buffer_codec.lexatom_range.end)

    def make_str(X):
        txt = X.code()
        if isinstance(X, (str, unicode)): return txt
        else: return "".join(txt)

    return TransitionMap.from_iterable(
        (interval, make_str(x)) for interval, x in TransitionMap.from_iterable(
            tm, TransitionCodeFactory.do))
예제 #4
0
파일: code.py 프로젝트: mplucinski/quex
def relate_to_TransitionCode(tm):
    assert tm is not None
    tm.assert_continuity()
    tm.assert_adjacency()
    tm.assert_boundary(Setup.buffer_codec.drain_set.minimum(), 
                       Setup.buffer_codec.drain_set.supremum()) 

    def make_str(X):
        txt = X.code()
        if isinstance(X, (str, unicode)): return txt
        else:                             return "".join(txt)

    return TransitionMap.from_iterable(
        (interval, make_str(x))
        for interval, x in TransitionMap.from_iterable(tm, TransitionCodeFactory.do)
    )
예제 #5
0
def construct_tm(IntervalList):
    letter = ord('a')

    return TransitionMap.from_iterable([ 
       (Interval(x[0], x[1]), letter + i) 
       for i, x in enumerate(IntervalList) 
    ])
예제 #6
0
def get_transition_map(TM, StateIndex, DropOutCatcher=None):
    if DropOutCatcher is None:
        DropOutCatcher = AnalyzerState(sm_index.get(), TransitionMap())

    def get_door_id(Target):
        return DoorID(Target, 0)
    tm = TransitionMap.from_iterable(TM, get_door_id)
    return tm.relate_to_TargetByStateKeys(StateIndex, DropOutCatcher)
예제 #7
0
def get_transition_map(TM, StateIndex, DropOutCatcher=None):
    if DropOutCatcher is None:
        DropOutCatcher = AnalyzerState(sm_index.get(), TransitionMap())

    def get_door_id(Target):
        return DoorID(Target, 0)

    tm = TransitionMap.from_iterable(TM, get_door_id)
    return tm.relate_to_TargetByStateKeys(StateIndex, DropOutCatcher)
예제 #8
0
def get_transition_map(TM, StateIndex, DropOutCatcher=None):
    global dial_db
    if DropOutCatcher is None:
        DropOutCatcher = FSM_State(sm_index.get(), TransitionMap(), dial_db=dial_db)

    def get_door_id(Target):
        return DoorID(Target, 0, dial_db)
    tm = TransitionMap.from_iterable(TM, get_door_id)
    return tm.relate_to_TargetByStateKeys(StateIndex, DropOutCatcher)
def test(TM, Target="X"):
    tm = TransitionMap.from_iterable([ (Interval(x[0], x[1]), y) for x, y in TM ])
    print "____________________________________________________________________"
    print "BEFORE:"
    show(tm)
    tm.combine_adjacents()
    tm.assert_continuity(StrictF=True)
    print "AFTER:"
    show(tm)
예제 #10
0
def test(TM, Target="X"):
    tm = TransitionMap.from_iterable([ (Interval(x[0], x[1]), y) for x, y in TM ])
    print "____________________________________________________________________"
    print "BEFORE:"
    show(tm)
    tm.combine_adjacents()
    tm.assert_continuity(StrictF=True)
    print "AFTER:"
    show(tm)
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)
예제 #13
0
def test(TM_plain):
    global interval_begin

    print "#" + "-" * 79
    tm = TransitionMap.from_iterable((interval, "%s" % target.related_address)
                                     for interval, target in TM_plain)
    print_tm(tm)
    most_often_appearing_target, target_n = TransitionMap.get_target_statistics(
        tm)
    node = get_Bisection(copy(tm))
    print "    ---"
    for element in node.implement():
        print "    %s" % element,

    interval_begin = 0
예제 #14
0
def test(TM_plain):
    global interval_begin

    print "#" + "-" * 79
    tm = TransitionMap.from_iterable(
        (interval, long(target)) for interval, target in TM_plain
    )
    print_tm(tm)
    most_often_appearing_target, target_n = TransitionMap.get_target_statistics(tm)
    node = BranchTable(copy(tm), most_often_appearing_target)
    print "    ---"
    for element in node.implement():
        print "    %s" % element,

    interval_begin = 0
예제 #15
0
def test(TM_plain):
    global interval_begin

    print "#" + "-" * 79
    tm = TransitionMap.from_iterable(
        (interval, "%s" % target) for interval, target in TM_plain)
    print_tm(tm)
    most_often_appearing_target, target_n = TransitionMap.get_target_statistics(
        tm)
    node = BranchTable(copy(tm), most_often_appearing_target)
    prev = "\n"
    print "    ---"
    for element in node.implement():
        if prev and prev[-1] == "\n": print "    %s" % element,
        else: print element,
        prev = element
예제 #16
0
def test(TM_plain):
    global interval_begin

    print "#" + "-" * 79
    tm = TransitionMap.from_iterable(
        (interval, long(target)) for interval, target in TM_plain)
    print_tm(tm)
    node = ComparisonSequence(copy(tm))
    print "    ---"
    tm, default = ComparisonSequence.optimize(tm)
    print_tm(tm)
    print "    default:   %s" % repr(default)
    print "    ---"
    for element in node.implement():
        print "    %s" % element,

    interval_begin = 0
예제 #17
0
def test(TM_plain):
    global interval_begin

    print "#" + "-" * 79
    tm = TransitionMap.from_iterable(
        (interval, long(target))
        for interval, target in TM_plain
    )
    print_tm(tm)
    node = ComparisonSequence(copy(tm))
    print "    ---"
    tm, default = ComparisonSequence.optimize(tm)
    print_tm(tm)
    print "    default:   %s" % repr(default)
    print "    ---"
    for element in node.implement():
        print "    %s" % element,

    interval_begin = 0
예제 #18
0
def get_AnalyzerState_Init(InitStateIndex, StateIndexList):
    init_tm = TransitionMap.from_iterable( 
        (Interval(i), state_index) 
        for i, state_index in enumerate(StateIndexList) 
    )
    return get_AnalyzerState(InitStateIndex, init_tm)
choice, codec = {
        "A": ("A", ""),
        "B": ("B", ""),
        "C": ("C", ""),
        "A-UTF8": ("A", "UTF8"),
        "B-UTF8": ("B", "UTF8"),
        "C-UTF8": ("C", "UTF8"),
}[sys.argv[1]]

# initialize pseudo random generator: produces always the same numbers.
random.seed(110270)   # must set the seed for randomness, otherwise system time
#                     # is used which is no longer deterministic.

if choice == "A":
    tm0 = TransitionMap.from_iterable([
        (Interval(10,20),    1L), 
        (Interval(195,196),  1L),
        (Interval(51,70),    2L), 
        (Interval(261,280),  2L),
        (Interval(90,100),   3L), 
        (Interval(110,130),  3L),
        (Interval(150,151),  4L), 
        (Interval(151,190),  4L),
        (Interval(190,195),  5L), 
        (Interval(21,30),    5L),
        (Interval(197, 198), 6L), 
        (Interval(200,230),  7L), 
        (Interval(231,240),  7L),
        (Interval(250,260),  8L), 
        (Interval(71,80),    8L), 
    ])
예제 #20
0
def test(TM, Character, Target="<X>"):
    tm = TransitionMap.from_iterable(TM)
    print "____________________________________________________________________"
    print "   len(TM) = %i; Insert at %i;" % (len(TM), Character)
    tm.set_target(Character, Target)
    show(tm)
예제 #21
0
파일: state.py 프로젝트: nyulacska/gpr
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
예제 #22
0
def test(TM, Character, Target="<X>"):
    tm = TransitionMap.from_iterable(TM)
    print "____________________________________________________________________"
    print "   len(TM) = %i; Insert at %i;" % (len(TM), Character)
    tm.set_target(Character, Target)
    show(tm)
예제 #23
0
def get_AnalyzerState_Init(InitStateIndex, StateIndexList):
    init_tm = TransitionMap.from_iterable(
        (Interval(i), state_index)
        for i, state_index in enumerate(StateIndexList))
    return get_AnalyzerState(InitStateIndex, init_tm)
예제 #24
0
def get_TransitionMap_with_TargetByStateKeys(TM_brief):
    return TransitionMap.from_iterable(
        (interval, get_TargetByStateKey(target_state_index_list))
        for interval, target_state_index_list in TM_brief)
예제 #25
0
파일: state.py 프로젝트: mplucinski/quex
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
예제 #26
0
def get_TransitionMap_with_TargetByStateKeys(TM_brief):
    return TransitionMap.from_iterable(
        (interval, get_TargetByStateKey(target_state_index_list))
        for interval, target_state_index_list in TM_brief
    )