<br>&nbsp;&nbsp; Please do! We will all thank you.<br><br>&nbsp;&nbsp; Regarding checking all the conditions in the LHS, that&#39;s what the engine does best, so always avoid &quot;if&quot;s in the RHS... in the LHS they get indexed, shared, hashed, etc, whenever possible, improving the performance of the whole system.
<br><br>&nbsp;&nbsp; []s<br>&nbsp;&nbsp; Edson<br><br><div><span class="gmail_quote">2007/7/13, Oleg Yavorsky &lt;<a href="mailto:oleg_yavorsky@yahoo.com">oleg_yavorsky@yahoo.com</a>&gt;:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Edson,<br><br>Thank you very match. I&#39;ve found a lot of ideas from your response. I didn&#39;t think about moving all conditions to LHS first and planned to use LHS only as a filter to find object to validate in RHS. I&#39;ll try to implement prototype and share experience in a list if you don&#39;t mind.
<br><br>Oleg.<div><span class="e" id="q_113bf44153a88c50_1"><br><br><b><i>Edson Tirelli &lt;<a href="mailto:tirelli@post.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">tirelli@post.com</a>&gt;
</i></b> wrote:</span></div><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><div><span class="e" id="q_113bf44153a88c50_3"> <br>&nbsp;&nbsp; Oleg,<br><br>&nbsp;&nbsp; As I mentioned to you, it is perfectly fine and easy to use the rules engine to validate your object tree as long as you instruct your users that they must write structured rules. What I mean is: as much as DSL templates allow you to write rules that resemble natural language, it is not really natural language. The main advice is to group objects and attributes together. So, instead of writing the rule as: 
<br><br>3. &quot;ServiceDescription&quot; contained
 inside &quot;ClientService&quot; with type &quot;BRONZE&quot; should has status &quot;ACCEPTED&quot;<br><br>&nbsp;&nbsp; They need to write the rule as:<br><br>3. If there is a ClientService whose type is &quot;BRONZE&quot; and a ServiceDescription contained inside it whose status is different of &quot;ACCEPTED&quot; then report error 
<br><br>&nbsp;&nbsp; As you can see, all the constraints/attributes are listed right after each object type. The above rule can be translated to:<br><br>rule &quot;3. Validate status for BRONZE&quot;<br>when<br>&nbsp;&nbsp;&nbsp; There is a ClientService 
<br>&nbsp;&nbsp;&nbsp; - whose type is &quot;BRONZE&quot;<br>&nbsp;&nbsp;&nbsp; There is a ServiceDescription<br>&nbsp;&nbsp;&nbsp; - contained inside it<br>&nbsp;&nbsp;&nbsp; - whose status is different of &quot;ACCEPTED&quot;<br>then<br>&nbsp;&nbsp;&nbsp; report error<br>end<br><br>&nbsp;&nbsp; As you can see, the rule definition is very close to the actual rule, if you take the appropriate care when writing it, having the attribute/constraints right after each object
 type. <br>&nbsp;&nbsp; Now, the DSL mapping will transform the above rule into:<br><br>rule &quot;3. Validate status for BRONZE&quot;<br> when<br> &nbsp;&nbsp;&nbsp; cs : ClientService( type == &quot;BRONZE&quot; )<br>&nbsp;&nbsp;&nbsp; sd : ServiceDescription( this memberOf 
cs.children, status != &quot;ACCEPTED&quot; )<br>then<br> &nbsp;&nbsp;&nbsp; # report error<br> end<br> <br>&nbsp;&nbsp; When you try to extrapolate the above example to your real business example, to make your DSL template completely  generic, you may find the need to explain to your users the concept of &quot;variable&quot; though.  
<br>&nbsp;&nbsp; So, what he would do is actually name each of your objects. Example (replace X and Y for any identifier that makes sense for the user):<br><br>3. If there is a ClientService named X whose type is &quot;BRONZE&quot; and a ServiceDescription name Y contained inside X whose status is different of &quot;ACCEPTED&quot; then report error
<br> <br>&nbsp;&nbsp; If the user names each object, it will be much easier to write
 generic templates, since your rule would become:<br><br>rule &quot;3. Validate status for BRONZE&quot;<br> when<br> &nbsp;&nbsp;&nbsp; There is a ClientService named X<br> &nbsp;&nbsp;&nbsp; - whose type is &quot;BRONZE&quot;<br> &nbsp;&nbsp;&nbsp; There is a ServiceDescription named Y
<br> &nbsp;&nbsp;&nbsp; - contained inside X<br> &nbsp;&nbsp;&nbsp; - whose status is different of &quot;ACCEPTED&quot;<br> then<br> &nbsp;&nbsp;&nbsp; report error<br> end<br> <br>&nbsp;&nbsp; And the DSL would translate that to:<br><br>rule &quot;3. Validate status for BRONZE&quot;
<br>  when<br>  &nbsp;&nbsp;&nbsp; X : ClientService( type == &quot;BRONZE&quot; )<br> &nbsp;&nbsp;&nbsp; Y : ServiceDescription( this memberOf X.children, status != &quot;ACCEPTED&quot; )<br> then<br>  &nbsp;&nbsp;&nbsp; # report error<br>  end<br>  <br>&nbsp;&nbsp; I don&#39;t think that it is too hard to explain to users that they need to name each of his objects and this adds a great flexibility to the rules when adding constraints between objects of the same type.
<br><br>
 &nbsp;&nbsp; The final (and hardest) problem you will face is that you mentioned that your objects may be any level deep inside each other and you want to abstract that from users. In this case I see only 2 options:<br><br>1. The first option is do what you are already doing and create a helper function that goes deep a hierarchy checking for the existence of the relationship.  
<br><br>2. The second option is to create a relationship object that will flatten your tree structure transforming it in a pure relational structure. This is a very common solution used in databases that do not support hierarchy queries. The idea is that you create an object with attributes parent, child and optionally the level (distance) between each other, and you assert a copy of it for each related object in your object hierarchy (in computational theory we call this set of objects the result of the transitive closure calculation). The result is really a trade-off between memory and query time.
 You need to check if for your use case, the performance gains and expressiveness flexibility pays off the memory cost. <br><br>&nbsp;&nbsp;&nbsp; Hope it helps.<br><br>&nbsp;&nbsp;&nbsp; []s<br>&nbsp;&nbsp;&nbsp; Edson<br> <br><div><span class="gmail_quote">2007/7/12, Oleg Yavorsky &lt;
<a href="mailto:oleg_yavorsky@yahoo.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">oleg_yavorsky@yahoo.com</a>&gt;:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
 Hi.<br><br>I&#39;ve got a lot of domain objects represented as instances of classes generated from XSD using Castor. My goal is to implement validation framework for whole tree which contains complex &quot;when&quot;&nbsp; conditions (see bellow). Furthermore, rules definitions should be very easy for unexperienced user to change so I need DSL. 
<br><br>Now I use reflection to assert whole tree of objects to working memory along with additional context information (like stack of parents associated
 with particular object). Then I use eval() in LHS to call boolean functions which performs checks in plain Java to see if object in particular context. But I think that this is ugly approach and it doesn&#39;t solve other cases. I&#39;m new to Drools but something tells me that  it can give me more elegant solution for such problems. 
<br><br>Here is an example of possible hierarchy (used XSD to better represent it). Make note that in production case similar hierarchy is more complex and has a lot of  objects. Sorry, but I don&#39;t know how to format it in email.
<br><br>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;schema xmlns=&quot;<a href="http://www.w3.org/2001/XMLSchema" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://www.w3.org/2001/XMLSchema
</a>&quot;<br>&nbsp;&nbsp;&nbsp; targetNamespace=&quot;<a href="http://www.example.org/ClientService" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.example.org/ClientService
 </a>&quot;<br>&nbsp;&nbsp;&nbsp; xmlns:tns=&quot;<a href="http://www.example.org/ClientService" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.example.org/ClientService</a>&quot;<br>&nbsp;&nbsp;&nbsp; elementFormDefault=&quot;qualified&quot;&gt; 
<br><br>&nbsp;&nbsp;&nbsp; &lt;complexType name=&quot;ClientService&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;sequence minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;element name=&quot;ClientInfo&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;complexType&gt; 
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;sequence&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;element name=&quot;Name&quot;  type=&quot;string&quot;/&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; &lt;element name=&quot;AccountID&quot; type=&quot;string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/sequence&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/complexType&gt; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/element&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;element name=&quot;ServiceDescription&quot; minOccurs=&quot;0&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;complexType&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;sequence&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;element name=&quot;Name&quot;  type=&quot;string&quot;/&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; &lt;element name=&quot;Status&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;simpleType&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;restriction base=&quot;string&quot;&gt; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;enumeration value=&quot;VERIFIED&quot;/&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;enumeration value=&quot;ACCEPTED&quot;/&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; &lt;enumeration value=&quot;DENIED&quot;/&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/restriction&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/simpleType&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/element&gt; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/sequence&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/complexType&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/element&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  &lt;/sequence&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;attribute name=&quot;Type&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
 &nbsp;&nbsp;&nbsp; &lt;simpleType&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;restriction base=&quot;string&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;enumeration value=&quot;BRONZE&quot; /&gt; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;enumeration value=&quot;SILVER&quot;/&gt;
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;enumeration value=&quot;GOLD&quot;/&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/restriction&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/simpleType&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/attribute&gt; <br>&nbsp;&nbsp;&nbsp;  &lt;/complexType&gt;<br>&lt;/schema&gt;
<br><br>And here are possible validation cases:<br><br>1. &quot;ClientInfo&quot; contained inside &quot;ClientService&quot; with type &quot;GOLD&quot; should
 have &quot;AccountID&quot; assigned. <br>2. &quot;ClientService&quot; with type &quot;BRONZE&quot; could have up to 3 &quot;ServiceDescription&quot;<br>3. &quot;ServiceDescription&quot; contained inside &quot;ClientService&quot; with type &quot;BRONZE&quot; should has status &quot;ACCEPTED&quot; 
<br>4. &quot;ClientInfo&quot; should have &quot;AccountID&quot; assigned if there is &quot;ServiceDescription&quot; with name &quot;SUBSCRIPTION&quot;<br><br>I&#39;d like to come up with rules definition that looks like this (for 3-d example): 
<br>rule &quot;Validate status for BRONZE&quot;<br>when<br>&nbsp;&nbsp;&nbsp; Validating ServiceDescription<br>&nbsp;&nbsp;&nbsp; Contained in ClientService having<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  type equals to &quot;BRONZE&quot;<br>then<br>&nbsp;&nbsp;&nbsp; # Validate status<br>end<br>
 <br>In this rule  ServiceDescription, ClientService and type literals could be anything else (so I don&#39;t need to create DSL for each  object in domain). But this could be easily done through&nbsp; reflection.<br><br>Any help with possible approach will&nbsp; be highly appreciated.
<br><span><br>Oleg.<br></span><span><div>           </div><hr size="1"><br>Вы уже с Yahoo!?  Испытайте обновленную и улучшенную. <a href="http://ru.mail.yahoo.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
Yahoo! Почту</a>!<div></div></span><br>_______________________________________________ <br>rules-users mailing list<br><a href="mailto:rules-users@lists.jboss.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> https://lists.jboss.org/mailman/listinfo/rules-users
</a><br><br></blockquote></div><br><br clear="all"><br>-- <br>&nbsp;&nbsp;Edson Tirelli<br>&nbsp;&nbsp;Software Engineer - JBoss Rules Core Developer<br>&nbsp;&nbsp;Office: +55 11 3529-6000<br>&nbsp;&nbsp;Mobile: +55 11 9287-5646 <br></span></div>&nbsp;&nbsp;JBoss, a division of Red Hat @ 
<a href="http://www.jboss.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">www.jboss.com</a> _______________________________________________<span class="q"><br>rules-users mailing list<br><a href="mailto:rules-users@lists.jboss.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
rules-users@lists.jboss.org</a><br><a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">https://lists.jboss.org/mailman/listinfo/rules-users
</a><br></span></blockquote><div><span class="e" id="q_113bf44153a88c50_7"><br><p> 
                </p><hr> 
Вы уже с Yahoo!?<br> 
Испытайте обновленную и улучшенную <a>Yahoo! Почту</a>!<p></p></span></div><br>_______________________________________________<br>rules-users mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org">
rules-users@lists.jboss.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users
</a><br><br></blockquote></div><br><br clear="all"><br>-- <br>&nbsp;&nbsp;Edson Tirelli<br>&nbsp;&nbsp;Software Engineer - JBoss Rules Core Developer<br>&nbsp;&nbsp;Office: +55 11 3529-6000<br>&nbsp;&nbsp;Mobile: +55 11 9287-5646<br>&nbsp;&nbsp;JBoss, a division of Red Hat @ 
<a href="http://www.jboss.com">www.jboss.com</a>