• 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: accumulate function

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't get accumulate function to work. I want to get a count of the number of matching cards of the same "name"

facts
(deffact hand(slot name)(slot value)) ;;name = color , value = number
(deffact pile(slot name)(slot value))

(defglobal ?*n* = "")
(defglobal ?*v* = "")

----------------------
(defrule init
(pile (name ?na)(value ?va))
=
(bind ?*n* ?na)
(bind ?*v* ?va)
)

-----------------------

(defrule count-same-color
?c-(accumulate (bind ?count 0) ;Initialise count
(bind ?count (+ ?count 1) ;;Action
?count ;;Result

;;CE - hand (name) is same as ?*n* [pile(name)]
(hand (name ?*n*))

)
=
(printout t "Color match: "?c crlf)
)

FACTS
----
f-0 (MAIN::hand (name "r") (value "2"))
f-1 (MAIN::hand (name "b") (value "5"))
f-2 (MAIN::hand (name "b") (value "3"))
f-3 (MAIN::hand (name "b") (value "4"))

f-4 (MAIN: ile (name "r") (value "5"))

?c should be equal to one because only f-0(name) match f-4(name).
But it's giving me ?c = 4. Why is this? Is my CE part written wrong?
 
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
Pattern-matching happens when facts are asserted. If a pattern depends on a defglobal or other value that's outside of working memory, then changing that value does not redo the pattern matching. Instead of using defglobals this way, use facts to hold the numbers you want to match against.
 
sandy ngai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm very slow on understanding this.

could you give me an example on "use facts to hold the numbers you want to match against"?

I'm trying to match hand(name) and pile(name).
 
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
Your "init" and "count-same-color" rules are two separate rules that "communicate" through defglobals. Don't do that; just make them into one rule, something like

 
sandy ngai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, thankx for your help. I was about to give up cos nothing in jess worked for me. I'm starting to understand it more with your help ^^

your sample code there helped alot. cheers ^^
 
reply
    Bookmark Topic Watch Topic
  • New Topic