-- CPM:关键路径法(Critical Path Method) jobBase = { schedule = function ( self, job, task, ... ) --由具体job构造任务列表 local finale = false local msg = nil local rc = nil local params = table.pack(self, task, ...) repeat ]; local fn = job[task] if type(fn) == "function" )) end finale = (task == "finale") msg = self.name.."["..task.."] done!" if not finale then -- yield 传回进度描述及任务反馈 -- 打包调度入参 params = table.pack(coroutine.yield(msg, rc)) end until finale self.co = nil -- over return msg, rc end; perform = function ( self, task, ... ) local co = self.co or coroutine.create(self.schedule) if self.co == nil then self.co = co end return coroutine.resume(co, self, task, ...) end; }--jobBase --------------------------------------------------------------- --记得华罗庚的统筹方法论对烧水泡茶的描述过程 --就是多任务协调的执行程序 --示例:project, another两项任务节点的调度与描述 --{}, 构造描述的脚本模式 local project = { name = "projectSample"; perform = jobBase.perform; schedule = function ( self, task, ... ) local todo = { ["stage1"] = function() end; ["stage2"] = function() end; ["stage3"] = function() end; ["finale"] = function()end; } return jobBase.schedule(self, todo, task, ...) end } --记得区分对象调用与域调用,一不小心就写成. 就无法识别self了 print(project:perform("stage1")) --creator, object based; 基于创建对象,对象扩展的脚本模式 function jobBase.create( name ) local o = {name = name} return setmetatable(o, {__index = jobBase}) end local another = jobBase.create("anotherProject") function another:schedule( task, ... ) local todo = { ["stage1"] = function() end; ["stage2"] = function() end; ["finale"] = function()end; } return getmetatable(self).__index.schedule(self, todo, task, ...) end print(another:perform("stage1")) print(project:perform("stage2")) print(project:perform("stage3")) print(another:perform("stage2")) print(project:perform("finale")) print(another:perform("finale"))
本贴代码重点,提示两种脚本应用模式。构造描述的对象模式;基于创建方法,对象扩展的脚本模式。
此例演示coroutine用于多任务协同,统筹方法的一种脚本描述。