| 1 | package Logic::Relational::Goal::AllDifferent; | ||||||||||||||||||||
| 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 | 30 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Logic::Relational::Goal::AllDifferent - Constraint goal that all variables must have distinct values. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =cut | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 4 | ▶ | sub new ( $class, $vars ) { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 15 | 4 | return bless { vars => $vars }, $class; | |||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 3 | ▶ | sub expand ( $self, %args ) { | ||||||||||||||||||
|
Subroutine
expand: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 19 | 3 | my $subst = $args{substitution}; | |||||||||||||||||||
| 20 | 3 | my $remaining = $args{remaining}; | |||||||||||||||||||
| 21 | 3 | my $state = $args{state}; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 3 | my @vars = @{ $self->{vars} }; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 190 | ▶ | my $propagator = sub ($curr_subst) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=25.2
| |||||||||||||||||||||
| 26 | 190 | my %bound_vals; | |||||||||||||||||||
| 27 | my %unbound_vars; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # 1. Collect bound values and unbound constraint variables | ||||||||||||||||||||
| 30 | 190 | for my $var (@vars) { | |||||||||||||||||||
| 31 | 708 | my $val = $curr_subst->walk($var); | |||||||||||||||||||
| 32 | 708 | ▶ | if ( ref($val) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 33 | 146 | $unbound_vars{ $val->id } = $val; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | elsif ( ref($val) eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||||
| 36 | 0 | croak "Walk returned raw domain object in propagator"; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||
| 39 | 562 | ▶ | if ( exists $bound_vals{$val} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 0 | return; # Conflict! Same value bound to two variables | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | 562 | $bound_vals{$val} = 1; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # 2. Prune bound values from unbound variables' domains | ||||||||||||||||||||
| 47 | 190 | for my $id ( keys %unbound_vars ) { | |||||||||||||||||||
| 48 | 146 | my $val = $unbound_vars{$id}; | |||||||||||||||||||
| 49 | 146 | my $d = $curr_subst->{bindings}{$id}; | |||||||||||||||||||
| 50 | 146 | ▶ | if ( ref($d) eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 146 | my %remaining_vals; | |||||||||||||||||||
| 52 | 146 | for my $v ( $d->values ) { | |||||||||||||||||||
| 53 | 392 | ▶ | if ( !exists $bound_vals{$v} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 318 | $remaining_vals{$v} = 1; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 146 | my $new_size = scalar keys %remaining_vals; | |||||||||||||||||||
| 59 | 146 | ▶ | return if $new_size == 0; # Conflict: domain is empty | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 146 | ▶ | if ( $new_size < $d->size ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 74 | require Logic::Relational::Domain; | |||||||||||||||||||
| 63 | 74 | my $d_new = | |||||||||||||||||||
| 64 | Logic::Relational::Domain->new( \%remaining_vals ); | ||||||||||||||||||||
| 65 | 74 | ▶ | if ( $d_new->is_bound ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 32 | $curr_subst = | |||||||||||||||||||
| 67 | $curr_subst->bind( $id, $d_new->bound_value ); | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||
| 70 | 42 | $curr_subst = $curr_subst->bind( $id, $d_new ); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 190 | return $curr_subst; | |||||||||||||||||||
| 77 | 3 | }; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 3 | my $new_subst = $propagator->($subst); | |||||||||||||||||||
| 80 | 3 | ▶ | if ($new_subst) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 3 | my $new_state = $state->add_propagator($propagator); | |||||||||||||||||||
| 82 | 3 | my $succ = $new_state->derive( | |||||||||||||||||||
| 83 | substitution => $new_subst, | ||||||||||||||||||||
| 84 | goals => $remaining, | ||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||
| 86 | 3 | ▶ | return $succ // (); | ||||||||||||||||||
|
Condition33%1/3
$succ // ()
MC/DC0%0/2
$succ // ()
$succ
()
Truth table33%1/3
A // B
A= $succB= ()
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | return (); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1 | ▶ | sub freshen ( $self, $var_map ) { | ||||||||||||||||||
|
Subroutine
freshen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 93 | 1 | require Logic::Relational::DSL; | |||||||||||||||||||
| 94 | 1 | my @fresh_vars = map { $_->freshen($var_map) } @{ $self->{vars} }; | |||||||||||||||||||
| 95 | 1 | return ref($self)->new( \@fresh_vars ); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | sub is_ground ( $self, $subst ) { | ||||||||||||||||||
|
Subroutine
is_ground: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 99 | 0 | for my $var ( @{ $self->{vars} } ) { | |||||||||||||||||||
| 100 | 0 | ▶ | return 0 unless $self->term_is_ground( $var, $subst ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 0 | return 1; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 30 | 1; | |||||||||||||||||||