Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Loop through all descendants of a note (Read 4140 times)
SteveS
Full Member
*
Offline



Posts: 15

Loop through all descendants of a note
Sep 27th, 2013, 4:11am
 
Hi All,

Seems like this should be easy to figure out, but I have spent about an hour tinkering and looking online, and no luck.

As part of a rule to a note, I would like to do a query, and loop through the result, of all descendants of that note.  I then want to set the $Rule for all of those descendants to a specific string.

Any help would be appreciated.

Cheers
Steve
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Loop through all descendants of a note
Reply #1 - Sep 27th, 2013, 7:41am
 
OK you want to set a new $Rule in every note descended from a given note. Below we'll call "XYZ", and by way of example we'll set a rule to make the descendants bright red. The best approach is an agent:

Query: descendedFrom("XYZ")

Action: $Rule='$Color="bright red"';

Once the agent has looped once you can delete it as all relevant notes have the new rule.  But, perhaps some notes already have a rule. In that case the above would replace the existing code which might not be what you want. An alternative action:

if($Rule!=""){$Rule='$Color="bright red"'}else{$Rule=$Rule+';$Color="bright red"'}

In this case, you test if a rule already exists and if so the new code is appended to the existing rule.

Other notes:
  • If "XYZ" is not a unique name, use the full path (without quotes) and not just note's $Name in the query.
  • Observe that we need to use single and double quotes when setting the rule. this is because the rule is set to a quoted string containing the action code that will form the new rule. It doesn't matter if single nest outside double as long as pairs of notes nest correctly.
  • Why not test for a $Rule using if($Rule) short form test? I'm being extra cautious. Your note may have rules with no code but which previously had code. Thus they will have no code but no longer inherit a value. the latter means that they would test true for if($Rule) but false for if($Rule!=""). Actually, starting a rule with a semi-colon won't break the rule, but will likely confuse you when you look at the code a few months later. To understand the nuance more, see this aTbRef note.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
SteveS
Full Member
*
Offline



Posts: 15

Re: Loop through all descendants of a note
Reply #2 - Sep 27th, 2013, 12:33pm
 
Thanks Mark, I really appreciate your timely and thorough feedback!

I am working on something pretty cool (in my opinion), when I have made a bit of progress I will share it with the forum.

Cheers!
Steve


Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Loop through all descendants of a note
Reply #3 - Sep 27th, 2013, 1:15pm
 
Actually, I realise I slightly misread your opening post (was rushing to catch a train). Anyway…

Is the issue that you want a note, on some change (e.g. some attribute), you want to affect all descendants? There may be some alternate choices, but it depends on he nature of the code you're trying to write. Could you offer and example? By all means bowdlerise if sensitive.

A find() in a rule can return a list of $Path for descendant notes but it can't then action that - see Mark Bernstein's post below. Well, it can, via a .each() call on the resulting list but I think that's probably not efficient - it rather depends on what you're trying to do and to how many notes and how often.

An alternate approach, if context allows is to alter a prototype - but IIRC your prototype structure probably wouldn't support that.

Edited:
Strikethrough edit reflects better answer below

Back to top
 
« Last Edit: Oct 4th, 2013, 10:45am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
SteveS
Full Member
*
Offline



Posts: 15

Re: Loop through all descendants of a note
Reply #4 - Oct 4th, 2013, 12:24am
 
Hi Mark,

I have been experimenting around with simulating inheritance as I think about is as a software developer, which is related to the question in the post.  I am going to start a separate thread to share some specific questions, and interesting ideas that I have been playing with"

Thanks
Steve
Back to top
 
 
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Loop through all descendants of a note
Reply #5 - Oct 4th, 2013, 10:40am
 
We call know that

    $Color(/thatNote)="black"

will turn thatNote black.  People often forget that the designator can be a list of notes.

    $Color(thingOne;thingTwo)="green"

will set the color of both thingOne and thingTwo.  This means you don't need a loop to do what you want!

   $Rule( ....which notes...) = .... the rule ...

Which notes do you want?  Probably something like this:

   find( descendedFrom(/the/interesting/container) )

I'd recommend saving the intermediate list to simplify testing and debugging. Something like this:

   $MyList= find( descendedFrom(/the/interesting/container) );
   $Rule($MyList) = $Rule(/configuration/example)
Back to top
 
 
WWW   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Loop through all descendants of a note
Reply #6 - Oct 4th, 2013, 10:51am
 
More on left-side expressions in Action code. My article doesn't currently include a list attribute (as in the post above) but I'll amend that next week when my dead Mac comes back from repairs.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print