File Coverage

blib/lib/Decision/Markov/State.pm
Criterion Covered Total %
statement 59 67 88.0
branch 14 22 63.6
condition 1 6 16.6
subroutine 13 14 92.8
pod 9 11 81.8
total 96 120 80.0


line stmt bran cond sub pod time code
1             package Decision::Markov::State;
2              
3             require 5.000;
4 1     1   7 use strict;
  1         3  
  1         52  
5 1     1   6 use diagnostics;
  1         2  
  1         7  
6 1     1   45 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         3  
  1         850  
7             require Exporter;
8             require AutoLoader;
9              
10             @ISA = qw(Exporter AutoLoader);
11             @EXPORT = qw();
12             $VERSION = "0.03";
13              
14             sub new {
15 3     3 1 4 my $this = shift;
16 3   33     15 my $class = ref($this) || $this;
17 3         4 my $name = shift;
18 3         5 my $utility = shift;
19 3         28 my $self = {};
20 3         7 bless $self, $class;
21 3         9 $self->Name($name); # State name
22 3         6 $self->{'utility'} = $utility; # State utility
23 3         5 $self->{'transitions'} = {}; # Transitions to other nodes
24 3         7 $self->Reset(); # Reset simulation parameters
25 3         8 return $self;
26             }
27              
28              
29             sub Reset {
30 12     12 1 14 my $self = shift;
31 12         24 $self->NumPatients(0);
32 12         27 $self->NewNumPatients(0);
33             }
34              
35             sub Name {
36 18     18 1 19 my $self = shift;
37 18 100       66 @_ ? $self->{'name'} = shift : $self->{'name'};
38             }
39             sub NumPatients {
40 595     595 1 1380 my $self = shift;
41 595 100       7678 @_ ? $self->{'numpatients'} = shift : $self->{'numpatients'};
42             }
43             sub NewNumPatients {
44 594     594 0 2041 my $self = shift;
45 594 100       7645 @_ ? $self->{'newnumpatients'} = shift : $self->{'newnumpatients'};
46             }
47 111     111 0 691 sub Transitions { %{ $_[0]->{'transitions'} } }
  111         2883  
48             sub Utility {
49 187     187 1 198 my $self = shift;
50 187         182 my $cycle = shift;
51 187         241 my $utility = $self->{'utility'};
52 187 50       374 $utility = &$utility($cycle) if (ref($utility));
53 187         4586 return $utility;
54             }
55              
56             sub AddTransition {
57 7     7 1 7 my $self = shift;
58 7         8 my $to = shift;
59 7         18 my $prob = shift;
60 7 100       18 return "AddTransition: There's already a transition from " . $self->Name . " to " . $to->Name if defined($self->{'transitions'}->{$to->Name});
61 6         16 $self->{'transitions'}->{$to->Name} = [ $to, $prob ];
62 6         12 return undef;
63             }
64              
65             sub TransitionProb {
66 0     0 1 0 my $self = shift;
67 0         0 my $to = shift;
68 0         0 my $cycle = shift;
69 0         0 my %transitions = $self->Transitions;
70 0 0       0 return 0 unless $transitions{$to->Name};
71 0         0 my $prob = $transitions{$to->Name}[1];
72 0 0 0     0 return $prob unless (ref($prob) and defined($cycle));
73 0         0 return &$prob($cycle);
74             }
75              
76             sub SumProbs {
77 3     3 1 4 my $self = shift;
78 3         4 my $cycle = shift;
79 3 50       8 $cycle = 3 unless defined($cycle);
80 3         4 my $sum = 0;
81 3         8 my %transitions = $self->Transitions;
82 3         8 foreach my $listref (values %transitions) {
83 6         7 my $prob = ${ $listref }[1];
  6         8  
84 6 50       14 $prob = &$prob($cycle) if (ref($prob));
85 6         9 $sum += $prob;
86             }
87 3         11 return $sum;
88             }
89              
90             # Are we in a final state? A final state is a state that has only
91             # one transition path, leading back to the state itself.
92             sub FinalState {
93 13     13 1 42 my $self = shift;
94 13         22 my %transitions = $self->Transitions;
95 13         38 my @transition_states = keys %transitions;
96             # Not a final state if there are multiple transitions
97 13 100       54 return 0 if (scalar(@transition_states) > 1);
98             # Not a final state if the transition is to a different state
99 2 50       12 return 0 if $transitions{$transition_states[0]}->[0] ne $self;
100 2         8 return 1;
101             }
102              
103              
104             # Autoload methods go after =cut, and are processed by the autosplit program.
105              
106             1;
107             __END__