Results 1 to 5 of 5

Thread: Yet another Rule...

  1. #1
    Join Date
    Aug 2005
    Location
    London (the English one!)
    Posts
    378

    Default Yet another Rule...

    Hi

    I really wishes there was a clear instruction/manual for Constraint as, yet again, my poor brain fried up...

    I want to be able to specify these 2 types of constraints, which seem easy enough:

    propertyA is required if propertyB=x AND propertyC=y
    also
    propertyA is required if propertyB=x OR propertyC=y

    I have tried the following without success:

    Code:
    new RequiredIfTrue("propertyB", and(eq("propertyB", y), eq("propertyC", y))
    ditto for the OR, it is always false.
    Code:
    new RequiredIfTrue("propertyB", or(eq("propertyB", y), eq("propertyC", y))
    I have also tried this for the 'and'
    Code:
    new RequiredIfTrue("propertyA", new ConditionalPropertyConstraint(eq("propertyB", x), eq("propertyC", y)))
    Can anyone help? why does and(eq("propertyB", y), eq("propertyC", y) always seems to return false???

    I then seem to have found a solution, which seems way over-complicated:
    Code:
    new RequiredIfTrue("propertA", new CompoundPropertyConstraint(new And(new PropertyConstraint[] {
                            eq("propertyB", x, eq("propertyC", y) }))));
    What is the difference between a "new And(...." and "and(..." ? Why does not an 'and' seem to use CompoundPropertyConstraint?

    Whoever knows all this... please write a blog... :-) Thanks!

    Many thanks in advance.

    Benoit

  2. #2

    Default

    As a first, there are some minor errors in your samples (first property is propertyA, missed out on one ')' at the end). I tried the first two as I was baffled that these didn't work. I used strings to match and noticed that they acted as I expected: for the first constraint I got a required on propertyA when B and C were exactly the given string, for the second constraint I got a required on propertyA when B or C was the given string.

    Now as a side remark: if you're using objects, make sure that the equals is correctly implemented or that the identity check is used.

    There is actually no difference between and() and new And(). The Constraints class provides a number of shortcut methods that are easy to use. You could however create each constraint on your own using the constraint objects directly.

    The and() results in a simple constraint. In some cases each part of the and() is a propertyConstraint on it's own. You can then wrap this constraint in a CompoundPropertyConstraint (remember that at the end you need to have a PropertyConstraint to be used in the framework). At other times, you want the and() to contain constraints that apply to one property. You can then use the normal PropertyConstraint in combination with the and().

    I hope this clarifies something. If you still experience problems with the constraints, please elaborate.

    Kind Regards,
    Jan

  3. #3
    Join Date
    Aug 2005
    Location
    London (the English one!)
    Posts
    378

    Default

    Hi Jan

    Thanks for replying. Unfortunately, the typos on missing ) and propertyA were only in my post on the forum...

    and if that works for you, the only difference I can think of is that propertyB is an enum and my eq is eq("propertyB", MyEnum.VALUE_TO_CHECK). I'd assume that the equals would work.

    We only use int, enum and Strings in our constraints.

    I'll try again to simplify the
    Code:
    new RequiredIfTrue("propertA", new CompoundPropertyConstraint(new And(new PropertyConstraint[] {
                            eq("propertyB", x, eq("propertyC", y) }))));
    to the followinf, do you think it should work?

    Code:
    new RequiredIfTrue("propertA", and(eq("propertyB", x), eq("propertyC", y) ))
    A cookbook would be great... to explain differences between PropertyConstraint and Constraint for instance. Examples of these kind of combinations would point in the best direction as constraint can sometimes become too convoluted...

    Many thanks!

    Kind regards

    Benoit

    PS: I have not forgotten that I have to comment on the "clear dirty flag" JIRA... I need more hours in a day...

  4. #4

    Default

    Quote Originally Posted by benoitx View Post
    Thanks for replying. Unfortunately, the typos on missing ) and propertyA were only in my post on the forum...
    Damn... that would have been too easy!
    Quote Originally Posted by benoitx View Post
    and if that works for you, the only difference I can think of is that propertyB is an enum and my eq is eq("propertyB", MyEnum.VALUE_TO_CHECK). I'd assume that the equals would work.

    We only use int, enum and Strings in our constraints.
    Have you tried putting a breakpoint in the equals method of your objects to check if it's actually used? Or try putting one in ObjectUtils.nullSafeEquals() and see if you get to that point when changing a value.
    Quote Originally Posted by benoitx View Post
    I'll try again to simplify the
    Code:
    new RequiredIfTrue("propertA", new CompoundPropertyConstraint(new And(new PropertyConstraint[] {
                            eq("propertyB", x, eq("propertyC", y) }))));
    Ok, quick analysis: the CompoundPropertyConstraint isn't really needed as it only makes sure that you have a PropertyConstraint (not required here) and that the inner compoundConstraint has only PropertyConstraints. So that's one too much. The new And is actually the same as a the Constraints.and() methods and you have chosen the array variant instead of the two-argument method, which is fine. But when taking this into account, you should get the same result as:
    Quote Originally Posted by benoitx View Post
    Code:
    new RequiredIfTrue("propertA", and(eq("propertyB", x), eq("propertyC", y) ))
    If this is not the case, try to send a simple example that shows this difference. I've not tested this in detail, this is just how it should be.
    Quote Originally Posted by benoitx View Post
    A cookbook would be great... to explain differences between PropertyConstraint and Constraint for instance. Examples of these kind of combinations would point in the best direction as constraint can sometimes become too convoluted...
    I'll see what I can do about that...
    Quote Originally Posted by benoitx View Post
    PS: I have not forgotten that I have to comment on the "clear dirty flag" JIRA... I need more hours in a day...
    Yeah, I get what you're saying. If someone knows a way to add some hours to a day or has a cloning machine, please let me know.

    Kind Regards,
    Jan

  5. #5
    Join Date
    Aug 2004
    Location
    San Francisco
    Posts
    423

    Default

    Hi,

    just my 2 cents here. I too struggle with the rule system. My applications don't typically have a lot of forms so I don't use the rules and validation system a lot but when I need it I appreciate its power. But, I always struggle to get anything complex working.

    When I read this post a couple of days ago my thought was wouldn't a cookbook of working examples be a great way to document this feature, and Benoit just suggested the same thing. I'm sure just collecting together all rule examples from the various users of the framework would help a great deal. I know I'd be happy to contribute my, albeit limited, examples.

    Jonny

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •