• 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
                
                
        
            
  • 相关阅读:
    Javascript倒计时
    Windows Live Writer的使用
    liunx下查看服务器硬件信息
    Ant+JSDocTookit生成Javascript文档
    文本文件及二进制文件的大小, Unicode字符编码
    读书笔记之:Linux与Unix shell编程指南v1
    读书笔记之:Linux程序设计(第2版)
    JM8.6中的encode_one_macroblock注释
    在fedora中挂载windows分区
    fedora14的yum源总结
  • 原文地址:https://www.cnblogs.com/instant7/p/4110361.html
Copyright © 2020-2023  润新知