def next_region(region, upper_guard): if region.is_point(): if float(region.max_value) == float(upper_guard): return Guard('(' + region.max_value + ',' + '+' + ')') else: return Guard('(' + region.max_value + ',' + str(int(region.max_value) + 1) + ')') else: if region.max_value == '+': return Guard('(' + region.min_value + ',' + '+' + ')') else: return Guard('[' + region.max_value + ',' + region.max_value + ']')
def complement_intervals(guards): partitions = [] key = [] floor_bn = BracketNum('0', Bracket.LC) ceil_bn = BracketNum('+', Bracket.RO) for guard in guards: min_bn = guard.min_bn max_bn = guard.max_bn if min_bn not in key: key.append(min_bn) if max_bn not in key: key.append(max_bn) copyKey = copy.deepcopy(key) for bn in copyKey: complement = bn.complement() if complement not in copyKey: copyKey.append(complement) if floor_bn not in copyKey: copyKey.insert(0, floor_bn) if ceil_bn not in copyKey: copyKey.append(ceil_bn) copyKey.sort() for index in range(len(copyKey)): if index % 2 == 0: tempGuard = Guard(copyKey[index].getBN() + ',' + copyKey[index + 1].getBN()) partitions.append(tempGuard) for g in guards: if g in partitions: partitions.remove(g) return partitions
def build_canonicalOTA(system): actions = system.actions states = system.states trans = system.trans init_state = system.init_state accept_states = system.accept_states sinkFlag = False newTrans = [] sink_state = 'sink' tranNumber = len(system.trans) for state in system.states: guardDict = {} for action in actions: guardDict[action] = [] for tran in trans: if tran.source == state: for action in actions: if tran.action == action: for guard in tran.guards: guardDict[action].append(guard) for key, value in guardDict.items(): if len(value) > 0: addGuards = complement_intervals(value) else: addGuards = [Guard('[0,+)')] if len(addGuards) > 0: sink_state = 'sink' sinkFlag = True for guard in addGuards: tempTran = OTATran(tranNumber, state, key, [guard], True, sink_state) tranNumber = tranNumber + 1 newTrans.append(tempTran) if sinkFlag: states.append(sink_state) for tran in newTrans: trans.append(tran) for action in actions: guards = [Guard('[0,+)')] tempTran = OTATran(tranNumber, sink_state, action, guards, True, sink_state) tranNumber = tranNumber + 1 trans.append(tempTran) newOTA = OTA(actions, states, trans, init_state, accept_states, sink_state) return newOTA
def compute_wsucc(letterword, upper_guard, A, B): # First compute all possible time delay results = [] last_region = Guard('(' + str(upper_guard) + ',' + '+' + ')') if len(letterword.lw) == 1: result = letterword.lw[0] while any(letter.guard != last_region for letter in result): results.append(LetterWord([result], letterword, 'DELAY')) new_result = set() for letter in result: new_letter = Letter(letter.state, next_region(letter.guard, upper_guard), letter.flag) new_result.add(new_letter) result = new_result current_lw = LetterWord([result], letterword, 'DELAY') 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].guard != last_region or list( result[1])[0].guard != last_region: results.append(LetterWord(result, letterword, 'DELAY')) new_result = [] l1, l2 = list(result[0])[0], list(result[1])[0] if l1.guard.is_point(): new_result.append({ Letter(l1.state, next_region(l1.guard, upper_guard), l1.flag) }) new_result.append({l2}) else: new_result.append({ Letter(l2.state, next_region(l2.guard, upper_guard), l2.flag) }) new_result.append({l1}) result = new_result current_lw = LetterWord(result, letterword, 'DELAY') if current_lw not in results: results.append(current_lw) new_result = LetterWord([current_lw.lw[1], current_lw.lw[0]], letterword, 'DELAY') if new_result not in results: results.append(new_result) else: raise NotImplementedError() # Next, perform the immediate 'a' transition next_list = [] for letterword in results: next_ws = immediate_asucc(letterword, A, B) for next_w in next_ws: if next_w not in next_list: next_list.append(next_w) return results, next_list
def immediate_letter_asucc(letter, action, ota, flag): location_name = letter.state region = letter.guard for tran in ota.trans: if tran.source == location_name and action == tran.action and region.is_subset( tran.guards[0]): succ_location = tran.target if tran.reset: region = Guard("[0,0]") if succ_location is not None: return Letter(succ_location, region, flag) return None
def struct_hypothesisOTA(discreteOTA): trans = [] for s in discreteOTA.states: s_dict = {} for key in discreteOTA.actions: s_dict[key] = [0] for tran in discreteOTA.trans: if tran.source == s: for action in discreteOTA.actions: if tran.action == action: temp_list = s_dict[action] if tran.time_point not in temp_list: temp_list.append(tran.time_point) s_dict[action] = temp_list for value in s_dict.values(): value.sort() for tran in discreteOTA.trans: if tran.source == s: time_points = s_dict[tran.action] guards = [] tw = tran.time_point index = time_points.index(tw) if index + 1 < len(time_points): if is_int(tw) and is_int(time_points[index + 1]): tempGuard = Guard("[" + str(tw) + "," + str(time_points[index + 1]) + ")") elif is_int(tw) and not is_int(time_points[index + 1]): tempGuard = Guard( "[" + str(tw) + "," + str(math.modf(time_points[index + 1])[1]) + "]") elif not is_int(tw) and is_int(time_points[index + 1]): tempGuard = Guard("(" + str(math.modf(tw)[1]) + "," + str(time_points[index + 1]) + ")") else: tempGuard = Guard( "(" + str(math.modf(tw)[1]) + "," + str(math.modf(time_points[index + 1])[1]) + "]") guards.append(tempGuard) else: if is_int(tw): tempGuard = Guard("[" + str(tw) + ",+)") else: tempGuard = Guard("(" + str(math.modf(tw)[1]) + ",+)") guards.append(tempGuard) for guard in guards: trans.append( OTATran(tran.tran_id, tran.source, tran.action, [guard], tran.reset, tran.target)) return OTA(discreteOTA.actions, discreteOTA.states, trans, discreteOTA.init_state, discreteOTA.accept_states, discreteOTA.sink_state)
def build_system(model): actions = model["inputs"] states = model["states"] init_state = model["initState"] accept_states = model["acceptStates"] tran_list = model["trans"] trans = [] for tran in tran_list: tran_id = str(tran) source = tran_list[tran][0] target = tran_list[tran][4] action = tran_list[tran][1] reset = tran_list[tran][3] == "r" # time guard intervals = tran_list[tran][2] intervals_list = intervals.split('U') guards = [] for guard in intervals_list: new_guard = Guard(guard.strip()) guards.append(new_guard) trans.append(SysTran(tran_id, source, action, guards, reset, target)) return System(actions, states, trans, init_state, accept_states)
def __init__(self, state, guard, flag): self.state = state if isinstance(guard, str): guard = Guard(guard) self.guard = guard self.flag = flag # "A" or "B"