• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jess Query

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically I wondered if someone could help me with a problem I have as I'm new to Jess and unsure of what to do - as a University assessment I must implement a set of rules (listed on this site http://www.patient.co.uk/showdoc/23069022) using Jess. So far I'm doing Ok and have written my rules but when it comes to creating a function which asks a question such as "How old are you?" and then getting Jess to compare the integer entered with the criteria specified in my rule(s) I'm stumped. I've tried using (assert (age (read))) where age is specified in a template then linking to the rule with phase but to no avail. Here is my first rule:
(defrule eligibility-rule1
(Patient (rule ?rule) (age ?age& < ?age 16)))
=>
(printout t "You are eligible for free prescriptions. (Rule: " ?rule ")" crlf) )

Here is the beginning of my first function:
(deffunction under-16 ()
(printout t "How old are you?")
(assert (age (read))))

Hopefully I'm making sense. Thanks in advance.

[ December 01, 2005: Message edited by: Jonathan Goode ]

[ December 01, 2005: Message edited by: Jonathan Goode ]
[ December 01, 2005: Message edited by: Jonathan Goode ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The only thing wrong with what you've shown is that you're asserting a fact

(age N) ;; N is a number

but you actually want the age slot of your "Patient" fact to contain N.

(Patient (age N))

Therefore, you want to either use the "modify" function to modify the existing Patient fact, or you want to ask this question before the Patient fact is created, so it can start out with the right age value. Make sense?

I don't know if you're using "Jess in Action" as a text for you course (see my signature) but if you are, then chapters 10 and 11 present an application with fairly flexible and capable question-asking machinery.
[ December 01, 2005: Message edited by: Ernest Friedman-Hill ]
 
Jonathan Goode
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code so perhaps you can see my mistake more easily or at least what I'm doing wrong:

(deftemplate Patient
"Patient Information"
(slot rule
(type STRING)
(default "-"))
(slot age
(type INTEGER)
(default 30))
(slot location
(type STRING)
(default "-"))
(slot full-time_education
(type ATOM)
(default No))
)


(deffunction under-16 ()
(printout t "How old are you?")
(assert (Patient (age (read)))))


(defrule eligibility-rule1
(Patient (rule ?rule) (age ?age& < ?age 16)))
=>
(printout t "You are eligible for free prescriptions. (Rule: " ?rule ")" crlf) )

(defrule eligibility-rule2
(Patient (rule ?rule) (age ?age& < ?age 25)) (location "Wales"))
=>
(printout t "You are eligible for free prescriptions. (Rule: " ?rule ")" crlf) )

(defrule eligibility-rule3
(Patient (rule ?rule) (age ?age& < ?age 19)) (full-time_education Yes))
=>
(printout t "You are eligible for free prescriptions. (Rule: " ?rule ")" crlf) )

(defrule eligibility-rule4
(Patient (rule ?rule) (age ?age& > ?age 59)))
=>
(printout t "You are eligible for free prescriptions. (Rule: " ?rule ")" crlf) )


Is this what you intended? When I run it it just returns 0 no matter what age value I enter! Your help is much appreciated and unfortunately I don't have the text.
[ December 01, 2005: Message edited by: Jonathan Goode ]
 
Jonathan Goode
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought of doing this.

(deffunction under-16 ()
(printout t "How old are you?")
(assert (Patient(age (read)))))

then to modify at a later stage:

(modify 1(Patient(age (read))))
(modify 1(Patient(location (read))))

etc...
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jonathan Goode:

Is this what you intended? When I run it it just returns 0 no matter what age value I enter! Your help is much appreciated and unfortunately I don't have the text.



You define that "under-16" function, but you don't call it. Most of the rules won't match regardless of the age because they require a location; eligibility-rule4 will fire for any age over 59, though; so after loading the above program, do the following

Jess> (under-16)
How old are you? 60
<Fact-1>
Jess> (run)
You are eligible for free prescriptions. (Rule: -)
1

[ December 01, 2005: Message edited by: Ernest Friedman-Hill ]
 
Jonathan Goode
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for your help. I've managed to get it all working - just in time for my hand in date of tomorrow! Topic can be closed.
reply
    Bookmark Topic Watch Topic
  • New Topic