AI-ExpertSystem-Advanced
view release on metacpan or search on metacpan
lib/AI/ExpertSystem/Advanced/Dictionary.pm view on Meta::CPAN
=item 2
Finding an element in the stack.
=item 3
Adding or removing elements from the stack.
=back
=cut
use Moose;
use List::MoreUtils qw(firstidx);
our $VERSION = '0.03';
=head1 Attributes
=over 4
=item B<stack>
An array with all the keys of C<stack_hash>. Useful for creating the
C<iterable_array> and for knowing the order of the items as they get added or
removed.
=cut
has 'stack' => (
is => 'rw',
isa => 'ArrayRef');
=item B<stack_hash>
The original hash, has all the elements with all their properties (eg extra
keys). The I<disadvantage> of it is that it doesn't keeps the order of the
elements, hence the need of C<stack>.
=cut
has 'stack_hash' => (
is => 'ro',
isa => 'HashRef[Str]');
=item B<iterable_array>
Used by the C<iterate()> and C<iterate_reverse()> methods. It starts as a copy
of C<stack> and as the iterate methods start running this array starts getting
I<reduced> until it gets to an empty list.
=back
=cut
has 'iterable_array' => (
is => 'ro',
isa => 'ArrayRef');
=head1 Methods
=head2 B<find($look_for, $find_by)>
Looks for a given value (C<$look_for>). By default it will look for the value
by reading the C<id> of each item, however this can be changed by passing
a different hash key (C<$find_by>).
In case there's no match C<undef> is returned.
=cut
sub find {
my ($self, $look_for, $find_by) = @_;
if (!defined($find_by)) {
if (defined $self->{'stack_hash'}->{$look_for}) {
return $look_for;
}
return undef;
}
foreach my $key (keys %{$self->{'stack_hash'}}) {
if ($self->{'stack_hash'}->{$key}->{$find_by} eq $look_for) {
return $key;
}
}
return undef;
}
=head2 B<get_value($id, $key)>
The L<AI::ExpertSystem::Advanced::Dictionary> consists of a hash of elements,
each element has its own properties (eg, extra keys).
This method looks for the value of the given C<$key> of a given element C<id>.
It will return the value, but if element doesn't have the given C<$key> then
C<undef> will be returned.
=cut
sub get_value {
my ($self, $id, $key) = @_;
if (!defined $self->{'stack_hash'}->{$id}) {
return undef;
}
if (defined $self->{'stack_hash'}->{$id}->{$key}) {
return $self->{'stack_hash'}->{$id}->{$key};
} else {
return undef;
}
}
=head2 B<append($id, %extra_keys)>
Adds a new element to the C<stack_hash> and C<stack>. The element gets added to
the end of C<stack>.
The C<$id> parameter specifies the id of the new element and the next parameter
is a stack of I<extra> keys.
=cut
sub append {
my $self = shift;
my $id = shift;
( run in 0.879 second using v1.01-cache-2.11-cpan-39bf76dae61 )