[rules-users] looping in drools

Sandhya Sree sandhyachinnaraj at gmail.com
Mon Mar 10 14:29:50 EDT 2014


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20140310/ab067a70/attachment.html 


More information about the rules-users mailing list