In Drools 3.0 we call them columns and field constraints.

Person() - is a column

Person( name == "gertrude" ) is a column with a single field constraint - in this case a Literal Constriant. The use of Literal Constraints are encouraged as we can aggresively optimise them.

Literal Constraints are for Strings, Dates, primitives and Enums. There are two other types of Field Constraints; Return Value and Predicate - both of which allow pure Java Expressions.

Predicate must evaluate to a boolean and can use previously bound variables. ReturnValue can also use previously bound variables but must evaluate to an Object; primitives must be wrapped. Future versions of Drools will try to deliver more intelligent boxing.

Person( sex = "f", ageOfWoman : age )
Person( sex = "m", ageOfMan : age -> (ageOfMan == ageOfWoman + 2)

The first Column finds all Females and declares the variable "ageOfWoman" and binds it to the woman's age. The second column finds all Males but it constraints all pair matches so that the man is 2 years older than the female. the above is a Predicate. Prediates bind the current field and then allow it to be used in a standard Java Expression that must evaluate to a boolean.

You can also use a Return Value Field Constraint, in the current situation both are equivalent - but the ReturnValue is less verbose and does not require the second binding.
Person( sex = "f", ageOfWoman : age )
Person( sex = "m",  age == (ageOfWoman + 2)

Inside of Predicates and Return Values you can use an standard java expression, so you can also call all the normal class methods on the instance. Further to this you can also call Drools functions.

Finally there is eval. Eval is the same as a predicate but it happens after the join attempt and is thus not quite as efficient.
Person( sex = "f", ageOfWoman : age )
Person( sex = "m", ageOfMan : age  )
eval( ageOfMan == ageOfWoman + 2)

Any function or methods used inside of Predicate and ReturnValue must be constant in their behaviour - their values cannot change over time; this is because they are indexed. Where as you are free to use values that change over time in evals.

It often helps to think in terms of SQL :
select * from Person f, Person m where f.sex = "f" and m.sex = "m" and m.age = f.age + 2

Finally notice, that unlike Drools 2.0, you do not always have to bind the Column or the Field to write rules - only where it is needed.

Is this starting to make sense nows? It takes a little while to get use to - welcome to the wonderful world of Declarative Programming :)

Mark

Dmitry Goldenberg wrote:
So, "stringAttr" is shorthand for "getStringAttr."  Okay.  Next - how do I 
invoke other types of methods on my Java object, other than getters?

________________________________

From: Mark Proctor [mailto:[EMAIL PROTECTED]
Sent: Fri 4/14/2006 1:20 PM
To: [email protected]
Subject: Re: [drools-user] The DRL notation vs. jbossrules Rule Language - ?



Dmitry Goldenberg wrote:
rule "First Rule"
when
    ClassA( stringAttr == "xyz" )
    ClassB( intAttr > 50 )
then
    // do some java code
end

In this example, where is the call made to the getStringAttr() and getIntAttr() 
methods on instances of ClassA and ClassB ??
look at LiteralConstraint and the ASM code, we take car of this for you.
We believe this syntax to be less verbose and more declarative and
clearer than standard java notation. It is also directly from JRules.
This should make it more trivial for JRules developers to directly
convert their code.
rule "First Rule"
when
    objA : ClassA( stringAttr == "xyz" )
    objB : ClassB( intAttr > 50 )
then
    // do some java code
   System.out.println(objA);
   System.out.println(objB);
end

Here, are objA and objB Boolean ?


No here objA is a reference to an instance of ClassA and objB is a
reference to an instance of ClassB.
________________________________

From: Edson Tirelli [mailto:[EMAIL PROTECTED]
Sent: Thu 4/13/2006 3:47 PM
To: [email protected]
Subject: Re: [drools-user] The DRL notation vs. jbossrules Rule Language - ?



    All,

    Find in the following link, the documentation that is being built
for drools 3.

http://labs.jboss.com/portal/jbossrules/docs

   On a quick explanation, the new syntax would be:

------------
rule "name"
    ATTRIBUTES
    when
        LHS
    then
        RHS
end
------------

ATTRIBUTES: those are rule attributes like salience, duration, etc, in a
similar way Drools 2.x uses.

LHS: this is the left hand side of the rule. This is the previous
"parameter" + "condition" statements. (see bellow)

RHS: this is the right hand side of the rule. This is the consequence,
the same way in drools 2.

   I think the simple way to explain LHS is to make a conversion example:


<rule name="First Rule">

<parameter identifier="objA">
  <class>ClassA</class>
</parameter>

<parameter identifier="objB">
  <class>ClassB</class>
</parameter>

<java:condition> objA.getStringAttr().equals("xyz") </java:condition>
<java:condition> objB.getIntAttr() &gt 50 </java:condition>

<java:consequence>
   // some java code
</java:consequence>

</rule>

   The above rule when converted to Drools 3 would became:

rule "First Rule"
when
    ClassA( stringAttr == "xyz" )
    ClassB( intAttr > 50 )
then
    // do some java code
end

   If you need a reference to the actual object matched in each "column"
(more or less what Drools 2 call parameter), you can bind it in the
following way:

rule "First Rule"
when
    objA : ClassA( stringAttr == "xyz" )
    objB : ClassB( intAttr > 50 )
then
    // do some java code
   System.out.println(objA);
   System.out.println(objB);
end

   In the documentation you will find syntax diagrams that can help you
understand all possible syntaxes and all the new operators/features
drools 3 has.

   []s
   Edson


Ronald van Kuijk wrote:

AINAE, but it could be as simple as 'replacing' condition with when and
consequence with then, but I'll let the experts tell me.

Ronald

2006/4/13, Dmitry Goldenberg <[EMAIL PROTECTED]>:


I don't understand the relationship between the XML-based DRL notation and
this new lingo with "when" / "then".

With the DRL notation, my understanding is that you write an XML structure
like the one I'm including below.  How does this change with the when/then
notation?  Thanks.


<?xml version="1.0"?>

<rule-set name="SamplePolicyRuleSet"

 xmlns="http://drools.org/rules";

 xmlns:java="http://drools.org/semantics/java";

 xmlns:xs="http://www.w3.org/2001/XMLSchema-instance";

 xs:schemaLocation="http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">



 <!-- Imports -->

 <java:import>java.lang.Object</java:import>

 <java:import>java.lang.String</java:import>

 <!-- Utility functions -->

 <java:functions>

   public boolean f1(com.weblayers.platform.rule.PolicyExecContextcontext)

   {

       return ...;

   }

   public boolean f2(com.weblayers.platform.rule.PolicyExecContextcontext)

   {

       return ...;

   }

 </java:functions>



 <!-First Rule: IF (P1 AND P2) THEN RETURN OK -->

 <rule name="First Rule">

   <!-- Rule parameters -->

   <parameter identifier="context">

     <class>MyContext</class>

   </parameter>



   <!-- Rule Conditions -->

   <java:condition>

     f1() && f2()

   </java:condition>



   <!-- Rule Consequences -->

   <java:consequence>

      context.setReturn(Constants.OK);

   </java:consequence>

 </rule>



 <!-Second Rule: IF (!(P1 AND P2)) THEN RETURN FAILURE -->

 <rule name="Second Rule">

   <!-- Rule parameters -->

   <parameter identifier="context">

     <class>MyContext</class>

   </parameter>



   <!-- Rule Conditions -->

   <java:condition>

     !(f1() && f2())

   </java:condition>



   <!-- Rule Consequences -->

   <java:consequence>

      context.setVerdict(Constants.FAIL);

   </java:consequence>

 </rule>



</rule-set>





--
  ---
  Edson Tirelli
  Auster Solutions do Brasil
  @ www.auster.com.br
  +55 11 5096-2277 / +55 11 9218-4151








Reply via email to