IHMParamManagerClass.php 4.37 KB
<?php

/**
 * @class IHMParamManagerClass
 * @brief Parameter manager
 * @details
 */
class IHMParamManagerClass
{
	protected $userParameterLoader = null;
	protected $expressionParser = null;
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
	}

	/*
	 * @brief Add an existing parameter
	*/
	public function addExistingParam($param,$paramsData)
	{
		if ($this->isDerivedParam($param))
			return $this->addDerivedParam($param,$paramsData);
		else if ($this->isUploadedParam($param))
			return $this->addUploadedParam($param,$paramsData);
		else
			return $this->addLocalParam($param,$paramsData);
		return "";
	}

	/*
	 * @brief Add a process parameter
	*/
	public function addProcessParam($paramId,$expression,$params,$sampling,$gap,$dateModif,$paramsData)
	{
		$paramsData->addProcessParamToCreate($paramId, $expression, $params, $sampling, $gap,$dateModif);

		foreach ($params as $param)
			$this->addExistingParam($param,$paramsData);

		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);
	}

	/*
	 * @brief Add a local parameter
	*/
	private function addLocalParam($param,$paramsData)
	{
		//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;
			
		$paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";

		if (!file_exists($paramPath))
			throw new Exception('Cannot find parameter local file '.$paramId);

		$paramsData->addParamToCopy($paramId,$paramPath);

		$this->addLinkedLocalParams($paramId,$paramsData);
			
		return array("id" => $paramId, "indexes" => $indexes, "calib_infos" => $calib_infos);
	}

	/*
	 * @brief Add linked parameter
	*/
	private function addLinkedLocalParams($paramId,$paramsData)
	{
		$doc = new DOMDocument("1.0", "UTF-8");
		$doc->preserveWhiteSpace = false;
		$doc->formatOutput = true;

		$paramPath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";

		if (!$doc->load($paramPath))
			throw new Exception('Cannot find parameter local file '.$paramId);

		//<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');
			if ($linkedParamId == '')
				continue;
			$linkedParamPath = IHMConfigClass::getLocalParamDBPath().$linkedParamId.".xml";
			$paramsData->addParamToCopy($linkedParamId,$linkedParamPath);
			$this->addLinkedLocalParams($linkedParamId,$paramsData);
		}
	}

	/*
	 * @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))
			$this->expressionParser = new IHMExpressionParserClass();
		$expressionInfo = $this->expressionParser->parse($res["param"]["expression"]);
		
		$paramId = $param;
		
		//create a process param for the derived parameter
		$this->addProcessParam($paramId, $expressionInfo["expression"],
				$expressionInfo['params'], $res["param"]["timestep"],
				0,$res["param"]["dateModif"],$paramsData);
		
		return array("id" => $paramId, "indexes" => array(), "calib_infos" => array());
	}

	/*
	 * @brief Add uploaded parameter
	*/
	private function addUploadedParam($param,$paramsData)
	{
		throw new Exception('Uploaded parameter not implemented.');
	}
}