« blib/lib/Logic/Relational/Goal/AllDifferent.pm
↑ Summary
lib/Logic/Relational/Goal/GeneratorStep.pm »
| 1 | package Logic::Relational::Goal::Domain; | ||||||||||||||||||||||||||||||||
| 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 | 30 | ▶ | use Logic::Relational::Domain; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 30 | ▶ | use Logic::Relational::Unifier qw(unify); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | Logic::Relational::Goal::Domain - Goal to constrain a variable to a range. | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 36 | ▶ | sub new ( $class, $var, @spec ) { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 17 | 36 | ▶ | if ( @spec == 1 && ref( $spec[0] ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@spec == 1 and ref $spec[0] eq "ARRAY"
MC/DC50%1/2
@spec == 1 and ref $spec[0] eq "ARRAY"
@spec == 1
ref $spec[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @spec == 1B= ref $spec[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 18 | 2 | return bless { var => $var, values => $spec[0] }, $class; | |||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||
| 20 | elsif ( @spec == 2 ) { | ||||||||||||||||||||||||||||||||
| 21 | 34 | return bless { var => $var, min => $spec[0], max => $spec[1] }, $class; | |||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | 0 | croak "Invalid Goal::Domain specification"; | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 22 | ▶ | sub expand ( $self, %args ) { | ||||||||||||||||||||||||||||||
|
Subroutine
expand: called CC=8 SCAR=28.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 27 | 22 | my $subst = $args{substitution}; | |||||||||||||||||||||||||||||||
| 28 | 22 | my $remaining = $args{remaining}; | |||||||||||||||||||||||||||||||
| 29 | 22 | my $state = $args{state}; | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 22 | my $val = $subst->walk( $self->{var} ); | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | my $domain_spec = | ||||||||||||||||||||||||||||||||
| 34 | exists $self->{values} | ||||||||||||||||||||||||||||||||
| 35 | ? { values => $self->{values} } | ||||||||||||||||||||||||||||||||
| 36 | 22 | ▶ | : { min => $self->{min}, max => $self->{max} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 22 | ▶ | if ( ref($val) eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 22 | my $domain = Logic::Relational::Domain->new($domain_spec); | |||||||||||||||||||||||||||||||
| 40 | 22 | my $new_subst = unify( $val, $domain, $subst ); | |||||||||||||||||||||||||||||||
| 41 | 22 | ▶ | if ($new_subst) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 22 | my $succ = $state->derive( | |||||||||||||||||||||||||||||||
| 43 | substitution => $new_subst, | ||||||||||||||||||||||||||||||||
| 44 | goals => $remaining, | ||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||
| 46 | 22 | ▶ | return $succ // (); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$succ // ()
MC/DC0%0/2
$succ // ()
$succ
()
Truth table33%1/3
A // B
A= $succB= ()
| |||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||||||
| 50 | 0 | my $domain = Logic::Relational::Domain->new($domain_spec); | |||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( $val =~ /^-?\d+$/x && $domain->contains($val) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$val =~ /^-?\d+$/ux and $domain->contains($val)
MC/DC0%0/2
$val =~ /^-?\d+$/ux and $domain->contains($val)
$val =~ /^-?\d+$/ux
$domain->contains($val)
Truth table0%0/3
A and B
A= $val =~ /^-?\d+$/uxB= $domain->contains($val)
| |||||||||||||||||||||||||||||||||
| 52 | 0 | my $succ = $state->derive( goals => $remaining, ); | |||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return $succ // (); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$succ // ()
MC/DC0%0/2
$succ // ()
$succ
()
Truth table0%0/3
A // B
A= $succB= ()
| |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 0 | return (); | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 14 | ▶ | sub freshen ( $self, $var_map ) { | ||||||||||||||||||||||||||||||
|
Subroutine
freshen: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 61 | 14 | require Logic::Relational::DSL; | |||||||||||||||||||||||||||||||
| 62 | my $fresh_var = | ||||||||||||||||||||||||||||||||
| 63 | 14 | Logic::Relational::DSL::freshen_val( $self->{var}, $var_map ); | |||||||||||||||||||||||||||||||
| 64 | return | ||||||||||||||||||||||||||||||||
| 65 | exists $self->{values} | ||||||||||||||||||||||||||||||||
| 66 | ? ref($self)->new( $fresh_var, $self->{values} ) | ||||||||||||||||||||||||||||||||
| 67 | 14 | ▶ | : ref($self)->new( $fresh_var, $self->{min}, $self->{max} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub is_ground ( $self, $subst ) { | ||||||||||||||||||||||||||||||
|
Subroutine
is_ground: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 71 | 0 | return $self->term_is_ground( $self->{var}, $subst ); | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 30 | 1; | |||||||||||||||||||||||||||||||