def __init__(self, name='', datastore=None, subscription=None, handle_response=None, handle_agree=None, handle_refuse=None, handle_inform=None, handle_failure=None, cancel=None, check_cancel=None, **namedargs): if datastore is None: datastore = {} if handle_response is not None: self.handle_response = handle_response if handle_agree is not None: self.handle_agree = handle_agree if handle_refuse is not None: self.handle_refuse = handle_refuse if handle_inform is not None: self.handle_inform = handle_inform if handle_failure is not None: self.handle_failure = handle_failure if cancel is not None: self.cancel = cancel if check_cancel is not None: self.check_cancel = check_cancel datastore[subscription_key] = subscription super(SubscriptionInitiatorBehaviour, self).__init__(name, **namedargs) # # States # send_initiation_state = State(SendSubscriptionBehaviour(datastore=datastore, check_cancel=self.check_cancel, name="Send-initiation")) await_response_state = State(AwaitResponseBehaviour(datastore=datastore, check_cancel=self.check_cancel, name="Await-response")) handle_response_state = State(HandleResponseBehaviour(datastore=datastore, check_cancel=self.check_cancel, handle_response=self.handle_response, name="Handle-response")) handled_response_state = State(HandledResponseBehaviour(datastore=datastore, check_cancel=self.check_cancel, name="Handled-response")) handle_agree_state = State(HandleAgreeBehaviour(datastore=datastore, check_cancel=self.check_cancel, handle_agree=self.handle_agree, name="Handle-agree")) handle_refuse_state = State(HandleRefuseBehaviour(datastore=datastore, check_cancel=self.check_cancel, handle_refuse=self.handle_refuse, name="Handle-refuse")) handle_inform_state = State(HandleInformBehaviour(datastore=datastore, check_cancel=self.check_cancel, handle_inform=self.handle_inform, name="Handle-inform")) handle_failure_state = State(HandleFailureBehaviour(datastore=datastore, check_cancel=self.check_cancel, handle_failure=self.handle_failure, name="Handle-failure")) request_cancel_state = State(RequestCancelBehaviour(datastore=datastore, check_cancel=self.check_cancel, cancel=self.cancel, name="Request-cancel")) await_cancellation_result_state = State(AwaitCancellationResultBehaviour(datastore=datastore, check_cancel=self.check_cancel, name="Await-cancellation-result")) # self.first_state = send_initiation_state # # Transitions # states = [ send_initiation_state, await_response_state, handle_response_state, handled_response_state, handle_agree_state, handle_refuse_state, handle_inform_state, handle_failure_state, request_cancel_state, await_cancellation_result_state ] for state in states: self.add_transition(state, request_cancel_state, CANCEL_SIGNALLED) # self.add_transition(send_initiation_state, await_response_state, INITIATION_SENT) self.add_transition(await_response_state, await_response_state) self.add_transition(await_response_state, handle_response_state, RESPONSE_RECEIVED) self.add_transition(handle_response_state, handle_agree_state, AGREE_RECEIVED) self.add_transition(handle_response_state, handle_refuse_state, REFUSE_RECEIVED) self.add_transition(handle_response_state, handle_inform_state, INFORM_RECEIVED) self.add_transition(handle_response_state, handle_failure_state, FAILURE_RECEIVED) self.add_transition(handle_agree_state, handled_response_state, AGREE_HANDLED) self.add_transition(handle_refuse_state, handled_response_state, REFUSE_HANDLED) self.add_transition(handle_inform_state, handled_response_state, INFORM_HANDLED) self.add_transition(handle_failure_state, None, FAILURE_HANDLED) self.add_transition(handled_response_state, await_response_state, RESPONSE_HANDLED) self.add_transition(handle_response_state, await_response_state, RESPONSE_HANDLED) self.add_transition(request_cancel_state, await_cancellation_result_state, CANCEL_REQUESTED) self.add_transition(await_cancellation_result_state, await_cancellation_result_state) self.add_transition(await_cancellation_result_state, None, CANCEL_SUCCEEDED) self.add_transition(await_cancellation_result_state, None, CANCEL_FAILED) # FIXME: Retry cancellation? self.add_transition(await_response_state, None, FINISHED)
def setup(self): print "MyFSMBehaviour setup" first_state = State(MyBehaviour(name='first', result_value='1')) second_state = State(MyBehaviour(name='second', result_value='2')) third_state = State(MyBehaviour(name='third', result_value='3')) self.add_transition(first_state, second_state, '1') self.add_transition(second_state, third_state, '2') self.add_transition(third_state, first_state, '3') self.first_state = first_state
def __init__(self, name='', datastore=None, send_response=None, send_notifications=None, cancel=None, check_updates=None, **namedargs): if datastore is None: datastore = {} if send_response is not None: self.send_response = send_response if send_notifications is not None: self.send_notifications = send_notifications if cancel is not None: self.cancel = cancel if check_updates is not None: self.check_updates = check_updates datastore[subscribers_key] = [] super(SubscriptionParticipantBehaviour, self).__init__(name, **namedargs) # # States # await_initiation_state = State(AwaitInitiationBehaviour(datastore=datastore, name="Await-initiation")) send_response_state = State(SendResponseBehaviour(datastore=datastore, send_response=self.send_response, name="Send-response")) check_updates_state = State(CheckUpdatesBehaviour(datastore=datastore, check_updates=self.check_updates, name="Check-updates")) send_notifications_state = State(SendNotificationBehaviour(datastore=datastore, send_notifications=self.send_notifications, name="Send-notifications")) process_cancel_state = State(ProcessCancelBehaviour(datastore=datastore, cancel=self.cancel, name="Process-cancel")) # self.first_state = await_initiation_state # # Transitions # self.add_transition(await_initiation_state, check_updates_state) self.add_transition(await_initiation_state, send_response_state, INITIATION_RECEIVED) self.add_transition(await_initiation_state, process_cancel_state, CANCEL_REQUESTED) self.add_transition(await_initiation_state, None, FINISHED) self.add_transition(send_response_state, await_initiation_state, RESPONSE_SENT) self.add_transition(process_cancel_state, await_initiation_state, CANCELLED) self.add_transition(check_updates_state, send_notifications_state, UPDATED) self.add_transition(check_updates_state, await_initiation_state, NOT_UPDATED) self.add_transition(send_notifications_state, await_initiation_state, NOTIFIED)
def __init__(self, name='', datastore=None, deadline=None, make_proposal=None, execute_contract=None, forget_contract=None, **namedargs): if datastore is None: datastore = {} if deadline is None: deadline = time.time() + 60 if make_proposal is not None: self.make_proposal = make_proposal if execute_contract is not None: self.execute_contract = execute_contract if forget_contract is not None: self.forget_contract = forget_contract super(ContractNetParticipantBehaviour, self).__init__(name, **namedargs) # # States # receive_initiation_state = State( self.ReceiveCallBehaviour(datastore=datastore, name="Receive-Initiation")) make_proposal_state = State( self.MakeProposalBehaviour(datastore=datastore, make_proposal=self.make_proposal, name="Make-Proposal")) send_response_state = State( self.SendResponseBehaviour(datastore=datastore, name="Send-Response")) receive_conclusion_state = State( self.ReceiveConclusionBehaviour(datastore=datastore, name="Receive-Conclusion")) handle_conclusion_state = State( self.HandleConclusionBehaviour(datastore=datastore, name="Handle-Conclusion")) handle_accept_proposal_state = State( self.HandleAcceptBehaviour(datastore=datastore, execute_contract=self.execute_contract, name="Handle-Accept-Proposal")) handle_reject_proposal_state = State( self.HandleRejectBehaviour(datastore=datastore, forget_contract=self.forget_contract, name="Handle-Reject-Proposal")) handled_conclusion_state = State( self.HandledConclusionBehaviour(datastore=datastore, name="Handled-Conclusion")) cancel_state = State( self.ParticipantCancelBehaviour(datastore=datastore, name="Cancel")) # self.first_state = receive_initiation_state # # Transitions # self.add_transition(receive_initiation_state, receive_initiation_state) self.add_transition(receive_initiation_state, make_proposal_state, self.INITIATION_RECEIVED) self.add_transition(make_proposal_state, make_proposal_state) self.add_transition(make_proposal_state, send_response_state, self.PROPOSAL_MADE) self.add_transition(send_response_state, receive_conclusion_state, self.RESPONSE_SENT) self.add_transition(receive_conclusion_state, receive_conclusion_state) self.add_transition(receive_conclusion_state, handle_conclusion_state, self.CONCLUSION_RECEIVED) self.add_transition(handle_conclusion_state, handle_accept_proposal_state, self.ACCEPT_RECEIVED) self.add_transition(handle_conclusion_state, handle_reject_proposal_state, self.REJECT_RECEIVED) self.add_transition(handle_accept_proposal_state, handled_conclusion_state, self.ACCEPT_HANDLED) self.add_transition(handle_reject_proposal_state, handled_conclusion_state, self.REJECT_HANDLED) self.add_transition(handled_conclusion_state, receive_initiation_state, self.CONCLUSION_HANDLED) self.add_transition(receive_initiation_state, cancel_state, self.CANCELLING) self.add_transition(cancel_state, None, self.CANCELLED) self.add_transition(receive_initiation_state, None, self.FINISHED)
def __init__(self, name='', datastore=None, deadline=None, select_proposal=None, process_result=None, **namedargs): if datastore is None: datastore = {} if deadline is None: deadline = time.time() + 60 if select_proposal is not None: self.select_proposal = select_proposal if process_result is not None: self.process_result = process_result super(ContractNetInitiatorBehaviour, self).__init__(name, **namedargs) # # States # send_initiation_state = State( self.SendCallBehaviour(datastore=datastore, name="Send-Initiation")) receive_response_state = State( self.ReceiveResponseBehaviour(datastore=datastore, deadline=deadline, name="Receive-Response")) handle_response_state = State( self.HandleResponseBehaviour(datastore=datastore, name="Handle-Response")) handle_propose_state = State( self.HandleProposeBehaviour(datastore=datastore, name="Handle-Propose")) handle_refuse_state = State( self.HandleRefuseBehaviour(datastore=datastore, name="Handle-Refuse")) handled_response_state = State( self.HandledResponseBehaviour(datastore=datastore, name="Handled-Response")) select_proposal_state = State( self.SelectProposalBehaviour(datastore=datastore, select_proposal=self.select_proposal, name="Select-Proposal")) send_conclusion_state = State( self.SendConclusionBehaviour(datastore=datastore, name="Send-Conclusion")) receive_result_state = State( self.ReceiveResultBehaviour(datastore=datastore, name="Receive-Result")) handle_result_state = State( self.HandleResultBehaviour(datastore=datastore, name="Handle-Result")) handle_inform_done_state = State( self.HandleInformDoneBehaviour(datastore=datastore, process_result=self.process_result, name="Handle-Inform-Done")) handle_inform_result_state = State( self.HandleInformResultBehaviour( datastore=datastore, process_result=self.process_result, name="Handle-Inform-Result")) handle_failure_state = State( self.HandleFailureBehaviour(datastore=datastore, name="Handle-Failure")) cancel_state = State( self.InitiatorCancelBehaviour(datastore=datastore, name="Cancel")) # self.first_state = send_initiation_state # # Transitions # self.add_transition(send_initiation_state, receive_response_state, self.INITIATION_SENT) self.add_transition(receive_response_state, receive_response_state) self.add_transition(receive_response_state, handle_response_state, self.RESPONSE_RECEIVED) self.add_transition(handle_response_state, handle_propose_state, self.PROPOSE_RECEIVED) self.add_transition(handle_response_state, handle_refuse_state, self.REFUSE_RECEIVED) self.add_transition(handle_propose_state, handled_response_state, self.PROPOSE_HANDLED) self.add_transition(handle_refuse_state, handled_response_state, self.REFUSE_HANDLED) self.add_transition(handled_response_state, receive_response_state, self.RESPONSE_HANDLED) self.add_transition(handled_response_state, select_proposal_state, self.ALL_RESPONSES_RECEIVED) #self.add_transition(handled_response_state, select_proposal_state, self.DEADLINE_REACHED) self.add_transition(receive_response_state, select_proposal_state, self.DEADLINE_REACHED) self.add_transition(select_proposal_state, send_conclusion_state, self.PROPOSAL_SELECTED) self.add_transition(select_proposal_state, cancel_state, self.CANCELLING) self.add_transition(send_conclusion_state, receive_result_state, self.CONCLUSION_SENT) self.add_transition(receive_result_state, receive_result_state) self.add_transition(receive_result_state, handle_result_state, self.RESULT_RECEIVED) self.add_transition(handle_result_state, handle_inform_done_state, self.INFORM_DONE_RECEIVED) self.add_transition(handle_result_state, handle_inform_result_state, self.INFORM_RESULT_RECEIVED) self.add_transition(handle_result_state, handle_failure_state, self.FAILURE_RECEIVED) self.add_transition(handle_inform_done_state, None, self.FINISHED) self.add_transition(handle_inform_result_state, None, self.FINISHED) self.add_transition(handle_failure_state, None, self.FINISHED) # self.add_transition(receive_response_state, cancel_state, self.CANCELLING) self.add_transition(receive_result_state, cancel_state, self.CANCELLING) self.add_transition(cancel_state, None, self.CANCELLED) self.add_transition(receive_response_state, None, self.FINISHED)
def __init__(self, name='', datastore=None, send_response=None, perform_request=None, send_result=None, cancel=None, **namedargs): if datastore is None: datastore = {} if send_response is not None: self.send_response = send_response if perform_request is not None: self.perform_request = perform_request if send_result is not None: self.send_result = send_result if cancel is not None: self.cancel = cancel datastore[customer_key] = None super(RequestParticipantBehaviour, self).__init__(name, **namedargs) # # States # await_initiation_state = State( AwaitInitiationBehaviour(datastore=datastore, name="Await-initiation")) send_response_state = State( SendResponseBehaviour(datastore=datastore, send_response=self.send_response, name="Send-response")) perform_request_state = State( PerformRequestBehaviour(datastore=datastore, perform_request=self.perform_request, name="Perform-Request")) send_result_state = State( SendResultBehaviour(datastore=datastore, send_result=self.send_result, name="Send-result")) process_cancel_state = State( ProcessCancelBehaviour(datastore=datastore, cancel=self.cancel, name="Process-cancel")) # self.first_state = await_initiation_state # # Transitions # self.add_transition(await_initiation_state, await_initiation_state) self.add_transition(await_initiation_state, send_response_state, INITIATION_RECEIVED) self.add_transition(await_initiation_state, process_cancel_state, CANCEL_REQUESTED) self.add_transition(await_initiation_state, None, FINISHED) self.add_transition(send_response_state, perform_request_state, RESPONSE_SENT) self.add_transition(perform_request_state, perform_request_state) self.add_transition(perform_request_state, send_result_state, REQUEST_PERFORMED) self.add_transition(send_result_state, await_initiation_state, RESULT_SENT) self.add_transition(process_cancel_state, None, CANCELLED)