2 package ZOOM::IRSpy::Node;
12 ZOOM::IRSpy::Node - node in a tree of names
16 $node1 = new ZOOM::IRSpy::Node("LowLevelTest");
17 $node2 = new ZOOM::IRSpy::Node("AnotherTest");
18 $node3 = new ZOOM::IRSpy::Node("Aggregate", $node1, $node2);
19 $node = new ZOOM::IRSpy::Node("Main", $node3);
21 $subnode = $node->select("0:1");
22 die "oops" if $subnode->name() ne "AnotherTest";
26 IRSpy maintains a declarative hierarchy of the tests that each
27 connection may be required to perform, which it compiles recursively
28 from the C<subtests()> method of the top-level test and each of its
29 subtests, sub-subtests, etc. The result of this compilation is a
30 hierarchy represented by a tree of C<ZOOM::IRSpy::Node> objects.
32 Note that each node contains a test I<name>, not an actual test
33 object. Test objects are different, and are implemented by the
34 C<ZOOM::IRSpy::Test> class and its subclasses. In fact, there is
35 nothing test-specific about the Node module: it can be used to build
36 hierarchies of anything.
38 You can't do much with a node. Each node carries a name string and a
39 list of its subnodes, both of which are specified at creation time and
40 can be retrieved by accessor methods; trees can be pretty-printed, but
41 that's really only useful for debugging; and finally, nodes can be
42 selected from a tree using an address, which is a bit like a totally
47 $node = new ZOOM::IRSpy::Node($name, @subnodes);
49 Creates a new node with the name specified as the first argument of
50 the constructor. If further arguments are provided, they are taken to
51 be existing nodes that become subnodes of the new one. Once a node
52 has been created, neither its name nor its list of subnodes can be
59 my($name, @subnodes) = @_;
62 subnodes => \@subnodes,
63 address => undef, # filled in by resolve()
64 previous => undef, # filled in by resolve()
65 next => undef, # filled in by resolve()
73 print "Node is called '", $node->name(), "'\n";
75 Returns the name of the node.
86 @nodes = $node->subnodes();
87 print "Node has ", scalar(@nodes), " subnodes\n";
89 Returns a list of the subnodes of the node.
95 return @{ $this->{subnodes} };
102 Pretty-prints the node and, recursively, all its children. The
103 parameter is the level of indentation to use in printing the node;
104 this method recursively invokes itself with higher levels.
112 print "\t" x $level, $this->name();
113 if (my @sub = $this->subnodes()) {
115 foreach my $sub (@sub) {
116 $sub->print($level+1);
118 print "\t" x $level, "}";
125 $sameNode = $node->select("");
126 $firstSubNode $node->select("0");
127 $secondSubNode $node->select("1");
128 $deepNode $node->select("0:3:2");
130 Returns a specified node from the tree of which C<$node> is the root,
131 or an undefined value if the specified node does not exist. The sole
132 argument is the address of the node to be returned, which consists of
133 zero or more colon-separated components. Each component is an
134 integer, a zero-based index into the subnodes at that level. Example
141 The node itself, i.e. the root of the tree.
145 Subnode number 0 (i.e. the first subnode) of the root.
149 Subnode number 1 (i.e. the second subnode) of the root.
153 Subnode 2 of subnode 3 of subnode zero of the root (i.e. the third
154 subnode of the fourth subnode of the first subnode of the root).
164 my @sub = $this->subnodes();
165 if ($address eq "") {
167 } elsif (my($head, $tail) = $address =~ /(.*?):(.*)/) {
168 return $sub[$head]->select($tail);
170 return $sub[$address];
175 =head2 resolve(), address(), parent(), previous(), next()
178 assert(!defined $root->parent());
179 print $node->address();
180 assert($node eq $node->next()->previous());
182 C<resolve()> walks the tree rooted at C<$root>, adding addresses and
183 parent/previous/next links to each node in the tree, such that they
184 can respond to the C<address()>, C<parent()>, C<previous()> and
187 C<address()> returns the address of the node within the tree whose root
188 it was resolved from.
190 C<parent()> returns the parent node of this one, or an undefined value
193 C<previous()> returns the node that occurs before this one in a pre-order
196 C<next()> causes global thermonuclear warfare. Do not use C<next()>
197 in a production environment.
206 # Returns the last child-node in the subtree
211 $this->{address} = $address;
212 my $previous = $this;
214 my @subnodes = $this->subnodes();
215 foreach my $i (0 .. @subnodes-1) {
216 my $subnode = $subnodes[$i];
217 $subnode->{parent} = $this;
218 $subnode->{previous} = $previous;
219 $previous->{next} = $subnode;
221 my $subaddr = $address;
222 $subaddr .= ":" if $subaddr ne "";
224 $previous = $subnode->_resolve($subaddr);
230 sub address { shift()->{address} }
231 sub parent { shift()->{parent} }
232 sub previous { shift()->{previous} }
233 sub next { shift()->{next} }
242 Mike Taylor, E<lt>mike@indexdata.comE<gt>
244 =head1 COPYRIGHT AND LICENSE
246 Copyright (C) 2006 by Index Data ApS.
248 This library is free software; you can redistribute it and/or modify
249 it under the same terms as Perl itself, either Perl version 5.8.7 or,
250 at your option, any later version of Perl 5 you may have available.