[rules-users] Working with collections

Marcin Krol mrkafk at gmail.com
Wed Oct 1 11:42:01 EDT 2008


Michael Neale wrote:
> The days list property needs to be a Collection (at the moment I think
> it uses the collection semantics in java, which sadly arrays are not
> part of the collections family).
> 
> You can do Arrays.asList() to convert it to a list in the getter
> method, and it should work. If you however make it a hashset, it in
> theory will be slightly faster then most other collection types.

Thanks for answer, Michael, but it didn't work:

private String[] dl = {"Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday"};
			
public WorkDays() {
}
			
public List<String> getDaylist() {
  return Arrays.asList(this.dl);
}

I even coded this as List explicitly:

public class WorkDays {
private List<String> wd;
		
public WorkDays() {
  this.wd = new ArrayList<String>();
  this.wd.add("Monday");
  this.wd.add("Tuesday");
  this.wd.add("Wednesday");
  this.wd.add("Thursday");			
  this.wd.add("Friday");			
}
	
	
public List<String> getWorkDays() {
  return this.wd;
}

It seems like Drools can't instantiate the List in question, because 
when I leave only this as condition of the rule, it still cannot fire:

import com.sample.WorkDays;

rule "Rule 1"
  when
     $wd : WorkDays()
  then
     System.out.println("Rule 1 fired.");
end



> 
> On Sat, Sep 27, 2008 at 2:04 AM, Marcin Krol <mrkafk at gmail.com> wrote:
>> Hello everyone,
>>
>> I'm a beginner in Drools, so please account for that. :-)
>>
>> How do I use memberOf operator when working with collections? Depending on a
>> day of week, I want to check if a day is a working day or weekend day.
>>
>> I tried this:
>>
>> package com.sample
>> dialect "mvel"
>> import com.sample.ThermostatTimeData;
>> import com.sample.Thermostat.WorkDay;
>> import com.sample.Thermostat.WorkDays;
>>
>>
>> rule "Rule 1"
>>
>> when
>>        $wd : WorkDays()
>>        $ttd : ThermostatTimeData( day memberOf $wd.daylist )
>>        //$ttd: ThermostatTimeData ( day == "Monday" || day == "Tuesday" ||
>> day == "Wednesday" || day == "Thursday" || day == "Friday" )
>>
>> then
>>        insert( new WorkDay() );
>>        System.out.println("inserting new workday");
>> end
>>
>>
>> with this in my Thermostat class:
>>
>> public static class WorkDays {
>>  private String[] dl = {"Monday", "Tuesday", "Wednesday", "Thursday",
>> "Friday"};
>>
>>  public WorkDays() {
>>  }
>>
>>  public String[] getDaylist() {
>>        return this.dl;
>>  }
>> }
>>
>>
>> Now the above rule works fine when I select days using the regular fact
>> field selection in Drools:
>>
>> $ttd: ThermostatTimeData ( day == "Monday" || day == "Tuesday" || day ==
>> "Wednesday" || day == "Thursday" || day == "Friday"
>>
>> However, when I try to access WorkDays.dl or WorkDays.daylist(), the rule
>> doesn't fire (the program output is empty).
>>
>> Regards,
>> MK
>>
>> _______________________________________________
>> 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