<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Am 24.02.2010 08:37, schrieb Wolfgang Laun:
<blockquote
 cite="mid:17de7ee81002232337l22e54a64n660693632210ade5@mail.gmail.com"
 type="cite">You really ought to read all the pertaining sections in
the Drools Expert manual and experiment. Nobody can write complex rules
without some exercise.<br>
  <br>
  <div class="gmail_quote">2010/2/24 dhari <span dir="ltr">&lt;<a
 moz-do-not-send="true" href="mailto:sdhari@hotmail.com">sdhari@hotmail.com</a>&gt;</span><br>
  <blockquote class="gmail_quote"
 style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
    <p>Thanks Jeffery.&nbsp;
I'll try this but what if I have more complex condition e.g. </p>
    <p><br>
Order ($discount : discount, $items : items)<br>
Item(grade &gt; 3 &amp;&amp; (quantity &lt; 5 || $discount &gt; 10))
from $items</p>
  </blockquote>
  <div><br>
Again, you are using a condition on discount not next to the field in
the fact where it occurs (i.e., Order); it's just not possible to write
it this way.<br>
  <br>
Order( discount &gt; 10, $items : items )&nbsp;&nbsp; # all Orders where discount
&gt; 10 <br>
Item( grade &gt; 3, quantity &lt; 5 ) from $items # and each of its
Items where grade &gt; 3 and quantity &lt; 5<br>
  <br>
There is no need to use &amp;&amp; to combine two conditions for the
same fact.<br>
  <br>
Now if you need to detect Order Items with grade &gt; 3 where either
the quantitiy of the Item is &lt; 5, or&nbsp; the Order's discount &gt; 10,
(or both) you'd have to write<br>
  <br>
Order( $discount : discount, $items : items )&nbsp;&nbsp; # all Orders<br>
Item( grade &gt; 3, $quantity : quantity) from $items # and each of its
Items where grade &gt; 3<br>
eval( $discount &gt; 10 || $quantitiy &lt; 5 ) # and discount &gt; 10
or quantity &lt; 5 (or both)<br>
  </div>
  </div>
</blockquote>
<br>
Hi!<br>
<br>
Another technique we commonly use at my employer to break up overly
complex rules is to "factor out" subexpressions, preferrably using
locally declared fact types to express the subexpressions, e. g.:<br>
<br>
declare ItemPreventingDiscount<br>
&nbsp; item: Item<br>
&nbsp; order: Order<br>
&nbsp; reason: String<br>
end<br>
<br>
rule "Prevent discount &gt; 10 for items graded &gt; 3"<br>
when<br>
&nbsp; $order: Order(discount &gt; 10, $items: items)<br>
&nbsp; $item: Item(grade &gt; 3) from $items<br>
then<br>
&nbsp; ItemPreventingDiscount fact = new ItemPreventingDiscount();<br>
&nbsp; fact.setItem($item); // optional<br>
&nbsp; fact.setOrder($order); // optional<br>
&nbsp; fact.setReason("discount percentage &gt; 10 not valid for items
graded &gt; 3");<br>
&nbsp; insertLogical($fact);<br>
end<br>
<br>
// add more rules to prevent other combinations of discount, quantity,
... whatever<br>
<br>
rule "Issue error message if items preventing discount exist"<br>
when<br>
&nbsp; exists( ItemPreventingDiscount( $reason: reason, $item: item ) )<br>
then<br>
&nbsp; System.out.println("Discount not allowed: Item '"+
$item.getDescription() + "' prevents discount because " + $reason +
".");<br>
end<br>
<br>
One might debate whether it is already appropriate to factor out
subexpressions for this still rather simple example, yet our experience
is that in general it leads to code with an improved readability. I'd
go so far to suggest that whenever you're struggling with the syntax of
the language you're using to solve a problem, ask yourself whether
you're trying to achieve too much with too little code. The answer is
of course depending on your level of experience with the language,
nevertheless you (the author of the code) probably want to be able to
read and understand the code at any time.<br>
<br>
Kind regards<br>
<br>
Ansgar<br>
</body>
</html>