/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2014 Ausenco Engineering Canada Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.jaamsim.input;

import java.util.ArrayList;

import com.jaamsim.units.DimensionlessUnit;
import com.jaamsim.units.Unit;
import com.sandwell.JavaSimulation.Entity;
import com.sandwell.JavaSimulation.Input;
import com.sandwell.JavaSimulation.InputErrorException;
import com.sandwell.JavaSimulation.ListInput;
import com.sandwell.JavaSimulation.StringVector;

/**
 * AttributeDefinitionListInput is an object for parsing inputs consisting of a list of
 * Attribute definitions using the syntax:
 * Entity AttributeDefinitionList { { AttibuteName1 Value1 Unit1 } { AttibuteName2 Value2 Unit2 } ... }
 * @author Harry King
 */
public class AttributeDefinitionListInput extends ListInput<ArrayList<AttributeHandle>> {

	private Entity ent;

	public AttributeDefinitionListInput(Entity e, String key, String cat, ArrayList<AttributeHandle> def) {
		super(key, cat, def);
		ent = e;
	}

	@Override
	public void parse(StringVector input) throws InputErrorException {

		// Divide up the inputs by the inner braces
		ArrayList<StringVector> splitData = InputAgent.splitStringVectorByBraces(input);
		ArrayList<AttributeHandle> temp = new ArrayList<AttributeHandle>(splitData.size());

		// Parse the inputs within each inner brace
		for (int i = 0; i < splitData.size(); i++) {
			try {
				StringVector data = splitData.get(i);
				Input.assertCount(data, 2, 3);

				// Parse the attribute name
				String name = data.get(0);
				if (OutputHandle.hasOutput(ent.getClass(), name)) {
					throw new InputErrorException("Attribute name is the same as existing output name: %s", name);
				}

				// Parse the unit type
				double factor = 1.0;
				Class<? extends Unit> unitType = DimensionlessUnit.class;
				if (data.size() == 3) {
					Unit unit = Input.parseUnits(data.get(2));
					unitType = unit.getClass();
					factor = unit.getConversionFactorToSI();
				}

				// Parse the initial value
				double val;
				try {
					val = factor * Double.valueOf(data.get(1));
				} catch (Exception e) {
					throw new InputErrorException(INP_ERR_DOUBLE, data.get(1));
				}

				// Save the data for this attribute
				AttributeHandle h = new AttributeHandle(ent, name);
				h.setUnitType(unitType);
				h.setInitialValue(val);
				h.setValue(val);
				temp.add(h);

			} catch (InputErrorException e) {
				throw new InputErrorException(INP_ERR_ELEMENT, i, e.getMessage());
			}
		}

		// Save the data for each attribute
		value = temp;
	}

}

@@ -774,50 +774,8 @@
 ty;%0A
-import com.sandwell.JavaSimulation.Input;%0A
 impo
@@ -2992,14 +2992,1055 @@
 = temp;%0A
+%09%09setValueString(getInputString(value));%0A%09%7D%0A%0A%09@Override%0A%09public String getDefaultString() %7B%0A%09%09if (defValue == null %7C%7C defValue.isEmpty()) return NO_VALUE;%0A%09%09return this.getInputString(defValue);%0A%09%7D%0A%0A%09public String getInputString(ArrayList%3CAttributeHandle%3E handleList) %7B%0A%0A%09%09StringBuilder tmp = new StringBuilder();%0A%09%09for (int i = 0; i %3C handleList.size(); i++) %7B%0A%09%09%09if (i %3E 0) tmp.append(SEPARATOR);%0A%09%09%09AttributeHandle h = handleList.get(i);%0A%09%09%09tmp.append(%22%7B %22);%0A%09%09%09tmp.append(h.getName());%0A%09%09%09tmp.append(SEPARATOR);%0A%0A%09%09%09double val = h.getInitialValue();%0A%09%09%09String unitString = Unit.getSIUnit(h.getUnitType());%0A%0A%09%09%09// Check for a preferred unit%0A%09%09%09Unit unit = Unit.getPreferredUnit(h.getUnitType());%0A%09%09%09if (unit != null) %7B%0A%09%09%09%09unitString = unit.toString();%0A%09%09%09%09val = h.getValueAsDouble(0.0d, 0.0d, unit);%0A%09%09%09%7D%0A%09%09%09tmp.append(val);%0A%0A%09%09%09// Print the unit unless it is dimensionless%0A%09%09%09if (h.getUnitType() != DimensionlessUnit.class) %7B%0A%09%09%09%09tmp.append(SEPARATOR);%0A%09%09%09%09tmp.append(unitString);%0A%09%09%09%7D%0A%09%09%09tmp.append(%22 %7D%22);%0A%09%09%7D%0A%09%09return tmp.toString();%0A
 %09%7D%0A%0A%7D%0A

Add getValueString method for AttributionDefinitionListInput