Hello all,

I would like to suggest we return to the beggining here to explore interesting ways for a football manager and coaches to use JBRules. :) First of all, lets define a possible object business model. Lets start with a single class (pseudo code):

class Player {
 String name;
 String position;
 int skill; // how good the player is: grade from 0 to 10.
 int age;
 double salary;

 // getters and setters
}

Ok, so, we have a set of players asserted into working memory. Lets say now, the manager asks the coach to define who will be the main team for the next season, and the coach wants to use his best player for each position. A rule like this one could be used:

global java.util.List mainTeam;

rule "Main Team"
 $player: Player( $name: name, $skill: skill, $position: position)
 not Player( position == $position, skill > $skill )
then
System.out.println(" The best player for position "+$player.getPosition()+" is "+$player.getName() );
 mainTeam.add($player);
end

So, using this single rule, the coach will cast his best player for each position. Now, lets say, all tournments have age constraints. So, the coach wants to know his best team for each tournment, matching age constraints. So, lets improve our business model to allow Tournments to be defined:

class Tournment {
  String name;
  int minimumAge;
  int maximumAge;
  List team;
  // getters and setters
}

  So, now, we can change our rule to take tournments into account:

rule "Team for each tournment"
  $tournment : Tournment( $minAge: minimumAge, $maxAge: maximumAge )
$player: Player( $name: name, $skill: skill, $position: position, age >= $minAge, age <= $maxAge ) not Player( position == $position, skill > $skill, age >= $minAge, age <= $maxAge )
then
  $tournment.addPlayerToTeam( $player );
end

As you can see by above examples, it is always good to define rules in terms of our business model and usually not use "hardcoded" constraints. This will make for clear and more maintainable rules, as well as avoid an "infinite" sequence of nested "or" and "and" operators. What if a team must be under a certain total salary cost? What if players can't join more than X silmultaneous tournments? It is always simple and straightforward to define this kind of rule, as long as you have a good business model with supporting data. Having said that, JBRules does support nested "or" and "and" operators (though it does not support "not" with nested operators inside it), but it is important to note that the top level "or" operator in rules engines (this is not JBRules specific) will effectivelly create multiple rules in the backend. So, if you say:

rule "A"
  ClassX(<constraints>) || ClassY( <constraints> )
then
  ..
end

   It is only syntax sugar, as it will compile to:

rule "A1"
  ClassX(<constraints>)
then
  ..
end

rule "A2"
  ClassY( <constraints> )
then
  ..
end

If you have a complex sequence of nested "and" and "or" operators, it will use Morgan's theorem to transform it in a disjunction of conjunctions, i.e., a top level "or" with "and"s beneath it. After that, it will break the "or" making each logical branch a single subrule. Again, this is not JBRules specific, but general rules engine's behavior.

Sorry for the long e-mail, but I hope this brings some light to the subject under discussion, i.e., I hope our football managers and coaches continue to use JBRules and winning tournments! ;)

Let me know if you guys have any question or issue you would like to discuss further, and thanks for bringing some fun and good humor to the list!

  []s
  Edson

John Cocktolstoy wrote:

First - thanks for answers.

So, basically I could use evaluation such as:

Player1 ( hisage : age -> ( compare(hisage, 19, 21) ) )

or even, without the inline function:

Player1 ( hisage : age -> ( (age == 19) || (age == 21)) ) )

This is equivalent to what Matias did although he put it into dsl.

As to what is stated in documentation, it indeed suggest that man can create rule like in algebra with grouping and nesting "and"s and "or"s. From my experience it all work as long as you don't nest.

regards
John


On 11/8/06, *gabriel quennesson* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    That's why I said It probably wasn't the answer you expected.
    You could use an inline function that would compare the value and
    return
    true if it matches one argument or the other :

    when

        Player1 ( hisage : age -> ( compare(hisage, 19, 21) ) )

    then

        doStuff();

    end

    function boolean compare(int age, int firstValue, int secondValue) {

    if (age == firstValue)

        return true;

    if (age == secondValue)

        return true;

    return false;

    }


    But I must admit it is not very clean either.

    Now, in drools doc section * 3.23 *it is stated you can group using
    parenthesis and keywords, pretty much like you would in algebra. I
    abandoned the idea though, and did the same by writing several rules.

    John Cocktolstoy a écrit :
    > Thanks for the answer!
    > Yep, in case of String types I could use 'matches' . But what if
    each
    > player have also field:
    > int age
    > and instead of names I would like to compare age of players? How to
    > cope with that?
    >
    > As to the second idea - I think I can't just write the rule the
    exact
    > way I did with "and" and "or" keywords. I believe it is
    impossible to
    > nest "and"s and "or"s. Am I right? I also wasn't able to
    successfully
    > write such a rule.
    >
    > regards
    > John
    >
    > On 11/8/06, *gabriel quennesson*
    <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    > <mailto: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>>> wrote:
    >
    >     I'm not sure this is the answer you expect but you could do the
    >     following :
    >
    >     when
    >
    >         Player1 (name matches "Roy|Bob")
    >
    >         Player2(name matches "Tom|Eric")
    >
    >     ...
    >
    >         Player11(name matches "Paulo|Ryan")
    >
    >     then
    >
    >         doStuff();
    >
    >     end
    >
    >
    >
    >     In some trickier cases, you could use functions to perform other
    >     forms of matching.
    >
    >     Or you could just write the rule the exact way you did with
    "and"
    >     and "or" keywords, which is documented in Drools (I wasn't
    able to
    >     successfully write such a rule, though).
    >
    >     Regards,
    >
    >     Gabriel
    >
    >     John Cocktolstoy a écrit :
    >>     Hi,
    >>
    >>     I am new to Drools, I just read documentations and went through
    >>     some examples.
    >>     I am wondering about the following example (I spiced it up
    with a
    >>     story so it should not be boring :-) ):
    >>
    >>     Suppose I am a football manager and trying to use Drools to
    >>     automate some processes.
    >>     I have 11 classes (these are going to be facts):
    >>     Player1, Player2, Player3, ..., Player11
    >>     One for each player of my team, each of class has one field:
    >>     String name
    >>     In run-time I will assert into working memory only one instance
    >>     of each class as I would like to evaluate some rules over my
    >>     current team.
    >>     Now I would like to create simple rule - I express it in
    >>     pseudo-language:
    >>
    >>     when
    >>       ((Player1.name <http://Player1.name>
    <http://player1.name/> == "Roy") or (
    >>     Player1.name <http://Player1.name> <http://player1.name/>
    == "Bob"))
    >>     and
    >>       ((Player2.name <http://Player2.name>
    <http://player2.name/> == "Tom") or (
    >>     Player2.name <http://Player2.name> <http://player2.name/>
    == "Eric"))
    >>     and
    >>       ...
    >>     and
    >>       ((Player11.name <http://Player11.name>
    <http://player11.name/> == "Paulo") or
    >>     (Player11.name <http://Player11.name>
    <http://player11.name/> == "Ryan"))
    >>     then
    >>       do something
    >>
    >>     Could anyone show me how to express this rule using Drools
    >>     without going into huge number of rules? As I understand from
    >>     documentation each 'or' is creating two sub-rules. Isn't that
    >>     limitation that makes Drools useless for football managers?
    >>
    >>     regards
    >>     John
    >
    >

    --

    ___________________
    *Gabriel Quennesson *| CTC/I | *SEAM*
    Int :* *7 906 6910 | Ext: 04 73 64 69 10
    [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    <mailto:[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>>


    ---------------------------------------------------------------------
    To unsubscribe from this list please visit:

        http://xircles.codehaus.org/manage_email
    <http://xircles.codehaus.org/manage_email>




--
 ---
 Edson Tirelli
 Software Engineer - JBoss Rules Core Developer
 Office: +55 11 3124-6000
 Mobile: +55 11 9218-4151
 JBoss, a division of Red Hat @ www.jboss.com

 IT executives: Red Hat still #1 for value
http://www.redhat.com/promo/vendor/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

   http://xircles.codehaus.org/manage_email

Reply via email to