sym.cpp 21.1 KB
Newer Older
1
/**
2
 * evaluates symbols
3
4
5
6
7
8
 * @author Tobias Weber <tweber@ill.fr>
 * @date 15-Jun-2018
 * @license see 'LICENSE' file
 */

#include "cliparser.h"
Tobias WEBER's avatar
Tobias WEBER committed
9
#include "../globals.h"
10
#include "funcs.h"
Tobias WEBER's avatar
Tobias WEBER committed
11
#include "libs/algos.h"
12
#include "libs/str.h"
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

#include <cmath>


using t_real = t_real_cli;


// ----------------------------------------------------------------------------
// evaluation of symbols
// ----------------------------------------------------------------------------

/**
 * symbol type name
 */
const std::string& Symbol::get_type_name(const Symbol &sym)
{
	static const std::unordered_map<SymbolType, std::string> map =
	{
		std::make_pair(SymbolType::REAL, "real"),
		std::make_pair(SymbolType::STRING, "string"),
		std::make_pair(SymbolType::LIST, "list"),
		std::make_pair(SymbolType::ARRAY, "array"),
		std::make_pair(SymbolType::DATASET, "dataset"),
	};

	return map.find(sym.GetType())->second;
}


/**
 * unary minus of a symbol
 */
std::shared_ptr<Symbol> Symbol::uminus(const Symbol &sym)
{
	if(sym.GetType()==SymbolType::REAL)
	{ // -1.23
		return std::make_shared<SymbolReal>(-dynamic_cast<const SymbolReal&>(sym).GetValue()); 
	}
	if(sym.GetType()==SymbolType::ARRAY)
	{ // -[1,2,3]
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym).GetValue();
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::uminus(*arr[idx]));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	else if(sym.GetType()==SymbolType::DATASET)
	{ // -data
		return std::make_shared<SymbolDataset>(-dynamic_cast<const SymbolDataset&>(sym).GetValue()); 
	}

	return nullptr;
}


/**
 * addition of symbols
 */
std::shared_ptr<Symbol> Symbol::add(const Symbol &sym1, const Symbol &sym2)
{
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
	{ // 1.23 + 1.23
		return std::make_shared<SymbolReal>(
			dynamic_cast<const SymbolReal&>(sym1).GetValue() + 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
	{ // [1,2,3] + [1,2,3]
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr1 = dynamic_cast<const SymbolList&>(sym1).GetValue();
		const auto& arr2 = dynamic_cast<const SymbolList&>(sym2).GetValue();
		for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
			arrNew.emplace_back(Symbol::add(*arr1[idx], *arr2[idx]));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::ARRAY)
	{ // 1.23 + [1,2,3], point-wise addition
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& val = dynamic_cast<const SymbolReal&>(sym1);
		const auto& arr = dynamic_cast<const SymbolList&>(sym2).GetValue();
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::add(val, *arr[idx]));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
	{ // [1,2,3] + 1.23, point-wise addition
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
		const auto& val = dynamic_cast<const SymbolReal&>(sym2);
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::add(*arr[idx], val));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::STRING)
	{ // "abc" + "123"
		return std::make_shared<SymbolString>(
			dynamic_cast<const SymbolString&>(sym1).GetValue() + 
			dynamic_cast<const SymbolString&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
	{ // data1 + data2
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolDataset&>(sym1).GetValue() + 
			dynamic_cast<const SymbolDataset&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
	{ // data + 1.23
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolDataset&>(sym1).GetValue() + 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
	{ // 1.23 + data
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolReal&>(sym1).GetValue() + 
			dynamic_cast<const SymbolDataset&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::REAL)
	{ // "abc" + 3
		const std::string &str = dynamic_cast<const SymbolString&>(sym1).GetValue();
		t_real val = dynamic_cast<const SymbolReal&>(sym2).GetValue();
		std::ostringstream ostr;
		ostr << str << val;
		return std::make_shared<SymbolString>(ostr.str());
	}
	else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::STRING)
	{ // 3 + "abc"
		t_real val = dynamic_cast<const SymbolReal&>(sym1).GetValue();
		const std::string &str = dynamic_cast<const SymbolString&>(sym2).GetValue();
		std::ostringstream ostr;
		ostr << val << str;
		return std::make_shared<SymbolString>(ostr.str());
	}

	return nullptr;
}

std::shared_ptr<Symbol> Symbol::add(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
	return Symbol::add(*sym1, *sym2);
}


/**
 * subtraction of symbols
 */
std::shared_ptr<Symbol> Symbol::sub(const Symbol &sym1, const Symbol &sym2)
{
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
	{ // 1.23 - 2.34
		return std::make_shared<SymbolReal>(
			dynamic_cast<const SymbolReal&>(sym1).GetValue() - 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
	{ // [1,2,3] - [1,2,3]
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr1 = dynamic_cast<const SymbolList&>(sym1).GetValue();
		const auto& arr2 = dynamic_cast<const SymbolList&>(sym2).GetValue();
		for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
			arrNew.emplace_back(Symbol::sub(*arr1[idx], *arr2[idx]));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
	{ // [1,2,3] - 1.23, point-wise subtraction
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
		const auto& val = dynamic_cast<const SymbolReal&>(sym2);
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::sub(*arr[idx], val));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
	{ // data1 - data2
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolDataset&>(sym1).GetValue() - 
			dynamic_cast<const SymbolDataset&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
	{ // data - 1.23
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolDataset&>(sym1).GetValue() - 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}

	return nullptr;
}

std::shared_ptr<Symbol> Symbol::sub(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
	return Symbol::sub(*sym1, *sym2);
}


/**
 * multiplication of symbols
 */
std::shared_ptr<Symbol> Symbol::mul(const Symbol &sym1, const Symbol &sym2)
{
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
	{ // 1.23 * 2.34
		return std::make_shared<SymbolReal>(
			dynamic_cast<const SymbolReal&>(sym1).GetValue() * 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::STRING)
	{ // 5 * "123"
		const std::string &str = dynamic_cast<const SymbolString&>(sym2).GetValue();
		std::string strOut;

		std::size_t n = static_cast<std::size_t>(dynamic_cast<const SymbolReal&>(sym1).GetValue());
		for(std::size_t i=0; i<n; ++i)
			strOut += str;

		return std::make_shared<SymbolString>(strOut);
	}
	else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::REAL)
	{ // 5 * "123"
		const std::string &str = dynamic_cast<const SymbolString&>(sym1).GetValue();
		std::string strOut;

		std::size_t n = static_cast<std::size_t>(dynamic_cast<const SymbolReal&>(sym2).GetValue());
		for(std::size_t i=0; i<n; ++i)
			strOut += str;

		return std::make_shared<SymbolString>(strOut);
	}
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::ARRAY)
	{ // 5 * [1,2,3]
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym2).GetValue();
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::mul(sym1, *arr[idx]));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
	{ // [1,2,3] * 5
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::mul(*arr[idx], sym2));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
	{ // [1,2,3] * [1,2,3]
		return func_dot(std::make_shared<SymbolList>(dynamic_cast<const SymbolList&>(sym1)),
			std::make_shared<SymbolList>(dynamic_cast<const SymbolList&>(sym2)));
	}
	else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::DATASET)
	{ // 3 * data
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolReal&>(sym1).GetValue() * 
			dynamic_cast<const SymbolDataset&>(sym2).GetValue());
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
	{ // data * 3
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolDataset&>(sym1).GetValue() * 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}

	return nullptr;
}

std::shared_ptr<Symbol> Symbol::mul(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
	return Symbol::mul(*sym1, *sym2);
}


/**
 * division of symbols
 */
std::shared_ptr<Symbol> Symbol::div(const Symbol &sym1, const Symbol &sym2)
{
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
	{ // 1.23 / 5.67
		return std::make_shared<SymbolReal>(
			dynamic_cast<const SymbolReal&>(sym1).GetValue() / 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
	{ // [1,2,3] / 5
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::div(*arr[idx], sym2));
		return std::make_shared<SymbolList>(arrNew, false);
	}
	else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
	{ // data / 5.
		return std::make_shared<SymbolDataset>(
			dynamic_cast<const SymbolDataset&>(sym1).GetValue() / 
			dynamic_cast<const SymbolReal&>(sym2).GetValue());
	}

	return nullptr;
}

std::shared_ptr<Symbol> Symbol::div(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
	return Symbol::div(*sym1, *sym2);
}


/**
 * modulo of symbols
 */
std::shared_ptr<Symbol> Symbol::mod(const Symbol &sym1, const Symbol &sym2)
{
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
	{ // 12. % 5.
		return std::make_shared<SymbolReal>(std::fmod(
			dynamic_cast<const SymbolReal&>(sym1).GetValue(),
			dynamic_cast<const SymbolReal&>(sym2).GetValue()));
	}
	if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
	{ // [1,2,3] % 5
		std::vector<std::shared_ptr<Symbol>> arrNew;
		const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
		for(std::size_t idx=0; idx<arr.size(); ++idx)
			arrNew.emplace_back(Symbol::mod(*arr[idx], sym2));
		return std::make_shared<SymbolList>(arrNew, false);
	}

	return nullptr;
}


/**
 * power of symbols
 */
std::shared_ptr<Symbol> Symbol::pow(const Symbol &sym1, const Symbol &sym2)
{
	if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
	{
		return std::make_shared<SymbolReal>(std::pow(
			dynamic_cast<const SymbolReal&>(sym1).GetValue(),
			dynamic_cast<const SymbolReal&>(sym2).GetValue()));
	}

	return nullptr;
}

Tobias WEBER's avatar
Tobias WEBER committed
357

358
// ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
359
360


Tobias WEBER's avatar
Tobias WEBER committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// array markers
static const std::string begin_arr = "###[";
static const std::string end_arr = "###]";
static const std::string arr_next_elem = "###,";

// dataset markers
static const std::string next_ch = "###CH;";
static const std::string begin_ax_name = "###AXID[";
static const std::string end_ax_name = "###AXID]";
static const std::string begin_axes = "###AXES[";
static const std::string end_axes = "###AXES]";
static const std::string begin_ctrs = "###CTRS[";
static const std::string end_ctrs = "###CTRS]";
static const std::string begin_ctr_errs = "###CTRERRS[";
static const std::string end_ctr_errs = "###CTRERRS]";
static const std::string begin_mons = "###MONS[";
static const std::string end_mons = "###MONS]";
static const std::string begin_mon_errs = "###MONERRS[";
static const std::string end_mon_errs = "###MONERRS]";
static const std::string next_elem = "###;";



Tobias WEBER's avatar
Tobias WEBER committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
 * string representation of real
 */
std::string SymbolReal::serialise() const
{
	std::ostringstream ostr;
	ostr.precision(std::numeric_limits<t_real>::digits10);

	ostr << Symbol::get_type_name(*this) << ":" << GetValue();
	return ostr.str();
}


/**
 * string representation of string (trivial)
 */
std::string SymbolString::serialise() const
{
	return Symbol::get_type_name(*this) + ":" + GetValue();
}


/**
Tobias WEBER's avatar
Tobias WEBER committed
407
 * string representation of a list
Tobias WEBER's avatar
Tobias WEBER committed
408
409
410
411
412
413
 */
std::string SymbolList::serialise() const
{
	std::ostringstream ostr;
	ostr.precision(std::numeric_limits<t_real>::digits10);

Tobias WEBER's avatar
Tobias WEBER committed
414
	ostr << Symbol::get_type_name(*this) << ":" << begin_arr;
Tobias WEBER's avatar
Tobias WEBER committed
415
416
417
	bool firstelem = true;
	for(const auto &elem : m_val)
	{
Tobias WEBER's avatar
Tobias WEBER committed
418
		if(!firstelem) ostr << arr_next_elem;
Tobias WEBER's avatar
Tobias WEBER committed
419
420
421
		ostr << elem->serialise();
		firstelem = false;
	}
Tobias WEBER's avatar
Tobias WEBER committed
422
	ostr << end_arr;
Tobias WEBER's avatar
Tobias WEBER committed
423
424
425
426
427
428

	return ostr.str();
}


/**
Tobias WEBER's avatar
Tobias WEBER committed
429
 * string representation of a dataset
Tobias WEBER's avatar
Tobias WEBER committed
430
431
432
433
 */
std::string SymbolDataset::serialise() const
{
	std::ostringstream ostr;
Tobias WEBER's avatar
Tobias WEBER committed
434
	ostr.precision(std::numeric_limits<t_real>::digits10);
Tobias WEBER's avatar
Tobias WEBER committed
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
	ostr << Symbol::get_type_name(*this) << ":";

	const auto &dataset = GetValue();
	for(std::size_t chnr=0; chnr<dataset.GetNumChannels(); ++chnr)
	{
		const auto &data = dataset.GetChannel(chnr);
		const std::size_t num_x = data.GetNumAxes();
		const std::size_t num_ctr = data.GetNumCounters();
		const std::size_t num_mon = data.GetNumMonitors();

		// x axis names
		ostr << begin_ax_name;
		for(std::size_t xidx=0; xidx<num_x; ++xidx)
		{
			ostr << data.GetAxisName(xidx);
			if(xidx < num_x-1) ostr << ",";
		}
		ostr << end_ax_name;

		// x axes
		ostr << begin_axes;
		for(std::size_t xidx=0; xidx<num_x; ++xidx)
		{
			const auto &ax = data.GetAxis(xidx);
			for(std::size_t axidx=0; axidx<ax.size(); ++axidx)
			{
				ostr << ax[axidx];
				if(axidx < ax.size()-1) ostr << ",";
			}
			if(xidx < num_x-1) ostr << next_elem;
		}
		ostr << end_axes;


		// counters
		ostr << begin_ctrs;
		for(std::size_t ctridx=0; ctridx<num_ctr; ++ctridx)
		{
			const auto &ax = data.GetCounter(ctridx);
			for(std::size_t axidx=0; axidx<ax.size(); ++axidx)
			{
				ostr << ax[axidx];
				if(axidx < ax.size()-1) ostr << ",";
			}

			if(ctridx < num_ctr-1) ostr << next_elem;
		}
		ostr << end_ctrs;

		// counter errors
		ostr << begin_ctr_errs;
		for(std::size_t ctridx=0; ctridx<num_ctr; ++ctridx)
		{
			const auto &ax = data.GetCounterErrors(ctridx);
			for(std::size_t axidx=0; axidx<ax.size(); ++axidx)
			{
				ostr << ax[axidx];
				if(axidx < ax.size()-1) ostr << ",";
			}

			if(ctridx < num_ctr-1) ostr << next_elem;
		}
		ostr << end_ctr_errs;


		// monitors
		ostr << begin_mons;
		for(std::size_t ctridx=0; ctridx<num_mon; ++ctridx)
		{
			const auto &ax = data.GetMonitor(ctridx);
			for(std::size_t axidx=0; axidx<ax.size(); ++axidx)
			{
				ostr << ax[axidx];
				if(axidx < ax.size()-1) ostr << ",";
			}

			if(ctridx < num_mon-1) ostr << next_elem;
		}
		ostr << end_mons;

		// monitor errors
		ostr << begin_mon_errs;
		for(std::size_t ctridx=0; ctridx<num_mon; ++ctridx)
		{
			const auto &ax = data.GetMonitorErrors(ctridx);
			for(std::size_t axidx=0; axidx<ax.size(); ++axidx)
			{
				ostr << ax[axidx];
				if(axidx < ax.size()-1) ostr << ",";
			}

			if(ctridx < num_mon-1) ostr << next_elem;
		}
		ostr << end_mon_errs;

		ostr << next_ch;
	}
Tobias WEBER's avatar
Tobias WEBER committed
532
533
534
535
536
537
538
539
540
541

	return ostr.str();
}


/**
 * re-construct a symbol from a string representation
 */
std::shared_ptr<Symbol> Symbol::unserialise(const std::string &str)
{
Tobias WEBER's avatar
Tobias WEBER committed
542
543
544
	static const std::size_t len_begin_arr = begin_arr.length();


545
546
	auto [ty, val] = tl2::split_first(str, std::string(":"));
	tl2::trim(ty);
Tobias WEBER's avatar
Tobias WEBER committed
547
548
549

	if(ty == "real")
	{
550
		t_real d = tl2::str_to_var<t_real>(val);
Tobias WEBER's avatar
Tobias WEBER committed
551
552
553
554
555
556
557
558
		return std::make_shared<SymbolReal>(d);
	}
	else if(ty == "string")
	{
		return std::make_shared<SymbolString>(val);
	}
	else if(ty == "list" || ty == "array")
	{
Tobias WEBER's avatar
Tobias WEBER committed
559
560
561
562
563
564
565
566
567
568
569
570
		// find array boundaries in string repr
		std::size_t arr_begin = val.find(begin_arr);
		std::size_t arr_end = val.rfind(end_arr);
		if(arr_begin == std::string::npos || arr_end == std::string::npos)
		{
			print_err("Invalid array begin or end indicators: \"", val, "\".");
			return nullptr;
		}
		arr_begin += len_begin_arr;
		val = val.substr(arr_begin, arr_end-arr_begin);


Tobias WEBER's avatar
Tobias WEBER committed
571
		std::vector<std::shared_ptr<Symbol>> vec;
Tobias WEBER's avatar
Tobias WEBER committed
572
573
574

		// unserialise vector elements
		std::vector<std::string> toks;
575
		tl2::get_tokens_seq<std::string, std::string>(val, arr_next_elem, toks);
Tobias WEBER's avatar
Tobias WEBER committed
576
577
578
579
580
581
582
583
584
585
		std::string accum_tok;
		for(const auto &tok : toks)
		{
			// chain back together elements of nested sub-array
			if(accum_tok == "")
				accum_tok += tok;
			else
				accum_tok += arr_next_elem + tok;

			// balance brackets of nested arrays
586
			int nest_level = int(tl2::count_occurrences(accum_tok, begin_arr)) - int(tl2::count_occurrences(accum_tok, end_arr));
Tobias WEBER's avatar
Tobias WEBER committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
			if(nest_level < 0)
			{
				print_err("Unbalanced brackets in serialised array representation: \"", accum_tok, "\"");
				return nullptr;
			}
			else if(nest_level > 0) 	// get next token until end bracket is found
			{
				continue;
			}
			else if(nest_level == 0)	// found matching brackets
			{
				auto elemsym = unserialise(accum_tok);
				if(elemsym == nullptr)
				{
					print_err("Invalid array element: \"", accum_tok, "\".");
					return nullptr;
				}
				vec.emplace_back(elemsym);

				accum_tok = "";
			}
		}

Tobias WEBER's avatar
Tobias WEBER committed
610
611
612
613
614
		return std::make_shared<SymbolList>(vec, ty=="list");
	}
	else if(ty == "dataset")
	{
		Dataset dataset;
Tobias WEBER's avatar
Tobias WEBER committed
615
616
617

		// iterate channels
		std::vector<std::string> tok_channels;
618
		tl2::get_tokens_seq<std::string, std::string>(val, next_ch, tok_channels, true);
Tobias WEBER's avatar
Tobias WEBER committed
619
620
		for(auto &tok_ch : tok_channels)
		{
621
			tl2::trim(tok_ch);
Tobias WEBER's avatar
Tobias WEBER committed
622
623
624
			if(tok_ch == "") continue;

			// get the various parts of the data structure
625
626
627
628
629
630
			std::string str_axis_names = tl2::str_between(tok_ch, begin_ax_name, end_ax_name, false, true);
			std::string str_axes = tl2::str_between(tok_ch, begin_axes, end_axes, false, true);
			std::string str_ctrs = tl2::str_between(tok_ch, begin_ctrs, end_ctrs, false, true);
			std::string str_ctr_errs = tl2::str_between(tok_ch, begin_ctr_errs, end_ctr_errs, false, true);
			std::string str_mons = tl2::str_between(tok_ch, begin_mons, end_mons, false, true);
			std::string str_mon_errs = tl2::str_between(tok_ch, begin_mon_errs, end_mon_errs, false, true);
Tobias WEBER's avatar
Tobias WEBER committed
631
632
633
634
635
636
637


			Data data;

			// axis names
			{
				std::vector<std::string> axis_names;
638
				tl2::get_tokens<std::string, std::string>(str_axis_names, ",", axis_names);
Tobias WEBER's avatar
Tobias WEBER committed
639
640
641
642
643
644
				data.SetAxisNames(std::move(axis_names));
			}

			// axes
			{
				std::vector<std::string> tok_axes;
645
				tl2::get_tokens_seq<std::string, std::string>(str_axes, next_elem, tok_axes, true);
Tobias WEBER's avatar
Tobias WEBER committed
646
647
648
				for(const auto &tok_axis : tok_axes)
				{
					std::vector<t_real_dat> axis;
649
					tl2::get_tokens<t_real_dat, std::string>(tok_axis, ",", axis);
Tobias WEBER's avatar
Tobias WEBER committed
650
651
652
653
654
655
656
					data.AddAxis(std::move(axis));
				}
			}

			// counters
			{
				std::vector<std::string> tok_ctrs, tok_ctr_errs;
657
658
				tl2::get_tokens_seq<std::string, std::string>(str_ctrs, next_elem, tok_ctrs, true);
				tl2::get_tokens_seq<std::string, std::string>(str_ctr_errs, next_elem, tok_ctr_errs, true);
Tobias WEBER's avatar
Tobias WEBER committed
659
660
661
662
663
664
				for(std::size_t idxax=0; idxax<std::min(tok_ctrs.size(),tok_ctr_errs.size()); ++idxax)
				{
					const auto &tok_axis = tok_ctrs[idxax];
					const auto &tok_axis_err = tok_ctr_errs[idxax];

					std::vector<t_real_dat> axis, axis_err;
665
666
					tl2::get_tokens<t_real_dat, std::string>(tok_axis, ",", axis);
					tl2::get_tokens<t_real_dat, std::string>(tok_axis_err, ",", axis_err);
Tobias WEBER's avatar
Tobias WEBER committed
667
668
669
670
671
672
673
674

					data.AddCounter(std::move(axis), std::move(axis_err));
				}
			}

			// monitors
			{
				std::vector<std::string> tok_ctrs, tok_ctr_errs;
675
676
				tl2::get_tokens_seq<std::string, std::string>(str_mons, next_elem, tok_ctrs, true);
				tl2::get_tokens_seq<std::string, std::string>(str_mon_errs, next_elem, tok_ctr_errs, true);
Tobias WEBER's avatar
Tobias WEBER committed
677
678
679
680
681
682
				for(std::size_t idxax=0; idxax<std::min(tok_ctrs.size(),tok_ctr_errs.size()); ++idxax)
				{
					const auto &tok_axis = tok_ctrs[idxax];
					const auto &tok_axis_err = tok_ctr_errs[idxax];

					std::vector<t_real_dat> axis, axis_err;
683
684
					tl2::get_tokens<t_real_dat, std::string>(tok_axis, ",", axis);
					tl2::get_tokens<t_real_dat, std::string>(tok_axis_err, ",", axis_err);
Tobias WEBER's avatar
Tobias WEBER committed
685
686
687
688
689
690
691

					data.AddMonitor(std::move(axis), std::move(axis_err));
				}
			}

			dataset.AddChannel(std::move(data));
		}
Tobias WEBER's avatar
Tobias WEBER committed
692
693
694
695
696

		return std::make_shared<SymbolDataset>(dataset);
	}

	print_err("Unknown variable type: ", ty, ".");
Tobias WEBER's avatar
Tobias WEBER committed
697
698
	return nullptr;
}