blib/lib/Logic/Relational/Goal/Label.pm

filter: Statementstmt 91.4% 43 / 47 Branchbran 71.4% 10 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 85.7% 6 / 7 Podpod 75.0% 3 / 4 totaltotal 80.4% 66 / 82 CC 13 SCAR 21.5
CC
13
Cov
84%
is_ground
12.0
expand
11.2
BEGIN
1.0
CRAP 8.6
« lib/Logic/Relational/Substitution.pm ↑ Summary blib/lib/Logic/Relational/Goal/AllDifferent.pm »
1 package Logic::Relational::Goal::Label;
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 30 ▶use parent 'Logic::Relational::Goal';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Logic::Relational::Goal::Label - Backtracking labeling goal for domain variables.
10  
11 =cut
12  
13 17 ▶sub new ( $class, $vars ) {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 17 return bless { vars => $vars }, $class;
15 }
16  
17 102 ▶sub expand ( $self, %args ) {
Subroutine expand: called CC=11 SCAR=24.1
Pod documented
18 102 my $subst = $args{substitution};
19 102 my $remaining = $args{remaining};
20 102 my $state = $args{state};
21  
22 102 my @vars = @{ $self->{vars} };
23  
24 # Find the unbound domain variable with the smallest domain (First-Fail heuristic)
25 102 my $selected_var;
26 my $selected_domain;
27 102 my $min_size;
28  
29 102 for my $var (@vars) {
30 291 my $val = $subst->walk($var);
31 291 ▶ if ( ref($val) eq 'Logic::Relational::Variable' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "Logic::Relational::Variable") 73 218 291
32 73 my $d = $subst->{bindings}{ $val->id };
33 73 ▶ if ( ref($d) eq 'Logic::Relational::Domain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d eq "Logic::Relational::Domain") 73 0 73
34 73 my $size = $d->size;
35 73 ▶ if ( !defined $min_size || $size < $min_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $min_size or $size < $min_size) 39 34 73
Condition66%2/3
not defined $min_size or $size < $min_size
l!l&&r!l&&!r
39034
MC/DC50%1/2
not defined $min_size or $size < $min_size
not defined $min_size $size < $min_size
Truth table66%2/3
not(A) or B
A= not defined $min_sizeB= $size < $min_size
ABresult
1X1
011
000
36 39 $min_size = $size;
37 39 $selected_var = $val;
38 39 $selected_domain = $d;
39 }
40 }
41 }
42 }
43  
44 # If all domain variables are bound, we succeed
45 102 ▶ if ( !$selected_var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selected_var) 63 39 102
46 63 my $succ = $state->derive( goals => $remaining, );
47 63 ▶ return $succ // ();
Condition33%1/3
$succ // ()
l!l&&r!l&&!r
6300
MC/DC0%0/2
$succ // ()
$succ ()
Truth table33%1/3
A // B
A= $succB= ()
ABresult
1X1
011
000
48 }
49  
50 # Backtrack over each value in the selected domain
51 39 my @successors;
52 39 require Logic::Relational::Unifier;
53  
54 39 for my $val ( $selected_domain->values ) {
55 117 my $new_subst =
56 Logic::Relational::Unifier::unify( $selected_var, $val, $subst );
57 117 ▶ if ($new_subst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_subst) 117 0 117
58 117 my $succ = $state->derive(
59 substitution => $new_subst,
60 goals => [ $self, @$remaining ],
61 );
62 117 ▶ push @successors, $succ if $succ;
Branch: 1 branch
BranchTrueFalseTotal
if $succ 91 26 117
63 }
64 }
65  
66 39 return @successors;
67 }
68  
69 7 ▶sub freshen ( $self, $var_map ) {
Subroutine freshen: called CC=1 SCAR=0.0
Pod documented
70 7 require Logic::Relational::DSL;
71 7 my @fresh_vars = map { $_->freshen($var_map) } @{ $self->{vars} };
72 7 return ref($self)->new( \@fresh_vars );
73 }
74  
75 0 ▶sub is_ground ( $self, $subst ) {
Subroutine is_ground: not called CC=3 SCAR=24.8
Pod documented
76 0 for my $var ( @{ $self->{vars} } ) {
77 0 ▶ return 0 unless $self->term_is_ground( $var, $subst );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->term_is_ground($var, $subst) 0 0 0
78 }
79 0 return 1;
80 }
81  
82 30 1;
« lib/Logic/Relational/Substitution.pm ↑ Summary blib/lib/Logic/Relational/Goal/AllDifferent.pm »