Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Agent, Actions, Rules & Automation >> collect_if --- seeking greater efficiency
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1213701593

Message started by Loryn on Jun 17th, 2008, 7:19am

Title: collect_if --- seeking greater efficiency
Post by Loryn on Jun 17th, 2008, 7:19am

In an Agent Action, I use the following pattern:


Code:
Field1(agent) = count(collect_if(child,$TextFrom="Number1",$Name));
Field2(agent) = count(collect_if(child,$TextFrom="Number2",$Name));
Field3(agent) = count(collect_if(child,$TextFrom="Number3",$Name));
TermScore(agent) = ($Field1*2) + ($Field2*1) + ($Field3*0.5)


Is there a more efficient way of writing this code? It seems horribly inefficient that I create a set simply to count the number of entries within it.

I notice the manual (for 4.2.4) documents sum_if. Is there a count_if function available? Or is there some other way I can conditionally count the child-aliases returned by the agent?

Title: Re: collect_if --- seeking greater efficiency
Post by Mark Bernstein on Jun 17th, 2008, 11:46am

If you might have other uses for a list of notes that meet the collect_if criterion, you could simple make several auxilliary agents with the query

    TextFrom="..."

Alternatively, you could keep each of the sets around in separate attributes

       Set1=collect_if(child,$TextFrom="Number1",$Name);

perhaps running this action in a low-priority agent.  Then, you could update

     Field1=count(Set1)

This is probably better for debugging and reuse -- and it lets you separate parts of the work into agents (which have adjustable priorities) and other parts into rules (which are constantly updated)



I'm not even certain that count_if would be more efficient in your case.

Title: Re: collect_if --- seeking greater efficiency
Post by Loryn on Jun 17th, 2008, 4:31pm

Thanks, Mark.

You're right your suggested version being better for maintenance and debugging.

Should I need to perform any other query on these children, I will certainly refactor the code along the lines you suggest.

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.