def test_add_ctx_normal(self): experiments_path = os.path.dirname(os.getcwd()) + "/experiments/" A = buildOTA(experiments_path + 'example3.json', 's') AA = buildAssistantOTA(A, 's') sigma = AA.sigma max_time_value = AA.max_time_value() H = buildOTA(experiments_path + 'example3_1.json', 'q') HH = buildAssistantOTA(H, 'q') # AA.show() # print("------------------------------") # HH.show() # print("------------------------------") # H.show() flag, ctx = equivalence_query_normal(max_time_value, AA, HH) # print("-------------ctx-----------------") # print(ctx.tws) ctxs = guess_ctx_reset(ctx.tws, AA) # print(len(ctxs)) # for rtws in ctxs: # print(rtws) # print("-------------local tws-----------------") for ctx in ctxs: local_tws = dRTWs_to_lRTWs(ctx) normalize(local_tws) # #if check_guessed_reset(local_tws, table) == True: # print(ctx) # print(local_tws) # pref = prefixes(local_tws) # for tws in pref: # print(tws) # print("-------------------") T1_tables = init_table_normal(sigma, AA) T1_table_0 = T1_tables[0] test_E = [[Timedword('b', 2), Timedword('a', 4)], [Timedword('a', 5)]] T1_table_0.E = test_E # T1_table_0.show() # print("----------------------------------------") # tables = add_ctx_normal(ctx, T1_table_0, AA) #self.assertEqual(len(tables),65536) # tables[0].show() # tables[1].show() # tables[2].show() # tables[100].show() # tables[4095].show() # for table in tables: # table.show() # print("---------------------") T1_tables = init_table_normal(sigma, AA) T1_table_0 = T1_tables[0] test_E = [[Timedword('b', 2), Timedword('a', 4)]] T1_table_0.E = test_E # T1_table_0.show() # print("----------------------------------------") tables = add_ctx_normal(ctx, T1_table_0, AA) self.assertEqual(len(tables), 128)
def ltw_to_dtw(ltw): dtw = [] for i in range(len(ltw)): if i == 0 or reset[i - 1]: dtw.append(Timedword(ltw[i].action, ltw[i].time)) else: dtw.append( Timedword(ltw[i].action, round1(ltw[i].time - ltw[i - 1].time))) return Element(dtw, [])
def findDelayRTWs(letterword, flag, ota): """Given a path, return delay timedword with reset information. """ path = findpath(letterword, flag, ota.sigma) delay_timedwords = [] delay_resettimedwords = [] current_clock_valuation = 0 delay_time = 0 reset = False for letterword in path: temp_location, temp_region = None, None letter1, letter2 = None, None if len(letterword.lw) == 1: letter1, letter2 = list(letterword.lw[0]) elif len(letterword.lw) == 2: letter1, letter2 = list(letterword.lw[0])[0], list( letterword.lw[1])[0] else: raise NotImplementedError() if letter1.location.flag == flag: temp_location = letter1.location temp_region = letter1.constraint else: temp_location = letter2.location temp_region = letter2.constraint if letterword.action == "DELAY": delay_time = minnum_in_region( temp_region) - current_clock_valuation current_clock_valuation = minnum_in_region(temp_region) source_location = temp_location elif letterword.action in ota.sigma: new_timedword = Timedword(letterword.action, delay_time) delay_timedwords.append(new_timedword) target_location = temp_location local_timedwords = None if reset == True: local_timedwords = Timedword(letterword.action, delay_time) else: local_timedwords = Timedword( letterword.action, current_clock_valuation + delay_time) for otatran in ota.trans: if otatran.source == source_location.name and otatran.target == target_location.name and otatran.is_pass( local_timedwords): #print(source_location.name,target_location.name) reset = otatran.reset delay_resettimedwords.append( ResetTimedword(letterword.action, delay_time, reset)) break current_clock_valuation = minnum_in_region(temp_region) elif letterword.action == '': pass else: raise NotImplementedError() return delay_resettimedwords
def test_guess_resets_in_newsuffix(self): A = buildOTA('example6.json', 's') AA = buildAssistantOTA(A, 's') # Assist #max_time_value = AA.max_time_value() sigma = AA.sigma T1_tables = init_table_normal(sigma, AA) T1_table_0 = T1_tables[0] test_E = [[Timedword('a', 2), Timedword('b', 3), Timedword('a', 1)], [Timedword('b', 2), Timedword('a', 4)]] T1_table_0.E = test_E suffixes_resets = guess_resets_in_newsuffix(T1_table_0, 0, 0, True, True, AA) self.assertEqual(len(suffixes_resets), 1) self.assertEqual(len(suffixes_resets[0]), 4) self.assertEqual( suffixes_resets[0], [[True, None], [True, None], [True, None], [True, None]]) test_E = [[Timedword('a', 2), Timedword('b', 3), Timedword('a', 1)]] T1_table_0.E = test_E suffixes_resets = guess_resets_in_newsuffix(T1_table_0, 0, 0, True, True, AA) self.assertEqual(len(suffixes_resets), 256) self.assertEqual(len(suffixes_resets[34]), 4) self.assertEqual(suffixes_resets[1], [[True, True, None], [True, True, None], [True, True, None], [True, False, None]])
def findGlobalTimedwords(letterword, flag, sigma): """Given a path, return the global timedword. """ path = findpath(letterword, flag, sigma) global_timedwords = [] last_time = 0 temp_time = 0 reset = False for letterword in path: temp_location, temp_region = None, None letter1, letter2 = None, None if len(letterword.lw) == 1: letter1, letter2 = list(letterword.lw[0]) elif len(letterword.lw) == 2: letter1, letter2 = list(letterword.lw[0])[0], list(letterword.lw[1])[0] else: raise NotImplementedError() if letter1.location.flag == flag: temp_location = letter1.location temp_region = letter1.constraint else: temp_location = letter2.location temp_region = letter2.constraint if letterword.action == "DELAY": temp_time = last_time + minnum_in_region(temp_region) elif letterword.action in sigma: new_timedword = Timedword(letterword.action, temp_time) global_timedwords.append(new_timedword) if minnum_in_region(temp_region) == 0: last_time = temp_time elif letterword.action == '': pass else: raise NotImplementedError() return global_timedwords
def new_rtw_in_closed(tws, action, ota): if is_valid_rtws(tws) == False: return ResetTimedword(action,0,True), -1 else: current_statename = ota.run_resettimedwords(tws) current_clock_valuation = 0 reset = True if len(tws) > 0: current_clock_valuation = tws[-1].time reset = tws[-1].reset if current_statename == ota.sink_name: return ResetTimedword(action,0,True), -1 if reset == False and current_clock_valuation > 0: return ResetTimedword(action,0,True), -1 else: flag = False for tran in ota.trans: if tran.source == current_statename and tran.is_pass(Timedword(action,0)): flag = True current_statename = tran.target new_rtw = ResetTimedword(action,0,tran.reset) if current_statename == ota.sink_name: return new_rtw, -1 elif current_statename in ota.accept_names: return new_rtw, 1 else: return new_rtw, 0 if flag == False: raise NotImplementedError("new_rtw_in_closed: an unhandle timedword "+Timedword(action,0).show())
def findDelayTimedwords(letterword, flag, sigma): """Given a path, return the delay timedword. """ path = findpath(letterword, flag, sigma) delay_timedwords = [] current_clock_valuation = 0 delay_time = 0 for letterword in path: temp_location, temp_region = None, None letter1, letter2 = None, None if len(letterword.lw) == 1: letter1, letter2 = list(letterword.lw[0]) elif len(letterword.lw) == 2: letter1, letter2 = list(letterword.lw[0])[0], list(letterword.lw[1])[0] else: raise NotImplementedError() if letter1.location.flag == flag: temp_location = letter1.location temp_region = letter1.constraint else: temp_location = letter2.location temp_region = letter2.constraint if letterword.action == "DELAY": delay_time = minnum_in_region(temp_region) - current_clock_valuation current_clock_valuation = minnum_in_region(temp_region) elif letterword.action in sigma: new_timedword = Timedword(letterword.action, delay_time) delay_timedwords.append(new_timedword) current_clock_valuation = minnum_in_region(temp_region) elif letterword.action == '': pass else: raise NotImplementedError() return delay_timedwords
def init_table(sigma, ota): S = [Element([],[])] R = [] E = [] for s in S: if ota.initstate_name in ota.accept_names: s.value.append(1) else: s.value.append(0) for action in sigma: new_tw = Timedword(action, 0) new_element = None for tran in ota.trans: if tran.source == ota.initstate_name and tran.is_pass(new_tw): new_rtw = ResetTimedword(new_tw.action,new_tw.time,tran.reset) new_value = [] if tran.target in ota.accept_names: new_value = [1] elif tran.target == ota.sink_name: new_value = [-1] else: new_value = [0] new_element = Element([new_rtw], new_value) R.append(new_element) break T = OTATable(S, R, E) return T
def delayTWs_to_globalTWs(delay_timedwords): """Given a delay timedword, return the global timedword. """ global_timedwords = [] temp_time = 0 for timedword in delay_timedwords: temp_action = timedword.action temp_time = temp_time + timedword.time global_timedwords.append(Timedword(temp_action,temp_time)) return global_timedwords
def get_TW_delay_zero(tws, action, ota): """When move a timedwords tws from R to S, generate the new delay timedwords with reset information with delay 0. """ new_timedword = None #local_tws = dRTWs_to_lRTWs(tws) local_tws = tws if tws[len(local_tws)-1].reset == False: new_timedword = Timedword(action,tws[len(local_tws)-1].time) else: new_timedword = Timedword(action,0) source_location_name = ota.run_resettimedwords(local_tws) new_resettimedword = None for otatran in ota.trans: if otatran.source == source_location_name and otatran.is_pass(new_timedword): new_resettimedword = ResetTimedword(action,new_timedword.time,otatran.reset) # return the delay timed words with reset information #new_resettimedword = ResetTimedword(action,0,otatran.reset) break return new_resettimedword
def fix_resets(ctx, ota): #print(ctx) new_tws = [Timedword(rtw.action,rtw.time) for rtw in ctx] #print(new_tws) dRTWs = [] current_location = ota.initstate_name current_clock_valuation = 0 reset = True for tw in new_tws: if reset == False: current_clock_valuation = current_clock_valuation + tw.time else: current_clock_valuation = tw.time for tran in ota.trans: if tran.source == current_location and tran.is_pass(Timedword(tw.action,current_clock_valuation)): dRTWs.append(ResetTimedword(tw.action,tw.time,tran.reset)) current_location = tran.target reset = tran.reset break return dRTWs
def sampleGeneration(inputs, upperGuard, stateNum, length=None): """Generate a sample.""" sample = [] if length is None: length = random.randint(1, stateNum) for i in range(length): input = inputs[random.randint(0, len(inputs) - 1)] time = random.randint(0, upperGuard * 2 + 1) if time % 2 == 0: time = time // 2 else: time = time // 2 + 0.1 temp = Timedword(input, time) sample.append(temp) return Element(sample, [])
def is_evidence_closed(self, ota): """Determine whether the table is evidence-closed. """ flag = True table_tws = [s.tws for s in self.S] + [r.tws for r in self.R] new_added = [] for s in self.S: #local_s = dRTWs_to_lRTWs(s.tws) local_s = s.tws current_location_name = ota.run_resettimedwords(local_s) for e in self.E: temp_e = [] current_location = copy.deepcopy(current_location_name) reset = True clock_valuation = 0 if len(s.tws) > 0: reset = local_s[len(local_s)-1].reset clock_valuation = local_s[len(local_s)-1].time for tw in e: new_timedword = Timedword(tw.action,tw.time) if reset == False and new_timedword.time < clock_valuation: temp_e.append(ResetTimedword(tw.action,tw.time,True)) current_location = ota.sink_name clock_valuation = 0 break else: for otatran in ota.trans: if otatran.source == current_location and otatran.is_pass(new_timedword): new_resettimedword = ResetTimedword(tw.action,tw.time,otatran.reset) temp_e.append(new_resettimedword) clock_valuation = new_timedword.time reset = otatran.reset if reset == True: clock_valuation = 0 current_location = otatran.target break temp_se = [rtw for rtw in s.tws] + [rtw for rtw in temp_e] prefs = prefixes(temp_se) for pref in prefs: if pref not in table_tws: table_tws.append(pref) #table_tws = [tws for tws in table_tws] + [pref] new_tws = [tws for tws in pref] new_element = Element(new_tws,[]) new_added.append(new_element) if len(new_added) > 0: flag = False return flag, new_added
def make_consistent(new_a, new_e_index, table, sigma, ota): #flag, new_a, new_e_index = table.is_consistent() #print flag new_E = [tws for tws in table.E] #new_E = copy.deepcopy(table.E) new_e = [Timedword(tw.action,tw.time) for tw in new_a] if new_e_index > 0: e = table.E[new_e_index-1] new_e.extend(e) new_E.append(new_e) new_S = [s for s in table.S] new_R = [r for r in table.R] for i in range(0, len(new_S)): fill(new_S[i], new_E, ota) for j in range(0, len(new_R)): fill(new_R[j], new_E, ota) consistent_table = OTATable(new_S, new_R, new_E) return consistent_table
def dTWs_to_dRTWs(letterword,flag, ota): tws = findDelayTimedwords(letterword,flag,ota.sigma) dRTWs = [] if len(tws) == 0: return [] else: current_location = ota.initstate_name current_clock_valuation = 0 reset = True for tw in tws: if reset == False: current_clock_valuation = current_clock_valuation + tw.time else: current_clock_valuation = tw.time for tran in ota.trans: if tran.source == current_location and tran.is_pass(Timedword(tw.action,current_clock_valuation)): dRTWs.append(ResetTimedword(tw.action,tw.time,tran.reset)) reset = tran.reset break return dRTWs
def test_guess_resets_in_suffixes(self): A = buildOTA('example6.json', 's') AA = buildAssistantOTA(A, 's') # Assist #max_time_value = AA.max_time_value() sigma = AA.sigma T1_tables = init_table_normal(sigma, AA) T1_table_0 = T1_tables[0] test_E = [[Timedword('a', 2), Timedword('b', 3), Timedword('a', 1)], [Timedword('b', 2), Timedword('a', 4)], [Timedword('a', 5)]] T1_table_0.E = test_E suffixes_resets = guess_resets_in_suffixes(T1_table_0) self.assertEqual(len(suffixes_resets), 8) self.assertEqual(len(suffixes_resets[5]), 3)
def sampleGeneration_valid(teacher, upperGuard, length): """Generate a sample adapted to the given teacher.""" # First produce a path (as a list of transitions) in the OTA path = [] current_state = teacher.initstate_name for i in range(length): edges = [] for tran in teacher.trans: if current_state == tran.source: edges.append(tran) edge = random.choice(edges) path.append(edge) current_state = edge.target # Next, figure out (double of) the minimum and maximum logical time. min_time, max_time = [], [] for tran in path: assert len(tran.constraints) == 1 min_time.append(min_constraint_double(tran.constraints[0])) max_time.append(max_constraint_double(tran.constraints[0], upperGuard)) # For each transition, maintain a mapping from logical time to the # number of choices. weight = dict() for i in reversed(range(length)): tran = path[i] mn, mx = min_time[i], max_time[i] weight[i] = dict() if i == length - 1 or tran.reset: for j in range(mn, mx + 1): weight[i][j] = 1 else: for j in range(mn, mx + 1): weight[i][j] = 0 for k, w in weight[i + 1].items(): if k >= j: weight[i][j] += w # Now sample according to the weights double_times = [] cur_time = 0 for i in range(length): start_time = max(min_time[i], cur_time) distr = [] for j in range(start_time, max_time[i] + 1): distr.append(weight[i][j]) if sum(distr) == 0: return None # sampling failed cur_time = sampleDistribution(distr) + start_time double_times.append(cur_time) if path[i].reset: cur_time = 0 # Finally, change doubled time to fractions. ltw = [] for i in range(length): if double_times[i] % 2 == 0: time = double_times[i] // 2 else: time = double_times[i] // 2 + 0.1 ltw.append(Timedword(path[i].label, time)) # Convert logical-timed word to delayed-timed word. dtw = [] for i in range(length): if i == 0 or path[i - 1].reset: dtw.append(Timedword(path[i].label, ltw[i].time)) else: dtw.append(Timedword(path[i].label, ltw[i].time - ltw[i - 1].time)) return Element(dtw, [])
def minimizeCounterexample(teacher, hypothesis, sample): """Minimize a given delay-timed word.""" reset = [] current_state = hypothesis.initstate_name current_clock = 0 ltw = [] # Fix computation with 0.1 def round1(x): return int(x * 10 + 0.5) / 10 def one_lower(x): if round1(x - int(x)) == 0.1: return int(x) else: return round1(x - 0.9) # Find sequence of reset information for tw in sample.tws: current_clock = round1(current_clock + tw.time) ltw.append(Timedword(tw.action, current_clock)) for tran in hypothesis.trans: found = False if current_state == tran.source and tran.is_pass( Timedword(tw.action, current_clock)): reset.append(tran.reset) current_state = tran.target if tran.reset: current_clock = 0 found = True break assert found # print('ltw:', ltw) def ltw_to_dtw(ltw): dtw = [] for i in range(len(ltw)): if i == 0 or reset[i - 1]: dtw.append(Timedword(ltw[i].action, ltw[i].time)) else: dtw.append( Timedword(ltw[i].action, round1(ltw[i].time - ltw[i - 1].time))) return Element(dtw, []) # print('initial:', ltw_to_dtw(ltw).tws) for i in range(len(ltw)): while True: if i == 0 or reset[i - 1]: can_reduce = (ltw[i].time > 0) else: can_reduce = (ltw[i].time > ltw[i - 1].time) if not can_reduce: break ltw2 = copy.deepcopy(ltw) ltw2[i] = Timedword(ltw[i].action, one_lower(ltw[i].time)) # print('try', ltw_to_dtw(ltw2).tws) if not isCounterexample( teacher, hypothesis, ltw_to_dtw(ltw2), is_mem=True): break # print('change') ltw = ltw2 # print('final:', ltw_to_dtw(ltw).tws) return ltw_to_dtw(ltw)