blib/lib/Logic/Relational/DSL.pm

filter: Statementstmt 72.4% 71 / 98 Branchbran 50.0% 11 / 22 Conditioncond 19.0% 4 / 21 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 71.8% 23 / 32 Podpod 0.0% 0 / 23 totaltotal 52.3% 110 / 210 CC 21 SCAR 18.4
CC
21
Cov
61%
save_snapshot
20.4
load_snapshot
20.4
freshen_val
8.1
CRAP 6.3
« blib/lib/Logic/Relational/Goal/Is.pm ↑ Summary lib/Logic/Relational/ArrayRest.pm »
1 package Logic::Relational::DSL;
2  
3 30 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Logic::Relational::DSL - Domain Specific Language helpers for relational logic.
9  
10 =head1 SYNOPSIS
11  
12 use Logic::Relational::DSL qw(variable call all not_goal);
13 my $perp = variable('perp');
14  
15 =head1 DESCRIPTION
16  
17 C<Logic::Relational::DSL> exports functional constructor helpers to build
18 terms, variables, goals, and run unification directly in standard Perl.
19  
20 =cut
21  
22 30 our @EXPORT_OK = qw(
23 variable
24 atom
25 term
26 unify
27 call
28 all
29 any_goal
30 not_goal
31 unify_goal
32 true_goal
33 fail_goal
34 guard
35 rest
36 slurp
37 freshen_val
38 in_domain
39 all_different
40 label
41 is_goal
42 identical
43 constraint_fd
44 save_snapshot
45 load_snapshot
46 );
47 30 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 35 ▶sub import ( $class, @args ) {
Subroutine import: called CC=1 SCAR=0.0
50 30 ▶ no warnings 'prototype', 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 return $class->export_to_level( 1, $class, @args );
52 }
53  
54 30 require Logic::Relational::Variable;
55 30 require Logic::Relational::Atom;
56 30 require Logic::Relational::Term;
57 30 require Logic::Relational::Unifier;
58 30 require Logic::Relational::Goal::True;
59 30 require Logic::Relational::Goal::Fail;
60 30 require Logic::Relational::Goal::Unify;
61 30 require Logic::Relational::Goal::Call;
62 30 require Logic::Relational::Goal::All;
63 30 require Logic::Relational::Goal::Any;
64 30 require Logic::Relational::Goal::Not;
65 30 require Logic::Relational::Goal::Guard;
66 30 require Logic::Relational::Goal::Is;
67 30 require Logic::Relational::Goal::Identical;
68 30 require Logic::Relational::Goal::Domain;
69 30 require Logic::Relational::Goal::ConstraintFD;
70 30 require Logic::Relational::Goal::AllDifferent;
71 30 require Logic::Relational::Goal::Label;
72  
73 897 ▶sub variable ($name) {
Subroutine variable: called CC=1 SCAR=0.0
Pod undocumented
74 897 return Logic::Relational::Variable->new( name => $name );
75 }
76  
77 0 ▶sub atom ($value) {
Subroutine atom: not called CC=1 SCAR=6.9
Pod undocumented
78 0 return Logic::Relational::Atom->new($value);
79 }
80  
81 0 ▶sub term ( $functor, @args ) {
Subroutine term: not called CC=1 SCAR=6.9
Pod undocumented
82 0 return Logic::Relational::Term->new( functor => $functor, args => \@args );
83 }
84  
85 20 ▶sub unify ( $left, $right, $subst = undef ) {
Subroutine unify: called CC=3 SCAR=11.0
Pod undocumented
86 20 ▶ if ( !$subst ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subst) 18 2 20
87 18 require Logic::Relational::Substitution;
88 18 $subst = Logic::Relational::Substitution->new;
89 }
90 20 return Logic::Relational::Unifier::unify( $left, $right, $subst );
91 }
92  
93 642 ▶sub call ( $name, @args ) {
Subroutine call: called CC=1 SCAR=0.0
Pod undocumented
94 642 return Logic::Relational::Goal::Call->new( $name, \@args );
95 }
96  
97 103 ▶sub all (@goals) {
Subroutine all: called CC=1 SCAR=0.0
Pod undocumented
98 103 return Logic::Relational::Goal::All->new( \@goals );
99 }
100  
101 0 ▶sub any_goal (@goals) {
Subroutine any_goal: not called CC=1 SCAR=6.9
Pod undocumented
102 0 return Logic::Relational::Goal::Any->new( \@goals );
103 }
104  
105 3 ▶sub not_goal ($goal) {
Subroutine not_goal: called CC=1 SCAR=0.0
Pod undocumented
106 3 return Logic::Relational::Goal::Not->new($goal);
107 }
108  
109 0 ▶sub unify_goal ( $left, $right ) {
Subroutine unify_goal: not called CC=1 SCAR=6.9
Pod undocumented
110 0 return Logic::Relational::Goal::Unify->new( $left, $right );
111 }
112  
113 0 ▶sub true_goal () {
Subroutine true_goal: not called CC=1 SCAR=6.9
Pod undocumented
114 0 return Logic::Relational::Goal::True->new;
115 }
116  
117 0 ▶sub fail_goal () {
Subroutine fail_goal: not called CC=1 SCAR=6.9
Pod undocumented
118 0 return Logic::Relational::Goal::Fail->new;
119 }
120  
121 92 ▶sub guard ( $vars, $code ) {
Subroutine guard: called CC=1 SCAR=0.0
Pod undocumented
122 92 return Logic::Relational::Goal::Guard->new( $vars, $code );
123 }
124  
125 275 ▶sub rest ($term) {
Subroutine rest: called CC=1 SCAR=0.0
Pod undocumented
126 275 require Logic::Relational::ArrayRest;
127 275 return Logic::Relational::ArrayRest->new($term);
128 }
129  
130 5 ▶sub slurp ($term) {
Subroutine slurp: called CC=1 SCAR=0.0
Pod undocumented
131 5 require Logic::Relational::ArrayRest;
132 5 return Logic::Relational::ArrayRest->new($term);
133 }
134  
135 30 ▶use builtin 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 30 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138 505 ▶sub freshen_val ( $val, $var_map ) {
Subroutine freshen_val: called CC=7 SCAR=21.0
Pod undocumented
139 505 ▶ if ( ref($val) eq 'ARRAY' ) { ## no critic (ProhibitCascadingIfElse)
Branch: 4 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 6 499 505
elsif (ref $val eq 'HASH') { } 0 499 499
elsif (ref $val eq 'Logic::Relational::ArrayRest') { } 4 495 499
elsif (builtin::blessed($val) and $val->can('freshen')) { } 483 24 507
140 6 return [ map { freshen_val( $_, $var_map ) } @$val ];
141 }
142 elsif ( ref($val) eq 'HASH' ) {
143 0 my %fresh_hash;
144 0 for my $k ( keys %$val ) {
145 0 $fresh_hash{$k} = freshen_val( $val->{$k}, $var_map );
146 }
147 0 return \%fresh_hash;
148 }
149 elsif ( ref($val) eq 'Logic::Relational::ArrayRest' ) {
150 4 return ref($val)->new( freshen_val( $val->term, $var_map ) );
151 }
152 ▶ elsif ( blessed($val) && $val->can('freshen') ) {
Condition66%2/3
builtin::blessed($val) and $val->can("freshen")
!ll&&!rl&&r
120483
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
153 483 return $val->freshen($var_map);
154 }
155 12 return $val;
156 }
157  
158 8 ▶sub in_domain ( $var, $min, $max ) {
Subroutine in_domain: called CC=1 SCAR=0.0
Pod undocumented
159 8 return Logic::Relational::Goal::Domain->new( $var, $min, $max );
160 }
161  
162 2 ▶sub all_different (@vars) {
Subroutine all_different: called CC=3 SCAR=14.2
Pod undocumented
163 2 ▶ if ( @vars == 1 && ref( $vars[0] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vars == 1 and ref $vars[0] eq "ARRAY") 0 2 2
Condition33%1/3
@vars == 1 and ref $vars[0] eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
@vars == 1 and ref $vars[0] eq "ARRAY"
@vars == 1 ref $vars[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @vars == 1B= ref $vars[0] eq "ARRAY"
ABresult
0X0
100
111
164 0 return Logic::Relational::Goal::AllDifferent->new( $vars[0] );
165 }
166 2 return Logic::Relational::Goal::AllDifferent->new( \@vars );
167 }
168  
169 3 ▶sub label (@vars) {
Subroutine label: called CC=3 SCAR=14.2
Pod undocumented
170 3 ▶ if ( @vars == 1 && ref( $vars[0] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vars == 1 and ref $vars[0] eq "ARRAY") 3 0 3
Condition33%1/3
@vars == 1 and ref $vars[0] eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
@vars == 1 and ref $vars[0] eq "ARRAY"
@vars == 1 ref $vars[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @vars == 1B= ref $vars[0] eq "ARRAY"
ABresult
0X0
100
111
171 3 return Logic::Relational::Goal::Label->new( $vars[0] );
172 }
173 0 return Logic::Relational::Goal::Label->new( \@vars );
174 }
175  
176 65 ▶sub is_goal ( $target_var, $vars, $code ) {
Subroutine is_goal: called CC=1 SCAR=0.0
Pod undocumented
177 65 return Logic::Relational::Goal::Is->new( $target_var, $vars, $code );
178 }
179  
180 3 ▶sub identical ( $t1, $t2 ) {
Subroutine identical: called CC=1 SCAR=0.0
Pod undocumented
181 3 return Logic::Relational::Goal::Identical->new( $t1, $t2 );
182 }
183  
184 0 ▶sub constraint_fd ( $op, $vars, $eval_sub ) {
Subroutine constraint_fd: not called CC=1 SCAR=6.9
Pod undocumented
185 0 return Logic::Relational::Goal::ConstraintFD->new( $op, $vars, $eval_sub );
186 }
187  
188 0 ▶sub save_snapshot ( $arg1, @rest ) {
Subroutine save_snapshot: not called CC=5 SCAR=30.1
Pod undocumented
189 0 ▶ if ( blessed($arg1)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($arg1) and $arg1->isa("Logic::Relational::Program")) 0 0 0
Condition0%0/3
builtin::blessed($arg1) and $arg1->isa("Logic::Relational::Program")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arg1) and $arg1->isa("Logic::Relational::Program")
builtin::blessed($arg1) $arg1->isa("Logic::Relational::Program")
Truth table0%0/3
A and B
A= builtin::blessed($arg1)B= $arg1->isa("Logic::Relational::Program")
ABresult
0X0
100
111
190 && $arg1->isa('Logic::Relational::Program') )
191 {
192 0 return $arg1->save_snapshot(@rest);
193 }
194  
195 0 my ($caller_pkg) = caller;
196 30 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
197 0 ▶ my $prog = ${"${caller_pkg}::PROGRAM"} // $main::PROGRAM;
Condition0%0/3
${"${caller_pkg}::PROGRAM";} // $main::PROGRAM
l!l&&r!l&&!r
000
MC/DC0%0/2
${"${caller_pkg}::PROGRAM";} // $main::PROGRAM
${"${caller_pkg}::PROGRAM";} $main::PROGRAM
Truth table0%0/3
A // B
A= ${"${caller_pkg}::PROGRAM";}B= $main::PROGRAM
ABresult
1X1
011
000
198 0 ▶ Carp::croak "No active \$PROGRAM object found for save_snapshot"
Branch: 1 branch
BranchTrueFalseTotal
unless $prog 0 0 0
199 unless $prog;
200 0 return $prog->save_snapshot( $arg1, @rest );
201 }
202  
203 0 ▶sub load_snapshot ( $arg1, @rest ) {
Subroutine load_snapshot: not called CC=5 SCAR=30.1
Pod undocumented
204 0 ▶ if ( blessed($arg1)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($arg1) and $arg1->isa("Logic::Relational::Program")) 0 0 0
Condition0%0/3
builtin::blessed($arg1) and $arg1->isa("Logic::Relational::Program")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arg1) and $arg1->isa("Logic::Relational::Program")
builtin::blessed($arg1) $arg1->isa("Logic::Relational::Program")
Truth table0%0/3
A and B
A= builtin::blessed($arg1)B= $arg1->isa("Logic::Relational::Program")
ABresult
0X0
100
111
205 && $arg1->isa('Logic::Relational::Program') )
206 {
207 0 return $arg1->load_snapshot(@rest);
208 }
209  
210 0 my ($caller_pkg) = caller;
211 30 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
212 0 ▶ my $prog = ${"${caller_pkg}::PROGRAM"} // $main::PROGRAM;
Condition0%0/3
${"${caller_pkg}::PROGRAM";} // $main::PROGRAM
l!l&&r!l&&!r
000
MC/DC0%0/2
${"${caller_pkg}::PROGRAM";} // $main::PROGRAM
${"${caller_pkg}::PROGRAM";} $main::PROGRAM
Truth table0%0/3
A // B
A= ${"${caller_pkg}::PROGRAM";}B= $main::PROGRAM
ABresult
1X1
011
000
213 0 ▶ Carp::croak "No active \$PROGRAM object found for load_snapshot"
Branch: 1 branch
BranchTrueFalseTotal
unless $prog 0 0 0
214 unless $prog;
215 0 return $prog->load_snapshot( $arg1, @rest );
216 }
217  
218 30 1;
« blib/lib/Logic/Relational/Goal/Is.pm ↑ Summary lib/Logic/Relational/ArrayRest.pm »