2

Just installed clipspy on Fedora 26. I can assert simple facts like so:

>>> from clips import Environment
>>> env = Environment()
>>> env.assert_string('(a)')
ImpliedFact: f-1     (a)

How do I programmatically define a rule please? I can't find any examples or documentation about that. TIA.

noxdafox
  • 14,439
  • 4
  • 33
  • 45

1 Answers1

3

You can find the documentation here: clipspy documentation

However, you can either use build or eval like in CLIPS:

>>> env.build(...your defrule)
>>> env.eval("(build ...)")

or you can create it with:

clips.agenda.Rule(env, rule_you_want_to_define)

Don't forget the " " around your rule definition.

kombinatorix
  • 122
  • 1
  • 2
  • 6
  • Thank you. The following works env.build("(defrule r1 (a) => (assert (b)))") so I have accepted the answer. However. Giving the same input to env.eval gives [EXPRNPSR3] Missing function declaration for defrule. Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/site-packages/clips/environment.py", line 168, in eval raise CLIPSError(self._env) clips.error.CLIPSError: [EXPRNPSR3] Missing function declaration for defrule. – user2366646 Jun 04 '18 at 09:30
  • Giving the same input directly to `Rule` (e.g., `Rule(env, ...)`) and then trying to print the rule gives `Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/site-packages/clips/agenda.py", line 223, in __repr__ string = lib.EnvGetDefrulePPForm(self._env, self._rule) TypeError: initializer for ctype 'void *' must be a cdata pointer, not Environment` – user2366646 Jun 04 '18 at 09:36
  • I edited my answer. The problem in eval is, that you have to evaluate a build function. Otherwise CLIPS will interpret defrule as function an therefore you get [EXPRNPSR3] Missing function declaration for defrule. Your other problem is python related. Without your code I can't say nothing. – kombinatorix Jun 05 '18 at 08:01
  • Author here, do not use class constructors to build rules or facts. Those objects are returned by the `Environment` methods. To define rules/deftemplates/declasses/etc you need to pass `CLIPS` code to the `Environment.build` method. Another way is to write your definitions in a file and use the `Environment.load` method as for the example in the README. – noxdafox Jun 20 '18 at 11:46