def persist(self, session, reason, finished_name, next_name): """ 提供必要的持久化实现 .. Note:: session中的控制消息应在处理完成之后被清理,否则会造成重复触发 :param object session: 状态机的session :param str reason: 持久化的原因 :param str finished_name: 已经完成的节点名 :param str next_name: 下一个将处理的节点名 :return: 无返回 :rtype: None """ if reason == graph.PersistedStateMachineHelper.Reason.CONTROL: message_name = "PERSIST_SESSION_MESSAGE" elif reason == graph.PersistedStateMachineHelper.Reason.STARTED: message_name = "STATE_COMPLETE_MESSAGE" elif reason == graph.PersistedStateMachineHelper.Reason.NODE_CHANGED: message_name = "STATE_COMPLETE_MESSAGE" if session is not None: params = { "session": session, "finished_node": finished_name, "current_node": next_name, "timestamp": int(time.time()) } notice = framework.OperationMessage( message_name, str(session.id), params) self._result_queue.put(notice) else: log.e("operation persist but session is None")
def message_handler(self, message): """ 消息处理逻辑,该函数调用具体的消息执行函数,并获取结果放入结果队列中 .. Note:: 由于操作异步执行,因此各子进程执行结果统一放入多进程安全的结果队列, 由主进程统一进程结果的后续处理 :param Message message: 消息对象 :return: 无返回 :rtype: None """ try: log.Logger.setoid(message.operation_id) log.i("process new message") log.d("process new message,param:%s" % str(message.params)) ret = self.execute_message(message) except Exception as e: log.f("execute_message fail") ret = "err:{}".format(e) message = framework.OperationMessage( "COMPLETE_MESSAGE", message.operation_id, ret) self._result_queue.put(message) log.Logger.clearoid() return
def decision_logic(self, message): """ 状态机决策逻辑,该函数直接根据Message生成决策完成的消息对象 :param Message message: 感知完成的消息对象 :return: 决策完成的消息对象 :rtype: Message """ decided_message = framework.OperationMessage( "DECIDED_MESSAGE", message.operation_id, {}) return decided_message
def decision_logic(self, message): """ 具体决策逻辑 :param Message message: 消息对象 :return: 决策完成的消息 :rtype: Message :raises ETypeMismatch: 事件参数不匹配异常 """ log.i("begin decision logic, message:{}".format(message.name)) operation_id = message.operation_id params = message.params if self._from_key in params \ and params[self._from_key] in self._mapping: params_cp = copy.deepcopy(params) params_cp[self._to_key] = self._mapping[params_cp[self._from_key]] decided_message = framework.OperationMessage( "DECIDED_MESSAGE", operation_id, params_cp) return decided_message else: raise exception.ETypeMismatch( "{} not in params or params[{}] not in " "mapping".format(self._from_key, self._from_key))