File Coverage

blib/lib/AI/ExpertSystem/Simple/Knowledge.pm
Criterion Covered Total %
statement 57 57 100.0
branch 34 34 100.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 10 10 100.0
total 116 116 100.0


line stmt bran cond sub pod time code
1             package AI::ExpertSystem::Simple::Knowledge;
2              
3 1     1   3872 use strict;
  1         2  
  1         35  
4 1     1   5 use warnings;
  1         2  
  1         744  
5              
6             our $VERSION = '1.2';
7              
8             sub new {
9 4     4 1 2532 my ($class, $name) = @_;
10              
11 4 100       24 die "Knowledge->new() takes 1 argument" if scalar(@_) != 2;
12 2 100       11 die "Knowledge->new() argument 1, (NAME) is undefined" if ! defined($name);
13              
14 1         3 my $self = {};
15              
16 1         3 $self->{_name} = $name;
17 1         2 $self->{_value} = undef;
18 1         3 $self->{_setter} = undef;
19 1         2 $self->{_question} = undef;
20 1         2 $self->{_responses} = ();
21              
22 1         6 return bless $self, $class;
23             }
24              
25             sub reset {
26 4     4 1 2083 my ($self) = @_;
27              
28 4 100       18 die "Knowledge->reset() takes no arguments" if scalar(@_) != 1;
29              
30 3         4 $self->{_value} = undef;
31 3         5 $self->{_setter} = undef;
32             }
33              
34             sub set_value {
35 9     9 1 1798 my ($self, $value, $setter) = @_;
36              
37 9 100       39 die "Knowledge->set_value() takes 2 argument" if scalar(@_) != 3;
38 6 100       19 die "Knowledge->set_value() argument 1, (VALUE) is undefined" if ! defined($value);
39 5 100       16 die "Knowledge->set_value() argument 2, (SETTER) is undefined" if ! defined($setter);
40              
41 4 100       14 if(defined($self->{_value})) {
42 1         9 die "Knowledge->set_value() has already been set";
43             }
44              
45 3         5 $self->{_value} = $value;
46 3         31 $self->{_setter} = $setter;
47             }
48              
49             sub get_value {
50 3     3 1 1563 my ($self) = @_;
51              
52 3 100       17 die "Knowledge->get_value() takes no arguments" if scalar(@_) != 1;
53              
54 2         11 return $self->{_value};
55             }
56              
57             sub get_setter {
58 2     2 1 471 my ($self) = @_;
59              
60 2 100       12 die "Knowledge->get_setter() takes no arguments" if scalar(@_) != 1;
61              
62 1         5 return $self->{_setter};
63             }
64              
65             sub is_value_set {
66 8     8 1 925 my($self) = @_;
67              
68 8 100       38 die "Knowledge->is_value_set() takes no arguments" if scalar(@_) != 1;
69              
70 7         30 return defined($self->{_value});
71             }
72              
73             sub set_question {
74 4     4 1 1184 my ($self, $question, @responses) = @_;
75              
76 4 100       12 if(defined($self->{_question})) {
77 1         9 die "Knowledge->set_question() has already been set";
78             }
79              
80 3 100       18 die "Knowledge->set_question() takes 2 arguments" if scalar(@_) < 3;
81 2 100       14 die "Knowledge->set_question() argument 1, (QUESTION) is undefined" if ! defined($question);
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         2 $self->{_question} = $question;
86 1         2 push(@{$self->{_responses}}, @responses);
  1         4  
87             }
88              
89             sub get_question {
90 3     3 1 1103 my ($self) = @_;
91              
92 3 100       15 die "Knowledge->get_question() takes no arguments" if scalar(@_) != 1;
93              
94 2 100       5 if(!defined($self->{_question})) {
95 1         7 die "Knowledge->set_question() has not been set";
96             }
97              
98 1         2 return ($self->{_question}, @{$self->{_responses}});
  1         6  
99             }
100              
101             sub has_question {
102 7     7 1 505 my ($self) = @_;
103              
104 7 100       27 die "Knowledge->has_question() takes no arguments" if scalar(@_) != 1;
105              
106 6   100     43 return (defined($self->{_question}) and !defined($self->{_value}));
107             }
108              
109             sub name {
110 2     2 1 897 my ($self) = @_;
111              
112 2 100       15 die "Knowledge->name() takes no arguments" if scalar(@_) != 1;
113              
114 1         14 return $self->{_name};
115             }
116              
117             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.