How to check pattern details in output of "matches"?

36 views
Skip to first unread message

Xuan Wu

unread,
Mar 18, 2025, 10:29:46 PMMar 18
to CLIPSESG
Hi all, I just started to study the book "Adventures in Rule‑Based Programming". On page 54:

CLIPS> (matches get_command)
Matches for Pattern 1
None
Activations
*
(0 0 1)

And here's the first part of get_command
```
(defrule get_command
(declare (salience -10))
(not (command))
=>
...

It seems, "Pattern 1" refers to `(command)` instead of `(not (command))`. I wonder if there's command like ppfact to check the detail of a pattern in a rule? I don't find any in the reference manual.

Thanks.

CLIPS Support

unread,
Mar 18, 2025, 11:20:25 PMMar 18
to CLIPSESG
Yes, pattern in the matches command refers to pattern conditional element. So (command) is a pattern conditional element, while (not (command)) is a not conditional element containing a pattern conditional element. There is no command that breaks down which facts partially match a specific pattern in a rule.

Xuan Wu

unread,
Mar 19, 2025, 12:46:33 AMMar 19
to CLIPSESG
Thanks for explaining. By "check the detail of a pattern in a rule", I was wondering if there's any command like this in this case:

```
> (show_pattern 1 get_command)
(command)
```

In other words, checking which pattern conditional element is "Pattern 1" referring to.

CLIPS Support

unread,
Mar 19, 2025, 12:15:49 PMMar 19
to CLIPSESG
You have to visually examine the rule to determine the specifics of what each pattern is trying to match. You can display the text of the rule at the command prompt using the ppdefrule command to do this quickly or can browse through your source code for the rule. Generally, the first fact pattern corresponds to pattern 1, the second to pattern 2, and so on. 

CLIPS>
(defrule example
   (not (and (1st)
             (2nd)
             (not (3rd))))
   (4th)
   (not (5th))
   =>)
CLIPS> (assert (1st) (3rd) (5th))
<Fact-3>
CLIPS> (matches example)
Matches for Pattern 1
f-1
Matches for Pattern 2
 None
Matches for Pattern 3
f-2
Matches for Pattern 4
 None
Matches for Pattern 5
f-3
Partial matches for CEs 1 - 2
 None
Partial matches for CEs 1 - 3
 None
Partial matches for CEs 1 (P1 - P3)
*
Partial matches for CEs 1 (P1 - P3) , 2 (P4)
 None
Partial matches for CEs 1 (P1 - P3) , 2 (P4) - 3 (P5)
 None
Activations
 None
(3 1 0)
CLIPS> (facts)
f-1     (1st)
f-2     (3rd)
f-3     (5th)
For a total of 3 facts.
CLIPS> (ppdefrule example)
(defrule MAIN::example
   (not (and (1st)
             (2nd)
             (not (3rd))))
   (4th)
   (not (5th))
   =>)
CLIPS>  

Xuan Wu

unread,
Mar 22, 2025, 3:32:37 PMMar 22
to CLIPSESG
Thanks for the illustration. IMO it might be helpful for new learners to directly see what each pattern is when debugging, without comparing and counting in the rule code.

By looking at the core source code of CLIPS, whichCE seems to be the id of each pattern. I wonder if patternBegin and patternEnd are the begin and end index of the pattern in the source code? If so, we can output the pattern? By adding some WriteInteger in the source code and building a binary to test out, they seem to be all 0 though.
Reply all
Reply to author
Forward
0 new messages