def test_parkingmeter_fsm(self): parking_meter = MooreMachine('Parking Meter') ready = State('Ready', initial=True) verify = State('Verify') await_action = State(r'Await\naction') print_tkt = State('Print ticket') return_money = State(r'Return\nmoney') reject = State('Reject coin') ready[r'coin inserted'] = verify verify.update({'valid': State(r'add value\rto ticket'), 'invalid': reject}) for coin_value in verify: verify[coin_value][''] = await_action await_action.update({'print': print_tkt, 'coin': verify, 'abort': return_money, 'timeout': return_money}) return_money[''] = print_tkt[''] = ready get_graph(parking_meter).draw('parking.png', prog='dot')
def test_tcp_fsm(self): STATES = ['LISTEN', 'SYN RCVD', 'ESTABLISHED', 'SYN SENT', 'FIN WAIT 1', 'FIN WAIT 2', 'TIME WAIT', 'CLOSING', 'CLOSE WAIT', 'LAST ACK'] tcpip = FiniteStateMachine('TCP IP') closed = State('CLOSED', initial=True) listen, synrcvd, established, synsent, finwait1, finwait2, timewait, \ closing, closewait, lastack = [State(s) for s in STATES] timewait['(wait)'] = closed closed.update({r'passive\nopen': listen, 'send SYN': synsent}) synsent.update({r'close /\ntimeout': closed, r'recv SYN,\nsend\nSYN+ACK': synrcvd, r'recv SYN+ACK,\nsend ACK': established}) listen.update({r'recv SYN,\nsend\nSYN+ACK': synrcvd, 'send SYN': synsent}) synrcvd.update({'recv ACK': established, 'send FIN': finwait1, 'recv RST': listen}) established.update({'send FIN': finwait1, r'recv FIN,\nsend ACK': closewait}) closewait['send FIN'] = lastack lastack['recv ACK'] = closed finwait1.update({'send ACK': closing, 'recv ACK': finwait2, r'recv FIN, ACK\n send ACK': timewait}) finwait2[r'recv FIN,\nsend ACK'] = timewait closing[r'recv\nACK'] = timewait graph = get_graph(tcpip) graph.draw('tcp.png', prog='dot')
def drawStateTransitionGraph(): #Here we appy the state transitions to create a finite state machine ktail = FiniteStateMachine('K-TAIL') for nx,kvx in stateMap1.items(): for c in kvx: State(nx).update({kvx[c]:State(c)}) print 'State Transition: ' +str(nx) + '-->'+str(c) + '[label='+kvx[c] +']' #Define initial state if nx==0: nx=State(0, initial=True) #Create a state machine print '------------------------------------------------------------------------------------' #Check if there is existing graph data try: graph=get_graph(ktail) if graph!=None: graph.draw('../graph/ktail.png', prog='dot') print graph else: pass except GraphvizError: tkMessageBox.ERROR
def generateStateTransition(self,loadEquivalentState): #print k_tail_value,trace #kt=kTails('k-tails') ktailx=loadEquivalentState#kt.do_kTailEquivCheck(k_tail_value,trace) print 'transition ' + str(ktailx) if len(ktailx)==0: pass #for mv in ktailx: # if mv==match_Values(ktailx): # print mv #Identify unique states from the merged state list getUniqueStates = set() for val in ktailx: getUniqueStates.add(val) print getUniqueStates #Dictionary to keed track of the state and its assocated transition labels transDict={} for g in getUniqueStates: for tmpk,tmpv in ktail.tmpDict.items(): if g==tmpk: transDict[g]=tmpv print 'transDict' + str(transDict) #Create a list of mapping combinations identified from the state merged list #Example: [0,1,1,3,4] ==>[0-->1,1-->1,1-->3,3-->4] current = None nxt = None index=0 mapping=[] while (index+1)<len(ktailx): current=ktailx[index] nxt=ktailx[index+1] mapping.append(str(current) +'-->'+ str(nxt)) index+=1 print 'mapping' + str(mapping) #This dictionary stores transition of each state #A state may have multiple transitions. #Example: State 0: may have a or more transitions to itself and another transition to other state stateMap={} print transDict for td,tv in transDict.items(): #for tm in mapping: # stm=[int(s) for s in tm.split('-->') if s.isdigit()] # if str(td)==stm[0]: #stateMap[td]={ tv:' '} #Intialize the embedded dictionary with empty string for each state stateMap[td]={} #The embedded dictionary stores the next transition state with the #transition label as the key. print 'stateMap'+ str(stateMap) for z in getUniqueStates: for e,f in transDict.items(): if z==e: for m in mapping: st=[int(s) for s in m.split('-->') if s.isdigit()] #extract digits a mapping entry if str(z)==str(st[0]) and str(z)==str(st[1]): #if str(z)==m[-1] and str(z)==m[0]:#Check for occurance of transition to itself #if m[0] not in stateMap[z]: stateMap[z][int(st[0])]=f #print 'x'+stateMap[z][m[-1]] + m #Check if the transition from the current node #to the next node is the same as the self-transition on current node #If so then we assign and arbitrary label-as it might cause non-deterministic fsm elif str(z)!=str(st[1]) and str(z)==str(st[0]): #print stateMap[z][int(st[1])] stateMap[z][int(st[1])]=f #elif str(z)==str(st[1]) and str(z)!=str(st[0]): # stateMap[z][int(st[0])]=f print 'statemap'+str(stateMap) #Here we appy the state transitions to create a finite state machine for nx,kvx in stateMap.items(): #n=[State(s) for s in list(getUniqueStates)] for c in kvx: State(nx).update({kvx[c]:State(c)}) print 'State: ' +str(nx) + kvx[c] +':'+str(c) #Define initial state if nx==0: nx=State(0, initial=True) #Create a state machine graph = get_graph(ktail) graph.draw('ktail.png', prog='dot') print 'End of Print line'
} }) fsm.cmd_advanced() elif callback_query['data'] == 'USD': bot.sendMessage(callback_query['from']['id'], 'You have choosed USD as fiat') fsm.cmd_fiat(useTWD=False) elif callback_query['data'] == 'TWD': bot.sendMessage(callback_query['from']['id'], 'You have choosed TWD as fiat') fsm.cmd_fiat(useTWD=True) else: for name, short, url in cryptos: crypto = callback_query['data'] if crypto == url: fsm.cmd_crypto(crypto) fsm.send(callback_query['from']['id'], short) bot.answerCallbackQuery(callback_query['id']) return "OK" if __name__ == '__main__': initial() fsm.get_graph().draw('state_diagram.png', prog='dot') Timer(5.0, update).start() app.run(host=server_host, port=server_port, debug=False, ssl_context=('../ssl/public.pem', '../ssl/key.pem'))
closed.update({r'passive\nopen': listen, 'send SYN': synsent}) synsent.update({r'close /\ntimeout': closed, r'recv SYN,\nsend\nSYN+ACK': synrcvd, r'recv SYN+ACK,\nsend ACK': established}) listen.update({r'recv SYN,\nsend\nSYN+ACK': synrcvd, 'send SYN': synsent}) synrcvd.update({'recv ACK': established, 'send FIN': finwait1, 'recv RST': listen}) established.update({'send FIN': finwait1, r'recv FIN,\nsend ACK': closewait}) closewait['send FIN'] = lastack lastack['recv ACK'] = closed finwait1.update({'send ACK': closing, 'recv ACK': finwait2, r'recv FIN, ACK\n send ACK': timewait}) finwait2[r'recv FIN,\nsend ACK'] = timewait closing[r'recv\nACK'] = timewait graph = get_graph(tcpip) graph.draw('tcp.png', prog='dot')
def generateStateTransition(self, loadEquivalentState): #print k_tail_value,trace #kt=kTails('k-tails') ktailx = loadEquivalentState #kt.do_kTailEquivCheck(k_tail_value,trace) print 'transition ' + str(ktailx) if len(ktailx) == 0: pass #for mv in ktailx: # if mv==match_Values(ktailx): # print mv #Identify unique states from the merged state list getUniqueStates = set() for val in ktailx: getUniqueStates.add(val) print getUniqueStates #Dictionary to keed track of the state and its assocated transition labels transDict = {} for g in getUniqueStates: for tmpk, tmpv in ktail.tmpDict.items(): if g == tmpk: transDict[g] = tmpv print 'transDict' + str(transDict) #Create a list of mapping combinations identified from the state merged list #Example: [0,1,1,3,4] ==>[0-->1,1-->1,1-->3,3-->4] current = None nxt = None index = 0 mapping = [] while (index + 1) < len(ktailx): current = ktailx[index] nxt = ktailx[index + 1] mapping.append(str(current) + '-->' + str(nxt)) index += 1 print 'mapping' + str(mapping) #This dictionary stores transition of each state #A state may have multiple transitions. #Example: State 0: may have a or more transitions to itself and another transition to other state stateMap = {} print transDict for td, tv in transDict.items(): #for tm in mapping: # stm=[int(s) for s in tm.split('-->') if s.isdigit()] # if str(td)==stm[0]: #stateMap[td]={ tv:' '} #Intialize the embedded dictionary with empty string for each state stateMap[td] = { } #The embedded dictionary stores the next transition state with the #transition label as the key. print 'stateMap' + str(stateMap) for z in getUniqueStates: for e, f in transDict.items(): if z == e: for m in mapping: st = [int(s) for s in m.split('-->') if s.isdigit()] #extract digits a mapping entry if str(z) == str(st[0]) and str(z) == str(st[1]): #if str(z)==m[-1] and str(z)==m[0]:#Check for occurance of transition to itself #if m[0] not in stateMap[z]: stateMap[z][int(st[0])] = f #print 'x'+stateMap[z][m[-1]] + m #Check if the transition from the current node #to the next node is the same as the self-transition on current node #If so then we assign and arbitrary label-as it might cause non-deterministic fsm elif str(z) != str(st[1]) and str(z) == str(st[0]): #print stateMap[z][int(st[1])] stateMap[z][int(st[1])] = f #elif str(z)==str(st[1]) and str(z)!=str(st[0]): # stateMap[z][int(st[0])]=f print 'statemap' + str(stateMap) #Here we appy the state transitions to create a finite state machine for nx, kvx in stateMap.items(): #n=[State(s) for s in list(getUniqueStates)] for c in kvx: State(nx).update({kvx[c]: State(c)}) print 'State: ' + str(nx) + kvx[c] + ':' + str(c) #Define initial state if nx == 0: nx = State(0, initial=True) #Create a state machine graph = get_graph(ktail) graph.draw('ktail.png', prog='dot') print 'End of Print line'
@author: dlmu__000 """ from fsm import State, Transducer, get_graph microwave = Transducer('Microwave Oven') closed_i = State(r'CLOSED\nidle', initial=True) closed_p = State(r'CLOSED\nprocessing') opened_i = State(r'OPEN\nidle') paused = State('PAUSED') settings = State(r'SETUP') closed_i[r'PSB, TK /\nset program,\nset timer'] = closed_i closed_i['SSB / start'] = closed_p closed_i['ODH / open door'] = opened_i closed_i['ELS / enter setup'] = settings settings['SSB / save setup'] = settings settings['ELS / leave setup'] = closed_i opened_i['DL / shut door'] = closed_i closed_p['PRB / pause'] = paused closed_p['ODH / open door'] = opened_i closed_p['TO / ready'] = closed_i paused['SSB / stop'] = closed_i paused['PRB / resume'] = closed_p paused[r'PSB, TK /\nreset program,\nreset timer'] = paused paused['ODH / open door'] = opened_i get_graph(microwave).draw('microwave.png', prog='dot')
def generateStateTransition(self, loadEquiState, tmpDictx, dotFile): ktailx = loadEquiState if len(ktailx) == 0: pass #Identify unique states from the merged state list kTailFSMGraph.getUniqueStates = set() for val in ktailx: kTailFSMGraph.getUniqueStates.add(val) print 'unique states' + str(kTailFSMGraph.getUniqueStates) #Dictionary to keep track of the state and its assocated transition labels kTailFSMGraph.transDict = {} for g in kTailFSMGraph.getUniqueStates: for tmpk, tmpv in tmpDictx.items(): if g == tmpk: kTailFSMGraph.transDict[g] = tmpv print 'transDict' + str(kTailFSMGraph.transDict) #Create a list of mapping combinations identified from the state merged list #Example: [0,1,1,3,4] ==>[0-->1,1-->1,1-->3,3-->4] current = None nxt = None index = 0 kTailFSMGraph.mapping = [] while (index + 1) < len(ktailx): current = ktailx[index] nxt = ktailx[index + 1] kTailFSMGraph.mapping.append(str(current) + '-->' + str(nxt)) index += 1 print 'mapping' + str(kTailFSMGraph.mapping) #This dictionary stores transition of each state #A state may have multiple transitions. #Example: State 0: may have a or more transitions to itself and another transition to other state kTailFSMGraph.stateMap = {} kTailFSMGraph.sampleStatemap = {} print kTailFSMGraph.transDict for td, tv in kTailFSMGraph.transDict.items(): #Intialize the embedded dictionary with empty string for each state if dotFile == 'sample': kTailFSMGraph.sampleStatemap[td] = {} elif dotFile == 'ktail': kTailFSMGraph.stateMap[td] = { } #The embedded dictionary stores the next transition state with the transition label as the key. for z in kTailFSMGraph.getUniqueStates: for e, f in kTailFSMGraph.transDict.items(): if z == e: for m in kTailFSMGraph.mapping: st = [int(s) for s in m.split('-->') if s.isdigit() ] #extract digits in a mapping entry if str(z) == str(st[0]) and str(z) == str(st[1]): if dotFile == 'sample': kTailFSMGraph.sampleStatemap[z][int(st[0])] = f else: kTailFSMGraph.stateMap[z][int(st[0])] = f #Check if the transition from the current node #to the next node is the same as the self-transition on current node #If so then we assign and arbitrary label-as it might cause non-deterministic fsm elif str(z) != str(st[1]) and str(z) == str(st[0]): if dotFile == 'sample': kTailFSMGraph.sampleStatemap[z][int(st[1])] = f else: kTailFSMGraph.stateMap[z][int(st[1])] = f print 'Test Input Trace Map' + str(kTailFSMGraph.stateMap) print 'Sample Input Trace Map' + str(kTailFSMGraph.sampleStatemap) #Look for non-determistic paths in the traces for the sample input. if dotFile == 'sample': print 'Checking for non-deterministic paths in Sample Trace Automata:' kTailFSMGraph.samplendfaloginfor.append( 'Checking for non-deterministic paths in Sample Trace Automata:' ) for k, v in kTailFSMGraph.transDict.items(): if self.duplicate_dictionary_check( kTailFSMGraph.sampleStatemap, str(v)) == None: pass else: kTailFSMGraph.samplendfaloginfor.append('==>' + str( self.duplicate_dictionary_check( kTailFSMGraph.sampleStatemap, str(v)))) print self.duplicate_dictionary_check( kTailFSMGraph.sampleStatemap, str(v)) tkMessageBox.showinfo( "Non-deterministic FA", self.duplicate_dictionary_check( kTailFSMGraph.sampleStatemap, str(v))) elif dotFile == 'ktail': kTailFSMGraph.ndfaloginfor = [] #re print 'Checking for non-deterministic paths in Input Trace Automata:' kTailFSMGraph.ndfaloginfor.append( 'Checking for non-deterministic paths in Input Trace Automata:' ) #print 'alpa'+str(alphabet) for k, v in kTailFSMGraph.transDict.items(): #print self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v)) if self.duplicate_dictionary_check(kTailFSMGraph.stateMap, str(v)) == None: pass else: kTailFSMGraph.ndfaloginfor.append('==>' + str( self.duplicate_dictionary_check( kTailFSMGraph.stateMap, str(v)))) print self.duplicate_dictionary_check( kTailFSMGraph.stateMap, str(v)) #tkMessageBox.showinfo("Non-deterministic FA", self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v))) #Here we appy the state transitions to create a finite state machine ktailFSM = FiniteStateMachine('K-TAIL') kTailFSMGraph.alphabetfromtrace.clear() tmpstateMap = {} #make a shallow copy of the state map dictionaries if dotFile == 'sample': tmpstateMap = kTailFSMGraph.sampleStatemap.copy() elif dotFile == 'ktail': tmpstateMap = kTailFSMGraph.stateMap.copy() for nx, kvx in tmpstateMap.items(): #n=[State(s) for s in list(getUniqueStates)] for c in kvx: State(nx).update({kvx[c]: State(c)}) print 'State Transition: ' + str(nx) + '-->' + str( c) + '[label=' + kvx[c] + ']' kTailFSMGraph.alphabetfromtrace[(nx, kvx[c])] = c #Define initial state if nx == 0: nx = State(0, initial=True) #Define acceptings states for the state machine if dotFile == 'sample': for a in kTailFSMGraph.accepting_states: if a == nx: nx = State(a, accepting=True) #clear the the tmp states map after the operation tmpstateMap.clear() #Create a state machine print '------------------------------------------------------------------------------------' try: graph = get_graph(ktailFSM) if graph != None: #Check if there is existing graph data graph.draw('../graph/' + dotFile + '.png', prog='dot') else: pass except GraphvizError: tkMessageBox.ERROR print '-------------------------------------------------------------------------------------'
def generateStateTransition(self,loadEquiState,tmpDictx,dotFile): ktailx=loadEquiState if len(ktailx)==0: pass #Identify unique states from the merged state list kTailFSMGraph.getUniqueStates = set() for val in ktailx: kTailFSMGraph.getUniqueStates.add(val) print 'unique states'+ str(kTailFSMGraph.getUniqueStates) #Dictionary to keep track of the state and its assocated transition labels kTailFSMGraph.transDict={} for g in kTailFSMGraph.getUniqueStates: for tmpk,tmpv in tmpDictx.items(): if g==tmpk: kTailFSMGraph.transDict[g]=tmpv print 'transDict' + str(kTailFSMGraph.transDict) #Create a list of mapping combinations identified from the state merged list #Example: [0,1,1,3,4] ==>[0-->1,1-->1,1-->3,3-->4] current = None nxt = None index=0 kTailFSMGraph.mapping=[] while (index+1)<len(ktailx): current=ktailx[index] nxt=ktailx[index+1] kTailFSMGraph.mapping.append(str(current) +'-->'+ str(nxt)) index+=1 print 'mapping' + str(kTailFSMGraph.mapping) #This dictionary stores transition of each state #A state may have multiple transitions. #Example: State 0: may have a or more transitions to itself and another transition to other state kTailFSMGraph.stateMap={} kTailFSMGraph.sampleStatemap={} print kTailFSMGraph.transDict for td,tv in kTailFSMGraph.transDict.items(): #Intialize the embedded dictionary with empty string for each state if dotFile=='sample': kTailFSMGraph.sampleStatemap[td]={} elif dotFile=='ktail': kTailFSMGraph.stateMap[td]={} #The embedded dictionary stores the next transition state with the transition label as the key. for z in kTailFSMGraph.getUniqueStates: for e,f in kTailFSMGraph.transDict.items(): if z==e: for m in kTailFSMGraph.mapping: st=[int(s) for s in m.split('-->') if s.isdigit()] #extract digits in a mapping entry if str(z)==str(st[0]) and str(z)==str(st[1]): if dotFile=='sample': kTailFSMGraph.sampleStatemap[z][int(st[0])]=f else: kTailFSMGraph.stateMap[z][int(st[0])]=f #Check if the transition from the current node #to the next node is the same as the self-transition on current node #If so then we assign and arbitrary label-as it might cause non-deterministic fsm elif str(z)!=str(st[1]) and str(z)==str(st[0]): if dotFile=='sample': kTailFSMGraph.sampleStatemap[z][int(st[1])]=f else: kTailFSMGraph.stateMap[z][int(st[1])]=f print 'Test Input Trace Map'+str(kTailFSMGraph.stateMap) print 'Sample Input Trace Map'+str(kTailFSMGraph.sampleStatemap) #Look for non-determistic paths in the traces for the sample input. if dotFile=='sample': print 'Checking for non-deterministic paths in Sample Trace Automata:' kTailFSMGraph.samplendfaloginfor.append('Checking for non-deterministic paths in Sample Trace Automata:') for k,v in kTailFSMGraph.transDict.items(): if self.duplicate_dictionary_check(kTailFSMGraph.sampleStatemap,str(v))==None: pass else: kTailFSMGraph.samplendfaloginfor.append('==>'+str(self.duplicate_dictionary_check(kTailFSMGraph.sampleStatemap,str(v)))) print self.duplicate_dictionary_check(kTailFSMGraph.sampleStatemap,str(v)) tkMessageBox.showinfo("Non-deterministic FA", self.duplicate_dictionary_check(kTailFSMGraph.sampleStatemap,str(v))) elif dotFile=='ktail': kTailFSMGraph.ndfaloginfor=[] #re print 'Checking for non-deterministic paths in Input Trace Automata:' kTailFSMGraph.ndfaloginfor.append('Checking for non-deterministic paths in Input Trace Automata:') #print 'alpa'+str(alphabet) for k,v in kTailFSMGraph.transDict.items(): #print self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v)) if self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v))==None: pass else: kTailFSMGraph.ndfaloginfor.append('==>'+str(self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v)))) print self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v)) #tkMessageBox.showinfo("Non-deterministic FA", self.duplicate_dictionary_check(kTailFSMGraph.stateMap,str(v))) #Here we appy the state transitions to create a finite state machine ktailFSM = FiniteStateMachine('K-TAIL') kTailFSMGraph.alphabetfromtrace.clear() tmpstateMap={} #make a shallow copy of the state map dictionaries if dotFile=='sample': tmpstateMap=kTailFSMGraph.sampleStatemap.copy() elif dotFile=='ktail': tmpstateMap=kTailFSMGraph.stateMap.copy() for nx,kvx in tmpstateMap.items(): #n=[State(s) for s in list(getUniqueStates)] for c in kvx: State(nx).update({kvx[c]:State(c)}) print 'State Transition: ' +str(nx) + '-->'+str(c) + '[label='+kvx[c] +']' kTailFSMGraph.alphabetfromtrace[(nx,kvx[c])]=c #Define initial state if nx==0: nx=State(0, initial=True) #Define acceptings states for the state machine if dotFile=='sample': for a in kTailFSMGraph.accepting_states: if a==nx: nx=State(a,accepting=True) #clear the the tmp states map after the operation tmpstateMap.clear() #Create a state machine print '------------------------------------------------------------------------------------' try: graph=get_graph(ktailFSM) if graph!=None:#Check if there is existing graph data graph.draw('../graph/'+dotFile+'.png', prog='dot') else: pass except GraphvizError: tkMessageBox.ERROR print '-------------------------------------------------------------------------------------'