本文整理汇总了Python中transitions.Machine.is_new方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.is_new方法的具体用法?Python Machine.is_new怎么用?Python Machine.is_new使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transitions.Machine
的用法示例。
在下文中一共展示了Machine.is_new方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import is_new [as 别名]
class GraphEngine:
states = ['new', 'resolved', 'unresolved', 'running', 'idle', 'stopping', 'stopped', 'shutdown']
transitions = [
{'trigger': 'resolve', 'source': 'new', 'dest': 'resolved'},
{'trigger': 'resolve', 'source': 'unresolved', 'dest': 'resolved'},
{'trigger': 'unresolve', 'source': 'new', 'dest': 'unresolved'},
{'trigger': 'run', 'source': ['resolved', 'idle'], 'dest': 'running'},
{'trigger': 'idle', 'source': 'running', 'dest': 'idle'},
{'trigger': 'stop', 'source': ['running', 'idle'], 'dest': 'stopping'},
{'trigger': 'stop', 'source': 'stopping', 'dest': 'stopped'},
{'trigger': 'shutdown', 'source': 'stopped', 'dest': 'shutdown'},
]
def __init__(self, graph=None, loop=None):
self.logger = logging.getLogger(__name__)
self.state = Machine(states=GraphEngine.states, transitions=GraphEngine.transitions, initial='new')
if loop:
self._loop = loop
else:
self._loop = asyncio.get_event_loop()
self.processes = dict()
self.connections = dict()
self._graph = None
self._process_manager = None
if graph:
self.bind(graph)
async def bind(self, g):
"""
Bind the engine to a graph description and instantiates processes
:param g:
:return:
"""
if not (self.state.is_new() or self.state.is_shutdown()):
raise EngineException("Engine is already bounded to a graph instance")
self._graph = g
await self._init_graph()
async def init_from_dictionary(self, dict_spec: dict):
# Allow 'graph' as optional root
if 'graph' in dict_spec:
graph_config = dict_spec.get('graph')
else:
graph_config = dict_spec
name = graph_config.get('name', None)
description = graph_config.get('description', None)
author = graph_config.get('author', None)
date = graph_config.get('date', None)
graph = Graph(name, description, author, date)
processes = graph_config.get('processes', dict())
for process in processes:
component_name = processes[process].get('component', None)
if not component_name:
raise GraphException("No component class given for process '%s'", process)
graph.add_process(process, component_name)
connections = graph_config.get('connections')
for cnx in connections:
try:
source_component = cnx['source']['process']
source_port = cnx['source']['port']
target_component = cnx['target']['process']
target_port = cnx['target']['port']
cnx_capacity = cnx.get('capacity', 1) # TODO: don't hard-code, use default
cnx_name = cnx.get('name', None)
except KeyError as ke:
raise GraphException("Invalid parameters for connection '%s' definition" % cnx_name) from ke
graph.add_connection(cnx_name, source_component, source_port, target_component, target_port, cnx_capacity)
await self.bind(graph)
def _get_process(self, process_name, silent=True):
"""
Get a process by its name. Raises a GraphException if no process is found
:param self:
:param process_name:
:return:
"""
return [p for p in self.processes.values() if p.name == process_name]
async def _init_processes(self):
for proc_desc in self._graph.processes_desc:
try:
process = new_component_instance(proc_desc.class_name, proc_desc.process_name)
self.processes[process.id] = process
self.logger.debug("Process '%s' created (Id=%s)" % (process.name, process.id))
except ComponentException as ce:
raise GraphException("Process '%s' instanciation failed" % process.name) from ce
async def _init_connections(self):
for cnx_desc in self._graph.connections_desc:
# find source : source process output port
source_port = None
try:
process_list = self._get_process(cnx_desc.source_process_name)
if len(process_list) > 1:
raise GraphException("Can't create connection '%s': ambiguous process name '%s'" % (cnx_desc.connection_name, cnx_desc.source_process_name))
#.........这里部分代码省略.........