Derek,


The year is young, but this may be a contender for Most Open Ended Question
of 2011. As a first step, you'll need to define your Jess data structures.
Assuming that you'll be pulling employee data from an RDBMS, you'll probably
want to start with the relevant schema from there. Then you'll need to
decide how to flow data between Jess and the schema. The online Jess manual
has an extensive discussion of the alternatives available. For example, Jess
supports shadow facts, which can provide a bridge between Java objects and
Jess facts.



With the caveat that I haven't run or debugged this code, here's a simple
example, using Jess templates (and ignoring where the data comes from):



(deftemplate employee

  (slot uid)                 ;unique identifier, possibly SSN (or not
depending on applicable HIPAA regulations)

  (slot last-name)

  (slot first-name)

  (slot DOB)              ;date of birth

  (slot spouse-uid))



(deftemplate spouse

  (slot uid)                 ;unique identifier, possibly SSN (or not
depending on applicable HIPAA regulations)

  (slot last-name)

  (slot first-name)

  (slot DOB)              ;date of birth

  (slot spouse-uid)) ;this is the identifier of the employee married to the
spouse



(defrule benefit-cost-calculation

    (employee (UID ?emp-uid) (DOB ?emp-DOB) (spouse-UID ?spouse-UID))

    (spouse (UID ?spouse-UID) (DOB ?spouse-DOB))

    =>

    ;; Here you write the logic for computing the benefits cost

   ;; Refer to http://www.jessrules.com/doc/70/index.html

   (bind ?benefits-cost (+ (emp-benefit-cost ?emp-DOB) (spouse-benefit-cost
?spouse-DOB))

   (assert (benefits-cost ?emp-uid ?benefits-cost)))





;; This is one approach, illustrating the use of a Jess deffunction

;; However, you might want to make the benefits computation more
declarative, rather than embedding the table in a function

;; that you would have to edit each time the costs change, so another
approach  would be to assert each person's age as a Jess fact

;; and use that as the trigger for a set of rules that assert the
appropriate benefits  cost.



(deffunction emp-benefit-cost (?DOB)

   (bind ?age (calc-age ?DOB))

   (if (< ?age 29) then

           (return 11.55)

    elif (< 29 ?age 39) then

              ;;etc.

           ))





(defrule benefit-age-calculation

    (employee (UID ?emp-uid) (DOB ?emp-DOB) (spouse-UID ?spouse-UID))

    (spouse (UID ?spouse-UID) (DOB ?spouse-DOB))

    =>

   (bind ?emp-age (calc-age ?emp-DOB))

   (bind ?spouse-age (calc-age ?spouse-DOB))

  (bind ?today (get-current-date)) ;;so that you don't trigger on old
employee-age statements left in Jess memory

   (assert (employee-age-as-of (today ?today) (age ?emp-age) (UID ?emp-UID))

  (assert (spouse-age-as-of (today ?today) (age ?spouse-age)  (UID
?spouse-UID)))



(defrule benefit-cost-calculation

     (today ?date)

     (employee-age-as-of (today ?date) {age < 29} (UID ?emp-UID))

     =>

     (employee-benefits-cost (UID ?emp-UID) 11.55))



I leave it as an exercise for the reader to write the rest of the rules of
older employees, the equivalent rule set for the spouse, the rule that would
combine the two costs to provide a total cost, and the logic to handle
single employees.



Finally, to completely separate the benefits data, which is likely to change
from year to year, from the code, which should not, you might consider the
use of defglobal variables:



(defglobal *emp-benefit-cost-18-to-29* 11.55)



(defrule benefit-cost-calculation

     (today ?date)

     (employee-age-as-of (today ?date) {age < 29} (UID ?emp-UID))

     =>

     (employee-benefits-cost (UID ?emp-UID) *emp-benefit-cost-18-to-29*))



This suggests yet another exercise for the reader, in that the age range
bands may also be subject to change, so you might want to abstract those as
well, rather than leaving that constant 29 in the body of the rule.







Hopefully this will be enough to get you started. The Jess documentation on
the web is comprehensive, so I suggest that you start with a toy example
like this and gradually increase the complexity, referring to the
documentation as necessary, until you have what you need. For example, check
out Chapter 9, Jess Application Design, to understand the different ways you
can use Jess. Also, you'll need at least some knowledge of LISP,
particularly for writing functions. Refer to
http://www.lisp.org/alu/res-lisp as a starting point.



-John





From: Derek Adams [mailto:dad...@arahant.com]
Sent: Wednesday, January 05, 2011 12:50 PM
To: jess-users@sandia.gov
Subject: JESS: Jess Rule Question - need help!



Hey Jess Users!  This is my first post, so I apologize for anything "noob"
that I say/do...

I'm very new to Jess.  I understand the basics but applying my knowledge for
the first time is proving more difficult than I anticipated.  Here is the
problem:

I need to set up a rule in Jess that calculates the cost of a benefit based
on age and enrollees.

Example:
The Benefit Configuration is "Employee $10k / Spouse $5k"
The enrollees are the employee (John Smith age 30) and spouse (Jane Smith
and 28).
The costs are calculated as follows:

Age               Employee            Spouse
18-29            11.55                    6.65
30-39            18.95                   10.35
40-49            38.35                   20.05

So John's cost is 18.95 and Jane's cost is 6.65 for a total of 25.60.

Any advice would be greatly appreciated!

Thanks,

Derek Adams

Reply via email to