What's wrong with my code?


(Tianrui Niu) #1

Hi there,
I’m tinkering with TB’s rules and expecting it to do some jobs automatically for me. I’ve defined a prototype named “Thesis” that contains informations of thesis I’ve read. I defined a user attribute “Like” of type number which describes how much I like the thesis. For visual convenience I wanted to create a rule that automatically set different colors for different level of “Like” of thesis. So I defined a rule:
$ColorList=list(“black”,“brown”,“yellow”,“green”,“orange”,“red”); $Color=$ColorList.at($Like);
As is described in aTBRef, ColorList is a list and I use the value of Like to index that list. Ideally if the Like value is 3, it returns “green” and $Color will be set to green.
It’s strange that this rule does not work. In such a simple piece of code I actually cannot tell where goes wrong.
BTW, is there possibly an editor that checks the syntax of rule/action codes or just provides debug informations? I think the TB rule editor is not quite friendly for newbies like me.
Any suggestions are welcome, thanks!


(Paul Walters) #2

First, I would not load your colors into $ColorList in a rule. I would define them as defaults in the user attribute inspector. (You do not need to put the values in quotes.)

Also, recall that the indexing of a list starts with zero – so $Color=$ColorList.at(1) is “blue” in my example, above.

$Like should be a numeric attribute.

Otherwise, over here at least, the rule $Color=$ColorList.at($Like) is working without error. Because I defined defaults, I did not include $ColorList=list("black","brown","yellow","green","orange","red"); in my test rule.


(Mark Anderson) #3

This works for me. Note that the rule is looking at $ColorList in the current note. If you’ve set this list in one note and want all the rules to to use that note’s list then you need a slightly different form. Assuming the note is in a uniquely named note “Colour List”, then the rule would be:

$Color=$ColorList("Colour List").at($Like);

Note, unless you’ve actually defined ‘brown’ as a user-defined colour in your TBX a $Like value of 1 (i.e. second list item) sets the redd-ish ‘undefined’ colour as a default TBX has no ‘brown’ colour defined.


(Tianrui Niu) #4

Thanks for both of your suggestion. I just found the problem here is because that I did not define the attribute “colorlist” in attribute inspector so $colorlist returns nothing. But personally I choose not to define colorlist as attribute, instead, declare it as a local variable.
The below code works good for me:
var colorlist(“blue;warm gray dark;yellow;green;orange;red”); $Color=colorlist.at($Like);


(eastgate) #5

Alternatively, the sandbox attribute $MyList is available when you need to construct a list, but don’t really want to bother adding an attribute or using a local variable.


(Tim Wallin) #6

Eastgate people - I’m a Tbx newbie with lots of computing experience, a science PhD, and I love the high level of abstraction & potential of Tbx. I’ve read all the references & startup guides multiple times (thank you frequent posters). I’m getting there, but WAY WAY WAY too slowly! I’m here to tell you that the forum quote above is THE single most important thing new users need. Without that, at my current rate of progress I may be dead before I develop adequate Tbx facility :wink: . Thanks for your efforts and an amazing product.


(Tim Wallin) #7

Even a web-based one that you host (as opposed to built-in) would be a miracle and help reduce wasted time.


(eastgate) #8

Often, this complaint will generalized to any programmable system, including visual programming, Do What I Mean, programming by example, and everything else we know. Identifying programming errors is hard.

But, for the most part, Tinderbox actions are easy. Almost everything is an assignment:

$Color=“red”;

or a predicate

$Price > 100

So, my first suggestion is: if your actions are really complicated, step back and think about them for a minute. Might it be simpler to use an agent? Two cooperating agents?

Second: if something really needs to be complex and it’s not doing what you expect, break it down into a component that’s too simple to possibly fail. Try that. OK? Now, add one more simple component. Try that! Build it up bit by bit; this will only take a minute or two longer, but solves lots of potential troubles.

Third: if you haven’t read the Agents and Dashboards tutorial in the Help menu, I think it’s pretty good.

Fourth: if you’re stuck on something, ask here. It’ll take a day to get an answer, maybe less. Helpful hint: tell us first what you’re trying to do – the big picture as well as the details. The big picture matters!