7.2.4.1. BlueDispatcher¶
-
class
BlueDispatcher
(dmap=None, name='', default_values=None, raises=False, description='', executor=None)[source]¶ Blueprint object is a blueprint of how to construct or extend a Dispatcher.
————————————————————————
Example:
Create a BlueDispatcher:
>>> import schedula as sh >>> blue = sh.BlueDispatcher(name='Dispatcher')
Add data/function/dispatcher nodes to the dispatcher map as usual:
>>> blue.add_data(data_id='a', default_value=3) <schedula.utils.blue.BlueDispatcher object at ...> >>> @sh.add_function(blue, True, True, outputs=['c']) ... def diff_function(a, b=2): ... return b - a ... >>> blue.add_function(function=max, inputs=['c', 'd'], outputs=['e']) <schedula.utils.blue.BlueDispatcher object at ...> >>> from math import log >>> sub_blue = sh.BlueDispatcher(name='Sub-Dispatcher') >>> sub_blue.add_data(data_id='a', default_value=2).add_function( ... function=log, inputs=['a'], outputs=['b'] ... ) <schedula.utils.blue.BlueDispatcher object at ...> >>> blue.add_dispatcher(sub_blue, ('a',), {'b': 'f'}) <schedula.utils.blue.BlueDispatcher object at ...>
You can set the default values as usual:
>>> blue.set_default_value(data_id='c', value=1, initial_dist=6) <schedula.utils.blue.BlueDispatcher object at ...>
You can also create a Blueprint out of SubDispatchFunction and add it to the Dispatcher as follow:
>>> func = sh.SubDispatchFunction(sub_blue, 'func', ['a'], ['b']) >>> blue.add_from_lists(fun_list=[ ... dict(function=func, inputs=['a'], outputs=['d']), ... dict(function=func, inputs=['c'], outputs=['g']), ... ]) <schedula.utils.blue.BlueDispatcher object at ...>
Finally you can create the dispatcher object using the method new:
>>> dsp = blue.register(memo={}); dsp <schedula.dispatcher.Dispatcher object at ...>
Or dispatch, calling the Blueprint object:
>>> sol = blue({'a': 1}); sol Solution([('a', 1), ('b', 2), ('c', 1), ('d', 0.0), ('f', 0.0), ('e', 1), ('g', 0.0)])
Methods
__init__
Initialize self. add_data
Add a single data node to the dispatcher. add_dispatcher
Add a single sub-dispatcher node to dispatcher. add_from_lists
Add multiple function and data nodes to dispatcher. add_func
Add a single function node to dispatcher. add_function
Add a single function node to dispatcher. extend
Extends deferred operations calling each operation of given Blueprints. register
Creates a Blueprint.cls
and calls each deferred operation.set_default_value
Set the default value of a data node in the dispatcher. -
__init__
(dmap=None, name='', default_values=None, raises=False, description='', executor=None)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
add_data
(data_id=None, default_value=empty, initial_dist=0.0, wait_inputs=False, wildcard=None, function=None, callback=None, description=None, filters=None, await_result=None, **kwargs)[source]¶ Add a single data node to the dispatcher.
Parameters: - data_id (str, optional) – Data node id. If None will be assigned automatically (‘unknown<%d>’) not in dmap.
- default_value (T, optional) – Data node default value. This will be used as input if it is not specified as inputs in the ArciDispatch algorithm.
- initial_dist (float, int, optional) – Initial distance in the ArciDispatch algorithm when the data node default value is used.
- wait_inputs (bool, optional) – If True ArciDispatch algorithm stops on the node until it gets all input estimations.
- wildcard (bool, optional) – If True, when the data node is used as input and target in the ArciDispatch algorithm, the input value will be used as input for the connected functions, but not as output.
- function (callable, optional) – Data node estimation function. This can be any function that takes only one dictionary (key=function node id, value=estimation of data node) as input and return one value that is the estimation of the data node.
- callback (callable, optional) – Callback function to be called after node estimation. This can be any function that takes only one argument that is the data node estimation output. It does not return anything.
- description (str, optional) – Data node’s description.
- filters (list[function], optional) – A list of functions that are invoked after the invocation of the main function.
- await_result (bool|int|float, optional) – If True the Dispatcher waits data results before assigning them to the solution. If a number is defined this is used as timeout for Future.result method [default: False]. Note this is used when asynchronous or parallel execution is enable.
- kwargs (keyword arguments, optional) – Set additional node attributes using key=value.
Returns: Self.
Return type:
-
add_function
(function_id=None, function=None, inputs=None, outputs=None, input_domain=None, weight=None, inp_weight=None, out_weight=None, description=None, filters=None, await_domain=None, await_result=None, **kwargs)[source]¶ Add a single function node to dispatcher.
Parameters: - function_id (str, optional) – Function node id. If None will be assigned as <fun.__name__>.
- function (callable, optional) – Data node estimation function.
- inputs (list, optional) – Ordered arguments (i.e., data node ids) needed by the function.
- outputs (list, optional) – Ordered results (i.e., data node ids) returned by the function.
- input_domain (callable, optional) – A function that checks if input values satisfy the function domain. This can be any function that takes the same inputs of the function and returns True if input values satisfy the domain, otherwise False. In this case the dispatch algorithm doesn’t pass on the node.
- weight (float, int, optional) – Node weight. It is a weight coefficient that is used by the dispatch algorithm to estimate the minimum workflow.
- inp_weight (dict[str, float | int], optional) – Edge weights from data nodes to the function node. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
- out_weight (dict[str, float | int], optional) – Edge weights from the function node to data nodes. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
- description (str, optional) – Function node’s description.
- filters (list[function], optional) – A list of functions that are invoked after the invocation of the main function.
- await_domain (bool|int|float, optional) – If True the Dispatcher waits all input results before executing the input_domain function. If a number is defined this is used as timeout for Future.result method [default: True]. Note this is used when asynchronous or parallel execution is enable.
- await_result (bool|int|float, optional) – If True the Dispatcher waits output results before assigning them to the workflow. If a number is defined this is used as timeout for Future.result method [default: False]. Note this is used when asynchronous or parallel execution is enable.
- kwargs (keyword arguments, optional) – Set additional node attributes using key=value.
-
add_func
(function, outputs=None, weight=None, inputs_kwargs=False, inputs_defaults=False, filters=None, input_domain=None, await_domain=None, await_result=None, inp_weight=None, out_weight=None, description=None, inputs=None, function_id=None, **kwargs)[source]¶ Add a single function node to dispatcher.
Parameters: - inputs_kwargs (bool) – Do you want to include kwargs as inputs?
- inputs_defaults (bool) – Do you want to set default values?
- function_id (str, optional) – Function node id. If None will be assigned as <fun.__name__>.
- function (callable, optional) – Data node estimation function.
- inputs (list, optional) – Ordered arguments (i.e., data node ids) needed by the function. If None it will take parameters names from function signature.
- outputs (list, optional) – Ordered results (i.e., data node ids) returned by the function.
- input_domain (callable, optional) – A function that checks if input values satisfy the function domain. This can be any function that takes the same inputs of the function and returns True if input values satisfy the domain, otherwise False. In this case the dispatch algorithm doesn’t pass on the node.
- weight (float, int, optional) – Node weight. It is a weight coefficient that is used by the dispatch algorithm to estimate the minimum workflow.
- inp_weight (dict[str, float | int], optional) – Edge weights from data nodes to the function node. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
- out_weight (dict[str, float | int], optional) – Edge weights from the function node to data nodes. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
- description (str, optional) – Function node’s description.
- filters (list[function], optional) – A list of functions that are invoked after the invocation of the main function.
- await_domain (bool|int|float, optional) – If True the Dispatcher waits all input results before executing the input_domain function. If a number is defined this is used as timeout for Future.result method [default: True]. Note this is used when asynchronous or parallel execution is enable.
- await_result (bool|int|float, optional) – If True the Dispatcher waits output results before assigning them to the workflow. If a number is defined this is used as timeout for Future.result method [default: False]. Note this is used when asynchronous or parallel execution is enable.
- kwargs (keyword arguments, optional) – Set additional node attributes using key=value.
Returns: Self.
Return type:
-
add_dispatcher
(dsp, inputs, outputs, dsp_id=None, input_domain=None, weight=None, inp_weight=None, description=None, include_defaults=False, await_domain=None, **kwargs)[source]¶ Add a single sub-dispatcher node to dispatcher.
Parameters: - dsp (Dispatcher | dict[str, list]) – Child dispatcher that is added as sub-dispatcher node to the parent dispatcher.
- inputs (dict[str, str | list[str]] | tuple[str] | (str, .., dict[str, str | list[str]])) – Inputs mapping. Data node ids from parent dispatcher to child sub-dispatcher.
- outputs (dict[str, str | list[str]] | tuple[str] | (str, .., dict[str, str | list[str]])) – Outputs mapping. Data node ids from child sub-dispatcher to parent dispatcher.
- dsp_id (str, optional) – Sub-dispatcher node id. If None will be assigned as <dsp.name>.
- input_domain ((dict) -> bool, optional) –
A function that checks if input values satisfy the function domain. This can be any function that takes the a dictionary with the inputs of the sub-dispatcher node and returns True if input values satisfy the domain, otherwise False.
Note
This function is invoked every time that a data node reach the sub-dispatcher node.
- weight (float, int, optional) – Node weight. It is a weight coefficient that is used by the dispatch algorithm to estimate the minimum workflow.
- inp_weight (dict[str, int | float], optional) – Edge weights from data nodes to the sub-dispatcher node. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
- description (str, optional) – Sub-dispatcher node’s description.
- include_defaults (bool, optional) – If True the default values of the sub-dispatcher are added to the current dispatcher.
- await_domain (bool|int|float, optional) – If True the Dispatcher waits all input results before executing the input_domain function. If a number is defined this is used as timeout for Future.result method [default: True]. Note this is used when asynchronous or parallel execution is enable.
- kwargs (keyword arguments, optional) – Set additional node attributes using key=value.
Returns: Self.
Return type:
-
add_from_lists
(data_list=None, fun_list=None, dsp_list=None)[source]¶ Add multiple function and data nodes to dispatcher.
Parameters: Returns: Self.
Return type:
-