[rules-users] How to empty the Working memory

Wolfgang Laun wolfgang.laun at gmail.com
Mon Mar 12 09:34:38 EDT 2012


You can save the FactHandle returned from the insert() and retract that.

Or you can retrieve the FactHandle via the WorkingMemorEntryPoint
method getFactHandle( Object obj ).

-W



On 12/03/2012, Spade87 <johan.spade at gmail.com> wrote:
> Using that rule worked, sort of... It correctly removes all of the objects
> in
> the working memory. The problem is that it clears everything... If you use
> mule together with drools the drools module will send back whatever you sen
> to it to mule when there are no more rules fireing. If I delete everything
> as a last step then I wont get my modified data back. Right now i have one
> method that takes a WorkingMemory and a collection and inserts everything
> from the collection in the working memory.
>
> *Rule*
> rule 'addAllFromCollection'
> when
> 	c : MyCollection ( newCollection == true )
> then
> 	DroolsUtil.insertCollection(drools.workingMemory, c )
> end
>
> *JavaCode*
> public static void insertCollection(WorkingMemory workingMemory, Collection
> collection) {
> 	Iterator iterator = collection.iterator();
> 	while (iterator.hasNext()) {
> 		workingMemory.insert(iterator.next());
> 	}
> }
>
> Basically what i want to do is write a corresponding function to retract
> everything that is old
>
> *Rule*
> rule 'retractAllFromCollection'
> when
> 	c : MyCollection ( newCollection == false )
> then
> 	DroolsUtil.retractCollection(drools.workingMemory, c )
> end
>
> *JavaCode*
> public static void retractCollection(WorkingMemory workingMemory, Collection
> collection) {
> 	Iterator iterator = collection.iterator();
> 	while (iterator.hasNext()) {
> 		workingMemory.retract(iterator.next());
> 	}
> }
>
> The problem is that the method workingMemory.retract() doesn't take a
> argument but a FactHandle as an argument but if i try something like
>
> *JavaCode*
> public static void retractCollection(WorkingMemory workingMemory) {
> 	Collection collection = workingMemory.getFactHandles();
> 	Iterator iterator = collection.iterator();
> 	while (iterator.hasNext()) {
> 		workingMemory.retract(iterator.next());
> 	}
> }
>
> I get a rather strange exception
> java.lang.UnsupportedOperationException: this is implementedby
> StatefulKnowledgeImpl
> 	at
> org.drools.reteoo.ReteooWorkingMemory.getFactHandles(ReteooWorkingMemory.java:363)
> 	at
> se.sunstone.ebs.mama.oneqc.drools.DroolsUtil.insertCollection(DroolsUtil.java:21)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>
> Any thoughts?
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/How-to-empty-the-Working-memory-tp3818693p3819245.html
> Sent from the Drools: User forum 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