StateInstanceFactory.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Christian Mandery (christian.mandery at kit dot edu)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "StateInstanceFactory.h"
24 #include "XmlReader.h"
25 
26 #include "../../StatechartViewerPlugin/model/stateinstance/EndState.h"
27 #include "../../StatechartViewerPlugin/model/stateinstance/LocalState.h"
28 #include "../../StatechartViewerPlugin/model/stateinstance/RemoteState.h"
29 #include "../../StatechartViewerPlugin/model/stateinstance/DynamicRemoteState.h"
30 
31 
32 using namespace armarx::exceptions::local;
33 using namespace armarx::statechartio;
34 using namespace armarx::statechartmodel;
35 
36 // TODO: This function is copied from XmlReader. The code duplication will be resolved once we use the RapidXmlWrapper here.
37 QString readAttribute(rapidxml::xml_node<>* node, const QString& attributeName, bool required = true)
38 {
39  rapidxml::xml_attribute<>* attribute = node->first_attribute(attributeName.toUtf8());
40 
41  if (!attribute)
42  {
43  if (required)
44  {
45  throw XmlReaderException("Attribute \"" + attributeName.toStdString() + "\" for \"" + node->name() + "\" node not found!");
46  }
47  else
48  {
49  return "";
50  }
51  }
52 
53  return QString::fromUtf8(attribute->value());
54 }
55 
56 StateInstanceFactoryBase::SubClassRegistry EndStateInstanceFactory::registry(EndStateInstanceFactory::getName(), &EndStateInstanceFactory::createInstance);
57 StateInstanceFactoryBase::SubClassRegistry LocalStateInstanceFactory::registry(LocalStateInstanceFactory::getName(), &LocalStateInstanceFactory::createInstance);
58 StateInstanceFactoryBase::SubClassRegistry RemoteStateInstanceFactory::registry(RemoteStateInstanceFactory::getName(), &RemoteStateInstanceFactory::createInstance);
59 StateInstanceFactoryBase::SubClassRegistry DynamicRemoteStateInstanceFactory::registry(DynamicRemoteStateInstanceFactory::getName(), &DynamicRemoteStateInstanceFactory::createInstance);
60 
61 StateInstanceFactoryBase::StateInstanceFactoryBase(rapidxml::xml_node<>* xmlNode, armarx::statechartmodel::StatePtr parentState) :
62  xmlNode(xmlNode),
63  parentState(parentState) {}
64 
66  StateInstanceFactoryBase(xmlNode, parentState) {}
67 
69 {
70  QString instanceName = readAttribute(xmlNode, "name");
71  QString eventName = readAttribute(xmlNode, "event");
72 
73  EndStatePtr endStateInstance(new EndState(instanceName, eventName, parentState));
74  endStateInstance->setPosition(QPointF(readAttribute(xmlNode, "left").toFloat(), readAttribute(xmlNode, "top").toFloat()));
75  endStateInstance->setBoundingBox(readAttribute(xmlNode, "boundingSquareSize").toFloat());
76 
77  return endStateInstance;
78 }
79 
81 {
82  StateInstanceFactoryBasePtr factoryPtr(new EndStateInstanceFactory(xmlParentPair.first, xmlParentPair.second));
83  return factoryPtr;
84 }
85 
87  StateInstanceFactoryBase(xmlNode, parentState) {}
88 
90 {
91  QString uuid = readAttribute(xmlNode, "refuuid");
92  QString instanceName = readAttribute(xmlNode, "name");
93 
94  LocalStatePtr localStateInstance(new LocalState(uuid, instanceName, parentState));
95  localStateInstance->setPosition(QPointF(readAttribute(xmlNode, "left").toFloat(), readAttribute(xmlNode, "top").toFloat()));
96  localStateInstance->setBoundingBox(readAttribute(xmlNode, "boundingSquareSize").toFloat());
97 
98  return localStateInstance;
99 }
100 
102 {
103  StateInstanceFactoryBasePtr factoryPtr(new LocalStateInstanceFactory(xmlParentPair.first, xmlParentPair.second));
104  return factoryPtr;
105 }
106 
108  StateInstanceFactoryBase(xmlNode, parentState) {}
109 
111 {
112  QString uuid = readAttribute(xmlNode, "refuuid");
113  QString instanceName = readAttribute(xmlNode, "name");
114 
115  statechartmodel::RemoteStatePtr remoteStateInstance(new statechartmodel::RemoteState(uuid, instanceName, parentState));
116  remoteStateInstance->proxyName = readAttribute(xmlNode, "proxyName");
117  remoteStateInstance->setPosition(QPointF(readAttribute(xmlNode, "left").toFloat(), readAttribute(xmlNode, "top").toFloat()));
118  remoteStateInstance->setBoundingBox(readAttribute(xmlNode, "boundingSquareSize").toFloat());
119 
120  return remoteStateInstance;
121 }
122 
124 {
125  StateInstanceFactoryBasePtr factoryPtr(new RemoteStateInstanceFactory(xmlParentPair.first, xmlParentPair.second));
126  return factoryPtr;
127 }
128 
129 
131  StateInstanceFactoryBase(xmlNode, parentState) {}
132 
134 {
135  QString instanceName = readAttribute(xmlNode, "name");
136  QString uuid = readAttribute(xmlNode, "refuuid");
137  DynamicRemoteStatePtr dynamicRemoteStateInstance(new statechartmodel::DynamicRemoteState(uuid, instanceName, parentState));
138  dynamicRemoteStateInstance->setPosition(QPointF(readAttribute(xmlNode, "left").toFloat(), readAttribute(xmlNode, "top").toFloat()));
139  dynamicRemoteStateInstance->setBoundingBox(readAttribute(xmlNode, "boundingSquareSize").toFloat());
140 
141  return dynamicRemoteStateInstance;
142 }
143 
145 {
146  StateInstanceFactoryBasePtr factoryPtr(new DynamicRemoteStateInstanceFactory(xmlParentPair.first, xmlParentPair.second));
147  return factoryPtr;
148 }
149 
150 
armarx::exceptions::local::XmlReaderException
Definition: XmlReader.h:103
XmlReader.h
armarx::statechartio
Definition: StateInstanceFactory.h:31
armarx::statechartio::StateInstanceFactoryBase::xmlNode
rapidxml::xml_node * xmlNode
Definition: StateInstanceFactory.h:44
armarx::statechartmodel::RemoteState
Definition: RemoteState.h:28
armarx::statechartmodel::EndStatePtr
std::shared_ptr< EndState > EndStatePtr
Definition: EndState.h:44
armarx::statechartio::DynamicRemoteStateInstanceFactory::getStateInstance
armarx::statechartmodel::StateInstancePtr getStateInstance() override
Definition: StateInstanceFactory.cpp:133
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:97
rapidxml::xml_node::first_attribute
xml_attribute< Ch > * first_attribute(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets first attribute of node, optionally matching attribute name.
Definition: rapidxml.hpp:1151
armarx::statechartio::DynamicRemoteStateInstanceFactory::DynamicRemoteStateInstanceFactory
DynamicRemoteStateInstanceFactory(rapidxml::xml_node<> *xmlNode, armarx::statechartmodel::StatePtr parentState)
Definition: StateInstanceFactory.cpp:130
armarx::statechartmodel::StateInstancePtr
std::shared_ptr< StateInstance > StateInstancePtr
Definition: StateInstance.h:138
rapidxml::xml_attribute
Class representing attribute node of XML document.
Definition: rapidxml.hpp:139
armarx::statechartio::RemoteStateInstanceFactory::createInstance
static StateInstanceFactoryBasePtr createInstance(XmlParentPair)
Definition: StateInstanceFactory.cpp:123
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::statechartio::EndStateInstanceFactory::createInstance
static StateInstanceFactoryBasePtr createInstance(XmlParentPair)
Definition: StateInstanceFactory.cpp:80
armarx::statechartio::LocalStateInstanceFactory::createInstance
static StateInstanceFactoryBasePtr createInstance(XmlParentPair)
Definition: StateInstanceFactory.cpp:101
armarx::statechartmodel::LocalStatePtr
std::shared_ptr< LocalState > LocalStatePtr
Definition: LocalState.h:43
armarx::statechartio::DynamicRemoteStateInstanceFactory::createInstance
static StateInstanceFactoryBasePtr createInstance(XmlParentPair)
Definition: StateInstanceFactory.cpp:144
armarx::exceptions::local
Definition: DynamicLibraryException.h:31
armarx::statechartmodel::DynamicRemoteState
Definition: DynamicRemoteState.h:29
armarx::statechartmodel
Definition: XmlWriter.h:36
readAttribute
QString readAttribute(rapidxml::xml_node<> *node, const QString &attributeName, bool required=true)
Definition: StateInstanceFactory.cpp:37
armarx::statechartio::LocalStateInstanceFactory::getStateInstance
armarx::statechartmodel::StateInstancePtr getStateInstance() override
Definition: StateInstanceFactory.cpp:89
armarx::statechartio::LocalStateInstanceFactory::LocalStateInstanceFactory
LocalStateInstanceFactory(rapidxml::xml_node<> *xmlNode, armarx::statechartmodel::StatePtr parentState)
Definition: StateInstanceFactory.cpp:86
StateInstanceFactory.h
armarx::statechartmodel::EndState
Definition: EndState.h:28
armarx::statechartio::RemoteStateInstanceFactory::getStateInstance
armarx::statechartmodel::StateInstancePtr getStateInstance() override
Definition: StateInstanceFactory.cpp:110
armarx::statechartio::StateInstanceFactoryBasePtr
std::shared_ptr< StateInstanceFactoryBase > StateInstanceFactoryBasePtr
Definition: StateInstanceFactory.h:48
rapidxml::xml_node
Class representing a node of XML document.
Definition: rapidxml.hpp:138
armarx::statechartmodel::StatePtr
std::shared_ptr< State > StatePtr
Definition: State.h:46
armarx::statechartio::StateInstanceFactoryBase
Definition: StateInstanceFactory.h:35
armarx::statechartio::EndStateInstanceFactory::getStateInstance
armarx::statechartmodel::StateInstancePtr getStateInstance() override
Definition: StateInstanceFactory.cpp:68
armarx::statechartmodel::LocalState
Definition: LocalState.h:31
armarx::statechartio::XmlParentPair
std::pair< rapidxml::xml_node<> *, armarx::statechartmodel::StatePtr > XmlParentPair
Definition: StateInstanceFactory.h:33
armarx::statechartmodel::RemoteStatePtr
std::shared_ptr< RemoteState > RemoteStatePtr
Definition: RemoteState.h:43
armarx::statechartio::StateInstanceFactoryBase::parentState
armarx::statechartmodel::StatePtr parentState
Definition: StateInstanceFactory.h:45
armarx::statechartio::RemoteStateInstanceFactory::RemoteStateInstanceFactory
RemoteStateInstanceFactory(rapidxml::xml_node<> *xmlNode, armarx::statechartmodel::StatePtr parentState)
Definition: StateInstanceFactory.cpp:107
armarx::statechartio::EndStateInstanceFactory::EndStateInstanceFactory
EndStateInstanceFactory(rapidxml::xml_node<> *xmlNode, armarx::statechartmodel::StatePtr parentState)
Definition: StateInstanceFactory.cpp:65