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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
/**
* @file
* @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include "uscxml/config.h"
#include "uscxml/plugins/Factory.h"
#include "uscxml/messages/Data.h"
#include "uscxml/Interpreter.h"
#include "uscxml/interpreter/Logging.h"
#include "uscxml/plugins/ExecutableContent.h"
#include "uscxml/plugins/ExecutableContentImpl.h"
#include "uscxml/plugins/EventHandler.h"
#include "uscxml/plugins/IOProcessor.h"
#include "uscxml/plugins/IOProcessorImpl.h"
#include "uscxml/plugins/Invoker.h"
#include "uscxml/plugins/InvokerImpl.h"
#include "uscxml/plugins/DataModelImpl.h"
#if 0
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include "uscxml/util/DOM.h"
#endif
// see http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
/*** BEGIN PLUGINS ***/
#ifdef BUILD_AS_PLUGINS
# include "uscxml/plugins/Plugins.h"
#else
#ifdef WITH_IOPROC_SCXML
# include "uscxml/plugins/ioprocessor/scxml/SCXMLIOProcessor.h"
#endif
#ifdef WITH_IOPROC_BASICHTTP
# include "uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.h"
#endif
#include "uscxml/plugins/datamodel/null/NullDataModel.h"
#if defined WITH_DM_ECMA_V8
# include "uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h"
#endif
#ifdef WITH_DM_ECMA_JSC
# include "uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.h"
#endif
#ifdef WITH_DM_LUA
# include "uscxml/plugins/datamodel/lua/LuaDataModel.h"
#endif
#ifdef WITH_DM_C89
# include "uscxml/plugins/datamodel/c89/C89DataModel.h"
#endif
#ifdef WITH_DM_PROMELA
# include "uscxml/plugins/datamodel/promela/PromelaDataModel.h"
#endif
#ifdef WITH_INV_SCXML
# include "uscxml/plugins/invoker/scxml/USCXMLInvoker.h"
#endif
#ifdef WITH_INV_DIRMON
# include "uscxml/plugins/invoker/dirmon/DirMonInvoker.h"
#endif
#endif
/*** END PLUGINS ***/
namespace uscxml {
Factory::Factory(Factory* parentFactory) : _parentFactory(parentFactory) {
}
Factory::Factory(const std::string& pluginPath, Factory* parentFactory) : _parentFactory(parentFactory), _pluginPath(pluginPath) {
registerPlugins();
}
Factory::Factory(const std::string& pluginPath) : _parentFactory(NULL), _pluginPath(pluginPath) {
registerPlugins();
}
void Factory::setDefaultPluginPath(const std::string& path) {
_defaultPluginPath = path;
}
std::string Factory::getDefaultPluginPath() {
return _defaultPluginPath;
}
Factory::~Factory() {
#ifdef BUILD_AS_PLUGINS
pluma.unloadAll();
#endif
}
void Factory::registerPlugins() {
/*** PLUGINS ***/
#ifdef BUILD_AS_PLUGINS
if (_pluginPath.length() == 0) {
// try to read USCXML_PLUGIN_PATH environment variable
_pluginPath = (getenv("USCXML_PLUGIN_PATH") != NULL ? getenv("USCXML_PLUGIN_PATH") : "");
}
if (_pluginPath.length() > 0) {
pluma.acceptProviderType<InvokerImplProvider>();
pluma.acceptProviderType<IOProcessorImplProvider>();
pluma.acceptProviderType<DataModelImplProvider>();
pluma.acceptProviderType<ExecutableContentImplProvider>();
pluma.loadFromFolder(_pluginPath, true);
std::vector<InvokerImplProvider*> invokerProviders;
pluma.getProviders(invokerProviders);
for (auto provider : invokerProviders) {
InvokerImpl* invoker = provider->create();
registerInvoker(invoker);
}
std::vector<IOProcessorImplProvider*> ioProcessorProviders;
pluma.getProviders(ioProcessorProviders);
for (auto provider : ioProcessorProviders) {
IOProcessorImpl* ioProcessor = provider->create();
registerIOProcessor(ioProcessor);
}
std::vector<DataModelImplProvider*> dataModelProviders;
pluma.getProviders(dataModelProviders);
for (auto provider : dataModelProviders) {
DataModelImpl* dataModel = provider->create();
registerDataModel(dataModel);
}
std::vector<ExecutableContentImplProvider*> execContentProviders;
pluma.getProviders(execContentProviders);
for (auto provider : execContentProviders) {
ExecutableContentImpl* execContent = provider->create();
registerExecutableContent(execContent);
}
} else {
ERROR_EXECUTION_THROW("No path to plugins known, export USCXML_PLUGIN_PATH or pass path as parameter");
}
#else
#ifdef WITH_IOPROC_SCXML
{
SCXMLIOProcessor* ioProcessor = new SCXMLIOProcessor();
registerIOProcessor(ioProcessor);
}
#endif
#ifdef WITH_IOPROC_BASICHTTP
{
BasicHTTPIOProcessor* ioProcessor = new BasicHTTPIOProcessor();
registerIOProcessor(ioProcessor);
}
#endif
#ifdef WITH_DM_ECMA_V8
{
V8DataModel* dataModel = new V8DataModel();
registerDataModel(dataModel);
}
#endif
#ifdef WITH_DM_ECMA_JSC
{
JSCDataModel* dataModel = new JSCDataModel();
registerDataModel(dataModel);
}
#endif
#ifdef WITH_DM_LUA
{
LuaDataModel* dataModel = new LuaDataModel();
registerDataModel(dataModel);
}
#endif
#ifdef WITH_DM_C89
{
C89DataModel* dataModel = new C89DataModel();
registerDataModel(dataModel);
}
#endif
#ifdef WITH_DM_PROMELA
{
PromelaDataModel* dataModel = new PromelaDataModel();
registerDataModel(dataModel);
}
#endif
{
NullDataModel* dataModel = new NullDataModel();
registerDataModel(dataModel);
}
#ifdef WITH_INV_SCXML
{
USCXMLInvoker* invoker = new USCXMLInvoker();
registerInvoker(invoker);
}
#endif
#ifdef WITH_INV_DIRMON
{
DirMonInvoker* inv = new DirMonInvoker();
registerInvoker(inv);
}
#endif
#endif
/*** PLUGINS ***/
}
#define LIST_COMPONENTS(type, name) \
std::map<std::string, type*>::iterator iter = name.begin(); \
while(iter != name.end()) { \
std::list<std::string> names = iter->second->getNames(); \
std::list<std::string>::iterator nameIter = names.begin(); \
if (nameIter != names.end()) { \
LOGD(USCXML_VERBATIM) << "\t" << *nameIter; \
nameIter++; \
std::string seperator = ""; \
if (nameIter != names.end()) { \
LOGD(USCXML_VERBATIM) << "\t("; \
while(nameIter != names.end()) { \
LOGD(USCXML_VERBATIM) << seperator << *nameIter; \
seperator = ", "; \
nameIter++; \
} \
LOGD(USCXML_VERBATIM) << ")"; \
} \
LOGD(USCXML_VERBATIM) << "\n"; \
} \
iter++; \
}
void Factory::listComponents() {
{
LOGD(USCXML_VERBATIM) << "Available Datamodels:" << std::endl;
LIST_COMPONENTS(DataModelImpl, _dataModels);
LOGD(USCXML_VERBATIM) << "\n";
}
{
LOGD(USCXML_VERBATIM) << "Available Invokers:" << std::endl;
LIST_COMPONENTS(InvokerImpl, _invokers);
LOGD(USCXML_VERBATIM) << "\n";
}
{
LOGD(USCXML_VERBATIM) << "Available I/O Processors:" << std::endl;
LIST_COMPONENTS(IOProcessorImpl, _ioProcessors);
LOGD(USCXML_VERBATIM) << "\n";
}
{
LOGD(USCXML_VERBATIM) << "Available Elements:" << std::endl;
std::map<std::pair<std::string, std::string>, ExecutableContentImpl*>::iterator iter = _executableContent.begin();
while(iter != _executableContent.end()) {
LOGD(USCXML_VERBATIM) << "\t" << iter->second->getNamespace() << " / " << iter->second->getLocalName() << std::endl;
iter++;
}
LOGD(USCXML_VERBATIM) << "\n";
}
}
void Factory::registerIOProcessor(IOProcessorImpl* ioProcessor) {
std::list<std::string> names = ioProcessor->getNames();
std::list<std::string>::iterator nameIter = names.begin();
if (nameIter != names.end()) {
std::string canonicalName = *nameIter;
_ioProcessors[canonicalName] = ioProcessor;
while(nameIter != names.end()) {
_ioProcessorAliases[*nameIter] = canonicalName;
nameIter++;
}
}
}
void Factory::registerDataModel(DataModelImpl* dataModel) {
std::list<std::string> names = dataModel->getNames();
std::list<std::string>::iterator nameIter = names.begin();
if (nameIter != names.end()) {
std::string canonicalName = *nameIter;
_dataModels[canonicalName] = dataModel;
while(nameIter != names.end()) {
_dataModelAliases[*nameIter] = canonicalName;
nameIter++;
}
}
}
void Factory::registerInvoker(InvokerImpl* invoker) {
std::list<std::string> names = invoker->getNames();
std::list<std::string>::iterator nameIter = names.begin();
if (nameIter != names.end()) {
std::string canonicalName = *nameIter;
_invokers[canonicalName] = invoker;
while(nameIter != names.end()) {
_invokerAliases[*nameIter] = canonicalName;
nameIter++;
}
}
}
void Factory::registerExecutableContent(ExecutableContentImpl* executableContent) {
std::string localName = executableContent->getLocalName();
std::string nameSpace = executableContent->getNamespace();
_executableContent[std::make_pair(localName, nameSpace)] = executableContent;
}
std::map<std::string, IOProcessorImpl*> Factory::getIOProcessors() {
std::map<std::string, IOProcessorImpl*> ioProcs;
if (_parentFactory) {
ioProcs = _parentFactory->getIOProcessors();
}
std::map<std::string, IOProcessorImpl*>::iterator ioProcIter = _ioProcessors.begin();
while(ioProcIter != _ioProcessors.end()) {
ioProcs.insert(std::make_pair(ioProcIter->first, ioProcIter->second));
ioProcIter++;
}
return ioProcs;
}
bool Factory::hasInvoker(const std::string& type) {
if (_invokerAliases.find(type) != _invokerAliases.end()) {
return true;
} else if(_parentFactory) {
return _parentFactory->hasInvoker(type);
}
return false;
}
std::shared_ptr<InvokerImpl> Factory::createInvoker(const std::string& type, InvokerCallbacks* callbacks) {
// do we have this type ourself?
if (_invokerAliases.find(type) != _invokerAliases.end()) {
std::string canonicalName = _invokerAliases[type];
if (_invokers.find(canonicalName) != _invokers.end()) {
std::shared_ptr<InvokerImpl> invoker = _invokers[canonicalName]->create(callbacks);
return invoker;
}
}
// lookup in parent factory
if (_parentFactory) {
return _parentFactory->createInvoker(type, callbacks);
} else {
ERROR_EXECUTION_THROW("No Invoker named '" + type + "' known");
}
return std::shared_ptr<InvokerImpl>();
}
bool Factory::hasDataModel(const std::string& type) {
if (_dataModelAliases.find(type) != _dataModelAliases.end()) {
return true;
} else if(_parentFactory) {
return _parentFactory->hasDataModel(type);
}
return false;
}
std::shared_ptr<DataModelImpl> Factory::createDataModel(const std::string& type, DataModelCallbacks* callbacks) {
// do we have this type ourself?
if (_dataModelAliases.find(type) != _dataModelAliases.end()) {
std::string canonicalName = _dataModelAliases[type];
if (_dataModels.find(canonicalName) != _dataModels.end()) {
std::shared_ptr<DataModelImpl> dataModel = _dataModels[canonicalName]->create(callbacks);
return dataModel;
}
}
// lookup in parent factory
if (_parentFactory) {
return _parentFactory->createDataModel(type, callbacks);
} else {
ERROR_EXECUTION_THROW("No Datamodel name '" + type + "' known");
}
return std::shared_ptr<DataModelImpl>();
}
bool Factory::hasIOProcessor(const std::string& type) {
if (_ioProcessorAliases.find(type) != _ioProcessorAliases.end()) {
return true;
} else if(_parentFactory) {
return _parentFactory->hasIOProcessor(type);
}
return false;
}
std::shared_ptr<IOProcessorImpl> Factory::createIOProcessor(const std::string& type, IOProcessorCallbacks* callbacks) {
// do we have this type ourself?
if (_ioProcessorAliases.find(type) != _ioProcessorAliases.end()) {
std::string canonicalName = _ioProcessorAliases[type];
if (_ioProcessors.find(canonicalName) != _ioProcessors.end()) {
std::shared_ptr<IOProcessorImpl> ioProc = _ioProcessors[canonicalName]->create(callbacks);
// ioProc->setInterpreter(interpreter);
return ioProc;
}
}
// lookup in parent factory
if (_parentFactory) {
return _parentFactory->createIOProcessor(type, callbacks);
} else {
ERROR_EXECUTION_THROW("No IOProcessor named '" + type + "' known");
}
return std::shared_ptr<IOProcessorImpl>();
}
bool Factory::hasExecutableContent(const std::string& localName, const std::string& nameSpace) {
std::string actualNameSpace = (nameSpace.length() == 0 ? "http://www.w3.org/2005/07/scxml" : nameSpace);
if (_executableContent.find(std::make_pair(localName, actualNameSpace)) != _executableContent.end()) {
return true;
} else if(_parentFactory) {
return _parentFactory->hasExecutableContent(localName, nameSpace);
}
return false;
}
std::shared_ptr<ExecutableContentImpl> Factory::createExecutableContent(const std::string& localName, const std::string& nameSpace, InterpreterImpl* interpreter) {
// do we have this type in this factory?
std::string actualNameSpace = (nameSpace.length() == 0 ? "http://www.w3.org/2005/07/scxml" : nameSpace);
if (_executableContent.find(std::make_pair(localName, actualNameSpace)) != _executableContent.end()) {
std::shared_ptr<ExecutableContentImpl> execContent = _executableContent[std::make_pair(localName, actualNameSpace)]->create(interpreter);
execContent->setInterpreter(interpreter);
return execContent;
}
// lookup in parent factory
if (_parentFactory) {
return _parentFactory->createExecutableContent(localName, nameSpace, interpreter);
} else {
ERROR_EXECUTION_THROW("No Executable content name '" + localName + "' in namespace '" + actualNameSpace + "' known");
}
return std::shared_ptr<ExecutableContentImpl>();
}
void DataModelImpl::addExtension(DataModelExtension* ext) {
ERROR_EXECUTION_THROW("DataModel does not support extensions");
}
size_t DataModelImpl::replaceExpressions(std::string& content) {
std::stringstream ss;
size_t replacements = 0;
size_t indent = 0;
size_t pos = 0;
size_t start = std::string::npos;
size_t end = 0;
while (true) {
// find any of ${}
pos = content.find_first_of("${}", pos);
if (pos == std::string::npos) {
ss << content.substr(end, content.length() - end);
break;
}
if (content[pos] == '$') {
if (content.size() > pos && content[pos+1] == '{') {
pos++;
start = pos + 1;
// copy everything in between
ss << content.substr(end, (start - 2) - end);
}
} else if (content[pos] == '{' && start != std::string::npos) {
indent++;
} else if (content[pos] == '}' && start != std::string::npos) {
if (!indent) {
end = pos;
// we found a token to substitute
std::string expr = content.substr(start, end - start);
end++;
try {
Data data = getAsData(expr);
// if (data.type == Data::VERBATIM) {
// ss << "\"" << data.atom << "\"";
// } else {
// ss << data.atom;
// }
if (data.atom.length() > 0) {
ss << data.atom;
} else {
ss << Data::toJSON(data);
}
replacements++;
} catch (Event e) {
// insert unsubstituted
start -= 2;
ss << content.substr(start, end - start);
}
start = std::string::npos;
} else {
indent--;
}
}
pos++;
}
if (replacements)
content = ss.str();
return replacements;
}
Factory* Factory::getInstance() {
#if 0
// this needs to be here as some plugins use xercesc, now in X::X in DOM.h
try {
::xercesc_3_1::XMLPlatformUtils::Initialize();
} catch (const XERCESC_NS::XMLException& toCatch) {
ERROR_PLATFORM_THROW("Cannot initialize XercesC: " + X(toCatch.getMessage()).str());
}
#endif
if (_instance == NULL) {
_instance = new Factory(Factory::_defaultPluginPath);
}
return _instance;
}
//void EventHandlerImpl::returnErrorExecution(const std::string& cause) {
// ERROR_EXECUTION(exc, cause);
// returnEvent(exc);
//}
//
//void EventHandlerImpl::returnErrorCommunication(const std::string& cause) {
// ERROR_COMMUNICATION(exc, cause);
// returnEvent(exc);
//}
void IOProcessorImpl::eventToSCXML(Event& event,
const std::string& type,
const std::string& origin,
bool internal) {
if (event.eventType == 0)
event.eventType = (internal ? Event::INTERNAL : Event::EXTERNAL);
if (event.origin.length() == 0 && origin.length() > 0)
event.origin = origin;
if (event.origintype.length() == 0)
event.origintype = type;
if (internal) {
_callbacks->enqueueInternal(event);
} else {
_callbacks->enqueueExternal(event);
}
}
void InvokerImpl::eventToSCXML(Event& event,
const std::string& type,
const std::string& invokeId,
bool internal) {
if (event.invokeid.length() == 0)
event.invokeid = invokeId;
if (event.eventType == 0)
event.eventType = (internal ? Event::INTERNAL : Event::EXTERNAL);
if (event.origin.length() == 0 && invokeId.length() > 0)
event.origin = "#_" + invokeId;
if (event.origintype.length() == 0)
event.origintype = type;
if (internal) {
_callbacks->enqueueInternal(event);
} else {
_callbacks->enqueueExternal(event);
}
}
std::string Factory::_defaultPluginPath;
Factory* Factory::_instance = NULL;
}
|