<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
The WM is thread safe, on all insert/update/delete we apply a lock
making sure that not more than one operation can happen. However we do
not apply this lock when the iteraters are used externally via users -
so while iterating any working memroy actions should be avoided. For
the minute I think you would have to do it in two stages, iterate and
store the objects you wish to change in a list. Then iterate that list
and remove/modiy them via the normal working memory api. You can't
remove a fact via unsafe code, the workingmemory.retract(...) is thread
safe.<br>
<br>
Mark<br>
<br>
The iterators do not have "remove" so cannot damage the internal
"assert table" but if you change an underlying object and don't notify<br>
Michael Anstis wrote:
<blockquote
 cite="mid:fdd6e9c40710051240jd73e20cm465e87b8c4309c3e@mail.gmail.com"
 type="cite">
  <div>Excuse me if this would be best posted to the developer list...</div>
  <div>&nbsp;</div>
  <div>...but let me undertstand: The Fact table maintained for a
WorkingMemory is not thread safe(?); however WorkingMemories are thread
safe - suggesting Shadow Facts and (excuse my vague terminolgy) Node
Memories are the thread safe aspect of working memories - the Fact
table containing references to the original inserted objects not the
Shadows. So if a Fact is removed using my "unsafe" code it will cause
Shadow Facts to become disconnected from their original non-shadowed
Fact? Would using ThreadLocal storage for the Fact table (together&nbsp;with
node memories and shadow facts) mean a complete thread-safe Drools? I
don't mean this to become a tutorial in either Drools threading or
general java thread safety!! (I don't think there's a category on the
"how to have your emails ignored" listing for this). I just want to
increase my awareness of (Drools) threading issues... and this proves a
good example.
  <br>
  <br>
&nbsp;</div>
  <div><span class="gmail_quote">On 05/10/2007, <b
 class="gmail_sendername">Mark Proctor</b> &lt;<a moz-do-not-send="true"
 href="mailto:mproctor@codehaus.org">mproctor@codehaus.org</a>&gt;
wrote:</span>
  <blockquote class="gmail_quote"
 style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
    <div bgcolor="#ffffff" text="#000000">Anstis, Michael (M.) wrote:
    <blockquote type="cite"><span class="q">
      <pre>Wouldn't the best approach be to get the FactHandles iterator and retact
them from working memory rather than removing them through the iterator?

Iterator itr = wm.iterateFactHandles();
While(itr.next()) {
        FactHandle h = itr.next();
        wm.retract(h);
}

This would ensure truth maintenance is preserved.
  </pre>
      </span></blockquote>
Actually I'm not sure that would be safe.... the objects and the
handles are in the same hashtable. Those internal data structures where
built for performance and lightweightness, not thread safeness and
mutability. If you actually look we have an internal, fast, iterator
which we simple adapt to a slower java.util.Iterator. At the moment
none of our iterators are thread safe, but I do see a valid use case
here, we will have to think on it for the next major release - cleam
implementation patch welcome with unit tests :) I'm less concerned
about the iterator adapter performance, but I cannot compromise on the
performance of our internal iterators.
    <br>
    <br>
&nbsp;&nbsp;&nbsp; public static class HashTableIterator<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; implements<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Iterator {<br>
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private static final long serialVersionUID = 400L;<br>
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private AbstractHashTable hashTable;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Entry[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table;
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; row;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private Entry&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry;<br>
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public HashTableIterator(final AbstractHashTable hashTable) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.hashTable = hashTable;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* (non-Javadoc)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @see org.drools.util.Iterator#next()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Object next() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( this.entry == null ) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // keep skipping rows until we come to the end, or find
one that is populated
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ( this.entry == null ) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.row++;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( this.row == this.length ) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.entry = this.table[this.row];<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.entry = this.entry.getNext();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( this.entry == null ) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.entry = (Entry) next();
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this.entry;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* (non-Javadoc)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * @see org.drools.util.Iterator#reset()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void reset() {
    <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.table = this.hashTable.getTable();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.length = this.table.length;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.row = -1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.entry = null;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
    <blockquote type="cite">
      <div><span class="e" id="q_11570ff216e4caf0_3">
      <pre>-----Original Message-----
From: <a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">rules-users-bounces@lists.jboss.org</a>
[<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users-bounces@lists.jboss.org" target="_blank">mailto:rules-users-bounces@lists.jboss.org</a>] On Behalf Of Godmar Back
Sent: 05 October 2007 16:40
To: Rules Users List
Subject: Re: [rules-users] does WorkingMemory.iterator support remove()?

Thanks - consider supporting it for efficiency. (Otherwise, removing a
set of facts from working memory requires a temporary container to
hold the facts to be removed.)

 - Godmar

On 10/5/07, Mark Proctor <a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:mproctor@codehaus.org" target="_blank">&lt;mproctor@codehaus.org&gt;</a> wrote:
  </pre>
      <blockquote type="cite">
        <pre>Godmar Back wrote:
    </pre>
        <blockquote type="cite">
          <pre>Does the iterator returned by WorkingMemory.iterator support remove()?

I checked the javadoc and the Drools manual, but may have missed it.

      </pre>
        </blockquote>
        <pre>If you try it you'll get an exception thrown
    </pre>
      </blockquote>
      <pre>"UnsupportedOperationException"
  </pre>
      <pre><a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="http://anonsvn.labs.jboss.com/labs/jbossrules/trunk/drools-core/src/main/jav"
 target="_blank">http://anonsvn.labs.jboss.com/labs/jbossrules/trunk/drools-core/src/main/jav
</a>
a/org/drools/util/JavaIteratorAdapter.java
  </pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre>Please answer and augment documentation.

 - Godmar
_______________________________________________
rules-users mailing list
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="https://lists.jboss.org/mailman/listinfo/rules-users"
 target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>


      </pre>
        </blockquote>
        <pre>_______________________________________________
rules-users mailing list
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="https://lists.jboss.org/mailman/listinfo/rules-users"
 target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>

    </pre>
      </blockquote>
      <pre>_______________________________________________
rules-users mailing list
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="https://lists.jboss.org/mailman/listinfo/rules-users"
 target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre>
      </span></div>
      <pre><hr size="4" width="90%"><span class="q">
_______________________________________________
rules-users mailing list
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="https://lists.jboss.org/mailman/listinfo/rules-users"
 target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </span></pre>
    </blockquote>
    <br>
&nbsp;</div>
    <br>
_______________________________________________<br>
rules-users mailing list<br>
    <a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
    <a moz-do-not-send="true"
 onclick="return top.js.OpenExtLink(window,event,this)"
 href="https://lists.jboss.org/mailman/listinfo/rules-users"
 target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users
    </a><br>
    <br>
  </blockquote>
  </div>
  <br>
  <pre wrap="">
<hr size="4" width="90%">
_______________________________________________
rules-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre>
</blockquote>
<br>
</body>
</html>