new to Clips and dunno how to handle this elementary project

106 views Asked by At

I'm stuck here with this Clips project. I really have no idea how to fix my problems. Any help? this is the project

and here is my code:

     CLIPS> (assert (saving 30000))
<Fact-1>
CLIPS> (assert (income 50000))
<Fact-2>
CLIPS> (assert (job steady))
<Fact-3>
CLIPS> (assert (expenses 10000))
<Fact-4>
CLIPS> (defglobal ?*s* = 30000)
CLIPS> (defglobal ?*i* = 50000)
CLIPS> (defglobal ?*e* = 10000)
CLIPS> (defglobal ?*x* = 0.5)
CLIPS> (defrule rule1
   (test (> ?*s* (* ?*x* ?*i*)))
   =>
   (assert (savingst good)))
CLIPS> (defrule rule2
(job steady) 
(test(> ?*i* ?*e*))
=> (assert (incomest good))
)
CLIPS> (defrule rule3
(and (savingst good)(incomest good))
=>
(printout t "Advise is invest money in stocks" crlf)
(assert (investment ok))
)
CLIPS> (run)
Advise is invest money in stocks
CLIPS> (bsave "C:/Users/Home/Desktop/pro")
TRUE
CLIPS> (save file.clp)

first of all I dunno how should I export a *.clp file. but I did it as shown above. when I load this file and then run it , it only runs rule1. is there anyone who can help me?

1

There are 1 answers

2
Gary Riley On

If you open file.clp, you'll see that the assert statements are not present. The save function only saves constructs (such as defrule, defglobal, deftemplate, ...), not functions/commands. Use the deffacts construct to indicate facts which should be asserted each time a (reset) command is used:

(deffacts initial
   (saving 30000)
   (income 50000)
   (job steady)
   (expenses 10000))

You don't have to enter your constructs at the CLIPS command prompt and then save them. Just use a text editor (either a separate plain text editor or the one included with the CLIPS Windows and Mac OS IDEs). That makes it easier to create/copy/edit your constructs. You can then use the command prompt primarily for execution/debugging commands.

Here's a better approach for your problem:

CLIPS> 
(deftemplate client
   (slot name)
   (slot income (default 0))
   (slot savings (default 0))
   (slot job (allowed-values steady part-time unemployed unknown))
   (slot expenses (default 0)))
CLIPS> 
(deftemplate analysis
   (slot client)
   (slot attribute)
   (slot value))
CLIPS>          
(deffacts clients
   (client (name "John Doe")
           (income 50000)
           (savings 30000)
           (job steady)))
CLIPS>            
(defrule advise-stock-investment
   (analysis (client ?client)
             (attribute income)
             (value good))
   (analysis (client ?client)
             (attribute savings)
             (value good))
   =>
   (assert (analysis (client ?client)
                     (attribute advice)
                     (value "invest money in stocks"))))   
CLIPS> 
(defrule good-saver
   (client (name ?client)
           (savings ?savings)
           (income ?income))
   (test (> ?savings (* 0.5 ?income)))
   =>
   (assert (analysis (client ?client)
                     (attribute savings)
                     (value good))))
CLIPS>            
(defrule good-income
   (client (name ?client)
           (job ?status)
           (income ?income)
           (expenses ?expenses))
   (test (or (eq ?status steady)
             (> ?income ?expenses)))
   =>
   (assert (analysis (client ?client)
                     (attribute income)
                     (value good))))
CLIPS>                      
(defrule print-advice
   (analysis (client ?client)
             (attribute advice)
             (value ?text))
   =>
   (printout t ?client " should " ?text "." crlf))
CLIPS> (watch rules)
CLIPS> (reset)
CLIPS> (run)
FIRE    1 good-saver: f-1
FIRE    2 good-income: f-1
FIRE    3 advise-stock-investment: f-3,f-2
FIRE    4 print-advice: f-4
John Doe should invest money in stocks.
CLIPS>