You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2016/11/22 00:21:20 UTC

opennlp git commit: Make all models Serializable

Repository: opennlp
Updated Branches:
  refs/heads/trunk c28a54208 -> c9f2943c7


Make all models Serializable

Thanks to Tristan Nixon for providing a patch.

See issue OPENNLP-776


Project: http://git-wip-us.apache.org/repos/asf/opennlp/repo
Commit: http://git-wip-us.apache.org/repos/asf/opennlp/commit/c9f2943c
Tree: http://git-wip-us.apache.org/repos/asf/opennlp/tree/c9f2943c
Diff: http://git-wip-us.apache.org/repos/asf/opennlp/diff/c9f2943c

Branch: refs/heads/trunk
Commit: c9f2943c76fcc3191639400dbf8f383869ef814b
Parents: c28a542
Author: J�rn Kottmann <jo...@apache.org>
Authored: Fri Oct 28 18:00:10 2016 +0200
Committer: J�rn Kottmann <jo...@apache.org>
Committed: Tue Nov 22 01:20:13 2016 +0100

----------------------------------------------------------------------
 .../opennlp/tools/util/model/BaseModel.java     | 36 ++++++++++++++++----
 1 file changed, 30 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/opennlp/blob/c9f2943c/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
index 989c80b..4a40d15 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
@@ -24,7 +24,10 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.OutputStream;
+import java.io.Serializable;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
@@ -45,7 +48,7 @@ import opennlp.tools.util.ext.ExtensionLoader;
  * TODO:
  * Provide sub classes access to serializers already in constructor
  */
-public abstract class BaseModel implements ArtifactProvider {
+public abstract class BaseModel implements ArtifactProvider, Serializable {
 
   private static int MODEL_BUFFER_SIZE_LIMIT = Integer.MAX_VALUE;
 
@@ -64,19 +67,18 @@ public abstract class BaseModel implements ArtifactProvider {
 
   private static String SERIALIZER_CLASS_NAME_PREFIX = "serializer-class-";
 
-  private Map<String, ArtifactSerializer> artifactSerializers =
-      new HashMap<String, ArtifactSerializer>();
+  private Map<String, ArtifactSerializer> artifactSerializers = new HashMap<>();
 
-  protected final Map<String, Object> artifactMap = new HashMap<String, Object>();
+  protected Map<String, Object> artifactMap = new HashMap<>();
 
   protected BaseToolFactory toolFactory;
 
-  private final String componentName;
+  private String componentName;
 
   private boolean subclassSerializersInitiated = false;
   private boolean finishedLoadingArtifacts = false;
 
-  private final boolean isLoadedFromSerialized;
+  private boolean isLoadedFromSerialized;
 
   private BaseModel(String componentName, boolean isLoadedFromSerialized) {
     this.isLoadedFromSerialized = isLoadedFromSerialized;
@@ -632,4 +634,26 @@ public abstract class BaseModel implements ArtifactProvider {
   public boolean isLoadedFromSerialized() {
     return isLoadedFromSerialized;
   }
+
+  // These methods are required to serialize/deserialize the model because
+  // many of the included objects in this model are not Serializable.
+  // An alternative to this solution is to make all included objects
+  // Serializable and remove the writeObject and readObject methods.
+  // This will allow the usage of final for fields that should not change.
+
+  private void writeObject(ObjectOutputStream out) throws IOException {
+    out.writeUTF(componentName);
+    this.serialize(out);
+  }
+
+  private void readObject(final ObjectInputStream in) throws IOException {
+
+    isLoadedFromSerialized = true;
+    artifactSerializers = new HashMap<>();
+    artifactMap = new HashMap<>();
+
+    componentName = in.readUTF();
+
+    this.loadModel(in);
+  }
 }