/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2017-2018 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.BasicObjects;

import java.net.URI;
import java.util.ArrayList;

import com.jaamsim.input.ExpError;
import com.jaamsim.input.ExpResult;
import com.jaamsim.input.FileInput;
import com.jaamsim.input.Output;

public class FileToMatrix extends FileToArray {

	ArrayList<ArrayList<ExpResult>> value;

	public FileToMatrix() {
		value = new ArrayList<>();
	}

	@Override
	protected void setValueForURI(URI uri, double simTime) {
		value = getMatrixForURI(uri, simTime);
	}

	@Override
	protected void clearValue() {
		value = new ArrayList<>();
	}

	private ArrayList<ArrayList<ExpResult>> getMatrixForURI(URI uri, double simTime) {
		ArrayList<ArrayList<String>> tokens = FileInput.getTokensFromURI(uri);
		ArrayList<ArrayList<ExpResult>> ret = new ArrayList<>(tokens.size());
		for (ArrayList<String> strRecord : tokens) {
			ArrayList<ExpResult> record = getExpResultList(strRecord, simTime);
			ret.add(record);
		}
		return ret;
	}

	/**
	 * Sets the data for the FileToMatrix directly from a Java data structure, without the use
	 * of the DataFile input which can be left blank. The matrix input can contain the following
	 * Java classes and their sub-classes: Double, Integer, String, Entity, List, Map, and Array.
	 * @param matrix - List of lists of Java objects containing the input data.
	 * @throws ExpError
	 */
	public void setValue(ArrayList<ArrayList<Object>> matrix) throws ExpError {
		ArrayList<ArrayList<ExpResult>> temp = new ArrayList<>(matrix.size());
		for (ArrayList<Object> row : matrix) {
			ArrayList<ExpResult> resRow = getExpResultList(row);
			temp.add(resRow);
		}
		value = temp;
	}

	@Output(name = "Value",
	 description = "A matrix containing the data from the input file.",
	    sequence = 1)
	public ArrayList<ArrayList<ExpResult>> getValue(double simTime) {
		return value;
	}

}

@@ -729,16 +729,57 @@
 yList;%0A%0A
+import com.jaamsim.input.ExpCollections;%0A
 import c
@@ -909,16 +909,60 @@
 .Output;
+%0Aimport com.jaamsim.units.DimensionlessUnit;
 %0A%0Apublic
@@ -1005,36 +1005,16 @@
 ray %7B%0A%0A%09
-ArrayList%3CArrayList%3C
 ExpResul
@@ -1014,18 +1014,16 @@
 xpResult
-%3E%3E
  value;%0A
@@ -1050,39 +1050,26 @@
 x() %7B%0A%09%09
-value = new ArrayList%3C%3E
+clearValue
 ();%0A%09%7D%0A%0A
@@ -1230,68 +1230,145 @@
 %7B%0A%09%09
-value = new ArrayList%3C%3E();%0A%09%7D%0A%0A%09private ArrayList%3CArrayList%3C
+ArrayList%3CExpResult%3E resList = new ArrayList%3C%3E();%0A%09%09value = ExpCollections.getCollection(resList, DimensionlessUnit.class);%0A%09%7D%0A%0A%09private 
 ExpR
@@ -1368,26 +1368,24 @@
 te ExpResult
-%3E%3E
  getMatrixFo
@@ -1491,26 +1491,16 @@
 uri);%0A%09%09
-ArrayList%3C
 ArrayLis
@@ -1511,17 +1511,16 @@
 pResult%3E
-%3E
  ret = n
@@ -1675,41 +1675,181 @@
 %0A%09%09%09
-ret.add(record);%0A%09%09%7D%0A%09%09return ret
+ExpResult colRow = ExpCollections.getCollection(record, DimensionlessUnit.class);%0A%09%09%09ret.add(colRow);%0A%09%09%7D%0A%09%09return ExpCollections.getCollection(ret, DimensionlessUnit.class)
 ;%0A%09%7D
@@ -2314,34 +2314,24 @@
 xpError %7B%0A%09%09
-ArrayList%3C
 ArrayList%3CEx
@@ -2338,17 +2338,16 @@
 pResult%3E
-%3E
  temp = 
@@ -2475,16 +2475,101 @@
 t(row);%0A
+%09%09%09ExpResult colRow = ExpCollections.getCollection(resRow, DimensionlessUnit.class);%0A
 %09%09%09temp.
@@ -2572,19 +2572,19 @@
 emp.add(
-res
+col
 Row);%0A%09%09
@@ -2595,20 +2595,75 @@
 value = 
-temp
+ExpCollections.getCollection(temp, DimensionlessUnit.class)
 ;%0A%09%7D%0A%0A%09@
@@ -2781,36 +2781,16 @@
 %09public 
-ArrayList%3CArrayList%3C
 ExpResul
@@ -2790,18 +2790,16 @@
 xpResult
-%3E%3E
  getValu

convert FileToMatrix to return an ExpResult