lib/Logic/Relational/Program.pm

filter: Statementstmt 88.0% 376 / 427 Branchbran 63.2% 124 / 196 Conditioncond 54.8% 74 / 135 MC/DCmcdc 25.3% 18 / 71 Subroutinesub 97.5% 40 / 41 Podpod 57.1% 12 / 21 totaltotal 72.2% 644 / 891 CC 177 SCAR 31.6
CC
177
Cov
76%
load_snapshot
80.6
untable
42.0
_term_contains_var
27.8
CRAP 23.6
« blib/lib/Logic/Relational/Goal.pm ↑ Summary blib/lib/Logic/Relational/Goal/ConstraintFD.pm »
1 package Logic::Relational::Program;
2  
3 28 ▶use v5.38; ## no critic (ValuesAndExpressions::ProhibitVersionStrings)
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶no warnings 'experimental::try'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use builtin 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Logic::Relational::Program - Represents the mutable relational logic program.
16  
17 =head1 SYNOPSIS
18  
19 use Logic::Relational::Program;
20 my $program = Logic::Relational::Program->new;
21 $program->fact(thief => 'badguy');
22  
23 =head1 DESCRIPTION
24  
25 C<Logic::Relational::Program> manages the knowledge base containing facts and rules.
26 It supports transaction isolation (atomicity on commits/rollbacks) and snapshot
27 semantics for active queries.
28  
29 =head1 METHODS
30  
31 =head2 new
32  
33 Constructor.
34  
35 =head2 fact
36  
37 Asserts a fact. Can take a C<Logic::Relational::Clause> object, or a predicate
38 name and arguments. Returns the generated clause ID.
39  
40 =head2 rule
41  
42 Asserts a rule. Takes C<head> (Term or Call goal), C<body> (Goal), and C<metadata> (optional).
43 Returns the generated clause ID.
44  
45 =head2 retract
46  
47 Retracts the first clause whose head unifies with the pattern. Returns 1 if a clause
48 was retracted, 0 otherwise.
49  
50 =head2 retract_all
51  
52 Retracts all clauses whose heads unify with the pattern. Returns the number of retracted clauses.
53  
54 =head2 retract_clause
55  
56 Retracts a clause with the exact ID. Throws an exception if the ID does not exist.
57  
58 =head2 clauses_for
59  
60 Introspection method. Returns a list of C<Logic::Relational::Clause> objects
61 for the given name and arity.
62  
63 =head2 query
64  
65 Creates and returns a C<Logic::Relational::Query> snapshot query object.
66  
67 =head2 transaction
68  
69 Executes a coderef atomically. If the block throws an exception, all changes are
70 rolled back.
71  
72 =head2 table
73  
74 Marks a predicate (by name and optional arity, or C<'name/arity'>) for subgoal
75 tabling and answer memoization during queries.
76  
77 =head2 untable
78  
79 Removes tabling for the specified predicate.
80  
81 =head2 is_tabled
82  
83 Returns true if the predicate with the given name and arity is tabled.
84  
85 =cut
86  
87 66 ▶sub new ( $class, %args ) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
88 my $self = {
89 predicates => {},
90 active_predicates => {},
91 generators => {},
92 tabled => {},
93 on_expire_handlers => {},
94 version => 1,
95 next_clause_id => 1,
96 in_transaction => 0,
97 observers => [],
98 max_steps => $args{max_steps},
99 66 };
100 66 $self->{active_predicates} = $self->{predicates};
101 66 return bless $self, $class;
102 }
103  
104 166 ▶sub max_steps ( $self, @args ) {
Subroutine max_steps: called CC=2 SCAR=6.9
Pod undocumented
105 166 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 1 165 166
106 1 $self->{max_steps} = $args[0];
107 }
108 166 return $self->{max_steps};
109 }
110  
111 254 ▶sub fact ( $self, $name, @args ) {
Subroutine fact: called CC=2 SCAR=7.1
Pod documented
112 254 my $clause;
113 254 ▶ if ( ref($name) eq 'Logic::Relational::Clause' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name eq 'Logic::Relational::Clause') { } 0 254 254
114 0 $clause = $name;
115 }
116 else {
117 254 require Logic::Relational::Term;
118 254 require Logic::Relational::Clause;
119 254 my $head =
120 Logic::Relational::Term->new( functor => $name, args => \@args );
121 254 $clause = Logic::Relational::Clause->new( head => $head );
122 }
123 254 return $self->_add_clause($clause);
124 }
125  
126 357 ▶sub rule ( $self, %args ) {
Subroutine rule: called CC=13 SCAR=27.6
Pod documented
127 357 ▶ my $head = $args{head} // croak "rule head is required";
Condition50%1/2
$args{"head"} // croak("rule head is required")
l!l
3570
MC/DC0%0/1
$args{"head"} // croak("rule head is required")
$args{"head"}
Truth table50%1/2
A
A= $args{"head"}
Aresult
11
00
128 357 ▶ my $body = $args{body} // croak "rule body is required";
Condition50%1/2
$args{"body"} // croak("rule body is required")
l!l
3570
MC/DC0%0/1
$args{"body"} // croak("rule body is required")
$args{"body"}
Truth table50%1/2
A
A= $args{"body"}
Aresult
11
00
129 357 ▶ my $metadata = $args{metadata} // {};
Condition50%1/2
$args{"metadata"} // {}
l!l
0357
MC/DC0%0/1
$args{"metadata"} // {}
$args{"metadata"}
Truth table50%1/2
A
A= $args{"metadata"}
Aresult
11
00
130  
131 357 my $head_term;
132 357 ▶ if ( ref($head) eq 'Logic::Relational::Goal::Call' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $head eq 'Logic::Relational::Goal::Call') { } 357 0 357
elsif (ref $head eq 'Logic::Relational::Term') { } 0 0 0
133 357 require Logic::Relational::Term;
134 357 $head_term = Logic::Relational::Term->new(
135 functor => $head->name,
136 args => $head->args
137 );
138 }
139 elsif ( ref($head) eq 'Logic::Relational::Term' ) {
140 0 $head_term = $head;
141 }
142 else {
143 0 croak "Invalid rule head: must be a Term or Call goal";
144 }
145  
146 # Detect direct left-recursion (excluding well-founded structural recursion)
147 357 my $first_subgoal = $self->_first_subgoal($body);
148 357 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_subgoal and builtin::blessed($first_subgoal) and $first_subgoal->isa("Logic::Relational::Goal::Call") and $first_subgoal->name eq $head_term->functor and $first_subgoal->arity == $head_term->arity and not $self->_is_structurally_decreasing($head_term->args, $first_subgoal->args)) 4 353 357
Condition73%11/15
defined $first_subgoal and builtin::blessed($first_subgoal) and $first_subgoal->isa("Logic::Relational::Goal::Call") and $first_subgoal->name eq $head_term->functor and $first_subgoal->arity == $head_term->arity and not $self->_is_structurally_decreasing($head_term->args, $first_subgoal->args)
!ll&&!rl&&r
2041494
defined $first_subgoal and builtin::blessed($first_subgoal) and $first_subgoal->isa("Logic::Relational::Goal::Call") and $first_subgoal->name eq $head_term->functor and $first_subgoal->arity == $head_term->arity
!ll&&!rl&&r
2040153
defined $first_subgoal and builtin::blessed($first_subgoal) and $first_subgoal->isa("Logic::Relational::Goal::Call") and $first_subgoal->name eq $head_term->functor
!ll&&!rl&&r
14064153
defined $first_subgoal and builtin::blessed($first_subgoal) and $first_subgoal->isa("Logic::Relational::Goal::Call")
!ll&&!rl&&r
0140217
defined $first_subgoal and builtin::blessed($first_subgoal)
!ll&&!rl&&r
00357
MC/DC50%3/6
defined $first_subgoal and builtin::blessed($first_subgoal) and $first_subgoal->isa("Logic::Relational::Goal::Call") and $first_subgoal->name eq $head_term->functor and $first_subgoal->arity == $head_term->arity and not $self->_is_structurally_decreasing($head_term->args, $first_subgoal->args)
defined $first_subgoal builtin::blessed($first_subgoal) $first_subgoal->isa("Logic::Relational::Goal::Call") $first_subgoal->name eq $head_term->functor $first_subgoal->arity == $head_term->arity not $self->_is_structurally_decreasing($head_term->args, $first_subgoal->args)
Truth table57%4/7
A and B and C and D and E and not(F)
A= defined $first_subgoalB= builtin::blessed($first_subgoal)C= $first_subgoal->isa("Logic::Relational::Goal::Call")D= $first_subgoal->name eq $head_term->functorE= $first_subgoal->arity == $head_term->arityF= not $self->_is_structurally_decreasing($head_term->args, $first_subgoal->args)
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
149 defined $first_subgoal
150 && blessed($first_subgoal)
151 && $first_subgoal->isa('Logic::Relational::Goal::Call')
152 && $first_subgoal->name eq $head_term->functor
153 && $first_subgoal->arity == $head_term->arity
154 && !$self->_is_structurally_decreasing(
155 $head_term->args, $first_subgoal->args
156 )
157 )
158 {
159 4 my $pred_spec = $head_term->functor . '/' . $head_term->arity;
160 4 ▶ if ( !$self->is_tabled( $head_term->functor, $head_term->arity ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_tabled($head_term->functor, $head_term->arity)) 2 2 4
161 2 carp
162 "Warning: Direct left-recursion detected in rule for '$pred_spec'. Automatically enabling tabling for '$pred_spec'.";
163 2 $self->table( $head_term->functor, $head_term->arity );
164 }
165 }
166  
167 357 require Logic::Relational::Clause;
168 357 my $clause = Logic::Relational::Clause->new(
169 head => $head_term,
170 body => $body,
171 metadata => $metadata,
172 );
173 357 return $self->_add_clause($clause);
174 }
175  
176 357 ▶sub _first_subgoal ( $self, $goal ) {
Subroutine _first_subgoal: called CC=8 SCAR=24.5
177 357 ▶ return unless defined $goal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $goal 0 357 357
178 357 while (1) {
179 466 ▶ if ( ref($goal) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $goal eq 'ARRAY') { } 0 466 466
elsif (builtin::blessed($goal) and $goal->isa('Logic::Relational::Goal::All')) { } 109 357 466
180 0 ▶ return unless @$goal;
Branch: 1 branch
BranchTrueFalseTotal
unless @$goal 0 0 0
181 0 $goal = $goal->[0];
182 }
183 ▶ elsif ( blessed($goal) && $goal->isa('Logic::Relational::Goal::All') ) {
Condition66%2/3
builtin::blessed($goal) and $goal->isa("Logic::Relational::Goal::All")
!ll&&!rl&&r
0357109
MC/DC50%1/2
builtin::blessed($goal) and $goal->isa("Logic::Relational::Goal::All")
builtin::blessed($goal) $goal->isa("Logic::Relational::Goal::All")
Truth table66%2/3
A and B
A= builtin::blessed($goal)B= $goal->isa("Logic::Relational::Goal::All")
ABresult
0X0
100
111
184 109 my $goals = $goal->goals;
185 109 ▶ return unless $goals && @$goals;
Branch: 1 branch
BranchTrueFalseTotal
unless $goals and @$goals 0 109 109
Condition33%1/3
$goals and @$goals
!ll&&!rl&&r
00109
MC/DC0%0/2
$goals and @$goals
$goals @$goals
Truth table33%1/3
A and B
A= $goalsB= @$goals
ABresult
0X0
100
111
186 109 $goal = $goals->[0];
187 }
188 else {
189 357 last;
190 }
191 }
192 357 return $goal;
193 }
194  
195 199 ▶sub _is_compound ( $self, $term ) {
Subroutine _is_compound: called CC=8 SCAR=25.2
196 199 ▶ return 0 unless ref($term);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $term 0 199 199
197 199 ▶ return 1 if ref($term) eq 'ARRAY' || ref($term) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $term eq "ARRAY" or ref $term eq "HASH" 149 50 199
Condition66%2/3
ref $term eq "ARRAY" or ref $term eq "HASH"
l!l&&r!l&&!r
149050
MC/DC50%1/2
ref $term eq "ARRAY" or ref $term eq "HASH"
ref $term eq "ARRAY" ref $term eq "HASH"
Truth table66%2/3
A or B
A= ref $term eq "ARRAY"B= ref $term eq "HASH"
ABresult
1X1
011
000
198 50 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($term) and $term->isa("Logic::Relational::ArrayRest") || $term->isa("Logic::Relational::Term") && $term->arity > 0 0 50 50
Condition33%3/9
builtin::blessed($term) and $term->isa("Logic::Relational::ArrayRest") || $term->isa("Logic::Relational::Term") && $term->arity > 0
!ll&&!rl&&r
0500
$term->isa("Logic::Relational::ArrayRest") or $term->isa("Logic::Relational::Term") and $term->arity > 0
l!l&&r!l&&!r
0050
$term->isa("Logic::Relational::Term") and $term->arity > 0
!ll&&!rl&&r
5000
MC/DC0%0/4
builtin::blessed($term) and $term->isa("Logic::Relational::ArrayRest") || $term->isa("Logic::Relational::Term") && $term->arity > 0
builtin::blessed($term) $term->isa("Logic::Relational::ArrayRest") $term->isa("Logic::Relational::Term") $term->arity > 0
Truth table20%1/5
A and B or C and D
A= builtin::blessed($term)B= $term->isa("Logic::Relational::ArrayRest")C= $term->isa("Logic::Relational::Term")D= $term->arity > 0
ABCDresult
0XXX0
100X0
10100
11XX1
10111
199 if blessed($term)
200 && ( $term->isa('Logic::Relational::ArrayRest')
201 || ( $term->isa('Logic::Relational::Term') && $term->arity > 0 ) );
202 50 return 0;
203 }
204  
205 594 ▶sub _term_contains_var ( $self, $term, $target_name ) {
Subroutine _term_contains_var: called CC=15 SCAR=33.3
206 594 ▶ return 0 unless defined $term;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term 0 594 594
207 594 ▶ if ( blessed($term) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 443 151 594
208 443 ▶ if ( $term->isa('Logic::Relational::Variable') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->isa("Logic::Relational::Variable")) 296 147 443
209 296 ▶ return $term->name eq $target_name ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$term->name eq $target_name ? : 149 147 296
210 }
211 147 ▶ if ( $term->isa('Logic::Relational::ArrayRest') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->isa("Logic::Relational::ArrayRest")) 147 0 147
212 147 return $self->_term_contains_var( $term->term, $target_name );
213 }
214 0 ▶ if ( $term->isa('Logic::Relational::Term') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->isa("Logic::Relational::Term")) 0 0 0
215 0 for my $arg ( @{ $term->args } ) {
216 0 ▶ return 1 if $self->_term_contains_var( $arg, $target_name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_term_contains_var($arg, $target_name) 0 0 0
217 }
218 }
219 }
220 151 ▶ if ( ref($term) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $term eq "ARRAY") 149 2 151
221 149 for my $item (@$term) {
222 298 ▶ return 1 if $self->_term_contains_var( $item, $target_name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_term_contains_var($item, $target_name) 149 149 298
223 }
224 }
225 2 ▶ if ( ref($term) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $term eq "HASH") 0 2 2
226 0 for my $item ( values %$term ) {
227 0 ▶ return 1 if $self->_term_contains_var( $item, $target_name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_term_contains_var($item, $target_name) 0 0 0
228 }
229 }
230 2 return 0;
231 }
232  
233 153 ▶sub _is_structurally_decreasing ( $self, $head_args, $call_args ) {
Subroutine _is_structurally_decreasing: called CC=8 SCAR=22.4
234 153 ▶ return 0 unless $head_args && $call_args;
Branch: 1 branch
BranchTrueFalseTotal
unless $head_args and $call_args 0 153 153
Condition33%1/3
$head_args and $call_args
!ll&&!rl&&r
00153
MC/DC0%0/2
$head_args and $call_args
$head_args $call_args
Truth table33%1/3
A and B
A= $head_argsB= $call_args
ABresult
0X0
100
111
235 153 my $count = scalar(@$head_args);
236 153 ▶ for ( my $i = 0 ; $i < $count ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 199 4 203
237 199 my $h = $head_args->[$i];
238 199 my $c = $call_args->[$i];
239 199 ▶ if ( $self->_is_compound($h) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_compound($h)) 149 50 199
240 149 ▶ if ( blessed($c) && $c->isa('Logic::Relational::Variable') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($c) and $c->isa("Logic::Relational::Variable")) 149 0 149
Condition33%1/3
builtin::blessed($c) and $c->isa("Logic::Relational::Variable")
!ll&&!rl&&r
00149
MC/DC0%0/2
builtin::blessed($c) and $c->isa("Logic::Relational::Variable")
builtin::blessed($c) $c->isa("Logic::Relational::Variable")
Truth table33%1/3
A and B
A= builtin::blessed($c)B= $c->isa("Logic::Relational::Variable")
ABresult
0X0
100
111
241 149 ▶ return 1 if $self->_term_contains_var( $h, $c->name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_term_contains_var($h, $c->name) 149 0 149
242 }
243 }
244 }
245 4 return 0;
246 }
247  
248 648 ▶sub _add_clause ( $self, $clause ) {
Subroutine _add_clause: called CC=4 SCAR=13.9
249 648 ▶ $clause->{id} //= "clause-" . $self->{next_clause_id}++;
Condition100%2/2
$clause->{"id"} //= 'clause-' . $self->{"next_clause_id"}++
l!l
25623
MC/DC100%1/1
$clause->{"id"} //= 'clause-' . $self->{"next_clause_id"}++
$clause->{"id"}
Truth table100%2/2
A
A= $clause->{"id"}
Aresult
11
00
250  
251 648 my $key = $clause->head->functor . '/' . $clause->head->arity;
252  
253 # Copy the array for the predicate to avoid modifying existing snapshots
254 648 ▶ my $old_list = $self->{active_predicates}{$key} // [];
Condition100%2/2
$self->{"active_predicates"}{$key} // []
l!l
296352
MC/DC100%1/1
$self->{"active_predicates"}{$key} // []
$self->{"active_predicates"}{$key}
Truth table100%2/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
255 648 my $new_list = [ @$old_list, $clause ];
256  
257 648 $self->{active_predicates}{$key} = $new_list;
258  
259 648 ▶ if ( !$self->{in_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_transaction"}) 645 3 648
260 645 $self->{predicates} = $self->{active_predicates};
261 645 $self->{version}++;
262 }
263 648 $self->_dispatch_change( 'assert', $clause );
264 648 return $clause->id;
265 }
266  
267 6 ▶sub retract_clause ( $self, $id ) {
Subroutine retract_clause: called CC=8 SCAR=20.8
Pod documented
268 6 my $found = 0;
269 6 my $retracted_clause;
270 6 for my $key ( keys %{ $self->{active_predicates} } ) {
271 6 ▶ my $list = $self->{active_predicates}{$key} // [];
Condition50%1/2
$self->{"active_predicates"}{$key} // []
l!l
60
MC/DC0%0/1
$self->{"active_predicates"}{$key} // []
$self->{"active_predicates"}{$key}
Truth table50%1/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
272 6 my @new_list;
273 6 for my $clause (@$list) {
274 13 ▶ if ( $clause->id eq $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause->id eq $id) { } 5 8 13
275 5 $retracted_clause = $clause;
276 5 $found = 1;
277 }
278 else {
279 8 push @new_list, $clause;
280 }
281 }
282 6 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 5 1 6
283 5 $self->{active_predicates}{$key} = \@new_list;
284 5 last;
285 }
286 }
287  
288 6 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 5 1 6
289 5 ▶ if ( !$self->{in_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_transaction"}) 5 0 5
290 5 $self->{predicates} = $self->{active_predicates};
291 5 $self->{version}++;
292 }
293 5 $self->_dispatch_change( 'retract', $retracted_clause );
294 5 return 1;
295 }
296 croak "Clause ID $id does not exist in program version "
297 1 . $self->{version} . "\n";
298 }
299  
300 3 ▶sub retract ( $self, $name, @args ) {
Subroutine retract: called CC=7 SCAR=19.5
Pod documented
301 3 require Logic::Relational::Term;
302 3 require Logic::Relational::Unifier;
303 3 require Logic::Relational::Substitution;
304  
305 3 my $pattern =
306 Logic::Relational::Term->new( functor => $name, args => \@args );
307 3 my $key = $name . '/' . scalar(@args);
308  
309 3 ▶ my $list = $self->{active_predicates}{$key} // [];
Condition50%1/2
$self->{"active_predicates"}{$key} // []
l!l
30
MC/DC0%0/1
$self->{"active_predicates"}{$key} // []
$self->{"active_predicates"}{$key}
Truth table50%1/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
310 3 my $found_idx;
311 3 for my $i ( 0 .. $#$list ) {
312 4 my $clause = $list->[$i];
313 4 my $unifies = 0;
314 4 try {
315 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (Logic::Relational::Unifier::unify($pattern, $clause->head, "Logic::Relational::Substitution"->new)) 3 1 4
316 Logic::Relational::Unifier::unify(
317 $pattern, $clause->head,
318 Logic::Relational::Substitution->new
319 )
320 )
321 {
322 3 $unifies = 1;
323 }
324 }
325 catch ($e) { }
326  
327 4 ▶ if ($unifies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unifies) 3 1 4
328 3 $found_idx = $i;
329 3 last;
330 }
331 }
332  
333 3 ▶ if ( defined $found_idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_idx) 3 0 3
334 3 my $retracted_clause = $list->[$found_idx];
335 3 my @new_list = @$list;
336 3 splice( @new_list, $found_idx, 1 );
337 3 $self->{active_predicates}{$key} = \@new_list;
338 3 ▶ if ( !$self->{in_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_transaction"}) 3 0 3
339 3 $self->{predicates} = $self->{active_predicates};
340 3 $self->{version}++;
341 }
342 3 $self->_dispatch_change( 'retract', $retracted_clause );
343 3 return 1;
344 }
345 0 return 0;
346 }
347  
348 1 ▶sub retract_all ( $self, $name, @args ) {
Subroutine retract_all: called CC=8 SCAR=20.9
Pod documented
349 1 require Logic::Relational::Term;
350 1 require Logic::Relational::Unifier;
351 1 require Logic::Relational::Substitution;
352  
353 1 my $pattern =
354 Logic::Relational::Term->new( functor => $name, args => \@args );
355 1 my $key = $name . '/' . scalar(@args);
356  
357 1 ▶ my $list = $self->{active_predicates}{$key} // [];
Condition50%1/2
$self->{"active_predicates"}{$key} // []
l!l
10
MC/DC0%0/1
$self->{"active_predicates"}{$key} // []
$self->{"active_predicates"}{$key}
Truth table50%1/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
358 1 my @new_list;
359 my @retracted_clauses;
360 1 for my $clause (@$list) {
361 3 my $unifies = 0;
362 3 try {
363 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (Logic::Relational::Unifier::unify($pattern, $clause->head, "Logic::Relational::Substitution"->new)) 2 1 3
364 Logic::Relational::Unifier::unify(
365 $pattern, $clause->head,
366 Logic::Relational::Substitution->new
367 )
368 )
369 {
370 2 $unifies = 1;
371 }
372 }
373 catch ($e) { }
374  
375 3 ▶ if ($unifies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unifies) { } 2 1 3
376 2 push @retracted_clauses, $clause;
377 }
378 else {
379 1 push @new_list, $clause;
380 }
381 }
382  
383 1 ▶ if (@retracted_clauses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@retracted_clauses) 1 0 1
384 1 $self->{active_predicates}{$key} = \@new_list;
385 1 ▶ if ( !$self->{in_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_transaction"}) 1 0 1
386 1 $self->{predicates} = $self->{active_predicates};
387 1 $self->{version}++;
388 }
389 1 for my $clause (@retracted_clauses) {
390 2 $self->_dispatch_change( 'retract', $clause );
391 }
392 }
393 1 return scalar(@retracted_clauses);
394 }
395  
396 10 ▶sub clauses_for ( $self, $name, $arity ) {
Subroutine clauses_for: called CC=2 SCAR=7.1
Pod documented
397 10 my $key = "$name/$arity";
398 10 ▶ my $list = $self->{predicates}{$key} // [];
Condition50%1/2
$self->{"predicates"}{$key} // []
l!l
100
MC/DC0%0/1
$self->{"predicates"}{$key} // []
$self->{"predicates"}{$key}
Truth table50%1/2
A
A= $self->{"predicates"}{$key}
Aresult
11
00
399 10 return @$list;
400 }
401  
402 2 ▶sub on_change ( $self, $callback ) {
Subroutine on_change: called CC=1 SCAR=0.0
Pod undocumented
403 2 push @{ $self->{observers} }, $callback;
404 2 return;
405 }
406  
407 667 ▶sub _dispatch_change ( $self, $operation, $clause ) {
Subroutine _dispatch_change: called CC=4 SCAR=13.9
408 667 ▶ return unless $self->{observers} && @{ $self->{observers} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"observers"} and @{$self->{"observers"};} 662 5 667
Condition66%2/3
$self->{"observers"} and @{$self->{"observers"};}
!ll&&!rl&&r
06625
MC/DC50%1/2
$self->{"observers"} and @{$self->{"observers"};}
$self->{"observers"} @{$self->{"observers"};}
Truth table66%2/3
A and B
A= $self->{"observers"}B= @{$self->{"observers"};}
ABresult
0X0
100
111
409 5 require Logic::Relational::ChangeEvent;
410 5 my $event = Logic::Relational::ChangeEvent->new(
411 operation => $operation,
412 clause => $clause,
413 );
414 5 for my $cb ( @{ $self->{observers} } ) {
415 5 $cb->($event);
416 }
417 5 return;
418 }
419  
420 29 ▶sub generator ( $self, $name, $arity, $code ) {
Subroutine generator: called CC=1 SCAR=0.0
Pod undocumented
421 29 my $key = "$name/$arity";
422 29 $self->{generators}{$key} = $code;
423 29 $self->{version}++;
424 29 return;
425 }
426  
427 111 ▶sub query ( $self, $goal, %options ) {
Subroutine query: called CC=1 SCAR=0.0
Pod documented
428 111 $self->cleanup_expired_facts;
429 111 require Logic::Relational::Query;
430 return Logic::Relational::Query->new(
431 program => $self,
432 goals => [$goal],
433 111 predicates => { %{ $self->{predicates} } }, # snapshot shallow copy
434 111 generators => { %{ $self->{generators} } }, # snapshot generators
435 %options,
436 );
437 }
438  
439 1 ▶sub on_expire ( $self, $name, $code ) {
Subroutine on_expire: called CC=2 SCAR=7.0
Pod undocumented
440 1 ▶ croak "code callback is required" unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 1 1
441 1 push @{ $self->{on_expire_handlers}{$name} }, $code;
442 1 return;
443 }
444  
445 12 ▶sub assert_fact ( $self, %args ) {
Subroutine assert_fact: called CC=11 SCAR=25.7
Pod undocumented
446 12 ▶ my $term = $args{term} // croak "term is required";
Condition50%1/2
$args{"term"} // croak("term is required")
l!l
120
MC/DC0%0/1
$args{"term"} // croak("term is required")
$args{"term"}
Truth table50%1/2
A
A= $args{"term"}
Aresult
11
00
447 12 my $expires_at = $args{expires_at};
448 12 ▶ if ( my $expires_in = $args{expires_in} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires_in = $args{"expires_in"}) 9 3 12
449 9 $expires_at = time + $expires_in;
450 }
451  
452 12 my $expire_to = $args{expire_to};
453 12 ▶ if ( ref($expire_to) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expire_to eq 'ARRAY') { } 2 10 12
elsif (ref $expire_to eq 'Logic::Relational::Goal::Call') { } 0 10 10
454 2 my ( $e_name, @e_args ) = @$expire_to;
455 2 require Logic::Relational::Term;
456 2 $expire_to = Logic::Relational::Term->new(
457 functor => $e_name,
458 args => \@e_args
459 );
460 }
461 elsif ( ref($expire_to) eq 'Logic::Relational::Goal::Call' ) {
462 0 require Logic::Relational::Term;
463 0 $expire_to = Logic::Relational::Term->new(
464 functor => $expire_to->name,
465 args => $expire_to->args
466 );
467 }
468  
469 12 my %meta;
470 12 ▶ $meta{expires_at} = $expires_at if defined $expires_at;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires_at 12 0 12
471 12 ▶ $meta{expire_to} = $expire_to if defined $expire_to;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expire_to 2 10 12
472 12 ▶ $meta{on_expire} = $args{on_expire} if ref( $args{on_expire} ) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"on_expire"} eq "CODE" 2 10 12
473  
474 12 my $head;
475 12 ▶ if ( ref($term) eq 'Logic::Relational::Goal::Call' ) { ## no critic (ProhibitCascadingIfElse)
Branch: 3 branches
BranchTrueFalseTotal
if (ref $term eq 'Logic::Relational::Goal::Call') { } 0 12 12
elsif (ref $term eq 'Logic::Relational::Term') { } 0 12 12
elsif (ref $term eq 'ARRAY') { } 12 0 12
476 0 require Logic::Relational::Term;
477 0 $head = Logic::Relational::Term->new(
478 functor => $term->name,
479 args => $term->args
480 );
481 }
482 elsif ( ref($term) eq 'Logic::Relational::Term' ) {
483 0 $head = $term;
484 }
485 elsif ( ref($term) eq 'ARRAY' ) {
486 12 my ( $name, @args_list ) = @$term;
487 12 require Logic::Relational::Term;
488 12 $head = Logic::Relational::Term->new(
489 functor => $name,
490 args => \@args_list
491 );
492 }
493 else {
494 0 croak
495 "term must be a Logic::Relational::Term, Call goal, or array reference";
496 }
497  
498 12 require Logic::Relational::Clause;
499 12 my $clause = Logic::Relational::Clause->new(
500 head => $head,
501 metadata => \%meta,
502 );
503  
504 12 return $self->_add_clause($clause);
505 }
506  
507 2 ▶sub with_facts ( $self, $facts, $code ) {
Subroutine with_facts: called CC=10 SCAR=26.0
Pod undocumented
508 2 ▶ croak "facts must be an array reference" unless ref($facts) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $facts eq "ARRAY" 0 2 2
509 2 ▶ croak "code must be a code reference" unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 2 2
510  
511 2 my @clause_ids;
512 2 try {
513 2 for my $fact (@$facts) {
514 3 my $clause_id;
515 3 ▶ if ( ref($fact) eq 'Logic::Relational::Clause' ) { ## no critic (ProhibitCascadingIfElse)
Branch: 4 branches
BranchTrueFalseTotal
if (ref $fact eq 'Logic::Relational::Clause') { } 0 3 3
elsif (ref $fact eq 'Logic::Relational::Goal::Call') { } 0 3 3
elsif (ref $fact eq 'Logic::Relational::Term') { } 0 3 3
elsif (ref $fact eq 'ARRAY') { } 3 0 3
516 0 $clause_id = $self->_add_clause($fact);
517 }
518 elsif ( ref($fact) eq 'Logic::Relational::Goal::Call' ) {
519 0 $clause_id = $self->fact( $fact->name, @{ $fact->args } );
520 }
521 elsif ( ref($fact) eq 'Logic::Relational::Term' ) {
522 0 $clause_id = $self->fact( $fact->functor, @{ $fact->args } );
523 }
524 elsif ( ref($fact) eq 'ARRAY' ) {
525 3 my ( $name, @args ) = @$fact;
526 3 $clause_id = $self->fact( $name, @args );
527 }
528 else {
529 0 croak "Invalid fact definition in with_facts: " . ref($fact);
530 }
531 3 ▶ push @clause_ids, $clause_id if defined $clause_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $clause_id 3 0 3
532 }
533  
534 28 ▶ use feature 'defer';
Subroutine BEGIN: called CC=1 SCAR=0.0
535 28 ▶ no warnings 'experimental::defer'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
536 2 defer {
537 2 for my $id (@clause_ids) {
538 3 try { $self->retract_clause($id); } catch ($e) {
539 }
540 }
541 }
542  
543 2 return $code->();
544 }
545 catch ($e) {
546 1 die $e; ## no critic (RequireCarping, ProhibitStringyDeath)
547 }
548 0 return;
549 }
550  
551 9 ▶sub _handle_clause_expiration ( $self, $clause ) {
Subroutine _handle_clause_expiration: called CC=9 SCAR=22.7
552 9 my $meta = $clause->metadata;
553  
554 # Assert expire_to mutation before firing callbacks so queries inside callbacks see updated state
555 9 ▶ if ( my $expire_to = $meta->{expire_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expire_to = $meta->{"expire_to"}) 1 8 9
556 1 ▶ if ( ref($expire_to) eq 'Logic::Relational::Term' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $expire_to eq 'Logic::Relational::Term') { } 1 0 1
elsif (ref $expire_to eq 'ARRAY') { } 0 0 0
557 1 $self->fact( $expire_to->functor, @{ $expire_to->args } );
558 }
559 elsif ( ref($expire_to) eq 'ARRAY' ) {
560 0 my ( $fn, @fa ) = @$expire_to;
561 0 $self->fact( $fn, @fa );
562 }
563 }
564  
565 # 1. Per-clause on_expire callback
566 9 ▶ if ( my $cb = $meta->{on_expire} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $meta->{"on_expire"}) 2 7 9
567 2 ▶ if ( ref($cb) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq "CODE") 2 0 2
568 2 try { $cb->( $clause, $self ); } catch ($e) {
569 }
570 }
571 }
572  
573 # 2. Program-level on_expire handlers
574 9 my $pred_name = $clause->head->functor;
575 9 my $pred_key = $pred_name . '/' . $clause->head->arity;
576 my @handlers = (
577 9 ▶ @{ $self->{on_expire_handlers}{$pred_name} // [] },
Condition100%2/2
$self->{"on_expire_handlers"}{$pred_name} // []
l!l
27
MC/DC100%1/1
$self->{"on_expire_handlers"}{$pred_name} // []
$self->{"on_expire_handlers"}{$pred_name}
Truth table100%2/2
A
A= $self->{"on_expire_handlers"}{$pred_name}
Aresult
11
00
578 9 ▶ @{ $self->{on_expire_handlers}{$pred_key} // [] }
Condition50%1/2
$self->{"on_expire_handlers"}{$pred_key} // []
l!l
09
MC/DC0%0/1
$self->{"on_expire_handlers"}{$pred_key} // []
$self->{"on_expire_handlers"}{$pred_key}
Truth table50%1/2
A
A= $self->{"on_expire_handlers"}{$pred_key}
Aresult
11
00
579 );
580 9 for my $handler (@handlers) {
581 2 try { $handler->( $clause, $self ); } catch ($e) {
582 }
583 }
584  
585 9 return $meta->{expire_to};
586 }
587  
588 121 ▶sub cleanup_expired_facts ($self) {
Subroutine cleanup_expired_facts: called CC=11 SCAR=24.0
Pod undocumented
589 121 ▶ return 0 if $self->{in_cleanup};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_cleanup"} 1 120 121
590  
591 120 $self->{in_cleanup} = 1;
592 28 ▶ use feature 'defer';
Subroutine BEGIN: called CC=1 SCAR=0.0
593 28 ▶ no warnings 'experimental::defer'; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
594 120 defer { $self->{in_cleanup} = 0; }
595  
596 120 my $now = time;
597 120 my $count = 0;
598 120 my @expired_clauses;
599  
600 120 for my $key ( keys %{ $self->{active_predicates} } ) {
601 709 ▶ my $list = $self->{active_predicates}{$key} // [];
Condition50%1/2
$self->{"active_predicates"}{$key} // []
l!l
7090
MC/DC0%0/1
$self->{"active_predicates"}{$key} // []
$self->{"active_predicates"}{$key}
Truth table50%1/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
602 709 my @new_list;
603 709 for my $clause (@$list) {
604 1233 my $meta = $clause->metadata;
605 1233 ▶ if ( exists $meta->{expires_at}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $meta->{'expires_at'} and defined $meta->{'expires_at'} and $now >= $meta->{'expires_at'}) { } 9 1224 1233
Condition83%5/6
exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
!ll&&!rl&&r
121689
exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
!ll&&!rl&&r
1216017
MC/DC66%2/3
exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
exists $meta->{"expires_at"} defined $meta->{"expires_at"} $now >= $meta->{"expires_at"}
Truth table75%3/4
A and B and C
A= exists $meta->{"expires_at"}B= defined $meta->{"expires_at"}C= $now >= $meta->{"expires_at"}
ABCresult
0XX0
10X0
1100
1111
606 && defined $meta->{expires_at}
607 && $now >= $meta->{expires_at} )
608 {
609 9 $count++;
610 9 push @expired_clauses, $clause;
611 9 $self->_dispatch_change( 'retract', $clause );
612 }
613 else {
614 1224 push @new_list, $clause;
615 }
616 }
617 709 $self->{active_predicates}{$key} = \@new_list;
618 }
619  
620 120 ▶ if ( $count && !$self->{in_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count and not $self->{"in_transaction"}) 8 112 120
Condition66%2/3
$count and not $self->{"in_transaction"}
!ll&&!rl&&r
11208
MC/DC50%1/2
$count and not $self->{"in_transaction"}
$count not $self->{"in_transaction"}
Truth table66%2/3
A and not(B)
A= $countB= not $self->{"in_transaction"}
ABresult
0X0
100
111
621 8 $self->{predicates} = $self->{active_predicates};
622 8 $self->{version}++;
623 }
624  
625 120 for my $clause (@expired_clauses) {
626 9 $self->_handle_clause_expiration($clause);
627 }
628  
629 120 return $count;
630 }
631  
632 2 ▶sub transaction ( $self, $code ) {
Subroutine transaction: called CC=2 SCAR=7.0
Pod documented
633 2 ▶ if ( $self->{in_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_transaction"}) 0 2 2
634 0 return $code->();
635 }
636  
637 2 $self->{in_transaction} = 1;
638 2 $self->{active_predicates} = { %{ $self->{predicates} } };
639  
640 2 try {
641 2 my $res = $code->();
642 1 $self->{predicates} = $self->{active_predicates};
643 1 $self->{version}++;
644 1 $self->{in_transaction} = 0;
645 1 return $res;
646 }
647 catch ($e) {
648 1 $self->{active_predicates} = $self->{predicates};
649 1 $self->{in_transaction} = 0;
650 1 die $e; ## no critic (RequireCarping, ProhibitStringyDeath)
651 }
652 0 return;
653 }
654  
655 6 ▶sub save_snapshot ( $self, $target, %options ) {
Subroutine save_snapshot: called CC=11 SCAR=25.2
Pod undocumented
656 6 require Logic::Relational::Serializer;
657  
658 6 $self->cleanup_expired_facts;
659  
660 6 my $now = time;
661 6 my @encoded_facts;
662 6 ▶ my $pred_map = $self->{predicates} // {};
Condition50%1/2
$self->{"predicates"} // {}
l!l
60
MC/DC0%0/1
$self->{"predicates"} // {}
$self->{"predicates"}
Truth table50%1/2
A
A= $self->{"predicates"}
Aresult
11
00
663 6 for my $key ( sort keys %$pred_map ) {
664 25 ▶ my $list = $pred_map->{$key} // [];
Condition50%1/2
$pred_map->{$key} // []
l!l
250
MC/DC0%0/1
$pred_map->{$key} // []
$pred_map->{$key}
Truth table50%1/2
A
A= $pred_map->{$key}
Aresult
11
00
665 25 for my $clause (@$list) {
666  
667 # Only serialize facts (clauses with True body or no body)
668 35 my $body_ref = ref( $clause->body );
669 35 ▶ if ( !$body_ref || $body_ref eq 'Logic::Relational::Goal::True' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $body_ref or $body_ref eq "Logic::Relational::Goal::True") 19 16 35
Condition66%2/3
not $body_ref or $body_ref eq "Logic::Relational::Goal::True"
l!l&&r!l&&!r
01916
MC/DC50%1/2
not $body_ref or $body_ref eq "Logic::Relational::Goal::True"
not $body_ref $body_ref eq "Logic::Relational::Goal::True"
Truth table66%2/3
not(A) or B
A= not $body_refB= $body_ref eq "Logic::Relational::Goal::True"
ABresult
1X1
011
000
670 19 push @encoded_facts,
671 Logic::Relational::Serializer::encode_clause( $clause, $now );
672 }
673 }
674 }
675  
676 my $snapshot_data = {
677 metadata => {
678 format_version => 1,
679 engine => 'Logic::Relational',
680 exported_at => $now,
681 program_version => $self->{version},
682 6 known_predicates => [ sort keys %$pred_map ],
683 },
684 facts => \@encoded_facts,
685 };
686  
687 6 my $json_str =
688 Logic::Relational::Serializer::to_json( $snapshot_data, %options );
689  
690 6 ▶ if ( ref($target) eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $target eq 'SCALAR') { } 5 1 6
elsif (ref $target or &openhandle($target)) { } 0 1 1
691 5 $$target = $json_str;
692 }
693 ▶ elsif ( ref($target) || openhandle($target) ) {
Condition33%1/3
ref $target or &openhandle($target)
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $target or &openhandle($target)
ref $target &openhandle($target)
Truth table33%1/3
A or B
A= ref $targetB= &openhandle($target)
ABresult
1X1
011
000
694 0 print {$target} $json_str;
695 }
696 else {
697 1 ▶ open my $fh, '>:encoding(UTF-8)', $target
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $target or croak("Cannot write snapshot to file '${target}': $!") 1 0 1
698 or croak "Cannot write snapshot to file '$target': $!";
699 1 print {$fh} $json_str;
700 1 close $fh;
701 }
702  
703 6 return $json_str;
704 }
705  
706 8 ▶sub load_snapshot ( $self, $source, %options ) { ## no critic (Subroutines::ProhibitExcessComplexity)
Subroutine load_snapshot: called CC=36 SCAR=43.9
Pod undocumented
707 8 require Logic::Relational::Serializer;
708  
709 8 my $json_str;
710 8 ▶ if ( ref($source) eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source eq 'SCALAR') { } 7 1 8
elsif (ref $source or &openhandle($source)) { } 0 1 1
711 7 $json_str = $$source;
712 }
713 ▶ elsif ( ref($source) || openhandle($source) ) {
Condition33%1/3
ref $source or &openhandle($source)
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $source or &openhandle($source)
ref $source &openhandle($source)
Truth table33%1/3
A or B
A= ref $sourceB= &openhandle($source)
ABresult
1X1
011
000
714 0 local $/ = undef;
715 0 $json_str = <$source>;
716 }
717 else {
718 1 ▶ open my $fh, '<:encoding(UTF-8)', $source
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $source or croak("Cannot read snapshot from file '${source}': $!") 1 0 1
719 or croak "Cannot read snapshot from file '$source': $!";
720 1 local $/ = undef;
721 1 $json_str = <$fh>;
722 1 close $fh;
723 }
724  
725 8 my $snapshot_data = Logic::Relational::Serializer::from_json($json_str);
726 croak "Invalid snapshot file format"
727 unless ref($snapshot_data) eq 'HASH'
728 && exists $snapshot_data->{metadata}
729 8 ▶ && exists $snapshot_data->{facts};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $snapshot_data eq "HASH" and exists $snapshot_data->{"metadata"} and exists $snapshot_data->{"facts"} 0 8 8
Condition33%2/6
ref $snapshot_data eq "HASH" and exists $snapshot_data->{"metadata"} and exists $snapshot_data->{"facts"}
!ll&&!rl&&r
008
ref $snapshot_data eq "HASH" and exists $snapshot_data->{"metadata"}
!ll&&!rl&&r
008
MC/DC0%0/3
ref $snapshot_data eq "HASH" and exists $snapshot_data->{"metadata"} and exists $snapshot_data->{"facts"}
ref $snapshot_data eq "HASH" exists $snapshot_data->{"metadata"} exists $snapshot_data->{"facts"}
Truth table25%1/4
A and B and C
A= ref $snapshot_data eq "HASH"B= exists $snapshot_data->{"metadata"}C= exists $snapshot_data->{"facts"}
ABCresult
0XX0
10X0
1100
1111
730  
731 8 ▶ my $mode = $options{mode} // 'replace';
Condition100%2/2
$options{"mode"} // "replace"
l!l
35
MC/DC100%1/1
$options{"mode"} // "replace"
$options{"mode"}
Truth table100%2/2
A
A= $options{"mode"}
Aresult
11
00
732 8 ▶ my $filter_expired = $options{filter_expired} // 1;
Condition50%1/2
$options{"filter_expired"} // 1
l!l
08
MC/DC0%0/1
$options{"filter_expired"} // 1
$options{"filter_expired"}
Truth table50%1/2
A
A= $options{"filter_expired"}
Aresult
11
00
733 my $ttl_mode = $options{ttl_mode}
734 8 ▶ // ( $options{pause_ttl} ? 'relative' : 'absolute' );
Branch: 1 branch
BranchTrueFalseTotal
$options{'pause_ttl'} ? : 0 6 6
Condition100%2/2
$options{"ttl_mode"} // ($options{"pause_ttl"} ? "relative" : "absolute")
l!l
26
MC/DC100%1/1
$options{"ttl_mode"} // ($options{"pause_ttl"} ? "relative" : "absolute")
$options{"ttl_mode"}
Truth table100%2/2
A
A= $options{"ttl_mode"}
Aresult
11
00
735 8 my $now = time;
736  
737 8 ▶ if ( $mode eq 'replace' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "replace") 7 1 8
738 7 my %new_active;
739 7 for my $key ( keys %{ $self->{active_predicates} } ) {
740 19 ▶ my $list = $self->{active_predicates}{$key} // [];
Condition50%1/2
$self->{"active_predicates"}{$key} // []
l!l
190
MC/DC0%0/1
$self->{"active_predicates"}{$key} // []
$self->{"active_predicates"}{$key}
Truth table50%1/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
741 19 my @rules_only;
742 19 for my $clause (@$list) {
743 30 my $body_ref = ref( $clause->body );
744 30 ▶ if ( $body_ref && $body_ref ne 'Logic::Relational::Goal::True' )
Branch: 1 branch
BranchTrueFalseTotal
if ($body_ref and $body_ref ne "Logic::Relational::Goal::True") 16 14 30
Condition66%2/3
$body_ref and $body_ref ne "Logic::Relational::Goal::True"
!ll&&!rl&&r
01416
MC/DC50%1/2
$body_ref and $body_ref ne "Logic::Relational::Goal::True"
$body_ref $body_ref ne "Logic::Relational::Goal::True"
Truth table66%2/3
A and B
A= $body_refB= $body_ref ne "Logic::Relational::Goal::True"
ABresult
0X0
100
111
745 {
746 16 push @rules_only, $clause;
747 }
748 }
749 19 $new_active{$key} = \@rules_only;
750 }
751 7 $self->{active_predicates} = \%new_active;
752 7 $self->{predicates} = \%new_active;
753 }
754  
755 8 ▶ if ( my $kp = $snapshot_data->{metadata}{known_predicates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kp = $snapshot_data->{"metadata"}{"known_predicates"}) 8 0 8
756 8 ▶ if ( ref($kp) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $kp eq "ARRAY") 8 0 8
757 8 for my $key (@$kp) {
758 31 ▶ $self->{active_predicates}{$key} //= [];
Condition100%2/2
$self->{"active_predicates"}{$key} //= []
l!l
229
MC/DC100%1/1
$self->{"active_predicates"}{$key} //= []
$self->{"active_predicates"}{$key}
Truth table100%2/2
A
A= $self->{"active_predicates"}{$key}
Aresult
11
00
759 }
760 }
761 }
762  
763 8 my $loaded_count = 0;
764 8 ▶ for my $fact_data ( @{ $snapshot_data->{facts} // [] } ) {
Condition50%1/2
$snapshot_data->{"facts"} // []
l!l
80
MC/DC0%0/1
$snapshot_data->{"facts"} // []
$snapshot_data->{"facts"}
Truth table50%1/2
A
A= $snapshot_data->{"facts"}
Aresult
11
00
765 25 my $clause = Logic::Relational::Serializer::decode_clause($fact_data);
766 25 my $meta = $clause->metadata;
767  
768 25 ▶ if ( $ttl_mode eq 'relative' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttl_mode eq 'relative') { } 2 23 25
elsif ($filter_expired and exists $meta->{'expires_at'} and defined $meta->{'expires_at'} and $now >= $meta->{'expires_at'}) { } 0 45 45
769 2 ▶ if ( exists $meta->{remaining_ttl}
Branch: 2 branches
BranchTrueFalseTotal
if (exists $meta->{'remaining_ttl'} and defined $meta->{'remaining_ttl'}) { } 2 0 2
elsif ($filter_expired and exists $meta->{'expires_at'} and defined $meta->{'expires_at'} and $now >= $meta->{'expires_at'}) { } 0 0 0
Condition33%1/3
exists $meta->{"remaining_ttl"} and defined $meta->{"remaining_ttl"}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $meta->{"remaining_ttl"} and defined $meta->{"remaining_ttl"}
exists $meta->{"remaining_ttl"} defined $meta->{"remaining_ttl"}
Truth table33%1/3
A and B
A= exists $meta->{"remaining_ttl"}B= defined $meta->{"remaining_ttl"}
ABresult
0X0
100
111
770 && defined $meta->{remaining_ttl} )
771 {
772 2 my $rem = $meta->{remaining_ttl};
773 2 ▶ if ( $rem <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rem <= 0) 0 2 2
774 0 next; # Expired when saved
775 }
776 2 $meta->{expires_at} = $now + $rem;
777 }
778 ▶ elsif ( $filter_expired
Condition0%0/9
$filter_expired and exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
!ll&&!rl&&r
000
$filter_expired and exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
!ll&&!rl&&r
000
$filter_expired and exists $meta->{"expires_at"}
!ll&&!rl&&r
000
MC/DC0%0/4
$filter_expired and exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
$filter_expired exists $meta->{"expires_at"} defined $meta->{"expires_at"} $now >= $meta->{"expires_at"}
Truth table0%0/5
A and B and C and D
A= $filter_expiredB= exists $meta->{"expires_at"}C= defined $meta->{"expires_at"}D= $now >= $meta->{"expires_at"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
779 && exists $meta->{expires_at}
780 && defined $meta->{expires_at}
781 && $now >= $meta->{expires_at} )
782 {
783 0 next;
784 }
785 }
786 ▶ elsif ( $filter_expired
Condition66%6/9
$filter_expired and exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
!ll&&!rl&&r
2210
$filter_expired and exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
!ll&&!rl&&r
2201
$filter_expired and exists $meta->{"expires_at"}
!ll&&!rl&&r
0221
MC/DC0%0/4
$filter_expired and exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
$filter_expired exists $meta->{"expires_at"} defined $meta->{"expires_at"} $now >= $meta->{"expires_at"}
Truth table40%2/5
A and B and C and D
A= $filter_expiredB= exists $meta->{"expires_at"}C= defined $meta->{"expires_at"}D= $now >= $meta->{"expires_at"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
787 && exists $meta->{expires_at}
788 && defined $meta->{expires_at}
789 && $now >= $meta->{expires_at} )
790 {
791 0 next; # Skip expired fact
792 }
793  
794 25 $self->_add_clause($clause);
795 25 $loaded_count++;
796 }
797  
798 8 $self->{version}++;
799 8 return $loaded_count;
800 }
801  
802 5 ▶sub table ( $self, $name, $arity = undef ) {
Subroutine table: called CC=4 SCAR=13.9
Pod documented
803 5 ▶ if ( $name =~ m{\A([a-zA-Z_]\w*)/(\d+|\*)\z}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[\A([a-zA-Z_]\w*)/(\d+|\*)\z]ux) 1 4 5
804 1 ( $name, $arity ) = ( $1, $2 );
805 }
806 5 ▶ my $key = defined $arity ? "$name/$arity" : "$name/*";
Branch: 1 branch
BranchTrueFalseTotal
defined $arity ? : 5 0 5
807 5 $self->{tabled}{$key} = 1;
808 5 return $key;
809 }
810  
811 0 ▶sub untable ( $self, $name, $arity = undef ) {
Subroutine untable: not called CC=6 SCAR=37.4
Pod documented
812 0 ▶ if ( $name =~ m{\A([a-zA-Z_]\w*)/(\d+|\*)\z}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[\A([a-zA-Z_]\w*)/(\d+|\*)\z]ux) 0 0 0
813 0 ( $name, $arity ) = ( $1, $2 );
814 }
815 0 ▶ if ( defined $arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arity) { } 0 0 0
816 0 delete $self->{tabled}{"$name/$arity"};
817 }
818 else {
819 0 delete $self->{tabled}{"$name/*"};
820 0 for my $k ( keys %{ $self->{tabled} } ) {
821 0 ▶ delete $self->{tabled}{$k} if $k =~ m{\A\Q$name\E/}x;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ m[\A\Q$name\E/]ux 0 0 0
822 }
823 }
824 0 return 1;
825 }
826  
827 324 ▶sub is_tabled ( $self, $name, $arity = undef ) {
Subroutine is_tabled: called CC=9 SCAR=30.2
Pod documented
828 324 ▶ if ( $name =~ m{\A([a-zA-Z_]\w*)/(\d+|\*)\z}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[\A([a-zA-Z_]\w*)/(\d+|\*)\z]ux) 0 324 324
829 0 ( $name, $arity ) = ( $1, $2 );
830 }
831 324 ▶ if ( defined $arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arity) { } 324 0 324
832 return 1
833 324 ▶ if $self->{tabled}{"$name/$arity"} || $self->{tabled}{"$name/*"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tabled"}{"$name/$arity"} or $self->{"tabled"}{"$name/*"} 27 297 324
Condition66%2/3
$self->{"tabled"}{"$name/$arity"} or $self->{"tabled"}{"$name/*"}
l!l&&r!l&&!r
270297
MC/DC50%1/2
$self->{"tabled"}{"$name/$arity"} or $self->{"tabled"}{"$name/*"}
$self->{"tabled"}{"$name/$arity"} $self->{"tabled"}{"$name/*"}
Truth table66%2/3
A or B
A= $self->{"tabled"}{"$name/$arity"}B= $self->{"tabled"}{"$name/*"}
ABresult
1X1
011
000
834 }
835 else {
836 0 ▶ return 1 if $self->{tabled}{"$name/*"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tabled"}{"$name/*"} 0 0 0
837 0 for my $k ( keys %{ $self->{tabled} } ) {
838 0 ▶ return 1 if $k =~ m{\A\Q$name\E/}x;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ m[\A\Q$name\E/]ux 0 0 0
839 }
840 }
841 297 return 0;
842 }
843  
844 28 1;
845  
« blib/lib/Logic/Relational/Goal.pm ↑ Summary blib/lib/Logic/Relational/Goal/ConstraintFD.pm »