| 1 | package Logic::Relational::Goal; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 31 | ▶ | use v5.38; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 4 | 31 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||
| 8 | Logic::Relational::Goal - Base class for relational logic goals. | ||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||
| 12 | package Logic::Relational::Goal::MyGoal; | ||||||||||||||||||||||||||
| 13 | use parent 'Logic::Relational::Goal'; | ||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | C<Logic::Relational::Goal> defines the interface for all logic goals in the engine. | ||||||||||||||||||||||||||
| 18 | Every goal must implement C<expand> and C<freshen>. | ||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||
| 20 | =head1 METHODS | ||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | =head2 expand | ||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||
| 24 | Abstract method. Evaluates the goal and returns a list of successor C<Logic::Relational::State> objects. | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | =head2 freshen | ||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||
| 28 | Abstract method. Takes a variable mapping and returns a freshened copy of the goal. | ||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||
| 30 | =head2 is_ground | ||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||
| 32 | Abstract method. Checks if the goal is ground (contains no unbound variables) | ||||||||||||||||||||||||||
| 33 | under the current substitution. | ||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||
| 35 | =head2 term_is_ground | ||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||
| 37 | Helper method to check if a term is ground under a substitution. | ||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||
| 41 | 31 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub expand ( $self, %args ) { | ||||||||||||||||||||||||
|
Subroutine
expand: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||
| 44 | 0 | croak "expand method must be implemented by subclass"; | |||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub freshen ( $self, $var_map ) { | ||||||||||||||||||||||||
|
Subroutine
freshen: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||
| 48 | 0 | croak "freshen method must be implemented by subclass"; | |||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub is_ground ( $self, $subst ) { | ||||||||||||||||||||||||
|
Subroutine
is_ground: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||
| 52 | 0 | croak "is_ground method must be implemented by subclass"; | |||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||
| 55 | 40 | ▶ | sub term_is_ground ( $self, $term, $subst ) { | ||||||||||||||||||||||||
|
Subroutine
term_is_ground: called CC=12 SCAR=39.2
Pod
documented
| |||||||||||||||||||||||||||
| 56 | 40 | $term = $subst->walk($term); | |||||||||||||||||||||||||
| 57 | 40 | ▶ | if ( ref($term) eq 'Logic::Relational::Variable' ) { ## no critic (ProhibitCascadingIfElse) | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 58 | 1 | return 0; | |||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||
| 60 | elsif ( ref($term) eq 'Logic::Relational::Term' ) { | ||||||||||||||||||||||||||
| 61 | 0 | for my $arg ( @{ $term->args } ) { | |||||||||||||||||||||||||
| 62 | 0 | ▶ | return 0 unless $self->term_is_ground( $arg, $subst ); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||
| 65 | elsif ( ref($term) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
| 66 | 0 | for my $arg (@$term) { | |||||||||||||||||||||||||
| 67 | 0 | ▶ | return 0 unless $self->term_is_ground( $arg, $subst ); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||
| 70 | elsif ( ref($term) eq 'Logic::Relational::ArrayRest' ) { | ||||||||||||||||||||||||||
| 71 | 0 | return $self->term_is_ground( $term->term, $subst ); | |||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||
| 73 | elsif ( ref($term) eq 'HASH' ) { | ||||||||||||||||||||||||||
| 74 | 0 | for my $val ( values %$term ) { | |||||||||||||||||||||||||
| 75 | 0 | ▶ | return 0 unless $self->term_is_ground( $val, $subst ); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||
| 78 | 39 | return 1; | |||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub as_string ($self) { | ||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( $self->can('as_term') ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 83 | 0 | return $self->as_term->as_string; | |||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||
| 85 | 0 | my $pkg = ref($self); | |||||||||||||||||||||||||
| 86 | 0 | $pkg =~ s/^Logic::Relational::Goal:://x; | |||||||||||||||||||||||||
| 87 | 0 | return $pkg; | |||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||
| 90 | 31 | 1; | |||||||||||||||||||||||||