>xabsl   The Extensible Agent Behavior Specification Language

XabslEngine Class Library Reference

 

XabslState.cpp

Go to the documentation of this file.
00001 /** 
00002 * @file XabslState.cpp
00003 *
00004 * Implementation of class State and helper classes
00005 *
00006 * @author <a href="http://www.sim.informatik.tu-darmstadt.de/pers/card/risler.html">Max Risler</a>
00007 */
00008 
00009 #include "XabslState.h"
00010 #include "XabslOption.h"
00011 
00012 namespace xabsl 
00013 {
00014 
00015 State* TransitionToState::getNextState()
00016 {
00017   return nextState;
00018 }
00019 
00020 State::State(const char* name, ErrorHandler& errorHandler,
00021                          const unsigned& time,
00022                          int optionIndex,
00023                          int index)
00024 : NamedItem(name), 
00025 targetState(false), 
00026 errorHandler(errorHandler), 
00027 decisionTree(0),
00028 time(time),
00029 optionIndex(optionIndex),
00030 index(index)
00031 {
00032 }
00033 
00034 State::~State()
00035 {
00036   if (decisionTree != 0) delete decisionTree;
00037   for (int i=0; i<actions.getSize(); i++)
00038     delete actions[i];
00039 }
00040 
00041 void State::create(InputSource& input,
00042                          NamedArray<Option*>& options,
00043                          NamedArray<BasicBehavior*>& basicBehaviors,
00044                          NamedArray<State*>& states,
00045                          Symbols& symbols)
00046 { 
00047   XABSL_DEBUG_INIT(errorHandler.message("creating state \"%s\"",n));
00048 
00049   char c[100]; 
00050   int i;
00051   
00052   // target state or not
00053   input.readString(c,1);
00054   if (*c == '1') 
00055   {
00056     targetState = true;
00057   }
00058   
00059   // subsequent option, basic behaviors and output symbol assignments
00060   int numberOfActions = (int)input.readValue();
00061   
00062   subsequentOption = 0;
00063   for(i = 0; i< numberOfActions; i++)
00064   {
00065     Action* action = 
00066       Action::create(input, 
00067                       options, 
00068                       basicBehaviors, 
00069                       symbols, 
00070                       *options[optionIndex], 
00071                       *this, 
00072                       errorHandler,
00073                       time);
00074     if (errorHandler.errorsOccurred)
00075     {
00076       errorHandler.error("XabslState::create(): could not create action for state \"%s\"",n);
00077       return;
00078     }
00079     if (subsequentOption == 0)
00080       if (ActionOption* actionOption = dynamic_cast<ActionOption*>(action)) 
00081         subsequentOption = actionOption->option;
00082     actions.append(action);
00083   }
00084   
00085   // transition to state or if / else block
00086   decisionTree = Statement::createStatement(input,errorHandler,symbols,*options[optionIndex],*this);
00087   if (errorHandler.errorsOccurred)
00088     errorHandler.error("XabslState::create(): could not create decision tree for state \"%s\"",n);
00089 }
00090 
00091 State* State::getNextState()
00092 {
00093   timeOfStateExecution = time - timeWhenStateWasActivated;
00094   
00095   State* nextState = decisionTree->getNextState();
00096   
00097   return nextState->coopCheck() ? nextState : this;
00098 }
00099 
00100 void State::reset()
00101 {
00102   timeWhenStateWasActivated = time;
00103   timeOfStateExecution = 0;
00104 }
00105 
00106 bool State::isTargetState() const
00107 {
00108   return targetState;
00109 }
00110 
00111 } // namespace
00112 

Up | Main Page | Generated at Wed Aug 19 17:32:29 2009.