def impasse(self): if self.impassed: return true logger.debug(f"@impasse {self.id}") self.impassed = true self.post(Attempt(Achieve(null, _impasse, null))) self.schedule(self) return False
def dispatch(self, msg): T = type(msg) if T is Propose: logger.debug(f"* \t{msg}") pmsg = Attempt() pmsg.update(msg) self.proposals.append(pmsg) return elif T is Assert: logger.debug(f"+ \t{msg}") self.ctx.add(msg.data) return self.fire(msg) elif T is Retract: logger.debug(f"- \t${msg}") self.ctx.remove(msg.data) return self.fire(msg) else: logger.debug(f"Eval:\t{msg}") return self.fire(msg)
def main(self): t = None status = None post = None logger.debug(f"@main {self.id}") logger.debug('eval tasks') if len(self.queue) != 0: t = self.queue.pop(0) while t: logger.debug(f"Tick:\t({t.constructor.name}) {t.msg}") status = t.action() if status == TS_RUNNING: self.queue.append(t) elif t.parent: pStatus = t.parent.strategy(t) if pStatus == TS_RUNNING: self.queue.append(t.parent) elif t.caller: t.caller.resume() self.queue.append(t.caller) t = self.queue.pop(0) logger.debug('eval posts') if len(self.posts) != 0: post = self.posts.pop(0) while post: self.dispatch(post) post = self.posts.pop(0) if self.idle() and self.impasse() and not self.scheduled: for msg in self.proposals: for m in msg.sender.matchRules(msg): self.fork(m.to) self.proposals = [] self.resolve() self.status = TS_SUCCESS return self.status
def fire(self, msg): for m in msg.sender.matchRules(msg): logger.debug(f"Fire:\t{m}:") self.schedule(m.to)
def fork(self, t): logger.debug(f"Fork:\t{t.msg}") child = Runner() child.policy = self.policy child.ctx = self.ctx.copy() return child.run(t)
def post(self, msg): if not msg.sender: msg.sender = self logger.debug(f"Post:\t{msg}") return self.posts.append(msg)
def broadcast(self, msg): m = copy(msg) logger.debug(f"Broadcast:\t{m}") m.sender = self self.post(m) return map(self.tasks, lambda t: t.broadcast(m))
async def fire(self, msg): for m in self.policy.match(msg): print("fire", m) logger.debug(f"Fire:\t{m}:") await m.rule.action(self, m)