/*
 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.
 *
 * EXPORT CONTROLLED
 * ECCN#   EAR99
 * Warning: This document contains technical data which is controlled under the
 * Bureau of Industry & Security's Export Administration Regulations and may not be
 * exported or re-exported without authorization by the Bureau of Industry & Security.
 * Diversion contrary to US law is prohibited.
 *
 * (c) 2011 United Technologies Corp.
 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,
 * including any software or models in source or binary form, as well as any drawings, 
 * specifications, and documentation (collectively "the Data"), to deal in the Data without 
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Data.
 *
 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE
 * USE OR OTHER DEALINGS IN THE DATA.
*/

package proxy;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.jdom.Element;

import exception.ViewException;

/**
 * Encapsulte an architecture with result data. The XML for such a class looks like
 * 
  <?xml version="1.0" encoding="UTF-8" ?> 
- <architecture id="1" platform="engine" level="0">
-   <device id="1" name="Core_Diffuser">
      <var name="cost" value="788716.0" type="real" /> 
    </device>
-   <device id="2" name="Fan_Diffuser">
      <var name="cost" value="390177.0" type="real" /> 
    </device>
    . . .
  </architecture>
 */
public class ArchitectureResult {
	private int id;
	private Map<String, String> results;

	/**
	 * Constructor
	 *
	 * @param arch The top level architecture element
	 * @throws ViewException if the XML is improperly formatted
	 */
	public ArchitectureResult(Element arch) throws ViewException {
		results = new HashMap<String, String>();
		
		if (! arch.getName().equals("architecture"))
			throw new ViewException("Unexpected error: \"%s\" is not an architecture element",
					                     arch.getName());
		String idname = arch.getAttributeValue("id");
		if (idname == null)
			throw new ViewException("architecture element is missing an id");
		try {
			id = Integer.parseInt(idname);
		} catch (NumberFormatException nfe) {
			throw new ViewException(nfe.getMessage());
		}
		
		// Grab the var's of all devices. Use the device name to qualify the var name
		for (Object dchild : arch.getChildren("device")) {
			Element device = (Element) dchild;
			String  dname  = device.getAttributeValue("name");
			if (dname == null) dname  = "";
			else               dname += ".";
			
			// Process each var child of the device
			for (Object vchild : device.getChildren("var")) {
				Element var   = (Element) vchild;
				String  name  = var.getAttributeValue("name");
				String  value = var.getAttributeValue("value");
				if (name == null  ||  value == null)
					throw new ViewException("Missing var name or value for a \"%s\" device", dname);
				results.put(dname+name, value);
			} // vchild
		} // dchild
	}

	/**
	 * Report the architecture id
	 * @return The id
	 */
	public int getId() {
		return id;
	}

	/**
	 * Report the variable names
	 * @return A list of variable names in the architecture
	 */
	public Set<String> getAllVariableNames() {
		return results.keySet();
	}
	
	/**
	 * Report a variable value
	 * @param varname The variable name
	 * @return        The value or 0 if none
	 */
	public String getValue(String varname) {
		return (results.containsKey(varname))? results.get(varname) : "0";
	}
}
