VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Time Keeper Component

This subclass of the DtSimComponent will write the elapsed time to the DtTimeKeeperPSR every so often during its tick().


Header file:

/********************************************************************************* Copyright (c) 2004 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************//********************************************************************************* $RCSfile: timeKeeperCmpt.h,v $ $Revision: 1.3 $ $State: Exp $
*******************************************************************************/

//
//class DtTimeKeeperComponent
//
//This is a component which simply keeps track of the elapsed simulation time
//since the component was created.  It illustrates the creation and use of
//a process state repository to save the simulation time since creation
//into a checkpointed scenario file, and read it back in at load time.
//

#include "vrfobjcore/simComponent.h"

class DtTimeKeeperPSR;

//String which uniquely identifies this controller
const char TimeKeeperComponentType[] = "time-keeper-component";

class DtTimeKeeperComponent : public DtSimComponent
{
public:
   //constructor
   DtTimeKeeperComponent(const DtString & name, DtVrfObject* owner, 
      DtSimManager * simManager, DtComponentDescriptor* desc = NULL,
      DtReaderWriterRegistry * parentRegistry = 0);

   //destructor
   virtual ~DtTimeKeeperComponent();

   //copy constructor
   DtTimeKeeperComponent(const DtTimeKeeperComponent& orig);

   //assignment operator
   DtTimeKeeperComponent& operator=(const DtTimeKeeperComponent& orig);

   //Returns the string type for this component.
   virtual const char* type() const;

   //Overridden from the base class to call createPSR().
   virtual bool init();

   //Create a time-keeper process state repository, or a different 
   //type if it is specified in the OPD.
   virtual bool createPSR();

   //Update control values
   //For this controller, we simply add dT() to our elapsed time 
   //state variable, and print out the value every 10 seconds of 
   //simulation time.
   virtual void tick();

   //This is called by the factory
   static DtSimComponent* creator(const DtString & name, DtVrfObject* owner,
      DtSimManager * simManager, DtComponentDescriptor* desc,
      DtReaderWriterRegistry * parentRegistry);

protected:
   //This is the pointer to our process state repository used during
   //the execution of the simulation.
   DtTimeKeeperPSR* myProcessState;
};


Implementation:

/*******************************************************************************
** Copyright (c) 2011 MAK Technologies, Inc.
** All rights reserved.
*******************************************************************************/

//
//class DtTimeKeeperComponent
//

#include "timeKeeperCmpt.h"
#include "timeKeeperPSR.h"
#include "vrfobjparam/componentDescriptor.h"
#include "vrfobjcore/vrfProcessStateRepositoryManager.h"
#include "vrfobjcore/vrfObject.h"

//constructor
DtTimeKeeperComponent::DtTimeKeeperComponent(const DtString & name, 
   DtVrfObject* owner, DtSimManager * simManager, DtComponentDescriptor* desc,
   DtReaderWriterRegistry * parentRegistry) :
   DtSimComponent(name, owner, simManager, desc, 
      parentRegistry),
   myProcessState(NULL)
{
}

//destructor
//Since this class created the process state repository, it must also
//delete it when it is through.
DtTimeKeeperComponent::~DtTimeKeeperComponent()
{
   if (myProcessState)
   {
      psrManager()->removeProcessStateRepository(myProcessState);

      delete myProcessState;
      myProcessState = 0;
   }
}

const char* DtTimeKeeperComponent::type() const
{
   return TimeKeeperComponentType;
}

//First call down to the base class init.
//Call createPSR() to create a process state repository.
bool DtTimeKeeperComponent::init()
{
   if (!DtSimComponent::init())
   {
      return false;
   }

   if (!createPSR())
   {
      return false;
   }

   return true;
}

//Creates the process state repository that will be used by this 
//component (and possibly others) .
//A type and name are needed to create a PSR.
//These can be specified in the component descriptor usnig 
//"process-state-repository-type" and "process-state-repository-name" 
//parameters.  If nothing is specified, the defaults defined in this
//funtion are used.
//The name of the prorocess state repository is the name used when
//the entity is saved into a checkpoint file.  The names of all process
//state repositories on a single entity must be unique, but the types need
//not be.
bool DtTimeKeeperComponent::createPSR()
{
   //create the process state repository
   DtString processSRName = DtString::nullString();
   DtString processSRType = DtString::nullString();

   if (myComponentDescriptor)
   {
      processSRName = myComponentDescriptor->processStateRepositoryName();
      processSRType = myComponentDescriptor->processStateRepositoryType();
   }

   //For backwards compatibility, when name/type aren't in descriptor
   if (processSRName == DtString::nullString())
   {
      processSRName = 
         "time-keeper-psr";
   }

   if (processSRType == DtString::nullString())
   {
      processSRType = 
         TimeKeeperPSRType;
   }

   myProcessState = (DtTimeKeeperPSR*)
      DtVrfProcessStateRepository::createRepository(processSRName, 
      processSRType, myEntity);

   if (!myProcessState)
   {
      objectConsoleWarn() << "addPSR Example - Failed to create time keeper process state repository" << std::endl;
      return false;
   }

   //Add the new process SR to the process SR manager
   //This allows other components to access this PSR.
   //The process SR manager will also take care of reading and
   //writing the PSR to and from the oob file.
   //In the case of loading from an OOB file, the values will
   //be filled in after this function is called.
   psrManager()->addProcessStateRepository(myProcessState);

   return true;
}


//calculate new control values
void DtTimeKeeperComponent::tick()
{
   if (dT() > 0)
   {
      //We will always store our checkpoint-enabled state variables
      //inside the process state repository since a checkpoint file
      //can be written at any time.
      //Member variables of this component will not be saved in a checkpoint.
      double newTime = myProcessState->elapsedTime() + dT();
   
      //Print to the object console every 10 seconds to provide feedback that
      //the controller is working. 
      if ( ((int) myProcessState->elapsedTime() < (int) newTime) &&
         ((int) newTime % 10 == 0))
      {
         objectConsoleInfo() << "addPSR Example - " << 
            "Elapsed sim time since creation: " << newTime << std::endl;
      }
      myProcessState->setElapsedTime(newTime);
   }

}

DtSimComponent* DtTimeKeeperComponent::creator(const DtString& name, 
   DtVrfObject* owner, DtSimManager* simManager, DtComponentDescriptor* desc,
   DtReaderWriterRegistry* parentRegistry)
{
   return 
      new DtTimeKeeperComponent(name, owner, simManager, desc, parentRegistry);
}


Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)