lib/Logic/Relational/Serializer.pm

filter: Statementstmt 94.0% 110 / 117 Branchbran 77.7% 42 / 54 Conditioncond 45.8% 11 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 6 totaltotal 78.2% 180 / 230 CC 41 SCAR 23.7
CC
41
Cov
84%
decode_term
20.0
encode_term
10.3
decode_clause
9.6
CRAP 10.7
« lib/Logic/Relational/Query.pm ↑ Summary lib/Logic/Relational/Substitution.pm »
1 package Logic::Relational::Serializer;
2  
3 2 ▶use v5.38; ## no critic (ValuesAndExpressions::ProhibitVersionStrings)
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Logic::Relational::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Logic::Relational::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Logic::Relational::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Logic::Relational::Clause;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Logic::Relational::Serializer - Serializes and deserializes knowledge base snapshots.
16  
17 =cut
18  
19 56 ▶sub encode_term ($term) {
Subroutine encode_term: called CC=10 SCAR=23.3
Pod undocumented
20 56 ▶ if ( !ref($term) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $term) 28 28 56
21 28 return { type => 'scalar', value => $term };
22 }
23  
24 28 my $ref = ref($term);
25 28 ▶ if ( $ref eq 'Logic::Relational::Atom' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Logic::Relational::Atom") 0 28 28
26 0 return { type => 'atom', name => $term->name };
27 }
28 28 ▶ if ( $ref eq 'Logic::Relational::Variable' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Logic::Relational::Variable") 12 16 28
29 12 return { type => 'variable', name => $term->name };
30 }
31 16 ▶ if ( $ref eq 'Logic::Relational::Term' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Logic::Relational::Term") 1 15 16
32 1 my @encoded_args = map { encode_term($_) } @{ $term->args };
33 return {
34 1 type => 'term',
35 functor => $term->functor,
36 args => \@encoded_args,
37 };
38 }
39 15 ▶ if ( $ref eq 'Logic::Relational::ArrayRest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Logic::Relational::ArrayRest") 2 13 15
40 2 return { type => 'array_rest', term => encode_term( $term->term ) };
41 }
42 13 ▶ if ( $ref eq 'Logic::Relational::Domain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Logic::Relational::Domain") 0 13 13
43 0 return { type => 'domain', values => [ $term->values ] };
44 }
45 13 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 11 2 13
46 11 my @encoded_elements = map { encode_term($_) } @$term;
47 11 return { type => 'array', elements => \@encoded_elements };
48 }
49 2 ▶ if ( $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 2 0 2
50 2 my %encoded_pairs;
51 2 for my $k ( keys %$term ) {
52 4 $encoded_pairs{$k} = encode_term( $term->{$k} );
53 }
54 2 return { type => 'hash', pairs => \%encoded_pairs };
55 }
56  
57 0 croak "Cannot serialize unhandled term type: $ref";
58 }
59  
60 76 ▶sub decode_term ($data) {
Subroutine decode_term: called CC=16 SCAR=30.0
Pod undocumented
61 croak "Invalid term encoding"
62 76 ▶ unless ref($data) eq 'HASH' && exists $data->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and exists $data->{"type"} 0 76 76
Condition33%1/3
ref $data eq "HASH" and exists $data->{"type"}
!ll&&!rl&&r
0076
MC/DC0%0/2
ref $data eq "HASH" and exists $data->{"type"}
ref $data eq "HASH" exists $data->{"type"}
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= exists $data->{"type"}
ABresult
0X0
100
111
63 76 my $type = $data->{type};
64  
65 76 ▶ if ( $type eq 'scalar' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "scalar") 44 32 76
66 44 return $data->{value};
67 }
68 32 ▶ if ( $type eq 'atom' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "atom") 0 32 32
69 0 return Logic::Relational::Atom->new( $data->{name} );
70 }
71 32 ▶ if ( $type eq 'variable' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "variable") 12 20 32
72 12 return Logic::Relational::Variable->new( name => $data->{name} );
73 }
74 20 ▶ if ( $type eq 'term' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "term") 1 19 20
75 1 ▶ my @decoded_args = map { decode_term($_) } @{ $data->{args} // [] };
Condition50%1/2
$data->{"args"} // []
l!l
10
MC/DC0%0/1
$data->{"args"} // []
$data->{"args"}
Truth table50%1/2
A
A= $data->{"args"}
Aresult
11
00
76 return Logic::Relational::Term->new(
77 functor => $data->{functor},
78 1 args => \@decoded_args,
79 );
80 }
81 19 ▶ if ( $type eq 'array_rest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array_rest") 2 17 19
82 2 require Logic::Relational::ArrayRest;
83 return Logic::Relational::ArrayRest->new(
84 2 decode_term( $data->{term} ) );
85 }
86 17 ▶ if ( $type eq 'domain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "domain") 0 17 17
87 0 require Logic::Relational::Domain;
88 return Logic::Relational::Domain->new(
89 0 ▶ { values => $data->{values} // [] } );
Condition0%0/2
$data->{"values"} // []
l!l
00
MC/DC0%0/1
$data->{"values"} // []
$data->{"values"}
Truth table0%0/2
A
A= $data->{"values"}
Aresult
11
00
90 }
91 17 ▶ if ( $type eq 'array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array") 13 4 17
92 my @decoded_elements =
93 13 ▶ map { decode_term($_) } @{ $data->{elements} // [] };
Condition50%1/2
$data->{"elements"} // []
l!l
130
MC/DC0%0/1
$data->{"elements"} // []
$data->{"elements"}
Truth table50%1/2
A
A= $data->{"elements"}
Aresult
11
00
94 13 return \@decoded_elements;
95 }
96 4 ▶ if ( $type eq 'hash' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "hash") 4 0 4
97 4 my %decoded_pairs;
98 4 ▶ my $pairs = $data->{pairs} // {};
Condition50%1/2
$data->{"pairs"} // {}
l!l
40
MC/DC0%0/1
$data->{"pairs"} // {}
$data->{"pairs"}
Truth table50%1/2
A
A= $data->{"pairs"}
Aresult
11
00
99 4 for my $k ( keys %$pairs ) {
100 8 $decoded_pairs{$k} = decode_term( $pairs->{$k} );
101 }
102 4 return \%decoded_pairs;
103 }
104  
105 0 croak "Unknown term encoding type: $type";
106 }
107  
108 19 ▶sub encode_clause ( $clause, $now = time ) {
Subroutine encode_clause: called CC=8 SCAR=21.0
Pod undocumented
109 19 my $head_term = $clause->head;
110 19 my @encoded_args = map { encode_term($_) } @{ $head_term->args };
111  
112 19 my %data = (
113 id => $clause->id,
114 predicate => $head_term->functor,
115 arity => $head_term->arity,
116 args => \@encoded_args,
117 );
118  
119 19 ▶ if ( my $meta = $clause->metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = $clause->metadata) 19 0 19
120 19 ▶ $data{created_at} = $meta->{created_at} if exists $meta->{created_at};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{"created_at"} 0 19 19
121 19 ▶ if ( exists $meta->{expires_at} && defined $meta->{expires_at} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $meta->{"expires_at"} and defined $meta->{"expires_at"}) 3 16 19
Condition66%2/3
exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
!ll&&!rl&&r
1603
MC/DC50%1/2
exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
exists $meta->{"expires_at"} defined $meta->{"expires_at"}
Truth table66%2/3
A and B
A= exists $meta->{"expires_at"}B= defined $meta->{"expires_at"}
ABresult
0X0
100
111
122 3 $data{expires_at} = $meta->{expires_at};
123 3 my $rem = $meta->{expires_at} - $now;
124 3 ▶ $data{remaining_ttl} = $rem > 0 ? $rem : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rem > 0 ? : 3 0 3
125 }
126 19 ▶ if ( my $expire_to = $meta->{expire_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expire_to = $meta->{"expire_to"}) 1 18 19
127 1 $data{expire_to} = encode_term($expire_to);
128 }
129 }
130  
131 19 return \%data;
132 }
133  
134 25 ▶sub decode_clause ($data) {
Subroutine decode_clause: called CC=9 SCAR=22.7
Pod undocumented
135 croak "Invalid clause encoding"
136 25 ▶ unless ref($data) eq 'HASH' && exists $data->{predicate};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and exists $data->{"predicate"} 0 25 25
Condition33%1/3
ref $data eq "HASH" and exists $data->{"predicate"}
!ll&&!rl&&r
0025
MC/DC0%0/2
ref $data eq "HASH" and exists $data->{"predicate"}
ref $data eq "HASH" exists $data->{"predicate"}
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= exists $data->{"predicate"}
ABresult
0X0
100
111
137  
138 25 ▶ my @decoded_args = map { decode_term($_) } @{ $data->{args} // [] };
Condition50%1/2
$data->{"args"} // []
l!l
250
MC/DC0%0/1
$data->{"args"} // []
$data->{"args"}
Truth table50%1/2
A
A= $data->{"args"}
Aresult
11
00
139 my $head = Logic::Relational::Term->new(
140 functor => $data->{predicate},
141 25 args => \@decoded_args,
142 );
143  
144 25 my %meta;
145 25 ▶ $meta{created_at} = $data->{created_at} if exists $data->{created_at};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"created_at"} 0 25 25
146 25 ▶ $meta{expires_at} = $data->{expires_at} if exists $data->{expires_at};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"expires_at"} 3 22 25
147 $meta{remaining_ttl} = $data->{remaining_ttl}
148 25 ▶ if exists $data->{remaining_ttl};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"remaining_ttl"} 3 22 25
149 25 ▶ if ( exists $data->{expire_to} && defined $data->{expire_to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"expire_to"} and defined $data->{"expire_to"}) 1 24 25
Condition66%2/3
exists $data->{"expire_to"} and defined $data->{"expire_to"}
!ll&&!rl&&r
2401
MC/DC50%1/2
exists $data->{"expire_to"} and defined $data->{"expire_to"}
exists $data->{"expire_to"} defined $data->{"expire_to"}
Truth table66%2/3
A and B
A= exists $data->{"expire_to"}B= defined $data->{"expire_to"}
ABresult
0X0
100
111
150 1 $meta{expire_to} = decode_term( $data->{expire_to} );
151 }
152  
153 return Logic::Relational::Clause->new(
154 id => $data->{id},
155 25 head => $head,
156 metadata => \%meta,
157 );
158 }
159  
160 6 ▶sub to_json ( $snapshot_data, %opts ) {
Subroutine to_json: called CC=2 SCAR=7.0
Pod undocumented
161 6 my $coder = JSON::PP->new->utf8;
162 6 ▶ $coder->pretty( $opts{pretty} // 1 );
Condition50%1/2
$opts{"pretty"} // 1
l!l
06
MC/DC0%0/1
$opts{"pretty"} // 1
$opts{"pretty"}
Truth table50%1/2
A
A= $opts{"pretty"}
Aresult
11
00
163 6 $coder->canonical(1);
164 6 return $coder->encode($snapshot_data);
165 }
166  
167 8 ▶sub from_json ($json_str) {
Subroutine from_json: called CC=1 SCAR=0.0
Pod undocumented
168 8 my $coder = JSON::PP->new->utf8;
169 8 return $coder->decode($json_str);
170 }
171  
172 2 1;
« lib/Logic/Relational/Query.pm ↑ Summary lib/Logic/Relational/Substitution.pm »