[rules-users] Struggling with Rule syntax - please help...
Edson Tirelli
tirelli at post.com
Thu Mar 15 13:14:13 EDT 2007
James,
What are the "elements" in your list? Strings?
There are a few ways of doing it. Basically they differ on what are
the elements of your list, how many you expect to find in each list,
etc. The simplest way:
when
$code : String() from lookupDAO.getLookup(...)
( SHIPMENT_MOVE(customer_code == "abc", ship_code == $code ) OR
SHIPMENT_MOVE(customer_code == "abc", deliver_code == $code ) OR
SHIPMENT_MOVE(customer_code == "abc", port_code == $code )
)
then
// consequence
end
Remember that if your list contains codes that match more than one
condition above, your consequence will fire multiple times.
If you want it to fire only once, you can use a predicate:
when
$code : String() from lookupDAO.getLookup(...)
SHIPMENT_MOVE(customer_code == "abc", $sc : ship_code,
$dc : deliver_code, $pc : port_code,
( $code.equals($sc) ||
$code.equals($dc) || $code.equals($pc) ) )
)
then
// consequence
end
If you expect your list to have a lot of objects, or if you expect to
use the same list in more than one rule, you can improve performance by
doing a rule to retrieve and assert the list of codes, based on whatever
criteria you need (customer code for instance in the bellow example):
rule "retrieving the list"
when
Customer( $custCode : customer_code )
then
List codes = lookupDAO.getLookup( $custCode );
assert( codes );
end
rule "doing something"
when
Customer( $custCode : customer_code )
$list : List()
SHIPMENT_MOVE(customer_code == $custCode, $sc : ship_code,
$dc : deliver_code, $pc : port_code,
( $list.containts($sc) ||
$list.containts($dc) || $list.containts($pc) ) )
then
// do something
end
If your DAO returns a specific class, like the one mentioned in one
of your examples bellow (LookupTableResults) and it is not a collections
implementation, you can even do things like:
when
SHIPMENT_MOVE(customer_code == "abc", $sc : ship_code,
$dc : deliver_code, $pc : port_code )
LookupTableResult( elements contains $sc | contains $dc | contains
$pc ) from lookupDAO.getLookup(...)
)
then
// consequence
end
So, I don't have your code to run, but there are many ways of
doing... test and try to find which one best suites you.
Hope it helps.
Edson
jdepaul wrote:
>I'm having trouble figuring out syntax for my rules - could someone please
>steer me in the right direction - it's been a frustrating few days and no
>luck. I need to check existence of various attributes from the
>SHIPMENT_MOVE fact against a list of values (same list) I fetch from the
>DB...
>
>
>Get $valuesList from a DAO - then...
>
>If SHIPMENT_MOVE.customer_code = "abc" and SHIPMENT_MOVE.reference_code
>exists in $valuesList
>OR
>SHIPMENT_MOVE.customer_code = "abc" and SHIPMENT_MOVE.shipping_code exists
>in $valuesList
>OR
>SHIPMENT_MOVE.customer_code = "abc" and SHIPMENT_MOVE.partner_code exists in
>$valuesList
>
>
>
>I have the following rule working fine:
>
>$shipment SHIPMENT_MOVE(customer_code = "abc")
> LookupTableResult ( resultValue == 1 ) from lookupDAO.getLookup(...)
>
>But I'm having real problems with writing a rule like this:
>
>when
> SHIPMENT_MOVE(customer_code == "abc", $list contains ship_code) OR
> SHIPMENT_MOVE(customer_code == "abc", $list contains deliver_code) OR
> SHIPMENT_MOVE(customer_code == "abc", $list contains port_code)
>
>Your guidance will be appreciated -
>
>Thanks,
>James
>
>P.S.
>I'm using Drools 3.1.0M1
>
>
--
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
More information about the rules-users
mailing list