I'm sorry Lai. I'm a Java programmer at heart so I tend to use Jess's Java API extensively and just assume others do as well.
You can easily get a (pseudo) random number from Java within Jess as follows: (call java.lang.Math random) This will return a random (double) number, 'n' such that 0.0 >= n < 1.0. If you want more control over your random numbers, you can use the java.util.Random class as follows: (defglobal ?*rand* = (new java.util.Random (call System currentTimeMillis))) (bind ?randomValue (?*rand* nextInt 3)) This will assign a random integer between 0 (inclusive) and 3 (exclusive) to ?randomValue. -Mitch -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ihcheng Sent: Sunday, October 03, 2004 2:55 PM To: [EMAIL PROTECTED] Subject: RE: JESS: Counting the number within facts hi mitch I can't find the examples about random function within tht book "JESS in Action". Can you give me an example in using "random" function to select facts? lai -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Mitch Christensen Sent: Monday, October 04, 2004 12:35 AM To: [EMAIL PROTECTED] Subject: RE: JESS: Counting the number within facts Simply break out of your while loop when you have the one you want. If they are all truly equivelant, take the first one you get. If you want more random results, pick a random number between 0 and the number of query hits and take that one. -Mitch -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Ih-Cheng Lai Sent: Friday, October 01, 2004 8:18 AM To: [EMAIL PROTECTED] Subject: RE: JESS: Counting the number within facts hi Mitch I try to use defquery rule (as following) to retrieve the only one highest number Fact id randomly, but I still can not select only one highest number Fact id randomly. Could you tell me what the problem is? (deftemplate link-count "comment" (slot id)(slot count)) (deffacts catalog (link-count (id <Fact-1>)(count 2)) (link-count (id <Fact-2>)(count 3)) (link-count (id <Fact-4>)(count 4)) (link-count (id <Fact-5>)(count 1)) (link-count (id <Fact-6>)(count 4)) (link-count (id <Fact-8>)(count 4))) (reset) (defquery search-one-highest-number (declare (variables ?highestcount)) (link-count (count ?count&:(= ?count ?highestcount)))) (reset) ;; if highest number is 4 (bind ?it (run-query search-one-highest-number 4)) (while (?it hasNext) (bind ?token (call ?it next)) (bind ?fact (call ?token fact 1)) (bind ?id_name (fact-slot-value ?fact id )) (bind ?count_number (fact-slot-value ?fact count )) (printout t ?id_name " is " ?count_number crlf)) while I run Jess Jess> (batch work/defquery.clp) <Fact-4> is 4 <Fact-6> is 4 <Fact-8> is 4 FALSE lai -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Mitch Christensen Sent: Friday, October 01, 2004 2:19 AM To: [EMAIL PROTECTED] Subject: RE: JESS: Counting the number within facts If by 'random' you mean 'any one will do' (i.e. they are all equivalent), I'd probably just grab the first one and ignore the rest. There are lots of ways to do this. O You could define a defquery, and select randomly using the iterator. O You could assert a trigger fact (assert (process-link ?linkCount)) and then have a rule that looks for a (process-link) fact AND a (link-count) fact with a matching count and take whatever you get (then retract the (process-link) trigger fact). -Mitch -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of lai Sent: Thursday, September 30, 2004 6:20 AM To: [EMAIL PROTECTED] Subject: RE: JESS: Counting the number within facts hi Mitch Thank you for your kindly assistance. Also, I have another question. The question as follows: If some fact id have same highest number, the question is how to randomly select one of them? For example, Fact-8 is 3 Fact-6 is 3 Fact-4 is 1 Fact-1 is 3 Fact-0 is 2 The output can be Fact-8, Fact-6 or Fact-1. Lai -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Mitch Christensen Sent: Tuesday, September 28, 2004 4:10 AM To: [EMAIL PROTECTED] Subject: RE: JESS: Counting the number within facts Well, the following works. I'll leave it to others to comment on whether there is a better/easier way. for example, I was forced to declare salience. This doesn't seem necessary, but I couldn't quite get it to work without. -Mitch (deftemplate link "comment" (slot type)(slot id)) (deftemplate link-count "comment" (slot id)(slot count)) (deffacts initial-facts "comment" (MAIN::link (type similarity) (id <Fact-8>)) (MAIN::link (type contrast) (id <Fact-8>)) (MAIN::link (type contiguity) (id <Fact-8>)) (MAIN::link (type contrast) (id <Fact-6>)) (MAIN::link (type similarity) (id <Fact-6>)) (MAIN::link (type similarity) (id <Fact-4>)) (MAIN::link (type similarity) (id <Fact-1>)) (MAIN::link (type similarity) (id <Fact-0>)) (MAIN::link (type contiguity) (id <Fact-0>)) ) (reset) ;; define a query for counting (defquery count-link-assignments (declare (variables ?factId)) (MAIN::link (id ?factId))) ;; count the instances for each fact (defrule assert-type-counters (MAIN::link (type ?type)(id ?factId)) (not (MAIN::link-count (id ?factId))) => ;; count the number of link facts of this type (bind ?count (count-query-results count-link-assignments ?factId)) (assert (MAIN::link-count (id ?factId)(count ?count))) ) ;; select the largest counter (defrule print-highest-link-assignment "comment" (declare (salience -10)) (MAIN::link-count (id ?id)(count ?count)) (not (MAIN::link-count (count ?count2&:(> ?count2 ?count)))) => (printout t "Most active link found" ?id crlf)) -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of lai Sent: Monday, September 27, 2004 3:59 AM To: [EMAIL PROTECTED] Subject: JESS: Counting the number within facts Dear all I am a novice in implementing a design tool system by using JESS. Can anyone help me to solve the following questions? My questions are: If some facts are generated as following, (MAIN::link (type similarity) (id <Fact-8>)) (MAIN::link (type contrast) (id <Fact-8>)) (MAIN::link (type contiguity) (id <Fact-8>)) (MAIN::link (type contrast) (id <Fact-6>)) (MAIN::link (type similarity) (id <Fact-6>)) (MAIN::link (type similarity) (id <Fact-4>)) (MAIN::link (type similarity) (id <Fact-1>)) (MAIN::link (type similarity) (id <Fact-0>)) (MAIN::link (type contiguity) (id <Fact-0>)) 1. how to count link number within each Fact id. For examples, the link number of Fact-8 is 3, including similarity, contrast and contiguity links Fact-6 is 2 Fact-4 is 1 Fact-1 is 1 Fact-0 is 2 2. while the link number within each Fact id are compared, the Fact-8 with the highest number is selected as output. I looking forwards to your help! Lai -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] -------------------------------------------------------------------- -------------------------------------------------------------------- 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] --------------------------------------------------------------------