aspose file tools*
The moose likes Other Java Products and Servers and the fly likes About jess engine rule 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 "About jess engine rule" Watch "About jess engine rule" New topic
Author

About jess engine rule

Nicolas Daclin
Greenhorn

Joined: Sep 08, 2008
Posts: 2
I'm a new jess user and i have a problem to write a rule. I have one deftemplate such as: (deftemplate (x (slot x)). I assert : (assert (x (x 10))) and (assert (x(x 11))) and i would like to add these two assertions and print the result. I'm trying to write one rule that call a function but i never obtain the result. In the best case i get only the two assertions on my console (11 10) or i get an endless iteration. I' m using "retract", "for" and "while" iteration but i don't find the solution.
I can add up with two different deftemplates like (deftemplate x) and (deftemplate y) but no with only one. In my case, i want to perform this operation with n assertion of x. I'm lost...so, anybody can help me?
Many thanks

Nicolas
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,

Welcome to JavaRanch!

This isn't a typical rule engine application, but a simple program that does this might look something like



[Jess in Action][AskingGoodQuestions]
Nicolas Daclin
Greenhorn

Joined: Sep 08, 2008
Posts: 2
Hi,
Thanks a lot for your quick aid.
I have a little bit modified this base in order to perform the operation for n assertions and to print the final sum:
firstcode---------------------------------------------------------
(deftemplate number (slot name)(slot value (default 0)))

(defrule add-n-values
?n1<-(number(value ?v1))
?n2<-(number(value ?v2 & < ?n1 ?n2)))
=>
(retract ?n1 ?n2)
(assert(number (value (+ ?v1 ?v2))))
)

(defrule print-solution
(number(value ?v))
=>
(printout t "the sum is " ?v crlf))

(assert(number(name x)(value 1)))
(assert(number(name y)(value 3)))
(assert(number(name z)(value 1)))

(assert (number (name useless))); useless assertion to start operation
(run)
------------------------------------------------------------
I think this code is correct (at least for me, because it runs as i want), but i don't know why, when i put the printout in the the first rule (add-n-value):
second code-------------------------------------------------
(deftemplate number (slot name)(slot value (default 0)))

(defrule add-n-values
?n1<-(number(value ?v1))
?n2<-(number(value ?v2 & < ?n1 ?n2)))
=>
(retract ?n1 ?n2)
(assert(number (value (+ ?v1 ?v2))))
(printout t "the sum is " (+ ?v1 ?v2) crlf)
)

(assert(number(name x)(value 1)))
(assert(number(name y)(value 3)))
(assert(number(name z)(value 1)))

(assert (number (name useless))); useless assertion to start operation
(run)
------------------------------------------------------------------
i get 1,4,5 on my console (for me it is logic) and when i perform the printout in another rule (first code)i get the final result (for me, i should obtain the same result on my console). The last assertion allows to fire the rule when only one value is asserted.

Thanks again!!

Nicolas
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

By default, the most recently activated rule will fire first, so the rules activated by the newly-asserted partial sums will fire before the reporting rule fires. But there's really no guarantee this will give you what you want; depending on the number of facts and how you define them, you might see some partial results printed.

There's a special conditional element you can use to do a computation on all the facts that match a pattern: the "accumulate" conditional element:



accumulate is discussed in the manual, but basically it's something like a for loop. It allows you to initialize a variable "(bind ?sum 0)", modify the variable for each matched pattern "(bind ?sum (+ ?sum ?value))" and return the variable "?sum". The last part of accumulate is the pattern -- here our "number" pattern.

This may seem complicated but again, as I said, adding up numbers isn't really a rule-based activity. If something is easy to do in procedural code, you should probably do it in procedural code. A table saw is a powerful tool, but you can't use it to carve a turkey.
ksill pop
Greenhorn

Joined: Jan 09, 2011
Posts: 2
I have a problem..

is there someone who would be nice to help me, ill be really thankful..

jess program is like this:

(deftemplate person
(slot name(type SYMBOL))
(slot age(type INTEGER)))
(deftemplate persons
(person (name XX)(age 22))
(person (name YY)(age 44)))

a want to store slot values into some java object..
so, could that be possible on this way

..
Rete engine = new Rete();
Fact f = new Fact("person", engine);
..
engine.store ("variable", f.getSlotValue("name"));
..
???

I will really appreciate any help.... does anybody has some solution!?
thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About jess engine rule