AI-ExpertSystem-Advanced
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/AI/ExpertSystem/Advanced/Dictionary.pm view on Meta::CPAN
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;
return $self->_add($id, undef, @_);
}
=head2 B<prepend($id, %extra_keys)>
Same as C<append()>, but the element gets added to the top of the C<stack>.
=cut
sub prepend {
my $self = shift;
my $id = shift;
return $self->_add($id, 1, @_);
}
=head2 B<update($id, %extra_keys)>
Updates the I<extra> keys of the element that matches the given C<$id>.
Please note that it will only update or add new keys. So if the given element
already has a key and this is not provided in C<%extra_keys> then it wont
be modified.
=cut
sub update {
my ($self, $id, $properties) = @_;
if (defined $self->{'stack_hash'}->{$id}) {
foreach my $key (keys %$properties) {
$self->{'stack_hash'}->{$id}->{$key} = $properties->{$key};
}
} else {
warn "Not updating $id, does not exist!";
}
}
=head2 B<remove($id)>
Removes the element that matches the given C<$id> from C<stack_hash> and
C<stack>.
Returns true if the removal is successful, otherwise false is returned.
=cut
sub remove {
my ($self, $id) = @_;
if (defined $self->{'stack_hash'}->{$id}) {
delete($self->{'stack_hash'}->{$id});
# Find the index in the array, lets suppose our arrays are big
my $index = List::MoreUtils::first_index {
defined $_ and $_ eq $id
} @{$self->{'stack'}};
splice(@{$self->{'stack'}}, $index, 1);
return 1;
}
return 0;
}
=head2 B<size()>
Returns the size of C<stack>.
=cut
sub size {
my ($self) = @_;
return scalar(@{$self->{'stack'}});
}
=head2 B<iterate()>
Returns the first element of the C<iterable_array> and C<iterable_array> is
reduced by one.
If no more items are found in C<iterable_array> then C<undef> is returned.
=cut
sub iterate {
my ($self) = @_;
return shift(@{$self->{'iterable_array'}});
}
=head2 B<iterate_reverse()>
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.490 second using v1.00-cache-2.02-grep-82fe00e-cpan-b63e86051f13 )