Esempio n. 1
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)
    #flag = False
    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:
            cuintervals = []
            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
Esempio n. 2
0
def state_to_letter(state, max_time_value):
    region = None
    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 fraction, region
    return Letter(state.location, region)
Esempio n. 3
0
def next_region(region, max_time_value):
    """Returns r_0^1 for r_0, r_1 for r_0^1, etc.
    """
    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 + ']')
Esempio n. 4
0
def buildAssistantRTA(rta):
    """
        build an assistant RTA which has the partitions at every node.
        The acceptance language is equal to teacher.
    """
    location_number = len(rta.states)
    tran_number = len(rta.trans)
    new_state = State(str(location_number + 1), False, False)
    flag = False
    new_trans = []
    for s in rta.states:
        s_dict = {}
        for key in rta.sigma:
            s_dict[key] = []
        for tran in rta.trans:
            if tran.source == s.name:
                for label in rta.sigma:
                    if tran.label == label:
                        for constraint in tran.constraints:
                            s_dict[label].append(constraint)
        for key in s_dict:
            cuintervals = []
            if len(s_dict[key]) > 0:
                cuintervals = complement_intervals(s_dict[key])
            else:
                cuintervals = [Constraint("[0,+)")]
            if len(cuintervals) > 0:
                temp_tran = RTATran(tran_number, s.name, new_state.name, key,
                                    cuintervals)
                tran_number = tran_number + 1
                new_trans.append(temp_tran)
    assist_name = "Assist_" + rta.name
    assist_states = [state for state in rta.states]
    assist_trans = [tran for tran in rta.trans]
    assist_init = rta.initstate_name
    assist_accepts = [sn for sn in rta.accept_names]
    if len(new_trans) > 0:
        assist_states.append(new_state)
        for tran in new_trans:
            assist_trans.append(tran)
        for label in rta.sigma:
            constraints = [Constraint("[0,+)")]
            temp_tran = RTATran(tran_number, new_state.name, new_state.name,
                                label, constraints)
            tran_number = tran_number + 1
            assist_trans.append(temp_tran)
    return RTA(assist_name, rta.sigma, assist_states, assist_trans,
               assist_init, assist_accepts)
Esempio n. 5
0
def compute_wsucc(letterword, max_time_value, A, B):
    """Compute the Succ of letterword.
    """
    # 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
Esempio n. 6
0
def buildRTA(jsonfile):
    """
        build the teacher RTA from a json file.
    """
    data = json.load(open(jsonfile, 'r'))
    name = data["name"].encode("utf-8")
    states_list = [s.encode("utf-8") for s in data["s"]]
    sigma = [s.encode("utf-8") for s in data["sigma"]]
    trans_set = data["tran"]
    initstate = data["init"].encode("utf-8")
    accept_list = [s.encode("utf-8") for s in data["accept"]]
    S = [State(state) for state in states_list]
    for s in S:
        if s.name == initstate:
            s.init = True
        if s.name in accept_list:
            s.accept = True
    trans = []
    for tran in trans_set:
        tran_id = int(tran.encode("utf-8"))
        source = trans_set[tran][0].encode("utf-8")
        label = trans_set[tran][1].encode("utf-8")
        intervals_str = trans_set[tran][2].encode("utf-8")
        intervals_list = intervals_str.split('U')
        constraints_list = []
        for constraint in intervals_list:
            new_constraint = Constraint(constraint.strip())
            constraints_list.append(new_constraint)
        target = trans_set[tran][3].encode("utf-8")
        rta_tran = RTATran(tran_id, source, target, label, constraints_list)
        trans += [rta_tran]
    return RTA(name, sigma, S, trans, initstate, accept_list), sigma
Esempio n. 7
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
Esempio n. 8
0
def immediate_letter_asucc(letter, action, ota):
    """
    """
    location_name = letter.location.name
    region = letter.constraint
    succ_location = None
    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 == True:
                region = Constraint("[0,0]")
            if succ_location is not None:
                return Letter(succ_location, region)
    return None
Esempio n. 9
0
def buildOTA(jsonfile, otaflag):
    """
        build the teacher OTA from a json file.
    """
    #otaflag = 's'
    with open(jsonfile, 'r') as f:
        data = json.load(f)
        name = data["name"]
        locations_list = [l for l in data["l"]]
        sigma = [s for s in data["sigma"]]
        trans_set = data["tran"]
        initstate = data["init"]
        accept_list = [l for l in data["accept"]]
        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)
Esempio n. 10
0
 def __init__(self, location, constraint):
     self.location = location
     if isinstance(constraint, str):
         constraint = Constraint(constraint)
     self.constraint = constraint
Esempio n. 11
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)
    #sigma = [action for action in sigma]
    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)
                    temp_constraint = None
                    ## By now, we assuem that the timepoints are interger numbers.
                    # if index + 1 < len(timepoints):
                    #     temp_constraint = Constraint("[" + str(rtw.time) + "," + str(timepoints[index+1]) + ")")
                    # else:
                    #     temp_constraint = Constraint("[" + str(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