defrules in CLIPS

194 views
Skip to first unread message

Razan Abu-Khashabeh

unread,
Jan 12, 2023, 3:28:11 PMJan 12
to CLIPSESG
Hello,
I have problem in CLIPS rules: 

(defrule case-1
         (patient
            (age ?age)
        (MRN ?MRN)
        (first-name ?first-name)
        (surname ?surname)
        (gender ?gender))
    (physical-feature
            (HPO-ID ?HPO-ID)
        (physical-feature-description ?physical-feature-description))
    (mode-of-inheritance
            (HPO-ID HP:0000006)
        (mode-of-inheritance-description Autosomal-dominant-inheritance))
    (age-of-onset-classifier
            (childhood ?childhood))
=>
(assert (IBMFS-DB (cps-db))))

The error is: 
TMPLTDEF2] The single field slot 'cps-db' can only contain a single field value.

ERROR:
(defrule MAIN::case-1
   (patient (age ?age) (MRN ?MRN) (first-name ?first-name) (surname ?surname) (gender ?gender))
   (physical-feature (HPO-ID ?HPO-ID) (physical-feature-description ?physical-feature-description))
   (mode-of-inheritance (HPO-ID HP:0000006) (mode-of-inheritance-description Autosomal-dominant-inheritance))
   (age-of-onset-classifier (childhood ?childhood))
   =>
   (assert (IBMFS-DB (cps-db )


Any help is appreciated


Regards,
Razan

CLIPS Support

unread,
Jan 12, 2023, 6:24:38 PMJan 12
to CLIPSESG
If you defined the cps-db slot in the corresponding IBMFS-DB deftemplate as (slot cps-db), then it's a single field slot and you have to provide a value if the slot is specified in an assert command. In the actions of your rules, the command (assert (IBMFS-DB (cps-db)))) specifies the cps-db slot, but does not follow the slot name with a value. If you want the slot to be able to contain any number of values, including none, you must define it as (multislot cps-db).

Razan Abu-Khashabeh

unread,
Jan 14, 2023, 5:10:55 AMJan 14
to CLIPSESG
Thank you 

The deftemplate was (deftemplate IBMFS-DB (slot cps-db))

deffact: (deffacts IBMFS-DB (cps-db DBA))

and the defrule: (defrule case-1

         (patient
            (age ?age)
        (MRN ?MRN)
        (first-name ?first-name)
        (surname ?surname)
        (gender ?gender))
    (physical-feature
            (HPO-ID ?HPO-ID)
        (physical-feature-description ?physical-feature-description))
    (mode-of-inheritance
            (HPO-ID HP:0000006)
        (mode-of-inheritance-description Autosomal-dominant-inheritance))
    (age-of-onset-classifier
            (childhood ?childhood))
=>
(assert (IBMFS-DB (cps-db DBA))))


In defining the facts, should I write it as an assert command?

I tried it, but I face an error with the defrule syntax "[PRNTUTIL2] Syntax Error:  Check appropriate syntax for defrule.

ERROR:
(defrule MAIN::case-1
   (patient (age ?age) (MRN ?MRN) (first-name ?first-name) (surname ?surname) (gender ?gender))
   (physical-feature "


Any idea about this?


Regards,
Razan

CLIPS Support

unread,
Jan 14, 2023, 1:02:45 PMJan 14
to CLIPSESG
One possible explanation is that you didn't define a physical-feature deftemplate:

         CLIPS (6.4 2/9/21)
CLIPS> (defrule case-1

   (physical-feature
      (HPO-ID ?HPO-ID)
      (physical-feature-description ?physical-feature-description))
    =>)


[PRNTUTIL2] Syntax Error:  Check appropriate syntax for defrule.

ERROR:
(defrule MAIN::case-1
   (physical-feature (
CLIPS> 


You've only provided code fragments in your questions, so I can only guess as to what you did wrong in your complete set of code.

Razan Abu-Khashabeh

unread,
Jan 14, 2023, 1:20:01 PMJan 14
to CLIPSESG
Please see attached, the deftemplates, deffacts and deffrules.
Can you please check them and let me know.

Thanks,

Razan
deftemplates.pdf
defrules.pdf
Facts.pdf

CLIPS Support

unread,
Jan 14, 2023, 5:10:02 PMJan 14
to CLIPSESG
If all of the deftemplates are defined before the rule, the rule loads without error.

CLIPS>
(deftemplate patient
   (slot age)
   (slot MRN)
   (slot date-of-birth)
   (slot first-name)
   (slot surname)
   (slot gender))
CLIPS>
(deftemplate physical-feature
   (slot phenotypic-abnormality)
   (slot HPO-ID)
   (slot clinical-investigation-ID)
   (slot physical-feature-description))
CLIPS>
(deftemplate mode-of-inheritance
   (slot pattern-of-inheritance)
   (slot HPO-ID)
   (slot mode-of-inheritance-description))
CLIPS>
(deftemplate age-of-onset-classifier
   (slot infantile)
   (slot childhood)
   (slot juvenile)
   (slot adult))
CLIPS>
(deftemplate IBMFS-DB (slot cps-db))
CLIPS>
(defrule case-1
   (patient
       (age ?age)
       (MRN ?MRN)
       (first-name ?first-name)
       (surname ?surname)
       (gender ?gender))
   (physical-feature
       (HPO-ID ?HPO-ID)
       (physical-feature-description ?physical-feature-description))
   (mode-of-inheritance
      (HPO-ID HP:0000006)
      (mode-of-inheritance-description Autosomal-dominant-inheritance))
   (age-of-onset-classifier
      (childhood ?childhood))
   =>
   (assert (IBMFS-DB (cps-db DBA))))
CLIPS> 
Message has been deleted

Razan Abu-Khashabeh

unread,
Jan 15, 2023, 5:07:58 AMJan 15
to CLIPSESG
I tried it as you mentioned. It succeeded for the first rule. However, for the others, it gave the following error: [TMPLTDEF1] Invalid slot 'HPO-ID-is' not defined in corresponding deftemplate 'laboratoryscreening-and-hematologicalfinding' for defrule case-2 & case-4. [TMPLTDEF1] Invalid slot 'HPO-I-is' not defined in corresponding deftemplate 'physical-feature' for defrule case-5.

CLIPS Support

unread,
Jan 15, 2023, 5:58:29 PMJan 15
to CLIPSESG
Every single time you reference a slot in a deftemplate pattern, it has to be defined in the corresponding deftemplate or you'll get an error message. If you searched for HPO-ID-is in your source files, you'd see that it's referenced by patterns but it's never defined in a deftemplate. The error message is literally telling you the place in your code to look. In your rules, you're matching the slot HPO-ID-is, but your deftemplate defines the slot as HPO-ID.

Razan Abu-Khashabeh

unread,
Jan 18, 2023, 5:35:24 PMJan 18
to CLIPSESG
I correct it, but in your opinion, what is the error in the deftemplate patterns in this defrule:
(defrule case-2

          (patient
            (age ?age)
        (MRN ?MRN)
        (first-name ?first-name)
        (surname ?surname)
        (gender ?gender))
           (physical-feature
            (HPO-ID ?HPO-ID)
        (physical-feature-description ?physical-feature-description))
    (laboratoryscreening-and-hematologicalfinding
            (HPO-ID HP:0001903
                HP:0012410
            HP:0001896
            HP:0001972))
        (age-of-onset-classifier
            (infantile ?infantile))

    (mode-of-inheritance
                  (HPO-ID HP:0000006)
              (mode-of-inheritance-description Autosomal-dominant-inheritance))
=>
(assert(IBMFS-DB (cps-db DBA))))

[PRNTUTIL2] Syntax Error:  Check appropriate syntax for deftemplate patterns.

Thank you for your help. I am not from IT Background. Therefore, I am facing such problems. 

Is there a possibility for a ZOOM meeting in which we can communicate faster?


Regards,

CLIPS Support

unread,
Jan 18, 2023, 7:42:15 PMJan 18
to CLIPSESG
Post your entire program. Do not post it as a pdf or image file that cannot be directly loaded into CLIPS. Post the actual ASCII text files that you are loading.

CLIPS Support

unread,
Jan 19, 2023, 1:20:03 PMJan 19
to CLIPSESG
If you look at the error message, it shows you the point at which the syntax error occurs. This is similar to the first error you posted, only in this case instead of treating a single field slot as if it could contain no values, you're treating it as if it could contain two.

Defining defrule: case-2
[PRNTUTIL2] Syntax Error:  Check appropriate syntax for deftemplate patterns.

ERROR:
(defrule MAIN::case-2

   (patient (age ?age) (MRN ?MRN) (first-name ?first-name) (surname ?surname) (gender ?gender))
   (physical-feature (HPO-ID ?HPO-ID) (physical-feature-description ?physical-feature-description))
   (laboratoryscreening-and-hematologicalfinding (HPO-ID HP:0001903 HP:0012410
On Wednesday, January 18, 2023 at 4:35:24 PM UTC-6 rabukh...@gmail.com wrote:

Razan Abu-Khashabeh

unread,
Jan 20, 2023, 12:14:35 PMJan 20
to CLIPSESG
Please see attached.

Regards,
Razan

CLIPS

Razan Abu-Khashabeh

unread,
Jan 20, 2023, 2:59:01 PMJan 20
to CLIPSESG
Any suggestion for the deftemplate patterns that should be?

Razan Abu-Khashabeh

unread,
Jan 20, 2023, 5:02:19 PMJan 20
to CLIPSESG
What is the correct way to write the deftemplate pattern "(laboratoryscreening-and-hematologicalfinding
            (HPO-ID HP:0001903)

                HP:0012410
            HP:0001896
            HP:0001972))"

in 
(defrule case-2
          (patient
            (age ?age)
        (MRN ?MRN)
        (first-name ?first-name)
        (surname ?surname)
        (gender ?gender))
           (physical-feature
            (HPO-ID ?HPO-ID)
        (physical-feature-description ?physical-feature-description))
    (laboratoryscreening-and-hematologicalfinding
            (HPO-ID HP:0001903)

                HP:0012410
            HP:0001896
            HP:0001972))
        (mode-of-inheritance
                  (HPO-ID HP:0000006)
              (mode-of-inheritance-description Autosomal-dominant-inheritance))
        (age-of-onset-classifier
            (infantile ?infantile))
=>
(assert(IBMFS-DB (cps-db DBA))))


?

CLIPS Support

unread,
Jan 20, 2023, 7:42:24 PMJan 20
to CLIPSESG
Fixes for typos and cardinality issues with slot definitions are attached. The remaining errors, referencing variables that have never been assigned a value, are semantic.
program.clp

Razan Abu-Khashabeh

unread,
Jan 23, 2023, 5:01:59 PMJan 23
to CLIPSESG
Thanks, I added more rules and all the errors have been overcome. Attached, while running the program. In order to make sure that all the rules are going to be fired, all the deftemplate patterns should have facts related to, am I right?
Screen Shot 2023-01-24 at 12.58.00 AM.png

CLIPS Support

unread,
Jan 23, 2023, 5:56:28 PMJan 23
to CLIPSESG
Yes, you would need to test each rule with a facts matching the patterns in the rules.

Razan Abu-Khashabeh

unread,
Jan 25, 2023, 7:07:03 PMJan 25
to CLIPSESG
May I have your opinion about my work?

Please see attached.
CLIPS Program.clp

CLIPS Support

unread,
Jan 27, 2023, 4:12:22 PMJan 27
to CLIPSESG
Your program executes 3780 rules, but does nothing as all of the facts asserted in the actions of your rules are already present because they are included in deffacts.
Message has been deleted

Razan Abu-Khashabeh

unread,
Jan 27, 2023, 5:50:56 PMJan 27
to CLIPSESG
I removed them from the deffacts. However, I got the same result. See attached.
CLIPS Program.clp

Ronald Finkbine

unread,
Jan 27, 2023, 8:18:57 PMJan 27
to CLIPSESG
I have built a large Clips expert system to search graphs, recognizing software algorithms that have come from compilers, for my doctoral dissertation.

My project worked and worked well, So I feel qualified to issue an opinion about this overall project. 

Correction, my Version 2 worked well, Version 1 was built crappy and I finally realized it and took 6 days to start over and build in a designed way.

The author states he is not an IT guy. Well, you NEED one very much.

I had to build my expert system in very small pieces, the foundation pieces, then grouping sub-searches into larger ones.

I had 110 different test cases to verify small pieces of my matches.

When I saw something in this thread that said 3780 rules in system. I spit out my coffee.

You ask "What is wrong?"

Heck, can you show me one thing that is RIGHT? Verified CORRECT? Paper and pencil checkmarks and list of facts before/after a run of a few rules?

Incrementalism. Test-driven development.

And when you edit the code even one little character, you have invalidated ALL of the previous test cases executions. Regression testing is necessary.

You need to be able to run ALL the test cases in a batch environment from one command.

My 110 test cases took 45 minutes to run, I used to jog out to the railroad tracks and come back later during a regression run.

Save all your code, create a new EMPTY project, add back SMALL pieces with some test cases that you can see are accurate on like 2 sheets of paper. Not 3780.

Gary Riley, thanks for all your efforts over the years. It has been much appreciated.

Ronald Finkbine
ronaldf...@hotmail.com


From: clip...@googlegroups.com <clip...@googlegroups.com> on behalf of Razan Abu-Khashabeh <rabukh...@gmail.com>
Sent: Friday, January 27, 2023 5:50 PM
To: CLIPSESG <clip...@googlegroups.com>
Subject: Re: defrules in CLIPS
 
--
You received this message because you are subscribed to the Google Groups "CLIPSESG" group.
To post to this group, send email to CLIP...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CLIPSESG?hl=en
 
--> IF YOU NO LONGER WANT TO RECEIVE EMAIL <--
Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
Click on "Edit my membership" link.
Select the "No Email" radio button.
Click the "Save these settings" button.

--> IF YOU WANT TO UNSUBSCRIBE <--
Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
Sign in
Click on "Edit my membership" link.
Click the "Unsubscribe" button.
Note: This appears to be the most reliable way to unsubscribe
 
Alternately, send email to CLIPSESG-u...@googlegroups.com. You will receive an email which you must respond to as well to unsubscribe. Clicking the link mentioned in the unsubscribe reply does not appear to work reliably.
---
You received this message because you are subscribed to the Google Groups "CLIPSESG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clipsesg+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clipsesg/a127354e-329f-4e30-9953-15dab47b20c0n%40googlegroups.com.

CLIPS Support

unread,
Jan 28, 2023, 2:02:47 PMJan 28
to CLIPSESG
You get a few facts asserted by your rules. The problem with your program appears to be that the facts and results need to be patient specific, but they're not, so you're getting thousands of invalid activations for your rules. 

         CLIPS (6.4 2/9/21)
CLIPS> (load "CLIPS Program.clp")
%%%%%%%%%%%$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$********************
TRUE
CLIPS> (reset)
CLIPS> (watch statistics)
CLIPS> (watch facts)
CLIPS> (run)
==> f-47    (IBMFS-DB (cps-db DBA))
==> f-48    (IBMFS-FA (cps-fa FA))
==> f-49    (IBMFS-SDS (cps-sds SDS))
3780 rules fired        Run time is 0.00253701210021973 seconds.
1489941.65210037 rules per second.
49 mean number of facts (49 maximum).
0 mean number of instances (0 maximum).
1890 mean number of activations (3780 maximum).
CLIPS> 

Razan Abu-Khashabeh

unread,
Feb 1, 2023, 5:31:48 PMFeb 1
to CLIPSESG
Thank you for your reply.

Can you please give me an example how the results and facts being as you mentioned?

CLIPS Support

unread,
Feb 1, 2023, 7:07:43 PMFeb 1
to CLIPSESG
It's your program; you should be telling us what it's supposed to be doing. You shouldn't start coding until you have a few examples of what the program should be doing and why. That includes examples of input and expected output and the explanation for how the output is derived. If you can't explain it to another person, then you're going to have problems coding it regardless of what programming language you're using. And if you're unfamiliar with a programming language, don't try to write the entire program in one fell swoop. Write one small part of it first and make sure that part works before adding more code. 

Ronald Finkbine

unread,
Feb 1, 2023, 7:52:29 PMFeb 1
to CLIPSESG
What is your most simple rule?

Any rule with just one template as a CE?

This would be the most simple TC001 (Test case 001).

Get one small example together that will fire and print "THIS RULE WORKS".

Then repeat. Add one more template and another rule and make that TC002.

Repeat.

And again.

You cannot just put all of this together and expect it to work at first.

It has to be engineered.

People who are good at this are called Knowledge Engineers.

Ronald Finkbine
ronaldf...@hotmail.com

From: clip...@googlegroups.com <clip...@googlegroups.com> on behalf of Razan Abu-Khashabeh <rabukh...@gmail.com>
Sent: Wednesday, February 1, 2023 5:31 PM

To: CLIPSESG <clip...@googlegroups.com>
Subject: Re: defrules in CLIPS
--
You received this message because you are subscribed to the Google Groups "CLIPSESG" group.
To post to this group, send email to CLIP...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CLIPSESG?hl=en
 
--> IF YOU NO LONGER WANT TO RECEIVE EMAIL <--
Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
Click on "Edit my membership" link.
Select the "No Email" radio button.
Click the "Save these settings" button.

--> IF YOU WANT TO UNSUBSCRIBE <--
Visit this group at http://groups.google.com/group/CLIPSESG?hl=en
Sign in
Click on "Edit my membership" link.
Click the "Unsubscribe" button.
Note: This appears to be the most reliable way to unsubscribe
 
Alternately, send email to CLIPSESG-u...@googlegroups.com. You will receive an email which you must respond to as well to unsubscribe. Clicking the link mentioned in the unsubscribe reply does not appear to work reliably.
---
You received this message because you are subscribed to the Google Groups "CLIPSESG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clipsesg+u...@googlegroups.com.

Razan Abu-Khashabeh

unread,
Feb 23, 2023, 4:49:48 PMFeb 23
to CLIPSESG
What do you mean by patient specific?
This program is about diagnosing four disorders and the rules are about the approach(es) for diagnosing them. The facts the case when a patient has certain abnormalies associated with each of these disorders.


Regards,

CLIPS Support

unread,
Feb 23, 2023, 8:49:15 PMFeb 23
to CLIPSESG
Your program has over 3000 rule firings that mostly do nothing. The only way that seems plausible is if you've got data for thousands of patients mixed together. Your programming methodology makes it practically impossible to try to figure out what you're doing. I can tell that your program is doing something medical, but that's it.

Razan Abu-Khashabeh

unread,
Feb 24, 2023, 2:50:33 AMFeb 24
to CLIPSESG
My Program contains only the constructs. I have not loaded the patients data yet.
I have to load the patients data then check the activations. right?

CLIPS Support

unread,
Feb 24, 2023, 2:28:29 PMFeb 24
to CLIPSESG
Here's your 1200+ line program where I've cut out 95% of your code to illustrate the benefit of the advice you've been ignoring:

         CLIPS (6.4 2/9/21)

CLIPS>
(deftemplate patient
   (slot age)
   (slot MRN)
   (slot date-of-birth)
   (slot first-name)
   (slot surname)
   (slot gender))
CLIPS>
(deftemplate mode-of-inheritance
   (slot pattern-of-inheritance)
   (multislot HPO-ID)
   (multislot mode-of-inheritance-description))

CLIPS>
(deftemplate age-of-onset-classifier
   (slot infantile)
   (slot childhood)
   (slot juvenile)
   (slot adult))
CLIPS>
(deftemplate IBMFS-SDS
   (slot cps-sds))
CLIPS>
(deffacts CPS-4
   (patient
     (age 0)
     (MRN 236748)
     (first-name Rani)
     (surname Khateeb)
     (date-of-birth 09/10/2022)
     (gender male))
   (age-of-onset-classifier
      (infantile Infantile-onset))
   (mode-of-inheritance
      (HPO-ID HP:0001419)
      (mode-of-inheritance-description X-linked-recessive-inheritance)))
CLIPS>
(deffacts CPS-5
   (patient
      (age 9)
      (MRN 204561)
      (first-name Rani)
      (surname Khateeb)
      (date-of-birth 4/10/2014)
      (gender male))
   (age-of-onset-classifier
      (childhood Childhood-onset))
   (mode-of-inheritance
      (HPO-ID HP:0000007)
      (mode-of-inheritance-description Autosomal-recessive-inheritance)))
CLIPS>
(defrule case-7

   (patient
      (age ?age)
      (MRN ?MRN)
      (first-name ?first-name)
      (surname ?surname)
      (gender ?gender))
   (age-of-onset-classifier
      (childhood ?childhood))
   (mode-of-inheritance
      (HPO-ID HP:0000007)
      (mode-of-inheritance-description Autosomal-recessive-inheritance))
   =>
   (assert (IBMFS-SDS (cps-sds SDS))))
CLIPS> (reset)
CLIPS> (facts)
f-1     (patient (age 0) (MRN 236748) (date-of-birth 09/10/2022) (first-name Rani) (surname Khateeb) (gender male))
f-2     (age-of-onset-classifier (infantile Infantile-onset) (childhood nil) (juvenile nil) (adult nil))
f-3     (mode-of-inheritance (pattern-of-inheritance nil) (HPO-ID HP:0001419) (mode-of-inheritance-description X-linked-recessive-inheritance))
f-4     (patient (age 9) (MRN 204561) (date-of-birth 4/10/2014) (first-name Rani) (surname Khateeb) (gender male))
f-5     (age-of-onset-classifier (infantile nil) (childhood Childhood-onset) (juvenile nil) (adult nil))
f-6     (mode-of-inheritance (pattern-of-inheritance nil) (HPO-ID HP:0000007) (mode-of-inheritance-description Autosomal-recessive-inheritance))
For a total of 6 facts.

CLIPS> (agenda)
0      case-7: f-1,f-2,f-6
0      case-7: f-4,f-2,f-6
0      case-7: f-4,f-5,f-6
0      case-7: f-1,f-5,f-6
For a total of 4 activations.
CLIPS> 


You haven't provided any information about what you're expecting your program to do and why, again ignoring the advice you've been given, but my guess would be that you're expecting a single activation of case-7 for the patient with MRN 204561 because you're assuming that putting a group of facts in the same deffacts somehow forces the rule to keep those facts together when pattern-matching occurs, but that's clearly not what's happening when you look at the activations for the rule. The mode-of-inheritance with Autosomal-recessive-inheritance is being matched for both patients as well as the age-of-onset-classifier. If that's not what you want to happen and the MRN is unique for each patient, then what you need to do is add an MRN slot to the age-of-onset-classifier and mode-of-inheritance so you have a means of associating the right information with the right patient.
Message has been deleted
Message has been deleted

Razan Abu-Khashabeh

unread,
Feb 25, 2023, 1:20:00 PMFeb 25