Advanced/KnowledgeDB/Base.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 105 CC 14 SCAR 29.2
CC
14
Cov
0%
rule_goals
30.0
rule_causes
30.0
find_rule_by_goal
12.0
CRAP 18.6
1 #
2 # AI::ExpertSystem::Advanced::KnowledgeDB::Base
3 #
4 # Author(s): Pablo Fischer (pfischer@cpan.org)
5 # Created: 11/29/2009 19:14:28 PST 19:14:28
6 package AI::ExpertSystem::Advanced::KnowledgeDB::Base;
7  
8 =head1 NAME
9  
10 AI::ExpertSystem::Advanced::KnowledgeDB::Base - Base class for knowledge DBs.
11  
12 =head1 DESCRIPTION
13  
14 All knowledge databases that L<AI::ExpertSystem::Advanced> uses should extend
15 from this class.
16  
17 This base class implements the basic methods required for extracting the rules,
18 causes, goals and questions from the a plain text knowledge database, eg, all
19 the records remain in the application memory instead of a database engine such
20 as MySQL or SQLite.
21  
22 =cut
23 use Moose;
24 use AI::ExpertSystem::Advanced::Dictionary;
25  
26 our $VERSION = '0.03';
27  
28 =head1 Attributes
29  
30 =over 4
31  
32 =item B<rules>
33  
34 This hash has the rules contained in the knowledge database. It's populated
35 when an instance of L<AI::ExpertSystem::Advanced::KnowledgeDB::Base> is
36 created.
37  
38 B<TIP>: There's no sense in filling this hash if you are going to be using a
39 database engine such as MySQL, SQLite or others. The hash is useful if your
40 knowledge database will remain in application memory.
41  
42 =cut
43 has 'rules' => (
44 is => 'ro',
45 isa => 'HashRef');
46  
47 =item B<questions>
48  
49 Similar and same concept of C<rules>, but this will have a list (if available)
50 of what questions should be done to certain facts.
51  
52 =back
53  
54 =cut
55 has 'questions' => (
56 is => 'ro',
57 isa => 'HashRef',
58 default => sub { return {}; });
59  
60 =head1 Methods
61  
62 =head2 B<read()>
63  
64 This method reads the knowledge database. This is the only method you need to
65 define even if you are going to load the database in memory or if you are
66 going to query it.
67  
68 =cut
69 sub read {
70 confess "You can't call KnowedgeDB::Base! (abstract method)";
71 }
72  
73 =head2 B<rule_goals($rule)>
74  
75 Returns all the goals (usually only one) of the given C<$rule>.
76  
77 The goals B<should> be returned as a L<AI::ExpertSystem::Advanced::Dictionary>.
78  
79 B<NOTE>: Rewrite this method if you are not going to use the C<rules> hash (eg,
80 you will use a database engine).
81  
82 =cut
83 sub rule_goals {
84 my ($self, $rule) = @_;
85  
86 if (!defined $self->{'rules'}->[$rule]) {
87 confess "Rule $rule does not exist";
88 }
89 my @facts;
90 # Get all the facts of this goal (usually only one)
91 foreach (@{$self->{'rules'}->[$rule]->{'goals'}}) {
92 my $id;
93 # it has an ID?
94 if (defined $_->{'id'}) {
95 $id = $_->{'id'};
96 } elsif (defined $_->{'name'}) { # or a name?
97 $id = $_->{'name'};
98 }
99 if (defined $id) {
100 push(@facts, $id);
101 } else {
102 confess "Seems rule $rule does not have an id or name key";
103 }
104 }
105 my $goals_dict = AI::ExpertSystem::Advanced::Dictionary->new(
106 stack => \@facts);
107 return $goals_dict;
108 }
109  
110 =head2 B<rule_causes($rule)>
111  
112 Returns all the causes of the given C<$rule>.
113  
114 Same as C<rule_goals()>, the causes should be returned as a
115 L<AI::ExpertSystem::Advanced::Dictionary>.
116  
117 B<NOTE>: Rewrite this method if you are not going to use the C<rules> hash (eg,
118 you will use a database engine).
119  
120 =cut
121 sub rule_causes {
122 my ($self, $rule) = @_;
123  
124 if (!defined $self->{'rules'}->[$rule]) {
125 confess "Rule $rule does not exist";
126 }
127 my @facts;
128 # Get all the facts of this cause
129 foreach (@{$self->{'rules'}->[$rule]->{'causes'}}) {
130 my $id;
131 # it has an ID?
132 if (defined $_->{'id'}) {
133 $id = $_->{'id'};
134 } elsif (defined $_->{'name'}) { # or a name?
135 $id = $_->{'name'};
136 }
137 if (defined $id) {
138 push(@facts, $id);
139 } else {
140 confess "Seems rule $rule does not have an id or name key";
141 }
142 }
143 my $causes_dict = AI::ExpertSystem::Advanced::Dictionary->new(
144 stack => \@facts);
145 return $causes_dict;
146 }
147  
148 =head2 B<find_rule_by_goal($goal)>
149  
150 Looks for the first rule that has the given C<goal> in its goals.
151  
152 If a rule is found then its number is returned, otherwise C<undef> is
153 returned.
154  
155 B<NOTE>: Rewrite this method if you are not going to use the C<rules> hash (eg,
156 you will use a database engine).
157  
158 =cut
159 sub find_rule_by_goal {
160 my ($self, $goal) = @_;
161  
162 my $rule_counter = 0;
163 foreach my $rule (@{$self->{'rules'}}) {
164 foreach my $rule_goal (@{$rule->{'goals'}}) {
165 # Look in id and name for the match
166 foreach my $look_in (qw(id name)) {
167 if (defined $rule_goal->{$look_in}) {
168 if ($rule_goal->{$look_in} eq $goal) {
169 return $rule_counter;
170 }
171 }
172 }
173 }
174 $rule_counter++;
175 }
176 return undef;
177 }
178  
179 =head2 B<get_question($fact)>
180  
181 Looks for a question about the given C<$fact>. If a question exists then this is
182 returned, otherwise C<undef> is returned.
183  
184 B<NOTE>: Rewrite this method if you are not going to use the C<rules> hash (eg,
185 you will use a database engine).
186  
187 =cut
188 sub get_question {
189 my ($self, $fact) = @_;
190  
191 if (defined $self->{'questions'}->{$fact}) {
192 return $self->{'questions'}->{$fact};
193 }
194 return undef;
195 }
196  
197 =head2 B<get_next_rule($current_rule)>
198  
199 Returns the ID of the next rule. When there are no more rules to work then
200 C<undef> should be returned.
201  
202 When it starts looking for the first rule, C<$current_rule> value will
203 be C<undef>.
204  
205 B<NOTE>: Rewrite this method if you are not going to use the C<rules> hash (eg,
206 you will use a database engine).
207  
208 =cut
209 sub get_next_rule {
210 my ($self, $current_rule) = @_;
211  
212 my $next_rule;
213 if (defined $current_rule) {
214 $next_rule = $current_rule+1;
215 } else {
216 $next_rule = 0;
217 }
218  
219 if (defined $self->{'rules'}->[$next_rule]) {
220 return $next_rule;
221 } else {
222 return undef;
223 }
224 }
225  
226 =head1 AUTHOR
227
228 Pablo Fischer (pablo@pablo.com.mx).
229  
230 =head1 COPYRIGHT
231
232 Copyright (C) 2010 by Pablo Fischer.
233
234 This library is free software; you can redistribute it and/or modify
235 it under the same terms as Perl itself.
236  
237 =cut
238  
239 1;
240