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 >> Agent's Display Expression counts children
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1473703333

Message started by John Doernberg on Sep 12th, 2016, 2:02pm

Title: Agent's Display Expression counts children
Post by John Doernberg on Sep 12th, 2016, 2:02pm

I have a TBX file from a couple of years ago in which (with Mark's copious help) one of my agents has a DisplayExpression that counts the number of items retrieved by the agent using their respective prototypes.  It's very helpful, but when I copied and pasted it into a different file and changed the prototype names to reflect the new file, it returned "0" items for each prototype except one. I can't figure out why, although it strikes me as possible that this is a legacy syntax that has been deprecated. For what it's worth, the agent is intended to retrieve the number of items that fell within the selected month based on their $KeyDates (a user attribute). Here is the Display Expression:

$Name+": "+$ChildCount+" entries"+" ("+count(collect_if(children,$Prototype=“p_Task”,$Name))+" Tasks, "+count(collect_if(children,$Prototype="p_Event",$Name))+" Events, "+count(collect_if(children,$Prototype="p_Book",$Name))+" Books, "+count(collect_if(children,$Prototype=“p_Note”,$Name))+" Notes, "+count(collect_if(children,$Prototype=“p_Idea”,$Name))+" Ideas"+")"

The only item it returns properly is the number of Events. What am I doing wrong? Thanks for your help.

Title: Re: Agent's Display Expression counts children
Post by Mark Anderson on Sep 12th, 2016, 3:17pm

First, have you checked the agent contains items of the other prototypes that should be found by the agent?

Title: Re: Agent's Display Expression counts children
Post by Mark Anderson on Sep 12th, 2016, 3:38pm

Looking at your code you've got some curly quotes in there (e.g. around prototype Task, Note and Idea). That might just be typo in transcription but, action code must use only straight quotes (pairs of single or double) to enclose strings.

count(collect()), etc., can now be written collect().count but the latter does the same thing. However it does remove one set of parentheses.

Legacy code alert. Don't use '=' for 'is equals to' test, use '=='. A single = should only be used for assignment:

Set $Color to blue:  $Color = "blue"
Test $Color is blue:  $Color == "blue"

One equals used to do both and TB guessed intent. It still does but action code's more complex and you're better using the correct operator for the desired task.  See aTbREf for more on codes.

So (line breaks are just so it fits on screen better here) here is the DE code with the quotes fixed, using .count and == for comparisons:

$Name+": "+$ChildCount+" entries"+" ("+
collect_if(children,$Prototype=="p_Task",$Name).count+
" Tasks, "+
collect_if(children,$Prototype=="p_Event",$Name).count+
" Events, "+
collect_if(children,$Prototype=="p_Book",$Name).count+
" Books, "+
collect_if(children,$Prototype=="p_Note",$Name).count+
" Notes, "+
collect_if(children,$Prototype=="p_Idea",$Name).count+
" Ideas"+")"


Better would be to use a rule to set $MyString to the output of the above and then make the DE code just $MyString.

Rule:
$MyString = $Name+": "+$ChildCount+" entries"+" ("+
collect_if(children,$Prototype=="p_Task",$Name).count+
" Tasks, "+
collect_if(children,$Prototype=="p_Event",$Name).count+
" Events, "+
collect_if(children,$Prototype=="p_Book",$Name).count+
" Books, "+
collect_if(children,$Prototype=="p_Note",$Name).count+
" Notes, "+
collect_if(children,$Prototype=="p_Idea",$Name).count+
" Ideas"+")"


DisplayExpression:
$MyString

This makes all the calculation happen. The DE simply shows the current (static) $MyString value as updated via the rule. In general you don't want a lot of calculation in a DE as it can slow things up a bit.

Title: Re: Agent's Display Expression counts children
Post by John Doernberg on Sep 12th, 2016, 4:26pm

Thank you. I copied and pasted your code, changing $MyString to an attribute that I put into DisplayExpression instead of the code I had there (by the way, the apparent brackets are straight parenthesis in my version, so I think that is just how they appear). Funny result. The agent in question happens to contain only Events and Tasks. With my old version, it properly returned the number of Events but 0 Tasks even though there were some. After I used your version, it returns the proper number of Tasks but 0 Events.

Title: Re: Agent's Display Expression counts children
Post by Mark Anderson on Sep 12th, 2016, 4:40pm

At this point, with such code I test each collect_if() on it's own. IOW, I'd either make 4 more string attributes or set $MyString in turn to each count. Thus first check is:

$MyString = collect_if(children,$Prototype=="p_Task",$Name).count;

If the count is OK, change the prototype and check the next one, etc. This will tell you if the failure is in the individual collect statements or in your overall concatenation string.

I thought the quotes might be OK at source, but better safe than sorry! No censure intended.

The == test is cases sensitive so check the test strings are the same case as the actual prototype names.

If you can link to a small test file that shows the problem it might be easier to diagnose but otherwise we can carry on here.

Title: Re: Agent's Display Expression counts children
Post by John Doernberg on Sep 12th, 2016, 6:37pm

Thanks, I'll work that out in a test file created from a duplicate of the original. It's a pretty safe assumption with me that through trial and error I have larded my file with a lot of detritus over time that didn't manifest itself when entered but eventually causes problems.

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.