Newer
Older
Francois Bleibel
committed
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Copyright Projet JRL-Japan, 2007
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* File: pool.cpp
* Project: DYNAMIC-GRAPH
* Author: François Bleibel, Nicolas Mansard
Francois Bleibel
committed
*
* Version control
* ===============
*
* $Id$
*
* Description
* ============
*
*
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */
Francois Bleibel
committed
#include <dynamic-graph/pool.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
Francois Bleibel
committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <list>
#include <typeinfo>
using namespace dynamicgraph;
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
PoolStorage::
~PoolStorage( void )
{
dgDEBUGIN(15);
for( Entities::iterator iter=entity.begin();iter!=entity.end(); iter=entity.begin())
{
dgDEBUG(15) << "Delete \""
<< (iter->first) <<"\""<<std::endl;
delete ((Entity*)iter->second);
}
dgDEBUGOUT(15);
return;
}
/* --------------------------------------------------------------------- */
void PoolStorage::
registerEntity( const std::string& entname,Entity* ent )
{
Entities::iterator entkey = entity.find(entname);
if( entkey != entity.end() ) // key does exist
{
throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
"Another entity already defined with the same name. ",
"Entity name is <%s>.",entname.c_str() );
}
else
{
dgDEBUG(10) << "Register entity <"<< entname
<< "> in the pool." <<std::endl;
entity[entname] = ent;
}
}
void PoolStorage::
deregisterEntity( const std::string& entname )
{
Entities::iterator entkey = entity.find(entname);
if( entkey == entity.end() ) // key doesnot exist
{
throw ExceptionFactory( ExceptionFactory::OBJECT_CONFLICT,
"Entity not defined yet. ",
"Entity name is <%s>.",entname.c_str() );
}
else
{
dgDEBUG(10) << "Deregister entity <"<< entname
<< "> from the pool." <<std::endl;
entity.erase( entkey );
}
}
Entity& PoolStorage::
getEntity( const std::string& name )
{
dgDEBUG(25) << "Get <" << name << ">"<<std::endl;
Entities::iterator entPtr = entity .find( name );
if( entPtr == entity.end() )
{
DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_OBJECT,
"Unknown entity."," (while calling <%s>)",
name.c_str() );
}
else return *entPtr->second;
}
void PoolStorage::
clearPlugin( const std::string& name )
{
dgDEBUGIN(5);
std::list< Entity* > toDelete;
for( Entities::iterator entPtr=entity.begin(); entPtr!=entity.end(); entPtr++ )
{
if( entPtr->second->getClassName() == name )
{ toDelete.push_back( entPtr->second ); }
}
for( std::list< Entity* >::iterator iter=toDelete.begin();
iter!=toDelete.end(); ++iter )
{
delete (Entity*) *iter;
}
dgDEBUGOUT(5);
return;
}
/* --------------------------------------------------------------------- */
Francois Bleibel
committed
#include <dynamic-graph/entity.h>
Francois Bleibel
committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#ifdef WIN32
#include <time.h>
#endif /*WIN32*/
void PoolStorage::
writeGraph(const std::string &aFileName)
{
size_t IdxPointFound = aFileName.rfind(".");
std::string tmp1 = aFileName.substr(0,IdxPointFound);
size_t IdxSeparatorFound = aFileName.rfind("/");
std::string GenericName;
if (IdxSeparatorFound!=std::string::npos)
GenericName = tmp1.substr(IdxSeparatorFound,tmp1.length());
else
GenericName = tmp1;
/* Reading local time */
time_t ltime;
ltime = time(NULL);
struct tm ltimeformatted;
#ifdef WIN32
localtime_s(<imeformatted,<ime);
#else
localtime_r(<ime,<imeformatted);
#endif /*WIN32*/
/* Opening the file and writing the first comment. */
std::ofstream GraphFile;
GraphFile.open((char *)aFileName.c_str(),std::ofstream::out);
GraphFile << "/* This graph has been automatically generated. " << std::endl;
GraphFile << " " << 1900+ltimeformatted.tm_year
<< " Month: " << 1+ltimeformatted.tm_mon
<< " Day: " << ltimeformatted.tm_mday
<< " Time: " << ltimeformatted.tm_hour
<< ":" << ltimeformatted.tm_min;
GraphFile << " */" << std::endl;
GraphFile << "digraph " << GenericName << " { ";
GraphFile << "\t graph [ label=\"" << GenericName << "\" bgcolor = white rankdir=LR ]" << std::endl
<< "\t node [ fontcolor = black, color = black, fillcolor = gold1, style=filled, shape=box ] ; " << std::endl;
GraphFile << "\tsubgraph cluster_Entities { " << std::endl;
GraphFile << "\t} " << std::endl;
for( Entities::iterator iter=entity.begin();
iter!=entity.end();iter++ )
{
Entity* ent = iter->second;
GraphFile << ent->getName()
<<" [ label = \"" << ent->getName() << "\" ," << std::endl
<<" fontcolor = black, color = black, fillcolor=cyan, style=filled, shape=box ]" << std::endl;
ent->writeGraph(GraphFile);
}
GraphFile << "}"<< std::endl;
GraphFile.close();
}
void PoolStorage::
writeCompletionList(std::ostream& os)
{
for( Entities::iterator iter=entity.begin();
iter!=entity.end();iter++ )
{
Entity* ent = iter->second;
ent->writeCompletionList(os);
}
}
void PoolStorage::
commandLine( const std::string& objectName,const std::string& functionName,
std::istringstream& cmdArg, std::ostream& os )
{
dgDEBUG(15) << "Object <" << objectName<< "> function <"
<<functionName<<">"<<std::endl;
if( objectName=="pool" )
{
if( functionName=="help" )
{
os <<"Pool: " << std::endl
<<" - list" << std::endl
<< " - writegraph FileName" << std::endl;
}
else if( functionName=="list" )
{
for( Entities::iterator iter=entity.begin();
iter!=entity.end();iter++ )
{
Entity* ent = iter->second;
os << ent->getName()
<<" (" << ent->getClassName() << ")" << std::endl;
}
}
else if (functionName=="writegraph")
{
std::string aFileName;
cmdArg >> aFileName;
writeGraph(aFileName);
}
}
else
{
Entity& ent = getEntity(objectName);
ent.commandLine(functionName,cmdArg,os);
}
}
Francois Bleibel
committed
#include <dynamic-graph/interpreter.h>
Francois Bleibel
committed
SignalBase<int>&
PoolStorage::
getSignal( std::istringstream& sigpath )
{
std::string objname,signame;
if(! Interpreter::objectNameParser( sigpath,objname,signame ) )
{ DG_THROW ExceptionFactory( ExceptionFactory::UNREFERED_SIGNAL,
"Parse error in signal name" ); }
Entity& ent = getEntity( objname );
return ent.getSignal( signame );
}
namespace dynamicgraph {
//! The global g_pool object.
PoolStorage g_pool;