forked from s-nakaoka/openhrp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
OpenHRPInterpreterServiceItem.cpp
360 lines (286 loc) · 9.87 KB
/
OpenHRPInterpreterServiceItem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
131
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
@author Shin'ichiro Nakaoka
*/
#include "OpenHRPInterpreterServiceItem.h"
#include <cnoid/corba/OpenHRP/3.1/InterpreterService.hh>
#include <cnoid/ItemManager>
#include <cnoid/Archive>
#include <cnoid/MessageView>
#include <cnoid/OpenRTMUtil>
#include <cnoid/LazyCaller>
#include <cnoid/ScriptItem>
#include <cnoid/PutPropertyFunction>
#include <rtm/DataFlowComponentBase.h>
#include <rtm/CorbaPort.h>
#include <fmt/format.h>
#include "gettext.h"
using namespace std;
using namespace std::placeholders;
using namespace cnoid;
using fmt::format;
namespace {
const bool TRACE_FUNCTIONS = false;
class InterpreterService_impl
: public virtual POA_OpenHRP::InterpreterService,
public virtual PortableServer::RefCountServantBase
{
public:
char* interpret(const char* expr);
void interpretMain(const char* expr);
OpenHRPInterpreterServiceItemImpl* itemImpl;
string result;
};
class InterpreterRTC : public RTC::DataFlowComponentBase
{
public:
InterpreterRTC(RTC::Manager* manager);
virtual ~InterpreterRTC();
virtual RTC::ReturnCode_t onInitialize();
RTC::CorbaPort interpreterServicePort;
InterpreterService_impl interpreterService;
};
}
namespace cnoid {
class OpenHRPInterpreterServiceItemImpl
{
public:
OpenHRPInterpreterServiceItem* self;
InterpreterRTC* rtc;
string rtcInstanceName;
Connection scriptItemUpdateConnection;
ScriptItem* scriptItem;
bool isScriptItemBackgroundMode;
bool forceMainThreadExecution;
bool doPutScriptTextToInterpret;
ostream& os;
OpenHRPInterpreterServiceItemImpl(OpenHRPInterpreterServiceItem* self);
OpenHRPInterpreterServiceItemImpl(
OpenHRPInterpreterServiceItem* self, const OpenHRPInterpreterServiceItemImpl& org);
~OpenHRPInterpreterServiceItemImpl();
void setRTCinstanceName(const std::string& name);
bool createRTC();
bool deleteRTC();
void onScriptItemUpdated();
};
typedef OpenHRPInterpreterServiceItemImpl ItemImpl;
}
void OpenHRPInterpreterServiceItem::initializeClass(ExtensionManager* ext)
{
static const char* spec[] = {
"implementation_id", "OpenHRPInterpreterService",
"type_name", "OpenHRPInterpreterService",
"description", "Component for accessing the python interpreter executing a script",
"version", "1.0",
"vendor", "AIST",
"category", "Choreonoid",
"activity_type", "DataFlowComponent",
"max_instance", "100",
"language", "C++",
"lang_type", "compile",
""
};
RTC::Properties profile(spec);
RTC::Manager::instance().registerFactory(
profile, RTC::Create<InterpreterRTC>, RTC::Delete<InterpreterRTC>);
ext->itemManager()
.registerClass<OpenHRPInterpreterServiceItem>(N_("OpenHRPInterpreterServiceItem"))
.addCreationPanel<OpenHRPInterpreterServiceItem>();
}
OpenHRPInterpreterServiceItem::OpenHRPInterpreterServiceItem()
{
setName("OpenHRPInterpreterService");
impl = new ItemImpl(this);
}
ItemImpl::OpenHRPInterpreterServiceItemImpl(OpenHRPInterpreterServiceItem* self)
: self(self),
os(MessageView::instance()->cout())
{
rtc = 0;
scriptItem = 0;
isScriptItemBackgroundMode = false;
forceMainThreadExecution = false;
doPutScriptTextToInterpret = false;
}
OpenHRPInterpreterServiceItem::OpenHRPInterpreterServiceItem(const OpenHRPInterpreterServiceItem& org)
: Item(org)
{
impl = new ItemImpl(this, *org.impl);
}
ItemImpl::OpenHRPInterpreterServiceItemImpl(OpenHRPInterpreterServiceItem* self, const ItemImpl& org)
: self(self),
forceMainThreadExecution(org.forceMainThreadExecution),
os(MessageView::instance()->cout())
{
rtc = 0;
scriptItem = 0;
isScriptItemBackgroundMode = org.isScriptItemBackgroundMode;
doPutScriptTextToInterpret = org.doPutScriptTextToInterpret;
}
OpenHRPInterpreterServiceItem::~OpenHRPInterpreterServiceItem()
{
delete impl;
}
OpenHRPInterpreterServiceItemImpl::~OpenHRPInterpreterServiceItemImpl()
{
deleteRTC();
}
Item* OpenHRPInterpreterServiceItem::doDuplicate() const
{
return new OpenHRPInterpreterServiceItem(*this);
}
void OpenHRPInterpreterServiceItem::setRTCInstanceName(const std::string& name)
{
impl->setRTCinstanceName(name);
}
void ItemImpl::setRTCinstanceName(const std::string& name)
{
if(rtcInstanceName != name){
rtcInstanceName = name;
if(self->findRootItem()){
createRTC();
}
}
}
bool ItemImpl::createRTC()
{
if(rtc){
deleteRTC();
}
if(rtc || rtcInstanceName.empty()){
return false;
}
string param("OpenHRPInterpreterService?"
"instance_name={}&"
"exec_cxt.periodic_type=PeriodicExecutionContext&"
"exec_cxt.periodic.rate=10");
rtc = dynamic_cast<InterpreterRTC*>(cnoid::createManagedRTC(format(param, rtcInstanceName)));
if(rtc){
rtc->interpreterService.itemImpl = this;
os << format(_("RTC \"{0}\" of \"{1}\" has been created."),
rtcInstanceName, self->name()) << endl;
} else {
os << format(_("RTC \"{0}\" of \"{1}\" cannot be created."),
rtcInstanceName, self->name()) << endl;
}
return (rtc != 0);
}
bool ItemImpl::deleteRTC()
{
if(rtc){
if(cnoid::deleteRTC(rtc)){
os << format(_("RTC \"{0}\" of \"{1}\" has been deleted."),
rtcInstanceName, self->name()) << endl;
rtc = 0;
} else {
os << format(_("RTC \"{0}\" of \"{1}\" cannot be deleted."),
rtcInstanceName, self->name()) << endl;
}
}
return (rtc == 0);
}
void OpenHRPInterpreterServiceItem::onConnectedToRoot()
{
impl->createRTC();
}
void OpenHRPInterpreterServiceItem::onPositionChanged()
{
impl->scriptItemUpdateConnection.disconnect();
impl->scriptItem = findOwnerItem<ScriptItem>();
if(impl->scriptItem){
impl->onScriptItemUpdated();
impl->scriptItemUpdateConnection =
impl->scriptItem->sigUpdated().connect(std::bind(&ItemImpl::onScriptItemUpdated, impl));
}
}
void ItemImpl::onScriptItemUpdated()
{
// This property is stored to avoid the access from a background thread
isScriptItemBackgroundMode = scriptItem->isBackgroundMode();
}
void OpenHRPInterpreterServiceItem::onDisconnectedFromRoot()
{
impl->deleteRTC();
}
void OpenHRPInterpreterServiceItem::doPutProperties(PutPropertyFunction& putProperty)
{
putProperty(_("RTC Instance name"), impl->rtcInstanceName,
[&](const string& name){ impl->setRTCinstanceName(name); return true; });
putProperty(_("Force main thread execution"), impl->forceMainThreadExecution,
changeProperty(impl->forceMainThreadExecution));
putProperty(_("Put script text to interpret"), impl->doPutScriptTextToInterpret,
changeProperty(impl->doPutScriptTextToInterpret));
}
bool OpenHRPInterpreterServiceItem::store(Archive& archive)
{
archive.write("rtcInstance", impl->rtcInstanceName);
archive.write("forceMainThreadExecution", impl->forceMainThreadExecution);
archive.write("putScriptText", impl->doPutScriptTextToInterpret);
return true;
}
bool OpenHRPInterpreterServiceItem::restore(const Archive& archive)
{
impl->setRTCinstanceName(archive.get("rtcInstance", impl->rtcInstanceName));
archive.read("forceMainThreadExecution", impl->forceMainThreadExecution);
archive.read("putScriptText", impl->doPutScriptTextToInterpret);
return true;
}
InterpreterRTC::InterpreterRTC(RTC::Manager* manager)
: RTC::DataFlowComponentBase(manager),
interpreterServicePort("InterpreterService")
{
}
InterpreterRTC:: ~InterpreterRTC()
{
}
RTC::ReturnCode_t InterpreterRTC::onInitialize()
{
interpreterServicePort.registerProvider("service0", "InterpreterService", interpreterService);
addPort(interpreterServicePort);
return RTC::RTC_OK;
}
char* InterpreterService_impl::interpret(const char* expr)
{
result.clear();
if(!itemImpl->isScriptItemBackgroundMode || itemImpl->forceMainThreadExecution){
callSynchronously(std::bind(&InterpreterService_impl::interpretMain, this, expr));
} else {
interpretMain(expr);
}
CORBA::String_var ret(result.c_str());
return ret._retn();;
}
void InterpreterService_impl::interpretMain(const char* expr)
{
ostream& os = MessageView::instance()->cout();
Item* item = itemImpl->self;
if(itemImpl->doPutScriptTextToInterpret){
os << format(_("{0}: interpret(\"{1}\")"), item->name(), expr) << endl;
}
ScriptItem* scriptItem = item->findOwnerItem<ScriptItem>();
if(!scriptItem){
os << format(_("The owner script item of {} is not found. The interpret function cannot be executed."),
item->name()) << endl;
} else {
if(scriptItem->isRunning()){
os << format(_("Owner script item \"{}\" is running now. The interpret function cannot be executed."),
scriptItem->name()) << endl;
} else {
if(!scriptItem->executeCode(expr)){
os << _("Executing the script given to the interpret function failed.") << endl;
} else {
if(!scriptItem->waitToFinish()){
os << _("The script does not return.") << endl;
} else {
result = scriptItem->resultString();
if(itemImpl->doPutScriptTextToInterpret){
if(!result.empty()){
os << format(_("{0}: interpret() returns {1}."), item->name(), result) << endl;
} else {
os << format(_("{}: interpret() finished."), item->name()) << endl;
}
}
}
}
}
}
}