Eval of defrule fails

Help
2011-07-22
2013-04-25
  • Eval fails in a simple defrule:

    clips.Eval('(defrule duck (animal-is duck) => (assert (sound-is quack)))')
    

    And the output:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.6/dist-packages/clips/_clips_wrap.py", line 149, in _WRAPPER
        return f(*args)
      File "/usr/lib/python2.6/dist-packages/clips/_clips_wrap.py", line 238, in _WRAPPER
        return f(*newargs)
      File "/usr/lib/python2.6/dist-packages/clips/_clips_wrap.py", line 4014, in Eval
        return _cl2py(_c.eval(expr))
    _clips.ClipsError: C10: unable to evaluate expression
    
     
  • Johan Lindberg
    Johan Lindberg
    2011-07-24

    Hi,

    That's because you cannot use clips.Eval to define new rules. See Chapter 12.3.5 Evaluating a Function within a String in the CLIPS Basic Programming Guide.

    … NOTE: eval does not permit the use of local variables (except when the local variables are defined as part of the command such as with an instance query function), nor will it evaluate any of the construct definition forms (i.e., defrule, deffacts, etc.). The return value is the result of the evaluation of the string (or FALSE if an error occurs). …

    You should use the function Build instead. See 12.3.6 Evaluating a Construct within a String.

    HTH
    Johan Lindberg
    johan@pulp.se