RuleEngine

The default implementation of the IRuleEngine interface.  RuleEngine stores all of the IRules, ILogics, mixin strings, and priority branching logic provided by the loaded RuleSets in string-based hash maps.  The functions required by the IRuleEngine interface are implemented by taking the string-based hash maps and performing the necessary operations.  See the function and variable documentation below for more details.

Derived From

IRuleEngine

Project

RulesCore

Include

RuleEngine.h

Summary
The default implementation of the IRuleEngine interface.
Stops the RuleEngine.
Executes the given IRule with the given DataObject.
Calls the given logic with the given DataObjects.
Returns whether there is anything on the undo stack that can be undone.
Undoes the last action.
Branches down the rule chain.
Adds any mixins to the given IRule or ILogic.
The string-based hash of IRule objects.
The string-based hash of ILogic objects.
The string-based hash of string mixin names.
The string-based hash of priority branching logic.
The undo stack.

Public

Summary
Stops the RuleEngine.
Executes the given IRule with the given DataObject.
Calls the given logic with the given DataObjects.
Returns whether there is anything on the undo stack that can be undone.
Undoes the last action.

Functions

Stop

void Stop()

Stops the RuleEngine.  Cleans out the undo stack.  Clears the rule hash, logic hash, mixin hash, and branching logic hash.

Virtual Functions

Execute

virtual void Execute(const wxString &rule,
const DataObject &object)

Executes the given IRule with the given DataObject.  Looks up the IRule name in the string hash map.  If the string name does not exist in the hash, an error is thrown to the screen.  If the IRule is potentially undoable, it may be added to the undo stack if its CanUndo function returns true.  If there are any mixins that belong to this IRule, they are added to the IRule before it executes.  After the rule executes, the Branch function is called to see if the IRule has any branching logic that needs to get called.

Parameters

ruleThe name of the IRule to execute.
objectThe DataObject to pass to the IRule.

Decide

virtual void Decide(const wxString &logic,
const DataObject &input,
DataObject &output)

Calls the given logic with the given DataObjects.  Looks up the ILogic name in the string hash map.  If the string name does not exist in the hash, an error is thrown to the screen.  If there are any mixins that belong to this ILogic, they are added to the ILogic before it is called.  The output DataObject must be empty before the ILogic’s Decide function is called and non-empty afterwards.

Parameters

logicThe name of the ILogic to call.
inputThe input DataObject.
outputThe output DataObject.  Must be NULL.

CanUndo

virtual bool CanUndo() const

Returns whether there is anything on the undo stack that can be undone.

Returns

A bool giving whether the last action can be undone.  The value is true if the action is undoable, false if not.

Undo

virtual void Undo()

Undoes the last action.  Pops the last IRule name/DataObject pair off the undo stack and calls that IRules Unexecute function with the DataObject.  Plays the undo sound.

Private

Summary
Branches down the rule chain.
Adds any mixins to the given IRule or ILogic.
The string-based hash of IRule objects.
The string-based hash of ILogic objects.
The string-based hash of string mixin names.
The string-based hash of priority branching logic.
The undo stack.

Functions

Branch

void Branch(const wxString &rule,
const DataObject &object)

Branches down the rule chain.  Looks up the given IRule in the hash of rule chain logic.  If an entry is found with the given IRule name, the branch logic at that entry is called in priority order.  If any of the branch logic returns the name of another IRule, that IRule is then immediately executed via the Execute function.  In this way branching logic for IRules may be implemented without the IRules themselves knowing about each other.

Parameters

ruleThe name of the IRule to branch from.
objectThe DataObject to pass down the rule chain.

AddMixins

void AddMixins(const wxString &name,
BaseMixin *pMixin)

Adds any mixins to the given IRule or ILogic.  Looks up the name in the mixin hash.  If there are any mixin names available for that IRule or ILogic name, they are added to the given BaseMixin pointer so that the IRule or ILogic has access to them during its execution.

Parameters

nameThe name of the IRule or ILogic to look up mixins for.
pMixinA pointer to the BaseMixin object that will receive any mixin data.  It is safe for the pointer to be NULL.

Variables

mRuleHash

RuleHash mRuleHash

The string-based hash of IRule objects.  The rule hash contains the IRules specific to the game being played, copied from all of the RuleSets loaded by the IPluginEngine.  Each IRule is stored in a one to one mapping with a string that is the rule name.  Rules are executed by calling the Execute function with the rule name and a DataObject to pass to the IRule.

mLogicHash

LogicHash mLogicHash

The string-based hash of ILogic objects.  The logic hash contains the ILogic specific to the game being played, copied from all of the RuleSets loaded by the IPluginEngine.  Each piece of ILogic is stored in a one to one mapping with a string that is the logic name.  Logic is executed by calling the Decide function with the logic name, an input DataObject, which may not be empty, and an output DataObject, which must be empty, and is filled with the rendered decision of the piece of ILogic.

mMixinHash

MixinHash mMixinHash

The string-based hash of string mixin names.  Each IRule or ILogic that has mixins provided by the loaded RuleSets has an entry by name in the mixin hash.  At each mixin hash entry is stored another string-based hash map that contains the actual mixin entry names for that IRule or ILogic.

mBranchHash

BranchPriorityHash mBranchHash

The string-based hash of priority branching logic.  Some RuleSets need certain IRules to immediately branch to another IRule after it has finished executing.  However, other RuleSets may need to overload even this existing branching.  The priority branch hash provides a mechanism for this to happen.  The name of each rule with branching logic is stored as a key in the hash.  For each key, a std::map of integer/ILogic pairs is stored, with higher integer values being stored first (meaning, they have greater priority).  When the Branch function is called, the given rule name is looked up in the priority branching hash.  If it is discovered, each of its associated ILogic is called in the priority order.  If any piece of ILogic returns the name of a rule, that IRule is immediately branched to with the Execute function.

mUndoStack

UndoStack mUndoStack

The undo stack.  Still volatile.

The interface for the rule engine.
void Stop()
Stops the RuleEngine.
virtual void Execute(const wxString &rule,
const DataObject &object)
Executes the given IRule with the given DataObject.
The interface for a single game rule.
A generic container for passing any kind of data around.
virtual void Decide(const wxString &logic,
const DataObject &input,
DataObject &output)
Calls the given logic with the given DataObjects.
virtual bool CanUndo() const
Returns whether there is anything on the undo stack that can be undone.
virtual void Undo()
Undoes the last action.
void Branch(const wxString &rule,
const DataObject &object)
Branches down the rule chain.
void AddMixins(const wxString &name,
BaseMixin *pMixin)
Adds any mixins to the given IRule or ILogic.
The interface of a single piece of game logic.
RuleHash mRuleHash
The string-based hash of IRule objects.
LogicHash mLogicHash
The string-based hash of ILogic objects.
MixinHash mMixinHash
The string-based hash of string mixin names.
BranchPriorityHash mBranchHash
The string-based hash of priority branching logic.
UndoStack mUndoStack
The undo stack.
A repository of all things rules-related.
RulesCore is the HOSS of Cities3D.
The base class for any IRule or ILogic mixin objects.
The interface of the plugin engine.