Debugging in CLIPS

I spend part of my time at CLIPSESG and I try to help whenever I can. Usually that involves figuring out why a rule is, or isn't, firing and mostly it's a simple-enough error or an "obvious" mistake that causes the problem. The thing is that it's mostly real easy to find them as well. At least if you know where to look.

One (very useful) way of looking at your CLIPS program is by using the built-in facts and matches functions (see chapter 13.4.1 Displaying the Fact-List and chapter 13.6.4 Displaying Matches for a Rule in the CLIPS Basic Programming Guide). There are some things to think about though.

Evaluating (facts) is tricky in the same way select * from Foo is tricky in SQL. You've got to make sure that when you've got enough facts in Working Memory you don't print them all in the shell. Mostly because it takes a while, but also because you can't scroll more than a few hundred lines. So if what you're interested in is printed early on, chances are you won't be able to see it anyway.

The matches function is equally verbose. Unfortunately there's no way to turn that verbosity off. If you've got a lot of facts matching some pattern in a rule they will all be printed to the shell and if you've got a lot of patterns with a lot of matching facts you're not going to be able to inspect all of the output in the shell. Luckily you can use the dribble-on/off functions (see chapter 13.2.1 Generating Trace Files in the BPG) to copy output to a file. That way, you can use your favorite text editor to inspect the output instead.

I should also mention breakpoints and set-break (see chapter 13.6.5 Setting a Breakpoint for a Rule in the BPG). They're great if you need to inspect the application after it has done some processing but before it has finished. You can also use the poor-man's version: (run n) if you're really certain on how many rule firings it takes to build up the problematic state. But I've found that I usually only use that when I'm in an exploratory mood and step through the application using (run 1).

It might take a while to become familiar with debugging in CLIPS but it's well worth the effort.

2 kommentarer:

woolfel sa...

In jamocha I provide the option of filtering the facts. That way, you can print out just the facts for a given deftemplate.

Johan Lindberg sa...

That sounds very convenient (I'm assuming you're talking about the facts-function here). I always end up doing find-all-facts querys instead.