Agent Framework - craterdog-archives/js-bali-component-framework GitHub Wiki

Agent Framework

The following UML class diagram shows a high-level view of the abstract component types (yellow) and the agent classes (green) with which they interact.

Bali Agent Framework

Abstract type names are italicized whereas concrete type names are not. All of the types shown in this diagram are abstract and highlight the interdependencies within the framework. This project uses several common Gang of Four Design Patterns including the Composite, Iterator, Visitor and Strategy patterns as a foundation for the agent framework.

Structural Classes

The following abstract classes allow information to be modelled in a hierarchical structure.

Component

It should come as no surprise that most of the classes that makeup the Bali Component Framework™ are a type of component. Components are used to capture structured information as well as define procedures for doing things. There are three important abstract types of structural components.

Element

Elements are primitive components that cannot be broken down into subcomponents. Each element has an immutable value. And although the value of an element cannot be changed, many elements provide functions that operate on one or more elements of that type to produce a new element.

String

Some elements, even though they behave as primitive components, are sequential in nature. They consist of strings of characters or numbers that may be iterated over. A string element is still immutable however, unlike collections whose items may be added, modified or removed.

Collection

And finally, collection components contain zero or more items in a sequence, each of which is also a component. Items can be added and removed from a collection. In fact, an item may be contained in multiple collections at the same time. The lifetime of an item is not necessarily tied to the lifetime of a collection that contains it.

Agent Classes

The rest of the abstract classes shown in the UML diagram above act as agents that do things to the hierarchical structures defined using the components types described above.

Formatter

A formatter uses the visitor pattern to "walk" a component's structure generating an equivalent document string containing the formal equivalent of the component in a format like Bali Document Notation™ or HTML.

Duplicator

A duplicator is used to recursively copy a component and its subcomponents. Since elements are immutable they are not copied during the duplication process.

Parser

Whereas a formatter turns a component into a document, a parser reads a document and generates the equivalent component structure for that document. Parsers and formatters are useful for importing and exporting components in a canonical format.

Visitor

A visitor "walks" a component and all of its subcomponents recursively performing some type specific operation on each component. Within the Bali Component Framework™ visitors are used to format, duplicate and compile component structures. Custom visitors that perform different tasks can be created as well.

Comparator

A comparator is used to recursively compare two components for equality or relative ranking. When the first component is determined to be less than the second component, a value of -1 is returned. When the first component is greater than the second component, a value of 1 is returned. And when the two components are the same a value of 0 is returned. Comparators are used to maintain the order of the items in an ordered collection like a set. They are also used by sorters to determine the order of items when a set is being sorted.

Sorter

A sorter is used to sort a collection of items into a specific order using a comparator to rank each pair of items. The collection must be sortable and not enforce a preexisting order of its items.

Iterator

An iterator, not surprisingly, is used to iterate over the items in a sequence. It can move forward or backwards through the items. The iterator is positioned in the slots between items starting with the slot before the first item and ending in the slot after the last item. From a specific slot it can either retrieve the next item or the previous item. After retrieving the item the iterator moves to the next (or previous) slot.