This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit c6d5f4b65fc589a715c82d3c643a85dacbc0d1ac Author: Gilles Sadowski <gillese...@gmail.com> AuthorDate: Thu Dec 30 16:10:46 2021 +0100 MATH-1594: Remove "Serializable". --- .../apache/commons/math4/neuralnet/Network.java | 196 +++++++-------------- .../org/apache/commons/math4/neuralnet/Neuron.java | 57 +----- .../neuralnet/internal/NeuralNetException.java | 4 + .../commons/math4/neuralnet/oned/NeuronString.java | 64 +------ .../math4/neuralnet/twod/NeuronSquareMesh2D.java | 90 +--------- .../commons/math4/neuralnet/NetworkTest.java | 39 ---- .../apache/commons/math4/neuralnet/NeuronTest.java | 25 --- .../math4/neuralnet/oned/NeuronStringTest.java | 36 ---- .../neuralnet/twod/NeuronSquareMesh2DTest.java | 39 ---- 9 files changed, 75 insertions(+), 475 deletions(-) diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java index 19101a4..946d85f 100644 --- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java +++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Network.java @@ -17,8 +17,6 @@ package org.apache.commons.math4.neuralnet; -import java.io.Serializable; -import java.io.ObjectInputStream; import java.util.NoSuchElementException; import java.util.List; import java.util.ArrayList; @@ -45,10 +43,7 @@ import org.apache.commons.math4.neuralnet.internal.NeuralNetException; * @since 3.3 */ public class Network - implements Iterable<Neuron>, - Serializable { - /** Serializable. */ - private static final long serialVersionUID = 20130207L; + implements Iterable<Neuron> { /** Neurons. */ private final ConcurrentHashMap<Long, Neuron> neuronMap = new ConcurrentHashMap<>(); @@ -65,11 +60,7 @@ public class Network * to the increasing order of their identifier. */ public static class NeuronIdentifierComparator - implements Comparator<Neuron>, - Serializable { - /** Version identifier. */ - private static final long serialVersionUID = 20130207L; - + implements Comparator<Neuron> { /** {@inheritDoc} */ @Override public int compare(Neuron a, @@ -82,59 +73,59 @@ public class Network } /** - * Constructor with restricted access, solely used for deserialization. + * @param firstId Identifier of the first neuron that will be added + * to this network. + * @param featureSize Size of the neuron's features. + */ + public Network(long firstId, + int featureSize) { + this.nextId = new AtomicLong(firstId); + this.featureSize = featureSize; + } + + /** + * Builds a network from a list of neurons and their neighbours. * - * @param nextId Next available identifier. * @param featureSize Number of features. - * @param neuronList Neurons. + * @param idList List of neuron identifiers. + * @param featureList List of neuron features. * @param neighbourIdList Links associated to each of the neurons in - * {@code neuronList}. - * @throws IllegalStateException if an inconsistency is detected - * (which probably means that the serialized form has been corrupted). + * {@code idList}. + * @throws IllegalArgumentException if an inconsistency is detected. */ - Network(long nextId, - int featureSize, - Neuron[] neuronList, - long[][] neighbourIdList) { - final int numNeurons = neuronList.length; - if (numNeurons != neighbourIdList.length) { - throw new IllegalStateException(); + public static Network from(int featureSize, + long[] idList, + double[][] featureList, + long[][] neighbourIdList) { + final int numNeurons = idList.length; + if (idList.length != featureList.length) { + throw new NeuralNetException(NeuralNetException.SIZE_MISMATCH, + idList.length, featureList.length); } + if (idList.length != neighbourIdList.length) { + throw new NeuralNetException(NeuralNetException.SIZE_MISMATCH, + idList.length, neighbourIdList.length); + } + + final Network net = new Network(Long.MIN_VALUE, featureSize); for (int i = 0; i < numNeurons; i++) { - final Neuron n = neuronList[i]; - final long id = n.getIdentifier(); - if (id >= nextId) { - throw new IllegalStateException(); - } - neuronMap.put(id, n); - linkMap.put(id, new HashSet<Long>()); + final long id = idList[i]; + net.createNeuron(id, featureList[i]); } for (int i = 0; i < numNeurons; i++) { - final long aId = neuronList[i].getIdentifier(); - final Set<Long> aLinks = linkMap.get(aId); - for (final Long bId : neighbourIdList[i]) { - if (neuronMap.get(bId) == null) { - throw new IllegalStateException(); + final Neuron a = net.getNeuron(idList[i]); + for (final long id : neighbourIdList[i]) { + final Neuron b = net.neuronMap.get(id); + if (b == null) { + throw new NeuralNetException(NeuralNetException.ID_NOT_FOUND, id); } - addLinkToLinkSet(aLinks, bId); + net.addLink(a, b); } } - this.nextId = new AtomicLong(nextId); - this.featureSize = featureSize; - } - - /** - * @param initialIdentifier Identifier for the first neuron that - * will be added to this network. - * @param featureSize Size of the neuron's features. - */ - public Network(long initialIdentifier, - int featureSize) { - nextId = new AtomicLong(initialIdentifier); - this.featureSize = featureSize; + return net; } /** @@ -195,14 +186,35 @@ public class Network * {@link #Network(long,int) constructor}). */ public long createNeuron(double[] features) { + return createNeuron(createNextId(), features); + } + + /** + * @param id Identifier. + * @param features Features. + * @return {@¢ode id}. + * @throws IllegalArgumentException if the identifier is already used + * by a neuron that belongs to this network or the features size does + * not match the expected value. + */ + private long createNeuron(long id, + double[] features) { + if (neuronMap.get(id) != null) { + throw new NeuralNetException(NeuralNetException.ID_IN_USE, id); + } + if (features.length != featureSize) { throw new NeuralNetException(NeuralNetException.SIZE_MISMATCH, features.length, featureSize); } - final long id = createNextId(); - neuronMap.put(id, new Neuron(id, features)); + neuronMap.put(id, new Neuron(id, features.clone())); linkMap.put(id, new HashSet<Long>()); + + if (id > nextId.get()) { + nextId.set(id); + } + return id; } @@ -404,84 +416,4 @@ public class Network private Long createNextId() { return nextId.getAndIncrement(); } - - /** - * Prevents proxy bypass. - * - * @param in Input stream. - */ - private void readObject(ObjectInputStream in) { - throw new IllegalStateException(); - } - - /** - * Custom serialization. - * - * @return the proxy instance that will be actually serialized. - */ - private Object writeReplace() { - final Neuron[] neuronList = neuronMap.values().toArray(new Neuron[0]); - final long[][] neighbourIdList = new long[neuronList.length][]; - - for (int i = 0; i < neuronList.length; i++) { - final Collection<Neuron> neighbours = getNeighbours(neuronList[i]); - final long[] neighboursId = new long[neighbours.size()]; - int count = 0; - for (final Neuron n : neighbours) { - neighboursId[count] = n.getIdentifier(); - ++count; - } - neighbourIdList[i] = neighboursId; - } - - return new SerializationProxy(nextId.get(), - featureSize, - neuronList, - neighbourIdList); - } - - /** - * Serialization. - */ - private static class SerializationProxy implements Serializable { - /** Serializable. */ - private static final long serialVersionUID = 20130207L; - /** Next identifier. */ - private final long nextId; - /** Number of features. */ - private final int featureSize; - /** Neurons. */ - private final Neuron[] neuronList; - /** Links. */ - private final long[][] neighbourIdList; - - /** - * @param nextId Next available identifier. - * @param featureSize Number of features. - * @param neuronList Neurons. - * @param neighbourIdList Links associated to each of the neurons in - * {@code neuronList}. - */ - SerializationProxy(long nextId, - int featureSize, - Neuron[] neuronList, - long[][] neighbourIdList) { - this.nextId = nextId; - this.featureSize = featureSize; - this.neuronList = neuronList; - this.neighbourIdList = neighbourIdList; - } - - /** - * Custom serialization. - * - * @return the {@link Network} for which this instance is the proxy. - */ - private Object readResolve() { - return new Network(nextId, - featureSize, - neuronList, - neighbourIdList); - } - } } diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Neuron.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Neuron.java index 2032ca3..174cadd 100644 --- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Neuron.java +++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/Neuron.java @@ -17,8 +17,6 @@ package org.apache.commons.math4.neuralnet; -import java.io.Serializable; -import java.io.ObjectInputStream; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicLong; @@ -32,9 +30,7 @@ import org.apache.commons.math4.neuralnet.internal.NeuralNetException; * * @since 3.3 */ -public class Neuron implements Serializable { - /** Serializable. */ - private static final long serialVersionUID = 20130207L; +public class Neuron { /** Identifier. */ private final long identifier; /** Length of the feature set. */ @@ -219,55 +215,4 @@ public class Neuron implements Serializable { } return true; } - - /** - * Prevents proxy bypass. - * - * @param in Input stream. - */ - private void readObject(ObjectInputStream in) { - throw new IllegalStateException(); - } - - /** - * Custom serialization. - * - * @return the proxy instance that will be actually serialized. - */ - private Object writeReplace() { - return new SerializationProxy(identifier, - features.get()); - } - - /** - * Serialization. - */ - private static class SerializationProxy implements Serializable { - /** Serializable. */ - private static final long serialVersionUID = 20130207L; - /** Features. */ - private final double[] features; - /** Identifier. */ - private final long identifier; - - /** - * @param identifier Identifier. - * @param features Features. - */ - SerializationProxy(long identifier, - double[] features) { - this.identifier = identifier; - this.features = features; - } - - /** - * Custom serialization. - * - * @return the {@link Neuron} for which this instance is the proxy. - */ - private Object readResolve() { - return new Neuron(identifier, - features); - } - } } diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/internal/NeuralNetException.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/internal/NeuralNetException.java index 7e4fe9d..3f73c81 100644 --- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/internal/NeuralNetException.java +++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/internal/NeuralNetException.java @@ -34,6 +34,10 @@ public class NeuralNetException extends IllegalArgumentException { public static final String NO_DATA = "No data"; /** Error message for "size mismatch" condition. */ public static final String SIZE_MISMATCH = "Size mismatch: {0} != {1}"; + /** Error message for "identifier already used" condition. */ + public static final String ID_IN_USE = "Identifier already in use: {0}"; + /** Error message for "identifier not found" condition. */ + public static final String ID_NOT_FOUND = "Identifier not found: {0}"; /** Serializable version identifier. */ private static final long serialVersionUID = 20210515L; diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java index c1cec55..dcef125 100644 --- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java +++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/oned/NeuronString.java @@ -17,9 +17,6 @@ package org.apache.commons.math4.neuralnet.oned; -import java.io.Serializable; -import java.io.ObjectInputStream; - import org.apache.commons.math4.neuralnet.internal.NeuralNetException; import org.apache.commons.math4.neuralnet.FeatureInitializer; import org.apache.commons.math4.neuralnet.Network; @@ -30,9 +27,7 @@ import org.apache.commons.math4.neuralnet.Network; * * @since 3.3 */ -public class NeuronString implements Serializable { - /** Serial version ID. */ - private static final long serialVersionUID = 1L; +public class NeuronString { /** Minimal number of neurons. */ private static final int MIN_NEURONS = 2; /** Underlying network. */ @@ -180,61 +175,4 @@ public class NeuronString implements Serializable { network.addLink(network.getNeuron(size - 1), network.getNeuron(0)); } } - - /** - * Prevents proxy bypass. - * - * @param in Input stream. - */ - private void readObject(ObjectInputStream in) { - throw new IllegalStateException(); - } - - /** - * Custom serialization. - * - * @return the proxy instance that will be actually serialized. - */ - private Object writeReplace() { - final double[][] featuresList = new double[size][]; - for (int i = 0; i < size; i++) { - featuresList[i] = getFeatures(i); - } - - return new SerializationProxy(wrap, - featuresList); - } - - /** - * Serialization. - */ - private static class SerializationProxy implements Serializable { - /** Serializable. */ - private static final long serialVersionUID = 20130226L; - /** Wrap. */ - private final boolean wrap; - /** Neurons' features. */ - private final double[][] featuresList; - - /** - * @param wrap Whether the dimension is wrapped. - * @param featuresList List of neurons features. - * {@code neuronList}. - */ - SerializationProxy(boolean wrap, - double[][] featuresList) { - this.wrap = wrap; - this.featuresList = featuresList; - } - - /** - * Custom serialization. - * - * @return the {@link NeuronString} for which this instance is the proxy. - */ - private Object readResolve() { - return new NeuronString(wrap, - featuresList); - } - } } diff --git a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java index ce666c1..4a6b185 100644 --- a/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java +++ b/commons-math-neuralnet/src/main/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2D.java @@ -21,8 +21,6 @@ import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.util.Collection; -import java.io.Serializable; -import java.io.ObjectInputStream; import org.apache.commons.math4.neuralnet.DistanceMeasure; import org.apache.commons.math4.neuralnet.EuclideanDistance; @@ -46,10 +44,7 @@ import org.apache.commons.math4.neuralnet.twod.util.LocationFinder; * @since 3.3 */ public class NeuronSquareMesh2D - implements Iterable<Neuron>, - Serializable { - /** Serial version ID. */ - private static final long serialVersionUID = 1L; + implements Iterable<Neuron> { /** Minimal number of rows or columns. */ private static final int MIN_ROWS = 2; /** Underlying network. */ @@ -97,8 +92,6 @@ public class NeuronSquareMesh2D } /** - * Constructor with restricted access, solely used for deserialization. - * * @param wrapRowDim Whether to wrap the first dimension (i.e the first * and last neurons will be linked together). * @param wrapColDim Whether to wrap the second dimension (i.e the first @@ -109,10 +102,10 @@ public class NeuronSquareMesh2D * @throws IllegalArgumentException if {@code numRows < 2} or * {@code numCols < 2}. */ - NeuronSquareMesh2D(boolean wrapRowDim, - boolean wrapColDim, - SquareNeighbourhood neighbourhoodType, - double[][][] featuresList) { + public NeuronSquareMesh2D(boolean wrapRowDim, + boolean wrapColDim, + SquareNeighbourhood neighbourhoodType, + double[][][] featuresList) { numberOfRows = featuresList.length; numberOfColumns = featuresList[0].length; @@ -572,79 +565,6 @@ public class NeuronSquareMesh2D } /** - * Prevents proxy bypass. - * - * @param in Input stream. - */ - private void readObject(ObjectInputStream in) { - throw new IllegalStateException(); - } - - /** - * Custom serialization. - * - * @return the proxy instance that will be actually serialized. - */ - private Object writeReplace() { - final double[][][] featuresList = new double[numberOfRows][numberOfColumns][]; - for (int i = 0; i < numberOfRows; i++) { - for (int j = 0; j < numberOfColumns; j++) { - featuresList[i][j] = getNeuron(i, j).getFeatures(); - } - } - - return new SerializationProxy(wrapRows, - wrapColumns, - neighbourhood, - featuresList); - } - - /** - * Serialization. - */ - private static class SerializationProxy implements Serializable { - /** Serializable. */ - private static final long serialVersionUID = 20130226L; - /** Wrap. */ - private final boolean wrapRows; - /** Wrap. */ - private final boolean wrapColumns; - /** Neighbourhood type. */ - private final SquareNeighbourhood neighbourhood; - /** Neurons' features. */ - private final double[][][] featuresList; - - /** - * @param wrapRows Whether the row dimension is wrapped. - * @param wrapColumns Whether the column dimension is wrapped. - * @param neighbourhood Neighbourhood type. - * @param featuresList List of neurons features. - * {@code neuronList}. - */ - SerializationProxy(boolean wrapRows, - boolean wrapColumns, - SquareNeighbourhood neighbourhood, - double[][][] featuresList) { - this.wrapRows = wrapRows; - this.wrapColumns = wrapColumns; - this.neighbourhood = neighbourhood; - this.featuresList = featuresList; - } - - /** - * Custom serialization. - * - * @return the {@link Neuron} for which this instance is the proxy. - */ - private Object readResolve() { - return new NeuronSquareMesh2D(wrapRows, - wrapColumns, - neighbourhood, - featuresList); - } - } - - /** * Miscellaneous indicators of the map quality. * <ul> * <li>Hit histogram</li> diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java index b413bb1..da60ad1 100644 --- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java +++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java @@ -174,43 +174,4 @@ public class NetworkTest { Assert.assertFalse(netNeighbours.contains(netNeuron1)); Assert.assertTrue(copyNeighbours.contains(copyNeuron1)); } - - @Test - public void testSerialize() - throws IOException, - ClassNotFoundException { - final FeatureInitializer[] initArray = {init}; - final Network out = new NeuronSquareMesh2D(4, false, - 3, true, - SquareNeighbourhood.VON_NEUMANN, - initArray).getNetwork(); - - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(out); - - final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - final ObjectInputStream ois = new ObjectInputStream(bis); - final Network in = (Network) ois.readObject(); - - for (Neuron nOut : out) { - final Neuron nIn = in.getNeuron(nOut.getIdentifier()); - - // Same values. - final double[] outF = nOut.getFeatures(); - final double[] inF = nIn.getFeatures(); - Assert.assertEquals(outF.length, inF.length); - for (int i = 0; i < outF.length; i++) { - Assert.assertEquals(outF[i], inF[i], 0d); - } - - // Same neighbours. - final Collection<Neuron> outNeighbours = out.getNeighbours(nOut); - final Collection<Neuron> inNeighbours = in.getNeighbours(nIn); - Assert.assertEquals(outNeighbours.size(), inNeighbours.size()); - for (Neuron oN : outNeighbours) { - Assert.assertTrue(inNeighbours.contains(in.getNeuron(oN.getIdentifier()))); - } - } - } } diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NeuronTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NeuronTest.java index 682a826..14a8e90 100644 --- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NeuronTest.java +++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NeuronTest.java @@ -111,29 +111,4 @@ public class NeuronTest { Assert.assertNotEquals(n.getNumberOfSuccessfulUpdates(), copy.getNumberOfSuccessfulUpdates()); } - - @Test - public void testSerialize() - throws IOException, - ClassNotFoundException { - final Neuron out = new Neuron(123, new double[] {-98.76, -1, 0, 1e-23, 543.21, 1e234}); - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(out); - - final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - final ObjectInputStream ois = new ObjectInputStream(bis); - final Neuron in = (Neuron) ois.readObject(); - - // Same identifier. - Assert.assertEquals(out.getIdentifier(), - in.getIdentifier()); - // Same values. - final double[] outF = out.getFeatures(); - final double[] inF = in.getFeatures(); - Assert.assertEquals(outF.length, inF.length); - for (int i = 0; i < outF.length; i++) { - Assert.assertEquals(outF[i], inF[i], 0d); - } - } } diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java index 889ab70..5d63662 100644 --- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java +++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/oned/NeuronStringTest.java @@ -150,40 +150,4 @@ public class NeuronStringTest { Assert.assertTrue(neighbours.contains(net.getNeuron(4))); Assert.assertEquals(1, neighbours.size()); } - - @Test - public void testSerialize() - throws IOException, - ClassNotFoundException { - final FeatureInitializer[] initArray = {init}; - final NeuronString out = new NeuronString(4, false, initArray); - - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(out); - - final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - final ObjectInputStream ois = new ObjectInputStream(bis); - final NeuronString in = (NeuronString) ois.readObject(); - - for (Neuron nOut : out.getNetwork()) { - final Neuron nIn = in.getNetwork().getNeuron(nOut.getIdentifier()); - - // Same values. - final double[] outF = nOut.getFeatures(); - final double[] inF = nIn.getFeatures(); - Assert.assertEquals(outF.length, inF.length); - for (int i = 0; i < outF.length; i++) { - Assert.assertEquals(outF[i], inF[i], 0d); - } - - // Same neighbours. - final Collection<Neuron> outNeighbours = out.getNetwork().getNeighbours(nOut); - final Collection<Neuron> inNeighbours = in.getNetwork().getNeighbours(nIn); - Assert.assertEquals(outNeighbours.size(), inNeighbours.size()); - for (Neuron oN : outNeighbours) { - Assert.assertTrue(inNeighbours.contains(in.getNetwork().getNeuron(oN.getIdentifier()))); - } - } - } } diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java index ec56755..479e559 100644 --- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java +++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/twod/NeuronSquareMesh2DTest.java @@ -651,45 +651,6 @@ public class NeuronSquareMesh2DTest { Assert.assertEquals(16, neighbours.size()); } - @Test - public void testSerialize() - throws IOException, - ClassNotFoundException { - final FeatureInitializer[] initArray = {init}; - final NeuronSquareMesh2D out = new NeuronSquareMesh2D(4, false, - 3, true, - SquareNeighbourhood.VON_NEUMANN, - initArray); - - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final ObjectOutputStream oos = new ObjectOutputStream(bos); - oos.writeObject(out); - - final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - final ObjectInputStream ois = new ObjectInputStream(bis); - final NeuronSquareMesh2D in = (NeuronSquareMesh2D) ois.readObject(); - - for (Neuron nOut : out.getNetwork()) { - final Neuron nIn = in.getNetwork().getNeuron(nOut.getIdentifier()); - - // Same values. - final double[] outF = nOut.getFeatures(); - final double[] inF = nIn.getFeatures(); - Assert.assertEquals(outF.length, inF.length); - for (int i = 0; i < outF.length; i++) { - Assert.assertEquals(outF[i], inF[i], 0d); - } - - // Same neighbours. - final Collection<Neuron> outNeighbours = out.getNetwork().getNeighbours(nOut); - final Collection<Neuron> inNeighbours = in.getNetwork().getNeighbours(nIn); - Assert.assertEquals(outNeighbours.size(), inNeighbours.size()); - for (Neuron oN : outNeighbours) { - Assert.assertTrue(inNeighbours.contains(in.getNetwork().getNeuron(oN.getIdentifier()))); - } - } - } - /* * Test assumes that the network is *