Again, lots of ways to do it. Instead of using a defglobal, use a
deftemplate fact, one for each customer. To prevent duplicate rule
firings, you're going to need to put a slot into PURCHASE where you
can mark it accounted-for, then make the rule that adds them up
contingent on the value of that slot.
[Note -- deftemplate facts are much more efficient to use than ordered
facts, so (purchase (customer 1234) (amount 100) (recorded TRUE)) is
not only more readable, it's faster, too.]
Another way would be to use (store) and (fetch). (store) the sum
values by customer-id. (fetch) them, then re-store them as needed.
Yet another way: use the (bag) command. Set the defglobal to hold a
bag. Store the sums in the bag by customer-id.
As I said, there are a lot of ways to do things in Jess.
I think John Goalby wrote:
> Ernest
>
> Thanks, I really appreciate your speedy help.
> I like the progn implementation. As I said,
> I am new to this so that kinda thing helps a lot.
>
> I am now suffering from making my example too simple
> compared to what I am trying to do!!! I did think
> of the defglobal but I am trying to do this for
> different customers:
>
> (assert (PURCHASE 100 1234))
> (assert (PURCHASE 150 1234))
> (assert (PURCHASE 100 9999))
>
> etc, where the second param is the customer id.
>
> So, I need a way to be able to define a static
> variable type of construct within a rule. Does that
> make sense?
>
> I was thinking that I might write a small java class
> that looked like a rule and took a customer id. this
> class could then have a hashtable of ids with associated
> amounts. I was hoping for more of a built-in way
> of doing though as I am not even sure that would
> work.
>
> Thanks
>
> John.
>
> ----Original Message Follows----
> I think John Goalby wrote:
> > I understand the approach that you outlined and have got
> > someway to implementing it when I realized that I might
> > be looking for something a little different.
> >
> > I want to be able to add up the payments as the facts
> > get asserted - as I am doing backchaining.
> >
> > The payments are asserted separately (assert (PURCHASE 100)).
> >
> > I want to be able to define a rule such that I can accumulate
> > the purchases as they happen. The rule may look like:
> >
> > (defrule CHECKIT (PURCHASE ?AMT) (test (> ?AMT 80)) => (printout t ?AMT
> > crlf))
> >
> > What I would like is a rule to then specify that if the running
> > total is over a certain amount then printout a warning.
> >
> > (defrule CHECKIT (PURCHASE ?AMT) (test (> ?AMT 80)) (test (> ?TOT 200))
> =>
> > (printout t WARNING " " ?TOT crlf))
> >
> > Obviously I have not shown how to calc ?TOT as I don't
> > know how!!! This is where my knowledge is very limited.
>
>
> Well, as always, there are about a million ways to do it. Here's one:
> you could put the sum in a defglobal:
>
> (defglobal ?*sum* = 0)
>
> Then you could do something like this:
>
> (defrule CHECKIT
> (PURCHASE ?AMT)
> (test (> ?AMT 80))
> (test (progn (bind ?*sum* (+ ?*sum* ?AMT))
> (> ?*sum* 200)))
> =>
> (printout t WARNING " " ?*sum* crlf))
>
>
> (progn) just groups function calls together, so you can call more than
> one thing during a single (test) CE.
>
> >
> > Thanks
> >
> > John.
> >
> > ----Original Message Follows----
> > This is the trditional CLIPS solution, but it involves lots, lots more
> > computational work (both in CLIPS and in Jess.) If the number of
> > "purchase" facts is N, then roughly 2(N^2) partial matches will be
> > formed. The total number of rule activations is almost as large. The
> > defquery solution forms only N partial matches. If N is 50, 2(N^2) is
> > 5000, so the traditional solution does ~100 times more computation
> > (actually worse since all the activations have to be processed and the
> > rule has to fire ~50 times.) Plus, as you say, it's destructive.
> >
> > Defqueries are a Good Thing for applications like this.
> >
> > I think Thomas Gentsch wrote:
> > >
> > > I would have done something like that:
> > >
> > > (defrule sum
> > > ?f1 <- (purchase ?s1)
> > > ?f2 <- (purchase ?s2)
> > > (test (neq ?f1 ?f2))
> > > =>
> > > (retract ?f1)
> > > (retract ?f2)
> > > (assert (purchase (+ ?s1 ?s2)))
> > > )
> > >
> > > Works at least with CLIPS but I suppose with Jess as well. How does
> that
> > > compare performance-wise?
> > >
> > > (Of course, your initial facts are gone afterwards - if you want to
> keep
> > > them, it gets a little more difficult).
> > >
> > > tge
> > >
> > > [EMAIL PROTECTED] wrote:
> > > >
> > > > The most efficient way to do it would be to use a defquery to find
> all
> > > > the PURCHASE facts, then iterate over the query result directly and
> > > > add them up. You could define a deffunction to do this.
> > > >
> > > > I think John Goalby wrote:
> > > > > I would like to be able to have the following facts...
> > > > >
> > > > > (assert (PURCHASE 100))
> > > > > (assert (PURCHASE 150))
> > > > > (assert (PURCHASE 70))
> > > > >
> > > > > and then be able to have a rule that would give me the
> > > > > sum of these purchases. Is that possible?
> > > > >
> > > > > Thanks
> > > > >
> > > > > John.
> > > >
> > >
> > > --
> > > Thomas Gentsch
> > >
> > >
> > > ---------------------------------------------------------------------
> > > 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]
> > > ---------------------------------------------------------------------
> > >
> >
> >
> >
> > ---------------------------------------------------------
> > Ernest Friedman-Hill
> > Distributed Systems Research Phone: (925) 294-2154
> > Sandia National Labs FAX: (925) 294-2234
> > Org. 8920, MS 9012 [EMAIL PROTECTED]
> > PO Box 969 http://herzberg.ca.sandia.gov
> > Livermore, CA 94550
> >
> > ---------------------------------------------------------------------
> > 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]
> > ---------------------------------------------------------------------
> >
> >
> > _________________________________________________________________
> > Get your FREE download of MSN Explorer at http://explorer.msn.com
> >
>
>
>
> ---------------------------------------------------------
> Ernest Friedman-Hill
> Distributed Systems Research Phone: (925) 294-2154
> Sandia National Labs FAX: (925) 294-2234
> Org. 8920, MS 9012 [EMAIL PROTECTED]
> PO Box 969 http://herzberg.ca.sandia.gov
> Livermore, CA 94550
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>
>
> ---------------------------------------------------------------------
> 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]
> ---------------------------------------------------------------------
>
---------------------------------------------------------
Ernest Friedman-Hill
Distributed Systems Research Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
Org. 8920, MS 9012 [EMAIL PROTECTED]
PO Box 969 http://herzberg.ca.sandia.gov
Livermore, CA 94550
---------------------------------------------------------------------
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]
---------------------------------------------------------------------