These documents are For the HEAD of the CVS repository on July 19, 2007 Api docs for previous releases

Modware

Iterator

Summary Included libraries Package variables Synopsis Description General documentation Methods

Summary
   Modware::Iterator - An Iterator parent class inherited by the results returned from Modware::Search::* methods.
Package variables top
No package variables defined.
Included modulestop
Modware::Root
Inherit top
Modware::Root
Synopsistop
#### The following code shows how a search class method can return an iterator
## instead of an object array. This eliminates the overhead of returning one giant array.

use Modware::Iterator;

sub Search_term_in_all_ontologies_by_name {

my ($self, $term) = @_;
my $dbh = new Modware::DBH();
my @results;

my $sth = $dbh->prepare("
SELECT CVTERM_ID
FROM $ENV{'CHADO_USER'}.CVTERM
WHERE UPPER(NAME) LIKE UPPER(?)
");

$sth->execute( $term );

### Creating a data array.
### The data array to be passed to the ietrator is an array of anonymous hashes.
### The key for the hash is the key used in the constructor of the class
### For eg: A Term object can be instantiated by calling
### my $term = new Modware::CV::Term( -CVTERM_DBID => "12345" )
### Hence, in this schenario the key string for the hash would be CVTERM_DBID
### Example of an array of hashes [{ CVTERM_DBID => 122}, { CVTERM_DBID => 123}]

my @data;
while ( my $row = $sth->fetchrow() ) {
push(@data, {-CVTERM_DBID => $row});
}
$sth->finish;

### Instantiate an Iterator object with the class name and the data array as arguments.
my $itr = new Modware::Iterator(-CLASS => "Modware::CV::Term", -DATA => \@data);

#### The follwoing piece of code allws the method to return an array of objects or Itetrator
return wantarray ? $itr->to_array() : $itr;
}

### Manipulating the iterator object

my $iterator = Modware::Search::Ontology->Search_term_in_all_ontologies_by_name("mass%");

while(my $term = $iterator->next()) {
print $term->name()."\n";
}
print $iterator->first->name()."\n";

my @objs = $iterator->slice(2,4);

foreach my $obj (@objs) {
print $obj->name()."\n";
}

### This returns an array of objects instead of the iterator
my @objects = Modware::Search::Ontology->Search_term_in_all_ontologies_by_name("mass%");
Descriptiontop
   This class was adapted from the Class::DBI iterator class
Methodstop
classDescriptionCode
countDescriptionCode
dataDescriptionCode
firstDescriptionCode
newDescriptionCode
nextDescriptionCode
resetDescriptionCode
sliceDescriptionCode
to_arrayDescriptionCode

Methods description

classcodetopprevnext
 Title   : class
Usage : my $itr->class("Modware::CV::Term");
Function: get/set the type of the Class
Returns : A string
Args : Class type name
countcodetopprevnext
 Title   : count
Usage : my $count = $itr->count();
Function: Gives the count of elements in the array
Returns : number
Args : none
datacodetopprevnext
 Title   : data
Usage : $itr->data( \@data);
Function: get/set the data
Returns : Reference to an array of anonymous hashes [{ CVTERM_DBID => 122}, { CVTERM_DBID => 123}]
Args : Reference to an array of anonymous hashes
firstcodetopprevnext
 Title   : first
Usage : my $obj = $itr->first();
Function: Returns the first element from the iterator
Returns : First Object of type class specified in the constructor
Args : none
newcodetopprevnext
 Title   : new
Usage : my $itr = new Modware::Iterator("Modware::CV::Term", \@data);
Function: Builds a new Modware::Iterator object
Returns : an instance of Modware::Iterator
Args : -CLASS This is the class type over which the ietraor is defined
-DATA This is a reference to an array of anonymous hashes.
[{ CVTERM_DBID => 122}, { CVTERM_DBID => 123}]
nextcodetopprevnext
 Title   : next
Usage : my $obj = $itr->next();
Function: Returns the next element from the iterator
Returns : Object of type class specified in the constructor
Args : none
resetcodetopprevnext
 Title   : reset
Usage : my $obj = $itr->reset();
Function: Turns the pointer of the iterator to the first object. Hence, when you call next methoid
on it, it starts from the first object.
Returns : None
Args : none
slicecodetopprevnext
 Title   : slice
Usage : my @objs = $iterator->slice(2,4);
Function: Returns the objects specified between the range
Returns : An object array of type class
Args : range start
range end
to_arraycodetopprevnext
 Title   : to_array
Usage : my @objs = $itr->to_array();
Function: This returns an array of the instantiated objects
Returns : An array of INstantiated objects
Args : none

Methods code

classdescriptiontopprevnext
sub class {
 shift->{_class}
}
countdescriptiontopprevnext
sub count {
        my $self = shift;
        $self->{_count} ||= scalar $self->data;
}
datadescriptiontopprevnext
sub data {
 @{ shift->{_data} }
}
firstdescriptiontopprevnext
sub first {
        my $self = shift;
        $self->reset;
        return $self->next;
}
newdescriptiontopprevnext
sub new {
        my ( $self, @args ) = @_;
        my ($class, $data) = $self->_rearrange( [qw( CLASS DATA )], @args );
        bless {
                _class  => $class,
                _data   => $data,
                _place  => 0,
        }, $self;
}
nextdescriptiontopprevnext
sub next {
        my $self = shift;
        my $use  = $self->{_data}->[ $self->{_place}++ ] or return;
        $self->_load_module( $self->class() );

        return $self->class(%$use);
}
resetdescriptiontopprevnext
sub reset {
 shift->{_place} = 0
}
slicedescriptiontopprevnext
sub slice {
        my ($self, $start, $end) = @_;
        $end ||= $start;
        $self->{_place} = $start;
        my @return;
        while ($self->{_place} <= $end) {
                push @return, $self->next || last;
        }
        return @return;
}
to_arraydescriptiontopprevnext
sub to_array {
my $self = shift;

my @results;
while(my $obj = $self->next()) {
    push(@results, $obj);
}
return  @results;
}

General documentation

AUTHOR - Sohel Merchant top
   Sohel Merchant s-merchant@northwestern.edu
APPENDIX top
   The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _