Python Function in BK rules

Help
2008-03-21
2013-05-17
  • Hi.

    I've just begun to try Pyke: it's great. I'm having my first look at forward and backward rules, expert systems and so on... and I believe that Pyke could become a great help for me.
    So than, even if I'm not an expert user, thanks.

    I've just a question. Is it possible to insert python functions in the "when part" of a declaration of a backward rule? For example, is it possible to do something like (this is dummy, just to make the situation clearer):

    c2:
        use testFunc($c)
        when
            check $c == math.sqrt(4)
    ?
    If so, where have I to import math? (I tried to modify the compiled file, but it seems not to work).
    I'd like just to do some controls on tuples directly with function I write in python.

    I really thank you very much.
    Best regards,

    cristian.

     
    • There is a bc_extras clause that you put after the last backward-chaining rule.  Then indent any additional python code after that and it will be included in the generated *_bc.py file.

      c2:
      ....use testFunc($c)
      ....when
      ........check $c == math.sqrt(4)

      bc_extras
      ....import math

      If you need this kind of thing for your forward-chaining rules, use fc_extras after your last forward-chaining rule.

      And finally, if you need extra code for your plan functions, use plan_extras as the last thing in the .krb file.

      You can also define helper functions in these sections.  It just has to be indented so pyke knows where the section ends.

      Hope this helps!

       
    • Ok.
      It was exactly what I needed. I wantet to define helper function in python.

      In my post the indentations disappeared, I believe because of a "bad cut and pase". I'm sorry.

      Thanks a lot. Perhaps everything was clearly described in the documentation... if so, please forgive me, but as you can see english is not my cup of tea...

      Regards.

      Cristian