[openrtm-commit:00654] r2299 - trunk/OpenRTM-aist/src/lib/rtm

openrtm @ openrtm.org openrtm @ openrtm.org
2012年 2月 4日 (土) 02:28:21 JST


Author: n-ando
Date: 2012-02-04 02:28:19 +0900 (Sat, 04 Feb 2012)
New Revision: 2299

Added:
   trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.cpp
   trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.h
Log:
[incompat,header/impl,func] RTObjectStateMachine class was implemented. refs #2345

Added: trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.cpp	                        (rev 0)
+++ trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.cpp	2012-02-03 17:28:19 UTC (rev 2299)
@@ -0,0 +1,331 @@
+// -*- C++ -*-
+/*!
+ * @file RTObjectStateMachine.cpp
+ * @brief ExecutionContext's state machine worker class
+ * @date $Date$
+ * @author Noriaki Ando <n-ando at aist.go.jp>
+ *
+ * Copyright (C) 2011
+ *     Noriaki Ando
+ *     Intelligent Systems Research Institute,
+ *     National Institute of
+ *         Advanced Industrial Science and Technology (AIST), Japan
+ *     All rights reserved.
+ *
+ * $Id$
+ *
+ */
+
+#include <rtm/RTObjectStateMachine.h>
+#include <iostream>
+#include <stdio.h>
+
+namespace RTC_impl
+{
+  RTObjectStateMachine::RTObjectStateMachine(RTC::ExecutionContextHandle_t id,
+                                             RTC::LightweightRTObject_ptr comp)
+    : m_id(id),
+      m_rtobj(RTC::LightweightRTObject::_duplicate(comp)),
+      m_sm(NUM_OF_LIFECYCLESTATE),
+      m_ca(false), m_dfc(false), m_fsm(false), m_mode(false)
+  {
+    m_caVar   = RTC::ComponentAction::_nil();
+    m_dfcVar  = RTC::DataFlowComponentAction::_nil();
+    m_fsmVar  = RTC::FsmParticipantAction::_nil();
+    m_modeVar = RTC::MultiModeComponentAction::_nil();
+    // Setting Action callback objects
+    setComponentAction(comp);
+    setDataFlowComponentAction(comp);
+    setFsmParticipantAction(comp);
+    setMultiModeComponentAction(comp);
+    // Setting callback functions to StateMachine
+    m_sm.setListener(this);
+    m_sm.setEntryAction (RTC::ACTIVE_STATE,
+                         &RTObjectStateMachine::onActivated);
+    m_sm.setDoAction    (RTC::ACTIVE_STATE,
+                         &RTObjectStateMachine::onExecute);
+    m_sm.setPostDoAction(RTC::ACTIVE_STATE,
+                         &RTObjectStateMachine::onStateUpdate);
+    m_sm.setExitAction  (RTC::ACTIVE_STATE,
+                         &RTObjectStateMachine::onDeactivated);
+    m_sm.setEntryAction (RTC::ERROR_STATE,
+                         &RTObjectStateMachine::onAborting);
+    m_sm.setDoAction    (RTC::ERROR_STATE,
+                         &RTObjectStateMachine::onError);
+    m_sm.setExitAction  (RTC::ERROR_STATE,
+                         &RTObjectStateMachine::onReset);
+    // Setting inital state
+    ExecContextStates st;
+    st.prev = RTC::INACTIVE_STATE;
+    st.curr = RTC::INACTIVE_STATE;
+    st.next = RTC::INACTIVE_STATE;
+    m_sm.setStartState(st);
+    m_sm.goTo(RTC::INACTIVE_STATE);
+  }
+
+  RTObjectStateMachine::~RTObjectStateMachine(void)
+  {
+    if (!m_ca)
+      {
+        m_ca = false;
+        m_caVar = RTC::ComponentAction::_nil();
+      }
+    if (m_dfc)
+      {
+        m_dfc = false;
+        m_dfcVar = RTC::DataFlowComponentAction::_nil();
+      }
+    if (m_fsm)
+      {
+        m_fsm = false;
+        m_fsmVar = RTC::FsmParticipantAction::_nil();
+      }
+    if (m_mode)
+      {
+        m_mode = true;
+        m_modeVar = RTC::MultiModeComponentAction::_nil();
+      }
+  }
+
+//  RTObjectStateMachine::RTObjectStateMachine(const RTObjectStateMachine& other)
+//    : m_id(other.m_id),
+//      m_rtobj(RTC::LightweightRTObject::_duplicate(other.m_rtobj)),
+//      m_sm(other.m_sm),
+//      m_ca(NULL), m_dfc(NULL),
+//      m_fsm(NULL), m_mode(NULL)
+//  {
+//    // Setting Action callback objects
+//    RTC::LightweightRTObject_ptr comp = m_rtobj;
+//    setComponentAction(comp);
+//    setDataFlowComponentAction(comp);
+//    setFsmParticipantAction(comp);
+//    setMultiModeComponentAction(comp);
+//    m_sm.setListener(this);
+//    m_sm.setEntryAction (RTC::ACTIVE_STATE,
+//                         &RTObjectStateMachine::onActivated);
+//    m_sm.setDoAction    (RTC::ACTIVE_STATE,
+//                         &RTObjectStateMachine::onExecute);
+//    m_sm.setPostDoAction(RTC::ACTIVE_STATE,
+//                         &RTObjectStateMachine::onStateUpdate);
+//    m_sm.setExitAction  (RTC::ACTIVE_STATE,
+//                         &RTObjectStateMachine::onDeactivated);
+//    m_sm.setEntryAction (RTC::ERROR_STATE,
+//                         &RTObjectStateMachine::onAborting);
+//    m_sm.setDoAction    (RTC::ERROR_STATE,
+//                         &RTObjectStateMachine::onError);
+//    m_sm.setExitAction  (RTC::ERROR_STATE,
+//                         &RTObjectStateMachine::onReset);
+//  }
+//
+//  RTObjectStateMachine& RTObjectStateMachine::
+//  operator=(const RTObjectStateMachine& other)
+//  {
+//    RTObjectStateMachine temp(other);
+//    swap(temp);
+//    return *this;
+//  }
+//
+//  void RTObjectStateMachine::swap(RTObjectStateMachine& other) throw ()
+//  {
+//    std::swap(m_id, other.m_id);
+//    m_sm.swap(other.m_sm);
+//    std::swap(m_ca, other.m_ca);
+//    std::swap(m_dfc, other.m_dfc);
+//    std::swap(m_fsm, other.m_fsm);
+//    std::swap(m_mode, other.m_mode);
+//  }
+
+  RTC::LightweightRTObject_ptr RTObjectStateMachine::getRTObject()
+  {
+    return RTC::LightweightRTObject::_duplicate(m_rtobj);
+  }
+
+  bool RTObjectStateMachine::isEquivalent(RTC::LightweightRTObject_ptr comp)
+  {
+    return m_rtobj->_is_equivalent(comp);
+  }
+
+  RTC::ExecutionContextHandle_t RTObjectStateMachine::
+  getExecutionContextHandle()
+  {
+    return m_id;
+  }
+
+  void RTObjectStateMachine::
+  setComponentAction(const RTC::LightweightRTObject_ptr comp)
+  {
+    m_caVar = RTC::ComponentAction::_narrow(comp);
+    if (!CORBA::is_nil(m_caVar)) { m_ca = true; }
+  }
+
+  void RTObjectStateMachine::
+  setDataFlowComponentAction(const RTC::LightweightRTObject_ptr comp)
+  {
+    m_dfcVar = RTC::DataFlowComponentAction::_narrow(comp);
+    if (!CORBA::is_nil(m_dfcVar)) { m_dfc = true; }
+  }
+
+  void RTObjectStateMachine::
+  setFsmParticipantAction(const RTC::LightweightRTObject_ptr comp)
+  {
+    m_fsmVar = RTC::FsmParticipantAction::_narrow(comp);
+    if (!CORBA::is_nil(m_fsmVar)) { m_fsm = true; }
+  }
+
+  void RTObjectStateMachine::
+  setMultiModeComponentAction(const RTC::LightweightRTObject_ptr comp)
+  {
+    m_modeVar = RTC::MultiModeComponentAction::_narrow(comp);
+    if (!CORBA::is_nil(m_modeVar)) { m_mode = true; }
+  }
+
+  // RTC::ComponentAction operations
+  void RTObjectStateMachine::onStartup(void)
+  {
+    if (!m_ca) { return; }
+    m_caVar->on_startup(m_id);
+  }
+  void RTObjectStateMachine::onShutdown(void)
+  {
+    if (!m_ca) { return; }
+    m_caVar->on_shutdown(m_id);
+  }
+  void RTObjectStateMachine::onActivated(const ExecContextStates& st)
+  {
+    if (!m_ca) { return; }
+    if (m_caVar->on_activated(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+
+  void RTObjectStateMachine::onDeactivated(const ExecContextStates& st)
+  {
+    if (!m_ca) { return; }
+    m_caVar->on_deactivated(m_id);
+  }
+
+  void RTObjectStateMachine::onAborting(const ExecContextStates& st)
+  {
+    if (!m_ca) { return; }
+    m_caVar->on_error(m_id);
+  }
+
+  void RTObjectStateMachine::onError(const ExecContextStates& st)
+  {
+    if (!m_ca) { return; }
+    m_caVar->on_error(m_id);
+  }
+
+  void RTObjectStateMachine::onReset(const ExecContextStates& st)
+  {
+    if (!m_ca) { return; }
+    if (m_caVar->on_reset(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+
+  // RTC::DataflowComponentAction
+  void RTObjectStateMachine::onExecute(const ExecContextStates& st)
+  {
+    if (!m_dfc) { return; }
+    if (m_dfcVar->on_execute(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+
+  void RTObjectStateMachine::onStateUpdate(const ExecContextStates& st)
+  {
+    if (!m_dfc) { return; }
+    if (m_dfcVar->on_state_update(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+
+  void RTObjectStateMachine::onRateChanged(void)
+  {
+    if (!m_dfc) { return; }
+    if (m_dfcVar->on_rate_changed(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+
+  // FsmParticipantAction
+  void RTObjectStateMachine::onAction(const ExecContextStates& st)
+  {
+    if (!m_fsm) { return; }
+    if (m_fsmVar->on_action(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+  // MultiModeComponentAction
+  void RTObjectStateMachine::onModeChanged(const ExecContextStates& st)
+  {
+    if (!m_mode) { return; }
+    if (m_modeVar->on_mode_changed(m_id) != RTC::RTC_OK)
+      {
+        m_sm.goTo(RTC::ERROR_STATE);
+        return;
+      }
+    return;
+  }
+
+  // Getting state of the context
+  ExecContextState RTObjectStateMachine::getState(void)
+  {
+    return m_sm.getState();
+  }
+
+  ExecContextStates RTObjectStateMachine::getStates(void)
+  {
+    return m_sm.getStates();
+  }
+
+  bool RTObjectStateMachine::isCurrentState(ExecContextState state)
+  {
+    return getState() == state;
+  }
+
+  bool RTObjectStateMachine::isNextState(ExecContextState state)
+  {
+    return m_sm.getStates().next == state;
+  }
+
+  void RTObjectStateMachine::goTo(ExecContextState state)
+  {
+    m_sm.goTo(state);
+  }
+
+  // Workers
+  void RTObjectStateMachine::workerPreDo(void)
+  {
+    return m_sm.worker_pre();
+  }
+
+  void RTObjectStateMachine::workerDo(void)
+  {
+    return m_sm.worker_do();
+  }
+
+  void RTObjectStateMachine::workerPostDo(void)
+  {
+    return m_sm.worker_post();
+  }
+}; // namespace RTC_impl

Added: trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.h	                        (rev 0)
+++ trunk/OpenRTM-aist/src/lib/rtm/RTObjectStateMachine.h	2012-02-03 17:28:19 UTC (rev 2299)
@@ -0,0 +1,125 @@
+// -*- C++ -*-
+/*!
+ * @file RTObjectStateMachine.h
+ * @brief ExecutionContext's state machine worker class
+ * @date $Date$
+ * @author Noriaki Ando <n-ando at aist.go.jp>
+ *
+ * Copyright (C) 2011
+ *     Noriaki Ando
+ *     Intelligent Systems Research Institute,
+ *     National Institute of
+ *         Advanced Industrial Science and Technology (AIST), Japan
+ *     All rights reserved.
+ *
+ * $Id$
+ *
+ */
+
+#ifndef RTC_IMPL_RTOBJECTSTATEMACHINE_H
+#define RTC_IMPL_RTOBJECTSTATEMACHINE_H
+
+#include <stdlib.h>
+#include <rtm/SystemLogger.h>
+#include <coil/NonCopyable.h>
+#include <rtm/idl/RTCSkel.h>
+#include <rtm/StateMachine2.h>
+#include <assert.h>
+#include <iostream>
+
+#define NUM_OF_LIFECYCLESTATE 4
+
+#ifdef WIN32
+#pragma warning( disable : 4290 )
+#endif
+
+namespace RTC_impl
+{
+  typedef RTC::ExecutionContextHandle_t Ecid;
+  typedef RTC::LifeCycleState ExecContextState;
+  typedef RTC_Utils::StateHolder<ExecContextState> ExecContextStates;
+
+  class RTObjectStateMachine
+    : private coil::NonCopyable 
+  {
+  public:
+    RTObjectStateMachine(RTC::ExecutionContextHandle_t id,
+                         RTC::LightweightRTObject_ptr comp);
+    virtual ~RTObjectStateMachine(void);
+//    RTObjectStateMachine(const RTObjectStateMachine& rtobjsm);
+//    RTObjectStateMachine& operator=(const RTObjectStateMachine& rtobjsm);
+//    void swap(RTObjectStateMachine& rtobjsm) throw();
+
+    // functions for stored RTObject reference
+    RTC::LightweightRTObject_ptr getRTObject();
+    bool isEquivalent(RTC::LightweightRTObject_ptr comp);
+
+    RTC::ExecutionContextHandle_t getExecutionContextHandle();
+
+    // RTC::ComponentAction operations
+    void onStartup(void);
+    void onShutdown(void);
+    void onActivated(const ExecContextStates& st);
+    void onDeactivated(const ExecContextStates& st);
+    void onAborting(const ExecContextStates& st);
+    void onError(const ExecContextStates& st);
+    void onReset(const ExecContextStates& st);
+
+    // RTC::DataflowComponentAction
+    void onExecute(const ExecContextStates& st);
+    void onStateUpdate(const ExecContextStates& st);
+    void onRateChanged(void);
+
+    // FsmParticipantAction
+    void onAction(const ExecContextStates& st);
+
+    // MultiModeComponentAction
+    void onModeChanged(const ExecContextStates& st);
+
+    // Getting state of the context
+    ExecContextState getState(void);
+    ExecContextStates getStates(void);
+    bool isCurrentState(ExecContextState state);
+    bool isNextState(ExecContextState state);
+    void goTo(ExecContextState state);
+    
+    // Workers
+    void workerPreDo(void);
+    void workerDo(void);
+    void workerPostDo(void);
+
+  protected:
+    void setComponentAction(const RTC::LightweightRTObject_ptr comp);
+    void setDataFlowComponentAction(const RTC::LightweightRTObject_ptr comp);
+    void setFsmParticipantAction(const RTC::LightweightRTObject_ptr comp);
+    void setMultiModeComponentAction(const RTC::LightweightRTObject_ptr comp);
+
+  private: // member variables
+    RTC::Logger rtclog;
+    // ExecutionContext id
+    RTC::ExecutionContextHandle_t m_id;
+    // Associated RTObject reference
+    RTC::LightweightRTObject_var m_rtobj;
+    // State machine
+    RTC_Utils::StateMachine<ExecContextState,
+                            RTObjectStateMachine> m_sm;
+    bool m_ca;
+    bool m_dfc;
+    bool m_fsm;
+    bool m_mode;
+    RTC::ComponentAction_var          m_caVar;
+    RTC::DataFlowComponentAction_var  m_dfcVar;
+    RTC::FsmParticipantAction_var     m_fsmVar;
+    RTC::MultiModeComponentAction_var m_modeVar;
+
+    //    char dara[1000];
+    // Component action invoker
+    
+  };
+}; // namespace RTC
+
+#ifdef WIN32
+#pragma warning( default : 4290 )
+#endif
+
+#endif // RTC_RTOBJECTSTATEMACHINE_H



openrtm-commit メーリングリストの案内