Help coderanch get a
new server
by contributing to the fundraiser
  • 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

About jess engine rule

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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!

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

 
Nicolas Daclin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Crusading Chameleon likes the size of this ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic