I think Moore, David [IES] wrote:
> Folks,
>
> I'm just starting out on Jess and have set myself the initial task of
> writing a small program concerning granting loans. The system will ask the
> user various questions to see if they qualify for a loan, and if they do
> will calculate how much they will be able to borrow. The first part (whether
> they qualify) is fine, but I can't figure out how to get Jess to read in a
> number, do some maths on it and return the value.
Before I answer your question, let me point out that rule-based
programming is somewhat different than procedural programming --
that's the whole point, of course. To the extent that you can write a
program as one if-then statement, or a simple series of them, then
using a rule engine to solve it is ovekill. Now, a full-blown loan
application processor is a actually a good rule engine application,
because the "qualify" part is actually complicated and takes all sorts
of factors into consideration. The rest of this is just plumbing, and
so it's OK to write these peripheral parts of the program in the
procedural style I'm about to show you.
>
> In Pascal I would do something like this:
>
> procedure loan_size
> BEGIN
> if qualifies(person) {if they qualify for a loan - I can establish this in
> Jess)
> then
> writeln('what is your annual salary?');
> readln(salary);
> calc_amount(salary);
> END;
>
Well, qualifies(person) is complicated. Let's say that the
qualification is done by a series of other rules, not shown here, and
the result of that process was to assert the fact
(qualified "Fred Smith") into working memory. Then a direct
translation of the above looks like
(defrule loan-size
(qualified ?person)
=>
(printout t "What is your annual salary?" crlf)
(bind ?result (read))
(calc-amount ?result))
This is very similar to many rules in, for example,
JessXX/examples/sticks.clp (see player-select, pile-select,
get-human-move, etc.
> I would then need a function or procedure called calc_amount:
>
> procedure calc_amount(annual_salary)
> BEGIN
> loan_size := annual_salary * 3; {the bank will lend 3 times
> salary};
> writeln('you can borrow ', loan_size);
> END;
You can write calc-amount as a deffunction like this:
(deffunction calc-amount (?annual-salary)
(bind ?loan-size (* ?annual-salary 3))
(printout t "You can borrow " ?loan-size crlf))
Many of the little example programs include similar deffunctions --
see examples/browse.clp, draw.clp, frame.clp, fullmab.clp, stack.clp,
etc.
>
> Also, any suggestions about a relevant book or teaching materials would be
> excellent.
>
Well, of course the Jess manual is a good place to start -- it
includes references for a few books. And my own "Jess in Action" will
be published this Spring by Manning Press.
---------------------------------------------------------
Ernest Friedman-Hill
Distributed Systems Research Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
PO Box 969, MS 9012 [EMAIL PROTECTED]
Livermore, CA 94550 http://herzberg.ca.sandia.gov
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------