Prolog/Term.pm

filter: Statementstmt 78.3% 221 / 282 Branchbran 68.2% 101 / 148 Conditioncond 59.2% 45 / 76 MC/DCmcdc 34.0% 16 / 47 Subroutinesub 82.6% 38 / 46 Podpod 0.0% 0 / 30 totaltotal 66.9% 421 / 629 CC 108 SCAR 27.6
CC
108
Cov
73%
_dumpit
72.0
_clean_up
56.0
occurs1
30.0
CRAP 15.8
« Prolog/KnowledgeBase.pm ↑ Summary Prolog/Engine.pm »
1 package AI::Prolog::Term;
2 13 $REVISION = '$Id: Term.pm,v 1.10 2005/08/06 23:28:40 ovid Exp $';
3
4 13 $VERSION = '0.07';
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Carp qw( croak confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 13 ▶use Hash::Util 'lock_keys';
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 13 ▶use aliased 'AI::Prolog::Term::Cut';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use aliased 'AI::Prolog::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 13 ▶use aliased 'Hash::AsObject';
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 13 ▶use constant NULL => 'null';
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 # Var is a type of term
19 # A term is a basic data structure in Prolog
20 # There are three types of terms:
21 # 1. Values (i.e., have a functor and arguments)
22 # 2. Variables (i.e., unbound)
23 # 3. References (bound to another variable)
24
25 13 my $VARNUM = 1;
26
27 # controls where occurcheck is used in unification.
28 # In early Java versions, the occurcheck was always performed
29 # which resulted in lower performance.
30
31 13 my $OCCURCHECK = 0;
32
33 309 ▶sub occurcheck {
Subroutine occurcheck: called CC=2 SCAR=6.9
Pod undocumented
34 309 my ( $class, $value ) = @_;
35 309 ▶ $OCCURCHECK = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 3 306 309
36 309 return $OCCURCHECK;
37 }
38
39 # controls printing of lists as [a,b]
40 # instead of cons(a, cons(b, null))
41
42 359 ▶sub prettyprint {1}
Subroutine prettyprint: called CC=1 SCAR=0.0
Pod undocumented
43
44 13 my $CUT = Cut->new(0);
45 86 ▶sub CUT {$CUT}
Subroutine CUT: called CC=1 SCAR=0.0
Pod undocumented
46
47 2464 ▶sub new {
Subroutine new: called CC=9 SCAR=22.3
Pod undocumented
48 2464 my $proto = shift;
49 2464 ▶ my $class = CORE::ref $proto || $proto; # yes, I know what I'm doing
Condition66%2/3
CORE::ref $proto || $proto
l!l&&r!l&&!r
30821560
MC/DC0%0/2
CORE::ref $proto || $proto
CORE::ref $proto $proto
Truth table66%2/3
A || B
A= CORE::ref $protoB= $proto
ABresult
1X1
011
000
50 2464 ▶ return $class->_new_var unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 273 2191 2464
51 2191 ▶ if ( 2 == @_ ) { # more common (performance)
Branch: 2 branches
BranchTrueFalseTotal
if (2 == @_) { } 1651 540 2191
elsif (1 == @_) { } 539 1 540
52 1651 ▶ return _new_from_functor_and_arity( $class, @_ )
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq CORE::ref $_[1] 1651 0 1651
53 unless 'ARRAY' eq CORE::ref $_[1];
54 }
55 elsif ( 1 == @_ ) {
56 539 my $arg = shift;
57 539 ▶ return _new_with_id( $class, $arg )
Branch: 1 branch
BranchTrueFalseTotal
if not CORE::ref $arg and $arg =~ /^[[:digit:]]+$/ 498 41 539
Condition66%2/3
not CORE::ref $arg and $arg =~ /^[[:digit:]]+$/
!ll&&!rl&&r
041498
MC/DC50%1/2
not CORE::ref $arg and $arg =~ /^[[:digit:]]+$/
not CORE::ref $arg $arg =~ /^[[:digit:]]+$/
Truth table66%2/3
not(A) and B
A= not CORE::ref $argB= $arg =~ /^[[:digit:]]+$/
ABresult
0X0
100
111
58 if !CORE::ref $arg && $arg =~ /^[[:digit:]]+$/;
59 41 ▶ return _new_from_string( $class, $arg ) if !CORE::ref $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::ref $arg 41 0 41
60
61 #return $arg->_term($class) if CORE::ref $arg && $arg->isa(Parser);
62 }
63 1 croak("Unknown arguments to Term->new");
64 }
65
66 41 ▶sub _new_from_string {
Subroutine _new_from_string: called CC=1 SCAR=0.0
67 41 my ( $class, $string ) = @_;
68 41 my $parsed = Parser->new($string)->_term($class);
69 }
70
71 273 ▶sub _new_var {
Subroutine _new_var: called CC=1 SCAR=0.0
72 273 my $class = shift;
73
74 #print "*** _new_var @{[$VARNUM+1]}";
75 273 my $self = bless {
76 functor => undef,
77 arity => 0,
78 args => [],
79
80 # if bound is false, $self is a reference to a free variable
81 bound => 0,
82 varid => $VARNUM++,
83
84 # if bound and deref are both true, $self is a reference to a ref
85 deref => 0,
86 ref => undef,
87
88 ID => undef,
89 varname => undef,
90 _results => undef,
91
92 #source => "_new_var",
93 } => $class;
94 273 lock_keys %$self;
95 273 return $self;
96 }
97
98 498 ▶sub _new_with_id {
Subroutine _new_with_id: called CC=1 SCAR=0.0
99 498 my ( $class, $id ) = @_;
100
101 #print "*** _new_with_id: $id";
102 498 my $self = bless {
103 functor => undef,
104 arity => 0,
105 args => [],
106
107 # if bound is false, $self is a reference to a free variable
108 bound => 0,
109 varid => $id,
110
111 # if bound and deref are both true, $self is a reference to a ref
112 deref => 0,
113 ref => undef,
114
115 varname => undef,
116 ID => undef,
117 _results => undef,
118
119 #source => "_new_with_id: $id",
120 } => $class;
121 498 lock_keys %$self;
122 498 return $self;
123 }
124
125 1901 ▶sub _new_from_functor_and_arity {
Subroutine _new_from_functor_and_arity: called CC=3 SCAR=11.0
126 1901 my ( $class, $functor, $arity ) = @_;
127 1901 ▶ my $print_functor = defined $functor ? $functor : 'null';
Branch: 1 branch
BranchTrueFalseTotal
defined $functor ? : 404 1497 1901
128 1901 ▶ confess "undefined arity" unless defined $arity;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arity 0 1901 1901
129
130 #print "*** _new_from_functor_and_arity: ($print_functor) ($arity)";
131 1901 my $self = bless {
132 functor => $functor,
133 arity => $arity,
134 args => [],
135
136 # if bound is false, $self is a reference to a free variable
137 bound => 1,
138 varid => 0, # XXX ??
139 # if bound and deref are both true, $self is a reference to a ref
140 deref => 0,
141 ref => undef,
142
143 varname => undef,
144 ID => undef,
145 _results => undef,
146
147 #source => "_new_from_functor_and_arity: ($print_functor) ($arity)",
148 } => $class;
149 1901 lock_keys %$self;
150 1901 return $self;
151 }
152
153 0 ▶sub varnum {$VARNUM} # class method
Subroutine varnum: not called CC=1 SCAR=6.9
Pod undocumented
154 1265 ▶sub functor { shift->{functor} }
Subroutine functor: called CC=1 SCAR=0.0
Pod undocumented
155 1511 ▶sub arity { shift->{arity} }
Subroutine arity: called CC=1 SCAR=0.0
Pod undocumented
156 86 ▶sub args { shift->{args} }
Subroutine args: called CC=1 SCAR=0.0
Pod undocumented
157 52 ▶sub varid { shift->{varid} }
Subroutine varid: called CC=1 SCAR=0.0
Pod undocumented
158 262 ▶sub ref { shift->{ref} }
Subroutine ref: called CC=1 SCAR=0.0
Pod undocumented
159 668 ▶sub predicate { sprintf "%s/%d" => $_[0]->getfunctor, $_[0]->getarity }
Subroutine predicate: called CC=1 SCAR=0.0
Pod undocumented
160
161 36 ▶sub deref {
Subroutine deref: called CC=3 SCAR=11.0
Pod undocumented
162 36 my $self = shift;
163 36 ▶ while ( $self->{bound} && $self->{deref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"} and $self->{"deref"}) 19 36 55
Condition100%3/3
$self->{"bound"} and $self->{"deref"}
!ll&&!rl&&r
261019
MC/DC100%2/2
$self->{"bound"} and $self->{"deref"}
$self->{"bound"} $self->{"deref"}
Truth table100%3/3
A and B
A= $self->{"bound"}B= $self->{"deref"}
ABresult
0X0
100
111
164 19 $self = $self->{ref};
165 }
166 36 return $self;
167 }
168
169 37 ▶sub bound {
Subroutine bound: called CC=3 SCAR=12.0
Pod undocumented
170 37 my $self = shift;
171 37 ▶ while ( $self->{bound} && $self->{deref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"} and $self->{"deref"}) 0 37 37
Condition66%2/3
$self->{"bound"} and $self->{"deref"}
!ll&&!rl&&r
26110
MC/DC0%0/2
$self->{"bound"} and $self->{"deref"}
$self->{"bound"} $self->{"deref"}
Truth table66%2/3
A and B
A= $self->{"bound"}B= $self->{"deref"}
ABresult
0X0
100
111
172 0 $self = $self->{ref};
173 }
174 37 return $self->{bound};
175 }
176
177 26 ▶sub is_bound { shift->bound }
Subroutine is_bound: called CC=1 SCAR=0.0
Pod undocumented
178
179 0 ▶sub traceln {
Subroutine traceln: not called CC=2 SCAR=17.9
Pod undocumented
180 0 my ( $self, $msg ) = @_;
181 0 ▶ if ( $self->{trace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trace"}) 0 0 0
182 0 print "$msg\n";
183 }
184 }
185
186 0 ▶sub dup {
Subroutine dup: not called CC=1 SCAR=6.9
Pod undocumented
187 0 my $self = shift;
188 0 $self->new( $self->{functor}, $self->{arity} );
189 }
190
191 # bind a variable to a term
192 324 ▶sub bind {
Subroutine bind: called CC=3 SCAR=11.6
Pod undocumented
193 324 my ( $self, $term ) = @_;
194 324 ▶ return if $self eq $term;
Branch: 1 branch
BranchTrueFalseTotal
if $self eq $term 0 324 324
195 324 ▶ unless ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bound'}) { } 0 324 324
196 324 $self->{bound} = 1;
197 324 $self->{deref} = 1;
198 324 $self->{ref} = $term;
199 }
200 else {
201 0 croak( "AI::Prolog::Term->bind("
202 . $self->to_string
203 . "). Cannot bind to nonvar!" );
204 }
205 }
206
207 # unbinds a term -- i.e., resets it to a variable
208 201 ▶sub unbind {
Subroutine unbind: called CC=1 SCAR=0.0
Pod undocumented
209 201 my $self = shift;
210 201 $self->{bound} = 0;
211 201 $self->{ref} = undef;
212
213 # XXX Now possible for a bind to have had no effect so ignore safety test
214 # XXX if (bound) bound = false;
215 # XXX else IO.error("Term.unbind","Can't unbind var!");
216 }
217
218 # set specific arguments. A primitive way of constructing terms is to
219 # create them with Term(s,f) and then build up the arguments. Using the
220 # parser is much simpler
221 50 ▶sub setarg {
Subroutine setarg: called CC=3 SCAR=13.3
Pod undocumented
222 50 my ( $self, $pos, $val ) = @_;
223 50 ▶ if ( $self->{bound} && !$self->{deref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bound'} and not $self->{'deref'}) { } 50 0 50
Condition33%1/3
$self->{"bound"} and not $self->{"deref"}
!ll&&!rl&&r
0050
MC/DC0%0/2
$self->{"bound"} and not $self->{"deref"}
$self->{"bound"} not $self->{"deref"}
Truth table33%1/3
A and not(B)
A= $self->{"bound"}B= not $self->{"deref"}
ABresult
0X0
100
111
224 50 $self->{args}[$pos] = $val;
225 }
226 else {
227 0 croak( "AI::Prolog::Term->setarg($pos, "
228 . $val->to_string
229 . "). Cannot setarg on variables!" );
230 }
231 }
232
233 # retrieves an argument of a term
234 811 ▶sub getarg {
Subroutine getarg: called CC=3 SCAR=11.3
Pod undocumented
235 811 my ( $self, $pos ) = @_;
236
237 # should check if position is valid
238 811 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bound'}) { } 811 0 811
239 811 ▶ return $self->{ref}->getarg($pos) if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 78 733 811
240 733 return $self->{args}[$pos];
241 }
242 else {
243 0 croak("AI::Prolog::Term->getarg. Error -- lookup on unbound term!");
244 }
245 }
246
247 1252 ▶sub getfunctor {
Subroutine getfunctor: called CC=3 SCAR=11.0
Pod undocumented
248 1252 my $self = shift;
249 1252 ▶ return "" unless $self->{bound};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bound"} 0 1252 1252
250 1252 ▶ return $self->{ref}->getfunctor if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 92 1160 1252
251 1160 return $self->{functor};
252 }
253
254 1140 ▶sub getarity {
Subroutine getarity: called CC=3 SCAR=11.0
Pod undocumented
255 1140 my $self = shift;
256 1140 ▶ return 0 unless $self->{bound};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bound"} 0 1140 1140
257 1140 ▶ return $self->{ref}->getarity if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 67 1073 1140
258 1073 return $self->{arity};
259 }
260
261 # check whether a variable occurs in a term
262 # XXX Since a variable is not consideref to occur in itself,
263 # XXX added occurs1 and a new front end called occurs()
264 0 ▶sub occurs {
Subroutine occurs: not called CC=2 SCAR=17.9
Pod undocumented
265 0 my ( $self, $var ) = @_;
266 0 ▶ return if $self->{varid} == $var;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"varid"} == $var 0 0 0
267 0 return $self->occurs1($var);
268 }
269
270 0 ▶sub occurs1 {
Subroutine occurs1: not called CC=5 SCAR=34.0
Pod undocumented
271 0 my ( $self, $var ) = @_;
272 0 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bound'}) { } 0 0 0
273 0 ▶ return $self->ref->occurs1($var) if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 0 0 0
274 0 for my $i ( 0 .. $self->arity - 1 ) {
275 0 ▶ return 1 if $self->{args}[$i]->occurs1($var);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"args"}[$i]->occurs1($var) 0 0 0
276 }
277 }
278 else {
279 0 return $self->varid == $var;
280 }
281 }
282
283 # used internally for debugging
284 0 ▶sub _dumpit {
Subroutine _dumpit: not called CC=8 SCAR=42.8
285 0 local $^W;
286 0 my $self = shift;
287 0 ▶ my $indent = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
288 0 print( $indent . "source: ", $self->{source} );
289 0 ▶ print( $indent . "bound: ", ( $self->{bound} ? 'true' : 'false' ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bound'} ? : 0 0 0
290 0 ▶ print( $indent . "functor: ", ( $self->{functor} || 'null' ) );
Condition0%0/2
$self->{'functor'} || 'null'
l!l
00
MC/DC0%0/1
$self->{'functor'} || 'null'
$self->{'functor'}
Truth table0%0/2
A
A= $self->{'functor'}
Aresult
11
00
291 0 ▶ if ( !$self->{ref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'ref'}) { } 0 0 0
292 0 print( $indent . "ref: null" );
293 }
294 else {
295 0 print( "\n$indent" . "ref:" );
296 0 $self->{ref}->_dumpit( $indent . ' ' );
297 }
298 0 print( $indent . "arity: ", $self->{arity} );
299 0 ▶ if ( defined $self->{args}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'args'}[0]) { } 0 0 0
300 0 print( $indent. "args:" );
301 0 foreach ( @{ $self->{args} } ) {
302 0 $_->_dumpit( $indent . " " );
303 }
304 }
305 else {
306 0 print( $indent. "args: null" );
307 }
308
309 #print($indent . "args: ", scalar @{$self->{args}}) if defined $self->{args}[0];
310 0 ▶ print( $indent . "deref: ", ( $self->{deref} ? 'true' : 'false' ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'deref'} ? : 0 0 0
311 0 print( $indent . "varid: ", $self->{varid}, "\n" );
312 }
313
314 # Unification is the basic primitive operation in logic programming.
315 # $stack: the stack is used to store the address of variables which
316 # are bound by the unification. This is needed when backtracking.
317
318 476 ▶sub unify {
Subroutine unify: called CC=15 SCAR=27.9
Pod undocumented
319 476 my ( $self, $term, $stack ) = @_;
320
321 #_dumpit($self);
322 #_dumpit($term);
323
324 476 foreach ( $self, $term ) {
325 952 ▶ $_ = $_->{ref} while $_->{bound} and $_->{deref};
Branch: 1 branch
BranchTrueFalseTotal
$_->{"bound"} and $_->{"deref"} and do { $_ = $_->{"ref"} } 185 952 1137
Condition100%3/3
$_->{"bound"} and $_->{"deref"}
!ll&&!rl&&r
374578185
MC/DC100%2/2
$_->{"bound"} and $_->{"deref"}
$_->{"bound"} $_->{"deref"}
Truth table100%3/3
A and B
A= $_->{"bound"}B= $_->{"deref"}
ABresult
0X0
100
111
326 }
327
328 476 ▶ if ( $self->{bound} and $term->{bound} ) { # bound and not deref
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"} and $term->{"bound"}) 176 300 476
Condition100%3/3
$self->{"bound"} and $term->{"bound"}
!ll&&!rl&&r
24258176
MC/DC100%2/2
$self->{"bound"} and $term->{"bound"}
$self->{"bound"} $term->{"bound"}
Truth table100%3/3
A and B
A= $self->{"bound"}B= $term->{"bound"}
ABresult
0X0
100
111
329 176 ▶ if ( $self->functor eq $term->getfunctor
Branch: 1 branch
BranchTrueFalseTotal
if ($self->functor eq $term->getfunctor and $self->arity == $term->getarity) { } 164 12 176
Condition66%2/3
$self->functor eq $term->getfunctor and $self->arity == $term->getarity
!ll&&!rl&&r
120164
MC/DC50%1/2
$self->functor eq $term->getfunctor and $self->arity == $term->getarity
$self->functor eq $term->getfunctor $self->arity == $term->getarity
Truth table66%2/3
A and B
A= $self->functor eq $term->getfunctorB= $self->arity == $term->getarity
ABresult
0X0
100
111
330 && $self->arity == $term->getarity )
331 {
332 164 for my $i ( 0 .. $self->arity - 1 ) {
333 return
334 350 ▶ unless $self->{args}[$i]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"args"}[$i]->unify($term->getarg($i), $stack) 17 333 350
335 ->unify( $term->getarg($i), $stack );
336 }
337 147 return 1;
338 }
339 else {
340 12 return; # functor/arity don't match ...
341 }
342 } # at least one arg not bound ...
343 300 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"}) 58 242 300
344
345 # added missing occurcheck
346 58 ▶ if ( $self->occurcheck ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->occurcheck) 0 58 58
347 0 ▶ if ( $self->occurs( $term->varid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->occurs($term->varid)) 0 0 0
348 0 return;
349 }
350 }
351 58 $term->bind($self);
352 58 push @{$stack} => $term; # side-effect -- setting stack vars
353 58 return 1;
354 }
355
356 # do occurcheck if turned on
357 242 ▶ return if $self->occurcheck && $term->occurs( $self->varid );
Branch: 1 branch
BranchTrueFalseTotal
if $self->occurcheck and $term->occurs($self->varid) 0 242 242
Condition33%1/3
$self->occurcheck and $term->occurs($self->varid)
!ll&&!rl&&r
24200
MC/DC0%0/2
$self->occurcheck and $term->occurs($self->varid)
$self->occurcheck $term->occurs($self->varid)
Truth table33%1/3
A and B
A= $self->occurcheckB= $term->occurs($self->varid)
ABresult
0X0
100
111
358 242 $self->bind($term);
359 242 push @{$stack} => $self; # save for backtracking
360 242 return 1;
361 }
362
363 # refresh creates new variables. If the variables already exist
364 # in its arguments then they are used. This is used when parsing
365 # a clause so that variables throughout the clause are shared.
366 # Includes a copy operation.
367
368 1183 ▶sub refresh {
Subroutine refresh: called CC=6 SCAR=17.9
Pod undocumented
369 1183 my ( $self, $term_aref ) = @_;
370 1183 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"}) 745 438 1183
371 745 ▶ if ( $self->{deref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'deref'}) { } 437 308 745
372 437 return $self->{ref}->refresh($term_aref);
373 }
374 else {
375 308 ▶ if ( 0 == $self->{arity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $self->{'arity'}) { } 58 250 308
376 58 return $self;
377 }
378 else {
379 my $term = ( CORE::ref $self)
380 ->_new_from_functor_and_arity( $self->{functor},
381 250 $self->{arity} );
382 250 for my $i ( 0 .. $self->{arity} - 1 ) {
383 $term->{args}[$i]
384 562 = $self->{args}[$i]->refresh($term_aref);
385 }
386 250 return $term;
387 }
388 }
389 }
390
391 # else unbound
392 438 ▶ unless ( $term_aref->[ $self->{varid} ] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$term_aref[$self->{"varid"}]) 272 166 438
393 272 $term_aref->[ $self->{varid} ] = $self->new;
394 }
395 438 return $term_aref->[ $self->{varid} ];
396 }
397
398 15 ▶sub to_data {
Subroutine to_data: called CC=1 SCAR=0.0
Pod undocumented
399 15 my $self = shift;
400 15 $self->{_results} = {};
401
402 # @results is the full results, if we ever need it
403 15 my @results = $self->_to_data($self);
404 15 return AsObject->new( $self->{_results} ), \@results;
405 }
406
407 353 ▶sub _to_data {
Subroutine _to_data: called CC=16 SCAR=28.0
408 353 my ( $self, $parent ) = @_;
409 353 ▶ if ( defined $self->{varname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"varname"}) 26 327 353
410
411 # XXX here's where the [HEAD|TAIL] bug is. The engine works fine,
412 # but we can't bind TAIL to a result object and are forced to
413 # switch to raw_results.
414 26 my $varname = delete $self->{varname};
415 26 ( $parent->{_results}{$varname} ) = $self->_to_data($parent);
416 26 $self->{varname} = $varname;
417 }
418 353 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"}) 349 4 353
419 349 my $functor = $self->functor;
420 349 my $arity = $self->arity;
421 349 ▶ return $self->ref->_to_data($parent) if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 140 209 349
422 209 ▶ return [] if NULL eq $functor && !$arity;
Branch: 1 branch
BranchTrueFalseTotal
if "null" eq $functor and not $arity 8 201 209
Condition66%2/3
"null" eq $functor and not $arity
!ll&&!rl&&r
20108
MC/DC50%1/2
"null" eq $functor and not $arity
"null" eq $functor not $arity
Truth table66%2/3
A and not(B)
A= "null" eq $functorB= not $arity
ABresult
0X0
100
111
423 201 ▶ if ( "cons" eq $functor && 2 == $arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('cons' eq $functor and 2 == $arity) { } 46 155 201
Condition66%2/3
"cons" eq $functor and 2 == $arity
!ll&&!rl&&r
155046
MC/DC50%1/2
"cons" eq $functor and 2 == $arity
"cons" eq $functor 2 == $arity
Truth table66%2/3
A and B
A= "cons" eq $functorB= 2 == $arity
ABresult
0X0
100
111
424 46 my @result = $self->{args}[0]->_to_data($parent);
425 46 my $term = $self->{args}[1];
426
427 46 ▶ while ( "cons" eq $term->getfunctor && 2 == $term->getarity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("cons" eq $term->getfunctor and 2 == $term->getarity) 86 46 132
Condition66%2/3
"cons" eq $term->getfunctor and 2 == $term->getarity
!ll&&!rl&&r
46086
MC/DC50%1/2
"cons" eq $term->getfunctor and 2 == $term->getarity
"cons" eq $term->getfunctor 2 == $term->getarity
Truth table66%2/3
A and B
A= "cons" eq $term->getfunctorB= 2 == $term->getarity
ABresult
0X0
100
111
428 86 ▶ if ( $term->{varname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->{'varname'}) { } 1 85 86
429 1 push @result => $term->_to_data($parent);
430 } else {
431 85 push @result => $term->getarg(0)->_to_data($parent);
432 }
433 86 $term = $term->getarg(1);
434 }
435
436 # XXX Not really sure about this one
437 46 ▶ push @result => $term->_to_data($parent)
Branch: 1 branch
BranchTrueFalseTotal
unless "null" eq $term->getfunctor and not $term->getarity 0 46 46
Condition33%1/3
"null" eq $term->getfunctor and not $term->getarity
!ll&&!rl&&r
0046
MC/DC0%0/2
"null" eq $term->getfunctor and not $term->getarity
"null" eq $term->getfunctor not $term->getarity
Truth table33%1/3
A and not(B)
A= "null" eq $term->getfunctorB= not $term->getarity
ABresult
0X0
100
111
438 unless NULL eq $term->getfunctor && !$term->getarity;
439
440 # ? "]"
441 # : "|" . $term->_to_data($parent) . "]";
442 46 return \@result;
443 }
444 else {
445 155 my @results = $self->functor;
446 155 ▶ if ( $self->arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->arity) 15 140 155
447
448 #push @results => [];
449 15 my $arity = $self->arity;
450 15 my @args = @{ $self->args };
451 15 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 15 0 15
452 15 for my $i ( 0 .. $arity - 1 ) {
453 40 push @results => $args[$i]->_to_data($parent);
454 }
455
456 # I have no idea what the following line was doing.
457 #push @results => $args[$arity - 1]->_to_data($parent)
458 }
459 }
460 155 return @results;
461 }
462 } # else unbound;
463 4 return undef;
464 }
465
466 13 my %varname_for;
467 13 my $varname = 'A';
468
469 51 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
470 51 require Data::Dumper;
471 51 my $self = shift;
472 51 return $self->_to_string(@_);
473 }
474
475 380 ▶sub _to_string {
Subroutine _to_string: called CC=19 SCAR=30.0
476 380 my ( $self, $extended ) = @_;
477 380 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bound"}) 359 21 380
478 359 my $functor = $self->functor;
479 359 my $arity = $self->arity;
480 359 my $prettyprint = $self->prettyprint;
481 359 ▶ return $self->ref->_to_string($extended) if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 113 246 359
482 246 ▶ return "[]" if NULL eq $functor && !$arity && $prettyprint;
Branch: 1 branch
BranchTrueFalseTotal
if "null" eq $functor and not $arity and $prettyprint 4 242 246
Condition66%4/6
"null" eq $functor and not $arity and $prettyprint
!ll&&!rl&&r
24204
"null" eq $functor and not $arity
!ll&&!rl&&r
24204
MC/DC33%1/3
"null" eq $functor and not $arity and $prettyprint
"null" eq $functor not $arity $prettyprint
Truth table50%2/4
A and not(B) and C
A= "null" eq $functorB= not $arityC= $prettyprint
ABCresult
0XX0
10X0
1100
1111
483 242 my $string;
484 242 ▶ if ( "cons" eq $functor && 2 == $arity && $prettyprint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('cons' eq $functor and 2 == $arity and $prettyprint) { } 29 213 242
Condition66%4/6
"cons" eq $functor and 2 == $arity and $prettyprint
!ll&&!rl&&r
213029
"cons" eq $functor and 2 == $arity
!ll&&!rl&&r
213029
MC/DC33%1/3
"cons" eq $functor and 2 == $arity and $prettyprint
"cons" eq $functor 2 == $arity $prettyprint
Truth table50%2/4
A and B and C
A= "cons" eq $functorB= 2 == $arityC= $prettyprint
ABCresult
0XX0
10X0
1100
1111
485 29 $string = "[" . $self->{args}[0]->_to_string;
486 29 my $term = $self->{args}[1];
487
488 29 ▶ while ( "cons" eq $term->getfunctor && 2 == $term->getarity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("cons" eq $term->getfunctor and 2 == $term->getarity) 59 29 88
Condition66%2/3
"cons" eq $term->getfunctor and 2 == $term->getarity
!ll&&!rl&&r
29059
MC/DC50%1/2
"cons" eq $term->getfunctor and 2 == $term->getarity
"cons" eq $term->getfunctor 2 == $term->getarity
Truth table66%2/3
A and B
A= "cons" eq $term->getfunctorB= 2 == $term->getarity
ABresult
0X0
100
111
489 59 $string .= "," . $term->getarg(0)->_to_string;
490 59 $term = $term->getarg(1);
491 }
492
493 $string .=
494 29 ▶ ( NULL eq $term->getfunctor && !$term->getarity )
Branch: 1 branch
BranchTrueFalseTotal
'null' eq $term->getfunctor && !$term->getarity ? : 29 0 29
Condition33%1/3
"null" eq $term->getfunctor and not $term->getarity
!ll&&!rl&&r
0029
MC/DC0%0/2
"null" eq $term->getfunctor and not $term->getarity
"null" eq $term->getfunctor not $term->getarity
Truth table33%1/3
A and not(B)
A= "null" eq $term->getfunctorB= not $term->getarity
ABresult
0X0
100
111
495 ? "]"
496 : "|" . $term->_to_string . "]";
497 29 return "$string";
498 }
499 else {
500 213 $string = $self->functor;
501 213 ▶ if ( $self->arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->arity) 61 152 213
502 61 $string .= "(";
503 61 ▶ if ( $self->arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->arity) 61 0 61
504 61 local $Data::Dumper::Terse = 1; # don't use $var1
505 61 local $Data::Dumper::Indent = 0; # no newline
506 my @args = map {
507 128 my $string = $_->_to_string;
508 128 ▶ $string =~ /\s/
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /\s/ && !$_->arity ? : 3 125 128
Condition100%3/3
$string =~ /\s/ and not $_->arity
!ll&&!rl&&r
110153
MC/DC100%2/2
$string =~ /\s/ and not $_->arity
$string =~ /\s/ not $_->arity
Truth table100%3/3
A and not(B)
A= $string =~ /\s/B= not $_->arity
ABresult
0X0
100
111
509 && !$_->arity
510 ? Data::Dumper::Dumper($string)
511 : $string;
512 61 } @{ $self->args };
513 61 $string .= join ", " => @args;
514 }
515 61 $string .= ")";
516 }
517 }
518 213 return $string;
519 } # else unbound;
520 # return "_" . $self->varid;
521 21 ▶ my $var = $self->{varname} || $varname_for{ $self->varid } || $varname++;
Condition66%4/6
$self->{'varname'} || $varname_for{$self->varid} || $varname++
l!l&&r!l&&!r
1380
$self->{"varname"} or $varname_for{$self->varid}
l!l&&r!l&&!r
0138
MC/DC0%0/3
$self->{'varname'} || $varname_for{$self->varid} || $varname++
$self->{"varname"} $varname_for{$self->varid} $varname++
Truth table50%2/4
A or B || C
A= $self->{"varname"}B= $varname_for{$self->varid}C= $varname++
ABCresult
1XX1
01X1
0011
0000
522 21 $varname_for{ $self->varid } = $var;
523 21 return $var;
524 }
525
526 # ----------------------------------------------------------
527 # Copy a term to put in the database
528 # - with new variables (freshly renumbered)
529 # ----------------------------------------------------------
530
531 # XXX XProlog
532 13 my %CVDICT;
533 my $CVN;
534
535 0 ▶sub clean_up {
Subroutine clean_up: not called CC=1 SCAR=6.9
Pod undocumented
536 0 my $self = shift;
537 0 %CVDICT = ();
538 0 $CVN = 0;
539 0 return $self->_clean_up;
540 }
541
542 0 ▶sub _clean_up {
Subroutine _clean_up: not called CC=7 SCAR=40.3
543 0 my $self = shift;
544 0 my $term;
545 0 ▶ if ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bound'}) { } 0 0 0
546 0 ▶ if ( $self->{deref} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'deref'}) { } 0 0 0
elsif (defined $self->{'arity'} and 0 == $self->{'arity'}) { } 0 0 0
547 0 return $self->{ref}->_clean_up;
548 }
549 ▶ elsif ( defined $self->{arity} && 0 == $self->{arity} ) {
Condition0%0/3
defined $self->{"arity"} and 0 == $self->{"arity"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"arity"} and 0 == $self->{"arity"}
defined $self->{"arity"} 0 == $self->{"arity"}
Truth table0%0/3
A and B
A= defined $self->{"arity"}B= 0 == $self->{"arity"}
ABresult
0X0
100
111
550 0 return $self;
551 }
552 else {
553 0 $term = $self->dup;
554 0 for my $i ( 0 .. $self->{arity} - 1 ) {
555 0 $term->{args}[$i] = $self->{args}[$i]->_clean_up;
556 }
557 }
558 }
559 else { # unbound
560 0 $term = $CVDICT{$self};
561 0 ▶ unless ($term) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term) 0 0 0
562 0 $term = $self->new( $CVN++ );
563 0 $CVDICT{$self} = $term; # XXX Should this be $self->to_string?
564 }
565 }
566 0 return $term;
567 }
568
569 # From XProlog
570 46 ▶sub value {
Subroutine value: called CC=12 SCAR=25.5
Pod undocumented
571
572 # int i, res = 0;
573 46 my $self = shift;
574 46 my ( $i, $res ) = ( 0, 0 );
575
576 46 ▶ unless ( $self->{bound} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bound"}) 1 45 46
577 1 my $term = $self->to_string;
578 1 croak("Tried to to get value of unbound term ($term)");
579 }
580 45 ▶ return $self->{ref}->value if $self->{deref};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"deref"} 24 21 45
581 21 my $functor = $self->getfunctor;
582 21 my $arity = $self->getarity;
583 21 ▶ if ( 'rnd' eq $functor && 1 == $arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("rnd" eq $functor and 1 == $arity) 0 21 21
Condition33%1/3
"rnd" eq $functor and 1 == $arity
!ll&&!rl&&r
2100
MC/DC0%0/2
"rnd" eq $functor and 1 == $arity
"rnd" eq $functor 1 == $arity
Truth table33%1/3
A and B
A= "rnd" eq $functorB= 1 == $arity
ABresult
0X0
100
111
584
585 # implement rand
586 }
587 21 ▶ if ( $arity < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arity < 2) 0 21 21
588 0 my $term = $self->to_string;
589 0 croak("Term ($term) is not binary");
590 }
591 21 my $arg0 = $self->{args}[0]->value;
592 21 my $arg1 = $self->{args}[1]->value;
593
594 21 ▶ return $arg0 + $arg1 if 'plus' eq $functor;
Branch: 1 branch
BranchTrueFalseTotal
if "plus" eq $functor 4 17 21
595 17 ▶ return $arg0 - $arg1 if 'minus' eq $functor;
Branch: 1 branch
BranchTrueFalseTotal
if "minus" eq $functor 6 11 17
596 11 ▶ return $arg0 * $arg1 if 'mult' eq $functor;
Branch: 1 branch
BranchTrueFalseTotal
if "mult" eq $functor 6 5 11
597 5 ▶ return $arg0 / $arg1 if 'div' eq $functor;
Branch: 1 branch
BranchTrueFalseTotal
if "div" eq $functor 1 4 5
598 4 ▶ return $arg0 % $arg1 if 'mod' eq $functor;
Branch: 1 branch
BranchTrueFalseTotal
if "mod" eq $functor 1 3 4
599 3 ▶ return $arg0**$arg1 if 'pow' eq $functor;
Branch: 1 branch
BranchTrueFalseTotal
if "pow" eq $functor 3 0 3
600 0 croak("Unknown operator ($functor)");
601 }
602
603 13 1;
604
605 __END__
« Prolog/KnowledgeBase.pm ↑ Summary Prolog/Engine.pm »