![]() |
VR-Forces 5.0.2 Developer's Guide
|
The Add Conditional example demonstrates the following:
The Add Conditional example adds a new conditional evaulator, MyEvaluator, that will be used to test whether or not the requested entity is facing north (heading of 0 degrees)
To add a new kind of conditional expression to VR-Forces, you must create two new derived classes: a new conditional expression data class (DtSimCondExpr), and a new conditional expression evaluator class (DtEvaluator). The conditional expression data class represents the (optional) data parameters used in your conditional expression (for example, the name of an area to test in the entity-in-area class, DtCeEntInArea). The evaluator class contains an evaluate() member function that performs the test for the given DtSimCondExpr (for example, the DtEvalEntInArea class tests for the entity in area condition for a given DtCeEntInArea object).
To add a new kind of conditional expression data class, you must derive a new class from DtSimCondExpr or one of its subclasses. This example derives the class vrfAddConditionalExample::MyConditionalExpression directly from DtSimCondExpr. The new class must provide the member functions described below:
| DtSimCondExpr::type() | Returns an integer uniquely identifying the kind of conditional expression. Choose a type that does not conflict with any existing types (existing types are defined in condExprTypes.h). |
| DtSimCondExpr::clone() | Returns a newly allocated copy of this object. |
| DtSimCondExpr::netRepSize() | Returns the size of the network representation of the object. |
| DtSimCondExpr::stringRep() | Returns a string representation for the expression. |
| DtSimCondExpr::setFromNet() | Sets the member data in the class from a network representation of the class. |
| DtSimCondExpr::setToNet() | Maps the member data in the class to the network representation for the class. |
| DtSimCondExpr::creator() | Returns a newly created instance of the class. |
The new class must implement the netRepSize(), setToNet(), and setFromNet() member functions to make the conditional expression capable of being sent over the network. For examples and a discussion of how to implement these functions, please see Creating the Network Representation.
After creation of the new DtSimCondExpr class, the new conditional expression class must be registered with the conditional expression factory. Adding it to the factory makes the class available for use any time the new type is encountered (such as when reading from a plan file, or when receiving a plan containing the new expression over the network). For more information about how to register a new conditional expression class with the factory, please see VR-Forces Factories. Registration of the new DtSimCondExpr, in our example done in vrfAddConditionalExample::DtInitializeVrfPlugin, looks like this:
where vrfAddConditionalExample::MyConditionalExpression is the new DtSimCondExpr class, vrfAddConditionalExample::DtCondExprFacingNorthType is the new integer returned in implementation of vrfAddConditionalExample::MyConditionalExpression::type(), and vrfAddConditionalExample::DtCeFacingNorthTypeName is the string type which represents vrfAddConditionalExample::MyConditionalExpression in plan files. It must not contain any spaces.
To add a new evaluator class for a new conditional expression, a new class must be derived from DtEvaluator (evaluator.h) or one of its subclasses. This example derives the new evaluator class vrfAddConditionalExample::MyEvaluator directly from DtEvaluator. The new class you must provide the member functions described below.
| DtEvaluator::evaluate() | Evaluates the given DtSimCondExpr. |
| DtEvaluator::clone() | Returns a newly allocated copy of this object. |
| DtEvaluator::creator() | Returns a newly created instance of the class. |
Just as with the new DtSimCondExpr class, the new evaluator class must be registered with the evaluator factory. Care must be taken that it is registered with the same integer type assigned to the new DtSimCondExpr class. In our example:
where vrfAddConditionalExample::MyEvaluator is the new DtEvaluator class, and vrfAddConditionalExample::DtCondExprFacingNorthType is the integer type registed with the conditional factory and returned in the implementation of vrfAddConditionalExample::MyConditionalExpression::type().
The evaluate() function is where the conditional expression test to be performed is implemented. The evaluate() function takes a DtSimCondExpr* as one of its arguments. If new DtEvaluator class has been properly registered with the evaluator factory using the same integer type as the new DtSimCondExpr, the incoming base DtSimCondExpr* may be safely downcast to vrfAddConditionalExample::myConditionalExpression, the new derived class type.
Note again that the new DtEvaluator class and the new DtSimCondExpr must be registered with the factory using the same integer type. In our example, we set this to be a constant, vrfAddConditionalExample::DtCondExprFacingNorthType, in addConditional/addConditionalSim/conditionalExpression.h. Failure to maintain a consistent and unique integer type will result in undefined behavior.
Once the new DtSimCondExpr and DtEvaluator subclasses are created and registered, the new expression can be used in VR-Forces. VR-Forces takes care of creating and configuring the new conditional expression and its corresponding evaluator whenever it encounters them (such as when reading in plan files, or when receiving plans over the network).
Since this example is loaded as a plugin, it can be used in conjunctio nwith the released VR-Forces application
This example demonstrates how to add a new kind of conditional test that can be used within a conditional expression.
In the Launcher, click the Plug-ins button to bring up the Plug-ins Selection dialog. Enable the plugin.
To view the new behavior:
| MyConditionalExpression | The subclass of DtSimCondExpr that is used to communicate the new conditional. |
| MyEvaluator | The subclass of DtEvaluator that evaluates the MyConditionalExpression. |
| MySearch | Used by MyEvaluator to test the MyConditionalExpression for truth. |