Esempio n. 1
0
            def action_callback(e):
                if c._has_completed():
                    return

                if e:
                    rules.abandon_action(self._handle, c._handle)
                    complete(e, True)
                else:
                    try:
                        for timer_name, timer in c.get_cancelled_timers().items():
                            self.cancel_timer(c.s['sid'], timer_name)

                        for timer_id, timer_tuple in c.get_timers().items():
                            self.start_timer(c.s['sid'], timer_tuple[0], timer_tuple[1], timer_tuple[2])

                        for ruleset_name, q in c.get_queues().items():
                            for message in q.get_queued_posts():
                                self.queue_assert_event(message['sid'], ruleset_name, message)

                            for message in q.get_queued_asserts():
                                self.queue_assert_fact(message['sid'], ruleset_name, message)

                            for message in q.get_queued_retracts():
                                self.queue_retract_fact(message['sid'], ruleset_name, message)

  
                        for ruleset_name, sid in c.get_deletes().items():
                            self._host.delete_state(ruleset_name, sid)

                        binding  = 0
                        replies = 0
                        pending = {action_binding: 0}
        
                        for ruleset_name, facts in c.get_retract_facts().items():
                            if len(facts) == 1:
                                binding, replies = self._host.start_retract_fact(ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_retract_facts(ruleset_name, facts)
                           
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies
                        
                        for ruleset_name, facts in c.get_facts().items():
                            if len(facts) == 1:
                                binding, replies = self._host.start_assert_fact(ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_assert_facts(ruleset_name, facts)
                            
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, messages in c.get_messages().items():
                            if len(messages) == 1:
                                binding, replies = self._host.start_post(ruleset_name, messages[0])
                            else:
                                binding, replies = self._host.start_post_batch(ruleset_name, messages)
                            
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        binding, replies = rules.start_update_state(self._handle, c._handle, json.dumps(c.s._d, ensure_ascii=False))
                        if binding in pending:
                            pending[binding] = pending[binding] + replies
                        else:
                            pending[binding] = replies
                        
                        for binding, replies in pending.items():
                            if binding != 0:
                                if binding != action_binding:
                                    rules.complete(binding, replies)
                                else:
                                    new_result = rules.complete_and_start_action(self._handle, replies, c._handle)
                                    if new_result:
                                        if 'async' in result_container:
                                            def terminal(e, wait):
                                                return

                                            self.dispatch(terminal, [state, new_result, action_handle, action_binding])
                                        else:
                                            result_container['message'] = json.loads(new_result)

                    except BaseException as error:
                        t, v, tb = sys.exc_info()
                        print('base exception type {0}, value {1}, traceback {2}'.format(t, str(v), traceback.format_tb(tb)))
                        rules.abandon_action(self._handle, c._handle)
                        complete(error, True)
                    except:
                        print('unknown exception type {0}, value {1}, traceback {2}'.format(t, str(v), traceback.format_tb(tb)))
                        rules.abandon_action(self._handle, c._handle)
                        complete('unknown error', True)

                    if c._is_deleted():
                        try:
                            self.delete_state(c.s.sid)
                        except BaseException as error:
                            complete(error, True)
Esempio n. 2
0
            def action_callback(e):
                if c._has_completed():
                    return

                if e:
                    rules.abandon_action(self._handle, c._handle)
                    complete(e, True)
                else:
                    try:
                        for timer_id, timer in c.get_cancelled_timers().items(
                        ):
                            self.cancel_timer(c.s['sid'], timer)

                        for timer_id, timer_duration in c.get_timers().items():
                            self.start_timer(c.s['sid'], timer_duration[0],
                                             timer_duration[1])

                        for ruleset_name, q in c.get_queues().items():
                            for message in q.get_queued_posts():
                                self.queue_assert_event(
                                    message['sid'], ruleset_name, message)

                            for message in q.get_queued_asserts():
                                self.queue_assert_fact(message['sid'],
                                                       ruleset_name, message)

                            for message in q.get_queued_retracts():
                                self.queue_retract_fact(
                                    message['sid'], ruleset_name, message)

                        for ruleset_name, sid in c.get_deletes().items():
                            self._host.delete_state(ruleset_name, sid)

                        binding = 0
                        replies = 0
                        pending = {action_binding: 0}

                        for ruleset_name, facts in c.get_retract_facts().items(
                        ):
                            if len(facts) == 1:
                                binding, replies = self._host.start_retract_fact(
                                    ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_retract_facts(
                                    ruleset_name, facts)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, facts in c.get_facts().items():
                            if len(facts) == 1:
                                binding, replies = self._host.start_assert_fact(
                                    ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_assert_facts(
                                    ruleset_name, facts)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, messages in c.get_messages().items():
                            if len(messages) == 1:
                                binding, replies = self._host.start_post(
                                    ruleset_name, messages[0])
                            else:
                                binding, replies = self._host.start_post_batch(
                                    ruleset_name, messages)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        binding, replies = rules.start_update_state(
                            self._handle, c._handle,
                            json.dumps(c.s._d, ensure_ascii=False))
                        if binding in pending:
                            pending[binding] = pending[binding] + replies
                        else:
                            pending[binding] = replies

                        for binding, replies in pending.items():
                            if binding != 0:
                                if binding != action_binding:
                                    rules.complete(binding, replies)
                                else:
                                    new_result = rules.complete_and_start_action(
                                        self._handle, replies, c._handle)
                                    if new_result:
                                        if 'async' in result_container:

                                            def terminal(e, wait):
                                                return

                                            self.dispatch(
                                                terminal, [
                                                    state, new_result,
                                                    action_handle,
                                                    action_binding
                                                ])
                                        else:
                                            result_container[
                                                'message'] = json.loads(
                                                    new_result)

                    except BaseException as error:
                        t, v, tb = sys.exc_info()
                        print(
                            'base exception type {0}, value {1}, traceback {2}'
                            .format(t, str(v), traceback.format_tb(tb)))
                        rules.abandon_action(self._handle, c._handle)
                        complete(error, True)
                    except:
                        print(
                            'unknown exception type {0}, value {1}, traceback {2}'
                            .format(t, str(v), traceback.format_tb(tb)))
                        rules.abandon_action(self._handle, c._handle)
                        complete('unknown error', True)

                    if c._is_deleted():
                        try:
                            self.delete_state(c.s.sid)
                        except BaseException as error:
                            complete(error, True)
Esempio n. 3
0
            def action_callback(e):
                if e:
                    rules.abandon_action(self._handle, c._handle)
                    complete(e)
                else:
                    try:
                        for timer_name, timer_duration in c.get_timers(
                        ).iteritems():
                            self.start_timer(c.s['sid'], timer_name,
                                             timer_duration)

                        for ruleset_name, messages in c.get_queued_messages(
                        ).iteritems():
                            for message in messages:
                                self.queue_event(c.s['sid'], ruleset_name,
                                                 message)

                        binding = 0
                        replies = 0
                        pending = {action_binding: 0}

                        for ruleset_name, facts in c.get_retract_facts(
                        ).iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_retract_fact(
                                    ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_retract_facts(
                                    ruleset_name, facts)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, facts in c.get_facts().iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_assert_fact(
                                    ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_assert_facts(
                                    ruleset_name, facts)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, messages in c.get_messages(
                        ).iteritems():
                            if len(messages) == 1:
                                binding, replies = self._host.start_post(
                                    ruleset_name, messages[0])
                            else:
                                binding, replies = self._host.start_post_batch(
                                    ruleset_name, messages)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        binding, replies = rules.start_update_state(
                            self._handle, c._handle, json.dumps(c.s._d))
                        if binding in pending:
                            pending[binding] = pending[binding] + replies
                        else:
                            pending[binding] = replies

                        for binding, replies in pending.iteritems():
                            if binding != 0:
                                if binding != action_binding:
                                    rules.complete(binding, replies)
                                else:
                                    new_result = rules.complete_and_start_action(
                                        self._handle, replies, c._handle)
                                    if new_result:
                                        if 'async' in result_container:

                                            def terminal(e):
                                                return

                                            self.dispatch(
                                                terminal, [
                                                    state, new_result,
                                                    action_handle,
                                                    action_binding
                                                ])
                                        else:
                                            result_container[
                                                'message'] = json.loads(
                                                    new_result)

                    except Exception as error:
                        rules.abandon_action(self._handle, c._handle)
                        complete(error)
Esempio n. 4
0
            def action_callback(e):
                if e:
                    rules.abandon_action(self._handle, c._handle)
                    complete(e)
                else:
                    try:
                        for timer_name, timer_duration in c.get_timers().iteritems():
                            self.start_timer(c.s['sid'], timer_name, timer_duration)

                        for ruleset_name, messages in c.get_queued_messages().iteritems():
                            for message in messages:
                                self.queue_event(c.s['sid'], ruleset_name, message)                            
  
                        binding  = 0
                        replies = 0
                        pending = {action_binding: 0}
        
                        for ruleset_name, facts in c.get_retract_facts().iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_retract_fact(ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_retract_facts(ruleset_name, facts)
                           
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies
                        
                        for ruleset_name, facts in c.get_facts().iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_assert_fact(ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_assert_facts(ruleset_name, facts)
                            
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, messages in c.get_messages().iteritems():
                            if len(messages) == 1:
                                binding, replies = self._host.start_post(ruleset_name, messages[0])
                            else:
                                binding, replies = self._host.start_post_batch(ruleset_name, messages)
                            
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        binding, replies = rules.start_update_state(self._handle, c._handle, json.dumps(c.s._d))
                        if binding in pending:
                            pending[binding] = pending[binding] + replies
                        else:
                            pending[binding] = replies
                        
                        for binding, replies in pending.iteritems():
                            if binding != 0:
                                if binding != action_binding:
                                    rules.complete(binding, replies)
                                else:
                                    new_result = rules.complete_and_start_action(self._handle, replies, c._handle)
                                    if new_result:
                                        if 'async' in result_container:
                                            def terminal(e):
                                                return

                                            self.dispatch(terminal, [state, new_result, action_handle, action_binding])
                                        else:
                                            result_container['message'] = json.loads(new_result)

                    except Exception as error:
                        rules.abandon_action(self._handle, c._handle)
                        complete(error)
Esempio n. 5
0
            def action_callback(e):
                if e:
                    rules.abandon_action(self._handle, c._handle)
                    complete(e)
                else:
                    try:
                        for branch_name, branch_state in c.get_branches(
                        ).iteritems():
                            self._host.patch_state(branch_name, branch_state)

                        for timer_name, timer_duration in c.get_timers(
                        ).iteritems():
                            self.start_timer(c.s['sid'], timer_name,
                                             timer_duration)

                        binding = 0
                        replies = 0
                        pending = {action_binding: 0}

                        binding, replies = rules.start_update_state(
                            self._handle, c._handle, json.dumps(c.s._d))
                        pending[binding] = replies

                        for ruleset_name, facts in c.get_retract_facts(
                        ).iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_retract_fact(
                                    ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_retract_facts(
                                    ruleset_name, facts)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, facts in c.get_facts().iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_assert_fact(
                                    ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_assert_facts(
                                    ruleset_name, facts)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, messages in c.get_messages(
                        ).iteritems():
                            if len(messages) == 1:
                                binding, replies = self._host.start_post(
                                    ruleset_name, messages[0])
                            else:
                                binding, replies = self._host.start_post_batch(
                                    ruleset_name, messages)

                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for binding, replies in pending.iteritems():
                            if binding != action_binding:
                                rules.complete(binding, replies)
                            else:
                                new_result = rules.complete_and_start_action(
                                    self._handle, replies, c._handle)
                                if new_result:
                                    result_container['message'] = json.loads(
                                        new_result)

                    except Exception as error:
                        rules.abandon_action(self._handle, c._handle)
                        complete(error)
Esempio n. 6
0
            def action_callback(e):
                if e:
                    rules.abandon_action(self._handle, c._handle)
                    complete(e)
                else:
                    try:
                        for branch_name, branch_state in c.get_branches().iteritems():
                            self._host.patch_state(branch_name, branch_state)

                        for timer_name, timer_duration in c.get_timers().iteritems():
                            self.start_timer(c.s['sid'], timer_name, timer_duration)                            
  
                        binding  = 0
                        replies = 0
                        pending = {action_binding: 0}

                        binding, replies = rules.start_update_state(self._handle, c._handle, json.dumps(c.s._d))
                        pending[binding] = replies
                                
                        for ruleset_name, facts in c.get_retract_facts().iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_retract_fact(ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_retract_facts(ruleset_name, facts)
                           
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies
                        
                        for ruleset_name, facts in c.get_facts().iteritems():
                            if len(facts) == 1:
                                binding, replies = self._host.start_assert_fact(ruleset_name, facts[0])
                            else:
                                binding, replies = self._host.start_assert_facts(ruleset_name, facts)
                            
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for ruleset_name, messages in c.get_messages().iteritems():
                            if len(messages) == 1:
                                binding, replies = self._host.start_post(ruleset_name, messages[0])
                            else:
                                binding, replies = self._host.start_post_batch(ruleset_name, messages)
                            
                            if binding in pending:
                                pending[binding] = pending[binding] + replies
                            else:
                                pending[binding] = replies

                        for binding, replies in pending.iteritems():
                            if binding != action_binding:
                                rules.complete(binding, replies)
                            else:
                                new_result = rules.complete_and_start_action(self._handle, replies, c._handle)
                                if new_result:
                                    result_container['message'] = json.loads(new_result)

                    except Exception as error:
                        rules.abandon_action(self._handle, c._handle)
                        complete(error)