Example #1
0
def state_to_letter(state, max_time_value):
    integer = int(state.v)
    fraction = state.get_fraction()
    if fraction > 0.0:
        if integer < max_time_value:
            region = Constraint('(' + str(integer) + ',' + str(integer + 1) +
                                ')')
        else:
            region = Constraint('(' + str(integer) + ',' + '+' + ')')
    else:
        region = Constraint('[' + str(integer) + ',' + str(integer) + ']')
    return Letter(state.location, region)
Example #2
0
def next_region(region, max_time_value):
    if region.isPoint():
        if int(region.max_value) == max_time_value:
            return Constraint('(' + region.max_value + ',' + '+' + ')')
        else:
            return Constraint('(' + region.max_value + ',' +
                              str(int(region.max_value) + 1) + ')')
    else:
        if region.max_value == '+':
            return Constraint('(' + region.min_value + ',' + '+' + ')')
        else:
            return Constraint('[' + region.max_value + ',' + region.max_value +
                              ']')
Example #3
0
def buildAssistantOTA(ota, otaflag):
    """
        build an assistant OTA which has the partitions at every node. The acceptance language is equal to teacher.
    """
    location_number = len(ota.locations)
    tran_number = len(ota.trans)
    new_location = Location(str(location_number + 1), False, False, otaflag,
                            True)
    new_trans = []
    for l in ota.locations:
        l_dict = {}
        for key in ota.sigma:
            l_dict[key] = []
        for tran in ota.trans:
            if tran.source == l.name:
                for label in ota.sigma:
                    if tran.label == label:
                        for constraint in tran.constraints:
                            l_dict[label].append(constraint)
        for key in l_dict:
            if len(l_dict[key]) > 0:
                cuintervals = complement_intervals(l_dict[key])
            else:
                cuintervals = [Constraint("[0,+)")]
            if len(cuintervals) > 0:
                for c in cuintervals:
                    reset = True
                    temp_tran = OTATran(tran_number, l.name, key, [c], reset,
                                        new_location.name, otaflag)
                    tran_number = tran_number + 1
                    new_trans.append(temp_tran)
    assist_name = "Assist_" + ota.name
    assist_locations = [location for location in ota.locations]
    assist_trans = [tran for tran in ota.trans]
    assist_init = ota.initstate_name
    assist_accepts = [sn for sn in ota.accept_names]
    if len(new_trans) > 0:
        assist_locations.append(new_location)
        for tran in new_trans:
            assist_trans.append(tran)
        for label in ota.sigma:
            constraints = [Constraint("[0,+)")]
            reset = True
            temp_tran = OTATran(tran_number, new_location.name, label,
                                constraints, reset, new_location.name, otaflag)
            tran_number = tran_number + 1
            assist_trans.append(temp_tran)
    assist_ota = OTA(assist_name, ota.sigma, assist_locations, assist_trans,
                     assist_init, assist_accepts)
    assist_ota.sink_name = new_location.name
    return assist_ota
Example #4
0
def compute_wsucc(letterword, max_time_value, A, B):
    # First compute all possible time delay
    results = []
    last_region = Constraint('(' + str(max_time_value) + ',' + '+' + ')')
    if len(letterword.lw) == 1:
        result = letterword.lw[0]
        while any(letter.constraint != last_region for letter in result):
            results.append(Letterword([result], letterword))
            new_result = set()
            for letter in result:
                new_letter = Letter(
                    letter.location,
                    next_region(letter.constraint, max_time_value))
                new_result.add(new_letter)
            result = new_result
        current_lw = Letterword([result], letterword)
        if current_lw not in results:
            results.append(current_lw)
    elif len(letterword.lw) == 2:
        if len(letterword.lw[0]) != 1 and len(letterword.lw[1]) != 1:
            raise NotImplementedError()
        result = letterword.lw
        while list(result[0])[0].constraint != last_region or list(
                result[1])[0].constraint != last_region:
            results.append(Letterword(result, letterword))
            new_result = []
            l1, l2 = list(result[0])[0], list(result[1])[0]
            if l1.constraint.isPoint():
                new_result.append({
                    Letter(l1.location,
                           next_region(l1.constraint, max_time_value))
                })
                new_result.append({l2})
            else:
                new_result.append({
                    Letter(l2.location,
                           next_region(l2.constraint, max_time_value))
                })
                new_result.append({l1})
            result = new_result
        current_lw = Letterword(result, letterword)
        if current_lw not in results:
            results.append(current_lw)
            new_result = Letterword([current_lw.lw[1], current_lw.lw[0]],
                                    letterword)
            if new_result not in results:
                results.append(new_result)
    else:
        raise NotImplementedError()

    # Next, perform the immediate 'a' transition
    next = []
    for letterword in results:
        next_ws = immediate_asucc(letterword, A, B)
        for next_w in next_ws:
            if next_w not in next:
                next.append(next_w)
    return results, next
Example #5
0
def get_regions(max_time_value):
    """
        Partition R into a finite collection of one-dimensional regions depending on the appearing max time value.
    """
    regions = []
    bound = 2 * max_time_value + 1
    for i in range(0, bound + 1):
        if i % 2 == 0:
            temp = i // 2
            r = Constraint('[' + str(temp) + ',' + str(temp) + ']')
            regions.append(r)
        else:
            temp = (i - 1) // 2
            if temp < max_time_value:
                r = Constraint('(' + str(temp) + ',' + str(temp + 1) + ')')
                regions.append(r)
            else:
                r = Constraint('(' + str(temp) + ',' + '+' + ')')
                regions.append(r)
    return regions
Example #6
0
def immediate_letter_asucc(letter, action, ota):
    location_name = letter.location.name
    region = letter.constraint
    for tran in ota.trans:
        if tran.source == location_name and action == tran.label and region.issubset(
                tran.constraints[0]):
            succ_location_name = tran.target
            succ_location = ota.findlocationbyname(succ_location_name)
            if tran.reset:
                region = Constraint("[0,0]")
            if succ_location is not None:
                return Letter(succ_location, region)
    return None
Example #7
0
def buildOTA(model, otaflag):
    """
        build the teacher OTA from a json file.
    """
    name = 'TA'
    locations_list = [l for l in model["states"]]
    sigma = [s for s in model["inputs"]]
    trans_set = model["trans"]
    initstate = model["initState"]
    accept_list = [l for l in model["acceptStates"]]
    L = [
        Location(location, False, False, otaflag)
        for location in locations_list
    ]
    for l in L:
        if l.name == initstate:
            l.init = True
        if l.name in accept_list:
            l.accept = True
    trans = []
    for tran in trans_set:
        tran_id = int(tran)
        source = trans_set[tran][0]
        label = trans_set[tran][1]
        intervals_str = trans_set[tran][2]
        intervals_list = intervals_str.split('U')
        constraints_list = []
        for constraint in intervals_list:
            new_constraint = Constraint(constraint.strip())
            constraints_list.append(new_constraint)
        reset_temp = trans_set[tran][3]
        reset = False
        if reset_temp == "r":
            reset = True
        target = trans_set[tran][4]
        ota_tran = OTATran(tran_id, source, label, constraints_list, reset,
                           target, otaflag)
        trans += [ota_tran]
    trans.sort(key=lambda x: x.id)
    return OTA(name, sigma, L, trans, initstate, accept_list)
Example #8
0
def fa_to_ota(fa, sink_name, sigma, n):
    """
        Transform the finite automaton to an one-clock timed automaton as a hypothesis.
    """
    new_name = "H_" + str(n)
    states = [Location(state.name, state.init, state.accept, 'q') for state in fa.states]
    initstate_name = fa.initstate_name
    accept_names = [name for name in fa.accept_names]
    for l in states:
        if l.name == sink_name:
            l.sink = True
    ### generate the transitions
    trans = []
    for s in fa.states:
        s_dict = {}
        for key in sigma:
            s_dict[key] = []
        for tran in fa.trans:
            if tran.source == s.name:
                s_dict[tran.label[0].action].extend([rtw.time for rtw in tran.label])
        for tran in fa.trans:
            if tran.source == s.name:
                timepoints = [time for time in s_dict[tran.label[0].action]]
                timepoints.sort()
                for rtw in tran.label:
                    index = timepoints.index(rtw.time)
                    ## Consider the float timepoints
                    if index + 1 < len(timepoints):
                        if isinstance(rtw.time, int) and isinstance(timepoints[index + 1], int):
                            temp_constraint = Constraint("[" + str(rtw.time) + "," + str(timepoints[index + 1]) + ")")
                        elif isinstance(rtw.time, int) and not isinstance(timepoints[index + 1], int):
                            temp_constraint = Constraint("[" + str(rtw.time) + "," + str(int(timepoints[index + 1])) + "]")
                        elif not isinstance(rtw.time, int) and isinstance(timepoints[index + 1], int):
                            temp_constraint = Constraint("(" + str(int(rtw.time)) + "," + str(timepoints[index + 1]) + ")")
                        else:
                            temp_constraint = Constraint("(" + str(int(rtw.time)) + "," + str(int(timepoints[index + 1])) + "]")
                    else:
                        if isinstance(rtw.time, int):
                            temp_constraint = Constraint("[" + str(rtw.time) + "," + "+" + ")")
                        else:
                            temp_constraint = Constraint("(" + str(int(rtw.time)) + "," + "+" + ")")
                    temp_tran = OTATran(len(trans), tran.source, tran.label[0].action, [temp_constraint], rtw.reset, tran.target, 'q')
                    trans.append(temp_tran)
    ota = OTA(new_name, sigma, states, trans, initstate_name, accept_names)
    ota.sink_name = sink_name
    return ota
Example #9
0
 def __init__(self, location, constraint):
     self.location = location
     if isinstance(constraint, str):
         constraint = Constraint(constraint)
     self.constraint = constraint