| 1 | package Logic::Relational::Goal::Call; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use v5.38; ## no critic (ValuesAndExpressions::ProhibitVersionStrings) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use parent 'Logic::Relational::Goal'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 30 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | Logic::Relational::Goal::Call - Goal representing a predicate call. | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | Expands a predicate call against the program's clauses or generators, performing | ||||||||||||||||||||||||||||||||||||||||||
| 15 | automatic ancestor cycle pruning and optional subgoal answer tabling/memoization. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | Constructor. Takes C<$name> and C<\@args>. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head2 name | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | Returns the predicate functor name. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =head2 args | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | Returns the arrayref of call arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =head2 arity | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | Returns the number of arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head2 as_term | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Returns the call represented as a L<Logic::Relational::Term>. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head2 expand | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | Expands the call goal into successor states. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =head2 freshen | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | Returns a copy of the call goal with freshened logical variables. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =head2 is_ground | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | Returns true if all arguments of the call are ground under the given substitution. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 30 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 941 | ▶ | sub new ( $class, $name, $args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 941 | return bless { name => $name, args => $args }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 574 | ▶ | sub name ($self) { return $self->{name}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 511 | ▶ | sub args ($self) { return $self->{args}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 832 | ▶ | sub arity ($self) { return scalar @{ $self->{args} }; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
arity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 338 | ▶ | sub as_term ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_term: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 338 | require Logic::Relational::Term; | |||||||||||||||||||||||||||||||||||||||||
| 65 | return Logic::Relational::Term->new( | ||||||||||||||||||||||||||||||||||||||||||
| 66 | functor => $self->{name}, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | args => $self->{args} | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 338 | ); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 329 | ▶ | sub _check_defined ( $self, $key, $predicates, $generators ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_defined: called CC=4 SCAR=22.9
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 329 | ▶ | return if exists $predicates->{$key} || exists $generators->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $predicates->{$key} or exists $generators->{$key}
MC/DC0%0/2
exists $predicates->{$key} or exists $generators->{$key}
exists $predicates->{$key}
exists $generators->{$key}
Truth table66%2/3
A or B
A= exists $predicates->{$key}B= exists $generators->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my @alts = grep { $_ =~ /^\Q$self->{name}\E\//x } | |||||||||||||||||||||||||||||||||||||||||
| 75 | ( keys %$predicates, keys %$generators ); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (@alts) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my ( $alt_name, $alt_arity ) = split( '/', $alts[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | croak "Predicate " | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | . $self->{name} . "/" | |||||||||||||||||||||||||||||||||||||||||
| 80 | . $self->arity | ||||||||||||||||||||||||||||||||||||||||||
| 81 | . " is undefined; available predicate is $alt_name/$alt_arity\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | croak "Predicate " . $self->{name} . "/" . $self->arity . " is undefined\n"; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 329 | ▶ | sub expand ( $self, %args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expand: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 329 | ▶ | my $predicates = $args{predicates} // croak "predicates is required"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"predicates"} // croak("predicates is required")
MC/DC0%0/1
$args{"predicates"} // croak("predicates is required")
$args{"predicates"}
Truth table50%1/2
A
A= $args{"predicates"}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 329 | ▶ | my $generators = $args{generators} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"generators"} // {}
MC/DC0%0/1
$args{"generators"} // {}
$args{"generators"}
Truth table50%1/2
A
A= $args{"generators"}
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 329 | my $program = $args{program}; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 329 | my $state = $args{state}; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 329 | my $subst = $args{substitution}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 329 | my $remaining = $args{remaining}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 329 | my $key = $self->{name} . '/' . $self->arity; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 329 | $self->_check_defined( $key, $predicates, $generators ); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 329 | require Logic::Relational::Unifier; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 329 | require Logic::Relational::State; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 329 | ▶ | if ( my $gen_code = $generators->{$key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 13 | my @reified_args = map { $subst->reify($_) } @{ $self->{args} }; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 13 | my $iterator = $gen_code->(@reified_args); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 13 | ▶ | croak "Generator $key did not return a coderef iterator" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | unless ref($iterator) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 12 | require Logic::Relational::Goal::GeneratorStep; | |||||||||||||||||||||||||||||||||||||||||
| 107 | my $step_goal = Logic::Relational::Goal::GeneratorStep->new( | ||||||||||||||||||||||||||||||||||||||||||
| 108 | iterator => $iterator, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | call_args => $self->{args}, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | key => $key, | |||||||||||||||||||||||||||||||||||||||||
| 111 | arity => $self->arity, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 12 | my $succ = $state->derive( | |||||||||||||||||||||||||||||||||||||||||
| 115 | substitution => $subst, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | goals => [ $step_goal, @$remaining ], | ||||||||||||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | ▶ | return $succ // (); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$succ // ()
MC/DC0%0/2
$succ // ()
$succ
()
Truth table33%1/3
A // B
A= $succB= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 316 | ▶ | if ( $program | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$program and $program->can("is_tabled") and $program->is_tabled($self->{"name"}, $self->arity)
$program and $program->can("is_tabled")
MC/DC33%1/3
$program and $program->can("is_tabled") and $program->is_tabled($self->{"name"}, $self->arity)
$program
$program->can("is_tabled")
$program->is_tabled($self->{"name"}, $self->arity)
Truth table50%2/4
A and B and C
A= $programB= $program->can("is_tabled")C= $program->is_tabled($self->{"name"}, $self->arity)
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | && $program->can('is_tabled') | ||||||||||||||||||||||||||||||||||||||||||
| 123 | && $program->is_tabled( $self->{name}, $self->arity ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 22 | return $self->_expand_tabled(%args); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 294 | my $my_term = $self->as_term; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 294 | my $my_sig = $state->canonical_term_signature( $my_term, $subst ); | |||||||||||||||||||||||||||||||||||||||||
| 130 | 294 | my @anc = @{ $state->ancestors }; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 294 | for my $anc_term (@anc) { | |||||||||||||||||||||||||||||||||||||||||
| 133 | 348 | ▶ | if ( $state->canonical_term_signature( $anc_term, $subst ) eq $my_sig ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 8 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 286 | ▶ | return $self->_expand_clauses( $predicates->{$key} // [], | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$predicates->{$key} // []
MC/DC0%0/1
$predicates->{$key} // []
$predicates->{$key}
Truth table50%1/2
A
A= $predicates->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | $my_term, \@anc, $state, $subst, $remaining ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 286 | ▶ | sub _expand_clauses ( $self, $clauses, $my_term, $anc, $state, $subst, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_clauses: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | $remaining ) | ||||||||||||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 286 | my @successors; | |||||||||||||||||||||||||||||||||||||||||
| 147 | my $end_call; | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 286 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 286 | for my $clause (@$clauses) { | |||||||||||||||||||||||||||||||||||||||||
| 151 | 681 | my $meta = $clause->metadata; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 681 | ▶ | if ( exists $meta->{expires_at} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
MC/DC0%0/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 table50%2/4
A and B and C
A= exists $meta->{"expires_at"}B= defined $meta->{"expires_at"}C= $now >= $meta->{"expires_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | && defined $meta->{expires_at} | ||||||||||||||||||||||||||||||||||||||||||
| 154 | && $now >= $meta->{expires_at} ) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 681 | my $fresh_clause = $clause->freshen; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 681 | my $new_subst = Logic::Relational::Unifier::unify( $my_term, | |||||||||||||||||||||||||||||||||||||||||
| 161 | $fresh_clause->head, $subst ); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 681 | ▶ | if ($new_subst) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 347 | my $body = $fresh_clause->body; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 347 | ▶ | if ( ref($body) eq 'Logic::Relational::Goal::True' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 210 | my $succ = $state->derive( | |||||||||||||||||||||||||||||||||||||||||
| 166 | substitution => $new_subst, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | goals => [ $body, @$remaining ], | ||||||||||||||||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 210 | ▶ | push @successors, $succ if $succ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 137 | ▶ | if ( !$end_call ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 107 | require Logic::Relational::Goal::EndCall; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 107 | $end_call = Logic::Relational::Goal::EndCall->new; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 137 | my $succ = $state->derive( | |||||||||||||||||||||||||||||||||||||||||
| 177 | substitution => $new_subst, | ||||||||||||||||||||||||||||||||||||||||||
| 178 | goals => [ $body, $end_call, @$remaining ], | ||||||||||||||||||||||||||||||||||||||||||
| 179 | ancestors => [ @$anc, $my_term ], | ||||||||||||||||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 137 | ▶ | push @successors, $succ if $succ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 286 | return @successors; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 22 | ▶ | sub _expand_tabled ( $self, %args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_tabled: called CC=20 SCAR=31.5
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 22 | my $predicates = $args{predicates}; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 22 | ▶ | my $generators = $args{generators} // {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"generators"} // {}
MC/DC0%0/1
$args{"generators"} // {}
$args{"generators"}
Truth table50%1/2
A
A= $args{"generators"}
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 22 | my $program = $args{program}; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 22 | my $state = $args{state}; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 22 | my $subst = $args{substitution}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 22 | my $remaining = $args{remaining}; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 22 | my $key = $self->{name} . '/' . $self->arity; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 22 | my $my_term = $self->as_term; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 22 | my $call_sig = $state->canonical_term_signature( $my_term, $subst ); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 22 | my $cache = $state->table_cache; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 22 | my $entry = $cache->{$call_sig}; | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 22 | ▶ | if ( $entry && $entry->{complete} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$entry and $entry->{"complete"}
MC/DC100%2/2
$entry and $entry->{"complete"}
$entry
$entry->{"complete"}
Truth table100%3/3
A and B
A= $entryB= $entry->{"complete"}
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | return $self->_unify_cached_answers( $entry->{answers}, $state, | |||||||||||||||||||||||||||||||||||||||||
| 204 | $subst, $remaining ); | ||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 21 | ▶ | if ( $entry && $entry->{in_progress} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$entry and $entry->{"in_progress"}
MC/DC50%1/2
$entry and $entry->{"in_progress"}
$entry
$entry->{"in_progress"}
Truth table66%2/3
A and B
A= $entryB= $entry->{"in_progress"}
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 16 | $entry->{hit_self_recursion} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 16 | $cache->{_active_dependency} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 16 | return $self->_unify_cached_answers( $entry->{answers}, $state, | |||||||||||||||||||||||||||||||||||||||||
| 211 | $subst, $remaining ); | ||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | my $outer_in_progress = | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | scalar grep { ref($_) eq 'HASH' && $_->{in_progress} } values %$cache; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $_ eq 'HASH' && $_->{'in_progress'}
MC/DC0%0/2
ref $_ eq 'HASH' && $_->{'in_progress'}
ref $_ eq 'HASH'
$_->{'in_progress'}
Truth table0%0/3
A && B
A= ref $_ eq 'HASH'B= $_->{'in_progress'}
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 5 | $entry = $cache->{$call_sig} = { | |||||||||||||||||||||||||||||||||||||||||
| 218 | in_progress => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | complete => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 220 | answers => [], | ||||||||||||||||||||||||||||||||||||||||||
| 221 | answer_sigs => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 222 | hit_self_recursion => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 5 | ▶ | my $clauses = $predicates->{$key} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$predicates->{$key} // []
MC/DC0%0/1
$predicates->{$key} // []
$predicates->{$key}
Truth table50%1/2
A
A= $predicates->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 5 | my $now = time; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | require Logic::Relational::Query; | |||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 5 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 230 | 17 | my $new_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 17 | for my $clause (@$clauses) { | |||||||||||||||||||||||||||||||||||||||||
| 232 | 33 | my $meta = $clause->metadata; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 33 | ▶ | if ( exists $meta->{expires_at} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
exists $meta->{"expires_at"} and defined $meta->{"expires_at"} and $now >= $meta->{"expires_at"}
exists $meta->{"expires_at"} and defined $meta->{"expires_at"}
MC/DC0%0/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 table25%1/4
A and B and C
A= exists $meta->{"expires_at"}B= defined $meta->{"expires_at"}C= $now >= $meta->{"expires_at"}
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | && defined $meta->{expires_at} | ||||||||||||||||||||||||||||||||||||||||||
| 235 | && $now >= $meta->{expires_at} ) | ||||||||||||||||||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 33 | my $fresh_clause = $clause->freshen; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 33 | my $new_subst = Logic::Relational::Unifier::unify( $my_term, | |||||||||||||||||||||||||||||||||||||||||
| 242 | $fresh_clause->head, $subst ); | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 33 | ▶ | next unless $new_subst; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | my $sub_q = Logic::Relational::Query->new( | ||||||||||||||||||||||||||||||||||||||||||
| 246 | program => $program, | ||||||||||||||||||||||||||||||||||||||||||
| 247 | goals => [ $fresh_clause->body ], | ||||||||||||||||||||||||||||||||||||||||||
| 248 | predicates => $predicates, | ||||||||||||||||||||||||||||||||||||||||||
| 249 | generators => $generators, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | substitution => $new_subst, | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 33 | ancestors => [ @{ $state->ancestors }, $my_term ], | |||||||||||||||||||||||||||||||||||||||||
| 252 | table_cache => $cache, | ||||||||||||||||||||||||||||||||||||||||||
| 253 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 33 | ▶ | while ( my $sol = $sub_q->next ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 75 | my $sol_subst = $sol->substitution; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 75 | my $ans_term = $sol_subst->reify($my_term); | |||||||||||||||||||||||||||||||||||||||||
| 258 | 75 | my $ans_sig = | |||||||||||||||||||||||||||||||||||||||||
| 259 | $state->canonical_term_signature( $ans_term, $sol_subst ); | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 75 | ▶ | if ( !$entry->{answer_sigs}{$ans_sig}++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 24 | push @{ $entry->{answers} }, $ans_term; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 24 | $new_count++; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 17 | ▶ | if ( !( $entry->{hit_self_recursion} && $new_count > 0 ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$entry->{"hit_self_recursion"} and $new_count > 0
MC/DC100%2/2
$entry->{"hit_self_recursion"} and $new_count > 0
$entry->{"hit_self_recursion"}
$new_count > 0
Truth table100%3/3
A and B
A= $entry->{"hit_self_recursion"}B= $new_count > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 5 | ▶ | if ( $outer_in_progress && $cache->{_active_dependency} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$outer_in_progress and $cache->{"_active_dependency"}
MC/DC0%0/2
$outer_in_progress and $cache->{"_active_dependency"}
$outer_in_progress
$cache->{"_active_dependency"}
Truth table33%1/3
A and B
A= $outer_in_progressB= $cache->{"_active_dependency"}
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | delete $cache->{$call_sig}; | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 5 | $entry->{in_progress} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 5 | $entry->{complete} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 5 | ▶ | delete $cache->{_active_dependency} unless $outer_in_progress; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 5 | return $self->_unify_cached_answers( $entry->{answers}, $state, $subst, | |||||||||||||||||||||||||||||||||||||||||
| 281 | $remaining ); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 22 | ▶ | sub _unify_cached_answers ( $self, $answers, $state, $subst, $remaining ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unify_cached_answers: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 22 | my $my_term = $self->as_term; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 22 | my @successors; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 22 | for my $ans_term (@$answers) { | |||||||||||||||||||||||||||||||||||||||||
| 288 | 69 | my $fresh_ans = $ans_term->freshen( {} ); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 69 | my $new_subst = | |||||||||||||||||||||||||||||||||||||||||
| 290 | Logic::Relational::Unifier::unify( $my_term, $fresh_ans, $subst ); | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 69 | ▶ | if ($new_subst) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 69 | my $succ = $state->derive( | |||||||||||||||||||||||||||||||||||||||||
| 293 | substitution => $new_subst, | ||||||||||||||||||||||||||||||||||||||||||
| 294 | goals => $remaining, | ||||||||||||||||||||||||||||||||||||||||||
| 295 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 69 | ▶ | push @successors, $succ if $succ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 22 | return @successors; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 211 | ▶ | sub freshen ( $self, $var_map ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
freshen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 211 | require Logic::Relational::DSL; | |||||||||||||||||||||||||||||||||||||||||
| 304 | my @fresh_args = | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 412 | map { Logic::Relational::DSL::freshen_val( $_, $var_map ) } | |||||||||||||||||||||||||||||||||||||||||
| 306 | 211 | @{ $self->{args} }; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 211 | return ref($self)->new( $self->{name}, \@fresh_args ); | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | ▶ | sub is_ground ( $self, $subst ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_ground: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 3 | for my $arg ( @{ $self->{args} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 312 | 6 | ▶ | return 0 unless $self->term_is_ground( $arg, $subst ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 30 | 1; | |||||||||||||||||||||||||||||||||||||||||