Blame view

src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php 11.4 KB
bf27ba04   Benjamin Renard   Add templated par...
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @class IHMParamTemplateClass
 * @brief Class used to manage templated parameters
 * @details
 */
class IHMParamTemplateClass
{
	protected $paramTemplateList = null;
ffc5cb81   Elena.Budnik   temporary commit
11
	protected $paramTemplateListFilePath;
bf27ba04   Benjamin Renard   Add templated par...
12
	
ffc5cb81   Elena.Budnik   temporary commit
13
14
15
16
17
18
19
20
	protected static $paramTemplateNode                    = 'paramTemplate';
	protected static $paramTemplateIdAtt                   = 'paramId';
	protected static $paramTemplateFileNameAtt             = 'fileName';
	protected static $paramTemplateArgumentsNode           = 'arguments';
	protected static $paramTemplateArgumentNode            = 'argument';
	protected static $paramTemplateArgumentKeyAtt          = 'key';
	protected static $paramTemplateArgumentNameAtt         = 'name';
	protected static $paramTemplateArgumentTypeAtt         = 'type';
a7011f4d   Benjamin Renard   Generated list fo...
21
22
23
	protected static $paramTemplateArgumentMinKeyAtt       = 'minkey';
	protected static $paramTemplateArgumentMaxKeyAtt       = 'maxkey';
	protected static $paramTemplateArgumentNameTplAtt      = 'nametpl';
ffc5cb81   Elena.Budnik   temporary commit
24
25
26
27
	protected static $paramTemplateArgumentDefaultAtt      = 'default';
	protected static $paramTemplateArgumentListItemNode    = 'item';
	protected static $paramTemplateArgumentListItemKeyAtt  = 'key';
	protected static $paramTemplateArgumentListItemNameAtt = 'name';
bf27ba04   Benjamin Renard   Add templated par...
28
29
30
31
32
33
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
ffc5cb81   Elena.Budnik   temporary commit
34
		$this->paramTemplateListFilePath = IHMConfigClass::getParamTemplateListFilePath();
bf27ba04   Benjamin Renard   Add templated par...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	}
	
	/*
	 * @brief Get list of templated parameters
	 */
	public function getParamTemplates() {
		$this->loadUserParamManagerFile();
		return $this->paramTemplateList;
	}
	
	/*
	 * @brief Check if param_id is a templated parameter
	*/
	public function isTemplatedParam($param_id) {
		$list = $this->getParamTemplates();
		return array_key_exists($param_id, $list);
	}
	
	/*
e4545ed5   Benjamin Renard   Implement templat...
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
	 * 
	 */
	public function parseTemplatedParam($param_expression) {
		$exploded_param_expression = explode('_',$param_expression);
		
		$tmp_paramid = "";
		$real_param_id = "";
		
		foreach ($exploded_param_expression as $expression_part) {
			//add one by one each part of the expression to check if it's a templated parameter
			if ($tmp_paramid != "")
				$tmp_paramid .= "_";
			$tmp_paramid .= $expression_part;
			if ($this->isTemplatedParam($tmp_paramid)) {
				$real_param_id = $tmp_paramid;
				break;
			}
		}
		
		if (empty($real_param_id))
			return FALSE;
		
		$template_args_str = str_replace($real_param_id, "", $param_expression);
		
		if ($template_args_str == "")
			//A templated param need at least one argument
			return FALSE;
		
		$template_args_info = $this->paramTemplateList[$real_param_id];
		
		$args_format = "";
		foreach ($template_args_info["arguments"] as $arg) {
			$args_format .= "_";
			switch($arg["type"]) {
				case 'float' :
				case 'double' :
					$args_format .= "%f";
					break;
				case 'int' :
c76ef7b2   Benjamin Renard   Add boolean argum...
93
				case 'bool' :
e4545ed5   Benjamin Renard   Implement templat...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
					$args_format .= "%d";
					break;
				default:
					$args_format .= "%s";
			}
		}
		
		$exploded_args = sscanf($template_args_str , $args_format);
		
		if ($exploded_args === -1)
			return FALSE;
		
		$template_args = array();
		$i = 0;
		foreach ($template_args_info["arguments"] as $arg_key => $arg) {
8ef738e8   Benjamin Renard   Fix templated par...
109
110
111
112
113
			if ($arg["type"] == "list") {
				if (!array_key_exists($exploded_args[$i], $arg['items'])) {
					return FALSE;
				}
			}
e4545ed5   Benjamin Renard   Implement templat...
114
115
116
117
118
119
120
121
122
123
124
125
			$template_args[$arg_key] = $exploded_args[$i];
			++$i;
		}
		
		return array(
			"paramid"       => $real_param_id,
			"fullparamid" => $param_expression,
			"template_args" => $template_args
		);
	}
	
	/*
bf27ba04   Benjamin Renard   Add templated par...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	 * @brief Get templated parameter id
	*/
	public function getTemplatedParamId($param_id, $template_args) {
		$templatePath = $this->getTemplatePath($param_id);
		
		if (empty($templatePath))
			return "";
		
		if (!isset($template_args))
			$template_args = array();
		
		//Enrich template args with default values
		$this->addDefaultValues($param_id, $template_args);
		
		return basename($this->replaceArgs($templatePath, $template_args), ".xml");
	}
	
	/*
	 * @brief Generate parameter file from template
	 */
52859c27   Benjamin Renard   Give the possibil...
146
	public function generateTemplatedParamFile($param_id, $template_args, $table_link = NULL) {
bf27ba04   Benjamin Renard   Add templated par...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		$templatePath = $this->getTemplatePath($param_id);
		if (empty($templatePath))
			return "";
		
		if (!isset($template_args))
			$template_args = array();
		
		//Enrich template args with default values
		$this->addDefaultValues($param_id, $template_args);
		
		$dst_param_id = $this->getTemplatedParamId($param_id, $template_args);
			
		//Build destination file path
		$dstDir = IHMConfigClass::getTemplateParamGeneratePath();
		if (!is_dir($dstDir))
			mkdir($dstDir);		
		$dstFilePath = $dstDir."/".$dst_param_id.".xml";
		
		//Check if it's necessary to generate the parameter file
		if (file_exists($dstFilePath)) {
			$dateModifDst = filemtime($dstFilePath);
			$dateModifTemplate = filemtime($templatePath);
d2a2763f   Benjamin Renard   Fix test used to ...
169
			if ($dateModifTemplate == $dateModifDst)
bf27ba04   Benjamin Renard   Add templated par...
170
171
172
173
174
175
176
177
178
179
				//no modification - reuse old generated file
				return $dstFilePath;
		}
		
		//Generate file
		$templateHandle = fopen($templatePath, "r");
		$dstHandle = fopen($dstFilePath, "w");
		if (!$templateHandle || !$dstHandle)
			return "";
		while (($line = fgets($templateHandle)) !== false) {
6f98ec26   Benjamin Renard   Write argument it...
180
			fwrite($dstHandle, $this->replaceArgs($line, $template_args, $this->getArguments($param_id)));
bf27ba04   Benjamin Renard   Add templated par...
181
182
183
		}
		fclose($templateHandle);
		fclose($dstHandle);
52859c27   Benjamin Renard   Give the possibil...
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

		if (isset($table_link)) {
			//Inject table link
			$xml = new DOMDocument();
			$xml->preserveWhiteSpace = TRUE;
			$xml->load($dstFilePath);

			$infoNodes = $xml->documentElement->getElementsByTagName('info');
			if ($infoNodes->length == 0) {
				$infoNode = $xml->createElement('info');
				$infoNode = $xml->documentElement->insertBefore($infoNode, $xml->documentElement->firstChild);
			}
			else {
				$infoNode = $infoNodes->item(0);
			}
		
			$tableNodes = $infoNode->getElementsByTagName('table');
			if ($tableNodes->length == 0) {
				$tableNode = $xml->createElement('table');
				$tableNode = $infoNode->appendChild($tableNode);
			}
			else {
				$tableNode = $tableNodes->item(0);
			}

			$linkTableNode = $xml->createElement('linkTable');
			$linkTableNode->setAttribute('originParamId', $table_link['paramid']);
			$linkTableNode->setAttribute('originTableDim', $table_link['relateddim']);
			$tableNode->appendChild($linkTableNode);

			$xml->save($dstFilePath);
		}
bf27ba04   Benjamin Renard   Add templated par...
216
217
218
219
220
221
222
223
224
225
		
		$dateModifTemplate = filemtime($templatePath);
		touch($dstFilePath, $dateModifTemplate);
		
		return $dstFilePath;
	}
	
	/*
	 * @brief Get template file path
	 */
4fe90834   Benjamin Renard   Fix bug with get ...
226
	public function getTemplatePath($param_id) {
bf27ba04   Benjamin Renard   Add templated par...
227
228
229
230
		if (!$this->isTemplatedParam($param_id))
			return "";
		return IHMConfigClass::getParamTemplateFilePath($this->paramTemplateList[$param_id]['fileName']);
	}
7911f4bc   Benjamin Renard   Another bug with ...
231
232
233
234
235
236
237
238
239
240
241
242
243

	/*
	 *  @brief Get template id by template file name
	 */
	public function getTemplateId($template_name) {
		$list = $this->getParamTemplates();
		foreach ($list as $param_id => $param_info) {
			if ($param_info['fileName'] == $template_name) {
				return $param_id;
			}
		}
		return FALSE;
	}
bf27ba04   Benjamin Renard   Add templated par...
244
245
	
	/*
4fe90834   Benjamin Renard   Fix bug with get ...
246
247
248
249
250
251
252
253
254
	 * 
	 */
	public function getTemplateFileName($param_id) {
		if (!$this->isTemplatedParam($param_id))
			return "";
		return $this->paramTemplateList[$param_id]['fileName'];
	}
	
	/*
bf27ba04   Benjamin Renard   Add templated par...
255
256
	 * @brief Replace args in string
	*/
6f98ec26   Benjamin Renard   Write argument it...
257
	public function replaceArgs($string, $template_args, $arguments = array()) {
bf27ba04   Benjamin Renard   Add templated par...
258
		$result = $string;
6f98ec26   Benjamin Renard   Write argument it...
259
260
261
		if (empty($template_args)) {
			return $result;
		}
bf27ba04   Benjamin Renard   Add templated par...
262
263
		foreach ($template_args as $template_arg_key => $template_arg_value) {
			$result = str_replace("##".$template_arg_key."##", $template_arg_value, $result);
6f98ec26   Benjamin Renard   Write argument it...
264
265
266
267
			if (array_key_exists($template_arg_key, $arguments) && ($arguments[$template_arg_key]['type'] == 'list')) {
				$item_name = array_key_exists($template_arg_value, $arguments[$template_arg_key]['items']) ? $arguments[$template_arg_key]['items'][$template_arg_value] : 'Unknown';
				$result = str_replace("@@".$template_arg_key."@@", $item_name, $result);
			}
bf27ba04   Benjamin Renard   Add templated par...
268
269
270
271
272
273
274
275
		}
		return $result;
	}
	
	/*
	 * @brief Enrich Template args with default values
	 */
	private function addDefaultValues($param_id, &$template_args) {
6f98ec26   Benjamin Renard   Write argument it...
276
277
		$arguments = $this->getArguments($param_id);
		if (empty($arguments))
bf27ba04   Benjamin Renard   Add templated par...
278
			return;
bf27ba04   Benjamin Renard   Add templated par...
279
280
		foreach ($arguments as $arg_key => $arg_def) {
			if (!array_key_exists($arg_key, $template_args))
d2a2763f   Benjamin Renard   Fix test used to ...
281
				$template_args[$arg_key] = $arg_def['default'];
bf27ba04   Benjamin Renard   Add templated par...
282
283
		}
	}
6f98ec26   Benjamin Renard   Write argument it...
284
285
286
287
288
289
290
291
292
293
294

	/*
	 * @brief Get list of arguments for a given parameter
	 */
	protected function getArguments($param_id) {
		$list = $this->getParamTemplates();

		if (!array_key_exists($param_id, $list) || !isset($list[$param_id]['arguments']))
			return array();
		return $list[$param_id]['arguments'];
	}
bf27ba04   Benjamin Renard   Add templated par...
295
296
297
298
	
	/*
	 * @brief Load list of templated parameters
	*/
ffc5cb81   Elena.Budnik   temporary commit
299
	protected function loadUserParamManagerFile()
bf27ba04   Benjamin Renard   Add templated par...
300
301
302
303
304
	{
		if (isset($this->paramTemplateList))
			return;
		
		$this->paramTemplateList = array();
ffc5cb81   Elena.Budnik   temporary commit
305
	 
bf27ba04   Benjamin Renard   Add templated par...
306
307
		//load xml file
		$dom = new DomDocument("1.0");
ffc5cb81   Elena.Budnik   temporary commit
308
		if (!$dom->load($this->paramTemplateListFilePath))
bf27ba04   Benjamin Renard   Add templated par...
309
			return;
ffc5cb81   Elena.Budnik   temporary commit
310
			
bf27ba04   Benjamin Renard   Add templated par...
311
		$paramTemplateNodes = $dom->getElementsByTagName(self::$paramTemplateNode);
ffc5cb81   Elena.Budnik   temporary commit
312

bf27ba04   Benjamin Renard   Add templated par...
313
314
315
316
317
318
		foreach ($paramTemplateNodes as $paramTemplateNode) {
			$paramId       = $paramTemplateNode->getAttribute(self::$paramTemplateIdAtt);
			$paramFileName = $paramTemplateNode->getAttribute(self::$paramTemplateFileNameAtt);
			
			if (empty($paramId) || empty($paramFileName))
				continue;
ffc5cb81   Elena.Budnik   temporary commit
319
				
bf27ba04   Benjamin Renard   Add templated par...
320
321
322
323
324
325
326
327
328
329
330
331
332
			$arguments = array();
			
			$argumentsNode = $paramTemplateNode->getElementsByTagName(self::$paramTemplateArgumentsNode);
			
			if (count($argumentsNode) > 0) {
				$argumentsNode = $argumentsNode->item(0);
				$argumentNodes = $argumentsNode->getElementsByTagName(self::$paramTemplateArgumentNode);
				
				foreach($argumentNodes as $argumentNode) {
					$argumentKey  = $argumentNode->getAttribute(self::$paramTemplateArgumentKeyAtt);
					$argumentName = $argumentNode->getAttribute(self::$paramTemplateArgumentNameAtt);
					$argumentType = $argumentNode->getAttribute(self::$paramTemplateArgumentTypeAtt);
					$argumentDefault = $argumentNode->getAttribute(self::$paramTemplateArgumentDefaultAtt);
a7011f4d   Benjamin Renard   Generated list fo...
333
334
335
					$argumentMinKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMinKeyAtt);
					$argumentMaxKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMaxKeyAtt);
					$argumentNameTpl = $argumentNode->getAttribute(self::$paramTemplateArgumentNameTplAtt);
bf27ba04   Benjamin Renard   Add templated par...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
					
					if (empty($argumentKey) || empty($argumentType))
						continue;
					
					$arguments[$argumentKey] = array(
						"name" => (empty($argumentName) ? $argumentKey : $argumentName),
						"type" => $argumentType,
						"default" => $argumentDefault
					);
					
					switch ($argumentType) {
						case "list" :
							$arguments[$argumentKey]['items'] = array();
							
							$itemNodes = $argumentNode->getElementsByTagName(self::$paramTemplateArgumentListItemNode);
							foreach ($itemNodes as $itemNode) {
								$itemKey  = $itemNode->getAttribute(self::$paramTemplateArgumentListItemKeyAtt);
								$itemName = $itemNode->getAttribute(self::$paramTemplateArgumentListItemNameAtt);
								if ($itemKey == "")
									continue;
								$arguments[$argumentKey]['items'][$itemKey] = (empty($itemName) ? $itemKey : $itemName);
							}
a7011f4d   Benjamin Renard   Generated list fo...
358
359
360
361
362
363
364
365
							break;
						case "generated-list":
							$arguments[$argumentKey]['type'] = 'list';
							$arguments[$argumentKey]['items'] = array();
							for ($key = intval($argumentMinKey) ; $key <= intval($argumentMaxKey); ++$key) {
								$itemName = str_replace('##key##',$key,$argumentNameTpl);
								$arguments[$argumentKey]['items'][strval($key)] = $itemName;
							}
bf27ba04   Benjamin Renard   Add templated par...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
							break;
						default:
							//Nothing to do
					}
				}
			}
			
			$this->paramTemplateList[$paramId] = array(
				"fileName"  => $paramFileName,
				"arguments" => $arguments
			);
			
		}
	}
a7011f4d   Benjamin Renard   Generated list fo...
380
}