Blame view

src/InputOutput/IHMImpl/Tools/IHMUserParamLoaderClass.php 3.98 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

/**
 * @class IHMUserParamLoaderClass
 * @brief Loader for IHM user parameter properties
 * @details
 */
class IHMUserParamLoaderClass
{
	protected $userParamsList = null;
	
	//derived parameter in manager file
	private static $mgrDerivedNode               = 'paramList';
	private static $mgrDerivedParamNode          = 'param';
	private static $mgrDerivedParamIdAtt         = 'xml:id';
	private static $mgrDerivedParamNameAtt       = 'name';
	private static $mgrDerivedParamExpressionAtt = 'buildchain';
	private static $mgrDerivedParamTimeStepAtt   = 'timestep';
	
	//additional info for derived parameter
	private static $infoDerivedUnitsNode       = 'units';
	private static $infoDerivedDescriptionNode = 'description';
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
	}
	
	/*
	 * @brief Get derived parameter info from name ("ws_***")
	 */
	public function getDerivedParameterFromName($paramName)
	{
		if (!preg_match("#^ws_#",$paramName))
			return array("success" => false, "message" => "Bad derived parameter name");
		
		//extract real parameter name
		$realName = substr($paramName , 3);
		
		//try to load user parameters definition if not already done
		if (!isset($this->userParamsList))
			$this->userParamsList = $this->loadUserParamManagerFile();
		
		if (!$this->userParamsList["success"])
			return $this->userParamsList["success"];
		
		if (isset($this->userParamsList["params"]) && isset($this->userParamsList["params"]["derived"]))
		{
			//find the parameter info
			foreach($this->userParamsList["params"]["derived"] as $paramInfo)
			{
				if ($paramInfo["name"] == $realName)
					return array("success" => true, "param" => $paramInfo);
			}
		}
		
		return array("success" => false, "message" => "Cannot find derived parameter");
	}
	
	/*
	 * @brief Load derived parameter manager file
	 */
	private function loadUserParamManagerFile()
	{
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load(IHMConfigClass::getUserParamManagerFilePath()))
			return array("success" => false, "message" => "Cannot load user parameter manager file");
		
		$userParams = array();
		
		//get derived parameter node
		$derivedNodes = $dom->getElementsByTagName(self::$mgrDerivedNode);
				
		$derivedParams = array();
		if (count($derivedNodes) > 0)
		{	
			$derivedParamNodes = $derivedNodes->item(0)->getElementsByTagName(self::$mgrDerivedParamNode);
			foreach ($derivedParamNodes as $derivedParamNode)
			{	
				//id
				$paramId         = $derivedParamNode->getAttribute(self::$mgrDerivedParamIdAtt);
				
				//name
				$paramName       = $derivedParamNode->getAttribute(self::$mgrDerivedParamNameAtt);
				
				//expression
				$paramExpression = $derivedParamNode->getAttribute(self::$mgrDerivedParamExpressionAtt);
				
				//timestep
				$paramTimeStep   = $derivedParamNode->getAttribute(self::$mgrDerivedParamTimeStepAtt);
				
				array_push($derivedParams,array(
					"id"         => $paramId,
					"name"       => $paramName,
					"expression" => $paramExpression,
					"timestep"   => $paramTimeStep,
					"info"       => $this->loadDerivedParameterInfo($id)
				));
			}
		}
		
		$userParams["derived"] = $derivedParams;
		
		return array("success" => true, "params" => $userParams);
	}
	
	/*
	 * @brief Load additionnal derived parameter info from paramId
	*/
	private function loadDerivedParameterInfo($paramId)
	{
		//get full path
		$path = IHMConfigClass::getUserWSPath().$paramId.".xml";
	
		$result = array(
				"units"       => "",
				"description" => ""
		);
		
		if (!file_exists($path))
			return $result;
	
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load($path))
			return $result;
	
		//get parameter units
		$unitsNodes = $dom->getElementsByTagName(self::$infoDerivedUnitsNode);
		if (count($unitsNodes) > 0)
			$result["units"] = $unitsNodes->item(0)->nodeValue;
	
		//get parameter description
		$descNodes = $dom->getElementsByTagName(self::$infoDerivedDescriptionNode);
		if (count($descNodes) > 0)
			$result["description"] = $descNodes->item(0)->nodeValue;
	
		return $result;
	}
}