Algorithm-Dependency

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

                     "script",
                     "t",
                     "xt"
                  ],
                  "spell_cmd" : "",
                  "stopwords" : [
                     "ARRAYs",
                     "irc",
                     "param",
                     "readonly",
                     "unselected"
                  ],
                  "wordlist" : "Pod::Wordlist"
               }
            },
            "name" : "@Author::ETHER/Test::PodSpelling",
            "version" : "2.007005"
         },
         {
            "class" : "Dist::Zilla::Plugin::Test::Pod::No404s",
            "name" : "@Author::ETHER/Test::Pod::No404s",

META.yml  view on Meta::CPAN

            - lib
            - script
            - t
            - xt
          spell_cmd: ''
          stopwords:
            - ARRAYs
            - irc
            - param
            - readonly
            - unselected
          wordlist: Pod::Wordlist
      name: '@Author::ETHER/Test::PodSpelling'
      version: '2.007005'
    -
      class: Dist::Zilla::Plugin::Test::Pod::No404s
      name: '@Author::ETHER/Test::Pod::No404s'
      version: '1.004'
    -
      class: Dist::Zilla::Plugin::Test::Kwalitee
      config:

dist.ini  view on Meta::CPAN


[@Author::ETHER]
:version = 0.119
authority = cpan:ADAMK
installer = MakeMaker
-remove = Test::EOL
-remove = Test::NoTabs
-remove = Test::CleanNamespaces ; TODO
Test::PodSpelling.stopwords[0] = param
Test::PodSpelling.stopwords[1] = readonly
Test::PodSpelling.stopwords[2] = unselected
Test::PodSpelling.stopwords[3] = ARRAYs

[Prereqs]
Params::Util = 0.31
List::Util = 1.11

[Prereqs / TestRequires]
Test::More = 0.47
File::Spec = 0.80
Test::ClassAPI = 0.6

lib/Algorithm/Dependency.pm  view on Meta::CPAN

#pod     core  => [ ],
#pod     a     => [ 'core' ],
#pod     b     => [ 'a' ]
#pod     this  => [ ],
#pod     that  => [ ],
#pod   };
#pod   my $deps_source = Algorithm::Dependency::Source::HoA->new( $deps );
#pod
#pod   my $dep = Algorithm::Dependency::Ordered->new(
#pod     source   => $deps_source,
#pod     selected => [ 'this', 'that' ], # Items we have processed elsewhere or have already satisfied
#pod   )
#pod   or die 'Failed to set up dependency algorithm';
#pod
#pod   my $also = $dep->schedule_all();
#pod   # Returns: ['core', 'a', 'b'] -- ie: installation-order. Whereas using base
#pod   # Algorithm::Dependency would return sorted ['a', 'b', 'core']
#pod
#pod   my $also = $dep->schedule( 'b' );
#pod   # Returns: ['core', 'a', 'b'] -- installation order, including ourselves
#pod

lib/Algorithm/Dependency.pm  view on Meta::CPAN

#pod
#pod Base Classes
#pod
#pod   use Algorithm::Dependency;
#pod   use Algorithm::Dependency::Source::File;
#pod   
#pod   # Load the data from a simple text file
#pod   my $data_source = Algorithm::Dependency::Source::File->new( 'foo.txt' );
#pod   
#pod   # Create the dependency object, and indicate the items that are already
#pod   # selected/installed/etc in the database
#pod   my $dep = Algorithm::Dependency->new(
#pod       source   => $data_source,
#pod       selected => [ 'This', 'That' ]
#pod   ) or die 'Failed to set up dependency algorithm';
#pod   
#pod   # For the item 'Foo', find out the other things we also have to select.
#pod   # This WON'T include the item we selected, 'Foo'.
#pod   my $also = $dep->depends( 'Foo' );
#pod   print $also
#pod   	? "By selecting 'Foo', you are also selecting the following items: "
#pod   		. join( ', ', @$also )
#pod   	: "Nothing else to select for 'Foo'";
#pod   
#pod   # Find out the order we need to act on the items in.
#pod   # This WILL include the item we selected, 'Foo'.
#pod   my $schedule = $dep->schedule( 'Foo' );
#pod
#pod =head1 DESCRIPTION
#pod
#pod Algorithm::Dependency is a framework for creating simple read-only
#pod dependency hierarchies, where you have a set of items that rely on other
#pod items in the set, and require actions on them as well.
#pod
#pod Despite the most visible of these being software installation systems like
#pod the CPAN installer, or Debian apt-get, they are useful in other situations.

lib/Algorithm/Dependency.pm  view on Meta::CPAN

#pod items. This will be very application specific, and might be a flat file,
#pod some form of database, the list of files in a folder, or generated
#pod dynamically.
#pod
#pod =head2 General Description
#pod
#pod =for stopwords versioned
#pod
#pod Algorithm::Dependency implements algorithms relating to dependency
#pod hierarchies. To use this framework, all you need is a source for the master
#pod list of all the items, and a list of those already selected. If your
#pod dependency hierarchy doesn't require the concept of items that are already
#pod selected, simply don't pass anything to the constructor for it.
#pod
#pod Please note that the class Algorithm::Dependency does NOT implement an
#pod ordering, for speed and simplicity reasons. That is, the C<schedule> it
#pod provides is not in any particular order. If item 'A' depends on item 'B',
#pod it will not place B before A in the schedule. This makes it unsuitable for
#pod things like software installers, as they typically would need B to be
#pod installed before A, or the installation of A would fail.
#pod
#pod For dependency hierarchies requiring the items to be acted on in a particular
#pod order, either top down or bottom up, see L<Algorithm::Dependency::Ordered>.

lib/Algorithm/Dependency.pm  view on Meta::CPAN

#pod
#pod =over 4
#pod
#pod =item source => $Source
#pod
#pod The only compulsory option is the source of the dependency items. This is
#pod an object of a subclass of L<Algorithm::Dependency::Source>. In practical terms,
#pod this means you will create the source object before creating the
#pod Algorithm::Dependency object.
#pod
#pod =item selected => [ 'A', 'B', 'C', etc... ]
#pod
#pod The C<selected> option provides a list of those items that have already been
#pod 'selected', acted upon, installed, or whatever. If another item depends on one
#pod in this list, we don't have to include it in the output of the C<schedule> or
#pod C<depends> methods.
#pod
#pod =item ignore_orphans => 1
#pod
#pod Normally, the item source is expected to be largely perfect and error free.
#pod An 'orphan' is an item name that appears as a dependency of another item, but
#pod doesn't exist, or has been deleted.
#pod
#pod By providing the C<ignore_orphans> flag, orphans are simply ignored. Without

lib/Algorithm/Dependency.pm  view on Meta::CPAN


sub new {
	my $class  = shift;
	my %args   = @_;
	my $source = _INSTANCE($args{source}, 'Algorithm::Dependency::Source')
		or return undef;

	# Create the object
	my $self = bless {
		source   => $source, # Source object
		selected => {},
		}, $class;

	# Were we given the 'ignore_orphans' flag?
	if ( $args{ignore_orphans} ) {
		$self->{ignore_orphans} = 1;
	}

	# Done, unless we have been given some selected items
	_ARRAY($args{selected}) or return $self;

	# Make sure each of the selected ids exists
	my %selected = ();
	foreach my $id ( @{ $args{selected} } ) {
		# Does the item exist?
		return undef unless $source->item($id);

		# Is it a duplicate
		return undef if $selected{$id};

		# Add to the selected index
		$selected{$id} = 1;
	}

	$self->{selected} = \%selected;
	$self;
}





#####################################################################
# Basic methods

lib/Algorithm/Dependency.pm  view on Meta::CPAN

#pod
#pod The C<source> method retrieves the L<Algorithm::Dependency::Source> object
#pod for the algorithm context.
#pod
#pod =cut

sub source { $_[0]->{source} }

#pod =pod
#pod
#pod =head2 selected_list
#pod
#pod The C<selected_list> method returns, as a list and in alphabetical order,
#pod the list of the names of the selected items.
#pod
#pod =cut

sub selected_list { sort keys %{$_[0]->{selected}} }

#pod =pod
#pod
#pod =head2 selected $name
#pod
#pod Given an item name, the C<selected> method will return true if the item is
#pod selected, false is not, or C<undef> if the item does not exist, or an error
#pod occurs.
#pod
#pod =cut

sub selected { $_[0]->{selected}->{$_[1]} }

#pod =pod
#pod
#pod =head2 item $name
#pod
#pod The C<item> method fetches and returns the item object, as specified by the
#pod name argument.
#pod
#pod Returns an L<Algorithm::Dependency::Item> object on success, or C<undef> if
#pod an item does not exist for the argument provided.

lib/Algorithm/Dependency.pm  view on Meta::CPAN


#####################################################################
# Main algorithm methods

#pod =pod
#pod
#pod =head2 depends $name1, ..., $nameN
#pod
#pod Given a list of one or more item names, the C<depends> method will return
#pod a reference to an array containing a list of the names of all the OTHER
#pod items that also have to be selected to meet dependencies.
#pod
#pod That is, if item A depends on B and C then the C<depends> method would
#pod return a reference to an array with B and C. ( C<[ 'B', 'C' ]> )
#pod
#pod If multiple item names are provided, the same applies. The list returned
#pod will not contain duplicates.
#pod
#pod The method returns a reference to an array of item names on success, a
#pod reference to an empty array if no other items are needed, or C<undef>
#pod on error.

lib/Algorithm/Dependency.pm  view on Meta::CPAN

	my @stack   = @_ or return undef;
	my @depends = ();
	my %checked = ();

	# Process the stack
	while ( my $id = shift @stack ) {
		# Does the id exist?
		my $Item = $self->{source}->item($id)
		or $self->{ignore_orphans} ? next : return undef;

		# Skip if selected or checked
		next if $checked{$id};

		# Add its depends to the stack
		push @stack, $Item->depends;
		$checked{$id} = 1;

		# Add anything to the final output that wasn't one of
		# the original input.
		unless ( scalar grep { $id eq $_ } @_ ) {
			push @depends, $id;
		}
	}

	# Remove any items already selected
	my $s = $self->{selected};
	return [ sort grep { ! $s->{$_} } @depends ];
}

#pod =pod
#pod
#pod =head2 schedule $name1, ..., $nameN
#pod
#pod Given a list of one or more item names, the C<depends> method will
#pod return, as a reference to an array, the ordered list of items you
#pod should act upon in whichever order this particular dependency handler
#pod uses - see L<Algorithm::Dependency::Ordered> for one that implements
#pod the most common ordering (process sequence).
#pod
#pod This would be the original names provided, plus those added to satisfy
#pod dependencies, in the preferred order of action. For the normal algorithm,
#pod where order it not important, this is alphabetical order. This makes it
#pod easier for someone watching a program operate on the items to determine
#pod how far you are through the task and makes any logs easier to read.
#pod
#pod If any of the names you provided in the arguments is already selected, it
#pod will not be included in the list.
#pod
#pod The method returns a reference to an array of item names on success, a
#pod reference to an empty array if no items need to be acted upon, or C<undef>
#pod on error.
#pod
#pod =cut

sub schedule {
	my $self  = shift;
	my @items = @_ or return undef;

	# Get their dependencies
	my $depends = $self->depends( @items ) or return undef;

	# Now return a combined list, removing any items already selected.
	# We are allowed to return an empty list.
	my $s = $self->{selected};
	return [ sort grep { ! $s->{$_} } @items, @$depends ];
}

#pod =pod
#pod
#pod =head2 schedule_all;
#pod
#pod The C<schedule_all> method acts the same as the C<schedule> method, but 
#pod returns a schedule that selected all the so-far unselected items.
#pod
#pod =cut

sub schedule_all {
	my $self = shift;
	$self->schedule( map { $_->id } $self->source->items );
}

1;

lib/Algorithm/Dependency.pm  view on Meta::CPAN

    core  => [ ],
    a     => [ 'core' ],
    b     => [ 'a' ]
    this  => [ ],
    that  => [ ],
  };
  my $deps_source = Algorithm::Dependency::Source::HoA->new( $deps );

  my $dep = Algorithm::Dependency::Ordered->new(
    source   => $deps_source,
    selected => [ 'this', 'that' ], # Items we have processed elsewhere or have already satisfied
  )
  or die 'Failed to set up dependency algorithm';

  my $also = $dep->schedule_all();
  # Returns: ['core', 'a', 'b'] -- ie: installation-order. Whereas using base
  # Algorithm::Dependency would return sorted ['a', 'b', 'core']

  my $also = $dep->schedule( 'b' );
  # Returns: ['core', 'a', 'b'] -- installation order, including ourselves

lib/Algorithm/Dependency.pm  view on Meta::CPAN


Base Classes

  use Algorithm::Dependency;
  use Algorithm::Dependency::Source::File;
  
  # Load the data from a simple text file
  my $data_source = Algorithm::Dependency::Source::File->new( 'foo.txt' );
  
  # Create the dependency object, and indicate the items that are already
  # selected/installed/etc in the database
  my $dep = Algorithm::Dependency->new(
      source   => $data_source,
      selected => [ 'This', 'That' ]
  ) or die 'Failed to set up dependency algorithm';
  
  # For the item 'Foo', find out the other things we also have to select.
  # This WON'T include the item we selected, 'Foo'.
  my $also = $dep->depends( 'Foo' );
  print $also
  	? "By selecting 'Foo', you are also selecting the following items: "
  		. join( ', ', @$also )
  	: "Nothing else to select for 'Foo'";
  
  # Find out the order we need to act on the items in.
  # This WILL include the item we selected, 'Foo'.
  my $schedule = $dep->schedule( 'Foo' );

=head1 DESCRIPTION

Algorithm::Dependency is a framework for creating simple read-only
dependency hierarchies, where you have a set of items that rely on other
items in the set, and require actions on them as well.

Despite the most visible of these being software installation systems like
the CPAN installer, or Debian apt-get, they are useful in other situations.

lib/Algorithm/Dependency.pm  view on Meta::CPAN

items. This will be very application specific, and might be a flat file,
some form of database, the list of files in a folder, or generated
dynamically.

=head2 General Description

=for stopwords versioned

Algorithm::Dependency implements algorithms relating to dependency
hierarchies. To use this framework, all you need is a source for the master
list of all the items, and a list of those already selected. If your
dependency hierarchy doesn't require the concept of items that are already
selected, simply don't pass anything to the constructor for it.

Please note that the class Algorithm::Dependency does NOT implement an
ordering, for speed and simplicity reasons. That is, the C<schedule> it
provides is not in any particular order. If item 'A' depends on item 'B',
it will not place B before A in the schedule. This makes it unsuitable for
things like software installers, as they typically would need B to be
installed before A, or the installation of A would fail.

For dependency hierarchies requiring the items to be acted on in a particular
order, either top down or bottom up, see L<Algorithm::Dependency::Ordered>.

lib/Algorithm/Dependency.pm  view on Meta::CPAN


=over 4

=item source => $Source

The only compulsory option is the source of the dependency items. This is
an object of a subclass of L<Algorithm::Dependency::Source>. In practical terms,
this means you will create the source object before creating the
Algorithm::Dependency object.

=item selected => [ 'A', 'B', 'C', etc... ]

The C<selected> option provides a list of those items that have already been
'selected', acted upon, installed, or whatever. If another item depends on one
in this list, we don't have to include it in the output of the C<schedule> or
C<depends> methods.

=item ignore_orphans => 1

Normally, the item source is expected to be largely perfect and error free.
An 'orphan' is an item name that appears as a dependency of another item, but
doesn't exist, or has been deleted.

By providing the C<ignore_orphans> flag, orphans are simply ignored. Without

lib/Algorithm/Dependency.pm  view on Meta::CPAN

=back

The C<new> constructor returns a new Algorithm::Dependency object on success,
or C<undef> on error.

=head2 source

The C<source> method retrieves the L<Algorithm::Dependency::Source> object
for the algorithm context.

=head2 selected_list

The C<selected_list> method returns, as a list and in alphabetical order,
the list of the names of the selected items.

=head2 selected $name

Given an item name, the C<selected> method will return true if the item is
selected, false is not, or C<undef> if the item does not exist, or an error
occurs.

=head2 item $name

The C<item> method fetches and returns the item object, as specified by the
name argument.

Returns an L<Algorithm::Dependency::Item> object on success, or C<undef> if
an item does not exist for the argument provided.

=head2 depends $name1, ..., $nameN

Given a list of one or more item names, the C<depends> method will return
a reference to an array containing a list of the names of all the OTHER
items that also have to be selected to meet dependencies.

That is, if item A depends on B and C then the C<depends> method would
return a reference to an array with B and C. ( C<[ 'B', 'C' ]> )

If multiple item names are provided, the same applies. The list returned
will not contain duplicates.

The method returns a reference to an array of item names on success, a
reference to an empty array if no other items are needed, or C<undef>
on error.

lib/Algorithm/Dependency.pm  view on Meta::CPAN

should act upon in whichever order this particular dependency handler
uses - see L<Algorithm::Dependency::Ordered> for one that implements
the most common ordering (process sequence).

This would be the original names provided, plus those added to satisfy
dependencies, in the preferred order of action. For the normal algorithm,
where order it not important, this is alphabetical order. This makes it
easier for someone watching a program operate on the items to determine
how far you are through the task and makes any logs easier to read.

If any of the names you provided in the arguments is already selected, it
will not be included in the list.

The method returns a reference to an array of item names on success, a
reference to an empty array if no items need to be acted upon, or C<undef>
on error.

=head2 schedule_all;

The C<schedule_all> method acts the same as the C<schedule> method, but 
returns a schedule that selected all the so-far unselected items.

=head1 TO DO

Add the C<check_source> method, to verify the integrity of the source.

Possibly add Algorithm::Dependency::Versions, to implement an ordered
dependency tree with versions, like for perl modules.

Currently readonly. Make the whole thing writable, so the module can be
used as the core of an actual dependency application, as opposed to just

lib/Algorithm/Dependency/Ordered.pm  view on Meta::CPAN

our $VERSION = '1.112';
our @ISA     = 'Algorithm::Dependency';


sub schedule {
	my $self   = shift;
	my $source = $self->{source};
	my @items  = @_ or return undef;
	return undef if grep { ! $source->item($_) } @items;

	# The actual items to select will be the same as for the unordered
	# version, so we can simplify the algorithm greatly by using the
	# normal unordered ->schedule method to get the starting list.
	my $rv    = $self->SUPER::schedule( @items );
	my @queue = $rv ? @$rv : return undef;

	# Get a working copy of the selected index
	my %selected = %{ $self->{selected} };

	# If at any time we check every item in the stack without finding
	# a suitable candidate for addition to the schedule, we have found
	# a circular reference error. We need to create a marker to track this.
	my $error_marker = '';

	# Begin the processing loop
	my @schedule = ();
	while ( my $id = shift @queue ) {
		# Have we checked every item in the stack?
		return undef if $id eq $error_marker;

		# Are there any un-met dependencies
		my $Item    = $self->{source}->item($id) or return undef;
		my @missing = grep { ! $selected{$_} } $Item->depends;

		# Remove orphans if we are ignoring them
		if ( $self->{ignore_orphans} ) {
			@missing = grep { $self->{source}->item($_) } @missing;
		}

		if ( @missing ) {
			# Set the error marker if not already
			$error_marker = $id unless $error_marker;

			# Add the id back to the end of the queue
			push @queue, $id;
			next;
		}

		# All dependencies have been met. Add the item to the schedule and
		# to the selected index, and clear the error marker.
		push @schedule, $id;
		$selected{$id} = 1;
		$error_marker  = '';
	}

	# All items have been added
	\@schedule;
}

1;

__END__

lib/Algorithm/Dependency/Weight.pm  view on Meta::CPAN

#pod   
#pod   # Find the naive weight for an item
#pod   my $weight = scalar($dependency->schedule('itemname'));
#pod
#pod C<Algorithm::Dependency::Weight> provides a way of doing this
#pod with a little more sophistication, and in a way that should work
#pod reasonable well across all the L<Algorithm::Dependency> family.
#pod
#pod Please note that the this might be a little (or more than a little)
#pod slower than it could be for the limited case of generating weights
#pod for all of the items at once in a dependency system with no selected
#pod items and no circular dependencies. BUT you can at least rely on
#pod this class to do the job properly regardless of the particulars of
#pod the situation, which is probably more important.
#pod
#pod =head2 METHODS
#pod
#pod =cut

use 5.005;
use strict;

lib/Algorithm/Dependency/Weight.pm  view on Meta::CPAN

  
  # Find the naive weight for an item
  my $weight = scalar($dependency->schedule('itemname'));

C<Algorithm::Dependency::Weight> provides a way of doing this
with a little more sophistication, and in a way that should work
reasonable well across all the L<Algorithm::Dependency> family.

Please note that the this might be a little (or more than a little)
slower than it could be for the limited case of generating weights
for all of the items at once in a dependency system with no selected
items and no circular dependencies. BUT you can at least rely on
this class to do the job properly regardless of the particulars of
the situation, which is probably more important.

=head2 METHODS

=head2 new @params

The C<new> constructor creates a new C<Algorithm::Dependency::Weight>
object. It takes a number of key/value pairs as parameters (although

t/02_api.t  view on Meta::CPAN

Algorithm::Dependency=class
Algorithm::Dependency::Item=abstract
Algorithm::Dependency::Ordered=class
Algorithm::Dependency::Source=abstract
Algorithm::Dependency::Source::File=class
Algorithm::Dependency::Source::HoA=class

[Algorithm::Dependency]
new=method
source=method
selected_list=method
selected=method
item=method
depends=method
schedule=method
schedule_all=method

[Algorithm::Dependency::Item]
new=method
id=method
depends=method

t/03_basics.t  view on Meta::CPAN

# Try to create a basic unordered dependency
my $Dep = Algorithm::Dependency->new( source => $Source );
ok( $Dep, "Algorithm::Dependency->new returns true" );
ok( ref $Dep, "Algorithm::Dependency->new returns reference" );
isa_ok( $Dep, 'Algorithm::Dependency');
ok( $Dep->source, "Dependency->source returns true" );
ok( $Dep->source eq $Source, "Dependency->source returns the original source" );
ok( $Dep->item('A'), "Dependency->item returns true" );
ok( $Dep->item('A') eq $Source->item('A'), "Dependency->item returns the same as Source->item" );
my @tmp;
ok( scalar( @tmp = $Dep->selected_list ) == 0, "Dependency->selected_list returns empty list" );
ok( ! $Dep->selected('Foo'), "Dependency->selected returns false on bad input" );
ok( ! $Dep->selected('A'), "Dependency->selected returns false when not selected" );
ok( ! defined $Dep->depends('Foo'), "Dependency->depends fails correctly on bad input" );
foreach my $data ( [
	['A'],		[],		['A'] 			], [
	['B'],		['C'],		['B','C'] 		], [
	['C'],		[], 		['C']			], [
	['D'],		['E','F'],	[qw{D E F}]		], [
	['E'],		[],		['E']			], [
	['F'],		[],		['F']			], [
	['A','B'],	['C'],		[qw{A B C}]		], [
	['B','D'],	[qw{C E F}],	[qw{B C D E F}]		]

t/03_basics.t  view on Meta::CPAN

	ok( $rv, "Dependency->depends($args) returns something" );
	is_deeply( $rv, $data->[1], "Dependency->depends($args) returns expected values" );
	$rv = $Dep->schedule( @{ $data->[0] } );
	ok( $rv, "Dependency->schedule($args) returns something" );
	is_deeply( $rv, $data->[2], "Dependency->schedule($args) returns expected values" );
}

# Try a bad creation
ok( ! defined Algorithm::Dependency->new(), "Dependency->new fails correctly" );

# Create with one selected
$Dep = Algorithm::Dependency->new( source => $Source, selected => [ 'F' ] );
ok( $Dep, "Algorithm::Dependency->new returns true" );
ok( ref $Dep, "Algorithm::Dependency->new returns reference" );
isa_ok( $Dep, 'Algorithm::Dependency');
ok( $Dep->source, "Dependency->source returns true" );
ok( $Dep->source eq $Source, "Dependency->source returns the original source" );
ok( $Dep->item('A'), "Dependency->item returns true" );
ok( $Dep->item('A') eq $Source->item('A'), "Dependency->item returns the same as Source->item" );
ok( scalar( @tmp = $Dep->selected_list ) == 1, "Dependency->selected_list returns empty list" );
ok( ! $Dep->selected('Foo'), "Dependency->selected returns false when wrong" );
ok( ! $Dep->selected('A'), "Dependency->selected returns false when expected" );
ok( $Dep->selected('F'), "Dependency->selected return true" );
ok( ! defined $Dep->depends('Foo'), "Dependency->depends fails correctly on bad input" );
foreach my $data ( [
	['A'],		[],		['A'] 			], [
	['B'],		['C'],		[qw{B C}] 		], [
	['C'],		[], 		['C']		], [
	['D'],		['E'],		[qw{D E}]	], [
	['E'],		[],		['E']		], [
	['F'],		[],		[]		], [
	['A','B'],	['C'],		[qw{A B C}]	], [
	['B','D'],	[qw{C E}],	[qw{B C D E}]	]

t/04_complex.t  view on Meta::CPAN





# Load the data/complex.txt file in as a source file
my $file = File::Spec->catfile( $TESTDATA, 'complex.txt' );
my $Source = Algorithm::Dependency::Source::File->new( $file );
ok( $Source, "Complex source created" );
ok( eval {$Source->load;}, "Complex source loads" );

# Try it's unordere dependency with nothing selected
my $Dep = Algorithm::Dependency->new( source => $Source );
ok( $Dep, "Algorithm::Dependency->new returns true" );
ok( ref $Dep, "Algorithm::Dependency->new returns reference" );
isa_ok( $Dep, 'Algorithm::Dependency');

# Test each of the dependencies
foreach my $data ( [
	['A'],		[],				['A'] 				], [
	['B'],		['C'],				[qw{B C}] 			], [
	['C'],		[], 				['C']				], [

t/04_complex.t  view on Meta::CPAN

	is_deeply( $rv, $data->[1], "Dependency->depends($args) returns expected values" );
	$rv = $Dep->schedule( @{ $data->[0] } );
	ok( $rv, "Dependency->schedule($args) returns something" );
	is_deeply( $rv, $data->[2], "Dependency->schedule($args) returns expected values" );
}





# Try an unordered dependency with half a dozen random things selected
$Dep = Algorithm::Dependency->new( source => $Source, selected => [qw{F H J N R P}] );
ok( $Dep, "Algorithm::Dependency->new returns true" );
ok( ref $Dep, "Algorithm::Dependency->new returns reference" );
isa_ok( $Dep, 'Algorithm::Dependency');

# Test each of the dependencies
foreach my $data ( [
	['A'],		[],			['A'] 			], [
	['B'],		['C'],			[qw{B C}] 		], [
	['C'],		[], 			['C']			], [
	['D'],		['E'],			[qw{D E}]		], [

t/05_ordered.t  view on Meta::CPAN

my $complex = File::Spec->catfile( $TESTDATA, 'complex.txt' );
my $CSource = Algorithm::Dependency::Source::File->new( $complex );
ok( $CSource, "Complex source created" );
ok( eval {$CSource->load;}, "Complex source loads" );





# Test the creation of a basic ordered dependency tree
my $BDep = Algorithm::Dependency::Ordered->new( source => $BSource, selected => ['B'] );
ok( $BDep, "Algorithm::Dependency::Ordered->new returns true" );
ok( ref $BDep, "Algorithm::Dependency::Ordered->new returns reference" );
isa_ok( $BDep, 'Algorithm::Dependency::Ordered');
isa_ok( $BDep, 'Algorithm::Dependency');
ok( $BDep->source, "Dependency->source returns true" );
ok( $BDep->source eq $BSource, "Dependency->source returns the original source" );
ok( $BDep->item('A'), "Dependency->item returns true" );
ok( $BDep->item('A') eq $BSource->item('A'), "Dependency->item returns the same as Basic->item" );
my @tmp;
ok( scalar( @tmp = $BDep->selected_list ) == 1, "Dependency->selected_list returns empty list" );
ok( $tmp[0] eq 'B', "Dependency->selected_list returns as expected" );
ok( ! $BDep->selected('Foo'), "Dependency->selected returns false on bad input" );
ok( ! $BDep->selected('A'), "Dependency->selected returns false when not selected" );
ok( $BDep->selected('B'), "Dependency->selected returns true when selected" );
ok( ! defined $BDep->depends('Foo'), "Dependency->depends fails correctly on bad input" );





# Check the results of it's depends and schedule methods
$BDep = Algorithm::Dependency::Ordered->new( source => $BSource );
foreach my $data ( [
	['A'],	[],		['A'] 		], [

t/05_ordered.t  view on Meta::CPAN

	$rv = $CDep->schedule( @{ $data->[0] } );
	ok( $rv, "Dependency->schedule($args) returns something" );
	is_deeply( $rv, $data->[2], "Dependency->schedule($args) returns expected values" );
}





# Now do the ordered dependency on the complex data set
$CDep = Algorithm::Dependency::Ordered->new( source => $CSource, selected => [qw{F H J N R P}] );
ok( $CDep, "Algorithm::Dependency::Ordered->new returns true" );
ok( ref $CDep, "Algorithm::Dependency::Ordered->new returns reference" );
isa_ok( $CDep, 'Algorithm::Dependency::Ordered');

# Test each of the dependencies
foreach my $data ( [
	['A'],		[],			['A'] 			], [
	['B'],		['C'],			[qw{C B}] 		], [
	['C'],		[], 			['C']			], [
	['D'],		['E'],			[qw{E D}]		], [

t/07_hoa.t  view on Meta::CPAN

# Try to create a basic unordered dependency
my $Dep = Algorithm::Dependency->new( source => $Source );
ok( $Dep, "Algorithm::Dependency->new returns true" );
ok( ref $Dep, "Algorithm::Dependency->new returns reference" );
isa_ok( $Dep, 'Algorithm::Dependency');
ok( $Dep->source, "Dependency->source returns true" );
ok( $Dep->source eq $Source, "Dependency->source returns the original source" );
ok( $Dep->item('A'), "Dependency->item returns true" );
ok( $Dep->item('A') eq $Source->item('A'), "Dependency->item returns the same as Source->item" );
my @tmp;
ok( scalar( @tmp = $Dep->selected_list ) == 0, "Dependency->selected_list returns empty list" );
ok( ! $Dep->selected('Foo'), "Dependency->selected returns false on bad input" );
ok( ! $Dep->selected('A'), "Dependency->selected returns false when not selected" );
ok( ! defined $Dep->depends('Foo'), "Dependency->depends fails correctly on bad input" );
foreach my $data ( [
	['A'],		[],			['A'] 			], [
	['B'],		['C'],			['B','C'] 		], [
	['C'],		[], 			['C']			], [
	['D'],		['E','F::G'],		[qw{D E}, 'F::G']	], [
	['E'],		[],			['E']			], [
	['F::G'],	[],			['F::G']		], [
	['A','B'],	['C'],			[qw{A B C}]		], [
	['B','D'],	[qw{C E}, 'F::G'],	[qw{B C D E}, 'F::G']	]
) {
	my $args = join( ', ', map { "'$_'" } @{ $data->[0] } );
	my $rv = $Dep->depends( @{ $data->[0] } );
	ok( $rv, "Dependency->depends($args) returns something" );
	is_deeply( $rv, $data->[1], "Dependency->depends($args) returns expected values" );
	$rv = $Dep->schedule( @{ $data->[0] } );
	ok( $rv, "Dependency->schedule($args) returns something" );
	is_deeply( $rv, $data->[2], "Dependency->schedule($args) returns expected values" );
}

# Create with one selected
$Dep = Algorithm::Dependency->new( source => $Source, selected => [ 'F::G' ] );
ok( $Dep, "Algorithm::Dependency->new returns true" );
ok( ref $Dep, "Algorithm::Dependency->new returns reference" );
isa_ok( $Dep, 'Algorithm::Dependency');
ok( $Dep->source, "Dependency->source returns true" );
ok( $Dep->source eq $Source, "Dependency->source returns the original source" );
ok( $Dep->item('A'), "Dependency->item returns true" );
ok( $Dep->item('A') eq $Source->item('A'), "Dependency->item returns the same as Source->item" );
ok( scalar( @tmp = $Dep->selected_list ) == 1, "Dependency->selected_list returns empty list" );
ok( ! $Dep->selected('Foo'), "Dependency->selected returns false when wrong" );
ok( ! $Dep->selected('A'), "Dependency->selected returns false when expected" );
ok( $Dep->selected('F::G'), "Dependency->selected return true" );
ok( ! defined $Dep->depends('Foo'), "Dependency->depends fails correctly on bad input" );
foreach my $data ( [
	['A'],		[],		['A'] 		], [
	['B'],		['C'],		[qw{B C}] 	], [
	['C'],		[], 		['C']		], [
	['D'],		['E'],		[qw{D E}]	], [
	['E'],		[],		['E']		], [
	['F::G'],	[],		[]		], [
	['A','B'],	['C'],		[qw{A B C}]	], [
	['B','D'],	[qw{C E}],	[qw{B C D E}]	]

t/08_weight.t  view on Meta::CPAN

my $basic = {
	A => 1,	B => 2,	C => 1,
	D => 3,	E => 1,	F => 1,
	};
foreach my $item ( sort keys %$basic ) {
	is( $algorithm->weight($item), $basic->{$item}, "Got weight for '$item'" );
}
is_deeply( $algorithm->weight_all, $basic, 'Got weight for all' );
delete $basic->{B};
delete $basic->{D};
is_deeply( $algorithm->weight_hash(qw{A C E F}), $basic, 'basic: Got weight for selected' );





# Larger scale processing
$file = File::Spec->catfile( $TESTDATA, 'complex.txt' );
$Source = Algorithm::Dependency::Source::File->new( $file );
isa_ok( $Source, 'Algorithm::Dependency::Source::File' );
isa_ok( $Source, 'Algorithm::Dependency::Source' );

xt/author/pod-spell.t  view on Meta::CPAN

Source
Weight
adam
adamk
ether
irc
lib
markm
param
readonly
unselected



( run in 1.519 second using v1.01-cache-2.11-cpan-49f99fa48dc )