2009/7/10, Edson Tirelli <tirelli(a)post.com>:
Hi Aldian,
I think you misunderstood the semantics of forall(). Forall in Drools is
the same forall quantifier from the First Order Logic.
http://en.wikipedia.org/wiki/First-order_logic
So, if you want to write a rule that says: "When for all equipments of
type 'router' there is an alarm, then raise a general failure alarm".
when
forall( $e : Equipment( type == "router" )
Alarm( source == $e ) )
then
// raise a general failure alarm
end
So, if you have 10 routers in the working memory and only 9 of them have
associated alarms, this rule will NOT fire, because the condition is not
matched. But, if an alarm is raised for the 10th router, then all of them
have an associated alarm and the rule will fire.
So, as you can see, "forall" is a quantifier CE, in the same way that
"not" and "exists" are quantifiers. They operate on multiple facts at
once,
and as so, you can not use the variables bound inside their scope outside of
it. In the above example, if you tried to use $e in the consequence, which
of the 10 routers would it be bound to? So, since it makes no sense, the
engine disallow the usage of variables bound inside quantifiers outside of
them.
Hello, and Thank you very much for the answer. But your explanation
shows that my understanding of this keyword was correct. And what if I
want to ewecute this condition/consequence:
When
* All routers which index is different from 0 are raising the alarm 45
then
* Execute some specific code on all these routers
end
As you can see If I attribute a $e to the routers from the condition,
it makes sense hoping to retrieve a reference on these routers to
execute code on them. I think I will escape this problem doing it with
two rules:
rule "all routers but the root raise 45"
When
* All routers which index is different from 0 are raising the alarm 45
then
* write somewhere that rule "all routers but the root raise 45"
has been validated
end
rule "set routers about the alarm 45"
When
* there is written somewhere that "all routers but the root raise
45" has been validated
* $a : Alarm is raising 45 from an equipement $e different from root
then
* execute some specific code on $a and $e
end
There is also the possibility to do it in only one rule, but I have
some doubts about time optimization:
rule "detect and set routers different from root for alarm 45"
When
* All routers which index is different from 0 are raising the alarm 45
* There is an alarm $a of numero 45 raised by an equipement $e
different from the root
Then
* execute some specific code on ($a,$e)
end
this way, drools will proceed to two verifications:
* if forall routers which index is different from 0, they have raised
an alarm of numero 45
* if any alarm $a originated from a routers $e different from the
root, is of numero 45 and then execute some code on it.
Just to be easier to remember I call quantifier CEs "scope delimiters".
The general rule is: variables bound inside a scope delimiter are never
available outside of them.
I don't understand what you are trying to do in your first rule in your
example. If you write your rule in plain English, the people from the list
can help you write it correctly.*
I am sorry, English is not my mother language, but I hope you will
understand a lot. I will know give further explanations on my example:
I am supervising a network of equipements. They are not all routers,
and actually, they are very different from each others, but you can
consider them as routers, it doesn't matter. There is a central
equipement, the root, which centralized all the alarms sent by the
other equipements. And there is a software that supervise that root
equipment and that folow me the alarms. I am trying to make
correlation on some basic cases as this one:
* for all equipements but the root I have the alarm "impossible to
connect". It is a very common alarm, because we often work with
virtual equipements, so we know that when we have that alarm on every
equipement but the root, there is no worry to have. But we would like
to suppress all that alarms that are all the same to only one that
agregate all the others. So what we wish to do is the folowing:
when
* For all equipement different from the root an alarm of type 45
as been raised,
then
* Suppress all these alarms and create an alarm that agregate all
these alarms in only one
end
As you can see the ideal would be that the forall allow to attribute
to a $tabarray variable an Array of type Alarm [] containing all the
alarms meeting the requirement.
Finally, just for completeness, be careful with the semantics of time.
Your Alarm class is using the system clock to define timeout, and that will
give you all kinds of headaches, not to mention wrong/unexpected results.
I suppose you say this because of the risk of unsynchronized clocks on
the network? But for now I don't see how to improve that...
But this e-mail is already too long. I suggest you take a look at the
Drools
Fusion docs on temporal reasoning.
Thank you very much, I will take a look about this
Regards
Aldian
2009/7/10 Gab Aldian <aldian.gp(a)gmail.com>
> Hi everybody
>
> I have read with attention the drools expert documentation, which was
> very interesting, but a little 'too simple". For example, most
> examples don't present rules with the keyword "forall". I also met big
> difficulties mixing the references on the objects to my javacode. This
> is why I would need some more documentation with examples that go
> deeper into drools capacities.
>
>
> To give you clues about my plan, here is an example (which works
> perfectly in drools 5) that I have developed:
> java code -
http://drools.pastebin.com/m1a705614
> rule code -
http://drools.pastebin.com/m3f30cbdc
>
> As you can see I am studying the case where drools is used for the
> monitoring of a network of equipments that send alarms about their
> problems. But I have big difficulties, because I would like to get
> references on the objects of the "forall", and to execute javacode
> such as $toto.method($titi, $tata), but such things seems impossible
> (systematic failed of compilation)
>
> Could you please help me?
>
> Thank you very much!
>
> Aldian
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
--
Edson Tirelli
JBoss Drools Core Development
JBoss by Red Hat @
www.jboss.com