| 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
| |||||||||||||||||||||||||||||
| 32 | 73 | my $d = $subst->{bindings}{ $val->id }; | |||||||||||||||||||||||||||
| 33 | 73 | ▶ | if ( ref($d) eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 73 | my $size = $d->size; | |||||||||||||||||||||||||||
| 35 | 73 | ▶ | if ( !defined $min_size || $size < $min_size ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $min_size or $size < $min_size
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | 63 | my $succ = $state->derive( goals => $remaining, ); | |||||||||||||||||||||||||||
| 47 | 63 | ▶ | return $succ // (); | ||||||||||||||||||||||||||
|
Condition33%1/3
$succ // ()
MC/DC0%0/2
$succ // ()
$succ
()
Truth table33%1/3
A // B
A= $succB= ()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | return 1; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 30 | 1; | |||||||||||||||||||||||||||