<br> Oleg,<br><br> 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. "ServiceDescription" contained inside "ClientService" with type "BRONZE" should has status "ACCEPTED"<br><br> They need to write the rule as:<br><br>3. If there is a ClientService whose type is "BRONZE" and a ServiceDescription contained inside it whose status is different of "ACCEPTED" then report error
<br><br> 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 "3. Validate status for BRONZE"<br>when<br> There is a ClientService
<br> - whose type is "BRONZE"<br> There is a ServiceDescription<br> - contained inside it<br> - whose status is different of "ACCEPTED"<br>then<br> report error<br>end<br><br> 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> Now, the DSL mapping will transform the above rule into:<br><br>rule "3. Validate status for BRONZE"<br>
when<br>
cs : ClientService( type == "BRONZE" )<br> sd : ServiceDescription( this memberOf cs.children, status != "ACCEPTED" )<br>then<br>
# report error<br>
end<br>
<br> 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 "variable" though.
<br> 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 "BRONZE" and a
ServiceDescription name Y contained inside X whose status is different of
"ACCEPTED" then report error<br>
<br> If the user names each object, it will be much easier to write generic templates, since your rule would become:<br><br>rule "3. Validate status for BRONZE"<br>
when<br>
There is a ClientService named X<br>
- whose type is "BRONZE"<br>
There is a ServiceDescription named Y<br>
- contained inside X<br>
- whose status is different of "ACCEPTED"<br>
then<br>
report error<br>
end<br>
<br> And the DSL would translate that to:<br><br>rule "3. Validate status for BRONZE"<br>
when<br>
X : ClientService( type == "BRONZE" )<br>
Y : ServiceDescription( this memberOf X.children, status != "ACCEPTED" )<br>
then<br>
# report error<br>
end<br>
<br> I don'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>
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> Hope it helps.<br><br> []s<br> Edson<br> <br><div><span class="gmail_quote">2007/7/12, Oleg Yavorsky <<a href="mailto:oleg_yavorsky@yahoo.com">oleg_yavorsky@yahoo.com</a>>:</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'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 "when" 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't solve other cases. I'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't know how to format it in email.<br><br><?xml version="1.0" encoding="UTF-8"?><br><schema xmlns="<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>"<br> targetNamespace="<a href="http://www.example.org/ClientService" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.example.org/ClientService
</a>"<br> xmlns:tns="<a href="http://www.example.org/ClientService" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.example.org/ClientService</a>"<br> elementFormDefault="qualified">
<br><br> <complexType name="ClientService"><br> <sequence minOccurs="0" maxOccurs="1"><br> <element name="ClientInfo"><br> <complexType>
<br> <sequence><br> <element name="Name"
type="string"/><br> <element name="AccountID" type="string" minOccurs="0" maxOccurs="1"/><br> </sequence><br> </complexType>
<br> </element><br> <element name="ServiceDescription" minOccurs="0"><br> <complexType><br> <sequence><br> <element name="Name"
type="string"/><br> <element name="Status"><br> <simpleType><br> <restriction base="string">
<br> <enumeration value="VERIFIED"/><br> <enumeration value="ACCEPTED"/><br>
<enumeration value="DENIED"/><br> </restriction><br> </simpleType><br> </element>
<br> </sequence><br> </complexType><br> </element><br>
</sequence><br> <attribute name="Type"><br> <simpleType><br> <restriction base="string"><br> <enumeration value="BRONZE" />
<br> <enumeration value="SILVER"/><br> <enumeration value="GOLD"/><br> </restriction><br> </simpleType><br> </attribute>
<br>
</complexType><br></schema><br><br>And here are possible validation cases:<br><br>1. "ClientInfo" contained inside "ClientService" with type "GOLD" should have "AccountID" assigned.
<br>2. "ClientService" with type "BRONZE" could have up to 3 "ServiceDescription"<br>3. "ServiceDescription" contained inside "ClientService" with type "BRONZE" should has status "ACCEPTED"
<br>4. "ClientInfo" should have "AccountID" assigned if there is "ServiceDescription" with name "SUBSCRIPTION"<br><br>I'd like to come up with rules definition that looks like this (for 3-d example):
<br>rule "Validate status for BRONZE"<br>when<br> Validating ServiceDescription<br> Contained in ClientService having<br> type equals to "BRONZE"<br>then<br> # Validate status<br>end<br>
<br>In this rule ServiceDescription, ClientService and type literals could be anything else (so I don't need to create DSL for each
object in domain). But this could be easily done through reflection.<br><br>Any help with possible approach will be highly appreciated.<br><span class="sg"><br>Oleg.<br></span><span class="ad"><p>
</p><hr size="1"><br>Вы уже с Yahoo!?
Испытайте обновленную и улучшенную. <a href="http://ru.mail.yahoo.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Yahoo! Почту</a>!<p></p></span><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> Edson Tirelli<br> Software Engineer - JBoss Rules Core Developer<br> Office: +55 11 3529-6000<br> Mobile: +55 11 9287-5646
<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a>