[rules-users] looping in drools

Wolfgang Laun wolfgang.laun at gmail.com
Mon Mar 10 15:25:08 EDT 2014


Will you please, please, PLEASE have a short look at the Drools
documentation, available just for the effort of reading it? This will
tell you that "accumulate" and "from" are constructs to be used on the
left hand side of a rule and NOT on the right hand side, where (with
the exception of a few additional statements) everything is plain old
Java code.

Cheers
-WL

On 10/03/2014, Sandhya Sree <sandhyachinnaraj at gmail.com> wrote:
> i tried this :
>
> rule "new file"
> when
>     FileData($old : fileOld, $new : fileNew, fileOld != fileNew)
>     $newFile : String( this not memberOf $old ) from $new
> then
>     event : Event("file added", filename, new Date()) from $plus
>     Event.listOfEvents.add(event);
>     event.display();
> end
>
>
> this gives the following error:
> The method Event(String, File, Date) is undefined for the type
> Rule_new_file_0c90fff8d9b74b439ab79971b13b75ac
>  Syntax error on token "from", ; expected
>  $plus cannot be resolved to a type
>  Syntax error on token ".", ; expected
>  listOfEvents cannot be resolved
>
>
> On Tue, Mar 11, 2014 at 12:09 AM, Davide Sottara <dsotty at gmail.com> wrote:
>
>>  "from" will iterate for you, exactly like "accumulate" counted the
>> events.
>> If you need more details, please consult the documentation
>>
>>
>>
>> On 03/10/2014 07:29 PM, Sandhya Sree wrote:
>>
>> The problem here is even if multiple files are added, there is only one
>> event object created. i want to have like say if 3 new files are added, 3
>> event objects must be created, and all three event objects must be added
>> to
>> List<Event> listOfEvents.
>>
>>
>>  Thanks.
>>
>>
>> On Mon, Mar 10, 2014 at 11:48 PM, Davide Sottara <dsotty at gmail.com>
>> wrote:
>>
>>>  The "accumulates" should not be in the "then" part of a rule (see my
>>> previous email).
>>> That rule gave you two lists, in the variables $plus and $mins, that you
>>> can use in the consequence.
>>>
>>> If you need to process each file separately, you may want to do
>>> something
>>> like this:
>>>
>>> rule "new file"
>>>  when
>>>     FileData($old : fileOld, $new : fileNew, fileOld != fileNew)
>>>      $newFile : String( this not memberOf $old ) from $new
>>>  then
>>>    // create new event for a new file : new Event(...)
>>>    // add to list : Event.listOfEvents.add(...)
>>>  end
>>>
>>>  You'll need another rule for the files that have been removed.
>>>
>>> Notice that your Event class has no way to distinguish "added" from
>>> "removed" files, you may want to
>>> add a boolean or something there.
>>> Best
>>> Davide
>>>
>>>
>>> On 03/10/2014 07:04 PM, Sandhya Sree wrote:
>>>
>>>  hi,
>>> im new to drools..im trying to create a project as follows. i have a
>>> class called Monitor  which monitors a folder and creates two lists
>>> called
>>> fileOld( which is the list of filenames in that folder at time t1)  and
>>> fileNew(which is the list of filenames in that folder at time t2). i
>>> have
>>> another class called FileData which contains two members fileOld and
>>> fileNew (list of strings) with getters,setters and constructor. fileOld
>>> and
>>> fileNew from Monitor Class are passed to FileData class.
>>>
>>>  i also have another class called Event which is as follows:
>>>  public class Event {
>>>   public static String name;
>>>  private  File source;
>>>  private  Date timeStamp;
>>>         public  static  List<Event> listOfEvents = new
>>> ArrayList<Event>();
>>>
>>>  public Event(String name, File source, Date timeStamp) {
>>>  this.name = name;
>>>  this.source = source;
>>>  this.timeStamp = timeStamp;
>>>  }
>>>    public String getName() {
>>>  return name;
>>>  }
>>>  public void setName(String name) {
>>>  this.name = name;
>>>  }
>>>  public File getSource() {
>>>  return source;
>>>  }
>>>  public void setSource(File source) {
>>>  this.source = source;
>>>  }
>>>  public Date getTimeStamp() {
>>>  return timeStamp;
>>>  }
>>>  public void setTimeStamp(Date timeStamp) {
>>>  this.timeStamp = timeStamp;
>>>  }
>>>
>>>  now i have to compare these two lists(fileOld and fileNew) in a rule
>>> file and if they are not equal i have to create an event object for
>>> every
>>> file added and deleted and put it in the List<Event> listOfEvents.
>>>
>>>  here is my rule file:
>>>
>>>  rrule "files are equal"
>>>     when
>>>          FileData( fileOld == fileNew)
>>>     then
>>>        System.out.println("files are equal");
>>>     end
>>>
>>>
>>>  rule "files not equal"
>>> when
>>>     FileData($old : fileOld, $new : fileNew, fileOld != fileNew)
>>>
>>>  then
>>>    accumulate( $s : String( this not memberOf $old ) from $new, $plus :
>>> collectList( $s ) )
>>>    accumulate( $t : String( this not memberOf $new ) from $old, $mins :
>>> collectList( $t ) )
>>>    System.out.println("files added:" + $plus );
>>>    System.out.println( "files deleted:" + $mins );
>>>  end
>>>
>>>
>>>
>>>  how can i loop through each of the file added or deleted and create an
>>> Event Class object for every file added and deleted  and finally add all
>>> the created objects to List<Event> listOfEvents..
>>>
>>>
>>>  Thanks.
>>>
>>>
>>>  _______________________________________________
>>> rules-users mailing
>>> listrules-users at lists.jboss.orghttps://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
>>>
>>
>>
>>
>> _______________________________________________
>> rules-users mailing
>> listrules-users at lists.jboss.orghttps://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
>>
>


More information about the rules-users mailing list