File Coverage

blib/lib/Algorithm/Dependency/Ordered.pm
Criterion Covered Total %
statement 41 41 100.0
branch 12 16 75.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package Algorithm::Dependency::Ordered;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Algorithm::Dependency::Ordered - Implements an ordered dependency heirachy
8              
9             =head1 DESCRIPTION
10              
11             Algorithm::Dependency::Ordered implements the most common variety of
12             L, the one in which the dependencies of an item must
13             be acted upon before the item itself can be acted upon.
14              
15             In use and semantics, this should be used in exactly the same way as for the
16             main parent class. Please note that the output of the C method is
17             NOT changed, as the order of the depends is not assumed to be important.
18             Only the output of the C method is modified to ensure the correct
19             order.
20              
21             For API details, see L.
22              
23             =cut
24              
25 4     4   42455 use 5.005;
  4         13  
  4         159  
26 4     4   24 use strict;
  4         6  
  4         127  
27 4     4   873 use Algorithm::Dependency ();
  4         10  
  4         79  
28              
29 4     4   22 use vars qw{$VERSION @ISA};
  4         7  
  4         269  
30             BEGIN {
31 4     4   15 $VERSION = '1.110';
32 4         1319 @ISA = 'Algorithm::Dependency';
33             }
34              
35              
36              
37              
38              
39             sub schedule {
40 48     48 1 52432 my $self = shift;
41 48         89 my $source = $self->{source};
42 48 50       147 my @items = @_ or return undef;
43 48 50       73 return undef if grep { ! $source->item($_) } @items;
  48         141  
44              
45             # The actual items to select will be the same as for the unordered
46             # version, so we can simplify the algorithm greatly by using the
47             # normal unordered ->schedule method to get the starting list.
48 48         177 my $rv = $self->SUPER::schedule( @items );
49 48 100       287 my @queue = $rv ? @$rv : return undef;
50              
51             # Get a working copy of the selected index
52 47         63 my %selected = %{ $self->{selected} };
  47         180  
53              
54             # If at any time we check every item in the stack without finding
55             # a suitable candidate for addition to the schedule, we have found
56             # a circular reference error. We need to create a marker to track this.
57 47         70 my $error_marker = '';
58              
59             # Begin the processing loop
60 47         70 my @schedule = ();
61 47         113 while ( my $id = shift @queue ) {
62             # Have we checked every item in the stack?
63 135 50       228 return undef if $id eq $error_marker;
64              
65             # Are there any un-met dependencies
66 135 50       382 my $Item = $self->{source}->item($id) or return undef;
67 135         331 my @missing = grep { ! $selected{$_} } $Item->depends;
  151         365  
68              
69             # Remove orphans if we are ignoring them
70 135 100       299 if ( $self->{ignore_orphans} ) {
71 1         2 @missing = grep { $self->{source}->item($_) } @missing;
  1         170  
72             }
73              
74 135 100       279 if ( @missing ) {
75             # Set the error marker if not already
76 41 100       79 $error_marker = $id unless $error_marker;
77              
78             # Add the id back to the end of the queue
79 41         61 push @queue, $id;
80 41         126 next;
81             }
82              
83             # All dependencies have been met. Add the item to the schedule and
84             # to the selected index, and clear the error marker.
85 94         155 push @schedule, $id;
86 94         145 $selected{$id} = 1;
87 94         278 $error_marker = '';
88             }
89              
90             # All items have been added
91 47         194 \@schedule;
92             }
93              
94             1;
95              
96             =pod
97              
98             =head1 SUPPORT
99              
100             Bugs should be submitted via the CPAN bug tracker, located at
101              
102             L
103              
104             For general comments, contact the author.
105              
106             =head1 AUTHOR
107              
108             Adam Kennedy Eadamk@cpan.orgE
109              
110             =head1 SEE ALSO
111              
112             L
113              
114             =head1 COPYRIGHT
115              
116             Copyright 2003 - 2009 Adam Kennedy.
117              
118             This program is free software; you can redistribute
119             it and/or modify it under the same terms as Perl itself.
120              
121             The full text of the license can be found in the
122             LICENSE file included with this module.
123              
124             =cut