lib/Logic/Relational/State.pm

filter: Statementstmt 94.1% 80 / 85 Branchbran 83.3% 20 / 24 Conditioncond 59.2% 16 / 27 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 94.4% 17 / 18 Podpod 100% 12 / 12 totaltotal 81.3% 148 / 182 CC 26 SCAR 17.5
CC
26
Cov
85%
__ANON__
11.0
derive
8.4
new
6.1
CRAP 5.8
« blib/lib/Logic/Relational/Domain.pm ↑ Summary blib/lib/Logic/Relational/Goal/CellAt.pm »
1 package Logic::Relational::State;
2  
3 27 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use builtin 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶no warnings 'experimental::builtin'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Logic::Relational::State - Represents a frozen search state in the logic engine.
12  
13 =head1 METHODS
14  
15 =head2 new
16  
17 Constructor. Accepts C<substitution>, C<goals>, C<propagators>, C<ancestors>,
18 and C<table_cache>.
19  
20 =head2 substitution
21  
22 Returns the current L<Logic::Relational::Substitution>.
23  
24 =head2 goals
25  
26 Returns the arrayref of remaining goals.
27  
28 =head2 propagators
29  
30 Returns the arrayref of active constraint propagator callbacks.
31  
32 =head2 ancestors
33  
34 Returns the arrayref of active ancestor call terms along the current derivation path.
35  
36 =head2 table_cache
37  
38 Returns the shared subgoal memoization hashref for the active query.
39  
40 =head2 has_goals
41  
42 Returns true if there are remaining goals to prove.
43  
44 =head2 split_first_goal
45  
46 Returns the first goal and the list of remaining goals.
47  
48 =head2 add_propagator
49  
50 Returns a new state with the given constraint propagator added.
51  
52 =head2 propagate
53  
54 Runs all active constraint propagators until a fixpoint is reached.
55  
56 =head2 derive
57  
58 Derives a successor state with updated substitution, goals, propagators, or ancestors.
59  
60 =head2 canonical_term_signature
61  
62 Computes a canonical string signature for a term under a substitution, normalizing
63 unbound logical variables by order of first appearance (C<V0>, C<V1>, ...).
64  
65 =cut
66  
67 1912 ▶sub new ( $class, %args ) {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
68 1912 ▶ my $subst = $args{substitution} // croak "substitution is required";
Condition50%1/2
$args{"substitution"} // croak("substitution is required")
l!l
19120
MC/DC0%0/1
$args{"substitution"} // croak("substitution is required")
$args{"substitution"}
Truth table50%1/2
A
A= $args{"substitution"}
Aresult
11
00
69 1912 ▶ my $goals = $args{goals} // croak "goals list is required";
Condition50%1/2
$args{"goals"} // croak("goals list is required")
l!l
19120
MC/DC0%0/1
$args{"goals"} // croak("goals list is required")
$args{"goals"}
Truth table50%1/2
A
A= $args{"goals"}
Aresult
11
00
70 1912 ▶ my $props = $args{propagators} // [];
Condition100%2/2
$args{"propagators"} // []
l!l
1746166
MC/DC100%1/1
$args{"propagators"} // []
$args{"propagators"}
Truth table100%2/2
A
A= $args{"propagators"}
Aresult
11
00
71 1912 ▶ my $ancestors = $args{ancestors} // [];
Condition100%2/2
$args{"ancestors"} // []
l!l
19093
MC/DC100%1/1
$args{"ancestors"} // []
$args{"ancestors"}
Truth table100%2/2
A
A= $args{"ancestors"}
Aresult
11
00
72 1912 ▶ my $table_cache = $args{table_cache} // {};
Condition100%2/2
$args{"table_cache"} // {}
l!l
19093
MC/DC100%1/1
$args{"table_cache"} // {}
$args{"table_cache"}
Truth table100%2/2
A
A= $args{"table_cache"}
Aresult
11
00
73 1912 return bless {
74 substitution => $subst,
75 goals => $goals,
76 propagators => $props,
77 ancestors => $ancestors,
78 table_cache => $table_cache,
79 }, $class;
80 }
81  
82 1898 ▶sub substitution ($self) {
Subroutine substitution: called CC=1 SCAR=0.0
Pod documented
83 1898 return $self->{substitution};
84 }
85  
86 1451 ▶sub goals ($self) {
Subroutine goals: called CC=1 SCAR=0.0
Pod documented
87 1451 return $self->{goals};
88 }
89  
90 0 ▶sub propagators ($self) {
Subroutine propagators: not called CC=1 SCAR=6.9
Pod documented
91 0 return $self->{propagators};
92 }
93  
94 467 ▶sub ancestors ($self) {
Subroutine ancestors: called CC=1 SCAR=0.0
Pod documented
95 467 return $self->{ancestors};
96 }
97  
98 41 ▶sub table_cache ($self) {
Subroutine table_cache: called CC=1 SCAR=0.0
Pod documented
99 41 return $self->{table_cache};
100 }
101  
102 3598 ▶sub has_goals ($self) {
Subroutine has_goals: called CC=1 SCAR=0.0
Pod documented
103 3598 return scalar @{ $self->{goals} } > 0;
104 }
105  
106 1450 ▶sub split_first_goal ($self) {
Subroutine split_first_goal: called CC=1 SCAR=0.0
Pod documented
107 1450 my ( $first, @rest ) = @{ $self->{goals} };
108 1450 return ( $first, @rest );
109 }
110  
111 16 ▶sub add_propagator ( $self, $callback ) {
Subroutine add_propagator: called CC=1 SCAR=0.0
Pod documented
112 16 my $new_props = [ @{ $self->{propagators} }, $callback ];
113 return ref($self)->new(
114 substitution => $self->{substitution},
115 goals => $self->{goals},
116 propagators => $new_props,
117 ancestors => $self->{ancestors},
118 table_cache => $self->{table_cache},
119 16 );
120 }
121  
122 1756 ▶sub propagate ( $self, $subst ) {
Subroutine propagate: called CC=5 SCAR=16.1
Pod documented
123 1756 my $changed = 1;
124 1756 my $current_subst = $subst;
125  
126 1756 ▶ while ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 1807 1730 3537
127 1807 $changed = 0;
128 1807 for my $prop ( @{ $self->{propagators} } ) {
129 314 my $new_subst = $prop->($current_subst);
130 314 ▶ return unless $new_subst;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_subst 26 288 314
131  
132 # Scalar reference address comparison in Perl to detect changes
133 288 ▶ if ( $new_subst != $current_subst ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_subst != $current_subst) 51 237 288
134 51 $current_subst = $new_subst;
135 51 $changed = 1;
136 }
137 }
138 }
139 1730 return $current_subst;
140 }
141  
142 1756 ▶sub derive ( $self, %args ) {
Subroutine derive: called CC=7 SCAR=21.3
Pod documented
143 1756 ▶ my $new_subst = $args{substitution} // $self->{substitution};
Condition66%2/3
$args{"substitution"} // $self->{"substitution"}
l!l&&r!l&&!r
11456110
MC/DC0%0/2
$args{"substitution"} // $self->{"substitution"}
$args{"substitution"} $self->{"substitution"}
Truth table66%2/3
A // B
A= $args{"substitution"}B= $self->{"substitution"}
ABresult
1X1
011
000
144 1756 ▶ my $new_goals = $args{goals} // $self->{goals};
Condition33%1/3
$args{"goals"} // $self->{"goals"}
l!l&&r!l&&!r
175600
MC/DC0%0/2
$args{"goals"} // $self->{"goals"}
$args{"goals"} $self->{"goals"}
Truth table33%1/3
A // B
A= $args{"goals"}B= $self->{"goals"}
ABresult
1X1
011
000
145 1756 ▶ my $new_props = $args{propagators} // $self->{propagators};
Condition33%1/3
$args{"propagators"} // $self->{"propagators"}
l!l&&r!l&&!r
017560
MC/DC0%0/2
$args{"propagators"} // $self->{"propagators"}
$args{"propagators"} $self->{"propagators"}
Truth table33%1/3
A // B
A= $args{"propagators"}B= $self->{"propagators"}
ABresult
1X1
011
000
146 1756 ▶ my $new_anc = $args{ancestors} // $self->{ancestors};
Condition66%2/3
$args{"ancestors"} // $self->{"ancestors"}
l!l&&r!l&&!r
27714790
MC/DC0%0/2
$args{"ancestors"} // $self->{"ancestors"}
$args{"ancestors"} $self->{"ancestors"}
Truth table66%2/3
A // B
A= $args{"ancestors"}B= $self->{"ancestors"}
ABresult
1X1
011
000
147 1756 ▶ my $new_cache = $args{table_cache} // $self->{table_cache};
Condition33%1/3
$args{"table_cache"} // $self->{"table_cache"}
l!l&&r!l&&!r
017560
MC/DC0%0/2
$args{"table_cache"} // $self->{"table_cache"}
$args{"table_cache"} $self->{"table_cache"}
Truth table33%1/3
A // B
A= $args{"table_cache"}B= $self->{"table_cache"}
ABresult
1X1
011
000
148  
149 1756 my $temp_state = bless {
150 substitution => $new_subst,
151 goals => $new_goals,
152 propagators => $new_props,
153 ancestors => $new_anc,
154 table_cache => $new_cache,
155 },
156 ref($self);
157  
158 1756 my $propagated_subst = $temp_state->propagate($new_subst);
159 1756 ▶ return unless $propagated_subst;
Branch: 1 branch
BranchTrueFalseTotal
unless $propagated_subst 26 1730 1756
160  
161 1730 return ref($self)->new(
162 substitution => $propagated_subst,
163 goals => $new_goals,
164 propagators => $new_props,
165 ancestors => $new_anc,
166 table_cache => $new_cache,
167 );
168 }
169  
170 759 ▶sub canonical_term_signature ( $self, $term, $subst = $self->{substitution} ) {
Subroutine canonical_term_signature: called CC=2 SCAR=7.1
Pod documented
171 759 my %var_ids;
172 759 my $next_id = 0;
173 759 my $stringify;
174 2510 ▶ $stringify = sub ($val) {
Subroutine __ANON__: called CC=10 SCAR=24.0
175 2510 my $r = $subst->reify($val);
176 2510 ▶ return 'U' unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 2510 2510
177 2510 ▶ if ( blessed($r) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($r)) 1268 1242 2510
178 1268 ▶ if ( $r->isa('Logic::Relational::Variable') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->isa("Logic::Relational::Variable")) 523 745 1268
179 523 my $vid = $r->id;
180 523 ▶ $var_ids{$vid} //= $next_id++;
Condition50%1/2
$var_ids{$vid} //= $next_id++
l!l
0523
MC/DC0%0/1
$var_ids{$vid} //= $next_id++
$var_ids{$vid}
Truth table50%1/2
A
A= $var_ids{$vid}
Aresult
11
00
181 523 return 'V' . $var_ids{$vid};
182 }
183 745 ▶ if ( $r->isa('Logic::Relational::Atom') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->isa("Logic::Relational::Atom")) 0 745 745
184 0 return 'A:' . $r->name;
185 }
186 745 ▶ if ( $r->isa('Logic::Relational::Term') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->isa("Logic::Relational::Term")) 739 6 745
187 739 my @sub = map { $stringify->($_) } @{ $r->args };
188 739 return 'T:' . $r->functor . '(' . join( ',', @sub ) . ')';
189 }
190 6 ▶ if ( $r->isa('Logic::Relational::ArrayRest') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->isa("Logic::Relational::ArrayRest")) 6 0 6
191 6 return 'R:' . $stringify->( $r->term );
192 }
193 }
194 1242 ▶ if ( ref($r) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "ARRAY") 205 1037 1242
195 205 return '[' . join( ',', map { $stringify->($_) } @$r ) . ']';
196 }
197 1037 ▶ if ( ref($r) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r eq "HASH") 0 1037 1037
198 my @pairs =
199 0 map { $_ . ':' . $stringify->( $r->{$_} ) } sort keys %$r;
200 0 return '{' . join( ',', @pairs ) . '}';
201 }
202 1037 return "S:$r";
203 759 };
204 759 return $stringify->($term);
205 }
206  
207 27 1;
« blib/lib/Logic/Relational/Domain.pm ↑ Summary blib/lib/Logic/Relational/Goal/CellAt.pm »