Python状态机:重置循环?

时间:2016-05-03 22:24:54

标签: python fsm

我需要我的FSM读取二进制文件,与状态和​​转换进行比较。然后,说它是被接受还是被拒绝。接受状态是一个简单的010.现在,无论我做什么,循环从一开始就重新开始。我尝试了几种缩进变体。

@IBOutlet var countDown: UILabel!
var currentSeconds = 59
var currentMins = 5
var timer = NSTimer()

 @IBAction func start(sender: UIButton) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.updateTime), userInfo: nil, repeats: true)

}

 func updateTime() {

    if (currentSeconds > 9)   {
        countDown.text = "0\(currentMins):\(currentSeconds)"
        currentSeconds -= 1
    } else if ( currentSeconds > 0) && (currentSeconds <= 9) {
        countDown.text = "0\(currentMins):0\(currentSeconds)"
        currentSeconds -= 1
    } else {
        currentMins -= 1
        currentSeconds = 59
    }

    if (currentSeconds == 0) && (currentMins == 0) {
        countDown.text = "time is up!"
        timer.invalidate()
    }



}

 @IBAction func stop(sender: AnyObject) {
  timer.invalidate()
 }

我的几个文本文件是:

import sys
import os


try:
    Tfile = open("transistions2.txt","r")
except IOError:
    print "Could not open file", "transitions.txt"
    sys.exit()
Transitions = []


ReadLine = Tfile.readline()
while ReadLine != "":
    ReadLine = ReadLine.rstrip()
    CS, IN, NS = ReadLine.split(",")
    Transitions.append((CS, IN, NS))
    ReadLine = Tfile.readline()

print "Transitions:\n", Transitions, "\n"
Tfile.close()


try:
    Sfile = open("states.txt","r")
except IOError:
    print "Could not open file", "states.txt"
    sys.exit()
States = []

ReadLine = Sfile.readline()
while ReadLine != "":
    SN, SS, AS = ReadLine.split(",")
    States.append((SN, bool(int(SS)), bool(int(AS))))
    ReadLine = Sfile.readline()

print "States:\n", States, "\n"
Sfile.close()

try:
    Strfile = open("strings2.txt","r")
except IOError:
    print "Could not open file", strings2.txt
    sys.exit()
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
    Readline = ReadLine.rstrip()
    Strings.append(Readline)
    ReadLine = Strfile.readline()

print "Strings:\n", '\n'.join(Strings), '\n'
Strfile.close()

CurrentState = ''
Start ='State1' 


for S in Strings:
    if S != '':
            print "String:", S
            for C in S:
                print "Number:", C
                CurrentState =Start
                for  Transitions in (CS, IN, NS):
                    print Transitions
                    if CS == CurrentState and IN == C:
                           CurrentState = NS
                           print NS
                           break
                for  States in (SN,SS,AS):
                    if SN == CurrentState:
                        print S, "is accepted"
                    elif  AS == CurrentState:
                        print S, " is rejected"
                    else:
                        print S,"Doesnt work"

1 个答案:

答案 0 :(得分:1)

通过尝试使用states.txt文件,您的FSM实施比必要的更复杂 - 这里有没有它的样子:

import sys

try:
    Tfile = open("transitions2.txt", "r")
except IOError:
    print "Could not open file transitions2.txt"
    sys.exit()
Transitions = []

ReadLine = Tfile.readline()
while ReadLine != "":
    ReadLine = ReadLine.rstrip()
    CS, IN, NS = ReadLine.split(",")
    Transitions.append((CS, IN, NS))
    ReadLine = Tfile.readline()

print "Transitions:\n", Transitions, "\n"
Tfile.close()


try:
    Strfile = open("strings2.txt", "r")
except IOError:
    print "Could not open file strings2.txt"
    sys.exit()
Strings = []

ReadLine = Strfile.readline()
while ReadLine != "":
    Readline = ReadLine.rstrip()
    Strings.append(Readline)
    ReadLine = Strfile.readline()

print "Strings:\n", '\n'.join(Strings), '\n'
Strfile.close()


Start = 'State1'
Accept = 'State4'

for S in Strings:
    CurrentState = Start
    print "String:", S
    for C in S:
        print " Number:", C
        # find matching state and and input number
        for CS, IN, NS in Transitions:
            if CS == CurrentState and IN == C:
                CurrentState = NS  # make transition to next state
                print " NS ->", NS
                break

    if CurrentState == Accept:
        print " "+S, "Is accepted"
    else:
        print " "+S, "Doesn't work"
    print

输出:

Transitions:
[('State3', '1', 'State3'), ('State3', '0', 'State4'), ('State2', '1', 'State3'), 
 ('State2', '0', 'State2'), ('State1', '1', 'State1'), ('State1', '0', 'State2')]

Strings:
01010
1001
010

String: 01010
 Number: 0
 NS -> State2
 Number: 1
 NS -> State3
 Number: 0
 NS -> State4
 Number: 1
 Number: 0
 01010 Is accepted

String: 1001
 Number: 1
 NS -> State1
 Number: 0
 NS -> State2
 Number: 0
 NS -> State2
 Number: 1
 NS -> State3
 1001 Doesn't work

String: 010
 Number: 0
 NS -> State2
 Number: 1
 NS -> State3
 Number: 0
 NS -> State4
 010 Is accepted

由于Accept状态没有转换,因此只要达到break循环就可以for C in S:,并获得完全相同的结果。

相关问题