| 1 | package IDS::DataSource; | ||
| 2 | $IDS::DataSource::VERSION = "1.0"; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | IDS::DataSource - A data source for the IDS test framework. | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | A usage synopsis would go here. Since it is not here, read on. | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use Carp qw(cluck carp confess); | ||
| 19 | use IO::File; | ||
| 20 | use IDS::Utils qw(fh_or_stdout); | ||
| 21 | |||
| 22 | ### It would be better to autoload the source based on what we need. | ||
| 23 | ### However, for now we will load all possible ones. | ||
| 24 | |||
| 25 | use IDS::DataSource::HTTP; | ||
| 26 | |||
| 27 | $IDS::DataSource::VERSION = "1.0"; | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item new() | ||
| 32 | |||
| 33 | =item new(params) | ||
| 34 | |||
| 35 | =item new(source, params) | ||
| 36 | |||
| 37 | Create the object for the data source. If the parameters are supplied, | ||
| 38 | they are used; otherwise everything is defaults (unsurprisingly). | ||
| 39 | |||
| 40 | If a data source is supplied, it is the source passed to a load operation. | ||
| 41 | A source may be a file name, file handle, or other source understood by | ||
| 42 | the IDS::DataSource subclass. | ||
| 43 | |||
| 44 | The parameters affect how the algorithm operates. See the specific | ||
| 45 | source for most parameter information. General parameters are | ||
| 46 | described below. | ||
| 47 | |||
| 48 | In simple cases, this function can be used instead of the subclass | ||
| 49 | version. | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | # Part of the logic here seems kind of backwards, but we cannot load | ||
| 56 | # until parameters have been loaded. Some of the parameters may affect | ||
| 57 | # how we load. | ||
| 58 | sub new { | ||
| 59 | my $invocant = shift; | ||
| 60 | my $class = ref($invocant) || $invocant; | ||
| 61 | my $self = { }; | ||
| 62 | my $source; | ||
| 63 | |||
| 64 | # necessary before we call handle_parameters. | ||
| 65 | bless($self, $class); | ||
| 66 | |||
| 67 | $self->default_parameters; | ||
| 68 | $source = $self->handle_parameters(@_); | ||
| 69 | |||
| 70 | $self->load($source) if defined($source); | ||
| 71 | |||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | =over | ||
| 76 | |||
| 77 | =item parameters() | ||
| 78 | |||
| 79 | =item parameters(param) | ||
| 80 | |||
| 81 | =item parameters(param, value, ...) | ||
| 82 | |||
| 83 | Set or retrieve the current parameters (individual or group). | ||
| 84 | |||
| 85 | =back | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub parameters { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | return (wantarray ? %{$self->{"params"}} : $self->{"params"}) | ||
| 93 | if ($#_ == -1); | ||
| 94 | |||
| 95 | return ${$self->{"params"}}{$_[0]} if ($#_ == 0); | ||
| 96 | |||
| 97 | if ($#_ == 1) { | ||
| 98 | my $old = ${$self->{"params"}}{$_[0]}; | ||
| 99 | ${$self->{"params"}}{$_[0]} = $_[1]; | ||
| 100 | return $old; | ||
| 101 | } | ||
| 102 | |||
| 103 | scalar(@_) % 2 != 0 and confess "odd > 1 number of parameters passed to ", | ||
| 104 | *parameters{PACKAGE}, ". See documentation for proper usage.\n"; | ||
| 105 | |||
| 106 | for (my $i = 0; $i < $#_; $i+=2) { | ||
| 107 | ${$self->{"params"}}{$_[$i]} = $_[$i+1]; | ||
| 108 | } | ||
| 109 | |||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | =head2 Parameters | ||
| 114 | |||
| 115 | The following parameters are expected to be used (when the use makes | ||
| 116 | sense) by all subclasses. | ||
| 117 | |||
| 118 | =over | ||
| 119 | |||
| 120 | =item verbose | ||
| 121 | |||
| 122 | How much ``extra'' information to provide as we are working. 0 means | ||
| 123 | nothing other than warnings and errors. Increasing values mean | ||
| 124 | increasing output, but these details are left to the subclasses. | ||
| 125 | |||
| 126 | =item msg_fh | ||
| 127 | |||
| 128 | Where warning and error messages go; nowhere if undefined. | ||
| 129 | |||
| 130 | =back | ||
| 131 | |||
| 132 | Additionally, subclasses may define their own parameters. | ||
| 133 | |||
| 134 | All subclasses must define all parameters to default values when new() | ||
| 135 | is called. This is so the param_options can be properly handled. | ||
| 136 | |||
| 137 | =over | ||
| 138 | |||
| 139 | =item param_options() | ||
| 140 | |||
| 141 | These are options definitions for Getopt::Long. | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub param_options { | ||
| 148 | my $self = shift; | ||
| 149 | |||
| 150 | return ( | ||
| 151 | "verbose!" => \${$self->{"params"}}{"verbose"}, | ||
| 152 | "print_warnings!" => \${$self->{"params"}}{"print_warnings"}, | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 Utility functions | ||
| 157 | |||
| 158 | These may be useful for subclasses. | ||
| 159 | |||
| 160 | =over | ||
| 161 | |||
| 162 | =item handle_parameters($self, @_) | ||
| 163 | |||
| 164 | Handle the parameter string that IDS::Algorithm::new() will accept. | ||
| 165 | Extracted from new() for subclass usage. Returns a filehandle or filename | ||
| 166 | if we were called with a filehandle/filename from which to load. | ||
| 167 | |||
| 168 | =back | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | sub handle_parameters { | ||
| 173 | my $self = shift; | ||
| 174 | my $loadfh; | ||
| 175 | |||
| 176 | if (defined($_[0])) { | ||
| 177 | # If we have a non-zero even number of arguments, we have | ||
| 178 | # nothing but parameters | ||
| 179 | if (scalar(@_) % 2 == 0) { | ||
| 180 | undef $loadfh; | ||
| 181 | } else { | ||
| 182 | $loadfh = shift; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | $self->parameters(@_); | ||
| 186 | return $loadfh; | ||
| 187 | } | ||
| 188 | |||
| 189 | =over | ||
| 190 | |||
| 191 | =item default_parmeters() | ||
| 192 | |||
| 193 | Set all of the parameters to default values. | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub default_parameters { | ||
| 200 | confess "IDS::source::default_parameters called; this function must be subclassed.\n"; | ||
| 201 | } | ||
| 202 | |||
| 203 | =over | ||
| 204 | |||
| 205 | =item load() | ||
| 206 | |||
| 207 | Load a single instance from the source (filename or filehandle) provided. | ||
| 208 | This function must be implemented in the subclass. | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub load { | ||
| 215 | confess "IDS::source::load called; this function must be subclassed.\n"; | ||
| 216 | } | ||
| 217 | |||
| 218 | =over | ||
| 219 | |||
| 220 | =item foreach(source, coderef, object) | ||
| 221 | |||
| 222 | Read a series of instances (from the source specified), and call the | ||
| 223 | function pointed to by coderef, which is associated with the object | ||
| 224 | provided. This function may be overridden in the subclass. | ||
| 225 | |||
| 226 | The function to call is called as: | ||
| 227 | |||
| 228 | &$coderef($object, tokenref) | ||
| 229 | |||
| 230 | =back | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub foreach { | ||
| 235 | my $self = shift; | ||
| 236 | my $fh = shift or | ||
| 237 | cluck *foreach{PACKAGE} . "::foreach called without a filehandle"; | ||
| 238 | my $func = shift or | ||
| 239 | cluck *foreach{PACKAGE} . "::foreach called without function ref"; | ||
| 240 | my $obj = shift or | ||
| 241 | cluck *foreach{PACKAGE} . "::foreach called without object"; | ||
| 242 | |||
| 243 | my $n = 0; | ||
| 244 | while ($self->read_next($fh)) { | ||
| 245 | $n++; | ||
| 246 | &$func($obj, scalar $self->tokens); | ||
| 247 | } | ||
| 248 | return $n; | ||
| 249 | } | ||
| 250 | |||
| 251 | =over | ||
| 252 | |||
| 253 | =item mesg(level, message) | ||
| 254 | |||
| 255 | =item mesg(level, message, separator, arrayref) | ||
| 256 | |||
| 257 | Print a message if the verbosity level warrants it. If the separator | ||
| 258 | and array reference are provided, the array referenced is joined with | ||
| 259 | the separator provided and the result is appended to the message provided. | ||
| 260 | |||
| 261 | A message is produced only if the current verbosity level >= level. | ||
| 262 | |||
| 263 | =back | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub mesg { | ||
| 268 | my $self = shift; | ||
| 269 | my $level = shift; | ||
| 270 | defined($level) or confess *mesg{PACKAGE} . | ||
| 271 | "::mesg called without a level!"; | ||
| 272 | my $msg = shift or confess *mesg{PACKAGE} . | ||
| 273 | "::mesg called without a message!"; | ||
| 274 | my $sep = shift; | ||
| 275 | my $ref = shift; | ||
| 276 | my $addendum; | ||
| 277 | |||
| 278 | $addendum = defined($sep) && defined($ref) ? join($sep, @$ref) : ""; | ||
| 279 | |||
| 280 | my $fh = ${$self->{"params"}}{"msg_fh"}; | ||
| 281 | print $fh "$msg$addendum\n" | ||
| 282 | if defined($fh) && ${$self->{"params"}}{"verbose"} >= $level; | ||
| 283 | } | ||
| 284 | |||
| 285 | =over | ||
| 286 | |||
| 287 | =item warn(message, arrayref, token) | ||
| 288 | |||
| 289 | Print a warning message, and optionally push a warning token on the | ||
| 290 | referenced array (if return_warnings is set). | ||
| 291 | |||
| 292 | =back | ||
| 293 | |||
| 294 | =cut | ||
| 295 | |||
| 296 | sub warn { | ||
| 297 | my $self = shift; | ||
| 298 | my $pmsg = shift or confess *warn{PACKAGE} . | ||
| 299 | "::warn called without a print message"; | ||
| 300 | my $tref = shift or confess *warn{PACKAGE} . | ||
| 301 | "::warn called without a token list ref"; | ||
| 302 | my $token = shift; | ||
| 303 | defined($token) or confess *warn{PACKAGE} . | ||
| 304 | "::warn called without a token"; | ||
| 305 | my $fh = ${$self->{"params"}}{"msg_fh"}; | ||
| 306 | |||
| 307 | # ${$self->{"params"}}{"print_warnings"} and carp "WARNING: $pmsg"; | ||
| 308 | print $fh "WARNING: $pmsg\n" | ||
| 309 | if defined($fh) && ${$self->{"params"}}{"print_warnings"}; | ||
| 310 | |||
| 311 | push @$tref, $token | ||
| 312 | if ${$self->{"params"}}{"return_warnings"}; | ||
| 313 | } | ||
| 314 | |||
| 315 | =head1 AUTHOR INFORMATION | ||
| 316 | |||
| 317 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 318 | |||
| 319 | This library is free software; you can redistribute it and/or modify | ||
| 320 | it under the same terms as Perl itself. | ||
| 321 | |||
| 322 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 323 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 324 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 325 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 326 | speed of the response depends on how the research is proceeding. | ||
| 327 | |||
| 328 | =head1 BUGS | ||
| 329 | |||
| 330 | Please report them. | ||
| 331 | |||
| 332 | =head1 SEE ALSO | ||
| 333 | |||
| 334 | L<IDS::Test>, L<IDS::Algorithm> | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | 1; |