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
Generating a Daily Randomized Note (Read 8579 times)
russ lipton
Full Member
*
Offline



Posts: 126

Generating a Daily Randomized Note
Sep 30th, 2009, 12:11pm
 
I hope to generate-export a single, different random note once a day to a website. This seems simple, but I am blocked.

What type of agent syntax would work? How might it employ randomChildOf?
Back to top
 
 
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Generating a Daily Randomized Note
Reply #1 - Sep 30th, 2009, 1:58pm
 
I do this at http://markbernstein.org/ .  In the left column, there's a box titled "Lecture Notes" which presents information about one of my previous lectures; the export code is simply:

   ^randomChildOf(LectureNotes,/templates/lecture box)^

This picks any child of Lecture Notes and formats it using the export template "/templates/lecture box".
Back to top
 
 
WWW   IP Logged
russ lipton
Full Member
*
Offline



Posts: 126

Re: Generating a Daily Randomized Note
Reply #2 - Sep 30th, 2009, 11:20pm
 
Is it possible to fire an agent once a day to generate a new randomChildOf note?
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Generating a Daily Randomized Note
Reply #3 - Oct 1st, 2009, 7:22am
 
Busy, so not had time to run a full test but....

As not 100% certain, I've made these assumptions:
  • source TBX container for random notes is the note "Lecture Notes"; change the name if needed when using code below.
  • for any given calendar day we only wish to see one random text.
  • export may occur one or more times during the given day.


We need a few user attributes; if you don't like my choice of names and change them, also edit the code samples to reflect that.

Boolean: IsDateReset, IsPosted
String: MyText

I'll put paste-able code further below, but first an annotated walkthrough  with (non-code) comments in green):

We only want to do something if it's more than a day since we changed the random text
if(days($MyDate,"today")>0){
It's more than a day. Let's check if we've updated the random text today already
if (!$IsDateReset) {
The date reset flag isn't set so this is the first time today that we've run this code
First, let's roll our reference date a day right
$MyDate = day($MyDate1,(day($MyDate + "1 day"));
Now, store text of a random child
$MyText=$Text(randomChild);
Lastly, set the reset flag to ensure this code only runs once per new day.
$IsDateReset = true;
}
}


In your template where the text is exported

Only our 'Lecture Notes' will have a value for $MyText. Note in export code we don't use the $-prefix for attribute names
^if(^get($MyText1)^!="")^
Insert the random text
^get($MyText)^
^endIf^

Rule code (copy & paste). There should be no line breaks (forum's inserting them!):
if(days($MyDate,"today")>0){if(!$IsDateReset){$MyDate=day($MyDate1,(day($MyDate + "1 day"));$IsPosted=false;$MyText=$Text(randomChild);$IsDateReset = true;}}

Template code (copy & paste). There should be no line breaks (forum's inserting them!):
^if((^get(IsPosted)^=false)&(^get($MyText1)^!=""))^^get($MyText)^^action($IsPost
ed=true)^^endIf^

N.B. As said at outset, I've not had time to run a full test.

Post-edits:
  • $MyDate + "1 day" substituted for "today" in the above code. This stops the reset date/time creeping over time as only the day part of the original time is incremented.
  • On set-up you need to seed $MyTime with a time. I'd suggest time 00:01 on the first day of use. Thereafter the first agent update cycle after 00:01 each day will set a new random content in $MyText.

Back to top
 
« Last Edit: Oct 2nd, 2009, 3:39am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Generating a Daily Randomized Note
Reply #4 - Oct 1st, 2009, 9:29am
 
[Speculation] How about something along these lines?

      Query: $LastUpdate(agent)<"yesterday"
      Action: $ChosenPath(/Today's Saying=path(randomChild(/Sayings);
                   $LastUpdate(agent)="today"

Of course, you could choose a new random child every time you export, and simply update your site once a day. That would give you the same effect!

Another way to do this would be to keep Sayings sorted by $Priority, and export the first child.  Then, you agent could do something like

              Query: $LastUpdate(agent)<"yesterday"
             Action: $Priority(randomChild(/Sayings))=$TopPriority(/config); $TopPriority(/config)+=1;

So, every day we pick a random child and give it a higher priority, and the export template just picks off the top-priority child.
Back to top
 
 
WWW   IP Logged
russ lipton
Full Member
*
Offline



Posts: 126

Re: Generating a Daily Randomized Note
Reply #5 - Oct 1st, 2009, 6:53pm
 
Useful and interesting, both. Thank you.
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Generating a Daily Randomized Note
Reply #6 - Oct 2nd, 2009, 3:41am
 
Russ if you tried my code yesterday, notice a couple of post-edit points listed at bottom of my last post. Sorry, had to leave for a meeting yesterday before I got everything done.
Back to top
 
 

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



Posts: 267

Re: Generating a Daily Randomized Note
Reply #7 - Dec 29th, 2009, 9:03am
 
I want to do a variation of the techniques Mark B. described.  In the sidebar of my site I have two quotes each day.  So, I would like an agent that choses TWO random quotes.  I can do this by having the container that holds the published quotes look into the container that holds all the possible quotes (Call it "AllQuotes") and chose the first two.  But then I would want to randomize the order of notes in AllQuotes.  AFAIK, there is no 'random' sort order.
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Generating a Daily Randomized Note
Reply #8 - Dec 29th, 2009, 10:33am
 
As so often with TB, there probably is a way.  I assume you're using ^randomChild(item[, template]^? The simplest answer for the would be to extend the syntax to add a new optional parameter 'N' (^randomChild(item[, template][, N]]^) where N is the number of random children desired. That, however, is a feature request at this point. Another (would-be feature request) method to do this would be to have a randomChild(group[,attributeName][, N]), which would allow fetching a random child in action code.

In principle, via export code, you could chose random child #1 then using an ^action^ call set a user attribute Boolean that could be added as a query parameter for AllItems.  Them we pick a random child #2. The problem is I don't think there's a way to trigger an agent refresh before calling #2, without which we might get item #1 returned again.

So if action/export code can force refresh an agent before reading it's children there is a way to this.

Back to top
 
 

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



Posts: 267

Re: Generating a Daily Randomized Note
Reply #9 - Dec 29th, 2009, 11:48am
 
Thinking about this a bit more (which I should have done before posting) I realized the solution is rather trivial and uses existing features of Tinderbox.  

If the notes in my AllQuotes container have a number attribute "randID" and a prototype rule

$randID = rand()

All I need to do is sort the AllNotes container by randID.  The sequence of notes in AllNotes is randomized whenever there is activity in the document.  

The agent that looks into that container for two notes to publish merely uses

first(AllQuotes,2)

To avoid system churn, I used manual agent updating, which forces both the rule and the agent to execute, yielding a new selection of two notes each time.
Back to top
 
 
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Generating a Daily Randomized Note
Reply #10 - Dec 29th, 2009, 11:57am
 
How about a rule that sets a numeric user attribute $MySort to a random number?

    $MySort=rand()

Then sort by $MySort.  This will do the job, though it's a bit wasteful since Tinderbox is continuously generating randomg numbers and re-sorting.

You might instead use a low-priority agent to set MySort.  Or, perhaps you add a date attribute $LastSorted

    if($LastSorted<date("today-1 day")){$MySort=rand();}

If you have lots of quotations and don't mind an occasional collision, you could save bother by simply selecting two random children.

Alternatively, you might divide your quotations into two arbitrary lists. Choose one quote from column A, and one from column B.
Back to top
 
 
WWW   IP Logged
Pages: 1
Send Topic Print