1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
   | import argparse
  config_path = './output/config' output_path = './output/proc0'   
  def read_config():     f = open(config_path,"r")     data = f.readlines()     m = 0     dict = {}     count = 0     for line in data:         splited = line.split(" ")         if count == 0:             m = int(splited[0])          else: 	        sender = int(splited[0]) 	        for i in range(len(splited)): 	            dict.setdefault(sender,[]).append(int(splited[i]))          count+=1     return dict
 
  def getDependentList(id, dset):     cur_output_path = output_path+str(id)+'.output'     f = open(cur_output_path,"r")     data = f.readlines()          sequence = []     for line in data:         splited = line.split(" ")         if splited[0] == "d":              if int(splited[1]) in dset:                  sequence.append(line)     print(str(id),'#msg:',len(sequence))     return sequence
 
  def getDependentListOfCreator(id, dset):     cur_output_path = output_path+str(id)+'.output'     f = open(cur_output_path,"r")     data = f.readlines()          sequence = []     for line in data:         splited = line.split(" ")         if splited[0] == "b":             sequence.append(line)         else:             if int(splited[1]) in dset:                 sequence.append(line)     print(str(id),'#msg:',len(sequence))     return sequence     
  def getVectorClock(sequence, id, proc_num):     clock = []     msg_clock_list = []     for i in range(proc_num+1):         clock.append(0)     for msg in sequence:         splited = msg.split(" ")         sender = int(splited[1])         clock[sender]+=1         if sender==id:             msg_clock_list.append(clock.copy())     return msg_clock_list
 
  def getVectorClockOfCreator(sequence, id, proc_num):     clock = []     msg_clock_list = []     for i in range(proc_num+1):         clock.append(0)     for msg in sequence:         splited = msg.split(" ")         sender = int(splited[1])         if splited[0]=='d':             clock[sender]+=1         else:             msg_clock_list.append(clock.copy())     return msg_clock_list     
  def checkProcessId(id, dset):     print('checking process', str(id), ',depend on', dset)               ref_sequence = getDependentListOfCreator(id, dset)     ref_clock = getVectorClockOfCreator(ref_sequence, id, proc_num)     print(id,'#clock:',len(ref_clock))          for i in range(proc_num):         cur_id = i+1         if cur_id != id:                          sequence = getDependentList(cur_id, dset)                          clock = getVectorClock(sequence, id, proc_num)             print(cur_id,'#clock:',len(clock))                                       if len(clock)>len(ref_clock):                 print('Number exceeds!')                 return False                          for i in range(len(clock)):                 for j in range(proc_num+1):                     if ref_clock[i][j] > clock[i][j]:                         print('Clock not match!')                         print('ref_clock:',ref_clock[i])                         print('clock    :',clock[i])                         return False     return True
 
  def checkProcess(proc_num):     depend = read_config()     print('dependency:',depend,'\n')          for i in range(proc_num):         id = i+1         if checkProcessId(id, depend[id])==False:             return False         print('validate process',str(id),'OK\n')     return True
  if __name__ == "__main__":     parser = argparse.ArgumentParser()
      parser.add_argument(         "--proc_num",         required=True,         dest="proc_num",         help="Total number of processes",     )
      results = parser.parse_args()          proc_num = int(results.proc_num)     if checkProcess(proc_num):         print("Validation OK")     else:         print("Validation failed!")
   |