[rules-users] MVEL and Maps

Leonardo Gomes leonardo.f.gomes at gmail.com
Sun Nov 8 12:53:33 EST 2009


Wolfgang / Edson,

Thank you for the inputs.

We're using Drools in another three different subsystems, and in all
cases we already use regular java beans in our applications, but to
solve this particular problem I'll probably start with regular evals
and then migrate to Fact Types. We're actually using Drools 4.0.7, so
I think we'd have to do that like this:
http://orangemile.blogspot.com/2008/07/drools-fact-template-example.html

Regarding the problem with dates, even when using java beans, what
options would I have other than accepting the limitations of that
syntax sugar or to provide a static method, casting and converting
arguments, returning a boolean, and call this using an inline eval.

Thanks again,

Leo.

2009/11/8 Edson Tirelli <ed.tirelli at gmail.com>:
>
>    Wolfgang,
>
>    As you know, there are a ton of problems when you go down the "date"
> route. Probably the reason java never had a decent built-in date framework
> (fingers crossed for JSR-310).
>
>    Anyway, back to the point, the syntax sugar for string-based strings is a
> really simple way for people to use really simple dates in their rules. But,
> yes, with that, you can't define which calendar, timezone, daylight savings,
> etc, rules should use. It will just use the defaults for the JVM instance.
> You will probably never see *me* using that. But there are people that just
> want something that simple and in this case, it is there.
>
>    MVEL page is:
>
> http://mvel.codehaus.org/
>
>    I don't think that mvel has anything specific to dates, but I might be
> wrong.
>
>    The only thing "not available" in Drools are the control structures
> (if/while/etc) that we disable explicitly.
>
>     Edson
>
> 2009/11/8 Wolfgang Laun <wolfgang.laun at gmail.com>
>>
>> For the sake of clarification: Are you saying that there is, or should be,
>> some magic which would make a constraint like
>>    Map ( this["departureDate"] == "07-Jan-2009" )
>> behave as a comparison between two java.util.Date values if the LHS Object
>> is of this type? And also if it is a GregorianCalendar object?
>>
>> Since even you are "not sure" regarding MVEL: What is the definite
>> reference for MVEL in Drools? I have a vague recollection of reading
>> somewhere that not all of MVEL is available...
>>
>> -W
>>
>>
>> 2009/11/8 Edson Tirelli <ed.tirelli at gmail.com>
>>>
>>>      Ok, without discussing the merits of using (or not) maps, the reason
>>> you are having problems with dates is that the "string-based" date is syntax
>>> sugar in Drools parser/compiler. Although, when you create expressions using
>>> nested accessors or [] for collection/map element access, drools wraps the
>>> whole thing into an "eval" and delegates to MVEL. We do some transformations
>>> to bridge the gap between drools operators and mvel operators, but I don't
>>> think drools has anything to "transform" the string-based date into
>>> something mvel understands.
>>>
>>>      Having said that, we should probably add this functionality to the
>>> "MVELDumper" class for better integration. Would you please open a jira for
>>> that? Also, patches are welcome in case someone would like to help. This is
>>> an isolated part of the code and easy to understand.
>>>
>>>     Meanwhile, best you can do is avoid the use of this Map syntax +
>>> string-based dates, or write your expressions directly in a way the mvel
>>> understands (not sure mvel has a special syntax for dates).
>>>
>>>     Same explanation for the NPE.
>>>
>>>     Edson
>>>
>>> 2009/11/8 Leonardo Gomes <leonardo.f.gomes at gmail.com>
>>>>
>>>> Hi Wolfgang,
>>>>
>>>> Here's the context:
>>>>
>>>> We're replacing a very basic home-made rule engine with drools. Today,
>>>> fact data can be added dynamically b/c for all rules our fact is
>>>> nothing more than a Map and we can register new fact data through an
>>>> UI where you say what's the key and what's the type of the value.
>>>> Rules can also be created dynamically and operate against those newly
>>>> created attributes of my fact.
>>>>
>>>> Incoming requests are key / value structures and we have a service
>>>> that will look at the list of registered fact data and try to find
>>>> that information in the incoming request by looking for the keys and
>>>> getting the corresponding values, then populating the Map I just
>>>> talked about which will be injected in the working memory for
>>>> evaluation against our rules.
>>>>
>>>> That's why, in Drools, I need to have my rules accessing data from a
>>>> Map. By using MVEL, as I said, it works fine with strings, integers
>>>> and list of strings, but I'm having problems with dates and would like
>>>> some advice on that case.
>>>>
>>>> Here's another example of things that work:
>>>>
>>>> rule "test rule"
>>>>        when
>>>>                Map ( this["airportCode"] == "GIG"
>>>>                        , this["numberOfPassengers"] > 1
>>>>                        , this["class"] in ("BUSINESS", "FIRST CLASS") )
>>>>        then
>>>>                m.addMessage("rule matched");
>>>> end
>>>>
>>>> And here's where I have problems,
>>>>
>>>> Map ( this["departureDate"] ) == "07-Jan-2009" )
>>>>
>>>>
>>>> Thanks a lot,
>>>> Leo.
>>>>
>>>>
>>>>
>>>> On Sun, Nov 8, 2009 at 10:56 AM, Wolfgang Laun <wolfgang.laun at gmail.com>
>>>> wrote:
>>>> > On Sat, Nov 7, 2009 at 6:00 PM, Leonardo Gomes
>>>> > <leonardo.f.gomes at gmail.com>
>>>> > wrote:
>>>> >>
>>>> >> Hello,
>>>> >>
>>>> >> I'm feeding my working memory with Maps (unfortunately, this is a
>>>> >> requirement and I can't use beans).
>>>> >>
>>>> >
>>>> > Before people start racking their brains to overcome all the resulting
>>>> > difficulties: Could you please explain what this requirement is meant
>>>> > to
>>>> > achieve?
>>>> > -W
>>>> >
>>>> >
>>>> >>
>>>> >> To access the values in this maps I use the following MVEL expression
>>>> >> :
>>>> >>
>>>> >>                Map ( this["airportCode"] == "GIG"
>>>> >>                        , this["numberOfPassengers"] > "1"
>>>> >>                        , this["class"] memberOf ("BUSINESS", "FIRST")
>>>> >> )
>>>> >>
>>>> >> And it works fine, except for dates where I get a ClassCastException,
>>>> >> even tough I'm using the right pattern:
>>>> >>
>>>> >> Map ( this["departureDate"] > "07-Nov-2009" )   --- fails
>>>> >>
>>>> >> Is there anything I need to do to make the dynamic cast of dates
>>>> >> work,
>>>> >> in this particular case?
>>>> >>
>>>> >> --
>>>> >>
>>>> >> Morever, I get NPEs if there's no value for a given key. For example,
>>>> >> the expression Map ( this ["name"] == 'Leonardo' ) would fail if
>>>> >> there's no entry with "name" as the key.
>>>> >>
>>>> >> Any help is welcome!
>>>> >>
>>>> >> Thanks in advance,
>>>> >> Leo.
>>>> >> _______________________________________________
>>>> >> rules-users mailing list
>>>> >> rules-users at lists.jboss.org
>>>> >> https://lists.jboss.org/mailman/listinfo/rules-users
>>>> >
>>>> >
>>>>
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>>
>>> --
>>>  Edson Tirelli
>>>  JBoss Drools Core Development
>>>  JBoss by Red Hat @ www.jboss.com
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
>
> --
>  Edson Tirelli
>  JBoss Drools Core Development
>  JBoss by Red Hat @ www.jboss.com
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>




More information about the rules-users mailing list