Blame view

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

/**
 * @class IHMParamManagerClass
 * @brief Parameter manager
 * @details
 */
class IHMParamManagerClass
{
	protected $userParameterLoader = null;
	protected $expressionParser = null;
bf27ba04   Benjamin Renard   Add templated par...
12
	protected $templateParamsManager = null;
ffc5cb81   Elena.Budnik   temporary commit
13
	protected $paramImpexMgr     = null;
22521f1c   Benjamin Renard   First commit
14
15
16
17
18
19
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
bf27ba04   Benjamin Renard   Add templated par...
20
		$this->templateParamsManager = new IHMParamTemplateClass();
22521f1c   Benjamin Renard   First commit
21
22
23
24
25
	}

	/*
	 * @brief Add an existing parameter
	*/
bf27ba04   Benjamin Renard   Add templated par...
26
	public function addExistingParam($param, $paramsData, $templateArgs = NULL)
ffc5cb81   Elena.Budnik   temporary commit
27
	{		
22521f1c   Benjamin Renard   First commit
28
29
30
31
		if ($this->isDerivedParam($param))
			return $this->addDerivedParam($param,$paramsData);
		else if ($this->isUploadedParam($param))
			return $this->addUploadedParam($param,$paramsData);
ffc5cb81   Elena.Budnik   temporary commit
32
33
		else if ($this->isImpexParam($param))
			return $this->addImpexParam($param,$paramsData,$templateArgs);	
22521f1c   Benjamin Renard   First commit
34
		else
ffc5cb81   Elena.Budnik   temporary commit
35
			return $this->addLocalParam($param,$paramsData,$templateArgs);
22521f1c   Benjamin Renard   First commit
36
37
38
39
40
41
		return "";
	}

	/*
	 * @brief Add a process parameter
	*/
c0535e83   Benjamin Renard   Use units and yTi...
42
43
44
	public function addProcessParam($paramId,$expression,$expression_info,$params,$sampling,$gap,$dateModif,$units,$ytitle,$paramsData)
	{
		$paramsData->addProcessParamToCreate($paramId, $expression, $expression_info, $params, $sampling, $gap,$dateModif, $units, $ytitle); 
ffc5cb81   Elena.Budnik   temporary commit
45
	 
e4545ed5   Benjamin Renard   Implement templat...
46
		foreach ($params as $param) {
ffc5cb81   Elena.Budnik   temporary commit
47
48
			$template_args = NULL; 
			 
e4545ed5   Benjamin Renard   Implement templat...
49
50
			if (array_key_exists("template_args", $param))
				$template_args = $param["template_args"];
ffc5cb81   Elena.Budnik   temporary commit
51
	 		 
e4545ed5   Benjamin Renard   Implement templat...
52
53
			$this->addExistingParam($param["paramid"],$paramsData,$template_args);
		}
22521f1c   Benjamin Renard   First commit
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

		return true;
	}

	/*
	 * @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
	*/
	private function isUploadedParam($param)
	{
		return preg_match("#^wsd_#",$param);
	}
ffc5cb81   Elena.Budnik   temporary commit
73
74
75
76
77
78
79
80
	
	/*
	 * @brief Detect if it's IMPEX parameter
	*/
	private function isImpexParam($param)
	{
		return preg_match("#^".IHMImpexParamClass::$paramPrefix."#",$param);
	}
22521f1c   Benjamin Renard   First commit
81
82
83
84

	/*
	 * @brief Add a local parameter
	*/
bf27ba04   Benjamin Renard   Add templated par...
85
	private function addLocalParam($param, $paramsData, $templateArgs)
22521f1c   Benjamin Renard   First commit
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	{
		//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
100
	 
bf27ba04   Benjamin Renard   Add templated par...
101
102
103
104
105
		//check templated parameter
		$real_param_id = $paramId;
		if ($this->templateParamsManager->isTemplatedParam($paramId)) {
			$paramPath = $this->templateParamsManager->generateTemplatedParamFile($paramId, $templateArgs);
			$real_param_id = $this->templateParamsManager->getTemplatedParamId($paramId, $templateArgs);
ffc5cb81   Elena.Budnik   temporary commit
106
			 
bf27ba04   Benjamin Renard   Add templated par...
107
108
109
110
111
112
113
114
			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
115

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

bf27ba04   Benjamin Renard   Add templated par...
118
		$this->addLinkedLocalParams($paramId, $paramsData, $templateArgs);
22521f1c   Benjamin Renard   First commit
119
			
bf27ba04   Benjamin Renard   Add templated par...
120
		return array("id" => $real_param_id, "indexes" => $indexes, "calib_infos" => $calib_infos);
22521f1c   Benjamin Renard   First commit
121
122
123
124
125
	}

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

cd3326be   Benjamin Renard   Fix function used...
132
		$real_param_id = $paramId;
bf27ba04   Benjamin Renard   Add templated par...
133
		if ($this->templateParamsManager->isTemplatedParam($paramId)) {
cd3326be   Benjamin Renard   Fix function used...
134
			$paramPath = $this->templateParamsManager->getTemplatePath($paramId);
8ade06fe   Benjamin Renard   Improve config load
135
			if (empty($paramPath) || !@$doc->load($paramPath))
f1252b32   Benjamin Renard   Add template for ...
136
				throw new Exception('Cannot find parameter template file '.$paramId);
bf27ba04   Benjamin Renard   Add templated par...
137
138
139
		}
		else {
			$paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";
8ade06fe   Benjamin Renard   Improve config load
140
			if (empty($paramPath) || !@$doc->load($paramPath))
f1252b32   Benjamin Renard   Add template for ...
141
				throw new Exception('Cannot find parameter local file '.$paramId);
bf27ba04   Benjamin Renard   Add templated par...
142
143
		}
			
22521f1c   Benjamin Renard   First commit
144
145
146
147
148
149
150
151
152
153
154
155
156
157
		//<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...
158
			if (empty($linkedParamId))
22521f1c   Benjamin Renard   First commit
159
				continue;
7911f4bc   Benjamin Renard   Another bug with ...
160
161
162
163
			$template_id = $this->templateParamsManager->getTemplateId($linkedParamId);
			if ($template_id !== FALSE) {
				$linkedParamId = $template_id;
			}
7d35612a   Benjamin Renard   Fix ParamManager ...
164
165
166
			else {
				$linkedParamId = $this->templateParamsManager->replaceArgs($linkedParamId, $templateArgs);
			}
7911f4bc   Benjamin Renard   Another bug with ...
167
168
169
170

			if ($this->templateParamsManager->isTemplatedParam($linkedParamId)) {
				$linkedParamPath = $this->templateParamsManager->generateTemplatedParamFile($linkedParamId, $templateArgs);
				$real_linked_param_id = $this->templateParamsManager->getTemplatedParamId($linkedParamId, $templateArgs);
cd3326be   Benjamin Renard   Fix function used...
171
172
			}
			else {
7911f4bc   Benjamin Renard   Another bug with ...
173
174
				$real_linked_param_id = $linkedParamId;
				$linkedParamPath = IHMConfigClass::getLocalParamDBPath().$real_linked_param_id.".xml";
bf27ba04   Benjamin Renard   Add templated par...
175
			}
7911f4bc   Benjamin Renard   Another bug with ...
176
177
			$paramsData->addParamToCopy($real_linked_param_id,$linkedParamPath);
			$this->addLinkedLocalParams($linkedParamId, $paramsData);
22521f1c   Benjamin Renard   First commit
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
		}
	}

	/*
	 * @brief Add derived parameter
	*/
	private function addDerivedParam($param,$paramsData)
	{
		if (!isset($this->userParameterLoader))
			$this->userParameterLoader = new IHMUserParamLoaderClass();
		
		//get derived parameter info
		$res = $this->userParameterLoader->getDerivedParameterFromName($param);
		
		if (!$res["success"])
			throw new Exception('Error to load derived parameter file : '.$res["message"]);

		//parse expression
		if (!isset($this->expressionParser))
952dd7c7   elena   catalog integration
197
			$this->expressionParser = new IHMExpressionParserClass();
22521f1c   Benjamin Renard   First commit
198
199
		$expressionInfo = $this->expressionParser->parse($res["param"]["expression"]);
		
952dd7c7   elena   catalog integration
200
201
202
		$paramId = $param;
		
		//create a process param for the derived parameter
e9165342   Benjamin Renard   Write data mining...
203
		$this->addProcessParam($paramId, $expressionInfo["expression"], $res["param"]["expression"],
952dd7c7   elena   catalog integration
204
				$expressionInfo['params'], $res["param"]["timestep"],
c0535e83   Benjamin Renard   Use units and yTi...
205
206
				0,$res["param"]["dateModif"],!empty($res["param"]["info"]["units"]) ? $res["param"]["info"]["units"] : "",
				!empty($res["param"]["info"]["yTitle"]) ? $res["param"]["info"]["yTitle"] : "", $paramsData);
22521f1c   Benjamin Renard   First commit
207
208
209
		
		return array("id" => $paramId, "indexes" => array(), "calib_infos" => array());
	}
ffc5cb81   Elena.Budnik   temporary commit
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
	
	/*
	 * @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"],
				$res["param"]["dateModif"],true);
				
577d5c04   Elena.Budnik   IMPEX integration
245
246
247
248
				$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
249
250
251
				$tableDef = $res["param"]["info"]["tableDef"];
				if (isset($tableDef) && array_key_exists('tableDefType', $tableDef) && ($tableDef['tableDefType'] != 'NONE'))
				{
577d5c04   Elena.Budnik   IMPEX integration
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
					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
284
285
286
287
288
289
290
291
292
293
294
295
296
				}
				
				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
297
298
299
300
301
	/*
	 * @brief Add uploaded parameter
	*/
	private function addUploadedParam($param,$paramsData)
	{
944199fe   Benjamin Renard   Use table definit...
302
303
304
305
306
307
308
309
310
311
312
313
		if (!isset($this->userParameterLoader))
			$this->userParameterLoader = new IHMUserParamLoaderClass();
		
		//get uploaded parameter info
		$res = $this->userParameterLoader->getUploadedParameterFromName($param);
		
		if (!$res["success"])
			throw new Exception('Error to load uploaded parameter file : '.$res["message"]);
		
		$newParamNode = $paramsData->addLocalParamToCreate($res["param"]["id"], 
				$res["param"]["info"]["viId"], $res["param"]["info"]["realVar"],
				$res["param"]["info"]["minSampling"], $res["param"]["info"]["maxSampling"],
e8ff4ba7   Elena.Budnik   integration of as...
314
				$res["param"]["info"]["type"],$res["param"]["info"]["size"],
944199fe   Benjamin Renard   Use table definit...
315
				$res["param"]["dateModif"]);
e8ff4ba7   Elena.Budnik   integration of as...
316

944199fe   Benjamin Renard   Use table definit...
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
		
		$tableDef = $res["param"]["info"]["tableDef"];
		if (isset($tableDef) && array_key_exists('tableDefType', $tableDef) && ($tableDef['tableDefType'] != 'NONE'))
		{
			switch ($tableDef['channelsDefType'])
			{
				case 'MINMAX' :
					$minMaxTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::MINMAX, $res["param"]["info"]["yTitle"]);
					$minMaxTable->setUnits($res["param"]["info"]["units"]);
					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'  :
					$boundTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::BOUNDS, $res["param"]["info"]["yTitle"]);
					$boundTable->setUnits($res["param"]["info"]["units"]);
					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' :
					$centerTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::CENTER, $res["param"]["info"]["yTitle"]);
					$centerTable->setUnits($res["param"]["info"]["units"]);
					$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' :
					$centerWidthTable = $newParamNode->getInfo()->addTable(InfoParamTableTypeEnum::CENTERWIDTH, $res["param"]["info"]["yTitle"]);
					$centerWidthTable->setUnits($res["param"]["info"]["units"]);
					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;
			}
		}
		
		return array("id" => $res["param"]["id"], "plotType" => $res["param"]["info"]["plotType"]);
22521f1c   Benjamin Renard   First commit
414
415
	}
}