[rules-users] Need help in formulating rule (using list)

Wolfgang Laun wolfgang.laun at gmail.com
Wed Nov 11 05:29:29 EST 2009


Implied by the List representation are concepts such as "first",
"last", "predecessor"
and "successor". All of these may or may not be represented by the fields in
TimeEntry. This would be so if it is *guaranteed* that one TimeEntry's
start time is another one's end time; (and then you wouldn't even need them
being sorted). Failing this, an explicit representation by adding an ordinal
number or (as I prefer doing) a reference to the previous and next TimeEntry
might be considered.

But, either way, reasoning over pairs of TimeEntry objects (of one
Schedule) requires that they are all (at least temporarily) present as facts in
WM. Then, to be identified as TimeEntrys of a specific Schedule, they also
could have a reference to that.

It's a simple task to write a rule that fires for a new Schedule and creates
the extended TimeEntryPlus facts from its List<TimeEntry>. To avoid NPEs,
I'd add a dummy TimeEntryPlus object (type=="OFF") before and after. Then, you
could write rules

rule checkValidBreak1
when
    $break : TimeEntryPlus( type == "BREAK" )
    TimeEntryPlus( type == "WORK", succ == $break || pred == $break,
length <= 10 )
then
   System.out.println( "Short work b/a break " + ... $break ...,
$break.getSchedule() ... )
end

rule checkValidBreak2
when
    $break : TimeEntryPlus( type == "BREAK" )
    TimeEntryPlus( type == "WORK", succ == $break )
then
   System.out.println( "Can't start with a break " + ... $break ...,
$break.getSchedule() ... )
end

-W


On Wed, Nov 11, 2009 at 10:46 AM, psentosa <psentosa at yahoo.com> wrote:
>
> Hi Brody,
>
> I have had similar situation several times, and each time I had to modify my
> objects to avoid this, and it was really tedious.
> I'd also would like to know whether it is possible to have a "real solution"
> in this case without needing to change the base objects, but rather use the
> rule language to formulate this kind of business rule
>
> *why didn't I receive this mail through the mailing list? maybe you haven't
> subscribed yet?*
>
>
>
>
> Brody bach wrote:
>>
>> Hi all,
>>
>> I have an object which I use as main fact object, let's say "Schedule",
>> which has a list of objects called "TimeEntry"  --->
>>
>> class Schedule(){
>>      List<TimeEntry> entries;
>> }
>>
>> class TimeEntry(){
>>      String type;
>>      Date start;
>>      Date end;
>>      Integer length;
>> }
>>
>>
>> Now, if I insert Schedule into the WM, and would like to iterate over the
>> list of TimeEntry, due to the following business rule :
>>
>> For all time entries, if it is of type BREAK:
>> check whether a time entry of type WORK exist,
>> before and/or after this BREAK, which has only length of 10 minutes
>>
>> The content of the time entry list has been sorted before so they are all
>> in correct time order, e.g
>>
>> 09:00 - 09:10  WORK (10 min)
>> 09:10 - 09:30  BREAK (20 min)
>> 09:30 - 10:30  WORK  (60 min)
>> --> in this case, the rule should fire
>>
>> How can I define this rule? Some questions which I have:
>> - how do I check previous and next element of the current element?
>> - the length of the list isn't fix; it may contain 1, 2, or more entries.
>> So somehow in an attempt to access next element, it should take care of
>> the bound of the list. But should this be really part of the rule
>> definition?
>>
>> Many thanks in advance!
>>
>> Regards
>>
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/Need-help-in-formulating-rule-%28using-list%29-tp26284362p26298480.html
> Sent from the drools - user mailing list archive at Nabble.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