예제 #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_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)
예제 #3
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)
예제 #4
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
예제 #5
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
예제 #6
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
예제 #7
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))
예제 #8
0
    def implement(self):
        L = len(self.sub_map)
        assert L != 0

        tm = [
            (interval, "".join(transition.do(interval, target)))
            for interval, target in self.sub_map
        ]

        if len(tm) == 1:
            return Lng.COMPARISON_SEQUENCE(tm, None)

        tm, default = ComparisonSequence.optimize(tm)

        # The buffer limit code is appears extreme seldomly
        # => if it's there, make sure that it is tested at last. 
        #    (This might require to reverse the trigger map.)
        # The 'BLC' might actually no longer occur in the optimized map. Thus, 
        # search for it in the original transition map.
        blc_index = TransitionMap.bisect(self.sub_map, Setup.buffer_limit_code)
        if blc_index is not None and blc_index < L / 2:
            def get_decision(interval, i, L):
                if   i == L-1:             return Lng.ELSE_SIMPLE
                elif interval.size() == 1: return Lng.IF_X("==", interval.begin, i, L)
                else:                      return Lng.IF_X(">=", interval.begin, i, L)

            tm = list(reversed(tm))
        else:
            def get_decision(interval, i, L):
                if   i == L-1:             return Lng.ELSE_SIMPLE
                elif interval.size() == 1: return Lng.IF_X("==", interval.begin, i, L)
                else:                      return Lng.IF_X("<",  interval.end,   i, L)

        if default is not None: tm.append(default)
        return Lng.COMPARISON_SEQUENCE(tm, get_decision)
예제 #9
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
예제 #10
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)
    )
예제 #11
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) 
    ])
예제 #12
0
    def implement(self):
        L = len(self.sub_map)
        assert L != 0

        tm = [
            (interval, "".join(transition.do(interval, target)))
            for interval, target in self.sub_map
        ]

        if len(tm) == 1:
            return Lng.COMPARISON_SEQUENCE(tm, None)

        tm, default = ComparisonSequence.optimize(tm)

        # The buffer limit code is appears extreme seldomly
        # => if it's there, make sure that it is tested at last. 
        #    (This might require to reverse the trigger map.)
        # The 'BLC' might actually no longer occur in the optimized map. Thus, 
        # search for it in the original transition map.
        blc_index = TransitionMap.bisect(self.sub_map, Setup.buffer_limit_code)
        if blc_index is not None and blc_index < L / 2:
            def get_decision(interval, i, L):
                if   i == L-1:             return Lng.ELSE_SIMPLE
                elif interval.size() == 1: return Lng.IF_X("==", interval.begin, i, L)
                else:                      return Lng.IF_X(">=", interval.begin, i, L)

            tm = list(reversed(tm))
        else:
            def get_decision(interval, i, L):
                if   i == L-1:             return Lng.ELSE_SIMPLE
                elif interval.size() == 1: return Lng.IF_X("==", interval.begin, i, L)
                else:                      return Lng.IF_X("<",  interval.end,   i, L)

        if default is not None: tm.append(default)
        return Lng.COMPARISON_SEQUENCE(tm, get_decision)
예제 #13
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)
예제 #14
0
def test(state):
    # (*) compute the trigger map
    tm = TransitionMap.from_TargetMap(state.target_map)
    # (*) print the trigger map entries
    for trigger_interval, target_index in tm:
        if target_index is None or target_index == E_StateIndices.DROP_OUT:
            print trigger_interval.gnuplot_string(-1) + "\n"
        else:   
            print trigger_interval.gnuplot_string(target_index) + "\n"
예제 #15
0
def test(state):
    # (*) compute the trigger map
    tm = TransitionMap.from_TargetMap(state.target_map)
    # (*) print the trigger map entries
    for trigger_interval, target_index in tm:
        if target_index is None or target_index == E_StateIndices.DROP_OUT:
            print trigger_interval.gnuplot_string(-1) + "\n"
        else:   
            print trigger_interval.gnuplot_string(target_index) + "\n"
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)
예제 #17
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 != -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)
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)
예제 #20
0
    def from_State(SM_State, StateIndex, EngineType):
        assert isinstance(SM_State, State)
        assert SM_State.target_map.is_DFA_compliant()
        assert isinstance(StateIndex, (int, long))

        x = AnalyzerState(StateIndex, TransitionMap.from_TargetMap(SM_State.target_map))

        # (*) Transition
        # Currently, the following is only used for path compression. If the alternative
        # is implemented, then the following is no longer necessary.
        x.map_target_index_to_character_set = SM_State.target_map.get_map()

        return x
예제 #21
0
def test(state):
    print "\n## Map: Target Index --> Trigger Set\n"
    # (*) compute the trigger map
    for key, trigger_set in state.target_map.get_map().items():
        print "    %3i <--- %s" % (int(key), repr(trigger_set))

    print "\n## Map: Trigger Intervals (sorted) --> Target Index\n"
    tm = TransitionMap.from_TargetMap(state.target_map)
    # (*) print the trigger map entries
    prev_end = None
    for trigger_interval, target_index in tm:
        print "    %s \t---> %s" % (repr(trigger_interval), repr(target_index))
        if prev_end is not None and trigger_interval.begin != prev_end:
            print "    <ERROR !>"
예제 #22
0
def get_solution(TM):
    """RETURNS: [0] Solution from E_Solution
                [1] Most often appearing target
    """
    interval_n = len(TM)
    assert interval_n > 0

    most_often_appearing_target, target_n = TransitionMap.get_target_statistics(TM)

    # If there's only one interval, there's no need to compare, just go!
    # Otherwise, if there's a very low number of intervals, make a small
    # comparison list that iterates linearly through the items.
    if target_n < 4 and interval_n < 6: 
        return E_Solution.COMPARISON_SEQUENCE, None

    # If the size of character ranges which do not target 'moat' is less
    # than a certain number, implement the transition as branch table. The
    # 'moat' is implemented as the 'default:' case.
    sz_non_moat = TransitionMap.get_size_of_range_other_targets(TM, most_often_appearing_target)
    if sz_non_moat < 256: 
        return E_Solution.BRANCH_TABLE, most_often_appearing_target

    return E_Solution.BISECTIONING, None
예제 #23
0
def test(state):
    print "\n## Map: Target Index --> Trigger Set\n"
    # (*) compute the trigger map
    for key, trigger_set in state.target_map.get_map().items():
        print "    %3i <--- %s" % (int(key), repr(trigger_set))

    print "\n## Map: Trigger Intervals (sorted) --> Target Index\n"
    tm = TransitionMap.from_TargetMap(state.target_map)
    # (*) print the trigger map entries
    prev_end = None
    for trigger_interval, target_index in tm:
        print "    %s \t---> %s" % (repr(trigger_interval), repr(target_index))
        if prev_end is not None and trigger_interval.begin != prev_end:
            print "    <ERROR !>"
예제 #24
0
def test(TM, Target="X"):
    tm = TransitionMap([ (Interval(x[0], x[1]), y) for x, y in TM ])
    print "____________________________________________________________________"
    print "BEFORE:"
    show(tm)
    tm.fill_gaps(Target, Setup.buffer_codec.source_set.minimum(), Setup.buffer_codec.source_set.supremum())
    tm.assert_adjacency(ChangeF=True)
    print "AFTER:"
    show(tm)
예제 #25
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
예제 #26
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
예제 #27
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)
예제 #28
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
예제 #29
0
파일: core.py 프로젝트: dkopecek/amplify
    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
예제 #30
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)
예제 #31
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
예제 #32
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)
        (Interval(200, 230), 7L),
        (Interval(231, 240), 7L),
        (Interval(250, 260), 8L),
        (Interval(71, 80), 8L),
    ])

    interval_end = 300

elif choice == "B":

    def make(start):
        size = int(random.random() * 4) + 1
        target_state_index = long(random.random() * 10)
        return (Interval(start, start + size), target_state_index)

    tm0 = TransitionMap()
    interval_begin = 0
    for i in range(4000):
        tm0.append(make(interval_begin))
        interval_begin = tm0[-1][0].end

    interval_end = interval_begin

elif choice == "C":

    def make(start, size=None):
        if size is None:
            size = int(random.random() * 3) + 1
        target_state_index = long(random.random() * 5)
        return (Interval(start, start + size), target_state_index)
예제 #34
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
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), 
    ])
예제 #36
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
    )
예제 #37
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)
예제 #38
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)
예제 #39
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)