blib/lib/Logic/Relational/Term.pm

filter: Statementstmt 90.3% 47 / 52 Branchbran 81.2% 13 / 16 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 84.7% 89 / 105 CC 14 SCAR 14.1
CC
14
Cov
85%
_val_to_string
7.6
_freshen_val
7.0
new
3.2
CRAP 4.1
« blib/lib/Logic/Relational/Goal/All.pm ↑ Summary lib/Logic/Relational/Clause.pm »
1 package Logic::Relational::Term;
2  
3 34 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Logic::Relational::Term - Represents a compound term in the logic engine.
9  
10 =head1 SYNOPSIS
11  
12 use Logic::Relational::Term;
13 my $term = Logic::Relational::Term->new(
14 functor => 'owns',
15 args => [$perp, $stuff]
16 );
17  
18 =head1 DESCRIPTION
19  
20 C<Logic::Relational::Term> represents a compound term containing a functor name
21 and an array of arguments. Arguments can be other terms, variables, or constants.
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 Constructor. Takes C<functor> and C<args> (arrayref).
28  
29 =head2 functor
30  
31 Returns the functor string.
32  
33 =head2 args
34  
35 Returns the arrayref of arguments.
36  
37 =head2 arity
38  
39 Returns the number of arguments.
40  
41 =head2 arg
42  
43 Returns the argument at the specified 0-based index.
44  
45 =head2 as_string
46  
47 Returns a formatted string representation of the compound term, e.g. C<owns(X, gold)>.
48  
49 =head2 freshen
50  
51 Recursively freshens all variables in its arguments using the provided variable
52 mapping, returning a new C<Logic::Relational::Term> object.
53  
54 =cut
55  
56 34 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2597 ▶sub new ( $class, %args ) {
Subroutine new: called CC=3 SCAR=11.7
Pod documented
59 2597 ▶ my $functor = $args{functor} // croak "functor is required";
Condition50%1/2
$args{"functor"} // croak("functor is required")
l!l
25970
MC/DC0%0/1
$args{"functor"} // croak("functor is required")
$args{"functor"}
Truth table50%1/2
A
A= $args{"functor"}
Aresult
11
00
60 2597 ▶ my $args = $args{args} // [];
Condition50%1/2
$args{"args"} // []
l!l
25970
MC/DC0%0/1
$args{"args"} // []
$args{"args"}
Truth table50%1/2
A
A= $args{"args"}
Aresult
11
00
61 2597 return bless { functor => $functor, args => $args }, $class;
62 }
63  
64 4044 ▶sub functor ($self) {
Subroutine functor: called CC=1 SCAR=0.0
Pod documented
65 4044 return $self->{functor};
66 }
67  
68 1732 ▶sub args ($self) {
Subroutine args: called CC=1 SCAR=0.0
Pod documented
69 1732 return $self->{args};
70 }
71  
72 3216 ▶sub arity ($self) {
Subroutine arity: called CC=1 SCAR=0.0
Pod documented
73 3216 return scalar @{ $self->{args} };
74 }
75  
76 2413 ▶sub arg ( $self, $i ) {
Subroutine arg: called CC=1 SCAR=0.0
Pod documented
77 2413 return $self->{args}[$i];
78 }
79  
80 2 ▶sub as_string ($self) {
Subroutine as_string: called CC=2 SCAR=7.0
Pod documented
81 2 my $functor = $self->{functor};
82 2 my $arity = $self->arity;
83 2 ▶ return $functor if $arity == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $arity == 0 0 2 2
84  
85 2 my @arg_strs = map { _val_to_string($_) } @{ $self->{args} };
86  
87 2 return "$functor(" . join( ', ', @arg_strs ) . ")";
88 }
89  
90 784 ▶sub freshen ( $self, $var_map ) {
Subroutine freshen: called CC=1 SCAR=0.0
Pod documented
91 784 my @fresh_args = map { _freshen_val( $_, $var_map ) } @{ $self->{args} };
92  
93 return ref($self)->new(
94 functor => $self->{functor},
95 784 args => \@fresh_args,
96 );
97 }
98  
99 34 ▶use builtin 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 34 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 3 ▶sub _val_to_string ($val) {
Subroutine _val_to_string: called CC=5 SCAR=20.3
103 3 ▶ if ( ref($val) eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 3 3
elsif (ref $val eq 'HASH') { } 0 3 3
elsif (builtin::blessed($val) and $val->can('as_string')) { } 2 2 4
104 0 return "[" . join( ', ', map { _val_to_string($_) } @$val ) . "]";
105 }
106 elsif ( ref($val) eq 'HASH' ) {
107 0 my @pairs = map { "$_ => " . _val_to_string($val->{$_}) } sort keys %$val;
108 0 return "{" . join(', ', @pairs) . "}";
109 }
110 ▶ elsif ( blessed($val) && $val->can('as_string') ) {
Condition66%2/3
builtin::blessed($val) and $val->can("as_string")
!ll&&!rl&&r
102
MC/DC50%1/2
builtin::blessed($val) and $val->can("as_string")
builtin::blessed($val) $val->can("as_string")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->can("as_string")
ABresult
0X0
100
111
111 2 return $val->as_string;
112 }
113 1 return "$val";
114 }
115  
116 1661 ▶sub _freshen_val ( $val, $var_map ) {
Subroutine _freshen_val: called CC=7 SCAR=19.5
117 1661 ▶ if ( ref($val) eq 'ARRAY' ) { ## no critic (ProhibitCascadingIfElse)
Branch: 4 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 82 1579 1661
elsif (ref $val eq 'HASH') { } 1 1578 1579
elsif (ref $val eq 'Logic::Relational::ArrayRest') { } 48 1530 1578
elsif (builtin::blessed($val) and $val->can('freshen')) { } 438 2184 2622
118 82 return [ map { _freshen_val( $_, $var_map ) } @$val ];
119 }
120 elsif ( ref($val) eq 'HASH' ) {
121 1 my %fresh_hash;
122 1 for my $k ( keys %$val ) {
123 2 $fresh_hash{$k} = _freshen_val( $val->{$k}, $var_map );
124 }
125 1 return \%fresh_hash;
126 }
127 elsif ( ref($val) eq 'Logic::Relational::ArrayRest' ) {
128 48 return ref($val)->new( _freshen_val( $val->term, $var_map ) );
129 }
130 ▶ elsif ( blessed($val) && $val->can('freshen') ) {
Condition66%2/3
builtin::blessed($val) and $val->can("freshen")
!ll&&!rl&&r
10920438
MC/DC50%1/2
builtin::blessed($val) and $val->can("freshen")
builtin::blessed($val) $val->can("freshen")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->can("freshen")
ABresult
0X0
100
111
131 438 return $val->freshen($var_map);
132 }
133 1092 return $val;
134 }
135  
136 34 1;
« blib/lib/Logic/Relational/Goal/All.pm ↑ Summary lib/Logic/Relational/Clause.pm »