aspose file tools*
The moose likes Other Java Products and Servers and the fly likes Jess Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "Jess Query" Watch "Jess Query" New topic
Author

Jess Query

Jonathan Goode
Greenhorn

Joined: Dec 01, 2005
Posts: 12
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Jonathan Goode
Greenhorn

Joined: Dec 01, 2005
Posts: 12
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

Joined: Dec 01, 2005
Posts: 12
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
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Dec 01, 2005
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jess Query