| 1 | package AI::ExpertSystem::Simple::Knowledge; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.2'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 4 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 9 | 4 | my ($class, $name) = @_; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 4 | ▶ | die "Knowledge->new() takes 1 argument" if scalar(@_) != 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 12 | 2 | ▶ | die "Knowledge->new() argument 1, (NAME) is undefined" if ! defined($name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | my $self = {}; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | $self->{_name} = $name; | |||||||||||||||||||
| 17 | 1 | $self->{_value} = undef; | |||||||||||||||||||
| 18 | 1 | $self->{_setter} = undef; | |||||||||||||||||||
| 19 | 1 | $self->{_question} = undef; | |||||||||||||||||||
| 20 | 1 | $self->{_responses} = (); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | return bless $self, $class; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 4 | ▶ | sub reset { | ||||||||||||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 26 | 4 | my ($self) = @_; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 4 | ▶ | die "Knowledge->reset() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 3 | $self->{_value} = undef; | |||||||||||||||||||
| 31 | 3 | $self->{_setter} = undef; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 9 | ▶ | sub set_value { | ||||||||||||||||||
|
Subroutine
set_value: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 35 | 9 | my ($self, $value, $setter) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 9 | ▶ | die "Knowledge->set_value() takes 2 argument" if scalar(@_) != 3; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 6 | ▶ | die "Knowledge->set_value() argument 1, (VALUE) is undefined" if ! defined($value); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 5 | ▶ | die "Knowledge->set_value() argument 2, (SETTER) is undefined" if ! defined($setter); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 4 | ▶ | if(defined($self->{_value})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 1 | die "Knowledge->set_value() has already been set"; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 3 | $self->{_value} = $value; | |||||||||||||||||||
| 46 | 3 | $self->{_setter} = $setter; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 3 | ▶ | sub get_value { | ||||||||||||||||||
|
Subroutine
get_value: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 50 | 3 | my ($self) = @_; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 3 | ▶ | die "Knowledge->get_value() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 2 | return $self->{_value}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | ▶ | sub get_setter { | ||||||||||||||||||
|
Subroutine
get_setter: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 58 | 2 | my ($self) = @_; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | ▶ | die "Knowledge->get_setter() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 1 | return $self->{_setter}; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 8 | ▶ | sub is_value_set { | ||||||||||||||||||
|
Subroutine
is_value_set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 66 | 8 | my($self) = @_; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 8 | ▶ | die "Knowledge->is_value_set() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 7 | return defined($self->{_value}); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 4 | ▶ | sub set_question { | ||||||||||||||||||
|
Subroutine
set_question: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 74 | 4 | my ($self, $question, @responses) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 4 | ▶ | if(defined($self->{_question})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 1 | die "Knowledge->set_question() has already been set"; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 3 | ▶ | die "Knowledge->set_question() takes 2 arguments" if scalar(@_) < 3; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 2 | ▶ | die "Knowledge->set_question() argument 1, (QUESTION) is undefined" if ! defined($question); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | # This test just doesnt work for a list | ||||||||||||||||||||
| 83 | # die "Knowledge->set_question() argument 2, (RESPONSES) is undefined" if scalar(@responses) == 0; | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 1 | $self->{_question} = $question; | |||||||||||||||||||
| 86 | 1 | push(@{$self->{_responses}}, @responses); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | ▶ | sub get_question { | ||||||||||||||||||
|
Subroutine
get_question: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 90 | 3 | my ($self) = @_; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 3 | ▶ | die "Knowledge->get_question() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 2 | ▶ | if(!defined($self->{_question})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 1 | die "Knowledge->set_question() has not been set"; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 1 | return ($self->{_question}, @{$self->{_responses}}); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 7 | ▶ | sub has_question { | ||||||||||||||||||
|
Subroutine
has_question: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 102 | 7 | my ($self) = @_; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 7 | ▶ | die "Knowledge->has_question() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 6 | ▶ | return (defined($self->{_question}) and !defined($self->{_value})); | ||||||||||||||||||
|
Condition100%3/3
defined $self->{'_question'} && !defined($self->{'_value'})
MC/DC100%2/2
defined $self->{'_question'} && !defined($self->{'_value'})
defined $self->{'_question'}
!defined($self->{'_value'})
Truth table100%3/3
A && not(B)
A= defined $self->{'_question'}B= !defined($self->{'_value'})
| |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 2 | ▶ | sub name { | ||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 110 | 2 | my ($self) = @_; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 2 | ▶ | die "Knowledge->name() takes no arguments" if scalar(@_) != 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 1 | return $self->{_name}; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 1 | 1; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head1 NAME | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | AI::ExpertSystem::Simple::Knowledge - Utility class for a simple expert system | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | =head1 VERSION | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | This document refers to verion 1.2 of AI::ExpertSystem::Simple::Knowledge, released June 10, 2003 | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head1 SYNOPSIS | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | This class handles the attributes and their values within the expert system along with the optional question that | ||||||||||||||||||||
| 130 | can be asked of the user to set the value of the attribute. The valid responses to the optional question are also held. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head1 DESCRIPTION | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head2 Overview | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | This is a utility class for AI::ExpertSystem::Simple | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =head2 Constructors and initialisation | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =over 4 | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | =item new( NAME ) | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | The constructor sets up the basic attribute name / value pairing. In the base case an attribute has a name with no value. | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Optional questions and the valid responses can be set later and the value of the attribute is set during the consultation. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =back | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 Public methods | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =over 4 | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =item reset( ) | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Resets the state of knowledge back to what it was when the object was created | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =item set_value( VALUE, SETTER ) | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | During the consultation process the VALUE for an attribute can be set by either asking the user a question or by deduction. The value is then recorded along with the rule that set the value (or blank it if was a question). | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | =item get_value( ) | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | Returns the current value of the attribute. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =item get_setter( ) | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Returns the current setter of the attribute. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =item is_value_set( ) | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Returns true if the value of the attribute is set or false if not. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =item set_question( QUESTION, RESPONSES ) | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Allows a question to ask of the user to set the value of the attribute. QUESTION is the text that will be displayed to the user and RESPONSES is a list of valid responses that the user may select from. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =item get_question( ) | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Returns the QUESTION and list of valid RESPONSES for the attribute. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =item has_question( ) | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | Returns true if the attribute has a question to ask the user if the VALUE of the attribute has not already been set. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =item name( ) | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | This method returns the value of the NAME argument that was set when the object was constructed. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =back | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head2 Private methods | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | None | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head1 ENVIRONMENT | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | None | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =head1 DIAGNOSTICS | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =over 4 | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | =item Knowledge->new() takes 1 argument | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | When the constructor is initialised it requires one argument. This message is given if more of less arguments are given. | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | =item Knowledge->new() argument 1, (NAME) is undefined | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | The correct number of arguments were supplied to the constructor, however the first argument, NAME, was undefined. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =item Knowledge->reset() takes no arguments | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =item Knowledge->set_value() takes 2 argument | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | When the method is called it requires two arguments. This message is given if more of less arguments are given. | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | =item Knowledge->set_value() argument 1, (VALUE) is undefined | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | The correct number of arguments were supplied to the method call, however the first argument, VALUE, was undefined. | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =item Knowledge->set_value() argument 2, (SETTER) is undefined | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | The correct number of arguments were supplied to the method call, however the second argument, SETTER, was undefined. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | =item Knowledge->set_value() has already been set | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | This method has already been called and the value set. It cannot be called twice. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =item Knowledge->get_value() takes no arguments | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | =item Knowledge->get_setter() takes no arguments | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =item Knowledge->is_value_set() takes no arguments | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =item Knowledge->set_question() takes 2 arguments | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | When the method is called it requires two arguments. This message is given if more of less arguments are given. | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =item Knowledge->set_question() argument 1, (QUESTION) is undefined | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | The correct number of arguments were supplied to the method call, however the first argument, QUESTION, was undefined. | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | =item Knowledge->set_question() has already been set | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | This method has already been called and the value set. It cannot be called twice. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | =item Knowledge->get_question() takes no arguments | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | =item Knowledge->get_question() has not been set | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | The value has not been set by Knowledge->set_question() and, therefore, cannot be retrieved. | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | =item Knowledge->has_question() takes no arguments | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | =item Knowledge->name() takes no arguments | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | When the method is called it requires no arguments. This message is given if some where supplied. | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | =back | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | =head1 BUGS | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | None | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | =head1 FILES | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | See Knowledge.t in the test directory | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | =head1 SEE ALSO | ||||||||||||||||||||
| 283 | |||||||||||||||||||||
| 284 | AI::ExpertSystem::Simple - The base class for the expert system shell | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | AI::ExpertSystem::Simple::Goal - A utility class | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | AI::ExpertSystem::Simple::Rules - A utility class | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | =head1 AUTHORS | ||||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | Peter Hickman (peterhi@ntlworld.com) | ||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | =head1 COPYRIGHT | ||||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | Copyright (c) 2003, Peter Hickman. All rights reserved. | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | This module is free software. It may be used, redistributed and/or | ||||||||||||||||||||
| 299 | modified under the same terms as Perl itself. | ||||||||||||||||||||