package Algorithm::Dependency; # git description: v1.111-8-g2fb772e # ABSTRACT: Base class for implementing various dependency trees #pod =pod #pod #pod =head1 SYNOPSIS #pod #pod Typical Usage: Ordering based on dependency requirements #pod #pod use Algorithm::Dependency::Ordered; #pod use Algorithm::Dependency::Source::HoA; #pod #pod my $deps = { #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 #pod my $also = $dep->depends( 'b' ); #pod # Returns: ['a', 'core'] -- sorted order, not including ourselves #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. #pod This module intentionally uses implementation-neutral words, to avoid #pod confusion. #pod #pod =head2 Terminology #pod #pod The term C refers to a single entity, such as a single software #pod package, in the overall set of possible entities. Internally, this is a #pod fairly simple object. See L for details. #pod #pod The term C means that a particular item, for your purposes, has already been acted up in the required way. For example, if the software package had already been installed, and didn't need to be re-installed, it would be C. The term C refers to a location that contains the master set of 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 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. It should be more applicable for your needs. This is the the subclass you would probably use to implement a simple ( non-versioned ) package installation system. Please note that an ordered hierarchy has additional constraints. For example, circular dependencies ARE legal in a non-ordered hierarchy, but ARE NOT legal in an ordered hierarchy. =head2 Extending A module for creating a source from a simple flat file is included. For details see L. Information on creating a source for your particular use is in L. =head1 METHODS =head2 new %args The constructor creates a new context object for the dependency algorithms to act in. It takes as argument a series of options for creating the object. =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. 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 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 or C 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 flag, orphans are simply ignored. Without the C flag, an error will be returned if an orphan is found. =back The C constructor returns a new Algorithm::Dependency object on success, or C on error. =head2 source The C method retrieves the L object for the algorithm context. =head2 selected_list The C 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 method will return true if the item is selected, false is not, or C if the item does not exist, or an error occurs. =head2 item $name The C method fetches and returns the item object, as specified by the name argument. Returns an L object on success, or C 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 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 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 on error. NOTE: The result of C is ordered by an internal C irrespective of the ordering provided by the dependency handler. Use L and C to use the most common ordering (process sequence) =head2 schedule $name1, ..., $nameN Given a list of one or more item names, the C method will return, as a reference to an array, the ordered list of items you should act upon in whichever order this particular dependency handler uses - see L 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 on error. =head2 schedule_all; The C method acts the same as the C method, but returns a schedule that selected all the so-far unselected items. =head1 TO DO Add the C 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 being a tool. =head1 SEE ALSO L, L, L, L =head1 SUPPORT Bugs may be submitted through L (or L). =head1 AUTHOR Adam Kennedy =head1 CONTRIBUTORS =for stopwords Adam Kennedy Karen Etheridge Mark Murawski =over 4 =item * Adam Kennedy =item * Karen Etheridge =item * Mark Murawski =back =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2003 by Adam Kennedy. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut