The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

sidali (23) [Avatar] Offline
#1
Hi there,

Is there a way to know exactly the moment when a rule finishes to match all facts that fires it? I need to communicate this information to another rule witch wait the first one finishing its job to fire it self.
ejfried (326) [Avatar] Offline
#2
Re: moment when a rule finishes to match all facts
You can use ACTIVATION JessEvents to learn when a rule is activated (the Jess term for what you're asking.) But I'm a little concerned about your question: it sounds like you're trying very hard to force rules to fire in a particular order, which really defeats the purpose of using rule-based programming in the first place. Maybe you could explain what you're trying to accomplish a little more and I could offer you some other method.
sidali (23) [Avatar] Offline
#3
Re: moment when a rule finishes to match all facts
ok, my problem is as follows :

(deftemplate activity(slot activityName)) ; activities
(deftemplate doneActivity(slot activityName))

(defglobal ?*activitiesList*= (create$)) ;a list used to accumulate activities

(defrule accumulate-activities ;select all activities not yet done
(activity(activityName ?act))
(not(doneActivity(activityName ?act)))
=>
(bind ?*activitiesList* (insert$ ?*activitiesList* (+ (length$ ?*activitiesList*) 1) ?act)))
(...)
)

(defrule display-accumulated-activities ;display available activities
?finishFlag <- (finish-accumulate-activities) ;flag
=>
(retract ?finishFlag)
(printout t "Available activities not done yet are :" ?*activitiesList* crlf)
(...)
)

if i place "?finishFlag" in the first rule the second one fires before the first one has finished and it is incorrect! so i must find a way to indicate to the second rule that the first one has finished all activities.

I have tried to declare a negative salience in the second rule in order that it wait until no rules were in agenda to fire but it causes a synchronization problem.
ejfried (326) [Avatar] Offline
#4
Re: moment when a rule finishes to match all facts
Well, salience is one way to deal with this that could work fine -- tell me about the "synchronization problem"?

A number of other options are shut off because you're using a defglobal instead of a fact to hold activitiesList. If this were a multislot in a fact, then you could pattern-match on it, and do something like

(activityList (activities $?list))
(not (doneActivity (activityName ?act&smilienot (member$ ?act ?list)))))
=> ...
sidali (23) [Avatar] Offline
#5
Re: moment when a rule finishes to match all facts
1)
Well, the synchronization problem is as follow:

I have simplified the rules but to understand the problem i will give all details as follow:

(defrule select-and-display-activities
?goal<-(goal select-n-display-activities)
(...) ;other conditions
=>
(assert(goal accumulate-activities))
(assert(goal display-accumulated-activities))
)

(defrule accumulate-activities ;select all activities not yet done
?goal<-(goal accumulate-activities)
(activity(activityName ?act))
(not(doneActivity(activityName ?act)))
=>
(bind ?*activitiesList* (insert$ ?*activitiesList* (+ (length$ ?*activitiesList*) 1) ?act)))
?finishFlag <- (finish-accumulate-activities) ;flag
(...)
)

(defrule display-accumulated-activities ;display available activities
(declare(salience -10))
?goal<-(goal display-accumulated-activities)
?finishFlag <- (finish-accumulate-activities) ;flag
=>
(retract ?goal ?finishFlag)
(printout t "Available activities not done yet are :" ?*activitiesList* crlf)
(...)
)

the problem is that in spite of negative salience, the last rule fires before the second one finishes matching all existing activities!

I suppose that the last rule matches the goal and wait only for the flag "?finishFlag" and fires immediately the flag is up and neglects the salience.

2)
Using facts instead of defglobal is a good solution but i don't think the problem is solved by using:

(activityList (activities $?list))
(not (doneActivity (activityName ?act&smilienot (member$ ?act ?list)))))
=> ...

because the rule still fires many times (for each doneActivity()in working memory) and there is not a way to indicate that it has finished!
ejfried (326) [Avatar] Offline
#6
Re: moment when a rule finishes to match all facts
1) If the last rule fires, as you say, while there are still activations for the second rule on the agenda, then that would be a bug. I'd be very interested to see a complete program which runs and displays this behavior. You can send the program to me at "ejfried at ca period sandia period gov". It's likely that there's another explanation, though.

2) The way I wrote that condition, the rule won't fire if there are any doneActivities that aren't on the activityList; based on my understanding of what you're doing, that should be exactly what you wanted.
sidali (23) [Avatar] Offline
#7
Re: moment when a rule finishes to match all facts
Ok, i will send you the entire program soon. Thank you for your active help.