| 1 | # Build::Hopen::G::DAG - hopen build graph | ||||||||||||||
| 2 | package Build::Hopen::G::DAG; | ||||||||||||||
| 3 | 2 | ▶ | use Build::Hopen::Base; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use Build::Hopen qw(hlog $QUIET); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | our $VERSION = '0.000008'; # TRIAL | |||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | ▶ | use parent 'Build::Hopen::G::Op'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use Class::Tiny { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||
| 10 | 0 | ▶ | goals => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 11 | 2 | default_goal => undef, | |||||||||||||
| 12 | |||||||||||||||
| 13 | # Private attributes with simple defaults | ||||||||||||||
| 14 | #_node_by_name => sub { +{} }, # map from node names to nodes in either | ||||||||||||||
| 15 | # # _init_graph or _graph | ||||||||||||||
| 16 | |||||||||||||||
| 17 | # Private attributes - initialized by BUILD() | ||||||||||||||
| 18 | _graph => undef, # L<Graph> instance | ||||||||||||||
| 19 | _final => undef, # The graph sink - all goals have edges to this | ||||||||||||||
| 20 | |||||||||||||||
| 21 | #Initialization operations | ||||||||||||||
| 22 | _init_graph => undef, # L<Graph> for initializations | ||||||||||||||
| 23 | _init_first => undef, # Graph node for initialization - the first | ||||||||||||||
| 24 | # init operation to be performed. | ||||||||||||||
| 25 | |||||||||||||||
| 26 | # TODO? also support fini to run operations after _graph runs? | ||||||||||||||
| 27 | 2 | }; | |||||||||||||
| 28 | |||||||||||||||
| 29 | 2 | ▶ | use Build::Hopen::G::Goal; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 2 | ▶ | use Build::Hopen::G::Link; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 2 | ▶ | use Build::Hopen::G::Node; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 2 | ▶ | use Build::Hopen::G::CollectOp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | 2 | ▶ | use Build::Hopen::Util::Data qw(forward_opts); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 2 | ▶ | use Build::Hopen::Arrrgs; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 2 | ▶ | use Graph; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 2 | ▶ | use Storable (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | # Class data {{{1 | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 2 | ▶ | use constant { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 2 | LINKS => 'link_list', # Graph edge attr: array of BHG::Link instances | |||||||||||||
| 42 | 2 | }; | |||||||||||||
| 43 | |||||||||||||||
| 44 | # A counter used for making unique names | ||||||||||||||
| 45 | 2 | my $_id_counter = 0; # threads: make shared | |||||||||||||
| 46 | |||||||||||||||
| 47 | # }}}1 | ||||||||||||||
| 48 | # Docs {{{1 | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =head1 NAME | ||||||||||||||
| 51 | |||||||||||||||
| 52 | Build::Hopen::G::DAG - A hopen build graph | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||||||
| 55 | |||||||||||||||
| 56 | This class encapsulates the DAG for a particular set of one or more goals. | ||||||||||||||
| 57 | It is itself a L<Build::Hopen::G::Op> so that it can be composed into | ||||||||||||||
| 58 | other DAGs. | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head1 ATTRIBUTES | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 goals | ||||||||||||||
| 63 | |||||||||||||||
| 64 | Arrayref of the goals for this DAG. | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =head2 default_goal | ||||||||||||||
| 67 | |||||||||||||||
| 68 | The default goal for this DAG. | ||||||||||||||
| 69 | |||||||||||||||
| 70 | =head2 _graph | ||||||||||||||
| 71 | |||||||||||||||
| 72 | The actual L<Graph>. If you find that you have to use it, please open an | ||||||||||||||
| 73 | issue so we can see about providing a documented API for your use case! | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =head2 _final | ||||||||||||||
| 76 | |||||||||||||||
| 77 | The node to which all goals are connected. | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =head2 _init_graph | ||||||||||||||
| 80 | |||||||||||||||
| 81 | A separate L<Graph> of operations that will run before all the operations | ||||||||||||||
| 82 | in L</_graph>. This is because I don't want to add an edge to every | ||||||||||||||
| 83 | single node just to force the topological sort to work out. | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head2 _init_first | ||||||||||||||
| 86 | |||||||||||||||
| 87 | The first node to be run in _init_graph. | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =head1 FUNCTIONS | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =cut | ||||||||||||||
| 92 | |||||||||||||||
| 93 | # }}}1 | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head2 _run | ||||||||||||||
| 96 | |||||||||||||||
| 97 | Traverses the graph. The DAG is similar to a subroutine in this respect. | ||||||||||||||
| 98 | The outputs from all the goals | ||||||||||||||
| 99 | of the DAG are aggregated and provided as the outputs of the DAG. | ||||||||||||||
| 100 | The output is a hash keyed by the name of each goal, with each goal's outputs | ||||||||||||||
| 101 | as the values under that name. Usage: | ||||||||||||||
| 102 | |||||||||||||||
| 103 | my $hrOutputs = $dag->run([-context=>$scope][, other options]) | ||||||||||||||
| 104 | |||||||||||||||
| 105 | C<$scope> must be a L<Build::Hopen::Scope> or subclass if provided. | ||||||||||||||
| 106 | Other options are as L<Build::Hopen::Runnable/run>. | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =cut | ||||||||||||||
| 109 | |||||||||||||||
| 110 | # The implementation of run(). $self->scope has already been linked to the context. | ||||||||||||||
| 111 | 1 | ▶ | sub _run { | ||||||||||||
|
Subroutine
_run: called CC=15 SCAR=28.4
| |||||||||||||||
| 112 | 1 | my ($self, %args) = parameters('self', [qw(; phase generator)], @_); | |||||||||||||
| 113 | 1 | my $retval = {}; | |||||||||||||
| 114 | |||||||||||||||
| 115 | # --- Get the initialization ops --- | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 1 | my @init_order = eval { $self->_init_graph->toposort }; | |||||||||||||
| 118 | 1 | ▶ | die "Initializations contain a cycle!" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 1 | ▶ | @init_order = () if $self->_init_graph->vertices == 1; # no init nodes => skip | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | |||||||||||||||
| 121 | # --- Get the runtime ops --- | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 1 | my @order = eval { $self->_graph->toposort }; | |||||||||||||
| 124 | # TODO someday support multi-core-friendly topo-sort, so nodes can run | ||||||||||||||
| 125 | # in parallel until they block each other. | ||||||||||||||
| 126 | 1 | ▶ | die "Graph contains a cycle!" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | |||||||||||||||
| 128 | # Remove _final from the order for now - I don't yet know what it means | ||||||||||||||
| 129 | # to traverse _final. | ||||||||||||||
| 130 | 1 | ▶ | die "Last item in order isn't _final! This might indicate a bug in hopen." | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | unless $order[$#order] == $self->_final; | ||||||||||||||
| 132 | 1 | pop @order; | |||||||||||||
| 133 | |||||||||||||||
| 134 | # --- Traverse --- | ||||||||||||||
| 135 | |||||||||||||||
| 136 | # Note: while hacking, please make sure Goal nodes can appear | ||||||||||||||
| 137 | # anywhere in the graph. | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | ▶ | hlog { my $x = 'Traversing DAG ' . $self->name; $x, '*' x (78-length($x)) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||
| 140 | 1 | my $graph = $self->_init_graph; | |||||||||||||
| 141 | 1 | foreach my $node (@init_order, undef, @order) { | |||||||||||||
| 142 | |||||||||||||||
| 143 | 3 | ▶ | if(!defined($node)) { # undef is the marker between init and run | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 1 | $graph = $self->_graph; | |||||||||||||
| 145 | 1 | next; | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | |||||||||||||||
| 148 | # Inputs to this node. These are different from the DAG's inputs. | ||||||||||||||
| 149 | # The scope stack is (outer to inner) DAG's inputs, DAG's overrides, | ||||||||||||||
| 150 | # then $node_inputs, then the individual node's overrides. | ||||||||||||||
| 151 | 2 | my $node_inputs = Build::Hopen::Scope::Hash->new; | |||||||||||||
| 152 | # TODO make this a BH::Scope::Inputs once it's implemented | ||||||||||||||
| 153 | 2 | $node_inputs->outer($self->scope); | |||||||||||||
| 154 | # Data specifically being provided to the current node, e.g., | ||||||||||||||
| 155 | # on input edges, beats the scope of the DAG as a whole. | ||||||||||||||
| 156 | 2 | $node_inputs->local(true); | |||||||||||||
| 157 | # A CollectOp won't reach above the node's inputs by default. | ||||||||||||||
| 158 | |||||||||||||||
| 159 | # Iterate over each node's edges and process any Links | ||||||||||||||
| 160 | 2 | foreach my $pred ($graph->predecessors($node)) { | |||||||||||||
| 161 | 0 | ▶ | hlog { ('From', $pred->name, 'to', $node->name) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 162 | |||||||||||||||
| 163 | # Goals do not feed outputs to other Goals. This is so you can | ||||||||||||||
| 164 | # add edges between Goals to set their order while keeping the | ||||||||||||||
| 165 | # data for each Goal separate. | ||||||||||||||
| 166 | # TODO add tests for this. Also TODO decide whether this is | ||||||||||||||
| 167 | # actually the Right Thing! | ||||||||||||||
| 168 | 1 | ▶ | next if eval { $pred->DOES('Build::Hopen::G::Goal') }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | |||||||||||||||
| 170 | 1 | my $links = $graph->get_edge_attribute($pred, $node, LINKS); | |||||||||||||
| 171 | |||||||||||||||
| 172 | 1 | ▶ | unless($links) { # Simple case: predecessor's outputs become our inputs | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 0 | $node_inputs->add(%{$pred->outputs}); | |||||||||||||
| 174 | 0 | next; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | # More complex case: Process all the links | ||||||||||||||
| 178 | 1 | my $hrPredOutputs = $pred->outputs; | |||||||||||||
| 179 | # In one test, outputs was undef if not on its own line. | ||||||||||||||
| 180 | 1 | my $link_inputs = Build::Hopen::Scope::Hash->new->add(%{$hrPredOutputs}); | |||||||||||||
| 181 | # All links get the same outer scope --- they are parallel, | ||||||||||||||
| 182 | # not in series. | ||||||||||||||
| 183 | 1 | $link_inputs->outer($self->scope); | |||||||||||||
| 184 | # The links run at the same scope level as the node. | ||||||||||||||
| 185 | 1 | $link_inputs->local(true); | |||||||||||||
| 186 | |||||||||||||||
| 187 | # Run the links in series - not parallel! | ||||||||||||||
| 188 | 1 | my $link_outputs = $link_inputs->as_hashref(-levels=>'local'); | |||||||||||||
| 189 | 1 | foreach my $link (@$links) { | |||||||||||||
| 190 | 0 | ▶ | hlog { ('From', $pred->name, 'via', $link->name, 'to', $node->name) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 191 | |||||||||||||||
| 192 | 1 | $link_outputs = $link->run( | |||||||||||||
| 193 | -context=>$link_inputs, | ||||||||||||||
| 194 | forward_opts(\%args, {'-'=>1}, 'phase') | ||||||||||||||
| 195 | # Generator not passed to links. | ||||||||||||||
| 196 | ); | ||||||||||||||
| 197 | } #foreach incoming link | ||||||||||||||
| 198 | |||||||||||||||
| 199 | 1 | $node_inputs->add(%{$link_outputs}); | |||||||||||||
| 200 | # TODO specify which set these are. | ||||||||||||||
| 201 | } #foreach predecessor node | ||||||||||||||
| 202 | |||||||||||||||
| 203 | 2 | my $step_output = $node->run(-context=>$node_inputs, | |||||||||||||
| 204 | forward_opts(\%args, {'-'=>1}, 'phase', 'generator') | ||||||||||||||
| 205 | ); | ||||||||||||||
| 206 | 2 | $node->outputs($step_output); | |||||||||||||
| 207 | |||||||||||||||
| 208 | # Give the Generator a chance, and stash the results if necessary. | ||||||||||||||
| 209 | 2 | ▶ | if(eval { $node->DOES('Build::Hopen::G::Goal') }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 210 | 1 | ▶ | $args{generator}->visit_goal($node) if $args{generator}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 211 | |||||||||||||||
| 212 | # Save the result if there is one. Don't save {}. | ||||||||||||||
| 213 | # use $node->outputs, not $step_output, since the generator may | ||||||||||||||
| 214 | # alter $node->outputs. | ||||||||||||||
| 215 | 1 | ▶ | $retval->{$node->name} = $node->outputs if keys %{$node->outputs}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 216 | } else { | ||||||||||||||
| 217 | 1 | ▶ | $args{generator}->visit_node($node) if $args{generator}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | } | ||||||||||||||
| 219 | |||||||||||||||
| 220 | } #foreach node in topo-sort order | ||||||||||||||
| 221 | |||||||||||||||
| 222 | 1 | return $retval; | |||||||||||||
| 223 | } #run() | ||||||||||||||
| 224 | |||||||||||||||
| 225 | =head1 ADDING DATA | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =head2 goal | ||||||||||||||
| 228 | |||||||||||||||
| 229 | Creates a goal of the DAG. Goals are names for sequences of operations, | ||||||||||||||
| 230 | akin to top-level Makefile targets. Usage: | ||||||||||||||
| 231 | |||||||||||||||
| 232 | my $goalOp = $dag->goal('name') | ||||||||||||||
| 233 | |||||||||||||||
| 234 | Returns a passthrough operation representing the goal. Any inputs passed into | ||||||||||||||
| 235 | that operation are provided as outputs of the DAG under the corresponding name. | ||||||||||||||
| 236 | |||||||||||||||
| 237 | The first call to C<goal()> also sets L</default_goal>. | ||||||||||||||
| 238 | |||||||||||||||
| 239 | =cut | ||||||||||||||
| 240 | |||||||||||||||
| 241 | 3 | ▶ | sub goal { | ||||||||||||
|
Subroutine
goal: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 242 | 3 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 243 | 3 | ▶ | my $name = shift or croak 'Need a goal name'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 244 | 3 | my $goal = Build::Hopen::G::Goal->new(name => $name); | |||||||||||||
| 245 | 3 | $self->_graph->add_vertex($goal); | |||||||||||||
| 246 | #$self->_node_by_name->{$name} = $goal; | ||||||||||||||
| 247 | 3 | $self->_graph->add_edge($goal, $self->_final); | |||||||||||||
| 248 | 3 | ▶ | $self->default_goal($goal) unless $self->default_goal; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 249 | 3 | return $goal; | |||||||||||||
| 250 | } #goal() | ||||||||||||||
| 251 | |||||||||||||||
| 252 | =head2 connect | ||||||||||||||
| 253 | |||||||||||||||
| 254 | - C<DAG:connect(<op1>, <out-edge>, <in-edge>, <op2>)>: | ||||||||||||||
| 255 | connects output C<< out-edge >> of operation C<< op1 >> as input C<< in-edge >> of | ||||||||||||||
| 256 | operation C<< op2 >>. No processing is done between output and input. | ||||||||||||||
| 257 | - C<< out-edge >> and C<< in-edge >> can be anything usable as a table index, | ||||||||||||||
| 258 | provided that table index appears in the corresponding operation's | ||||||||||||||
| 259 | descriptor. | ||||||||||||||
| 260 | - C<DAG:connect(<op1>, <op2>)>: creates a dependency edge from C<< op1 >> to | ||||||||||||||
| 261 | C<< op2 >>, indicating that C<< op1 >> must be run before C<< op2 >>. | ||||||||||||||
| 262 | Does not transfer any data from C<< op1 >> to C<< op2 >>. | ||||||||||||||
| 263 | - C<DAG:connect(<op1>, <Link>, <op2>)>: Connects C<< op1 >> to | ||||||||||||||
| 264 | C<< op2 >> via L<Build::Hopen::G::Link> C<< Link >>. | ||||||||||||||
| 265 | |||||||||||||||
| 266 | TODO return the name of the edge? The edge instance itself? Maybe a | ||||||||||||||
| 267 | fluent interface to the DAG for chaining C<connect> calls? | ||||||||||||||
| 268 | |||||||||||||||
| 269 | =cut | ||||||||||||||
| 270 | |||||||||||||||
| 271 | 1 | ▶ | sub connect { | ||||||||||||
|
Subroutine
connect: called CC=6 SCAR=20.5
Pod
documented
| |||||||||||||||
| 272 | 1 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 273 | 1 | my ($op1, $out_edge, $in_edge, $op2) = @_; | |||||||||||||
| 274 | |||||||||||||||
| 275 | 1 | my $link; | |||||||||||||
| 276 | 1 | ▶ | if(!defined($in_edge)) { # dependency edge | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 277 | 0 | $op2 = $out_edge; | |||||||||||||
| 278 | 0 | $out_edge = false; # No outputs | |||||||||||||
| 279 | 0 | $in_edge = false; # No inputs | |||||||||||||
| 280 | } elsif(!defined($op2)) { | ||||||||||||||
| 281 | 1 | $op2 = $in_edge; | |||||||||||||
| 282 | 1 | $link = $out_edge; | |||||||||||||
| 283 | 1 | $out_edge = false; # No outputs TODO | |||||||||||||
| 284 | 1 | $in_edge = false; # No inputs TODO | |||||||||||||
| 285 | } | ||||||||||||||
| 286 | |||||||||||||||
| 287 | # # Create the link | ||||||||||||||
| 288 | # unless($link) { | ||||||||||||||
| 289 | # $link = Build::Hopen::G::Link->new( | ||||||||||||||
| 290 | # name => 'link_' . $op1->name . '_' . $op2->name, | ||||||||||||||
| 291 | # in => [$out_edge], # Output of op1 | ||||||||||||||
| 292 | # out => [$in_edge], # Input to op2 | ||||||||||||||
| 293 | # ); | ||||||||||||||
| 294 | # } | ||||||||||||||
| 295 | |||||||||||||||
| 296 | 0 | ▶ | hlog { 'DAG::connect(): Edge from', $op1->name, | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||
| 297 | 'via', $link ? $link->name : '(no link)', | ||||||||||||||
| 298 | 1 | 'to', $op2->name }; | |||||||||||||
| 299 | |||||||||||||||
| 300 | # Add it to the graph (idempotent) | ||||||||||||||
| 301 | 1 | $self->_graph->add_edge($op1, $op2); | |||||||||||||
| 302 | #$self->_node_by_name->{$_->name} = $_ foreach ($op1, $op2); | ||||||||||||||
| 303 | |||||||||||||||
| 304 | # Save the BHG::Link as an edge attribute (not idempotent!) | ||||||||||||||
| 305 | 1 | ▶ | my $attrs = $self->_graph->get_edge_attribute($op1, $op2, LINKS) || []; | ||||||||||||
|
Condition50%1/2
$self->_graph->get_edge_attribute($op1, $op2, 'link_list') || []
MC/DC0%0/1
$self->_graph->get_edge_attribute($op1, $op2, 'link_list') || []
$self->_graph->get_edge_attribute($op1, $op2, 'link_list')
Truth table50%1/2
A
A= $self->_graph->get_edge_attribute($op1, $op2, 'link_list')
| |||||||||||||||
| 306 | 1 | ▶ | push @$attrs, $link if $link; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 307 | 1 | $self->_graph->set_edge_attribute($op1, $op2, LINKS, $attrs); | |||||||||||||
| 308 | |||||||||||||||
| 309 | 1 | return undef; # TODO decide what to return | |||||||||||||
| 310 | } #connect() | ||||||||||||||
| 311 | |||||||||||||||
| 312 | =head2 add | ||||||||||||||
| 313 | |||||||||||||||
| 314 | Add a regular node to the graph. An attempt to add the same node twice will be | ||||||||||||||
| 315 | ignored. Usage: | ||||||||||||||
| 316 | |||||||||||||||
| 317 | my $node = Build::Hopen::G::Op->new(name=>"whatever"); | ||||||||||||||
| 318 | $dag->add($node); | ||||||||||||||
| 319 | |||||||||||||||
| 320 | Returns the node, for the sake of chaining. | ||||||||||||||
| 321 | |||||||||||||||
| 322 | =cut | ||||||||||||||
| 323 | |||||||||||||||
| 324 | 0 | ▶ | sub add { | ||||||||||||
|
Subroutine
add: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 325 | 0 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 326 | 0 | ▶ | my $node = shift or croak 'Need a node'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 327 | 0 | ▶ | return if $self->_graph->has_vertex($node); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 328 | 0 | ▶ | hlog { __PACKAGE__, 'adding', Dumper($node) } 2; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 329 | |||||||||||||||
| 330 | 0 | $self->_graph->add_vertex($node); | |||||||||||||
| 331 | #$self->_node_by_name->{$node->name} = $node if $node->name; | ||||||||||||||
| 332 | |||||||||||||||
| 333 | 0 | return $node; | |||||||||||||
| 334 | } #add() | ||||||||||||||
| 335 | |||||||||||||||
| 336 | =head2 init | ||||||||||||||
| 337 | |||||||||||||||
| 338 | Add an initialization operation to the graph. Initialization operations run | ||||||||||||||
| 339 | before all other operations. An attempt to add the same initialization | ||||||||||||||
| 340 | operation twice will be ignored. Usage: | ||||||||||||||
| 341 | |||||||||||||||
| 342 | my $op = Build::Hopen::G::Op->new(name=>"whatever"); | ||||||||||||||
| 343 | $dag->init($op[, $first]); | ||||||||||||||
| 344 | |||||||||||||||
| 345 | If C<$first> is truthy, the op will be run before anything already in the | ||||||||||||||
| 346 | graph. However, later calls to C<init()> with C<$first> set will push | ||||||||||||||
| 347 | operations even before C<$op>. | ||||||||||||||
| 348 | |||||||||||||||
| 349 | Returns the node, for the sake of chaining. | ||||||||||||||
| 350 | |||||||||||||||
| 351 | =cut | ||||||||||||||
| 352 | |||||||||||||||
| 353 | 0 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 354 | 0 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 355 | 0 | ▶ | my $op = shift or croak 'Need an op'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 356 | 0 | my $first = shift; | |||||||||||||
| 357 | 0 | ▶ | return if $self->_init_graph->has_vertex($op); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 358 | |||||||||||||||
| 359 | 0 | $self->_init_graph->add_vertex($op); | |||||||||||||
| 360 | #$self->_node_by_name->{$op->name} = $op; | ||||||||||||||
| 361 | |||||||||||||||
| 362 | 0 | ▶ | if($first) { # $op becomes the new _init_first node | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 363 | 0 | $self->_init_graph->add_edge($op, $self->_init_first); | |||||||||||||
| 364 | 0 | $self->_init_first($op); | |||||||||||||
| 365 | } else { # Not first, so can happen anytime. Add it after the | ||||||||||||||
| 366 | # current first node. | ||||||||||||||
| 367 | 0 | $self->_init_graph->add_edge($self->_init_first, $op); | |||||||||||||
| 368 | } | ||||||||||||||
| 369 | |||||||||||||||
| 370 | 0 | return $op; | |||||||||||||
| 371 | } #init() | ||||||||||||||
| 372 | |||||||||||||||
| 373 | =head1 ACCESSORS | ||||||||||||||
| 374 | |||||||||||||||
| 375 | =head2 empty | ||||||||||||||
| 376 | |||||||||||||||
| 377 | Returns truthy if the only nodes in the graph are internal nodes. | ||||||||||||||
| 378 | Intended for use by hopen files. | ||||||||||||||
| 379 | |||||||||||||||
| 380 | =cut | ||||||||||||||
| 381 | |||||||||||||||
| 382 | 0 | ▶ | sub empty { | ||||||||||||
|
Subroutine
empty: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 383 | 0 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 384 | 0 | return ($self->_graph->vertices == 1); | |||||||||||||
| 385 | # _final is the node in an empty() graph. | ||||||||||||||
| 386 | # We don't check the _init_graph since empty() is intended | ||||||||||||||
| 387 | # for use by hopen files, not toolsets. | ||||||||||||||
| 388 | } #empty() | ||||||||||||||
| 389 | |||||||||||||||
| 390 | =head1 OTHER | ||||||||||||||
| 391 | |||||||||||||||
| 392 | =head2 BUILD | ||||||||||||||
| 393 | |||||||||||||||
| 394 | Initialize the instance. | ||||||||||||||
| 395 | |||||||||||||||
| 396 | =cut | ||||||||||||||
| 397 | |||||||||||||||
| 398 | 2 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 399 | #use Data::Dumper; | ||||||||||||||
| 400 | #say Dumper(\@_); | ||||||||||||||
| 401 | 2 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 402 | 2 | my $hrArgs = shift; | |||||||||||||
| 403 | |||||||||||||||
| 404 | # DAGs always have names | ||||||||||||||
| 405 | 2 | ▶ | $self->name('__R_DAG_' . $_id_counter++) unless $self->has_custom_name; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 406 | |||||||||||||||
| 407 | # Graph of normal operations | ||||||||||||||
| 408 | 2 | my $graph = Graph->new( directed => true, | |||||||||||||
| 409 | refvertexed => true); | ||||||||||||||
| 410 | 2 | my $final = Build::Hopen::G::Node->new( | |||||||||||||
| 411 | name => '__R_DAG_ROOT' . $_id_counter++); | ||||||||||||||
| 412 | 2 | $graph->add_vertex($final); | |||||||||||||
| 413 | 2 | $self->_graph($graph); | |||||||||||||
| 414 | 2 | $self->_final($final); | |||||||||||||
| 415 | |||||||||||||||
| 416 | # Graph of initialization operations | ||||||||||||||
| 417 | 2 | my $init_graph = Graph->new( directed => true, | |||||||||||||
| 418 | refvertexed => true); | ||||||||||||||
| 419 | 2 | my $init = Build::Hopen::G::CollectOp->new( | |||||||||||||
| 420 | name => '__R_DAG_INIT' . $_id_counter++); | ||||||||||||||
| 421 | 2 | $init_graph->add_vertex($init); | |||||||||||||
| 422 | |||||||||||||||
| 423 | 2 | $self->_init_graph($init_graph); | |||||||||||||
| 424 | 2 | $self->_init_first($init); | |||||||||||||
| 425 | } #BUILD() | ||||||||||||||
| 426 | |||||||||||||||
| 427 | 2 | 1; | |||||||||||||
| 428 | # Rest of the docs {{{1 | ||||||||||||||
| 429 | __END__ | ||||||||||||||