jruizgit / rules

Durable Rules Engine
MIT License
1.14k stars 209 forks source link

MessageNotHandledException in post_batch #363

Open alexsomoza opened 3 years ago

alexsomoza commented 3 years ago

I have this:

from durable.lang import * with ruleset('test1'): @when_all(m.insulation == 'OIP' and ((-44 <= m.tan_dif_hist_20 <= -25) or (25 <= m.tan_dif_hist_20 <= 55)) ) def clase_M(c): print('M') @when_all(m.insulation == 'OIP' and (-25 <= m.tan_dif_hist_20 <= 25)) def clase_G(c): print('G') and I run this post_batch('test1',[{ 'insulation': 'OIP', 'tan_dif_hist_20':55}, { 'insulation': 'OIP', 'tan_dif_hist_20':10}, { 'insulation': 'OIP', 'tan_dif_hist_20':-33}])

and get this error:

MessageNotHandledException Traceback (most recent call last)

in 1 post_batch('test1',[{ 'insulation': 'OIP', 'tan_dif_hist_20':55}, 2 { 'insulation': 'OIP', 'tan_dif_hist_20':10}, ----> 3 { 'insulation': 'OIP', 'tan_dif_hist_20':-33}]) c:\python\python37\lib\site-packages\durable\lang.py in post_batch(ruleset_name, messages, complete) 671 672 def post_batch(ruleset_name, messages, complete = None): --> 673 return get_host().post_batch(ruleset_name, messages, complete) 674 675 def assert_fact(ruleset_name, fact, complete = None): c:\python\python37\lib\site-packages\durable\engine.py in post_batch(self, ruleset_name, messages, complete) 808 def post_batch(self, ruleset_name, messages, complete = None): 809 rules = self.get_ruleset(ruleset_name) --> 810 return self._handle_function(rules, rules.assert_events, messages, complete) 811 812 def assert_fact(self, ruleset_name, fact, complete = None): c:\python\python37\lib\site-packages\durable\engine.py in _handle_function(self, rules, func, args, complete) 788 789 if not complete: --> 790 rules.do_actions(func(args), callback) 791 if error[0]: 792 raise error[0] c:\python\python37\lib\site-packages\durable\engine.py in assert_events(self, messages) 338 339 def assert_events(self, messages): --> 340 return self._handle_result(durable_rules_engine.assert_events(self._handle, json.dumps(messages, ensure_ascii=False)), messages) 341 342 def assert_fact(self, fact): c:\python\python37\lib\site-packages\durable\engine.py in _handle_result(self, result, message) 326 def _handle_result(self, result, message): 327 if result[0] == 1: --> 328 raise MessageNotHandledException(message) 329 elif result[0] == 2: 330 raise MessageObservedException(message) MessageNotHandledException: [{'insulation': 'OIP', 'tan_dif_hist_20': 55}, {'insulation': 'OIP', 'tan_dif_hist_20': 10}, {'insulation': 'OIP', 'tan_dif_hist_20': -33}] M G ¿ What happens with the last rule posted ????
jruizgit commented 3 years ago

Hi, thanks for asking the question. The logical operators for the rules engine are '&' and '|' (yes the bitwise operators). The reason is because in Python it is not possible to override the logical operators, so as much as I don't like it, I had to settle with such syntax. Please try the following ruleset:

from durable.lang import *

with ruleset('test1'):
    @when_all((m.insulation == 'OIP') & (((m.tan_dif_hist_20 <= -25) & (m.tan_dif_hist_20 >= -44)) | ((m.tan_dif_hist_20 <= 55) & (m.tan_dif_hist_20 >=25))))
    def clase_M(c):
        print('M')

    @when_all((m.insulation == 'OIP') & ((m.tan_dif_hist_20 <= 25) & (m.tan_dif_hist_20 >= -25)))
    def clase_G(c):
        print('G')

post_batch('test1',[{ 'insulation': 'OIP', 'tan_dif_hist_20':55},
{ 'insulation': 'OIP', 'tan_dif_hist_20':10},
{ 'insulation': 'OIP', 'tan_dif_hist_20':-33}])