Blame view

src/ParamOutputImpl/Plot/PlotConfigurationLoader.cc 16.5 KB
fbe3c2bb   Benjamin Renard   First commit
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
/*
 * PlotConfigurationLoader.cc
 *
 *  Created on: 8 nov. 2013
 *      Author: guillaume
 */

#include "PlotConfigurationLoader.hh"
#include "DefaultPlotConfiguration.hh"
#include "PanelPlotNodeRegistry.hh"
#include "ColormapManager.hh"
#include "Constant.hh"
#include "CommonNode.hh"
#include "Properties.hh"
#include "AxesNode.hh"

#include <boost/filesystem/path.hpp>
#include <boost/filesystem.hpp>


namespace plot {
static const std::string DEFAULT_NODE_NAME = "default";
static const std::string COLORMAP_NODE_NAME = "colormap";

PlotConfigurationLoader::PlotConfigurationLoader() {
	_nodeMapLoader[typeid(DefaultPlotConfiguration).name()] = NodeLoader(
			DEFAULT_NODE_NAME, NodeCfgSPtr(new DefaultNode()));
	_nodeMapLoader[typeid(ColormapManager).name()] = NodeLoader(
			COLORMAP_NODE_NAME, NodeCfgSPtr(new ColormapNode()));
}

PlotConfigurationLoader::~PlotConfigurationLoader() {

}

void PlotConfigurationLoader::initParser(
		const AMDA::Parameters::CfgContext& pCtx) {
	AMDA::helpers::Properties lProperties("app.properties");
	std::string configFile = lProperties["app.plot.configfile"];
	if (configFile.empty()) {
		LOG4CXX_WARN(gLogger,
				"Unset property app.plot.configfile in app.properties.");
		return;
	}


	xmlDocPtr lDoc = NULL;
	xmlNodePtr lRoot = NULL;

	LOG4CXX_DEBUG(gLogger, "File to parse: " << configFile)
	try {
		if (access(configFile.c_str(), F_OK) != 0) {
			LOG4CXX_WARN(gLogger, "Unknown file " + configFile);
			return;
		}
		if (!(lDoc = xmlParseFile(configFile.c_str()))) {
			LOG4CXX_ERROR(gLogger, "Invalid file " + configFile);
			return;
		}
		if (!(lRoot = xmlDocGetRootElement(lDoc))) {
			LOG4CXX_ERROR(gLogger, "Empty file " + configFile);
			return;
		}

		AMDA::XMLConfigurator::NodeGrpCfg rootNodeParser = RootNode(
				_nodeLoader);

		rootNodeParser.proceed(lRoot, pCtx);
	} catch (...) {
		if (lDoc) {
			xmlFreeDoc(lDoc);
		}
		throw;
	}
	if (lDoc) {
		xmlFreeDoc(lDoc);
	}
}

/**
 * ************************** RootNode *******************************
 */
RootNode::RootNode(const NodeLoader& pNodeLoader) :
		NodeGrpCfg() {
	// Push node loader.
	getChildList()[std::get < 0 > (pNodeLoader)] = std::get < 1 > (pNodeLoader);
}

RootNode::~RootNode() {
}

void RootNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, pCtx);
}

/**
 * ************************** DefaultNode *******************************
 */

DefaultNode::DefaultNode() :
		NodeGrpCfg() {
	getChildList()["page"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultPageNode());
	getChildList()["panel"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultPanelNode());
}

DefaultNode::~DefaultNode() {
}

void DefaultNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, pCtx);
}

/**
 * ************************** DefaultPageNode *******************************
 */

DefaultPageNode::DefaultPageNode() :
		NodeGrpCfg() {
	getChildList()["font"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new FontNode<Page>());
	getChildList()["margin"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new MarginNode());
}

DefaultPageNode::~DefaultPageNode() {

}

void DefaultPageNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "PageNode::proceed");
	DefaultPlotConfiguration* defaultConfiguration = pContext.get<
			DefaultPlotConfiguration*>();
	xmlChar* value = NULL;

	// -- dimension
	if ((value = xmlGetProp(pNode, (const xmlChar *) "dimension"))) {
		defaultConfiguration->_defaultPage.setDimension((const char*) value);
		xmlFree(value);
	}

	// -- orientation
	if ((value = xmlGetProp(pNode, (const xmlChar *) "orientation"))) {
		defaultConfiguration->_defaultPage.setOrientation((const char*) value);
		xmlFree(value);
	}

	// -- mode
	if ((value = xmlGetProp(pNode, (const xmlChar *) "mode"))) {
		defaultConfiguration->_defaultPage.setMode((const char*) value);
		xmlFree(value);
	}

	// -- dpi
	if ((value = xmlGetProp(pNode, (const xmlChar *) "dpi"))) {
		defaultConfiguration->_defaultPage._dpi = atoi((const char*) value);
		xmlFree(value);
	}

	AMDA::Parameters::CfgContext context;
	context.push<Page*>(&defaultConfiguration->_defaultPage);
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
}

/**
 * ************************** DefaultPanelNode *******************************
 */

DefaultPanelNode::DefaultPanelNode() :
		NodeGrpCfg() {
	getChildList()["font"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new FontNode<Panel>());
	getChildList()["title"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new TitleNode<Panel>());
	getChildList()["timeAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultTimeAxisNode());
	getChildList()["epochAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultEpochAxisNode());
	getChildList()["colorAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultColorAxisNode());
	getChildList()["xAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultDigitalAxisNode());
	getChildList()["yAxis"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new DefaultDigitalAxisNode());

	// retrieve registered plot properties node :
	for( auto child : PanelPlotNodeRegistry::getInstance().getConfigurationNodes() ){
		std::ostringstream msg;
		msg << "adding child : " << child.first << std::endl;
		LOG4CXX_DEBUG(gLogger, msg.str());
		getChildList()[child.first] = child.second;
	}
}

DefaultPanelNode::~DefaultPanelNode() {

}

void DefaultPanelNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "DefaultPanelNode::proceed");
	xmlChar* value = NULL;

	DefaultPlotConfiguration* defaultConfiguration = pContext.get<
			DefaultPlotConfiguration*>();

	// -- max serie resolution
	if ((value = xmlGetProp(pNode, (const xmlChar *) "resolution"))) {
		defaultConfiguration->_defaultPanel._resolution = atoi((const char*) value);
		xmlFree(value);
	}

	// -- color
	value = xmlGetProp(pNode, (const xmlChar *) "backgroundColor");
	if (value) {
		try {
			std::string strValue((const char*) value);
			createColor(defaultConfiguration->_defaultPanel._backgroundColor,
					strValue);
		} catch (std::logic_error& e) {
			LOG4CXX_WARN(gLogger,
					"Plot Default Configuration Panel BackgroundColor : " << e.what());
			defaultConfiguration->_defaultPanel._backgroundColor._colorIndex =	0;
			defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = 0;
		}
		xmlFree(value);
	} else {
		LOG4CXX_WARN(gLogger,
				"No default background color is defined for plot panels");
		defaultConfiguration->_defaultPanel._backgroundColor._colorIndex = 0;
		defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = 0;
	}

	// -- color map index
	value = xmlGetProp(pNode, (const xmlChar *) "colorMapIndex");
	if (value) {
		defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = atoi((const char*) value);
		xmlFree(value);
	} else if (defaultConfiguration->_defaultPanel._backgroundColor._colorIndex
			!= -1) {
		LOG4CXX_WARN(gLogger,
				"No default color map is defined, color map 0 will be used");
		defaultConfiguration->_defaultPanel._backgroundColor._colorMapIndex = 0;
	}

	AMDA::Parameters::CfgContext context;
	context.push<Panel*>(&defaultConfiguration->_defaultPanel);
	context.push<DefaultPlotConfiguration*>(defaultConfiguration);
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);
}

/**
 * ************************** Default*AxisNode *******************************
 */

void DefaultTimeAxisNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	LOG4CXX_DEBUG(gLogger, "DefaultTimeAxisNode::proceed");
	Panel* panel = pCtx.get<Panel*>();
	TimeAxis* axis = new TimeAxis();

	// read dedicated attributes
	xmlChar* value = NULL;
	// -- axis id
	//axis->_id = DefaultPlotConfiguration::TIME_DEFAULT_ID;
	// -- scale type
	value = xmlGetProp(pNode, (const xmlChar *) "id");
	if (value) {
		axis->_id = (const char*) value;
		xmlFree(value);
	}
	panel->addAxis(axis->_id, boost::shared_ptr<Axis>(axis));

	// -- scale type
	value = xmlGetProp(pNode, (const xmlChar *) "format");
	if (value) {
		axis->_timeFormat = (const char*) value;
		xmlFree(value);
	}

	AMDA::Parameters::CfgContext context;
	context.push<TimeAxis*>(axis);
	context.push<Panel*>(panel);

	AnyAxisNode<TimeAxis>::proceed(pNode, context);
}

void DefaultEpochAxisNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	LOG4CXX_DEBUG(gLogger, "DefaultEpochAxisNode::proceed");
	Panel* panel = pCtx.get<Panel*>();
	EpochAxis* axis = new EpochAxis();

	// read dedicated attributes
	xmlChar* value = NULL;
	value = xmlGetProp(pNode, (const xmlChar *) "id");
	if (value) {
		axis->_id = (const char*) value;
		xmlFree(value);
	}
	panel->addAxis(axis->_id, boost::shared_ptr<Axis>(axis));

	AMDA::Parameters::CfgContext context;
	context.push<EpochAxis*>(axis);
	context.push<Panel*>(panel);

	AnyAxisNode<EpochAxis>::proceed(pNode, context);
}

void DefaultColorAxisNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	LOG4CXX_DEBUG(gLogger, "DefaultColorAxisNode::proceed");
	Panel* panel = pCtx.get<Panel*>();
	ColorAxis* axis = new ColorAxis();

	// read dedicated attributes
	xmlChar* value = NULL;
	// -- axis id
	//axis->_id = DefaultPlotConfiguration::TIME_DEFAULT_ID;
	// -- scale type
	value = xmlGetProp(pNode, (const xmlChar *) "id");
	if (value) {
		axis->_id = (const char*) value;
		xmlFree(value);
	}
	panel->addAxis(axis->_id, boost::shared_ptr<Axis>(axis));

	AMDA::Parameters::CfgContext context;
	context.push<ColorAxis*>(axis);
	context.push<Panel*>(panel);

	AnyAxisNode<ColorAxis>::proceed(pNode, context);
}

void DefaultDigitalAxisNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	LOG4CXX_DEBUG(gLogger, "DefaultDigitalAxisNode::proceed");
	Panel* panel = pCtx.get<Panel*>();

	DigitalAxis* axis = new DigitalAxis;
	// read dedicated attributes
	xmlChar* value = NULL;

	// -- axis id
	axis->_id = (const char*) pNode->name;
	panel->addAxis(axis->_id, boost::shared_ptr<Axis>(axis));

	// -- scale type
	value = xmlGetProp(pNode, (const xmlChar *) "scale");
	if (value) {
		axis->setScale((const char*) value);
		xmlFree(value);
	}

	AMDA::Parameters::CfgContext context;
	context.push<DigitalAxis*>(axis);
	context.push<Panel*>(panel);

	AnyAxisNode<DigitalAxis>::proceed(pNode, context);
}

/**
 * ************************** ColormapNode *******************************
 */
ColormapNode::ColormapNode() :
		NodeGrpCfg() {
	getChildList()["grayscale"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new GrayscaleNode());
	getChildList()["color"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new ColorscaleNode());
	getChildList()["coloraxis"] = AMDA::XMLConfigurator::NodeCfgSPtr(
				new ColormapAxisNode());
}

ColormapNode::~ColormapNode() {

}

void ColormapNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "ColormapNode::proceed");

	// Get colormap folder
	xmlChar* lXmlFolder = xmlGetProp(pNode, (const xmlChar *) "path");

	if (lXmlFolder) {

		AMDA::helpers::Properties lProperties("app.properties");

		// Config file path
		boost::filesystem::path configFilePath(lProperties["app.plot.configfile"]);

		boost::filesystem::path colorPath(std::string((const char*) lXmlFolder));
		colorPath = boost::filesystem::absolute(colorPath, configFilePath.parent_path());

		pContext.get<ColormapManager*>()->_path = colorPath.string();
		xmlFree(lXmlFolder);
	}

	AMDA::Parameters::CfgContext context;
	context.push<ColormapManager*>(pContext.get<ColormapManager*>());
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, pContext);

}

/**
 * ************************** GrayscaleNode *******************************
 */
GrayscaleNode::GrayscaleNode() :
		NodeGrpCfg() {
	getChildList()["file"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new ColorFileNode());
}

GrayscaleNode::~GrayscaleNode() {

}

void GrayscaleNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "GrayscaleNode::proceed");
	ColormapManager* lColormapManager = pContext.get<ColormapManager*>();

	AMDA::Parameters::CfgContext context;
	std::shared_ptr<ColorFileMap> lColorFileMap(new ColorFileMap());
	context.push<ColorFileMap*>(lColorFileMap.get());
	context.push<ColormapManager*>(lColormapManager);
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);

	xmlChar* value = NULL;
	// -- default color map index
	value = xmlGetProp(pNode, (const xmlChar *) "default");
	if (value) {
		lColormapManager->_defaultColormap[PlotCommon::Mode::GRAYSCALE] = atoi((const char*) value);
		xmlFree(value);
	}
	if (!value
			|| lColorFileMap.get()->find(
					lColormapManager->_defaultColormap[PlotCommon::Mode::GRAYSCALE]) == lColorFileMap->end()) {
		lColormapManager->_defaultColormap[PlotCommon::Mode::GRAYSCALE] = 0;
		if (lColorFileMap->size() == 0) {
			LOG4CXX_WARN(gLogger,
					"No default color map (grayscale mode) is set, default color map will be 0");
		}
	}

	lColormapManager->_colormap[PlotCommon::Mode::GRAYSCALE] =
			*(lColorFileMap.get());
}

/**
 * ************************** ColorscaleNode *******************************
 */
ColorscaleNode::ColorscaleNode() :
		NodeGrpCfg() {
	getChildList()["file"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new ColorFileNode());
}

ColorscaleNode::~ColorscaleNode() {

}

void ColorscaleNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "ColorscaleNode::proceed");
	ColormapManager* lColormapManager = pContext.get<ColormapManager*>();

	AMDA::Parameters::CfgContext context;
	std::shared_ptr<ColorFileMap> lColorFileMap(new ColorFileMap());
	context.push<ColorFileMap*>(lColorFileMap.get());
	context.push<ColormapManager*>(lColormapManager);
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);

	// read dedicated attributes
	xmlChar* value = NULL;
	// -- default color map index
	value = xmlGetProp(pNode, (const xmlChar *) "default");
	if (value) {
		lColormapManager->_defaultColormap[PlotCommon::Mode::COLOR] = atoi((const char*) value);
		xmlFree(value);
	}
	if (!value
			|| lColorFileMap.get()->find(
					lColormapManager->_defaultColormap[PlotCommon::Mode::COLOR]) == lColorFileMap->end()) {
		lColormapManager->_defaultColormap[PlotCommon::Mode::COLOR] = 0;
		if (lColorFileMap->size() == 0) {
			LOG4CXX_WARN(gLogger,
					"No default color map (color mode) is set, default color map will be 0");
		}
	}

	lColormapManager->_colormap[PlotCommon::Mode::COLOR] =
			*(lColorFileMap.get());
}

/**
 * ************************** ColorAxisNode *******************************
 */
ColormapAxisNode::ColormapAxisNode() :
		NodeGrpCfg() {
	getChildList()["file"] = AMDA::XMLConfigurator::NodeCfgSPtr(
			new ColorFileNode());
}

ColormapAxisNode::~ColormapAxisNode() {

}

void ColormapAxisNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "ColormapAxisNode::proceed");
	ColormapManager* lColormapManager = pContext.get<ColormapManager*>();

	AMDA::Parameters::CfgContext context;
	std::shared_ptr<ColorFileMap> lColorFileMap(new ColorFileMap());
	context.push<ColorFileMap*>(lColorFileMap.get());
	context.push<ColormapManager*>(lColormapManager);
	AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode, context);

	xmlChar* value = NULL;
	// -- default color map index
	value = xmlGetProp(pNode, (const xmlChar *) "default");
	if (value) {
		lColormapManager->_defaultColorAxis = atoi((const char*) value);
		xmlFree(value);
	}
	if (!value
			|| lColorFileMap.get()->find(
					lColormapManager->_defaultColorAxis) == lColorFileMap->end()) {
		lColormapManager->_defaultColorAxis = 0;
		if (lColorFileMap->size() == 0) {
			LOG4CXX_WARN(gLogger,
					"No default color axis is set, default color map will be 0");
		}
	}

	lColormapManager->_coloraxis = *(lColorFileMap.get());
}

/**
 * ************************** ColorFileNode *******************************
 */
ColorFileNode::ColorFileNode() {
}
ColorFileNode::~ColorFileNode() {
}

void ColorFileNode::proceed(xmlNodePtr pNode,
		const AMDA::Parameters::CfgContext& pCtx) {
	LOG4CXX_DEBUG(gLogger, "ColorFileNode::proceed ");

	// Get list
	ColorFileMap* lColorFileList = pCtx.get<ColorFileMap*>();

	// Get filename.
		std::string lFilePath = std::string(
				((const char*) pNode->children->content));

	// Get default color map index
	xmlChar* value = NULL;
	int index = lColorFileList->size();

	value = xmlGetProp(pNode, (const xmlChar *) "index");
	if (value) {
		index = atoi((const char*) value);
		xmlFree(value);
	}
	else {
		LOG4CXX_WARN(gLogger,
							"No index is set for " << lFilePath << " color map file, force to " << index);
	}

	(*lColorFileList)[index] = lFilePath;
}

} //plot