Author: echatellier Date: 2011-05-30 12:11:56 +0200 (Mon, 30 May 2011) New Revision: 368 Url: http://nuiton.org/repositories/revision/nuiton-matrix/368 Log: #1549 : Add import/export support for matrix with dimension > 2 Added: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/SemanticMapper.java Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixException.java trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/ImportExportMatrixTest.java Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java 2011-05-30 10:10:30 UTC (rev 367) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/AbstractMatrixND.java 2011-05-30 10:11:56 UTC (rev 368) @@ -31,10 +31,13 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; import org.apache.commons.collections.primitives.ArrayIntList; +import org.apache.commons.collections.primitives.IntList; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.logging.Log; @@ -1096,7 +1099,7 @@ @Override @Deprecated public boolean isSupportedCSV() { - return getDimCount() <= 2; + return true; } /** @@ -1108,6 +1111,18 @@ */ @Override public void importCSV(Reader reader, int[] origin) throws IOException { + + // test file format + reader.mark(1); + char firstChar = (char)reader.read(); + reader.reset(); + if (firstChar == '[') { + importCSVND(reader, origin); + return; + } + + // if run here, it's a normal 1D or 2D import + // as before 2.2 version int rowsCount = 0; List<Double> row = new ArrayList<Double>(); StringBuffer number = new StringBuffer(20); @@ -1138,7 +1153,7 @@ number.setLength(0); if (!row.isEmpty()) { - if (getDim().length == 1) { + if (getDimCount() == 1) { int columnNumber = origin[0]; for (Double value : row) { if (columnNumber < getDim(0)) { @@ -1171,6 +1186,161 @@ } /** + * Import CSV file defined in Matrix ND format. + * + * @param reader reader containing content + * @param origin not used + * @throws IOException + */ + protected void importCSVND(Reader reader, int[] origin) throws IOException { + + int c = -1; + StringBuffer number = new StringBuffer(20); + IntList coordinates = new ArrayIntList(); + + // read dimension + reader.read(); // skip [ + while ((c = reader.read()) != -1) { + if (c == ' ') { + // skip space + } else if (c == ',' || c == ']') { + if (NUMBER.matcher(number.toString()).matches()) { + int coord = Integer.parseInt(number.toString()); + coordinates.add(coord); + } + number.setLength(0); + + if (c == ']') { + break; + } + } else { + number.append((char) c); + } + } + int[] dimensions = coordinates.toArray(); + coordinates.clear(); + // / read dimension + + // read defaut value + while ((c = reader.read()) != -1) { + if (c == ',' || c == ' ') { + // skip + } + else if (c == '\n' || c == '\r') { + break; + } + else { + number.append((char) c); + } + } + double defaultValue = 0.0; + if (NUMBER.matcher(number.toString()).matches()) { + defaultValue = Double.parseDouble(number.toString()); + MatrixHelper.fill(this, defaultValue); + } + number.setLength(0); + // / read default value + + List[] semantics = new List[dimensions.length]; + for (int indexDim = 0 ; indexDim < dimensions.length ; indexDim++) { + List dimension = importCSVNDReadDimension(reader); + if (dimension != null && dimension.size() != dimensions[indexDim]) { + throw new MatrixException(String.format("Semantics %d count not equals to semantics dimension, excepted %d, got %d", + indexDim, dimensions[indexDim], dimension.size())); + } + semantics[indexDim] = dimension; + } + + if (ArrayUtils.contains(semantics, null)) { + throw new MatrixException("Wrong semantics definition : " + Arrays.toString(semantics)); + } + MatrixND matrix = MatrixFactory.getInstance().create(semantics); + MatrixHelper.fill(matrix, defaultValue); + do { + c = reader.read(); + if (c == ' ') { + // skip space + } else if (c == CSV_SEPARATOR) { + if (NUMBER.matcher(number.toString()).matches()) { + int coord = Integer.parseInt(number.toString()); + coordinates.add(coord); + } + number.setLength(0); + } else if (c == -1 || c == '\n' || c == '\r' || c == -1) { + // is line return or equivalent char because space is already + // skiped + // or end of stream + + // at end of line, we must see if the leave number* + Double val = null; + if (NUMBER.matcher(number.toString()).matches()) { + val = Double.valueOf(number.toString()); + } + number.setLength(0); + + if (!coordinates.isEmpty()) { + int[] coords = coordinates.toArray(); + matrix.setValue(coords, val); + coordinates.clear(); + } + } else { + number.append((char) c); + } + } while (c != -1); + + // finally paste loaded matrix into this + paste(matrix); + } + + /** + * Read a line and convert line to semantic value. + * + * Use: + * - mapper to convert semantics values + * - return null if line is empty + * + * @param reader reader to read + * @return semantics for readed line + * @throws IOException + */ + protected List importCSVNDReadDimension(Reader reader) throws IOException { + + StringBuffer buffer = new StringBuffer(); + int c = -1; + while ((c = reader.read()) != -1) { + if (c == '\n') { + break; + } + else { + buffer.append((char)c); + } + } + + // read must be in form: + // type : PK1, PK2, PK3... + List sems = null; + if (buffer.length() > 0) { + sems = new ArrayList(); + // get type + int twodIndex = buffer.indexOf(":"); + if (twodIndex == -1) { + throw new MatrixException("Can't parse semantics line as 'Type: ids'"); + } + String type = buffer.substring(0, twodIndex).trim(); + Class typeClass = MatrixFactory.getSemanticMapper().getType(type); + + // get semantics value + String semanticsStrings = buffer.substring(twodIndex +1).trim(); + String[] semanticsPKs = semanticsStrings.split("\\s*,\\s*"); + for (String semanticsPK : semanticsPKs) { + Object value = MatrixFactory.getSemanticMapper().getValue(typeClass, semanticsPK); + sems.add(value); + } + } + return sems; + } + + /** * Export dans un writer au format CSV de la matrice * * @param writer le writer ou copier la matrice @@ -1178,17 +1348,29 @@ * writer */ @Override - public void exportCSV(Writer writer, boolean withSemantics) + public void exportCSV(Writer writer, boolean withSemantics) throws IOException { + if (getDimCount() <= 2) { + exportCSV2D(writer, withSemantics); + } + else { + exportCSVND(writer, withSemantics); + } + } + + /** + * Export dans un writer au format CSV de la matrice + * + * @param writer le writer ou copier la matrice + * @param withSemantics export ou pas des semantiques de la matrice dans le + * writer + */ + protected void exportCSV2D(Writer writer, boolean withSemantics) throws IOException { int dimsCount = getDimCount(); int rowsCount = dimsCount == 1 ? 1 : getDim(0); int columnsCount = dimsCount == 1 ? getDim(0) : getDim(1); int[] coordinates; - if (!isSupportedCSV()) { - throw new UnsupportedOperationException(); - } - /* Création de l'entete */ if (withSemantics) { /* Recuperation de la liste sur la bonne dimenssion */ @@ -1217,5 +1399,53 @@ writer.append("\n"); } } + + /** + * Export dans un writer au format CSV de la matrice + * + * @param writer le writer ou copier la matrice + * @param withSemantics export ou pas des semantiques de la matrice dans le + * writer + */ + protected void exportCSVND(Writer writer, boolean withSemantics) + throws IOException { + + SemanticMapper mapper = MatrixFactory.getSemanticMapper(); + // add meta + writer.append(Arrays.toString(getDim())).append("\n"); + for (List<?> semantic : getSemantics()) { + if (semantic != null) { + Object first = semantic.get(0); + if (first != null) { + writer.append(mapper.getTypeName(first)); + writer.append(':'); + Iterator itValue = semantic.iterator(); + while (itValue.hasNext()) { + Object value = itValue.next(); + writer.append(mapper.getValueId(value)); + if (itValue.hasNext()) { + writer.append(','); + } + } + } + } + writer.append('\n'); + } + + // add data + MatrixIterator matrixIterator = iterator(); + while (matrixIterator.hasNext()) { + matrixIterator.next(); + int[] coordinates = matrixIterator.getCoordinates(); + for (int i = 0 ; i < coordinates.length ; ++i) { + writer.append(String.valueOf(coordinates[i])); + writer.append(CSV_SEPARATOR); + } + // add data value + writer.append(String.valueOf(matrixIterator.getValue())); + writer.append('\n'); + } + } + } // AbstractMatrixND Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixException.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixException.java 2011-05-30 10:10:30 UTC (rev 367) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixException.java 2011-05-30 10:11:56 UTC (rev 368) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2004 - 2010 CodeLutin + * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -23,6 +23,8 @@ * #L% */ +package org.nuiton.math.matrix; + /** * MatriceException.java * @@ -34,8 +36,6 @@ * Mise a jour: $Date$ * par : $Author$ */ -package org.nuiton.math.matrix; - public class MatrixException extends RuntimeException { /** serialVersionUID. */ Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java 2011-05-30 10:10:30 UTC (rev 367) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/MatrixFactory.java 2011-05-30 10:11:56 UTC (rev 368) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2004 - 2010 CodeLutin + * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -48,6 +48,8 @@ protected Class<?> vectorClass = null; + protected static SemanticMapper defaultSemanticMapper = new SemanticMapper();; + protected MatrixFactory(Class<?> vectorClass) { this.vectorClass = vectorClass; } @@ -60,6 +62,14 @@ return defaultVectorClass; } + public static void setSemanticMapper(SemanticMapper semanticMapper) { + defaultSemanticMapper = semanticMapper; + } + + public static SemanticMapper getSemanticMapper() { + return defaultSemanticMapper; + } + /** * Retourne une factory utilisant vectorClass comme classe de base a * l'implantation des matrices. Added: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/SemanticMapper.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/SemanticMapper.java (rev 0) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/SemanticMapper.java 2011-05-30 10:11:56 UTC (rev 368) @@ -0,0 +1,86 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.math.matrix; + +/** + * Mapper used during import/export to map CSV file semantics to + * real semantics value depending on execution context. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class SemanticMapper { + + /** + * Return class for type identified by typeName. + * + * For example : "Population" can return "fr.ifremer.enetities.Population.class" + * + * Return {@code String} by default. + * + * @param typeName type to get class. + * @return type for typeId + */ + public Class getType(String typeName) { + return String.class; + } + + /** + * Return value identified by valueId and type {@code type}. + * + * Return {@code valueId} by default; + * + * @param type + * @param valueId + * @return value identified by {valueId} + */ + public Object getValue(Class type, String valueId) { + return valueId; + } + + /** + * Return type name for given type. + * + * @param type type to get name + * @return type name + */ + public String getTypeName(Object type) { + return type.getClass().getName(); + } + + /** + * Get value id for value. + * + * @param value value to get id + * @return value id + */ + public String getValueId(Object value) { + return value.toString(); + } +} Property changes on: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/SemanticMapper.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/ImportExportMatrixTest.java =================================================================== --- trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/ImportExportMatrixTest.java 2011-05-30 10:10:30 UTC (rev 367) +++ trunk/nuiton-matrix/src/test/java/org/nuiton/math/matrix/ImportExportMatrixTest.java 2011-05-30 10:11:56 UTC (rev 368) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2004 - 2010 CodeLutin + * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -32,9 +32,10 @@ import java.util.List; import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; +import org.junit.BeforeClass; import org.junit.Test; +import org.nuiton.i18n.I18n; +import org.nuiton.i18n.init.ClassPathI18nInitializer; /** * Test de l'import et export CSV. @@ -47,16 +48,41 @@ */ public class ImportExportMatrixTest { - protected MatrixND mat1D; - protected MatrixND mat2D; - protected MatrixND mat2DSemantics; + /** One dimension matrix. */ + protected static MatrixND mat1D; - public MatrixFactory getFactory() throws Exception { + /** Two dimension matrix. */ + protected static MatrixND mat2D; + + /** Two dimension matrix with semantics. */ + protected static MatrixND mat2DSemantics; + + /** Three dimension matrix. */ + protected static MatrixND mat3D; + + /** Four dimension matrix. */ + protected static MatrixND mat4D; + + /** + * Return factory configured with double. + * + * @return MatrixFactory + */ + public static MatrixFactory getFactory() { return MatrixFactory.getInstance(DoubleVector.class); } - @Before - public void setUp() throws Exception { + /** + * Setup all matrix with value. + * + * @throws Exception + */ + @BeforeClass + public static void init() throws Exception { + + I18n.init(new ClassPathI18nInitializer(), null); + + // One dimension matrix. mat1D = new MatrixNDImpl(getFactory(), new int[] { 10 }); mat1D.setValue(new int[] { 0 }, 0); mat1D.setValue(new int[] { 1 }, 1); @@ -64,6 +90,7 @@ mat1D.setValue(new int[] { 3 }, 3); mat1D.setValue(new int[] { 4 }, -4.0E-7); + // Two dimension matrix. mat2D = new MatrixNDImpl(getFactory(), new int[] { 20, 10 }); mat2D.setValue(new int[] { 0, 0 }, 0); mat2D.setValue(new int[] { 0, 1 }, 1); @@ -76,13 +103,49 @@ mat2D.setValue(new int[] { 1, 8 }, 4); mat2D.setValue(new int[] { 2, 9 }, -4.0E-7); - List sem1 = Arrays - .asList(new String[] { "toto", "titi", "tutu", "trtr" }); + // Two dimension matrix with semantics. + List sem1 = Arrays.asList(new String[] { "toto", "titi", "tutu", "trtr" }); List sem2 = Arrays.asList(new String[] { "tata", "tete", "tyty" }); mat2DSemantics = new MatrixNDImpl(getFactory(), "name", new List[] { sem1, sem2 }, new String[] { "dim1", "dim2" }); + + // Three dimension matrix. + mat3D = new MatrixNDImpl(getFactory(), new int[] { 4, 5, 4 }); + mat3D.setSemantic(0, Arrays.asList(new String[]{"2009","2010","2011","2012"})); + mat3D.setSemantic(1, Arrays.asList(new String[]{"Nantes","Lille","Paris","Lyon","Marseille"})); + mat3D.setSemantic(2, Arrays.asList(new String[]{"Informatique","Administration","Developpement","Recherche"})); + mat3D.setValue(new int[] { 0, 0, 0 }, 0); + mat3D.setValue(new int[] { 0, 0, 1 }, 1); + mat3D.setValue(new int[] { 0, 2, 0 }, 2); + mat3D.setValue(new int[] { 3, 0, 0 }, -4.0E-7); + mat3D.setValue(new int[] { 1, 1, 1 }, 42.0); + mat3D.setValue(new int[] { 1, 2, 3 }, 4); + mat3D.setValue(new int[] { 3, 2, 1 }, 4); + mat3D.setValue(new int[] { 2, 2, 2 }, 2); + mat3D.setValue(new int[] { 3, 4, 3 }, 0.004); + + // Four dimension matrix. + mat4D = new MatrixNDImpl(getFactory(), new int[] { 4, 5, 4, 3}); + mat4D.setSemantic(0, Arrays.asList(new String[]{"2009","2010","2011","2012"})); + mat4D.setSemantic(1, Arrays.asList(new String[]{"Nantes","Lille","Paris","Lyon","Marseille"})); + mat4D.setSemantic(2, Arrays.asList(new String[]{"Informatique","Administration","Developpement","Recherche"})); + mat4D.setSemantic(3, Arrays.asList(new String[]{"Test","Beta","Stable"})); + mat4D.setValue(new int[] { 0, 0, 0, 0 }, 0); + mat4D.setValue(new int[] { 0, 0, 0, 1 }, 1); + mat4D.setValue(new int[] { 0, 2, 0, 2 }, 2); + mat4D.setValue(new int[] { 3, 0, 0, 1 }, -4.0E-7); + mat4D.setValue(new int[] { 1, 1, 1, 1 }, 42.0); + mat4D.setValue(new int[] { 1, 2, 3, 2 }, 4); + mat4D.setValue(new int[] { 3, 2, 1, 0 }, 4); + mat4D.setValue(new int[] { 2, 2, 2, 2 }, 2); + mat4D.setValue(new int[] { 3, 4, 3, 2 }, 0.004); } + /** + * Test d'import d'une matrice 2D. + * + * @throws IOException + */ @Test public void testImport() throws IOException { String test = "5.0E-7;1.0;2.0;3.0;4.0;4.0;4.0;4.0;0.3;0.0\n" @@ -104,24 +167,38 @@ Assert.assertEquals(mat2D.getValue(2, 9), 4.0, 0); } + /** + * Test les sorties des exports suivit des export des imports. + * + * @throws IOException + */ @Test public void testExport() throws IOException { - testExport(mat1D, false); - testExport(mat2D, false); + exportCompare(mat1D, false); + exportCompare(mat2D, false); - testExport(mat1D, true); - testExport(mat2D, true); + exportCompare(mat1D, true); + exportCompare(mat2D, true); - testExport(mat2DSemantics, false); - testExport(mat2DSemantics, true); + exportCompare(mat2DSemantics, false); + exportCompare(mat2DSemantics, true); + + exportCompare(mat3D, true); + exportCompare(mat4D, false); } - @Ignore - protected void testExport(MatrixND matrixND, boolean withSemantics) + /** + * L'export de la matrice et l'export apres l'import doivent + * redonner le même résultat. + * + * @param matrixND matrix to test + * @param withSemantics with semantics + * @throws IOException + */ + protected void exportCompare(MatrixND matrixND, boolean withSemantics) throws IOException { StringWriter writer = new StringWriter(); matrixND.exportCSV(writer, withSemantics); - StringReader writerToReader = new StringReader(writer.toString()); matrixND.importCSV(writerToReader, new int[] { 0, 0 }); @@ -131,13 +208,133 @@ Assert.assertEquals(writer.toString(), readerToWriter.toString()); } + /** + * Test que l'import d'une matrice à partir d'une chaine produit + * l'import voulu. + * @throws IOException + */ @Test - public void testSupport() throws Exception { - Assert.assertEquals(true, new MatrixNDImpl(getFactory(), new int[] { 1 }) - .isSupportedCSV()); - Assert.assertEquals(true, new MatrixNDImpl(getFactory(), new int[] { 2, 2 }) - .isSupportedCSV()); - Assert.assertEquals(false, new MatrixNDImpl(getFactory(), - new int[] { 3, 3, 3 }).isSupportedCSV()); + public void testMatrix2DImport() throws IOException { + + String matrix3D = "[2, 2, 2]\n" + + "java.lang.String:2009,2010\n" + + "java.lang.String:Nantes,Lille\n" + + "java.lang.String:Informatique,Administration\n" + + "0;0;0;0.0\n" + + "0;0;1;1.0\n" + + "0;1;1;4.0\n" + + "1;0;0;-4.0E-7\n" + + "1;0;1;2.0\n" + + "1;1;0;4.0\n" + + "1;1;1;42.0"; + + MatrixND m = MatrixFactory.getInstance().create(new int[]{2, 2, 2}); + m.importCSV(new StringReader(matrix3D), null); + // dim + Assert.assertArrayEquals(new int[]{2, 2, 2}, m.getDim()); + // default value + Assert.assertEquals(0.0, m.getValue(0, 1, 0), 0.0001); + // some test + Assert.assertEquals(-4.0e-7, m.getValue(1, 0, 0), 0.0001); + Assert.assertEquals(42, m.getValue(1, 1, 1), 0.0001); } + + /** + * Test que l'import d'une matrice à partir d'une chaine produit + * l'import voulu avec une valeur par defaut. + * + * @throws IOException + */ + @Test + public void testMatrix2DImportDefaultValue() throws IOException { + + String matrix3D = "[2, 2, 2] , 3.14159265 \n" + + "java.lang.String:2009,2010\n" + + "java.lang.String:Nantes,Lille\n" + + "java.lang.String:Informatique,Administration\n" + + "0;0;0;0.0\n" + + "0;0;1;1.0\n" + + "0;1;1;4.0\n" + + "1;0;0;-4.0E-7\n" + + "1;0;1;2.0\n" + + "1;1;0;4.0\n" + + "1;1;1;42.0"; + + MatrixND m = MatrixFactory.getInstance().create(new int[]{2, 2, 2}); + m.importCSV(new StringReader(matrix3D), null); + // default value + Assert.assertEquals(Math.PI, m.getValue(0, 1, 0), 0.00001); + } + + /** + * Test que l'import d'une matrice à partir d'une chaine produit + * l'import voulu avec error de nmbre de valeur de semantiques. + * + * @throws IOException + */ + @Test(expected=MatrixException.class) + public void testMatrix2DImportSemError() throws IOException { + + String matrix3D = "[2, 2, 2] , 3.14159265 \n" + + "java.lang.String:2009,2010\n" + + "java.lang.String:Nantes\n" + + "java.lang.String:Informatique,Administration\n" + + "0;0;0;0.0\n" + + "0;0;1;1.0\n" + + "0;1;1;4.0\n" + + "1;0;0;-4.0E-7\n" + + "1;0;1;2.0\n" + + "1;1;0;4.0\n" + + "1;1;1;42.0"; + + MatrixND m = MatrixFactory.getInstance().create(new int[]{2, 2, 2}); + m.importCSV(new StringReader(matrix3D), null); + } + + /** + * Test que l'import d'une matrice à partir d'une chaine produit + * l'import voulu avec error semantics vide. + * + * @throws IOException + */ + @Test(expected=MatrixException.class) + public void testMatrix2DImportSemError2() throws IOException { + + String matrix3D = "[2, 2, 2]\n" + + "java.lang.String:2009,2010\n" + + "java.lang.String:Nantes, Lille\n" + + "\n" + + "0;0;0;0.0\n" + + "0;0;1;1.0\n" + + "0;1;1;4.0\n" + + "1;0;0;-4.0E-7\n" + + "1;0;1;2.0\n" + + "1;1;0;4.0\n" + + "1;1;1;42.0"; + + MatrixND m = MatrixFactory.getInstance().create(new int[]{2, 2, 2}); + m.importCSV(new StringReader(matrix3D), null); + } + + /** + * Test que l'import d'une matrice à partir d'une chaine produit + * l'import voulu avec 0 semantics. + * + * @throws IOException + */ + @Test(expected=MatrixException.class) + public void testMatrix2DImportSemError3() throws IOException { + + String matrix3D = "[2, 2, 2] , 3.14159265 \n" + + "0;0;0;0.0\n" + + "0;0;1;1.0\n" + + "0;1;1;4.0\n" + + "1;0;0;-4.0E-7\n" + + "1;0;1;2.0\n" + + "1;1;0;4.0\n" + + "1;1;1;42.0"; + + MatrixND m = MatrixFactory.getInstance().create(new int[]{2, 2, 2}); + m.importCSV(new StringReader(matrix3D), null); + } }