| 1 | package AI::Prolog::TermList; | ||||||||||||||||||||||||||||
| 2 | 13 | $REVISION = '$Id: TermList.pm,v 1.11 2005/08/06 23:28:40 ovid Exp $'; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 13 | $VERSION = 0.03; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Carp qw( croak confess ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Hash::Util 'lock_keys'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use aliased 'AI::Prolog::Term'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use aliased 'AI::Prolog::Term::Number'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use aliased 'AI::Prolog::Parser'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use aliased 'AI::Prolog::TermList::Clause'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use aliased 'AI::Prolog::TermList::Primitive'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1945 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | #my ($proto, $parser, $nexttermlist, $definertermlist) = @_; | ||||||||||||||||||||||||||||
| 21 | 1945 | my $proto = shift; | |||||||||||||||||||||||||||
| 22 | 1945 | ▶ | my $class = ref $proto || $proto; # yes, I know what I'm doing | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 23 | 1945 | ▶ | return _new_from_term( $class, @_ ) if 1 == @_ && $_[0]->isa(Term); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
1 == @_ and $_[0]->isa("AI::Prolog::Term")
MC/DC50%1/2
1 == @_ and $_[0]->isa("AI::Prolog::Term")
1 == @_
$_[0]->isa("AI::Prolog::Term")
Truth table66%2/3
A and B
A= 1 == @_B= $_[0]->isa("AI::Prolog::Term")
| |||||||||||||||||||||||||||||
| 24 | 1846 | ▶ | return _new_from_term_and_next( $class, @_ ) if 2 == @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 1291 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | croak "Unknown arguments to TermList->new: @_"; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 1291 | my $self = bless { | |||||||||||||||||||||||||||
| 29 | term => undef, | ||||||||||||||||||||||||||||
| 30 | next => undef, | ||||||||||||||||||||||||||||
| 31 | next_clause => | ||||||||||||||||||||||||||||
| 32 | undef, # serves two purposes: either links clauses in database | ||||||||||||||||||||||||||||
| 33 | # or points to defining clause for goals | ||||||||||||||||||||||||||||
| 34 | is_builtin => undef, | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | varname => undef, | ||||||||||||||||||||||||||||
| 37 | ID => undef, | ||||||||||||||||||||||||||||
| 38 | _results => undef, | ||||||||||||||||||||||||||||
| 39 | } => $class; | ||||||||||||||||||||||||||||
| 40 | 1291 | lock_keys %$self; | |||||||||||||||||||||||||||
| 41 | 1291 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 654 | ▶ | sub _new_from_term { | ||||||||||||||||||||||||||
|
Subroutine
_new_from_term: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 654 | my ( $class, $term ) = @_; | |||||||||||||||||||||||||||
| 46 | 654 | my $self = $class->new; | |||||||||||||||||||||||||||
| 47 | 654 | $self->{term} = $term; | |||||||||||||||||||||||||||
| 48 | 654 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 555 | ▶ | sub _new_from_term_and_next { | ||||||||||||||||||||||||||
|
Subroutine
_new_from_term_and_next: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 555 | my ( $class, $term, $next ) = @_; | |||||||||||||||||||||||||||
| 53 | 555 | my $self = $class->_new_from_term($term); | |||||||||||||||||||||||||||
| 54 | 555 | $self->{next} = $next; | |||||||||||||||||||||||||||
| 55 | 555 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 928 | ▶ | sub term { shift->{term} } | ||||||||||||||||||||||||||
|
Subroutine
term: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 503 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 503 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 503 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 59 | $self->{next} = shift; | |||||||||||||||||||||||||||
| 64 | 59 | return $self; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 444 | return $self->{next}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 242 | ▶ | sub next_clause { | ||||||||||||||||||||||||||
|
Subroutine
next_clause: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 242 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 242 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # XXX debug | ||||||||||||||||||||||||||||
| 74 | 190 | my $next_clause = shift; | |||||||||||||||||||||||||||
| 75 | 13 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 190 | ▶ | if ( $next_clause eq $self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | confess("Trying to assign a termlist as its own successor"); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 190 | $self->{next_clause} = $next_clause; | |||||||||||||||||||||||||||
| 80 | 190 | return $self; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 52 | return $self->{next_clause}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | ▶ | sub to_string { | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 5 | my $indent = "\n\t"; | |||||||||||||||||||||||||||
| 88 | 5 | my $to_string = $indent . $self->term->to_string; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | #my $to_string = "[" . $self->term->to_string; | ||||||||||||||||||||||||||||
| 91 | 5 | my $tl = $self->next; | |||||||||||||||||||||||||||
| 92 | 5 | ▶ | while ($tl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1 | $to_string .= ",$indent" . $tl->term->to_string; | |||||||||||||||||||||||||||
| 94 | 1 | $tl = $tl->next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 5 | return $to_string; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 108 | ▶ | sub resolve { # a.k.a. lookup_in | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 108 | my ( $self, $kb ) = @_; | |||||||||||||||||||||||||||
| 101 | 108 | my $predicate = $self->{term}->predicate; | |||||||||||||||||||||||||||
| 102 | 108 | $self->next_clause( $kb->get($predicate) ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 13 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||