Blame view

src/InputOutput/IHMImpl/Tools/IHMParamManagerClass.php 29.8 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
<?php

/**
 * @class IHMParamManagerClass
 * @brief Parameter manager
 * @details
 */
class IHMParamManagerClass
{
96c9cffa   Benjamin Renard   Keep expression p...
10
	protected $userParameterMgr = null;
bf27ba04   Benjamin Renard   Add templated par...
11
	protected $templateParamsManager = null;
ffc5cb81   Elena.Budnik   temporary commit
12
	protected $paramImpexMgr     = null;
52c1e291   Benjamin Renard   Generate TT and c...
13
	protected $ttCatMgr = null;
f03c62e3   Benjamin Renard   Keep in cache res...
14
15

	protected $cacheExpressionParser = null;
22521f1c   Benjamin Renard   First commit
16
17
18
19
20
21
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
bf27ba04   Benjamin Renard   Add templated par...
22
		$this->templateParamsManager = new IHMParamTemplateClass();
f03c62e3   Benjamin Renard   Keep in cache res...
23
		$this->cacheExpressionParser = array();
22521f1c   Benjamin Renard   First commit
24
25
	}

4c975261   Benjamin Renard   Add modif. time f...
26
	public function addGeneratedParam($paramId, $expression, $sampling_mode, $sampling_step, $ref_param, $paramsData)
cd28a380   Benjamin Renard   Generate param in...
27
	{
a928f9d3   Benjamin Renard   New parser integr...
28
29
30
31
		$expressionInfo = $this->parseExpression($expression, $paramsData->getWorkingPath());
		if (!$expressionInfo['success']) {
			throw new Exception($expressionInfo['message']);
		}
cd28a380   Benjamin Renard   Generate param in...
32
33
34

                //create a process param for the derived parameter
                $this->addProcessParam($paramId, $expressionInfo["expression"], $expression,
4c975261   Benjamin Renard   Add modif. time f...
35
                                $expressionInfo['params'], $sampling_mode, $sampling_step, $ref_param,
cd28a380   Benjamin Renard   Generate param in...
36
37
38
                                0, time(), "", "", $paramsData);
	}

22521f1c   Benjamin Renard   First commit
39
40
41
	/*
	 * @brief Add an existing parameter
	*/
52859c27   Benjamin Renard   Give the possibil...
42
	public function addExistingParam($param, $paramsData, $templateArgs = NULL, $tableLink = NULL)
d1a35428   Benjamin Renard   Update template a...
43
	{
22521f1c   Benjamin Renard   First commit
44
45
46
47
		if ($this->isDerivedParam($param))
			return $this->addDerivedParam($param,$paramsData);
		else if ($this->isUploadedParam($param))
			return $this->addUploadedParam($param,$paramsData);
ffc5cb81   Elena.Budnik   temporary commit
48
49
		else if ($this->isImpexParam($param))
			return $this->addImpexParam($param,$paramsData,$templateArgs);	
22521f1c   Benjamin Renard   First commit
50
		else
52859c27   Benjamin Renard   Give the possibil...
51
			return $this->addLocalParam($param,$paramsData,$templateArgs, $tableLink);
22521f1c   Benjamin Renard   First commit
52
53
54
55
56
57
		return "";
	}

	/*
	 * @brief Add a process parameter
	*/
4c975261   Benjamin Renard   Add modif. time f...
58
	public function addProcessParam($paramId,$expression,$expression_info,$params, $sampling_mode, $sampling_step, $ref_param, $gap,$dateModif,$units,$ytitle,$paramsData)
c0535e83   Benjamin Renard   Use units and yTi...
59
	{
4c975261   Benjamin Renard   Add modif. time f...
60
		$paramsData->addProcessParamToCreate($paramId, $expression, $expression_info, $params, $sampling_mode, $sampling_step, $ref_param, $gap,$dateModif, $units, $ytitle); 
ffc5cb81   Elena.Budnik   temporary commit
61
	 
e4545ed5   Benjamin Renard   Implement templat...
62
		foreach ($params as $param) {
ffc5cb81   Elena.Budnik   temporary commit
63
64
			$template_args = NULL; 
			 
e4545ed5   Benjamin Renard   Implement templat...
65
66
			if (array_key_exists("template_args", $param))
				$template_args = $param["template_args"];
ffc5cb81   Elena.Budnik   temporary commit
67
	 		 
e4545ed5   Benjamin Renard   Implement templat...
68
69
			$this->addExistingParam($param["paramid"],$paramsData,$template_args);
		}
4c975261   Benjamin Renard   Add modif. time f...
70
71
72
		if (!empty($ref_param)) {
			$this->addExistingParam($ref_param, $paramsData,$template_args);
		}
22521f1c   Benjamin Renard   First commit
73
74
75
76
77

		return true;
	}

	/*
52c1e291   Benjamin Renard   Generate TT and c...
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
	 *  @brief Add a TT or catalog parameter
	 */
	public function addTTCatParam($paramId, $paramsData)
	{
		if (!isset($this->ttCatMgr)) {
			$this->ttCatMgr = new IHMTTCatLoaderClass();
		}
		$info = $this->ttCatMgr->getTTCatInfoFromId($paramId);

		if (!$info["success"]) {
			throw new Exception($info["message"]);
		}

		$status = array();
		$units = "";
		$ytitle = "";
		$flag = "";

		if ($info["info"]["isCatalog"]) {
			$parameters = $this->ttCatMgr->getCatalogParameters($paramId);
			if (!$parameters["success"] || empty($parameters["parameters"])) {
				throw new Exception("Error to extract first parameter of ".$paramId);
			}
			//For the moment, use the first parameter
			$parameter = $parameters["parameters"][0];
			$units     = $parameter["units"];
			$ytitle    = !empty($parameter["name"]) ? $parameter["name"] : $parameter["id"];
			$flag      = $parameter["id"];
			$description = $parameter["description"];
			$desc_parts = array();
			foreach (explode("-",$description) as $def) {
				$def = trim($def);
				$p = strpos($def, ":");
				if ($p === FALSE) {
					continue;
				}
				$val = substr($def, 0, $p);
				$val = trim($val);
				$name = substr($def, $p+1);
				$name = trim($name);
				$color = "";
				$p = strpos($name, "[");
				if ($p !== FALSE) {
					$color = substr($name, $p);
					$color = trim($color);
					$name = substr($name, 0, $p);
					$name = trim($name);
				}
				$status[] = array(
					"min" => floatval($val),
					"max" => floatval($val),
					"name" => $name,
					"color" => $color,
				);
			}
		}
		else {
			$status = array(
				array(
					"min" => 1,
					"max" => 1,
					"name" => "Inside",
					"color" => "[255,0,0]",
				)
			);
			$ytitle = $info["info"]["name"];
		}

		$paramsData->addTTCatParamToCreate($paramId, $info["info"]["path"], $info["info"]["isShared"], filemtime($info["info"]["path"]), $units, $ytitle, $status, $flag);
		return array("id" => $paramId, "indexes" => array(), "calib_infos" => array());
	} 

	/*
22521f1c   Benjamin Renard   First commit
151
152
153
154
155
156
157
158
159
160
	 * @brief Detect if it's a derived parameter
	*/
	private function isDerivedParam($param)
	{
		return preg_match("#^ws_#",$param);
	}

	/*
	 * @brief Detect if it's a uploaded parameter
	*/
dcdacd5c   Benjamin Renard   Fill plot type in...
161
	public function isUploadedParam($param)
22521f1c   Benjamin Renard   First commit
162
163
164
	{
		return preg_match("#^wsd_#",$param);
	}
ffc5cb81   Elena.Budnik   temporary commit
165
166
167
168
169
170
171
172
	
	/*
	 * @brief Detect if it's IMPEX parameter
	*/
	private function isImpexParam($param)
	{
		return preg_match("#^".IHMImpexParamClass::$paramPrefix."#",$param);
	}
22521f1c   Benjamin Renard   First commit
173
174
175
176

	/*
	 * @brief Add a local parameter
	*/
52859c27   Benjamin Renard   Give the possibil...
177
	private function addLocalParam($param, $paramsData, $templateArgs, $tableLink)
22521f1c   Benjamin Renard   First commit
178
179
180
181
182
183
184
185
186
187
188
189
190
191
	{
		//local parameter
		$indexes     = array();
		$calib_infos = array();
		//check for components
		$pattern = "/(?P<param>.*)\((?P<components>.*)\)/";
		preg_match_all($pattern, $param, $matches);
		if ((count($matches["param"]) > 0) && (count($matches["components"]) > 0))
		{
			$paramId       = $matches["param"][0];
			$indexes = explode(",",$matches["components"][0]);
		}
		else
			$paramId = $param;
ffc5cb81   Elena.Budnik   temporary commit
192
	 
bf27ba04   Benjamin Renard   Add templated par...
193
194
		//check templated parameter
		$real_param_id = $paramId;
0dd42438   Benjamin Renard   WS getDataset wit...
195
		$paramPath = "";
bf27ba04   Benjamin Renard   Add templated par...
196
		if ($this->templateParamsManager->isTemplatedParam($paramId)) {
52859c27   Benjamin Renard   Give the possibil...
197
			$paramPath = $this->templateParamsManager->generateTemplatedParamFile($paramId, $templateArgs, $tableLink);
bf27ba04   Benjamin Renard   Add templated par...
198
			$real_param_id = $this->templateParamsManager->getTemplatedParamId($paramId, $templateArgs);
bf27ba04   Benjamin Renard   Add templated par...
199
200
201
202
203
204
205
206
			if (empty($paramPath) || !file_exists($paramPath))
				throw new Exception('Cannot generate parameter template file '.$paramId);
		}
		else {
			$paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";
			if (!file_exists($paramPath))
				throw new Exception('Cannot find parameter local file '.$paramId);
		}
22521f1c   Benjamin Renard   First commit
207

bf27ba04   Benjamin Renard   Add templated par...
208
		$paramsData->addParamToCopy($real_param_id,$paramPath);
22521f1c   Benjamin Renard   First commit
209

bf27ba04   Benjamin Renard   Add templated par...
210
		$this->addLinkedLocalParams($paramId, $paramsData, $templateArgs);
22521f1c   Benjamin Renard   First commit
211
			
0dd42438   Benjamin Renard   WS getDataset wit...
212
		return array("id" => $real_param_id, "indexes" => $indexes, "calib_infos" => $calib_infos, "path" => $paramPath);
22521f1c   Benjamin Renard   First commit
213
214
215
216
217
	}

	/*
	 * @brief Add linked parameter
	*/
bf27ba04   Benjamin Renard   Add templated par...
218
	private function addLinkedLocalParams($paramId,$paramsData,$templateArgs = NULL)
22521f1c   Benjamin Renard   First commit
219
	{
952dd7c7   elena   catalog integration
220
221
		$doc = new DOMDocument("1.0", "UTF-8");
		$doc->preserveWhiteSpace = false;
22521f1c   Benjamin Renard   First commit
222
223
		$doc->formatOutput = true;

cd3326be   Benjamin Renard   Fix function used...
224
		$real_param_id = $paramId;
bf27ba04   Benjamin Renard   Add templated par...
225
		if ($this->templateParamsManager->isTemplatedParam($paramId)) {
cd3326be   Benjamin Renard   Fix function used...
226
			$paramPath = $this->templateParamsManager->getTemplatePath($paramId);
8ade06fe   Benjamin Renard   Improve config load
227
			if (empty($paramPath) || !@$doc->load($paramPath))
f1252b32   Benjamin Renard   Add template for ...
228
				throw new Exception('Cannot find parameter template file '.$paramId);
bf27ba04   Benjamin Renard   Add templated par...
229
230
231
		}
		else {
			$paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";
8ade06fe   Benjamin Renard   Improve config load
232
			if (empty($paramPath) || !@$doc->load($paramPath))
f1252b32   Benjamin Renard   Add template for ...
233
				throw new Exception('Cannot find parameter local file '.$paramId);
bf27ba04   Benjamin Renard   Add templated par...
234
235
		}
			
22521f1c   Benjamin Renard   First commit
236
237
238
239
240
241
242
243
244
245
246
247
248
249
		//<get>
		$getNodes = $doc->getElementsByTagName('get');

		if ($getNodes->length <= 0)
			throw new Exception('Parameter local file '.$paramId.' dont have a getter node');

		$getNode = $getNodes->item(0);

		//<amdaParam name="imf"/>
		$amdaParamNodes = $doc->getElementsByTagName('amdaParam');

		foreach($amdaParamNodes as $amdaParamNode)
		{
			$linkedParamId = $amdaParamNode->getAttribute('name');
cd3326be   Benjamin Renard   Fix function used...
250
			if (empty($linkedParamId))
22521f1c   Benjamin Renard   First commit
251
				continue;
7911f4bc   Benjamin Renard   Another bug with ...
252
			$template_id = $this->templateParamsManager->getTemplateId($linkedParamId);
d1a35428   Benjamin Renard   Update template a...
253
			$tempArgs = isset($templateArgs) ? $templateArgs : array();
7911f4bc   Benjamin Renard   Another bug with ...
254
255
256
			if ($template_id !== FALSE) {
				$linkedParamId = $template_id;
			}
7d35612a   Benjamin Renard   Fix ParamManager ...
257
258
			else {
				$linkedParamId = $this->templateParamsManager->replaceArgs($linkedParamId, $templateArgs);
97d1c981   Benjamin Renard   Fix bug with a te...
259
260
261
				$parsedParam = $this->templateParamsManager->parseTemplatedParam($linkedParamId);
				if ($parsedParam !== FALSE) {
					$linkedParamId = $parsedParam['paramid'];
d1a35428   Benjamin Renard   Update template a...
262
263
264
					foreach ($parsedParam['template_args'] as $key => $val) {
						$tempArgs[$key] = $val;
					}
97d1c981   Benjamin Renard   Fix bug with a te...
265
				}
7d35612a   Benjamin Renard   Fix ParamManager ...
266
			}
7911f4bc   Benjamin Renard   Another bug with ...
267
268

			if ($this->templateParamsManager->isTemplatedParam($linkedParamId)) {
d1a35428   Benjamin Renard   Update template a...
269
270
				$linkedParamPath = $this->templateParamsManager->generateTemplatedParamFile($linkedParamId, $tempArgs);
				$real_linked_param_id = $this->templateParamsManager->getTemplatedParamId($linkedParamId, $tempArgs);
cd3326be   Benjamin Renard   Fix function used...
271
272
			}
			else {
7911f4bc   Benjamin Renard   Another bug with ...
273
274
				$real_linked_param_id = $linkedParamId;
				$linkedParamPath = IHMConfigClass::getLocalParamDBPath().$real_linked_param_id.".xml";
bf27ba04   Benjamin Renard   Add templated par...
275
			}
7911f4bc   Benjamin Renard   Another bug with ...
276
277
			$paramsData->addParamToCopy($real_linked_param_id,$linkedParamPath);
			$this->addLinkedLocalParams($linkedParamId, $paramsData);
22521f1c   Benjamin Renard   First commit
278
279
280
281
282
283
284
285
		}
	}

	/*
	 * @brief Add derived parameter
	*/
	private function addDerivedParam($param,$paramsData)
	{
f03c62e3   Benjamin Renard   Keep in cache res...
286
		$time = time();
96c9cffa   Benjamin Renard   Keep expression p...
287
288
		if (!isset($this->userParameterMgr))
			$this->userParameterMgr = new IHMUserParamManagerClass();
22521f1c   Benjamin Renard   First commit
289
290
		
		//get derived parameter info
96c9cffa   Benjamin Renard   Keep expression p...
291
		$res = $this->userParameterMgr->getDerivedParameterFromName($param);
f03c62e3   Benjamin Renard   Keep in cache res...
292

22521f1c   Benjamin Renard   First commit
293
294
295
		if (!$res["success"])
			throw new Exception('Error to load derived parameter file : '.$res["message"]);

96c9cffa   Benjamin Renard   Keep expression p...
296
297
298
299
300
301
302
		if (isset($res["param"]["info"]["parsedExpression"])) {
			//Re use existing parsed expression info
			$expressionInfo = $res["param"]["info"]["parsedExpression"];
		}
		else {
			//parse expression
			$expressionInfo = $this->parseExpression($res["param"]["expression"], $paramsData->getWorkingPath());
a928f9d3   Benjamin Renard   New parser integr...
303

96c9cffa   Benjamin Renard   Keep expression p...
304
305
306
307
308
			if (!$expressionInfo['success']) {
				throw new Exception($expressionInfo['message']);
			}

			$paramsList = array();
60305679   Benjamin Renard   Fix typo (#7500)
309
310
			foreach ($expressionInfo['params'] as $p) {
				$paramsList[] = $p["paramid"];
96c9cffa   Benjamin Renard   Keep expression p...
311
312
			}
			$this->userParameterMgr->saveDerivedParameterParsedExpression($res["param"]["id"], $expressionInfo["expression"], md5($res["param"]["expression"]), $paramsList);
a928f9d3   Benjamin Renard   New parser integr...
313
		}
96c9cffa   Benjamin Renard   Keep expression p...
314

952dd7c7   elena   catalog integration
315
316
317
		$paramId = $param;
		
		//create a process param for the derived parameter
e9165342   Benjamin Renard   Write data mining...
318
		$this->addProcessParam($paramId, $expressionInfo["expression"], $res["param"]["expression"],
4c975261   Benjamin Renard   Add modif. time f...
319
				$expressionInfo['params'], $res["param"]["info"]["sampling_mode"], $res["param"]["info"]["timestep"], $res["param"]["info"]["reference_param"],
c0535e83   Benjamin Renard   Use units and yTi...
320
321
				0,$res["param"]["dateModif"],!empty($res["param"]["info"]["units"]) ? $res["param"]["info"]["units"] : "",
				!empty($res["param"]["info"]["yTitle"]) ? $res["param"]["info"]["yTitle"] : "", $paramsData);
f03c62e3   Benjamin Renard   Keep in cache res...
322

22521f1c   Benjamin Renard   First commit
323
324
		return array("id" => $paramId, "indexes" => array(), "calib_infos" => array());
	}
d1a35428   Benjamin Renard   Update template a...
325

ffc5cb81   Elena.Budnik   temporary commit
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
	/*
	 * @brief Add IMPEX parameter : create dynamically xml parameter descriptor in user WS
	*/
	private function addImpexParam($param,$paramsData,$templateArgs = NULL)	
	{		 	
		if (!isset($this->paramImpexMgr))
			$this->paramImpexMgr = new ImpexParamManager();
			
		$parameterID = $this->paramImpexMgr->getImpexFullParamId($param, $templateArgs);
		
		$requestNodes = $paramsData->getRequestNodes();
		 
		// it is PARAMSGEN 
		if (!$requestNodes) {
			return array("id" => $parameterID); 
		}
	 
		$timesNode = $requestNodes[0]->getTimesNode();
		$intervals = $timesNode->getIntervals();
		$originFile = IHMConfigClass::getUserWSPath().$parameterID.".xml";
	  
		if (!file_exists($originFile)) {
			// create IMPEX parameter info and data				
			$res = $this->paramImpexMgr->createImpexParameter($param, $intervals, $templateArgs);
			
			if (!$res["success"])
				throw new Exception('Error to create IMPEX parameter : '.$res["message"]);
				
			$newParamNode = $paramsData->addLocalParamToCreate($res["param"]["id"], 
				$res["param"]["info"]["viId"], $res["param"]["info"]["realVar"],
				$res["param"]["info"]["minSampling"], $res["param"]["info"]["maxSampling"],
				$res["param"]["info"]["type"],$res["param"]["info"]["size"],
e99d9ed4   Benjamin Renard   Support fillValue...
358
				NULL,$res["param"]["dateModif"],true);
ffc5cb81   Elena.Budnik   temporary commit
359
				
577d5c04   Elena.Budnik   IMPEX integration
360
361
362
363
				$newParamNode->getInfo()->setName($res["param"]["info"]['name']);
				$newParamNode->getInfo()->setShortName($res["param"]["info"]['name']);
				$newParamNode->getInfo()->setUnits($res["param"]["info"]['units']);
				
ffc5cb81   Elena.Budnik   temporary commit
364
365
366
				$tableDef = $res["param"]["info"]["tableDef"];
				if (isset($tableDef) && array_key_exists('tableDefType', $tableDef) && ($tableDef['tableDefType'] != 'NONE'))
				{
577d5c04   Elena.Budnik   IMPEX integration
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
					switch ($tableDef['channelsDefType'])
					{
						case 'BOUND'  :
							$boundTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::BOUNDS, $res["param"]["info"]["yTitle"]);
							$boundTable->setUnits($res["param"]["info"]["yUnits"]);
							switch ($tableDef['tableDefType'])
							{
								case 'SELECT' :
									$boundTable->setBoundsName($tableDef['data']['bound']);
									$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['bound']);
									break;
								default :
									throw new Exception("Unknown tableDefType ".$tableDef['tableDefType']);
							}
							break;
						case 'CENTER' :
							$centerTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::CENTER, $res["param"]["info"]["yTitle"]);
							$centerTable->setUnits($res["param"]["info"]["yUnits"]);
							$centerTable->setSize($tableDef['data']['width']);
							switch ($tableDef['tableDefType'])
							{
								case 'SELECT' :
									$centerTable->setCenterName($tableDef['data']['center']);
									$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['center']);
									break;
								default :
									throw new Exception("Unknown tableDefType ".$tableDef['tableDefType']);
							}
						break;
						default :
							throw new Exception("Unknown tableDefType ".$tableDef['channelsDefType']);
					}
ffc5cb81   Elena.Budnik   temporary commit
399
400
401
402
403
404
405
406
407
408
409
410
411
				}
				
				return array("id" => $res["param"]["id"], "plotType" => $res["param"]["info"]["plotType"]);
			}
		else {		     
			 // add IMPEX parameter data
			 $this->paramImpexMgr->addImpexData($param, $intervals, $templateArgs);			 
			 $paramsData->addParamToCopy($parameterID, $originFile);
			 
			 return array("id" => $parameterID);
		} 
	}
	
22521f1c   Benjamin Renard   First commit
412
413
414
415
416
	/*
	 * @brief Add uploaded parameter
	*/
	private function addUploadedParam($param,$paramsData)
	{
96c9cffa   Benjamin Renard   Keep expression p...
417
418
		if (!isset($this->userParameterMgr))
			$this->userParameterMgr = new IHMUserParamManagerClass();
944199fe   Benjamin Renard   Use table definit...
419
420
		
		//get uploaded parameter info
96c9cffa   Benjamin Renard   Keep expression p...
421
		$res = $this->userParameterMgr->getUploadedParameterFromName($param);
944199fe   Benjamin Renard   Use table definit...
422
423
424
		
		if (!$res["success"])
			throw new Exception('Error to load uploaded parameter file : '.$res["message"]);
67c43328   Benjamin Renard   Fix uploaded para...
425
426

		$paramId = $param;
944199fe   Benjamin Renard   Use table definit...
427
		
67c43328   Benjamin Renard   Fix uploaded para...
428
		$newParamNode = $paramsData->addLocalParamToCreate($paramId, 
944199fe   Benjamin Renard   Use table definit...
429
430
				$res["param"]["info"]["viId"], $res["param"]["info"]["realVar"],
				$res["param"]["info"]["minSampling"], $res["param"]["info"]["maxSampling"],
e8ff4ba7   Elena.Budnik   integration of as...
431
				$res["param"]["info"]["type"],$res["param"]["info"]["size"],
e99d9ed4   Benjamin Renard   Support fillValue...
432
				$res["param"]["info"]["fillValue"],$res["param"]["dateModif"]);
e8ff4ba7   Elena.Budnik   integration of as...
433

8289ec06   Benjamin Renard   Y-Title and Units...
434
435
436
437
438
439
440
441
		if (!empty($res["param"]["info"]["yTitle"])) {
			$newParamNode->getInfo()->setName($res["param"]["info"]['yTitle']);
			$newParamNode->getInfo()->setShortName($res["param"]["info"]['yTitle']);
		}
		if (!empty($res["param"]["info"]['units'])) {
			$newParamNode->getInfo()->setUnits($res["param"]["info"]['units']);
		}

944199fe   Benjamin Renard   Use table definit...
442
443
444
445
		
		$tableDef = $res["param"]["info"]["tableDef"];
		if (isset($tableDef) && array_key_exists('tableDefType', $tableDef) && ($tableDef['tableDefType'] != 'NONE'))
		{
df380a21   Benjamin Renard   Fix table name de...
446
447
			$tableName = empty($tableDef['tableName']) ? 'Table' : $tableDef['tableName'];
			$tableUnits = empty($tableDef['tableUnits']) ? '' : $tableDef['tableUnits'];
944199fe   Benjamin Renard   Use table definit...
448
449
450
			switch ($tableDef['channelsDefType'])
			{
				case 'MINMAX' :
df380a21   Benjamin Renard   Fix table name de...
451
452
					$minMaxTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::MINMAX, $tableName);
					$minMaxTable->setUnits($tableUnits);
944199fe   Benjamin Renard   Use table definit...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
					switch ($tableDef['tableDefType'])
					{
						case 'SELECT' :
							$minMaxTable->setMinName($tableDef['data']['min']);
							$minMaxTable->setMaxName($tableDef['data']['max']);
							$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['min']);
							$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['max']);
							break;
						case 'MANUAL' :
							$minMaxTable->setMinName('min');
							$minMaxTable->setMaxName('max');
							$clbMinNode = $newParamNode->addClbManual('min');
							$clbMinValues = explode(',', $tableDef['data']['min']);
							foreach ($clbMinValues as $value)
								$clbMinNode->addClbValue($value);
							$clbMaxNode = $newParamNode->addClbManual('max');
							$clbMaxValues = explode(',', $tableDef['data']['max']);
							foreach ($clbMaxValues as $value)
								$clbMaxNode->addClbValue($value);
						break;
					}
					break;
				case 'BOUND'  :
df380a21   Benjamin Renard   Fix table name de...
476
477
					$boundTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::BOUNDS, $tableName);
					$boundTable->setUnits($tableUnits);
944199fe   Benjamin Renard   Use table definit...
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
					switch ($tableDef['tableDefType'])
					{
						case 'SELECT' :
							$boundTable->setBoundsName($tableDef['data']['bound']);
							$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['bound']);
							break;
						case 'MANUAL' :
							$boundTable->setBoundsName('bound');
							$clbBoundNode = $newParamNode->addClbManual('bound');
							$clbBoundValues = explode(',', $tableDef['data']['bound']);
							foreach ($clbBoundValues as $value)
								$clbBoundNode->addClbValue($value);
							break;
					}
					break;
				case 'CENTER' :
df380a21   Benjamin Renard   Fix table name de...
494
495
					$centerTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::CENTER, $tableName);
					$centerTable->setUnits($tableUnits);
944199fe   Benjamin Renard   Use table definit...
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
					$centerTable->setSize($tableDef['data']['width']);
					switch ($tableDef['tableDefType'])
					{
						case 'SELECT' :
							$centerTable->setCenterName($tableDef['data']['center']);
							$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['center']);
							break;
						case 'MANUAL' :
							$centerTable->setCenterName('center');
							$clbCenterNode = $newParamNode->addClbManual('center');
							$clbCenterValues = explode(',', $tableDef['data']['center']);
							foreach ($clbCenterValues as $value)
								$clbCenterNode->addClbValue($value);
							break;
					}
					break;
				case 'CENTERWIDTH' :
df380a21   Benjamin Renard   Fix table name de...
513
514
					$centerWidthTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::CENTERWIDTH, $tableName);
					$centerWidthTable->setUnits($tableUnits);
944199fe   Benjamin Renard   Use table definit...
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
					switch ($tableDef['tableDefType'])
					{
						case 'SELECT' :
							$centerWidthTable->setCenterName($tableDef['data']['center']);
							$centerWidthTable->setWidthName($tableDef['data']['width']);
							$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['center']);
							$newParamNode->getParamGet()->getLocalParam($res["param"]["info"]["realVar"])->addCalibInfo($tableDef['data']['width']);
							break;
						case 'MANUAL' :
							$centerWidthTable->setCenterName('center');
							$centerWidthTable->setWidthName('width');
							$clbCenterNode = $newParamNode->addClbManual('center');
							$clbWidthNode = $newParamNode->addClbManual('width');
							$clbCenterValues = explode(',', $tableDef['data']['center']);
							foreach ($clbCenterValues as $value)
								$clbCenterNode->addClbValue($value);
							$clbWidthValues = explode(',', $tableDef['data']['width']);
							foreach ($clbWidthValues as $value)
								$clbWidthNode->addClbValue($value);
							break;
					}
					break;
			}
		}
		
058b366f   Benjamin Renard   Fix typo
540
		return array("id" => $paramId, "plotType" => $res["param"]["info"]["plotType"]);
22521f1c   Benjamin Renard   First commit
541
	}
93c50989   Benjamin Renard   Support Tab2D par...
542
543

	public function applyRangesAndIndexes($paramsData, $paramData, $force_total_2d, &$paramInfo) {
db7ea505   Benjamin Renard   Fix min max range...
544
545
546
547
548
		$dim1_min = 0;
		$dim1_max = 0;
		$dim2_min = 0;
		$dim2_max = 0;

76792c0e   Benjamin Renard   Sum parameters un...
549
		$dim1_is_range = (isset($paramData->{'dim1-sum-type'}) && ($paramData->{'dim1-sum-type'} > 0));
eb0ae558   Hacene SI HADJ MOHAND   integration ok
550
                $processType1 = !empty($paramData->{'dim1-process-type'}) ? $paramData->{'dim1-process-type'} : '';
ef1d608b   Hacene SI HADJ MOHAND   correcting typo i...
551
                $processType2 = !empty($paramData->{'dim2-process-type'}) ? $paramData->{'dim2-process-type'} : '';
2c9760b6   Benjamin Renard   Fix empty index d...
552
		$dim1_index = ($dim1_is_range || !isset($paramData->{'dim1-index'}) || ($paramData->{'dim1-index'} == '')) ? '*' : $paramData->{'dim1-index'};
db7ea505   Benjamin Renard   Fix min max range...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
		if ($dim1_is_range) {
			switch ($paramData->{'dim1-sum-type'}) {
				case 1:
					//Sum between a range of value
					$dim1_min = !empty($paramData->{'dim1-min-value'}) ? $paramData->{'dim1-min-value'} : 0;
					$dim1_max = !empty($paramData->{'dim1-max-value'}) ? $paramData->{'dim1-max-value'} : 0;
					break;
				case 2:
					//Sum between indexes
					$dim1_min = !empty($paramData->{'dim1-min-index'}) ? $paramData->{'dim1-min-index'} : 0;
					$dim1_max = !empty($paramData->{'dim1-max-index'}) ? $paramData->{'dim1-max-index'} : 0;
					break;
			}
		}
93c50989   Benjamin Renard   Support Tab2D par...
567

76792c0e   Benjamin Renard   Sum parameters un...
568
		$dim2_is_range = (isset($paramData->{'dim2-sum-type'}) && ($paramData->{'dim2-sum-type'} > 0));
2c9760b6   Benjamin Renard   Fix empty index d...
569
		$dim2_index = ($dim2_is_range || !isset($paramData->{'dim2-index'}) || ($paramData->{'dim2-index'} == '')) ? '*' : $paramData->{'dim2-index'};
db7ea505   Benjamin Renard   Fix min max range...
570
571
572
573
574
575
576
577
578
579
580
581
582
583
		if ($dim2_is_range) {
			switch ($paramData->{'dim2-sum-type'}) {
				case 1:
					//Sum between a range of value
					$dim2_min = !empty($paramData->{'dim2-min-value'}) ? $paramData->{'dim2-min-value'} : 0;
					$dim2_max = !empty($paramData->{'dim2-max-value'}) ? $paramData->{'dim2-max-value'} : 0;
					break;
				case 2:
					//Sum between indexes
					$dim2_min = !empty($paramData->{'dim2-min-index'}) ? $paramData->{'dim2-min-index'} : 0;
					$dim2_max = !empty($paramData->{'dim2-max-index'}) ? $paramData->{'dim2-max-index'} : 0;
					break;
			}
		}
93c50989   Benjamin Renard   Support Tab2D par...
584
585
586
587
588
589
590
591
592
593
594
595
596

		switch ($paramData->{'type'}) {
			case 0:
				//scalar - nothing to do
				break;
			case 1:
				//Tab1D
				if ($dim1_is_range) {
					$template_args = array(
						'paramid' => $paramInfo['id'],
						'min' => $dim1_min,
						'max' => $dim1_max,
						'relateddim' => 0,
eb0ae558   Hacene SI HADJ MOHAND   integration ok
597
                                                'processType' => $processType1,
93c50989   Benjamin Renard   Support Tab2D par...
598
					);
76792c0e   Benjamin Renard   Sum parameters un...
599
					if ($paramData->{'dim1-sum-type'} == 2)
eb0ae558   Hacene SI HADJ MOHAND   integration ok
600
						$paramInfo = $this->addExistingParam('table_indexes', $paramsData, $template_args);
54219b37   Benjamin Renard   Add support for s...
601
					else
eb0ae558   Hacene SI HADJ MOHAND   integration ok
602
						$paramInfo = $this->addExistingParam('table_range', $paramsData, $template_args);
93c50989   Benjamin Renard   Support Tab2D par...
603
604
605
606
607
608
609
				}
				else if ($dim2_is_range) {
					$template_args = array(
						'paramid' => $paramInfo['id'],
						'min' => $dim2_min,
						'max' => $dim2_max,
						'relateddim' => 1,
eb0ae558   Hacene SI HADJ MOHAND   integration ok
610
                                                'processType' => $processType2,
93c50989   Benjamin Renard   Support Tab2D par...
611
					);
76792c0e   Benjamin Renard   Sum parameters un...
612
					if ($paramData->{'dim2-sum-type'} == 2)
eb0ae558   Hacene SI HADJ MOHAND   integration ok
613
						$paramInfo = $this->addExistingParam('table_indexes', $paramsData, $template_args);
54219b37   Benjamin Renard   Add support for s...
614
					else
eb0ae558   Hacene SI HADJ MOHAND   integration ok
615
						$paramInfo = $this->addExistingParam('table_range', $paramsData, $template_args);
93c50989   Benjamin Renard   Support Tab2D par...
616
617
618
619
620
621
622
623
624
625
626
				}
				else if ($dim1_index != '*') {
					$paramInfo['indexes'][] = $dim1_index;
				}
				else if ($dim2_index != '*') {
					$paramInfo['indexes'][] = $dim2_index;
				}
				break;
			case 2:
				//Tab2D
				if ($dim1_is_range && $dim2_is_range) {
76792c0e   Benjamin Renard   Sum parameters un...
627
					if ($paramData->{'dim1-sum-type'} != $paramData->{'dim2-sum-type'}) {
54219b37   Benjamin Renard   Add support for s...
628
629
						throw new Exception("Not supported - Dimensions ranges for ".$paramInfo['id']." must have the same type for each dimensions");
					}
93c50989   Benjamin Renard   Support Tab2D par...
630
631
632
633
					$template_args = array(
						'paramid' => $paramInfo['id'],
						'min1' => $dim1_min,
						'max1' => $dim1_max,
eb0ae558   Hacene SI HADJ MOHAND   integration ok
634
                                                'processType1' => $processType1,
93c50989   Benjamin Renard   Support Tab2D par...
635
636
637
						'relateddim1' => 0,
						'min2' => $dim2_min,
						'max2' => $dim2_max,
eb0ae558   Hacene SI HADJ MOHAND   integration ok
638
                                                'processType2' => $processType2,
93c50989   Benjamin Renard   Support Tab2D par...
639
					);
76792c0e   Benjamin Renard   Sum parameters un...
640
					if ($paramData->{'dim1-sum-type'} == 2)
eb0ae558   Hacene SI HADJ MOHAND   integration ok
641
						$paramInfo = $this->addExistingParam('table_indexes_2d', $paramsData, $template_args);
54219b37   Benjamin Renard   Add support for s...
642
					else
eb0ae558   Hacene SI HADJ MOHAND   integration ok
643
						$paramInfo = $this->addExistingParam('table_range_2d', $paramsData, $template_args);
93c50989   Benjamin Renard   Support Tab2D par...
644
645
646
647
648
649
650
				}
				else if ($dim1_is_range) {
					$template_args = array(
						'paramid' => $paramInfo['id'],
						'min' => $dim1_min,
						'max' => $dim1_max,
						'relateddim' => 0,
eb0ae558   Hacene SI HADJ MOHAND   integration ok
651
                                                'processType' => $processType1,
93c50989   Benjamin Renard   Support Tab2D par...
652
					);
52859c27   Benjamin Renard   Give the possibil...
653
654
655
656
					$tableLink = array(
						'paramid' => $paramInfo['id'],
						'relateddim' => 1,
					);
76792c0e   Benjamin Renard   Sum parameters un...
657
					if ($paramData->{'dim1-sum-type'} == 2)
eb0ae558   Hacene SI HADJ MOHAND   integration ok
658
						$paramInfo = $this->addExistingParam('table_indexes', $paramsData, $template_args, $tableLink);
54219b37   Benjamin Renard   Add support for s...
659
					else
eb0ae558   Hacene SI HADJ MOHAND   integration ok
660
						$paramInfo = $this->addExistingParam('table_range', $paramsData, $template_args, $tableLink);
93c50989   Benjamin Renard   Support Tab2D par...
661
662
663
664
665
666
667
668
669
670
					if ($dim2_index != '*') {
						$paramInfo['indexes'][] = $dim2_index;
					}
				}
				else if ($dim2_is_range) {
					$template_args = array(
						'paramid' => $paramInfo['id'],
						'min' => $dim2_min,
						'max' => $dim2_max,
						'relateddim' => 1,
eb0ae558   Hacene SI HADJ MOHAND   integration ok
671
                                                'processType' => $processType2,
93c50989   Benjamin Renard   Support Tab2D par...
672
					);
52859c27   Benjamin Renard   Give the possibil...
673
674
675
676
					$tableLink = array(
						'paramid' => $paramInfo['id'],
						'relateddim' => 0,
					);
76792c0e   Benjamin Renard   Sum parameters un...
677
					if ($paramData->{'dim2-sum-type'} == 2)
eb0ae558   Hacene SI HADJ MOHAND   integration ok
678
						$paramInfo = $this->addExistingParam('table_indexes', $paramsData, $template_args, $tableLink);
54219b37   Benjamin Renard   Add support for s...
679
					else
eb0ae558   Hacene SI HADJ MOHAND   integration ok
680
						$paramInfo = $this->addExistingParam('table_range', $paramsData, $template_args, $tableLink);
93c50989   Benjamin Renard   Support Tab2D par...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
					if ($dim1_index != '*') {
						$paramInfo['indexes'][] = $dim1_index;
					}
				}
				else if (($dim1_index != '*') && ($dim2_index != '*')) {
					$paramInfo['indexes'][] = "[".$dim1_index.",".$dim2_index."]";
				}
				else if ($dim1_index != '*') {
					$paramInfo['indexes'][] = "[".$dim1_index.",*]";
				}
				else if ($dim2_index != '*') {
					$paramInfo['indexes'][] = "[*,".$dim2_index."]";
				}
				else if ($force_total_2d) {
					//total over 2 dims
					$template_args = array(
						'paramid' => $paramInfo['id'],
					);
					$paramInfo = $this->addExistingParam('total_2D', $paramsData, $template_args);
				}
				break;
		}
	}
a928f9d3   Benjamin Renard   New parser integr...
704
705

	public function parseExpression($expression, $workingDir) {
f03c62e3   Benjamin Renard   Keep in cache res...
706
707
708
709
		$outputFileName = "parser_".md5($expression).".xml";
		if (array_key_exists($outputFileName, $this->cacheExpressionParser)) {
			return $this->cacheExpressionParser[$outputFileName];
		}
a928f9d3   Benjamin Renard   New parser integr...
710
711
712
713
714
715
716
		$parserData = new ParserRequestDataClass();
		$parserData->setManagerFilePath(IHMConfigClass::getProcessManagerFilePath());
		$parserData->addExpression($expression);
		$parserData->setWorkingPath($workingDir);
		$parserData->setLocalParamsPath(IHMConfigClass::getLocalParamDBPath());
		$parserData->setConstantsFilePath(IHMConfigClass::getConstantsFilePath());
		$parserData->setFunctionsFilePath(IHMConfigClass::getFunctionsFilePath());
65912bc8   Benjamin Renard   Templated paramet...
717
		$parserData->setParamTemplatesFilePath(IHMConfigClass::getParamTemplateListFilePath());
8cf39be4   Benjamin Renard   Add functions arg...
718
		$parserData->setFunctionsArgsListFilePath(IHMConfigClass::getFunctionsArgsListFilePath());
f03c62e3   Benjamin Renard   Keep in cache res...
719
		$parserData->setOutputFileName($outputFileName);
a928f9d3   Benjamin Renard   New parser integr...
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
		$resultFilePath = $workingDir."/".$outputFileName;

		$parserRequest = new ParserRequestClass();
		$parserRequest->setData($parserData);

		if (!$parserRequest->init() || !$parserRequest->run()) {
			if (file_exists($resultFilePath)) {
				unlink($resultFilePath);
			}
			return array('success' => FALSE, 'message' => 'Error to init or run expression parser');
		}

		//Load result file
		$doc = new DOMDocument();
		$doc->load($resultFilePath);
		if (!$doc->load($resultFilePath)) {
			if (file_exists($resultFilePath)) {
				unlink($resultFilePath);
			}
			return array('success' => FALSE, 'message' => 'Error to load parser result file');
		}

		$expressionNodes = $doc->getElementsByTagName('expression');
		if ($expressionNodes->length == 0) {
			unlink($resultFilePath);
			return array('success' => FALSE, 'message' => 'Cannot get expression nodes in parser result file');
		}

		$crtExpressionNode = NULL;
		foreach ($expressionNodes as $expressionNode) {
			$ihmExpressionNodes = $expressionNode->getElementsByTagName('ihm');
			if ($ihmExpressionNodes->length == 0) {
				continue;
			}
			if ($ihmExpressionNodes->item(0)->nodeValue == $expression) {
				$crtExpressionNode = $expressionNode;
				break;
			}
		}
		
		if (!isset($crtExpressionNode)) {
			unlink($resultFilePath);
			return array('success' => FALSE, 'message' => 'Cannot retrieve expression in parser result file');
		}

		if ($crtExpressionNode->getAttribute('success') != "true") {
			unlink($resultFilePath);
			return array('success' => FALSE, 'message' => 'Synthax error in expression '.$expression);
		}

		$kernelExpressionNodes = $crtExpressionNode->getElementsByTagName('kernel');
		if ($kernelExpressionNodes->length == 0) {
			unlink($resultFilePath);
			return array('success' => FALSE, 'message' => 'Cannot retrieve kernel expression in parser result file');
		}

		$kernelExpression = $kernelExpressionNodes->item(0)->nodeValue;

		if (empty($kernelExpression)) {
			unlink($resultFilePath);
			return array('success' => FALSE, 'message' => 'Parser return an empty expression');
		}

		$params = array();
		$paramsNodes = $crtExpressionNode->getElementsByTagName('params');
		if ($paramsNodes->length != 0) {
			$paramNodes = $paramsNodes->item(0)->getElementsByTagName('param');
			if ($paramNodes->length != 0) {
				foreach ($paramNodes as $paramNode) {
					if (!empty($paramNode->nodeValue)) {
						$params[] = array(
							'paramid' => $paramNode->nodeValue
						);
					}
				}
			}
		}

		unlink($resultFilePath);
		
		if (empty($params)) {
			return array('success' => FALSE, 'message' => 'Expression should contain at least one parameter');
		}

7fc2b4b7   Benjamin Renard   Fix templated par...
804
805
806
807
808
809
810
		foreach ($params as &$param) {
			$templated_param = $this->templateParamsManager->parseTemplatedParam($param['paramid']);
			if ($templated_param !== FALSE) {
				$param = $templated_param;
			}
		}

f03c62e3   Benjamin Renard   Keep in cache res...
811
812
		$this->cacheExpressionParser[$outputFileName] = array('success' => TRUE, 'expression' => $kernelExpression, 'params' => $params);
		return $this->cacheExpressionParser[$outputFileName];
a928f9d3   Benjamin Renard   New parser integr...
813
	}
c3e6d894   Benjamin Renard   Init IHMParamTemp...
814
815
816
817

	public function getTemplateParamsManager() {
		return $this->templateParamsManager;
	}
22521f1c   Benjamin Renard   First commit
818
}