From cd0c2a8b13c0dadfc4fed43d1d87fc7bda35060f Mon Sep 17 00:00:00 2001 From: Mathieu Bastian Date: Sun, 6 Nov 2022 14:57:22 +0100 Subject: [PATCH] Implement #156 --- .../java/org/gephi/graph/api/GraphModel.java | 21 +++++++++++++++++-- .../org/gephi/graph/impl/Serialization.java | 11 ++++++++++ .../gephi/graph/impl/SerializationTest.java | 15 +++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gephi/graph/api/GraphModel.java b/src/main/java/org/gephi/graph/api/GraphModel.java index 78e9ee8f..a748a566 100644 --- a/src/main/java/org/gephi/graph/api/GraphModel.java +++ b/src/main/java/org/gephi/graph/api/GraphModel.java @@ -101,7 +101,7 @@ public static class Factory { * * @return new instance */ - public static GraphModel newInstance() { + public static GraphModelImpl newInstance() { return new GraphModelImpl(); } @@ -111,7 +111,7 @@ public static GraphModel newInstance() { * @param config configuration * @return new instance */ - public static GraphModel newInstance(Configuration config) { + public static GraphModelImpl newInstance(Configuration config) { return new GraphModelImpl(config); } } @@ -137,6 +137,23 @@ public static GraphModel read(DataInput input) throws IOException { } } + /** + * Read the input into the given graph model. The provided graph + * model should be empty. + * + * @param input data input to read from + * @return the graphmodel passed as parameter + * @throws IOException if an io error occurs + */ + public static GraphModel read(DataInput input, GraphModel graphModel) throws IOException { + try { + org.gephi.graph.impl.Serialization s = new org.gephi.graph.impl.Serialization(); + return s.deserializeGraphModel(input, graphModel); + } catch (ClassNotFoundException e) { + throw new IOException(e); + } + } + /** * Read the input and return the read graph model without an * explicit version header in the input. To be used with old graphstore diff --git a/src/main/java/org/gephi/graph/impl/Serialization.java b/src/main/java/org/gephi/graph/impl/Serialization.java index 2ebc3761..f70e4fe5 100644 --- a/src/main/java/org/gephi/graph/impl/Serialization.java +++ b/src/main/java/org/gephi/graph/impl/Serialization.java @@ -58,6 +58,7 @@ import java.util.Map; import java.util.Set; import org.gephi.graph.api.Configuration; +import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.Origin; import org.gephi.graph.api.Estimator; import org.gephi.graph.api.TimeFormat; @@ -247,6 +248,16 @@ public GraphModelImpl deserializeGraphModel(DataInput is) throws IOException, Cl return model; } + public GraphModelImpl deserializeGraphModel(DataInput is, GraphModel graphModel) throws IOException, ClassNotFoundException { + model = (GraphModelImpl) graphModel; + readVersion = (Float) deserialize(is); + Configuration config = (Configuration) deserialize(is); + model.setConfiguration(config); + deserialize(is); + model.store.defaultColumns.resetConfiguration(); + return model; + } + public GraphModelImpl deserializeGraphModelWithoutVersionPrefix(DataInput is, float version) throws IOException, ClassNotFoundException { readVersion = version; Configuration config = (Configuration) deserialize(is); diff --git a/src/test/java/org/gephi/graph/impl/SerializationTest.java b/src/test/java/org/gephi/graph/impl/SerializationTest.java index 7b1b3a21..9cd89e87 100644 --- a/src/test/java/org/gephi/graph/impl/SerializationTest.java +++ b/src/test/java/org/gephi/graph/impl/SerializationTest.java @@ -55,6 +55,7 @@ import java.util.Map; import java.util.Set; import org.gephi.graph.api.Configuration; +import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.Origin; import org.gephi.graph.api.Estimator; import org.gephi.graph.api.TimeFormat; @@ -1224,6 +1225,20 @@ public void testDefaultColumns() throws Exception { Assert.assertSame(read.defaultColumns().edgeId(), read.getEdgeTable().getColumn("id")); } + @Test + public void testDeserializeWithGraphModel() throws Exception { + GraphModelImpl gm = GraphGenerator.generateSmallUndirectedGraphStore().graphModel; + Serialization ser = new Serialization(gm); + + DataInputOutput dio = new DataInputOutput(); + ser.serializeGraphModel(dio, gm); + byte[] bytes = dio.toByteArray(); + + GraphModelImpl read = GraphModel.Factory.newInstance(); + read = ser.deserializeGraphModel(dio.reset(bytes), read); + Assert.assertTrue(read.deepEquals(gm)); + } + @Test public void testDeserializeWithoutVersion() throws Exception { GraphModelImpl gm = GraphGenerator.generateSmallGraphStore().graphModel;