blib/lib/MOBY/Async/LSAE.pm untested

filter: Statementstmt 0.0% 0 / 240 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 421 CC 48 SCAR 42.1
CC
48
Cov
0%
XML
240.0
type
72.0
message
20.0
CRAP 67.3
1 =pod
2  
3 =head1 NAME
4  
5 MOBY::Async::LSAE - utilities to work with LSAE analysis event blocks
6  
7 =head1 AUTHORS
8  
9 Former developer
10 Enrique de Andres Saiz (enrique.deandres@pcm.uam.es) -
11 INB GNHC-1 (Madrid Science Park, Spain) (2006-2007).
12  
13 Maintainers
14 Jose Maria Fernandez (jmfernandez@cnio.es),
15 Jose Manuel Rodriguez (jmrodriguez@cnio.es) -
16 INB GN2 (CNIO, Spain).
17  
18 =head1 DESCRIPTION
19  
20 Provides functionalities to work with LSAE analysis event blocks.
21 It defines the following constants, which represents the different types of
22 LSAE Event Blocks:
23  
24 =over
25  
26 =item LSAE_BASE_EVENT
27  
28 e.g.
29 <analysis_event timestamp="today">
30 <message>This is a general analysis event.</message>
31 </analysis_event>
32  
33 =item LSAE_HEARTBEAT_PROGRESS_EVENT
34  
35 e.g.
36 <analysis_event timestamp="today">
37 <message>This is a HEARTBEAT analysis event.</message>
38 <heartbeat_progress/>
39 </analysis_event>
40  
41 =item LSAE_PERCENT_PROGRESS_EVENT
42  
43 e.g.
44 <analysis_event timestamp="today">
45 <message>This is a PERCENT PROGRESS analysis event.</message>
46 <percent_progress percentage="52"/>
47 </analysis_event>
48  
49 =item LSAE_STATE_CHANGED_EVENT
50  
51 e.g.
52 <analysis_event timestamp="today">
53 <message>This is a STATUS CHANGED analysis event.</message>
54 <state_changed previous_state="created" new_state="running"/>
55 </analysis_event>
56  
57 =item LSAE_STEP_PROGRESS_EVENT
58  
59 e.g.
60 <analysis_event timestamp="today">
61 <message>This is a STEP PROGRESS analysis event.</message>
62 <step_progress total_steps="10" steps_completed="5"/>
63 </analysis_event>
64  
65 =item LSAE_TIME_PROGRESS_EVENT
66  
67 e.g.
68 <analysis_event timestamp="today">
69 <message>This is a TIME PROGRESS analysis event.</message>
70 <time_progress remaining="324"/>
71 </analysis_event>
72  
73 =back
74  
75 It also defines LSAE::AnalysisEventBlock class.
76  
77 =head1 LSAE::AnalysisEventBlock METHODS
78  
79 =head2 new
80  
81 Name : new
82 Function : create a new LSAE::AnalysisEventBlock object.
83 Usage : $event = LSAE::AnalysisEventBlock->new()
84 $event = LSAE::AnalysisEventBlock->new($xml)
85 Args : $xml - (optional) a string containing the XML code of an
86 analysis event block according to the LSAE spec.
87 Returns : the LSAE::AnalysisEventBlock object created.
88  
89 =head2 type
90  
91 Name : type
92 Function : get/set the type of an analysis event block object.
93 Usage : $event->type()
94 $event->type($type)
95 Args : $type - a value representing a type of analysis event block.
96 Returns : a value representing the type of analysis event block object.
97  
98 =head2 id
99  
100 Name : id
101 Function : get/set the identifier of an analysis event block object.
102 Usage : $event->id()
103 $event->id($id)
104 Args : $id - (optional) a string.
105 Returns : the value of the identifier attribute.
106  
107 =head2 timestamp
108  
109 Name : timestamp
110 Function : get/set the timestamp of an analysis event block object.
111 Usage : $event->timestamp()
112 $event->timestamp($timestamp)
113 Args : $timestamp - (optional) a tiemestamp.
114 Returns : the value of the timestamp attribute.
115  
116 =head2 message
117  
118 Name : message
119 Function : get/set the message of an analysis event block object.
120 Usage : $event->message()
121 $event->message($message)
122 Args : $message - (optional) a string.
123 Returns : the content of the message element.
124  
125 =head2 percentage
126  
127 Name : percentage
128 Function : get/set the percentage attribute of an analysis event block of
129 the type LSAE_PERCENT_PROGRESS_EVENT.
130 Usage : $event->percentage()
131 $event->percentage($percentage)
132 Args : $percentage - an integer between 0 and 100.
133 Returns : the value of the percentage attribute.
134  
135 =head2 previous_state
136  
137 Name : previous_state
138 Function : get/set the previous_state attribute of an analysis event block of
139 the type LSAE_STATE_CHANGED_EVENT.
140 Usage : $event->previous_state()
141 $event->previous_state($state)
142 Args : $state - one of the following strings... created, running,
143 completed, terminated_by_request or terminated_by_error.
144 Returns : the value of the previous_state attribute.
145  
146 =head2 new_state
147  
148 Name : new_state
149 Function : get/set the new_state attribute of an analysis event block of
150 the type LSAE_STATE_CHANGED_EVENT.
151 Usage : $event->new_state()
152 $event->new_state($state)
153 Args : $state - one of the following strings... created, running,
154 completed, terminated_by_request or terminated_by_error.
155 Returns : the value of the new_state attribute.
156  
157 =head2 total_steps
158  
159 Name : total_steps
160 Function : get/set the total_steps attribute of an analysis event block of
161 the type LSAE_STEP_PROGRESS_EVENT.
162 Usage : $event->total_steps()
163 $event->total_steps($steps)
164 Args : $steps - an integer
165 Returns : the value of the total_steps attribute.
166  
167 =head2 steps_completed
168  
169 Name : steps_completed
170 Function : get/set the steps_completed attribute of an analysis event block of
171 the type LSAE_STEP_PROGRESS_EVENT.
172 Usage : $event->steps_completed()
173 $event->steps_completed($steps)
174 Args : $steps - an integer
175 Returns : the value of the steps_completed attribute.
176  
177 =head2 remaining
178  
179 Name : remaining
180 Function : get/set the remaining attribute of an analysis event block of
181 the type LSAE_TIME_PROGRESS_EVENT.
182 Usage : $event->remaining()
183 $event->remaining($seconds)
184 Args : $seconds - an integer
185 Returns : the value of the remaining attribute.
186  
187 =head2 XML
188  
189 Name : XML
190 Function : get an string with the XML code of an analysis event block.
191 Usage : $event->XML()
192 Args : none
193 Returns : the analysis event block.
194  
195 =cut
196  
197 package MOBY::Async::LSAE;
198 use strict;
199 use XML::LibXML;
200 use Exporter;
201  
202 use vars qw /$VERSION/;
203 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;
204  
205 use base qw(Exporter);
206  
207 our @EXPORT = qw(
208 LSAE_BASE_EVENT
209 LSAE_HEARTBEAT_PROGRESS_EVENT
210 LSAE_PERCENT_PROGRESS_EVENT
211 LSAE_STATE_CHANGED_EVENT
212 LSAE_STEP_PROGRESS_EVENT
213 LSAE_TIME_PROGRESS_EVENT
214 );
215  
216 our @EXPORT_OK = qw(
217 LSAE_BASE_EVENT
218 LSAE_HEARTBEAT_PROGRESS_EVENT
219 LSAE_PERCENT_PROGRESS_EVENT
220 LSAE_STATE_CHANGED_EVENT
221 LSAE_STEP_PROGRESS_EVENT
222 LSAE_TIME_PROGRESS_EVENT
223 );
224  
225 our %EXPORT_TAGS = (
226 all => [
227 qw(
228 LSAE_BASE_EVENT
229 LSAE_HEARTBEAT_PROGRESS_EVENT
230 LSAE_PERCENT_PROGRESS_EVENT
231 LSAE_STATE_CHANGED_EVENT
232 LSAE_STEP_PROGRESS_EVENT
233 LSAE_TIME_PROGRESS_EVENT
234 )
235 ]
236 );
237  
238 use constant LSAE_BASE_EVENT => 0;
239 use constant LSAE_HEARTBEAT_PROGRESS_EVENT => 1;
240 use constant LSAE_PERCENT_PROGRESS_EVENT => 2;
241 use constant LSAE_STATE_CHANGED_EVENT => 3;
242 use constant LSAE_STEP_PROGRESS_EVENT => 4;
243 use constant LSAE_TIME_PROGRESS_EVENT => 5;
244  
245 package LSAE::AnalysisEventBlock;
246 use strict;
247  
248 sub new {
249 my $this = shift;
250 my $class = ref($this) || $this;
251 my $self = {};
252 bless $self, $class;
253 if (@_) {
254 $self->{XML} = shift;
255 $self->type;
256 $self->id;
257 $self->timestamp;
258 $self->message;
259 $self->percentage;
260 $self->previous_state;
261 $self->new_state;
262 $self->total_steps;
263 $self->steps_completed;
264 $self->remaining;
265 } else {
266 $self->{XML} = undef;
267 $self->{type} = 0;
268 $self->{id} = undef;
269 $self->{timestamp} = undef;
270 $self->{message} = undef;
271 $self->{percentage} = undef;
272 $self->{previous_state} = undef;
273 $self->{new_state} = undef;
274 $self->{total_steps} = undef;
275 $self->{steps_completed} = undef;
276 $self->{remaining} = undef;
277 }
278 return($self);
279 }
280  
281 sub type {
282 my $self = shift;
283 if (@_) {
284 $self->{type} = shift;
285 } elsif ($self->{XML}) {
286 my $parser = XML::LibXML->new();
287 my $doc = $parser->parse_string($self->{XML});
288 my $lsae = $doc->getDocumentElement();
289 my @heartbeat_progress = ($lsae->getChildrenByTagName('heartbeat_progress'));
290 my @percent_progress = ($lsae->getChildrenByTagName('percent_progress'));
291 my @state_changed = ($lsae->getChildrenByTagName('state_changed'));
292 my @step_progress = ($lsae->getChildrenByTagName('step_progress'));
293 my @time_progress = ($lsae->getChildrenByTagName('time_progress'));
294 if (scalar(@heartbeat_progress)) {
295 $self->{type} = MOBY::Async::LSAE::LSAE_HEARTBEAT_PROGRESS_EVENT;
296 } elsif (scalar(@percent_progress)) {
297 $self->{type} = MOBY::Async::LSAE::LSAE_PERCENT_PROGRESS_EVENT;
298 } elsif (scalar(@state_changed)) {
299 $self->{type} = MOBY::Async::LSAE::LSAE_STATE_CHANGED_EVENT;
300 } elsif (scalar(@step_progress)) {
301 $self->{type} = MOBY::Async::LSAE::LSAE_STEP_PROGRESS_EVENT;
302 } elsif (scalar(@time_progress)) {
303 $self->{type} = MOBY::Async::LSAE::LSAE_TIME_PROGRESS_EVENT;
304 } else {
305 $self->{type} = MOBY::Async::LSAE::LSAE_BASE_EVENT;
306 }
307 }
308 return $self->{type};
309 }
310  
311 sub id {
312 my $self = shift;
313 if (@_) {
314 $self->{id} = shift;
315 } elsif ($self->{XML}) {
316 my $parser = XML::LibXML->new();
317 my $doc = $parser->parse_string($self->{XML});
318 my $lsae = $doc->getDocumentElement();
319 $self->{id} = $lsae->getAttribute('id') || $lsae->getAttributeNS($WSRF::Constants::MOBY, 'id');
320 }
321 return $self->{id};
322 }
323  
324 sub timestamp {
325 my $self = shift;
326 if (@_) {
327 $self->{timestamp} = shift;
328 } elsif ($self->{XML}) {
329 my $parser = XML::LibXML->new();
330 my $doc = $parser->parse_string($self->{XML});
331 my $lsae = $doc->getDocumentElement();
332 $self->{timestamp} = $lsae->getAttribute('timestamp') || $lsae->getAttributeNS($WSRF::Constants::MOBY, 'timestamp');
333 }
334 return $self->{timestamp};
335 }
336  
337 sub message {
338 my $self = shift;
339 if (@_) {
340 $self->{message} = shift;
341 } elsif ($self->{XML}) {
342 my $parser = XML::LibXML->new();
343 my $doc = $parser->parse_string($self->{XML});
344 my $lsae = $doc->getDocumentElement();
345 my @message = ($lsae->getChildrenByTagName('message'));
346 my $message = shift(@message);
347 $self->{message} = $message->getFirstChild->nodeValue if ($message);
348 }
349 return $self->{message};
350 }
351  
352 sub percentage {
353 my $self = shift;
354 if (@_) {
355 $self->{percentage} = shift;
356 } elsif ($self->{XML}) {
357 $self->{type} = $self->type;
358 if ($self->{type} == MOBY::Async::LSAE::LSAE_PERCENT_PROGRESS_EVENT) {
359 my $parser = XML::LibXML->new();
360 my $doc = $parser->parse_string($self->{XML});
361 my $lsae = $doc->getDocumentElement();
362 my @message = ($lsae->getChildrenByTagName('percent_progress'));
363 my $message = shift(@message);
364 $self->{percentage} = $message->getAttribute('percentage');
365 }
366 }
367 return $self->{percentage};
368 }
369  
370 sub previous_state {
371 my $self = shift;
372 if (@_) {
373 $self->{previous_state} = shift;
374 } elsif ($self->{XML}) {
375 $self->{type} = $self->type;
376 if ($self->{type} == MOBY::Async::LSAE::LSAE_STATE_CHANGED_EVENT) {
377 my $parser = XML::LibXML->new();
378 my $doc = $parser->parse_string($self->{XML});
379 my $lsae = $doc->getDocumentElement();
380 my @message = ($lsae->getChildrenByTagName('state_changed'));
381 my $message = shift(@message);
382 $self->{previous_state} = $message->getAttribute('previous_state');
383 }
384 }
385 return $self->{previous_state};
386 }
387  
388 sub new_state {
389 my $self = shift;
390 if (@_) {
391 $self->{new_state} = shift;
392 } elsif ($self->{XML}) {
393 $self->{type} = $self->type;
394 if ($self->{type} == MOBY::Async::LSAE::LSAE_STATE_CHANGED_EVENT) {
395 my $parser = XML::LibXML->new();
396 my $doc = $parser->parse_string($self->{XML});
397 my $lsae = $doc->getDocumentElement();
398 my @message = ($lsae->getChildrenByTagName('state_changed'));
399 my $message = shift(@message);
400 $self->{new_state} = $message->getAttribute('new_state');
401 }
402 }
403 return $self->{new_state};
404 }
405  
406 sub total_steps {
407 my $self = shift;
408 if (@_) {
409 $self->{total_steps} = shift;
410 } elsif ($self->{XML}) {
411 $self->{type} = $self->type;
412 if ($self->{type} == MOBY::Async::LSAE::LSAE_STEP_PROGRESS_EVENT) {
413 my $parser = XML::LibXML->new();
414 my $doc = $parser->parse_string($self->{XML});
415 my $lsae = $doc->getDocumentElement();
416 my @message = ($lsae->getChildrenByTagName('step_progress'));
417 my $message = shift(@message);
418 $self->{total_steps} = $message->getAttribute('total_steps');
419 }
420 }
421 return $self->{total_steps};
422 }
423  
424 sub steps_completed {
425 my $self = shift;
426 if (@_) {
427 $self->{steps_completed} = shift;
428 } elsif ($self->{XML}) {
429 $self->{type} = $self->type;
430 if ($self->{type} == MOBY::Async::LSAE::LSAE_STEP_PROGRESS_EVENT) {
431 my $parser = XML::LibXML->new();
432 my $doc = $parser->parse_string($self->{XML});
433 my $lsae = $doc->getDocumentElement();
434 my @message = ($lsae->getChildrenByTagName('step_progress'));
435 my $message = shift(@message);
436 $self->{steps_completed} = $message->getAttribute('steps_completed');
437 }
438 }
439 return $self->{steps_completed};
440 }
441  
442 sub remaining {
443 my $self = shift;
444 if (@_) {
445 $self->{remaining} = shift;
446 } elsif ($self->{XML}) {
447 $self->{type} = $self->type;
448 if ($self->{type} == MOBY::Async::LSAE::LSAE_TIME_PROGRESS_EVENT) {
449 my $parser = XML::LibXML->new();
450 my $doc = $parser->parse_string($self->{XML});
451 my $lsae = $doc->getDocumentElement();
452 my @message = ($lsae->getChildrenByTagName('time_progress'));
453 my $message = shift(@message);
454 $self->{remaining} = $message->getAttribute('remaining');
455 }
456 }
457 return $self->{remaining};
458 }
459  
460 sub XML {
461 my $self = shift;
462
463 $self->{type} = $self->type;
464 $self->{id} = $self->id;
465 $self->{timestamp} = $self->timestamp;
466 $self->{message} = $self->message;
467 $self->{percentage} = $self->percentage;
468 $self->{previous_state} = $self->previous_state;
469 $self->{new_state} = $self->new_state;
470 $self->{total_steps} = $self->total_steps;
471 $self->{steps_completed} = $self->steps_completed;
472 $self->{remaining} = $self->remaining;
473
474 my $id = "";
475 my $timestamp = "";
476 my $message = "";
477 $id = " id=\"".$self->{id}."\"" if defined $self->{id};
478 $timestamp = " timestamp=\"".$self->{timestamp}."\"" if defined $self->{timestamp};
479 my $header = "<analysis_event$id$timestamp xmlns=''>";
480 my $footer = "</analysis_event>";
481 $message = "<message>".$self->{message}."</message>" if defined $self->{message};
482
483 if ($self->{type} == MOBY::Async::LSAE::LSAE_BASE_EVENT) {
484
485 $self->{XML} = $header.$message.$footer;
486
487 } elsif ($self->{type} == MOBY::Async::LSAE::LSAE_HEARTBEAT_PROGRESS_EVENT) {
488
489 $self->{XML} = $header.$message;
490 $self->{XML} .= "<heartbeat_progress/>";
491 $self->{XML} .= $footer;
492
493 } elsif ($self->{type} == MOBY::Async::LSAE::LSAE_PERCENT_PROGRESS_EVENT) {
494
495 return if ($self->{percentage} > 100);
496
497 $self->{XML} = $header.$message;
498 $self->{XML} .= "<percent_progress percentage=\"".$self->{percentage}."\"/>";
499 $self->{XML} .= $footer;
500
501 } elsif ($self->{type} == MOBY::Async::LSAE::LSAE_STATE_CHANGED_EVENT) {
502
503 return unless ( $self->{previous_state} eq 'created' ||
504 $self->{previous_state} eq 'running' ||
505 $self->{previous_state} eq 'completed' ||
506 $self->{previous_state} eq 'terminated_by_request' ||
507 $self->{previous_state} eq 'terminated_by_error' );
508 return unless ( $self->{new_state} eq 'created' ||
509 $self->{new_state} eq 'running' ||
510 $self->{new_state} eq 'completed' ||
511 $self->{new_state} eq 'terminated_by_request' ||
512 $self->{new_state} eq 'terminated_by_error' );
513
514 my $previous_state = " previous_state=\"".$self->{previous_state}."\"" if ($self->{previous_state});
515 my $new_state = " new_state=\"".$self->{new_state}."\"" if ($self->{new_state});
516
517 $self->{XML} = $header.$message;
518 $self->{XML} .= "<state_changed$previous_state$new_state/>";
519 $self->{XML} .= $footer;
520
521 } elsif ($self->{type} == MOBY::Async::LSAE::LSAE_STEP_PROGRESS_EVENT) {
522
523 $self->{XML} = $header.$message;
524 $self->{XML} .= "<step_progress total_steps=\"".$self->{total_steps}."\" steps_completed=\"".$self->{steps_completed}."\"/>";
525 $self->{XML} .= $footer;
526
527 } elsif ($self->{type} == MOBY::Async::LSAE::LSAE_TIME_PROGRESS_EVENT) {
528
529 $self->{XML} = $header.$message;
530 $self->{XML} .= "<time_progress remaining=\"".$self->{remaining}."\"/>";
531 $self->{XML} .= $footer;
532 }
533
534 return $self->{XML};
535 }
536  
537 sub destroy {
538 my $self = shift;
539 delete($self->{type});
540 delete($self->{id});
541 delete($self->{timestamp});
542 delete($self->{message});
543 delete($self->{percentage});
544 delete($self->{previous_state});
545 delete($self->{new_state});
546 delete($self->{total_steps});
547 delete($self->{steps_completed});
548 delete($self->{remaining});
549 delete($self->{XML});
550 }
551  
552 1;