Prolog/KnowledgeBase.pm

filter: Statementstmt 31.6% 51 / 161 Branchbran 15.3% 8 / 52 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 46.1% 12 / 26 Podpod 0.0% 0 / 17 totaltotal 26.7% 71 / 265 CC 29 SCAR 31.2
CC
29
Cov
27%
dump
72.0
retract
56.0
assert
20.0
CRAP 22.5
« Prolog/Engine/Primitives.pm ↑ Summary Prolog/Term.pm »
1 package AI::Prolog::KnowledgeBase;
2 13 $REVISION = '$Id: KnowledgeBase.pm,v 1.5 2005/06/25 23:06:53 ovid Exp $';
3 13 $VERSION = '0.02';
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Carp qw( confess carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Hash::Util 'lock_keys';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use aliased 'AI::Prolog::Engine';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use aliased 'AI::Prolog::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use aliased 'AI::Prolog::TermList::Clause';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 22 my $self = bless {
16 ht => {},
17 primitives => {}, # only uses keys
18 oldIndex => "",
19 } => shift;
20 22 lock_keys %$self;
21 22 return $self;
22 }
23  
24 1 ▶sub ht { shift->{ht} } # temp hack XXX
Subroutine ht: called CC=1 SCAR=0.0
Pod undocumented
25  
26 0 ▶sub to_string {
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my $self = shift;
28 return "{"
29 . (
30 0 join ', ' => map { join '=' => $_->[0], $_->[1] }
31 0 sort { $a->[2] <=> $b->[2] }
32 0 map { [ $_, $self->_sortable_term( $self->{_vardict}{$_} ) ] }
33 0 keys %{ $self->{ht} }
34 ) . "}";
35 }
36  
37 0 ▶sub _sortable_term {
Subroutine _sortable_term: not called CC=1 SCAR=6.9
38 0 my ( $self, $term ) = @_;
39 0 my $string = $term->to_string;
40 0 my $number = substr $string => 1;
41 0 return $string, $number;
42 }
43  
44 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod undocumented
45 0 my ( $self, $key, $termlist ) = @_;
46 0 $self->{ht}{$key} = $termlist;
47 }
48  
49 0 ▶sub elements { [ values %{ shift->{ht} } ] }
Subroutine elements: not called CC=1 SCAR=6.9
Pod undocumented
50  
51 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my $self = shift;
53 0 $self->{ht} = {};
54 0 $self->{primitives} = {};
55 0 $self->{oldIndex} = '';
56 }
57  
58 0 ▶sub consult {
Subroutine consult: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my ( $self, $program ) = @_;
60 0 $self->{oldIndex} = '';
61 0 return Parser->consult( $program, $self );
62 }
63  
64 243 ▶sub add_primitive {
Subroutine add_primitive: called CC=3 SCAR=11.1
Pod undocumented
65 243 my ( $self, $clause ) = @_;
66 243 my $term = $clause->term;
67 243 my $predicate = $term->predicate;
68 243 my $c = $self->{ht}{$predicate};
69 243 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 9 234 243
70 9 ▶ while ( $c->next_clause ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->next_clause) 0 9 9
71 0 $c = $c->next_clause;
72 }
73 9 $c->next_clause($clause);
74 }
75 else {
76 234 $self->{primitives}{$predicate} = 1;
77 234 $self->{ht}{$predicate} = $clause;
78 }
79 }
80  
81 151 ▶sub add_clause {
Subroutine add_clause: called CC=4 SCAR=14.5
Pod undocumented
82 151 my ( $self, $clause ) = @_;
83 151 my $term = $clause->term;
84 151 my $predicate = $term->predicate;
85 151 ▶ if ( $self->{primitives}{$predicate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"primitives"}{$predicate}) 0 151 151
86 0 carp("Trying to modify primitive predicate: $predicate");
87 0 return;
88 }
89 151 ▶ unless ( $predicate eq $self->{oldIndex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predicate eq $self->{'oldIndex'}) { } 43 108 151
90 108 delete $self->{ht}{$predicate};
91 108 $self->{ht}{$predicate} = $clause;
92 108 $self->{oldIndex} = $predicate;
93 }
94 else {
95 43 my $c = $self->{ht}{$predicate};
96 43 ▶ while ( $c->next_clause ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->next_clause) 0 43 43
97 0 $c = $c->next_clause;
98 }
99 43 $c->next_clause($clause);
100 }
101 }
102  
103 0 ▶sub assert {
Subroutine assert: not called CC=4 SCAR=30.0
Pod undocumented
104 0 my ( $self, $term ) = @_;
105 0 $term = $term->clean_up;
106  
107 # XXX whoops. Need to check exact semantics in Term
108 0 my $newC = Clause->new( $term->deref, undef );
109  
110 0 my $predicate = $term->predicate;
111 0 ▶ if ( $self->{primitives}{$predicate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"primitives"}{$predicate}) 0 0 0
112 0 carp("Trying to assert a primitive: $predicate");
113 0 return;
114 }
115 0 my $c = $self->{ht}{$predicate};
116 0 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 0 0
117 0 ▶ while ( $c->next_clause ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->next_clause) 0 0 0
118 0 $c = $c->next_clause;
119 }
120 0 $c->next_clause($newC);
121 }
122 else {
123 0 $self->{ht}{$predicate} = $newC;
124 }
125 }
126  
127 0 ▶sub asserta {
Subroutine asserta: not called CC=2 SCAR=17.9
Pod undocumented
128 0 my ( $self, $term ) = @_;
129 0 my $predicate = $term->predicate;
130 0 ▶ if ( $self->{primitives}{$predicate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"primitives"}{$predicate}) 0 0 0
131 0 carp("Trying to assert a primitive: $predicate");
132 0 return;
133 }
134 0 $term = $term->clean_up;
135 0 my $newC = Clause->new( $term->deref, undef );
136 0 my $c = $self->{ht}{$predicate};
137 0 $newC->next_clause($c);
138 0 $self->{ht}{$predicate} = $newC;
139 }
140  
141 0 ▶sub retract {
Subroutine retract: not called CC=7 SCAR=40.3
Pod undocumented
142 0 my ( $self, $term, $stack ) = @_;
143 0 my $newC = Clause->new( $term, undef ); #, undef);
144 0 my $predicate = $term->predicate;
145 0 ▶ if ( exists $self->{primitives}{$predicate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"primitives"}{$predicate}) 0 0 0
146 0 carp("Trying to retract a primitive: $predicate");
147 0 return;
148 }
149 0 my $cc;
150 0 my $c = $self->{ht}{$predicate};
151  
152 0 ▶ while ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 0 0
153 0 my $vars = [];
154 0 my $xxx = $c->term->refresh($vars);
155 0 my $top = @{$stack};
156  
157 0 ▶ if ( $xxx->unify( $term, $stack ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xxx->unify($term, $stack)) 0 0 0
158 0 ▶ if ($cc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cc) { } 0 0 0
elsif (not $c->next_clause) { } 0 0 0
159 0 $cc->next_clause( $c->next_clause );
160 }
161 elsif ( !$c->next_clause ) {
162 0 delete $self->{ht}{$predicate};
163 }
164 else {
165 0 $self->{ht}{$predicate} = $c->next_clause;
166 }
167 0 return 1;
168 }
169 0 ▶ for ( my $i = @{$stack} - $top; $i > 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
170 0 my $t = pop @{$stack};
171 0 $t->unbind;
172 }
173 0 $cc = $c;
174 0 $c = $c->next_clause;
175 }
176 0 return;
177 }
178  
179 0 ▶sub retractall {
Subroutine retractall: not called CC=2 SCAR=17.9
Pod undocumented
180 0 my ( $self, $term, $arity ) = @_;
181 0 my $predicate = $term->predicate;
182 0 ▶ if ( $self->{primitives}{$predicate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"primitives"}{$predicate}) 0 0 0
183 0 carp("Trying to retractall primitives: $predicate");
184 0 return;
185 }
186 0 delete $self->{ht}{$predicate};
187 0 return 1;
188 }
189  
190 117 ▶sub get {
Subroutine get: called CC=2 SCAR=7.1
Pod undocumented
191 117 my ( $self, $term ) = @_;
192 117 ▶ my $key = ref $term ? $term->to_string : $term;
Branch: 1 branch
BranchTrueFalseTotal
ref $term ? : 0 117 117
193 117 return $self->{ht}{$key};
194 }
195  
196 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
Pod undocumented
197 0 my ( $self, $term, $value ) = @_;
198 0 ▶ my $key = ref $term ? $term->to_string : $term;
Branch: 1 branch
BranchTrueFalseTotal
ref $term ? : 0 0 0
199 0 $self->{ht}{$key} = $value->clean_up;
200 }
201  
202 0 ▶sub _print { print @_ }
Subroutine _print: not called CC=1 SCAR=6.9
203  
204 0 ▶sub dump {
Subroutine dump: not called CC=8 SCAR=42.8
Pod undocumented
205 0 my ( $self, $full ) = @_;
206 0 my $i = 1;
207 0 ▶ while ( my ( $key, $value ) = each %{ $self->{ht} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"ht"};}) 0 0 0
208 0 ▶ next if !$full && ( $self->{primitives}{$key} || $value->is_builtin );
Branch: 1 branch
BranchTrueFalseTotal
if not $full and $self->{"primitives"}{$key} || $value->is_builtin 0 0 0
Condition0%0/6
not $full and $self->{"primitives"}{$key} || $value->is_builtin
!ll&&!rl&&r
000
$self->{"primitives"}{$key} or $value->is_builtin
l!l&&r!l&&!r
000
MC/DC0%0/3
not $full and $self->{"primitives"}{$key} || $value->is_builtin
not $full $self->{"primitives"}{$key} $value->is_builtin
Truth table0%0/4
not(A) and B or C
A= not $fullB= $self->{"primitives"}{$key}C= $value->is_builtin
ABCresult
0XX0
1000
11X1
1011
209 0 ▶ if ( $value->isa(Clause) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('AI::Prolog::TermList::Clause')) { } 0 0 0
210 0 _print( $i++ . ". $key: \n" );
211 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$value and do { do { _print(' ' . $value->term->to_string); if ($value->next) { _print(' :- ' . $value->next->to_string); } ; _print(".\n"); $value = $value->next_clause } } 0 0 0
212 0 _print( " " . $value->term->to_string );
213 0 ▶ if ( $value->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->next) 0 0 0
214 0 _print( " :- " . $value->next->to_string );
215 }
216 0 _print(".\n");
217 0 $value = $value->next_clause;
218 } while ($value);
219 }
220 else {
221 0 _print( $i++ . ". $key = $value\n" );
222 }
223 }
224 0 _print("\n");
225 }
226  
227 0 ▶sub list {
Subroutine list: not called CC=4 SCAR=30.0
Pod undocumented
228 0 my ( $self, $predicate ) = @_;
229 0 print "\n$predicate: \n";
230 0 ▶ my $head = $self->{ht}{$predicate}
Branch: 1 branch
BranchTrueFalseTotal
my $head = $self->{"ht"}{$predicate} or warn "Cannot list unknown predicate ($predicate)" 0 0 0
231 or warn "Cannot list unknown predicate ($predicate)";
232 0 ▶ while ($head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head) 0 0 0
233 0 print " " . $head->term->to_string;
234 0 ▶ if ( $head->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head->next) 0 0 0
235 0 print " :- " . $head->next->to_string;
236 }
237 0 print ".\n";
238 0 $head = $head->next_clause;
239 }
240 }
241  
242 13 1;
243  
244 __END__
« Prolog/Engine/Primitives.pm ↑ Summary Prolog/Term.pm »