Example #1
0
    def dispatch(self):
        def callback(error, result):
            pass 

        result = rules.start_action(self._handle)
        if result:
            self._flush_actions(json.loads(result[0]), {'message': json.loads(result[1])}, result[2], callback)
Example #2
0
    'country': 'US',
    'currency': 'US',
    'seller': 'bookstore',
    'item': 'book',
    'reference': '75323',
    'amount': 500
}))
rules.assert_event(handle, json.dumps({
    'id': 1,
    'sid': 'first',
    'item': 'book',
    'status': 'available',
    'country': 'US',
    'seller': 'bookstore'
}))
result = rules.start_action(handle)

print(repr(json.loads(result[0])))
print(repr(json.loads(result[1])))

rules.complete_action(handle, result[2], result[0])
rules.delete_ruleset(handle)

print('books2 ******')

handle = rules.create_ruleset(5, 'books2',  json.dumps({
    'ship': {
        'all': [ 
            {'m': {'$and': [
                {'country': 'US'},
                {'seller': 'bookstore'},
Example #3
0
    def dispatch(self, complete, async_result = None):
        state = None
        action_handle = None
        action_binding = None
        result_container = {}
        if async_result:
            state = async_result[0]
            result_container = {'message': json.loads(async_result[1])}
            action_handle = async_result[2]
            action_binding = async_result[3]
        else:
            try:
                result = rules.start_action(self._handle)
                if not result:
                    complete(None, True)
                    return
                else: 
                    state = json.loads(result[0])
                    result_container = {'message': json.loads(result[1])}
                    action_handle = result[2]
                    action_binding = result[3]
            except BaseException as error:
                t, v, tb = sys.exc_info()
                print('start action base exception type {0}, value {1}, traceback {2}'.format(t, str(v), traceback.format_tb(tb)))
                complete(error, True)
                return
            except:
                t, v, tb = sys.exc_info()
                print('start action unknown exception type {0}, value {1}, traceback {2}'.format(t, str(v), traceback.format_tb(tb)))
                complete('unknown error', True)
                return
        
        while 'message' in result_container:
            action_name = None
            for action_name, message in result_container['message'].items():
                break

            del(result_container['message'])
            c = Closure(self._host, state, message, action_handle, self._name)
            
            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)

            if 'async' in result_container:
                del result_container['async']
                
            self._actions[action_name].run(c, action_callback) 
            result_container['async'] = True 
           
        complete(None, False)
Example #4
0
    def dispatch(self, complete, async_result=None):
        state = None
        action_handle = None
        action_binding = None
        result_container = {}
        if async_result:
            state = async_result[0]
            result_container = {'message': json.loads(async_result[1])}
            action_handle = async_result[2]
            action_binding = async_result[3]
        else:
            try:
                result = rules.start_action(self._handle)
                if not result:
                    complete(None, True)
                    return
                else:
                    state = json.loads(result[0])
                    result_container = {'message': json.loads(result[1])}
                    action_handle = result[2]
                    action_binding = result[3]
            except BaseException as error:
                t, v, tb = sys.exc_info()
                print(
                    'start action base exception type {0}, value {1}, traceback {2}'
                    .format(t, str(v), traceback.format_tb(tb)))
                complete(error, True)
                return
            except:
                t, v, tb = sys.exc_info()
                print(
                    'start action unknown exception type {0}, value {1}, traceback {2}'
                    .format(t, str(v), traceback.format_tb(tb)))
                complete('unknown error', True)
                return

        while 'message' in result_container:
            action_name = None
            for action_name, message in result_container['message'].items():
                break

            del (result_container['message'])
            c = Closure(self._host, state, message, action_handle, self._name)

            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)

            if 'async' in result_container:
                del result_container['async']

            self._actions[action_name].run(c, action_callback)
            result_container['async'] = True

        complete(None, False)
Example #5
0
    def dispatch(self, complete):
        result = None
        try:
            result = rules.start_action(self._handle)
        except Exception as error:
            complete(error)
            return

        if not result:
            complete(None)
        else:
            state = json.loads(result[0])
            event = json.loads(result[1])[self._name]
            actionName = None
            for actionName, event in event.iteritems():
                break

            s = Session(state, event, result[2], self._name)
            action = self._actions[actionName]
                        
            def timers_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    try:
                        rules.complete_action(self._handle, s._handle, json.dumps(s.state))
                        complete(None)
                    except Exception as error:
                        complete(error)

            def messages_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    timers = list(s.get_timers().items())
                    self._start_timers(timers, 0, s, timers_callback)

            def branches_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    messages = list(s.get_messages().items())
                    self._post_messages(messages, 0, s, messages_callback)

            def action_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    branches = list(s.get_branches().items())
                    self._start_branches(branches, 0, s, branches_callback)

            action.run(s, action_callback)
Example #6
0
    def dispatch(self, complete, async_result=None):
        state = None
        action_handle = None
        action_binding = None
        result_container = {}
        if async_result:
            state = async_result[0]
            result_container = {'message': json.loads(async_result[1])}
            action_handle = async_result[2]
            action_binding = async_result[3]
        else:
            try:
                result = rules.start_action(self._handle)
                if result:
                    state = json.loads(result[0])
                    result_container = {'message': json.loads(result[1])}
                    action_handle = result[2]
                    action_binding = result[3]
            except Exception as error:
                complete(error)
                return

        while 'message' in result_container:
            action_name = None
            for action_name, message in result_container['message'].iteritems(
            ):
                break

            del (result_container['message'])
            c = Closure(self._host, state, message, action_handle, self._name)

            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)

            self._actions[action_name].run(c, action_callback)
            result_container['async'] = True

        complete(None)
Example #7
0
    def dispatch(self, complete, async_result = None):
        state = None
        action_handle = None
        action_binding = None
        result_container = {}
        if async_result:
            state = async_result[0]
            result_container = {'message': json.loads(async_result[1])}
            action_handle = async_result[2]
            action_binding = async_result[3]
        else:
            try:
                result = rules.start_action(self._handle)
                if result: 
                    state = json.loads(result[0])
                    result_container = {'message': json.loads(result[1])}
                    action_handle = result[2]
                    action_binding = result[3]
            except Exception as error:
                complete(error)
                return
        
        while 'message' in result_container:
            action_name = None
            for action_name, message in result_container['message'].iteritems():
                break

            del(result_container['message'])
            c = Closure(self._host, state, message, action_handle, self._name)
            
            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)
                
            self._actions[action_name].run(c, action_callback) 
            result_container['async'] = True 
           
        complete(None)
Example #8
0
    def dispatch(self, complete):
        result = None
        try:
            result = rules.start_action(self._handle)
        except Exception as error:
            complete(error)
            return

        if not result:
            complete(None)
        else:
            state = json.loads(result[0])
            event = json.loads(result[1])[self._name]
            actionName = None
            for actionName, event in event.iteritems():
                break

            s = Session(state, event, result[2], self._name)
            action = self._actions[actionName]

            def timers_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    try:
                        rules.complete_action(self._handle, s._handle,
                                              json.dumps(s.state))
                        complete(None)
                    except Exception as error:
                        complete(error)

            def messages_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    timers = list(s.get_timers().items())
                    self._start_timers(timers, 0, s, timers_callback)

            def branches_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    messages = list(s.get_messages().items())
                    self._post_messages(messages, 0, s, messages_callback)

            def action_callback(e):
                if e:
                    try:
                        rules.abandon_action(self._handle, s._handle)
                        complete(e)
                    except Exception as error:
                        complete(error)
                else:
                    branches = list(s.get_branches().items())
                    self._start_branches(branches, 0, s, branches_callback)

            action.run(s, action_callback)