<div dir="ltr">My last comparison there may be problematic as well...too focused on the accumulate to see the obvious. I think that approach might still require an eval to check the size constraint on the list and we don&#39;t really want that. <div>
<br></div><div>What you could do instead, however, is insert the hashset into the working memory in that rule&#39;s consequence and have a 2nd rule that&#39;s just HashSet ( size &gt; 1 ) and its consequence does your actions and retracts the hashset back out of memory.</div>
<div><br></div><div>- J</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 22, 2014 at 9:39 AM, Jeremy Ary <span dir="ltr">&lt;<a href="mailto:jeremy.ary@gmail.com" target="_blank">jeremy.ary@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">err, typo there, add() and remove() should be using $myId instead of $id</div><div class="HOEnZb"><div class="h5">
<div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 22, 2014 at 9:38 AM, Jeremy Ary <span dir="ltr">&lt;<a href="mailto:jeremy.ary@gmail.com" target="_blank">jeremy.ary@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><pre style="color:rgb(51,51,51);font-size:15px;line-height:20.790000915527344px">I wonder if something like this might do it?</pre>

<pre><font color="#333333"><span style="font-size:15px;line-height:20.790000915527344px">when
    $set : HashSet ( ) from  accumulate ( ObjectA ( $myId : objectC.myId ),
                              init ( HashSet uniqueIds = new HashSet&lt;String&gt;(); ),
                              action ( uniqueIds.add($id); ),
                              reverse ( uniqueIds.remove($id); ),
                              result ( uniqueIds ) )
    HashSet ( this == $set, size &gt; 1 )
then
    // consequence
end<span><font color="#888888"><br></font></span></span></font></pre><span><font color="#888888"><pre><font color="#333333"><span style="font-size:15px;line-height:20.790000915527344px"><br>
</span></font></pre><pre><font color="#333333"><span style="font-size:15px;line-height:20.790000915527344px">- Jeremy</span></font></pre>
</font></span></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 22, 2014 at 8:42 AM, richardhands <span dir="ltr">&lt;<a href="mailto:richard.hands@uk.sopragroup.com" target="_blank">richard.hands@uk.sopragroup.com</a>&gt;</span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I’m trying to wrap my head around how to use a combination of accumulate<br>
from and collect from to get a count of how many unique items there are of a<br>
nested child property and run the RHS of a rule if it’s &gt; 1<br>
<br>
So I have<br>
<br>
Many of Object A<br>
<br>
Each one contains<br>
An object B<br>
An object C<br>
<br>
Object C contains a nested child property<br>
<br>
So<br>
<br>
A<br>
+-+-B<br>
   |<br>
   +-C – property myId<br>
<br>
(many of these in the working memory)<br>
<br>
The rule I’m working with right now is<br>
<br>
rule &quot;B must only be at 1 myId per day&quot;<br>
    when<br>
      $b : B()<br>
      $c : C()<br>
      $aList : ArrayList (size &gt; 1)<br>
        from collect(<br>
              A(b == $b, c.getDate() == $c.getDate()) )<br>
      eval ( RulesUtils.countIdsForC ($aList) &gt; 1 )<br>
   then<br>
     //fire some rules here<br>
end<br>
<br>
and the RulesUtils method is<br>
<br>
  public static int countIdsForC (List  aList)<br>
  {<br>
    Set&lt;Integer&gt; myIds = new HashSet&lt;Integer&gt;();<br>
    for (A a : aList)<br>
    {<br>
      myIds.add(a.getC().getMyId());<br>
    }<br>
    return myIds.size();<br>
  }<br>
<br>
<br>
Now I’m well aware that this is sub-optimal, and indeed is firing lots of<br>
extra times and is really (Really) slow.  I can vizualise that this should<br>
easily be possible with some combination of compound accumulate and collect<br>
statements, but for the life of me I can’t work out the correct arrangement<br>
(This is actually for use in an optaplanner ruleset so it will potentially<br>
be working on lots of data, over many iterations).<br>
<br>
Any suggestions on how to do this the way I know it needs to be, greatfully<br>
appreciated <br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://drools.46999.n3.nabble.com/how-to-count-distinct-nested-property-tp4030424.html" target="_blank">http://drools.46999.n3.nabble.com/how-to-count-distinct-nested-property-tp4030424.html</a><br>



Sent from the Drools: User forum mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>