An GObj is an object that has an inside simple-machine that defines its behavior.
The communication between gobj‘s happens via event‘s:
GObj has the next methods:
And this inherited from ginsfsm.smachine.SMachine:
GObj has the next public attributes:
Events to manage events:
sending events:
- sending events by direct delivery: GObj.send_event().
- sending events by queues: GObj.post_event().
- sending events from outside world: GObj.post_event_from_outside().
- sending events to subscribers: GObj.broadcast_event().
receiving events:
- directly from another gobj‘s who knows you.
- subscribing to events by the GObj.subscribe_event() method.
- you can filtering events being broadcasting with GObj.set_owned_event_filter() method.
Collect event properties. This is the argument received by actions.
Parameters: |
|
---|
Well, yes, I’m a very simple brain. Only a machine. But write a good FSM, and I never fail you. Derive me, and write my FSM.
Sample GObj:
class MyGObj(GObj):
def __init__(self):
GObj.__init__(self, FSM, GCONFIG)
def start_up(self):
''' Initialization zone.'''
Parameters: |
|
---|
My name.
Set by create_gobj().
My parent, destination of my events... or not.
Set by create_gobj().
Set of my gobj childs. Me too like be parent!.
Set a new state. Method to used inside actions, to force the change of state.
Parameters: | new_state – new state to set. |
---|
new_state must match some of the state names of the machine’s state-list or a StateError exception will be raised.
Return the name of the current state.
If there is no state it returns None.
Return the list with the input-event‘s names.
Return the list with the output-event‘s names.
Initialization zone.
Well, the __init__ method is used to build the FSM so I need another function to initialize the new gobj. Please, override me, and write here all the code you need to start up the machine: create your owns childs, etc. This function is called by create_gobj() after creating the gobj instance.
Factory function to create gobj’s instances.
Parameters: |
|
---|---|
Return type: | new gobj instance. |
The factory funcion does:
Add the gobj to their parent child list GObj.dl_childs,
If it’s a named-gobj call the GObj.register_unique_gobj().
Call GObj.start_up().
Add to the gobj several attributes:
Send event to destination, with associated event data kw.
Parameters: |
|
---|---|
Return type: | return the returned value from the executed action. Return None if the event has been post_event(). |
If the event-name exists in the machine, but it’s not accepted by the current state, then no exception is raised but the function returns EventNotAcceptedError.
Note
The inject_event() method doesn’t raise EventNotAcceptedError because a machine should run under any circumstances. In any way an action can raise exceptions.
Same funcionality as send_event(), but the event is processed by gaplic. If the destination is inside of the current gaplic the event will be sent in the next loop cycle.
Broadcast the event to all subscribers.
Parameters: |
---|
Use this function when you don’t know who are your event’s clients, when you don’t know the gobj destination of your output-event‘s
If there is no subscriptors, the event is not sent.
When an event has several subscriptors, there is a mechanism called event-filter that allows to a subcriptor to own the event and no further spread by more subscribers.
The filter function set by set_owned_event_filter() method, is call with the returned value of an action as argument: If the filter function return True, the event is owned, and the ginsfsm.gobj.GObj.broadcast_event() function doesn’t continue sending the event to other subscribers.
Note
If ginsfsm.gobj.GObj.broadcast_event() function uses ginsfsm.gobj.GObj.post_event(), the event-filter cannot be applied.
Same funcionality as post_event(), but the event is sent from non-gobj world.
Subscribe to an event.
Parameters: |
|
---|---|
Returns: | the subscription object. |
__use_post_event__: bool
You must set it to True in order to broadcast the events using post-event instead of send-event.
__rename_event_name__: ‘new event name’
You can rename the output original event name. The original_event_name attribute is added to the sent event with the value of the original event name.
__hard_subscription__: bool
True for permanent subscription.
This subscription cannot be remove, neither with delete_all_subscriptions(). (Well, with force you can)
__subscription_reference__: str
If exists, it will be added as kw in the event broadcast. Can be used by the subscriptor for general purposes.
__deferred_witout_oevent__: Bool
If True, the callback is called without event keyword parameter.
__first_shot__: Bool
If True then generate a broadcast event to the new subscriber. It’s necesary to have __new_subscriptor_event__ flag, and it’s responsability of the gobj to do the first shot.
Remove subscription.
Parameters: |
|
---|
Set a filter function to be used by broadcast_event() function to check the owner of events.
The parameters in settings must be defined in the gobj.
Parameters: |
|
---|
The settings are filtered by the named-gobj or gclass name of this gobj. The parameter name in settings, must be a dot-named, with the first item being the named-gobj o gclass name.
The parameters in settings must be defined in the gobj.
Parameters: |
|
---|
The settings are filtered by the named-gobj or gclass name of this gobj. The parameter name in settings, must be a dot-named, with the first item being the named-gobj o gclass name.
Raised when the event name doesn’t match any name of the event-list list.
Raised when the state name doesn’t match any name of the state-list list.
Raised when something is wrong in the simple-machine definition.
Raised when the event name is good, but the event is not accepted in the current machine’s state.