rules icon indicating copy to clipboard operation
rules copied to clipboard

Something wrong, the results are not what I really want.

Open benlyyan opened this issue 5 years ago • 1 comments

from durable.lang import res = {}

with ruleset('alarm'):

@when_all(all(c.root << (m.title == 'id1')&(m.loc !=None)&(m.time !=None)&(m.aid !=None),
          c.child << (m.title == 'id2') | (m.title=='id3')& (m.loc==c.root.loc) \
          &(m.time<=c.root.time+20)|(m.time>=c.root.time-20)&(m.aid !=None)))
def approved(c):
    print (' rule is ->{0},{1}'.format(c.root.aid,c.child.aid),'correlated')
    k = c.root.aid
    global res 
    if k not in res:
        res[k] = []
    if c.child.aid not in res[k]:
        res[k].append(c.child.aid)

@when_all(all(c.root << (m.title == 'id2')&(m.loc !=None)&(m.time !=None)&(m.aid !=None),
          c.child << (m.title == 'id4') | (m.title=='id5')& (m.loc==c.root.loc) \
          &(m.time<=c.root.time+20)|(m.time>=c.root.time-20)&(m.aid !=None)))
def approved(c):
    print (' rule is ->{0},{1}'.format(c.root.aid,c.child.aid),'correlated')
    k = c.root.aid
    global res 
    if k not in res:
        res[k] = []
    if c.child.aid not in res[k]:
        res[k].append(c.child.aid)

@when_all(all(c.root << (m.title == 'id6')&(m.loc !=None)&(m.time !=None)&(m.aid !=None),
          c.child << (m.title == 'id7') & (m.loc==c.root.loc) \
          &(m.time<=c.root.time+20)|(m.time>=c.root.time-20)&(m.aid !=None)))
def approved(c):
    print (' rule is ->{0},{1}'.format(c.root.aid,c.child.aid),'correlated')
    k = c.root.aid
    global res 
    if k not in res:
        res[k] = []
    if c.child.aid not in res[k]:
        res[k].append(c.child.aid)

@when_start
def start(host):
    # assert
    host.assert_fact('alarm', {'aid':'124','title': 'id1', 'loc': 'pos','time':20.5})
    host.assert_fact('alarm', {'aid':'123','title': 'id2', 'loc': 'pos','time':20})
    host.assert_fact('alarm',{'aid':'125','title':'id3','loc':'pos','time':22})
    host.assert_fact('alarm',{'aid':'126','title':'id4','loc':'pos','time':23})
    host.assert_fact('alarm',{'aid':'127','title':'id5','loc':'pos','time':24})
    host.assert_fact('alarm',{'aid':'128','title':'id6','loc':'pos','time':23})
    host.assert_fact('alarm',{'aid':'129','title':'id7','loc':'pos','time':24})
    

run_all() print(res)

I got results as following: {'128': ['127', '126', '125', '123', '124', '129'], '123': ['124', '125', '126', '127', '128', '129'], '124': ['123', '125', '126', '127', '128', '12 9']} How can I get the results like: {'128':['129'],'124':['123','125'],'123':['126','127']}

benlyyan avatar Apr 29 '19 08:04 benlyyan

I am also new and struggling. Best practise for me is:

    @when_all(m.value.matches('%d{1,3}[.]{1}%d{3}'))
    def value_remove_thousands(c):
        print(sys._getframe(  ).f_code.co_name+': {0}'.format(c.m.value))
        c.retract_fact(c.m)
        c.m.value=123
        c.assert_fact(c.m)

and the results I get "inplace" replaced because of the retract function and then returned by like this (the global I would not use): get_facts('test')

rokdd avatar Oct 23 '20 12:10 rokdd