[rules-users] function in drools

Davide Sottara dsotty at gmail.com
Fri Sep 28 13:22:03 EDT 2012


I would recommend you to read the drools manual and the examples, especially
the introduction... rule based programming is somewhat different from more
traditional procedural (e.g. java) programming. It may let you solve your
problems efficiently and elegantly, or just give you headaches.. :)

Now, as for the examples:
1) the "then" in a rule is a "when premise then conclusion", as in (a type
of) "logic", not a "then" as in "first this then that", as in sequential
programming. SO, the premise is about collecting (joining) and matching
(selecting) data against patterns. This said, the accumulate has to be in
the LHS, not in the RHS.
2) I guess you're trying to compare the average temperature against ? is
$max the max temperature? Or a threshold you provide from somewhere else?
3) I also guess you want to use the average temperature in the other rule...
As Wolfgang pointed out, 
you have to insert it in the working memory (or use a global, but this may
not the best of practices). Moreover, it's more useful to use the value to
create an instance of a more "meaningful" class than simply inserting a
Number. What if other rules insert Numbers?
4) Maybe you also want to make the number related to the person? It's an
interesting exercise on joins, to see what happens if you add that "foreign
key" or not
5) You may not need to use agenda groups, if your rules are implicitly made
sequential by the patterns. If a rule can only matched something potentially
inserted by another, there is no way for that rule to fire until the other
has been fired itself.

declare AverageTempRecord
  // pers  : Person
  avg : double
end

rule "MrFlow"
when
   $pers : person : Person( gender == 'M' )
   accumulate ( 
      SensorReading( $temp : temperature ) over window:time( 10m ),
      $max : max( $temp ),            // this is the max temperature over
the window, but this can't be your "$max"...
      $avg : average( $temp )
      // ;
      // $avg > MAX   // where do you get the threshold from? 
   )
then
   insertLogical( /*$pers,*/ new AverageTempRecord( $avg ) );
end


rule "Hello MR"
when
  $p : Person()
  $a : AverageTemperatureRecord( /* pers == $p, */   $avg  : avg )
then
   System.out.println( "Hello mr " + $p.getName() + ", your temp is " + $avg
);
   retract( $p ); 
end

Best
Davide



--
View this message in context: http://drools.46999.n3.nabble.com/function-in-drools-tp4020021p4020046.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list