lib/Logic/Relational/Substitution.pm

filter: Statementstmt 90.0% 45 / 50 Branchbran 87.5% 21 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 4 / 4 totaltotal 90.4% 76 / 84 CC 17 SCAR 22.2
CC
17
Cov
89%
reify
13.8
walk
4.0
new
2.0
CRAP 9.2
« lib/Logic/Relational/Serializer.pm ↑ Summary blib/lib/Logic/Relational/Goal/Label.pm »
1 package Logic::Relational::Substitution;
2  
3 33 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Logic::Relational::Substitution - Represents an immutable variable substitution mapping.
9  
10 =head1 SYNOPSIS
11  
12 use Logic::Relational::Substitution;
13 my $subst = Logic::Relational::Substitution->new;
14 my $new_subst = $subst->bind($var_id => $val);
15  
16 =head1 DESCRIPTION
17  
18 C<Logic::Relational::Substitution> stores logical variable bindings. It is
19 immutable (copy-on-write) to make depth-first backtracking search simple and
20 robust without needing manual rollbacks of variables.
21  
22 =head1 METHODS
23  
24 =head2 new
25  
26 Constructor. Optionally takes a hash reference of initial bindings.
27  
28 =head2 bind
29  
30 Takes a variable ID and a value, returning a new C<Logic::Relational::Substitution>
31 object containing the new binding.
32  
33 =head2 walk
34  
35 Dereferences a term through the substitution. If the term is a variable and is bound,
36 recursively dereferences until it hits an unbound variable or a non-variable term.
37  
38 =head2 reify
39  
40 Recursively walks a term, replacing all bound variables within compound terms
41 with their bound values. Unbound variables remain unchanged.
42  
43 =cut
44  
45 1348 ▶sub new ( $class, $bindings = {} ) {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
46 1348 return bless { bindings => $bindings }, $class;
47 }
48  
49 1204 ▶sub bind ( $self, $var_id, $val ) {
Subroutine bind: called CC=1 SCAR=0.0
Pod documented
50 1204 my $new_bindings = { %{ $self->{bindings} }, $var_id => $val };
51 1204 return ref($self)->new($new_bindings);
52 }
53  
54 13719 ▶sub walk ( $self, $term ) {
Subroutine walk: called CC=4 SCAR=13.9
Pod documented
55 13719 ▶ while ( ref($term) eq 'Logic::Relational::Variable' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $term eq "Logic::Relational::Variable") 6782 10801 17583
56 6782 my $id = $term->id;
57 6782 ▶ if ( exists $self->{bindings}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'bindings'}{$id}) { } 4291 2491 6782
58 4291 my $bound = $self->{bindings}{$id};
59 4291 ▶ if ( ref($bound) eq 'Logic::Relational::Domain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bound eq "Logic::Relational::Domain") 427 3864 4291
60 427 last;
61 }
62 3864 $term = $bound;
63 }
64 else {
65 2491 last;
66 }
67 }
68 13719 return $term;
69 }
70  
71 5638 ▶sub reify ( $self, $term ) {
Subroutine reify: called CC=13 SCAR=26.2
Pod documented
72 5638 $term = $self->walk($term);
73 5638 ▶ if ( ref($term) eq 'Logic::Relational::Variable' ) { ## no critic (ProhibitCascadingIfElse)
Branch: 5 branches
BranchTrueFalseTotal
if (ref $term eq 'Logic::Relational::Variable') { } 1157 4481 5638
elsif (ref $term eq 'Logic::Relational::Term') { } 815 3666 4481
elsif (ref $term eq 'ARRAY') { } 556 3110 3666
elsif (ref $term eq 'Logic::Relational::ArrayRest') { } 41 3069 3110
elsif (ref $term eq 'HASH') { } 3 3066 3069
74 1157 return $term;
75 }
76 elsif ( ref($term) eq 'Logic::Relational::Term' ) {
77 815 my @reified_args = map { $self->reify($_) } @{ $term->args };
78 815 return ref($term)->new(
79 functor => $term->functor,
80 args => \@reified_args,
81 );
82 }
83 elsif ( ref($term) eq 'ARRAY' ) {
84 556 my @flat;
85 556 for my $elem (@$term) {
86 1085 my $r_elem = $self->reify($elem);
87 1085 ▶ if ( ref($r_elem) eq 'Logic::Relational::ArrayRest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r_elem eq 'Logic::Relational::ArrayRest') { } 35 1050 1085
88 35 my $inner = $r_elem->term;
89 35 ▶ if ( ref($inner) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inner eq 'ARRAY') { } 23 12 35
90 23 push @flat, @$inner;
91 }
92 else {
93 12 push @flat, $r_elem;
94 }
95 }
96 else {
97 1050 push @flat, $r_elem;
98 }
99 }
100 556 return \@flat;
101 }
102 elsif ( ref($term) eq 'Logic::Relational::ArrayRest' ) {
103 41 return ref($term)->new( $self->reify( $term->term ) );
104 }
105 elsif ( ref($term) eq 'HASH' ) {
106 3 my %flat;
107 3 for my $k ( keys %$term ) {
108 4 my $r_val = $self->reify( $term->{$k} );
109 4 ▶ if ( ref($r_val) eq 'Logic::Relational::ArrayRest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r_val eq 'Logic::Relational::ArrayRest') { } 0 4 4
110 0 my $inner = $r_val->term;
111 0 ▶ if ( ref($inner) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inner eq 'HASH') { } 0 0 0
112 0 for my $sub_k ( keys %$inner ) {
113 0 $flat{$sub_k} = $inner->{$sub_k};
114 }
115 }
116 else {
117 0 $flat{$k} = $r_val;
118 }
119 }
120 else {
121 4 $flat{$k} = $r_val;
122 }
123 }
124 3 return \%flat;
125 }
126 else {
127 3066 return $term;
128 }
129 }
130  
131 33 1;
« lib/Logic/Relational/Serializer.pm ↑ Summary blib/lib/Logic/Relational/Goal/Label.pm »