Commit a928f9d35cdddcf9d8044004de10b8af350abaec
1 parent
7d33380d
Exists in
master
and in
54 other branches
New parser integration
Showing
8 changed files
with
255 additions
and
990 deletions
Show diff stats
src/InputOutput/IHMImpl/Params/DataMiningImpl/IHMInputOutputParamsDataMiningClass.php
... | ... | @@ -32,7 +32,11 @@ class IHMInputOutputParamsDataMiningClass extends IHMInputOutputParamsAbstractCl |
32 | 32 | $dataMiningNode->setFileFormat(RequestOutputDataMiningFileFormatEnum::XML); |
33 | 33 | |
34 | 34 | //parse expression |
35 | - $expressionInfo = $this->expressionParser->parse($input->expression); | |
35 | + $expressionInfo = $this->paramManager->parseExpression($input->expression, $this->paramsData->getWorkingPath()); | |
36 | + if (!$expressionInfo['success']) { | |
37 | + throw new Exception($expressionInfo['message']); | |
38 | + } | |
39 | + | |
36 | 40 | $paramId = "datamining_".md5($expressionInfo["expression"]); |
37 | 41 | |
38 | 42 | //create a derived param for the expression | ... | ... |
src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php
... | ... | @@ -8,7 +8,6 @@ |
8 | 8 | abstract class IHMInputOutputParamsAbstractClass implements InputOutputInterface |
9 | 9 | { |
10 | 10 | protected $paramManager = null; |
11 | - protected $expressionManager = null; | |
12 | 11 | protected $jobsManager = null; |
13 | 12 | |
14 | 13 | protected $paramsData = null; |
... | ... | @@ -23,7 +22,6 @@ abstract class IHMInputOutputParamsAbstractClass implements InputOutputInterface |
23 | 22 | function __construct() |
24 | 23 | { |
25 | 24 | $this->paramManager = new IHMParamManagerClass(); |
26 | - $this->expressionParser = new IHMExpressionParserClass(); | |
27 | 25 | $this->jobsManager = new IHMJobsManagerClass(); |
28 | 26 | } |
29 | 27 | ... | ... |
src/InputOutput/IHMImpl/Tools/IHMExpressionParserClass.php deleted
... | ... | @@ -1,975 +0,0 @@ |
1 | -<?php | |
2 | - | |
3 | -/** | |
4 | - * @class IHMExpressionParserClass | |
5 | - * @brief Parser for IHM expression | |
6 | - * @details | |
7 | - */ | |
8 | -class IHMExpressionParserClass | |
9 | -{ | |
10 | - //constants | |
11 | - private static $constantTag = '@'; | |
12 | - private static $constantNode = 'constant'; | |
13 | - private static $constantNameAtt = 'name'; | |
14 | - | |
15 | - //aliases | |
16 | - private static $aliasTag = '#'; | |
17 | - private static $aliasNode = 'alias'; | |
18 | - private static $aliasNameAtt = 'name'; | |
19 | - | |
20 | - //functions | |
21 | - private static $functionsNode = 'function'; | |
22 | - private static $functionsNameAtt = 'name'; | |
23 | - private static $functionsArgsAtt = 'args'; | |
24 | - private static $functionsParamsAtt = 'params'; | |
25 | - private static $functionsNewKernelNode = 'new_kernel'; | |
26 | - | |
27 | - //operator | |
28 | - private static $operators = array( | |
29 | - //standard operators | |
30 | - "+" => array("type" => "operator"), | |
31 | - "-" => array("type" => "operator"), | |
32 | - "*" => array("type" => "operator"), | |
33 | - "/" => array("type" => "operator"), | |
34 | - //operators that's need to be translate by a function for the kernel | |
35 | - "^" => array("type" => "function", "function" => "pow"), | |
36 | - "<" => array("type" => "function", "function" => "lower_than"), | |
37 | - ">" => array("type" => "function", "function" => "greater_than"), | |
38 | - "&" => array("type" => "function", "function" => "And"), | |
39 | - "|" => array("type" => "function", "function" => "Or") | |
40 | - ); | |
41 | - | |
42 | - private $constantsArray; | |
43 | - | |
44 | - private $aliasesArray; | |
45 | - | |
46 | - private $functionsArray; | |
47 | - | |
48 | - private $templatedParamMgr = NULL; | |
49 | - private $impexParamMgr = NULL; | |
50 | - | |
51 | - private $isTest = FALSE; | |
52 | - | |
53 | - /* | |
54 | - * @brief Constructor | |
55 | - */ | |
56 | - function __construct($isTest = FALSE) | |
57 | - { | |
58 | - $this->isTest = $isTest; | |
59 | - if (!$isTest) | |
60 | - $this->templatedParamMgr = new IHMParamTemplateClass(); | |
61 | - } | |
62 | - | |
63 | - /* | |
64 | - * @brief test function | |
65 | - */ | |
66 | - public function test() | |
67 | - { | |
68 | - //tests | |
69 | - $tests = array( | |
70 | - "dst>3" => array ("expression" => "greater_than(\$dst,3)", "params" => array(array("paramid" => "dst"))), | |
71 | - "@const_1<dst" => array("expression" => "lower_than(1.0E-10,\$dst)", "params" => array(array("paramid" =>"dst"))), | |
72 | - "dst^#alias_2" => array("expression" => "pow(\$dst,\$imf[1])", "params" => array(array("paramid" => "dst"),array("paramid" => "imf"))), | |
73 | - "dst^2+@const_2" => array("expression" => "pow(\$dst,2)+-2.8", "params" => array(array("paramid" => "dst"))), | |
74 | - "atan(imf(2)/imf(1))+speed/100.0" => array("expression" => "(atan(\$imf[2]/\$imf[1]))+\$speed/100.0", "params" => array(array("paramid" => "imf"), array("paramid" => "speed"))), | |
75 | - "shiftT_(clust1_hia_pad,60)" => array("expression" => "(#timeShift(\$clust1_hia_pad;60))", "params" => array(array("paramid" => "clust1_hia_pad"))), | |
76 | - "smooth_(density,1200)" => array("expression" => "(#boxcar(\$density;1200))", "params" => array(array("paramid" => "density"))), | |
77 | - "shiftT_(density,-600)" => array("expression" => "(#timeShift(\$density;-600))", "params" => array(array("paramid" => "density"))), | |
78 | - "deriv(density)" => array("expression" => "(#deriv(\$density))", "params" => array(array("paramid" => "density"))), | |
79 | - "abs(dst)" =>array("expression" => "(abs(\$dst))", "params" => array(array("paramid" => "dst"))), | |
80 | - "density*speed+(density^2-1.0)/speed" => array("expression" => "\$density*\$speed+(pow(\$density,2)-1.0)/\$speed", "params" => array(array("paramid" => "density"),array("paramid" => "speed"))), | |
81 | - "(-7<dst)&(dst<-3)" => array("expression" => "And((lower_than(-7,\$dst)),(lower_than(\$dst,-3)))", "params" => array(array("paramid" => "dst"))), | |
82 | - "cass_caps_elssec4(range[0,10])" => array("expression" => "\$sum_into_table_range_cass_caps_elssec4_0_0_10", "params" => array(array("paramid" => "sum_into_table_range", "fullparamid" => "sum_into_table_range_cass_caps_elssec4_0_0_10", "template_args" => array("paramid"=>"cass_caps_elssec4","relateddim"=>0,"min"=>"0","max"=>"10")))), | |
83 | - "abs(dst)>0" => array("expression" => "greater_than((abs(\$dst)),0)", "params" => array(array("paramid" => "dst"))), | |
84 | - "2/(1+dst)" => array("expression" => "2/(1+\$dst)", "params" => array(array("paramid" => "dst"))), | |
85 | - "2/abs(1+dst)" => array("expression" => "2/(abs(1+\$dst))", "params" => array(array("paramid" => "dst"))), | |
86 | - "cross(vect_1,vect_2)" => array("expression" => "(cross(\$vect_1,\$vect_2))", "params" => array(array("paramid" => "vect_1"), array("paramid" => "vect_2"))), | |
87 | - "cross(cross(vect_1,vect_2),vect_3)" => array("expression" => "(cross((cross(\$vect_1,\$vect_2)),\$vect_3))","params" => array(array("paramid" => "vect_1"), array("paramid" => "vect_2"), array("paramid" => "vect_3"))), | |
88 | - "c1_hia_pad(0,0)/c1_hia_pad(0,1)" => array ("expression" => "\$c1_hia_pad[0][0]/\$c1_hia_pad[0][1]", "params" => array(array("paramid" => "c1_hia_pad"))), | |
89 | - "cass_caps_elssec4(indexes[0,10])" => array("expression" => "\$sum_into_table_indexes_cass_caps_elssec4_0_0_10", "params" => array(array("paramid" => "sum_into_table_indexes", "fullparamid" => "sum_into_table_indexes_cass_caps_elssec4_0_0_10", "template_args" => array("paramid"=>"cass_caps_elssec4","relateddim"=>0,"min"=>"0","max"=>"10")))), | |
90 | - ); | |
91 | - | |
92 | - //init constants, aliases and functions for test | |
93 | - $this->constantsArray = array( | |
94 | - "@const_1" => 1e-10, | |
95 | - "@const_2" => -2.8 | |
96 | - ); | |
97 | - | |
98 | - $this->aliasesArray = array( | |
99 | - "#alias_1" => "dst", | |
100 | - "#alias_2" => "imf(1)" | |
101 | - ); | |
102 | - | |
103 | - $this->functionsArray = array( | |
104 | - "func_old" => array("kernel_name" => "func_new", "nb_args" => 0, "nb_params" => 1), | |
105 | - "atan" => array("kernel_name" => "atan", "nb_args" => 0, "nb_params" => 1), | |
106 | - "shiftT_" => array("kernel_name" => "#timeShift", "nb_args" => 1, "nb_params" => 1), | |
107 | - "smooth_" => array("kernel_name" => "#boxcar", "nb_args" => 1, "nb_params" => 1), | |
108 | - "deriv" => array("kernel_name" => "#deriv", "nb_args" => 0, "nb_params" => 1), | |
109 | - "abs" => array("kernel_name" => "abs", "nb_args" => 0, "nb_params" => 1), | |
110 | - "sqrt" => array("kernel_name" => "sqrt", "nb_args" => 0, "nb_params" => 1), | |
111 | - "cross" => array("kernel_name" => "cross", "nb_args" => 0, "nb_params" => 2), | |
112 | - ); | |
113 | - | |
114 | - //add operator to change in function | |
115 | - foreach (self::$operators as $operator) | |
116 | - { | |
117 | - if ($operator["type"] == "function") | |
118 | - $this->functionsArray[$operator["function"]] = | |
119 | - array("kernel_name" => $operator["function"], "nb_args" => 0); | |
120 | - } | |
121 | - | |
122 | - // | |
123 | - foreach ($tests as $key => $value) | |
124 | - { | |
125 | - echo "==> Test : ".$key.PHP_EOL; | |
126 | - | |
127 | - $res = $this->parse($key); | |
128 | - | |
129 | - echo "Result : ".$res["expression"].PHP_EOL; | |
130 | - | |
131 | - echo json_encode($value).PHP_EOL.json_encode($res).PHP_EOL; | |
132 | - | |
133 | - if (json_encode($value) == json_encode($res)) | |
134 | - echo "OK !".PHP_EOL; | |
135 | - else | |
136 | - echo "ERROR !".PHP_EOL; | |
137 | - } | |
138 | - } | |
139 | - | |
140 | - /* | |
141 | - * @brief main method to parse a IHM expression | |
142 | - */ | |
143 | - public function parse($expression) | |
144 | - { | |
145 | - //echo "Source expression : ".$expression.PHP_EOL; | |
146 | - //clean expression and replace constants and aliases by associated value | |
147 | - $this->clean($expression); | |
148 | - $this->replaceConstants($expression); | |
149 | - $this->replaceAliases($expression); | |
150 | - $this->clean($expression); | |
151 | - | |
152 | - //explode expression | |
153 | - $elements = $this->explodeExpression($expression); | |
154 | - | |
155 | - //group elements in tree | |
156 | - $tree = $this->buildTreeElements($elements); | |
157 | - | |
158 | - //translate | |
159 | - $params_full = array(); | |
160 | - $translated = $this->translate($tree,$params_full); | |
161 | - | |
162 | - //keep only params id | |
163 | - $params = array(); | |
164 | - foreach ($params_full as $param_full) { | |
165 | - if (array_key_exists("template_args", $param_full)) { | |
166 | - $params[] = array( | |
167 | - "paramid" => $param_full['id'], | |
168 | - "fullparamid" => $param_full['fullparamid'], | |
169 | - "template_args" => $param_full['template_args'] | |
170 | - ); | |
171 | - } | |
172 | - else if (!$this->isTest && (($templated_param_info = $this->templatedParamMgr->parseTemplatedParam($param_full["id"])) !== FALSE)) { | |
173 | - $params[] = $templated_param_info; | |
174 | - } | |
175 | - else if ($this->isImpexParam($param_full["id"])) | |
176 | - { | |
177 | - if (!$this->impexParamMgr) | |
178 | - { | |
179 | - $this->impexParamMgr = new IHMImpexParamClass(); | |
180 | - } | |
181 | - $templated_param_info = $this->impexParamMgr->parseImpexParam($param_full["id"]); | |
182 | - $params[] = $templated_param_info; | |
183 | - } | |
184 | - else | |
185 | - $params[] = array("paramid" => $param_full["id"]); | |
186 | - } | |
187 | - | |
188 | - return array("expression" => $translated, "params" => $params); | |
189 | - } | |
190 | - | |
191 | - private function isImpexParam($paramID) | |
192 | - { | |
193 | - if (!$this->isTest) | |
194 | - return preg_match("#^".IHMImpexParamClass::$paramPrefix."#",$paramID); | |
195 | - return FALSE; | |
196 | - } | |
197 | - | |
198 | - /* | |
199 | - * @brief clean expression | |
200 | - */ | |
201 | - private function clean(&$expression) | |
202 | - { | |
203 | - //remove all " " | |
204 | - $expression = str_replace(" ", "", $expression); | |
205 | - | |
206 | - //replace [ and { by ( | |
207 | - $expression = str_replace(array("[","{"), "(", $expression); | |
208 | - | |
209 | - //replace ] and } by ) | |
210 | - $expression = str_replace(array("]","}"), ")", $expression); | |
211 | - } | |
212 | - | |
213 | - /* | |
214 | - * @brief detect if some constants are defined in the expression | |
215 | - */ | |
216 | - private function isConstantDetected($expression) | |
217 | - { | |
218 | - return preg_match('/'.self::$constantTag.'/', $expression); | |
219 | - } | |
220 | - | |
221 | - /* | |
222 | - * @brief replace constants by real values in expression | |
223 | - */ | |
224 | - private function replaceConstants(&$expression) | |
225 | - { | |
226 | - if (!$this->isConstantDetected($expression)) | |
227 | - return; | |
228 | - | |
229 | - if (!isset($this->constantsArray)) | |
230 | - { | |
231 | - //load constants array | |
232 | - $dom = new DomDocument("1.0"); | |
233 | - $constantsXMLFile = IHMConfigClass::getConstantsFilePath(); | |
234 | - if (!$dom->load($constantsXMLFile)) | |
235 | - throw new Exception('Cannot load constants file'); | |
236 | - | |
237 | - $this->constantsArray = array(); | |
238 | - | |
239 | - $constants_ = $dom->getElementsByTagName(self::$constantNode); | |
240 | - for ($i = 0; $i < $constants_->length; $i++) | |
241 | - $this->constantsArray[self::$constantTag.$constants_->item($i)->getAttribute(self::$constantNameAtt)] = $constants_->item($i)->nodeValue; | |
242 | - } | |
243 | - | |
244 | - //replace | |
245 | - $expression = strtr($expression, $this->constantsArray); | |
246 | - | |
247 | - //be sure that all constants are replaced | |
248 | - if ($this->isConstantDetected($expression)) | |
249 | - throw new Exception('Cannot replace some constants : '.$expression); | |
250 | - } | |
251 | - | |
252 | - /* | |
253 | - * @brief detect if at least one aliases is defined in the expression | |
254 | - */ | |
255 | - private function isAliasDetected($expression) | |
256 | - { | |
257 | - return preg_match('/'.self::$aliasTag.'/', $expression); | |
258 | - } | |
259 | - | |
260 | - /* | |
261 | - * @brief replace aliases by real parameter id in expression | |
262 | - */ | |
263 | - private function replaceAliases(&$expression) | |
264 | - { | |
265 | - if (!$this->isAliasDetected($expression)) | |
266 | - return; | |
267 | - | |
268 | - if (!isset($this->aliasesArray)) | |
269 | - { | |
270 | - //load aliases array | |
271 | - $dom = new DomDocument("1.0"); | |
272 | - $aliasesXMLFile = IHMConfigClass::getUserAliasesFilePath(); | |
273 | - if (!$dom->load($aliasesXMLFile)) | |
274 | - throw new Exception('Cannot load aliases file'); | |
275 | - | |
276 | - $this->aliasesArray = array(); | |
277 | - | |
278 | - $aliases_ = $dom->getElementsByTagName(self::$aliasNode); | |
279 | - for ($i = 1; $i < $aliases_->length; $i++) | |
280 | - $this->aliasesArray[self::$alias_tag.$aliases_->item($i)->getAttribute(self::$aliasNameAtt)] = $aliases_->item($i)->nodeValue; | |
281 | - } | |
282 | - | |
283 | - //replace | |
284 | - $expression = strtr($expression, $this->aliasesArray); | |
285 | - | |
286 | - //be sure that all aliases are replaced | |
287 | - if ($this->isAliasDetected($expression)) | |
288 | - throw new Exception('Cannot replace some aliases : '.$expression); | |
289 | - } | |
290 | - | |
291 | - /* | |
292 | - * @brief load list of available functions | |
293 | - */ | |
294 | - private function loadFunctions() | |
295 | - { | |
296 | - if (isset($this->functionsArray)) | |
297 | - return; | |
298 | - | |
299 | - $dom = new DomDocument("1.0"); | |
300 | - $functionsXMLFile = IHMConfigClass::getFunctionsFilePath(); | |
301 | - | |
302 | - if (!$dom->load($functionsXMLFile)) | |
303 | - throw new Exception('Cannot load functions file'); | |
304 | - | |
305 | - $this->functionsArray = array(); | |
306 | - | |
307 | - $functions_ = $dom->getElementsByTagName(self::$functionsNode); | |
308 | - | |
309 | - for ($i = 0; $i < $functions_->length; $i++) | |
310 | - { | |
311 | - $tempArr = explode('(', $functions_->item($i)->getAttribute(self::$functionsNameAtt)); | |
312 | - $nbArgs = $functions_->item($i)->getAttribute(self::$functionsArgsAtt); | |
313 | - $nbParams = $functions_->item($i)->getAttribute(self::$functionsParamsAtt); | |
314 | - $kernelFunctions_ = $functions_->item($i)->getElementsByTagName(self::$functionsNewKernelNode); | |
315 | - if ($kernelFunctions_->length == 0) | |
316 | - $kernelFunction = ""; | |
317 | - else | |
318 | - $kernelFunction = $kernelFunctions_->item(0)->nodeValue; | |
319 | - $this->functionsArray[$tempArr[0]] = array( | |
320 | - "kernel_name" => $kernelFunction, | |
321 | - "nb_args" => intval($nbArgs), | |
322 | - "nb_params" => intval($nbParams), | |
323 | - "isOperator" => false | |
324 | - ); | |
325 | - } | |
326 | - | |
327 | - //add operator to change in function | |
328 | - foreach (self::$operators as $operator) | |
329 | - { | |
330 | - if ($operator["type"] == "function") | |
331 | - $this->functionsArray[$operator["function"]] = array( | |
332 | - "kernel_name" => $operator["function"], | |
333 | - "nb_args" => 0, | |
334 | - "isOperator" => true | |
335 | - ); | |
336 | - } | |
337 | - } | |
338 | - | |
339 | - /* | |
340 | - * @brief detect if the element is a function | |
341 | - */ | |
342 | - private function isFunction($element) | |
343 | - { | |
344 | - $this->loadFunctions(); | |
345 | - return array_key_exists($element,$this->functionsArray); | |
346 | - } | |
347 | - | |
348 | - /* | |
349 | - * @brief detect if the element is "range" or "indexes" | |
350 | - */ | |
351 | - private function isRangeOrIndexes($element) | |
352 | - { | |
353 | - return (($element == "range") || ($element == "indexes")); | |
354 | - } | |
355 | - | |
356 | - /* | |
357 | - * @brief detect if the element is an operator | |
358 | - */ | |
359 | - private function isOperator($c) | |
360 | - { | |
361 | - foreach (self::$operators as $key => $value) | |
362 | - if ($key == $c) | |
363 | - return true; | |
364 | - } | |
365 | - | |
366 | - /* | |
367 | - * @brief get the function associated to an operator | |
368 | - */ | |
369 | - private function getOperatorAssociatedFunction($c) | |
370 | - { | |
371 | - foreach (self::$operators as $key => $value) | |
372 | - if ($key == $c) | |
373 | - if ($value["type"] == "function") | |
374 | - return $value["function"]; | |
375 | - return ""; | |
376 | - } | |
377 | - | |
378 | - /* | |
379 | - * @brief detect if the operator needed to be replace by a function | |
380 | - */ | |
381 | - private function isOperatorToFunction($c) | |
382 | - { | |
383 | - return ($this->getOperatorAssociatedFunction($c) != ""); | |
384 | - } | |
385 | - | |
386 | - /* | |
387 | - * @brief detect if the element is a separator | |
388 | - */ | |
389 | - private function isSeparator($c) | |
390 | - { | |
391 | - return ($this->isOperator($c) || $c == "(" || $c == ")" || $c == ","); | |
392 | - } | |
393 | - | |
394 | - /* | |
395 | - * @brief detect a decimal value | |
396 | - */ | |
397 | - private function isDecimal($element) | |
398 | - { | |
399 | - return preg_match("/^[+\-]?(?:0|[1-9]\d*)(?:\.?\d*)?(?:[eE][+\-]?\d+)?/",$element); | |
400 | - //return preg_match("/[+\-]?(?:0|[1-9]\d*)(?:\.?\d*)?(?:[eE][+\-]?\d+)/",$element); | |
401 | - } | |
402 | - | |
403 | - /* | |
404 | - * @brief detect an integer value | |
405 | - */ | |
406 | - private function isInteger($element) | |
407 | - { | |
408 | - return preg_match("/^\d+$/",$element); | |
409 | - } | |
410 | - | |
411 | - /* | |
412 | - * @brief explode expression for parsing | |
413 | - */ | |
414 | - private function explodeExpression($expression) | |
415 | - { | |
416 | - $crt = ""; | |
417 | - $elements = array(); | |
418 | - for ($i = 0; $i < strlen($expression); ++$i) | |
419 | - { | |
420 | - $c = $expression[$i]; | |
421 | - if ($this->isSeparator($c)) | |
422 | - { | |
423 | - if ((($c == "+") || ($c == "-")) && | |
424 | - preg_match('/^\d*\.?\d*[eE]$/',$crt)) | |
425 | - { | |
426 | - //scientific decimal element => false positive separator | |
427 | - $crt .= $c; | |
428 | - continue; | |
429 | - } | |
430 | - //push in elements list | |
431 | - if ($crt != "") | |
432 | - $elements[] = $crt; | |
433 | - $elements[] = $c; | |
434 | - $crt = ""; | |
435 | - continue; | |
436 | - } | |
437 | - $crt .= $c; | |
438 | - } | |
439 | - if ($crt != "") | |
440 | - $elements[] = $crt; | |
441 | - | |
442 | - //regroup parameters and components before to add brackets for operators priority | |
443 | - $els = array(); | |
444 | - $i = 0; | |
445 | - for ($i; $i < count($elements)-3; ++$i) | |
446 | - { | |
447 | - if (($elements[$i+1] == "(") && | |
448 | - $this->isParameter($elements[$i]) && | |
449 | - $this->isInteger($elements[$i+2]) && | |
450 | - ($elements[$i+3] == ")")) | |
451 | - { | |
452 | - $els[] = ($elements[$i].$elements[$i+1].$elements[$i+2].$elements[$i+3]); | |
453 | - $i = $i+3; | |
454 | - continue; | |
455 | - } | |
456 | - $els[] = $elements[$i]; | |
457 | - } | |
458 | - | |
459 | - for ($j = $i; $j < count($elements); ++$j) | |
460 | - $els[] = $elements[$j]; | |
461 | - | |
462 | - $elements = $els; | |
463 | - | |
464 | - //regroup negative number | |
465 | - $els = array(); | |
466 | - $i = 0; | |
467 | - for ($i; $i < count($elements)-1; ++$i) | |
468 | - { | |
469 | - if (($elements[$i] == "-") && ( | |
470 | - $this->isDecimal($elements[$i+1]) || $this->isInteger($elements[$i+1]))) | |
471 | - { | |
472 | - if (($i == 0) || (!$this->isDecimal($elements[$i-1]) && !$this->isInteger($elements[$i-1]) && !$this->isParameter($elements[$i-1]))) | |
473 | - { | |
474 | - $els[] = ($elements[$i].$elements[$i+1]); | |
475 | - $i = $i+1; | |
476 | - continue; | |
477 | - } | |
478 | - } | |
479 | - $els[] = $elements[$i]; | |
480 | - } | |
481 | - | |
482 | - for ($j = $i; $j < count($elements); ++$j) | |
483 | - $els[] = $elements[$j]; | |
484 | - | |
485 | - $elements = $els; | |
486 | - | |
487 | - //add brackets for operators priority | |
488 | - //Obsolete cf. https://projects.irap.omp.eu/issues/5239 | |
489 | - | |
490 | - /*$els = array(); | |
491 | - $i = 0; | |
492 | - for ($i; $i < count($elements)-2; ++$i) | |
493 | - { | |
494 | - if ($elements[$i+1] == "*" || $elements[$i+1] == "/" || $elements[$i+1] == "^" || $elements[$i+1] == "<" || $elements[$i+1] == ">") | |
495 | - { | |
496 | - if ($elements[$i] != ")" && ($this->isDecimal($elements[$i+2]) || $this->isInteger($elements[$i+2]))) | |
497 | - { | |
498 | - $els[] = "("; | |
499 | - $els[] = $elements[$i]; | |
500 | - $els[] = $elements[$i+1]; | |
501 | - $els[] = $elements[$i+2]; | |
502 | - $els[] = ")"; | |
503 | - $i += 2; | |
504 | - } | |
505 | - else | |
506 | - $els[] = $elements[$i]; | |
507 | - } | |
508 | - else | |
509 | - $els[] = $elements[$i]; | |
510 | - } | |
511 | - for ($j = $i; $j < count($elements); ++$j) | |
512 | - $els[] = $elements[$j];*/ | |
513 | - | |
514 | - //split parameters and components | |
515 | - $elements = $els; | |
516 | - | |
517 | - $els = array(); | |
518 | - $i = 0; | |
519 | - for ($i; $i < count($elements); ++$i) | |
520 | - { | |
521 | - $tmpArray = explode('(',$elements[$i]); | |
522 | - if ((count($tmpArray) == 2) && ($tmpArray[0] != "") && ($tmpArray[1] != "")) | |
523 | - { | |
524 | - $tmpArray2 = explode(')',$tmpArray[1]); | |
525 | - if ((count($tmpArray2) == 2) && ($tmpArray2[0] != "") && ($tmpArray2[1] == "")) | |
526 | - { | |
527 | - $els[] = "("; | |
528 | - $els[] = $tmpArray[0]; | |
529 | - $els[] = "("; | |
530 | - $els[] = $tmpArray2[0]; | |
531 | - $els[] = ")"; | |
532 | - $els[] = ")"; | |
533 | - continue; | |
534 | - } | |
535 | - } | |
536 | - $els[] = $elements[$i]; | |
537 | - } | |
538 | - | |
539 | - return $els; | |
540 | - } | |
541 | - | |
542 | - /* | |
543 | - * @brief build a tree of the expression | |
544 | - */ | |
545 | - private function buildTreeElements($elements) | |
546 | - { | |
547 | - $expression_group = array(); | |
548 | - $crt_group = &$expression_group; | |
549 | - $opened_groups = array($expression_group); | |
550 | - for($i = 0; $i < count($elements); ++$i) | |
551 | - { | |
552 | - if ($elements[$i] == "(") | |
553 | - { | |
554 | - array_push($opened_groups,array()); | |
555 | - continue; | |
556 | - } | |
557 | - if ($elements[$i] == ")") | |
558 | - { | |
559 | - $group_to_close = $opened_groups[count($opened_groups)-1]; | |
560 | - array_pop($opened_groups); | |
561 | - if (count($opened_groups) <= 0) | |
562 | - throw new Exception('Expression error - Brackets definition'); | |
563 | - array_push($opened_groups[count($opened_groups)-1],$group_to_close); | |
564 | - continue; | |
565 | - } | |
566 | - if (count($opened_groups) <= 0) | |
567 | - throw new Exception('Expression error - Brackets definition'); | |
568 | - array_push($opened_groups[count($opened_groups)-1],$elements[$i]); | |
569 | - } | |
570 | - | |
571 | - if (count($opened_groups) != 1) | |
572 | - throw new Exception('Expression error - Brackets definition'); | |
573 | - | |
574 | - $expression_group = $opened_groups[0]; | |
575 | - | |
576 | - return $expression_group; | |
577 | - } | |
578 | - | |
579 | - /* | |
580 | - * @brief detect if the element is a parameter | |
581 | - */ | |
582 | - private function isParameter($element) | |
583 | - { | |
584 | - return (!is_array($element) && | |
585 | - !$this->isDecimal($element) && | |
586 | - !$this->isOperator($element) && | |
587 | - !$this->isFunction($element) && | |
588 | - !$this->isRangeOrIndexes($element) && | |
589 | - ($element != ",") && ($element != ";") && ($element != "(")); | |
590 | - } | |
591 | - | |
592 | - /* | |
593 | - * @brief detect if the element is a component for a parameter | |
594 | - */ | |
595 | - private function isParameterComponent($tree, $i) | |
596 | - { | |
597 | - $paramId = ""; | |
598 | - if ($i <= 0) | |
599 | - return false; | |
600 | - if (!$this->isParameter($tree[$i-1])) | |
601 | - return false; | |
602 | - return (is_array($tree[$i]) && | |
603 | - (count($tree[$i] == 1)) && | |
604 | - $this->isInteger($tree[$i][0])); | |
605 | - } | |
606 | - | |
607 | - /* | |
608 | - * @brief detect if the element is a range for a parameter | |
609 | - */ | |
610 | - private function isParameterRangeOrIndexes($tree, $i, &$isIndexes) | |
611 | - { | |
612 | - $paramId = ""; | |
613 | - $isIndexes = FALSE; | |
614 | - if ($i <= 0) | |
615 | - return false; | |
616 | - if (!$this->isParameter($tree[$i-1])) | |
617 | - return false; | |
618 | - $result = (is_array($tree[$i]) && | |
619 | - (count($tree[$i] == 1)) && | |
620 | - $this->isRangeOrIndexes($tree[$i][0])); | |
621 | - if ($result) | |
622 | - $isIndexes = ($tree[$i][0] == "indexes"); | |
623 | - return $result; | |
624 | - } | |
625 | - | |
626 | - /* | |
627 | - * @brief add a parameter | |
628 | - */ | |
629 | - private function addParameterIdInParameterArray($paramId, &$params) | |
630 | - { | |
631 | - foreach ($params as $param) | |
632 | - if ($param["id"] == $paramId) | |
633 | - return; | |
634 | - $params[] = array("id" => $paramId, "indexes" => array(), "calib_infos" => array()); | |
635 | - } | |
636 | - | |
637 | - /* | |
638 | - * @brief add a parameter component | |
639 | - */ | |
640 | - private function addParameterComponentInParameterArray($paramId, $component, &$params) | |
641 | - { | |
642 | - foreach ($params as &$param) { | |
643 | - if ($param["id"] == $paramId) | |
644 | - { | |
645 | - array_push($param["indexes"],$component); | |
646 | - return; | |
647 | - } | |
648 | - } | |
649 | - } | |
650 | - | |
651 | - /* | |
652 | - * @brief add a parameter range | |
653 | - */ | |
654 | - private function addParameterRangeOrIndexesInParameterArray($paramId, $range, $isIndexes, &$params) | |
655 | - { | |
656 | - $sum_parameter_id = ""; | |
657 | - if ((count($range) == 2) && (count($range[1]) == 3) && ($this->isInteger($range[1][0])) && ($this->isInteger($range[1][2])) & ($range[1][1] == ",")) { | |
658 | - $sum_param = $isIndexes ? "sum_into_table_indexes" : "sum_into_table_range"; | |
659 | - $sum_parameter_id = $sum_param."_".$paramId."_0_".$range[1][0]."_".$range[1][2]; | |
660 | - $params[] = array("id" => $sum_param, | |
661 | - "fullparamid" => $sum_parameter_id, | |
662 | - "indexes" => array(), | |
663 | - "calib_infos" => array(), | |
664 | - "template_args" => array( | |
665 | - "paramid" => $paramId, | |
666 | - "relateddim" => 0, | |
667 | - "min" => $range[1][0], | |
668 | - "max" => $range[1][2] | |
669 | - ) | |
670 | - ); | |
671 | - } | |
672 | - return $sum_parameter_id; | |
673 | - } | |
674 | - | |
675 | - /* | |
676 | - * @brief process used to treat parameter components | |
677 | - */ | |
678 | - private function regroupParameterAndComponent($tree,&$params,&$result) | |
679 | - { | |
680 | - for ($i = 0; $i < count($tree); ++$i) | |
681 | - { | |
682 | - if (is_array($tree[$i]) && (count($tree[$i]) > 0)) | |
683 | - { | |
684 | - $res = array(); | |
685 | - $this->regroupParameterAndComponent($tree[$i],$params,$res); | |
686 | - $result[] = $res; | |
687 | - } | |
688 | - else if ($this->isParameter($tree[$i])) | |
689 | - { | |
690 | - $param = $tree[$i]; | |
691 | - | |
692 | - if ($i < count($tree) - 1) | |
693 | - { | |
694 | - $isIndexes = FALSE; | |
695 | - if ($this->isParameterRangeOrIndexes($tree,$i+1,$isIndexes)) | |
696 | - { | |
697 | - $sum_parameter_id = $this->addParameterRangeOrIndexesInParameterArray($param, $tree[$i+1], $isIndexes, $params); | |
698 | - $param = $sum_parameter_id; | |
699 | - ++$i; | |
700 | - } | |
701 | - else | |
702 | - { | |
703 | - $this->addParameterIdInParameterArray($param,$params); | |
704 | - if ($this->isParameterComponent($tree,$i+1)) | |
705 | - { | |
706 | - if ((count($tree[$i+1]) == 3) && ($tree[$i+1][1] == ',') && $this->isInteger($tree[$i+1][2])) { | |
707 | - //2D | |
708 | - $this->addParameterComponentInParameterArray($param, array($tree[$i+1][0], $tree[$i+1][2]), $params); | |
709 | - $param .= ("[".$tree[$i+1][0]."][".$tree[$i+1][2]."]"); | |
710 | - } | |
711 | - else { | |
712 | - //1D | |
713 | - $this->addParameterComponentInParameterArray($param, $tree[$i+1][0], $params); | |
714 | - $param .= ("[".$tree[$i+1][0]."]"); | |
715 | - } | |
716 | - ++$i; | |
717 | - } | |
718 | - } | |
719 | - } | |
720 | - else | |
721 | - $this->addParameterIdInParameterArray($param,$params); | |
722 | - | |
723 | - $result[] = $param; | |
724 | - } | |
725 | - else | |
726 | - { | |
727 | - $result[] = $tree[$i]; | |
728 | - } | |
729 | - } | |
730 | - } | |
731 | - | |
732 | - /* | |
733 | - * @brief replace all operators that's needed to be replace by a function | |
734 | - */ | |
735 | - private function fixOperatorToFunction($tree,&$result) | |
736 | - { | |
737 | - $i = 0; | |
738 | - for ($i; $i < count($tree)-1; ++$i) | |
739 | - { | |
740 | - if ($this->isOperatorToFunction($tree[$i+1])) | |
741 | - { | |
742 | - if ($i >= count($tree)-2) | |
743 | - throw new Exception('Expression error - Error in operator definition'); | |
744 | - | |
745 | - if (is_array($tree[$i])) | |
746 | - { | |
747 | - $left = array(); | |
748 | - $this->fixOperatorToFunction($tree[$i],$left); | |
749 | - } | |
750 | - else | |
751 | - $left = $tree[$i]; | |
752 | - $function = $this->getOperatorAssociatedFunction($tree[$i+1]); | |
753 | - if (is_array($tree[$i+2])) | |
754 | - { | |
755 | - $right = array(); | |
756 | - $this->fixOperatorToFunction($tree[$i+2],$right); | |
757 | - } | |
758 | - else | |
759 | - $right = $tree[$i+2]; | |
760 | - $result[] = $function; | |
761 | - $result[] = array($left,",",$right); | |
762 | - $i += 2; | |
763 | - } | |
764 | - else | |
765 | - { | |
766 | - if (is_array($tree[$i])) | |
767 | - { | |
768 | - $res = array(); | |
769 | - $this->fixOperatorToFunction($tree[$i],$res); | |
770 | - $result[] = $res; | |
771 | - } | |
772 | - else | |
773 | - $result[] = $tree[$i]; | |
774 | - } | |
775 | - } | |
776 | - | |
777 | - for ($j = $i; $j < count($tree); ++$j) | |
778 | - { | |
779 | - if (is_array($tree[$j])) | |
780 | - { | |
781 | - $res = array(); | |
782 | - $this->fixOperatorToFunction($tree[$j],$res); | |
783 | - $result[] = $res; | |
784 | - } | |
785 | - else | |
786 | - $result[] = $tree[$j]; | |
787 | - } | |
788 | - } | |
789 | - | |
790 | - private function fixGroupFunction($tree,&$result) { | |
791 | - $result = array(); | |
792 | - for ($i = 0; $i < count($tree); ++$i) | |
793 | - { | |
794 | - if (is_array($tree[$i])) { | |
795 | - $res = array(); | |
796 | - $this->fixGroupFunction($tree[$i],$res); | |
797 | - $result[] = $res; | |
798 | - } | |
799 | - else if ($this->isFunction($tree[$i])) { | |
800 | - if ($i == count($tree) - 1) { | |
801 | - throw new Exception('Expression error - Error in function call'); | |
802 | - } | |
803 | - $res = array(); | |
804 | - $this->fixGroupFunction($tree[$i+1],$res); | |
805 | - $result[] = array($tree[$i],$res); | |
806 | - ++$i; | |
807 | - } | |
808 | - else | |
809 | - $result[] = $tree[$i]; | |
810 | - } | |
811 | - } | |
812 | - | |
813 | - /* | |
814 | - * @brief process used to replace "," by ";" for function | |
815 | - */ | |
816 | - private function fixComaFunction($tree,$crt_function,&$result) | |
817 | - { | |
818 | - $nbParams = 0; | |
819 | - for ($i = 0; $i < count($tree); ++$i) | |
820 | - { | |
821 | - if (is_array($tree[$i]) && (count($tree[$i]) > 0)) | |
822 | - { | |
823 | - $groupFunc = false; | |
824 | - if (($i > 0) && (!is_array($tree[$i-1]))) | |
825 | - { | |
826 | - $groupFunc = $this->isFunction($tree[$i-1]); | |
827 | - } | |
828 | - $res = array(); | |
829 | - $this->fixComaFunction($tree[$i],$groupFunc ? $tree[$i-1] : "",$res); | |
830 | - $result[] = $res; | |
831 | - } | |
832 | - else | |
833 | - { | |
834 | - if (!empty($crt_function) && ($tree[$i] == ',')) { | |
835 | - $nb_func_params = $this->functionsArray[$crt_function]["nb_params"]; | |
836 | - $result[] = ($nb_func_params <= $nbParams) ? ';' : ','; | |
837 | - } | |
838 | - else { | |
839 | - ++$nbParams; | |
840 | - $result[] = $tree[$i]; | |
841 | - } | |
842 | - } | |
843 | - } | |
844 | - } | |
845 | - | |
846 | - /* | |
847 | - * @brief write the result expression from tree elements | |
848 | - */ | |
849 | - private function writeTranslate($tree,$params) | |
850 | - { | |
851 | - $translated = ""; | |
852 | - | |
853 | - for ($i = 0; $i < count($tree); ++$i) | |
854 | - { | |
855 | - #echo "=> ".$tree[$i].PHP_EOL; | |
856 | - if (is_array($tree[$i])) | |
857 | - { | |
858 | - //echo "ARRAY".PHP_EOL; | |
859 | - if ((count($tree[$i]) > 1) || | |
860 | - (count($tree[$i]) == 1) && ($i>0) && (!is_array($tree[$i-1]) && $this->isFunction($tree[$i-1]))) | |
861 | - $translated .= ("(".$this->writeTranslate($tree[$i],$params).")"); | |
862 | - else | |
863 | - $translated .= ($this->writeTranslate($tree[$i],$params)); | |
864 | - } | |
865 | - else if ($this->isDecimal($tree[$i])) | |
866 | - { | |
867 | - //echo "DECIMAL ".$tree[$i].PHP_EOL; | |
868 | - $translated .= $tree[$i]; | |
869 | - } | |
870 | - else if ($this->isOperator($tree[$i])) | |
871 | - { | |
872 | - //echo "OPERATOR".PHP_EOL; | |
873 | - $translated .= $tree[$i]; | |
874 | - } | |
875 | - else if ($tree[$i] == ",") | |
876 | - { | |
877 | - //echo "COMA".PHP_EOL; | |
878 | - $translated .= ","; | |
879 | - } | |
880 | - else if ($tree[$i] == ";") | |
881 | - { | |
882 | - // echo "DOT COMA".PHP_EOL; | |
883 | - $translated .= ";"; | |
884 | - } | |
885 | - else if ($this->isFunction($tree[$i])) | |
886 | - { | |
887 | - //echo "FUNCTION".PHP_EOL; | |
888 | - $kernelFunction = $this->functionsArray[$tree[$i]]["kernel_name"]; | |
889 | - if ($kernelFunction == "") | |
890 | - throw new Exception('Expression error - Function '.$tree[$i]." not implemented"); | |
891 | - $firstArgForFunc = ($this->functionsArray[$tree[$i]]["nb_args"] != 0); | |
892 | - $translated .= $kernelFunction; | |
893 | - } | |
894 | - else | |
895 | - { | |
896 | - $founded = false; | |
897 | - foreach ($params as $param) | |
898 | - { | |
899 | - #echo "TEST ".$param["id"].PHP_EOL; | |
900 | - | |
901 | - if (array_key_exists("template_args", $param)) | |
902 | - { | |
903 | - $founded = true; | |
904 | - $translated .= "\$".$param["fullparamid"]; | |
905 | - break; | |
906 | - } | |
907 | - else if ($param["id"] == $tree[$i]) | |
908 | - { | |
909 | - $founded = true; | |
910 | - $translated .= ("\$".$tree[$i]); | |
911 | - break; | |
912 | - } | |
913 | - | |
914 | - foreach ($param["indexes"] as $index) | |
915 | - { | |
916 | - $temp = ""; | |
917 | - if (is_array($index) && (count($index) == 2)) { | |
918 | - //2D | |
919 | - $temp .= ($param["id"]."[".$index[0]."][".$index[1]."]"); | |
920 | - } | |
921 | - else { | |
922 | - //1D | |
923 | - $temp .= ($param["id"]."[".$index."]"); | |
924 | - } | |
925 | - #echo "TEST 2 ".$temp.PHP_EOL; | |
926 | - if ($temp == $tree[$i]) | |
927 | - { | |
928 | - $founded = true; | |
929 | - $translated .= ("\$".$tree[$i]); | |
930 | - break; | |
931 | - } | |
932 | - } | |
933 | - | |
934 | - if ($founded) | |
935 | - break; | |
936 | - } | |
937 | - if (!$founded) | |
938 | - { | |
939 | - throw new Exception('Expression error - Unknown element '.$tree[$i]); | |
940 | - } | |
941 | - } | |
942 | - } | |
943 | - | |
944 | - return $translated; | |
945 | - } | |
946 | - | |
947 | - /* | |
948 | - * @brief sequence used to translate a tree elements | |
949 | - */ | |
950 | - private function translate($tree,&$params) | |
951 | - { | |
952 | - $res_1 = array(); | |
953 | - $this->regroupParameterAndComponent($tree,$params,$res_1); | |
954 | - | |
955 | - $res_2 = array(); | |
956 | - $this->fixGroupFunction($res_1,$res_2); | |
957 | - | |
958 | - $res_3 = array(); | |
959 | - $this->fixComaFunction($res_2,"",$res_3); | |
960 | - | |
961 | - $res_4 = array(); | |
962 | - $this->fixOperatorToFunction($res_3,$res_4); | |
963 | - | |
964 | - return $this->writeTranslate($res_4,$params); | |
965 | - } | |
966 | -} | |
967 | - | |
968 | -/*$parser = new IHMExpressionParserClass(TRUE); | |
969 | - try { | |
970 | -$parser->test(); | |
971 | -} catch (Exception $e) { | |
972 | -echo 'Exception detected : '.$e->getMessage().PHP_EOL; | |
973 | -}*/ | |
974 | - | |
975 | -?> |
src/InputOutput/IHMImpl/Tools/IHMParamManagerClass.php
... | ... | @@ -8,7 +8,6 @@ |
8 | 8 | class IHMParamManagerClass |
9 | 9 | { |
10 | 10 | protected $userParameterLoader = null; |
11 | - protected $expressionParser = null; | |
12 | 11 | protected $templateParamsManager = null; |
13 | 12 | protected $paramImpexMgr = null; |
14 | 13 | protected $ttCatMgr = null; |
... | ... | @@ -23,9 +22,10 @@ class IHMParamManagerClass |
23 | 22 | |
24 | 23 | public function addGeneratedParam($paramId, $expression, $sampling, $paramsData) |
25 | 24 | { |
26 | - if (!isset($this->expressionParser)) | |
27 | - $this->expressionParser = new IHMExpressionParserClass(); | |
28 | - $expressionInfo = $this->expressionParser->parse($expression); | |
25 | + $expressionInfo = $this->parseExpression($expression, $paramsData->getWorkingPath()); | |
26 | + if (!$expressionInfo['success']) { | |
27 | + throw new Exception($expressionInfo['message']); | |
28 | + } | |
29 | 29 | |
30 | 30 | //create a process param for the derived parameter |
31 | 31 | $this->addProcessParam($paramId, $expressionInfo["expression"], $expression, |
... | ... | @@ -286,9 +286,11 @@ class IHMParamManagerClass |
286 | 286 | throw new Exception('Error to load derived parameter file : '.$res["message"]); |
287 | 287 | |
288 | 288 | //parse expression |
289 | - if (!isset($this->expressionParser)) | |
290 | - $this->expressionParser = new IHMExpressionParserClass(); | |
291 | - $expressionInfo = $this->expressionParser->parse($res["param"]["expression"]); | |
289 | + $expressionInfo = $this->parseExpression($res["param"]["expression"], $paramsData->getWorkingPath()); | |
290 | + | |
291 | + if (!$expressionInfo['success']) { | |
292 | + throw new Exception($expressionInfo['message']); | |
293 | + } | |
292 | 294 | |
293 | 295 | $paramId = $param; |
294 | 296 | |
... | ... | @@ -669,4 +671,101 @@ class IHMParamManagerClass |
669 | 671 | break; |
670 | 672 | } |
671 | 673 | } |
674 | + | |
675 | + public function parseExpression($expression, $workingDir) { | |
676 | + $parserData = new ParserRequestDataClass(); | |
677 | + $parserData->setManagerFilePath(IHMConfigClass::getProcessManagerFilePath()); | |
678 | + $parserData->addExpression($expression); | |
679 | + $parserData->setWorkingPath($workingDir); | |
680 | + $parserData->setLocalParamsPath(IHMConfigClass::getLocalParamDBPath()); | |
681 | + $parserData->setConstantsFilePath(IHMConfigClass::getConstantsFilePath()); | |
682 | + $parserData->setFunctionsFilePath(IHMConfigClass::getFunctionsFilePath()); | |
683 | + $outputFileName = "parser_".md5($expression).".xml"; | |
684 | + $parserData->setOutputFileName("parser_".md5($expression).".xml"); | |
685 | + $resultFilePath = $workingDir."/".$outputFileName; | |
686 | + | |
687 | + $parserRequest = new ParserRequestClass(); | |
688 | + $parserRequest->setData($parserData); | |
689 | + | |
690 | + if (!$parserRequest->init() || !$parserRequest->run()) { | |
691 | + if (file_exists($resultFilePath)) { | |
692 | + unlink($resultFilePath); | |
693 | + } | |
694 | + return array('success' => FALSE, 'message' => 'Error to init or run expression parser'); | |
695 | + } | |
696 | + | |
697 | + //Load result file | |
698 | + $doc = new DOMDocument(); | |
699 | + $doc->load($resultFilePath); | |
700 | + if (!$doc->load($resultFilePath)) { | |
701 | + if (file_exists($resultFilePath)) { | |
702 | + unlink($resultFilePath); | |
703 | + } | |
704 | + return array('success' => FALSE, 'message' => 'Error to load parser result file'); | |
705 | + } | |
706 | + | |
707 | + $expressionNodes = $doc->getElementsByTagName('expression'); | |
708 | + if ($expressionNodes->length == 0) { | |
709 | + unlink($resultFilePath); | |
710 | + return array('success' => FALSE, 'message' => 'Cannot get expression nodes in parser result file'); | |
711 | + } | |
712 | + | |
713 | + $crtExpressionNode = NULL; | |
714 | + foreach ($expressionNodes as $expressionNode) { | |
715 | + $ihmExpressionNodes = $expressionNode->getElementsByTagName('ihm'); | |
716 | + if ($ihmExpressionNodes->length == 0) { | |
717 | + continue; | |
718 | + } | |
719 | + if ($ihmExpressionNodes->item(0)->nodeValue == $expression) { | |
720 | + $crtExpressionNode = $expressionNode; | |
721 | + break; | |
722 | + } | |
723 | + } | |
724 | + | |
725 | + if (!isset($crtExpressionNode)) { | |
726 | + unlink($resultFilePath); | |
727 | + return array('success' => FALSE, 'message' => 'Cannot retrieve expression in parser result file'); | |
728 | + } | |
729 | + | |
730 | + if ($crtExpressionNode->getAttribute('success') != "true") { | |
731 | + unlink($resultFilePath); | |
732 | + return array('success' => FALSE, 'message' => 'Synthax error in expression '.$expression); | |
733 | + } | |
734 | + | |
735 | + $kernelExpressionNodes = $crtExpressionNode->getElementsByTagName('kernel'); | |
736 | + if ($kernelExpressionNodes->length == 0) { | |
737 | + unlink($resultFilePath); | |
738 | + return array('success' => FALSE, 'message' => 'Cannot retrieve kernel expression in parser result file'); | |
739 | + } | |
740 | + | |
741 | + $kernelExpression = $kernelExpressionNodes->item(0)->nodeValue; | |
742 | + | |
743 | + if (empty($kernelExpression)) { | |
744 | + unlink($resultFilePath); | |
745 | + return array('success' => FALSE, 'message' => 'Parser return an empty expression'); | |
746 | + } | |
747 | + | |
748 | + $params = array(); | |
749 | + $paramsNodes = $crtExpressionNode->getElementsByTagName('params'); | |
750 | + if ($paramsNodes->length != 0) { | |
751 | + $paramNodes = $paramsNodes->item(0)->getElementsByTagName('param'); | |
752 | + if ($paramNodes->length != 0) { | |
753 | + foreach ($paramNodes as $paramNode) { | |
754 | + if (!empty($paramNode->nodeValue)) { | |
755 | + $params[] = array( | |
756 | + 'paramid' => $paramNode->nodeValue | |
757 | + ); | |
758 | + } | |
759 | + } | |
760 | + } | |
761 | + } | |
762 | + | |
763 | + unlink($resultFilePath); | |
764 | + | |
765 | + if (empty($params)) { | |
766 | + return array('success' => FALSE, 'message' => 'Expression should contain at least one parameter'); | |
767 | + } | |
768 | + | |
769 | + return array('success' => TRUE, 'expression' => $kernelExpression, 'params' => $params); | |
770 | + } | |
672 | 771 | } | ... | ... |
src/Request/Config/KernelConfigClass.php
... | ... | @@ -27,8 +27,9 @@ class KernelConfigClass |
27 | 27 | private static $defaultGapThreshold = "5"; |
28 | 28 | |
29 | 29 | |
30 | - private static $userHost = ""; | |
31 | - private static $userName = ""; | |
30 | + private static $userHost = ""; | |
31 | + private static $userName = ""; | |
32 | + private static $forceParamsDir = ""; | |
32 | 33 | |
33 | 34 | private static $timeToBatchMode = 10; // secs : interval after which batch mode is launched |
34 | 35 | |
... | ... | @@ -56,9 +57,14 @@ class KernelConfigClass |
56 | 57 | self::$userName = $user; |
57 | 58 | } |
58 | 59 | |
60 | + public static function setForceParamsDir($paramsDir) | |
61 | + { | |
62 | + self::$forceParamsDir = $paramsDir; | |
63 | + } | |
64 | + | |
59 | 65 | public static function getRequestParamsPath($working_dir) |
60 | 66 | { |
61 | - $paramsPath = $working_dir.self::$requestParamsDir; | |
67 | + $paramsPath = empty(self::$forceParamsDir) ? $working_dir.self::$requestParamsDir : self::$forceParamsDir; | |
62 | 68 | |
63 | 69 | if (!is_dir($paramsPath)) |
64 | 70 | mkdir($paramsPath); |
... | ... | @@ -105,6 +111,16 @@ class KernelConfigClass |
105 | 111 | { |
106 | 112 | return AMDA_KERNEL_DIR.'/config/xsd/spicekernel/config.xsd'; |
107 | 113 | } |
114 | + | |
115 | + public static function getXSDFunctionsFilePath() | |
116 | + { | |
117 | + return AMDA_KERNEL_DIR.'/config/xsd/parser/functions.xsd'; | |
118 | + } | |
119 | + | |
120 | + public static function getXSDConstantsFilePath() | |
121 | + { | |
122 | + return AMDA_KERNEL_DIR.'/config/xsd/parser/constants.xsd'; | |
123 | + } | |
108 | 124 | |
109 | 125 | |
110 | 126 | public static function getDatasetInfoPath() |
... | ... | @@ -152,7 +168,7 @@ class KernelConfigClass |
152 | 168 | return self::$timeToBatchMode; |
153 | 169 | } |
154 | 170 | |
155 | - public static function write($working_dir, $compilation_path, $localbase_path) | |
171 | + public static function write($working_dir, $compilation_path = "", $localbase_path = "", $functionsfile_path = "", $constantsfile_path = "") | |
156 | 172 | { |
157 | 173 | //create app.properties file |
158 | 174 | $appProperties = array(); |
... | ... | @@ -175,6 +191,11 @@ class KernelConfigClass |
175 | 191 | |
176 | 192 | $appProperties["app.spicekernel.configxsd"] = self::getXSDSpiceKernelConfigFilePath(); |
177 | 193 | $appProperties["app.spicekernel.configfile"] = KERNEL_CONFIG_DIR.'/spiceKernelConfig.xml'; |
194 | + | |
195 | + $appProperties["app.ihmConstants.xsd"] = self::getXSDConstantsFilePath(); | |
196 | + $appProperties["app.ihmConstants.xml"] = $constantsfile_path; | |
197 | + $appProperties["app.ihmFunctions.xsd"] = self::getXSDFunctionsFilePath(); | |
198 | + $appProperties["app.ihmFunctions.xml"] = $functionsfile_path; | |
178 | 199 | |
179 | 200 | $appProperties["app.plugin"] = AMDA_KERNEL_BUILD_DIR.'/'.self::$pluginDir; |
180 | 201 | $appProperties["app.process.src"] = $compilation_path.self::$userSrcDir; | ... | ... |
... | ... | @@ -0,0 +1,51 @@ |
1 | +<?php | |
2 | +/** | |
3 | + * @class ParserRequestClass | |
4 | + * @brief Treats a parser request. This class inherits from ProcessRequestClass | |
5 | + * @details | |
6 | + */ | |
7 | +class ParserRequestClass extends ProcessRequestClass | |
8 | +{ | |
9 | + /* | |
10 | + * @brief Init a params request | |
11 | + */ | |
12 | + public function init() | |
13 | + { | |
14 | + if (!isset($this->requestData)) | |
15 | + return false; | |
16 | + | |
17 | + $this->requestData->setType(ProcessTypeEnumClass::RUN); | |
18 | + $this->requestData->setBatchEnable(FALSE); | |
19 | + | |
20 | + $cmd = KernelConfigClass::getKernelBinPath()."expressionParser "; | |
21 | + foreach ($this->requestData->getExpressions() as $expression) | |
22 | + { | |
23 | + $cmd .= "-e \"".$expression."\" "; | |
24 | + } | |
25 | + $cmd .= "-o \"".$this->requestData->getOutputFileName()."\""; | |
26 | + $this->requestData->setCmd($cmd); | |
27 | + | |
28 | + $this->requestData->setEnvVars(KernelConfigClass::getExecEnvVarArray()); | |
29 | + | |
30 | + if (!parent::init()) | |
31 | + return false; | |
32 | + | |
33 | + $this->requestData->setSuccess(false); | |
34 | + $this->requestData->setLastErrorMessage('Cannot init parser request'); | |
35 | + | |
36 | + //create config files | |
37 | + KernelConfigClass::setForceParamsDir($this->requestData->getLocalParamsPath()); | |
38 | + KernelConfigClass::write($this->requestData->getWorkingPath(), "", "", $this->requestData->getFunctionsFilePath(), $this->requestData->getConstantsFilePath()); | |
39 | + KernelConfigClass::setForceParamsDir(""); | |
40 | + | |
41 | + return true; | |
42 | + } | |
43 | + /* | |
44 | + * @brief Run a params request | |
45 | + */ | |
46 | + public function run() | |
47 | + { | |
48 | + return parent::run(); | |
49 | + } | |
50 | +} | |
51 | +?> | ... | ... |
src/Request/ParserRequestImpl/ParserRequestDataClass.php
0 โ 100644
... | ... | @@ -0,0 +1,66 @@ |
1 | +<?php | |
2 | + | |
3 | +/** | |
4 | + * @class ParserRequestDataClass | |
5 | + * @brief Data for a parser request. Inherits from ProcessRequestDataClass | |
6 | + * @details | |
7 | + */ | |
8 | +class ParserRequestDataClass extends ProcessRequestDataClass | |
9 | +{ | |
10 | + private $outputFileName = ""; | |
11 | + private $expressions = array(); | |
12 | + private $constantsFilePath = ""; | |
13 | + private $functionsFilePath = ""; | |
14 | + private $localParamsPath = ""; | |
15 | + | |
16 | + function __construct() | |
17 | + { | |
18 | + parent::__construct(); | |
19 | + } | |
20 | + | |
21 | + public function getOutputFileName() | |
22 | + { | |
23 | + return $this->outputFileName; | |
24 | + } | |
25 | + | |
26 | + public function setOutputFileName($outputFileName) | |
27 | + { | |
28 | + $this->outputFileName = $outputFileName; | |
29 | + } | |
30 | + | |
31 | + public function addExpression($expression) | |
32 | + { | |
33 | + $this->expressions[] = $expression; | |
34 | + } | |
35 | + | |
36 | + public function getExpressions() | |
37 | + { | |
38 | + return $this->expressions; | |
39 | + } | |
40 | + | |
41 | + public function getConstantsFilePath() { | |
42 | + return $this->constantsFilePath; | |
43 | + } | |
44 | + | |
45 | + public function setConstantsFilePath($constantsFilePath) { | |
46 | + $this->constantsFilePath = $constantsFilePath; | |
47 | + } | |
48 | + | |
49 | + public function getFunctionsFilePath() { | |
50 | + return $this->functionsFilePath; | |
51 | + } | |
52 | + | |
53 | + public function setFunctionsFilePath($functionsFilePath) { | |
54 | + $this->functionsFilePath = $functionsFilePath; | |
55 | + } | |
56 | + | |
57 | + public function setLocalParamsPath($localParamsPath) { | |
58 | + $this->localParamsPath = $localParamsPath; | |
59 | + } | |
60 | + | |
61 | + public function getLocalParamsPath() { | |
62 | + return $this->localParamsPath; | |
63 | + } | |
64 | +} | |
65 | + | |
66 | +?> | ... | ... |
src/amdaintegration_autoload.php
... | ... | @@ -36,7 +36,8 @@ function amdaintegration_autoload($class_name) |
36 | 36 | 'Request/ParamsRequestImpl/Nodes/Requests', |
37 | 37 | 'Request/ProcessRequestImpl', |
38 | 38 | 'Request/ProcessRequestImpl/Process', |
39 | - 'Request/TTRequestImpl' | |
39 | + 'Request/TTRequestImpl', | |
40 | + 'Request/ParserRequestImpl', | |
40 | 41 | ); |
41 | 42 | |
42 | 43 | $ihm_dirs = array( | ... | ... |