KahnProcessNetwork的Python实现

用Pytho实现了一个Kahn Process Network:

思路:

用Python的list模拟queue。

每个channel一个queue

用一个list (fgLog)来记录所有push到fg channel的值用于最后的显示

channel的queue设置为全局变量

代码实现:

h1f=[];fg=[];gh1=[];gh0=[];h0f=[]
fgLog = []

h1FirstRun = True
h0FirstRun = True

fLastChoose = 0
gLastChoose = 0

def h1():
    global h1FirstRun
    if h1FirstRun:
        h1FirstRun = False
        global h1f
        h1f.append(1)
    else:
        global gh1
        if len(gh1) != 0:
            value = gh1.pop(0)
            global h1f
            h1f.append(value)

def h0():
    global h0FirstRun
    if h0FirstRun:
        h0FirstRun = False
        global h0f
        h0f.append(0)
    else:
        global gh0
        if len(gh0) != 0:
            value = gh0.pop(0)
            global h0f
            h0f.append(value)

def f():
    global fLastChoose
    if fLastChoose == 0:
        global h1f
        if len(h1f) != 0:
            value = h1f.pop(0)
            global fg
            global fgLog
            fg.append(value)
            fgLog.append(value)
            fLastChoose = 1
    else:
        global h0f
        if len(h0f) != 0:
            value = h0f.pop(0)
            global fg
            global fgLog
            fg.append(value)
            fgLog.append(value)
            fLastChoose = 0
def g():
    global fg
    if len(fg) != 0:
        global gLastChoose
        value = fg.pop(0)
        if gLastChoose == 0:
            global gh1
            gh1.append(value)
            gLastChoose = 1
        else:
            global gh0
            gh0.append(value)
            gLastChoose = 0

if __name__ == '__main__':
    runOrder = 'order3'
    print runOrder
    if runOrder == 'order1':
        for i in range(50):
            h1();h0();f();g()
    elif runOrder == 'order2':
        for i in range(50):
            h1();h1();h1();g();f();h0();h0();
    elif runOrder == 'order3':
        for i in range(50):
            f();f();g();h1();h0();h1();h0();h0();g()
    print fgLog
            
            
    
        

 

转载于:https://www.cnblogs.com/instant7/p/4110361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值