Exec-tree-example.pl

From Organic Design wiki
Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.

Example of nodal reduction in Perl.

#!/usr/bin/perl -w
use strict;

# First, we set up three test threads each of three sequencial functions
# - Each function simply prints its name and returns a reference to the next in its thread
# - In reality the functions would be acting on their local environment
#   (in PERL the local environment is in the global $cwd hashRef)
# - and what's returned can be not only a function, but another hashRef or void if its finished

our ($A,$B,$C, $P,$Q,$R, $X,$Y,$Z);
$A = sub {print "A"; return $B;};
$B = sub {print "B"; return $C;};
$C = sub {print "C"; return $A;};

$P = sub {print "P"; return $Q;};
$Q = sub {print "Q"; return $R;};
$R = sub {print "R"; return $P;};

$X = sub {print "X"; return $Y;};
$Y = sub {print "Y"; return $Z;};
$Z = sub {print "Z"; return $X;};

# The execution tree is set up with the root containing thread-ABC
#   and another node which contains threads PQR & XYZ
# - So PQR and XYZ execute in parallel and as a whole they execute in parallel with ABC

my $someNode = {queue => [$P,$X]};
my $root = {queue => [$A,$someNode]};
our $cwd;  # In PERL the functions act on their local environment which is the $cwd hashRef

while (1) {
	$cwd = $root;
	for (my $i = 1; $i == 1;) {
		my $queue = $$cwd{'queue'};
		if ($#$queue >= 0) {
			my $todo = shift @$queue;
			push @$queue, $_ if $_ = (ref $todo eq 'CODE') ? ($i = &$todo) : ($cwd = $todo);
			}
		else	{$i = 0}
		}
	}