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 >> Complex conditional on-add actions: syntax?
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1205605511

Message started by Rafter T. Sass on Mar 15th, 2008, 2:25pm

Title: Complex conditional on-add actions: syntax?
Post by Rafter T. Sass on Mar 15th, 2008, 2:25pm

As somebody who has never done any scripting or programming, customizing Tinderbox is definitely proving an invigorating and challenging experience.

So when I say "complex" above, it may just be me that finds it so...

I'm creating a workspace-mapview, where notes moved from adornment to adornment have values assigned to a swath of user attributes.

I understand the "if(condition) {action} else {action}" syntax pretty good. (I think.)

What I'm trying to figure out is the proper syntax for conditional actions of the following forms:
1) if(conditionA or conditionB or conditionC) {action}
and
2) if(condition) {action} else if(condition) {action} else if(condition) {action} etc.

and while we're at it, (1) and (2) combined, if combining them is non-linear in some way.

So far I'm not having any luck.

Title: Re: Complex conditional on-add actions: syntax?
Post by Amber Vaesca on Mar 15th, 2008, 4:29pm

Tinderbox prefers nested if statements instead of elseif chains, like you see in many programming languages. So if you need to check a second logical set within the first logical set you'll have to do this:


Code:
if (condition) {
   if (secondCondition) {
       attribute=value;
   } else {
       if (secondSecondCondition) {
           attribute=value;
       };
   };
} else {
   if (thirdCondition) {
       attribute=value;
   };
};


To perform boolean conditions, use & for AND and | (the pipe symbol) for OR. This example makes ConditionA mandatory, but either B or C must be true.


Code:
if (conditionA & (conditionB | conditionC)) { action };


Note that you can sometimes do a degree of integrated logic in the assignment action itself, and this should be utilised whenever possible as it results in cleaner code.


Code:
Attribute |= value;


Will assign value to Attribute if Attribute tests false; the value is not tested at all, so it can result in Attribute getting assigned to a false value. It is precisely like saying:


Code:
if (!Attribute) { Attribute=value; }


Additionally there is this form:


Code:
Attribute &= value;


In this case both Attribute and Value must test true for the assignment to take place. A comparable syntax would be:


Code:
if (Attribute & value) { Attribute=value; }


This is good for situations where you want something set, but do not want it set if the value doesn't match a certain criteria yet. It could also be used as an override on Attribute; leaving it alone if it is default, but modifying it if something else has altered it.

Generally speaking, you'll probably come across more instances were |= is useful. It is good for initialising default values and making sure things do not overwrite attributes unless you wish them to. An example would be an Adornment that sets a due date of 'today+3' but you don't want it to adjust the note if it already has a due date. A simple "DueDate|=today+3" will accomplish that task in one move. It is also useful to avoid needless re-assignment. Using |= in a Rule can set a variable when a condition is met, but once that variable is set, it will no longer waste cycles setting it over and over.

So consider using these shortcuts when you are constructing complicated logic sequences. The trickiest part is keeping track of what 'else' means, sometimes, and where it is appropriate to put a default fall-back.

Here is an example of a nested AgentAction from one of my projects:


Code:
if(StatusTags(waitingFor) | StatusTags(Someday)) {
     Badge = wait;
} else {
     if(SettingDueDate < today 23:59) {
           Badge = 'arrow right';
     } else {
           if(Prototype="Sequential Action" & InternalNextAction) {
                 Badge = next;
           } else {
                 if (URL) {
                       Badge = web;
                 } else {
                       Badge=;
                 };
           };
     };
};


This is responsible for setting badges for the notes which match the agent's criteria. You'll note I am off-loading logic to other rules and agents. Things are simply being checked for truth, with one exception---the date check. This is a good idea. Let something else decided if InternalNextAction is true (because in this case that is a rather complicated judgement anyway), and have the badge application AgentAction simply check that attribute for truth.

It uses a descending priority pattern. Badges at the top will override badges at the bottom if the conditions match. This is also an example of a logic chain with a default fall-back. The final 'else' sets the Badge to the prototype default. If you wanted a more gentle version of this, that final statement could be an |=. Then you could apply your own custom badges to notes without them getting wiped out---but you would also have a problem where notes that no longer match the superior criterion would not lose the attention badges. To get around that, you would probably want to put on final if statement that checks for the presence of these specific attention badges, because if the logic chain has reached that point, they should no longer have them, and should thus be removed.

Title: Re: Complex conditional on-add actions: syntax?
Post by Rafter T. Sass on Mar 20th, 2008, 6:36pm

Thanks so much! That gives me a lot to chew on for a while...
;)


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.