| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 26 | 0 | return { type => 'atom', name => $term->name }; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 28 | ▶ | if ( $ref eq 'Logic::Relational::Variable' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 12 | return { type => 'variable', name => $term->name }; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 16 | ▶ | if ( $ref eq 'Logic::Relational::Term' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 2 | return { type => 'array_rest', term => encode_term( $term->term ) }; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 13 | ▶ | if ( $ref eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return { type => 'domain', values => [ $term->values ] }; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 13 | ▶ | if ( $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $data eq "HASH" and exists $data->{"type"}
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"}
| |||||||||||||||||||||||||||||
| 63 | 76 | my $type = $data->{type}; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 76 | ▶ | if ( $type eq 'scalar' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 44 | return $data->{value}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 32 | ▶ | if ( $type eq 'atom' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | return Logic::Relational::Atom->new( $data->{name} ); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 32 | ▶ | if ( $type eq 'variable' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 12 | return Logic::Relational::Variable->new( name => $data->{name} ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 20 | ▶ | if ( $type eq 'term' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | my @decoded_args = map { decode_term($_) } @{ $data->{args} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"args"} // []
MC/DC0%0/1
$data->{"args"} // []
$data->{"args"}
Truth table50%1/2
A
A= $data->{"args"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 87 | 0 | require Logic::Relational::Domain; | |||||||||||||||||||||||||||
| 88 | return Logic::Relational::Domain->new( | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | { values => $data->{values} // [] } ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$data->{"values"} // []
MC/DC0%0/1
$data->{"values"} // []
$data->{"values"}
Truth table0%0/2
A
A= $data->{"values"}
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 17 | ▶ | if ( $type eq 'array' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | my @decoded_elements = | ||||||||||||||||||||||||||||
| 93 | 13 | ▶ | map { decode_term($_) } @{ $data->{elements} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"elements"} // []
MC/DC0%0/1
$data->{"elements"} // []
$data->{"elements"}
Truth table50%1/2
A
A= $data->{"elements"}
| |||||||||||||||||||||||||||||
| 94 | 13 | return \@decoded_elements; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 4 | ▶ | if ( $type eq 'hash' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 4 | my %decoded_pairs; | |||||||||||||||||||||||||||
| 98 | 4 | ▶ | my $pairs = $data->{pairs} // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"pairs"} // {}
MC/DC0%0/1
$data->{"pairs"} // {}
$data->{"pairs"}
Truth table50%1/2
A
A= $data->{"pairs"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 19 | ▶ | $data{created_at} = $meta->{created_at} if exists $meta->{created_at}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 19 | ▶ | if ( exists $meta->{expires_at} && defined $meta->{expires_at} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 19 | ▶ | if ( my $expire_to = $meta->{expire_to} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $data eq "HASH" and exists $data->{"predicate"}
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"}
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 25 | ▶ | my @decoded_args = map { decode_term($_) } @{ $data->{args} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{"args"} // []
MC/DC0%0/1
$data->{"args"} // []
$data->{"args"}
Truth table50%1/2
A
A= $data->{"args"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 146 | 25 | ▶ | $meta{expires_at} = $data->{expires_at} if exists $data->{expires_at}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | $meta{remaining_ttl} = $data->{remaining_ttl} | ||||||||||||||||||||||||||||
| 148 | 25 | ▶ | if exists $data->{remaining_ttl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 25 | ▶ | if ( exists $data->{expire_to} && defined $data->{expire_to} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $data->{"expire_to"} and defined $data->{"expire_to"}
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"}
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$opts{"pretty"} // 1
$opts{"pretty"}
Truth table50%1/2
A
A= $opts{"pretty"}
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||