NAME

AI::ExpertSystem::Advanced::Dictionary - Array/hash dictionary

DESCRIPTION

The dictionary offers a unified interface for:

  1. Reading through a list of items with a minimal use of memory since it offers an iterator that works with a stack. So everytime it gets asked for the next element it drops the first or last element of the stack.

  2. Finding an element in the stack.

  3. Adding or removing elements from the stack.

Attributes

stack

An array with all the keys of stack_hash. Useful for creating the iterable_array and for knowing the order of the items as they get added or removed.

stack_hash

The original hash, has all the elements with all their properties (eg extra keys). The disadvantage of it is that it doesn't keeps the order of the elements, hence the need of stack.

iterable_array

Used by the iterate() and iterate_reverse() methods. It starts as a copy of stack and as the iterate methods start running this array starts getting reduced until it gets to an empty list.

Methods

find($look_for, $find_by)

Looks for a given value ($look_for). By default it will look for the value by reading the id of each item, however this can be changed by passing a different hash key ($find_by).

In case there's no match undef is returned.

get_value($id, $key)

The 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 $key of a given element id.

It will return the value, but if element doesn't have the given $key then undef will be returned.

append($id, %extra_keys)

Adds a new element to the stack_hash and stack. The element gets added to the end of stack.

The $id parameter specifies the id of the new element and the next parameter is a stack of extra keys.

prepend($id, %extra_keys)

Same as append(), but the element gets added to the top of the stack.

update($id, %extra_keys)

Updates the extra keys of the element that matches the given $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 %extra_keys then it wont be modified.

remove($id)

Removes the element that matches the given $id from stack_hash and stack.

Returns true if the removal is successful, otherwise false is returned.

size()

Returns the size of stack.

iterate()

Returns the first element of the iterable_array and iterable_array is reduced by one.

If no more items are found in iterable_array then undef is returned.

iterate_reverse()

Same as iterate(), but instead of returning the first element, it returns the last element of iterable_array.

populate_iterable_array()

The iterable_array gets populated when a dictionary instance is created, however if new items are added or removed then it's extremely needed to call this method so iterable_array gets populated again.

AUTHOR

Pablo Fischer (pablo@pablo.com.mx).

COPYRIGHT

Copyright (C) 2010 by Pablo Fischer.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.