JESS: Re: calculating benefit costs
"Wolfgang Laun" <[email protected]>
| Newsgroups | gmane.comp.java.jess |
|---|---|
| Message-ID | <24919_1294267863_p05MhemZ027541_AANLkTimiO0rTuMohG6b8ECX2pO__stdW9TQiOP_J4KpP@mail.gmail.com> |
With this kind of problem, there are (at least) two approaches. Given that
Employee and Spouse are represented as facts:
(deftemplate Spouse (slot name)(slot age))
(deftemplate Employee (slot name)(slot age)(slot spouse))
(deffacts test-facts
(Employee (name "John Smith")(age 35)(spouse "Jane Smith"))
(Spouse (name "Jane Smith")(age 28))
(Employee (name "Honey Rider")(age 39)(spouse "James Rider"))
(Spouse (name "James Rider")(age 45))
)
(1) You can write one rule for each possible combination of age brackets.
(defrule compute_1_1
(Employee (name ?name){age >= 18 && age <= 29}(spouse ?spouse))
(Spouse {name == ?spouse}{age >= 18 && age <= 29})
=>
(printout t "cost " ?name " " (+ 11.55 6.65) crlf)
)
... more boring code ...
(defrule compute_3_3
(Employee (name ?name){age >= 40 && age <= 49}(spouse ?spouse))
(Spouse {name == ?spouse}{age >= 40 && age <= 49})
=>
(printout t "cost " ?name " " (+ 38.35 20.05) crlf)
)
(2) You can represent the tables for the costs as facts:
(deftemplate CostEmployee (slot lo)(slot hi)(slot cost))
(deftemplate CostSpouse (slot lo)(slot hi)(slot cost))
(deffacts costs
(CostEmployee (lo 18)(hi 29)(cost 11.55))
(CostEmployee (lo 30)(hi 39)(cost 18.95))
(CostEmployee (lo 40)(hi 49)(cost 38.35))
(CostSpouse (lo 18)(hi 29)(cost 6.65))
(CostSpouse (lo 30)(hi 39)(cost 10.35))
(CostSpouse (lo 40)(hi 49)(cost 20.05))
)
and use a single rule:
(defrule compute
(Employee (name ?name)(age ?ageE)(spouse ?spouse))
(Spouse {name == ?spouse}(age ?ageS))
(CostEmployee {lo <= ?ageE}{hi >= ?ageE}(cost ?costE))
(CostSpouse {lo <= ?ageS}{hi >= ?ageS}(cost ?costS))
=>
(printout t "cost " ?name " " (+ ?costE ?costS) crlf)
)
HTH
Wolfgang
On 5 January 2011 22:30, Wolfgang Laun <[email protected]> wrote:
> Sent on behalf of Derek Adams:
>
>
> 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
>