opennlp-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject svn commit: r1244179 - in /incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools: postag/POSModel.java util/BaseToolFactory.java util/model/BaseModel.java
Date Tue, 14 Feb 2012 19:25:18 GMT
Author: colen
Date: Tue Feb 14 19:25:17 2012
New Revision: 1244179

URL: http://svn.apache.org/viewvc?rev=1244179&view=rev
Log:
OPENNLP-429: The BaseModel was creating a POSTaggerFactory by default, but it is invalid.
Should get the default factory from the Model implementation.

Modified:
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSModel.java
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSModel.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSModel.java?rev=1244179&r1=1244178&r2=1244179&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSModel.java
(original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSModel.java
Tue Feb 14 19:25:17 2012
@@ -24,6 +24,7 @@ import java.util.Map;
 
 import opennlp.model.AbstractModel;
 import opennlp.tools.dictionary.Dictionary;
+import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
 import opennlp.tools.util.model.ArtifactSerializer;
 import opennlp.tools.util.model.BaseModel;
@@ -80,9 +81,8 @@ public final class POSModel extends Base
   }
 
   @Override
-  protected void initializeFactory() {
-    super.initializeFactory();
-
+  protected Class<? extends BaseToolFactory> getDefaultFactory() {
+    return POSTaggerFactory.class;
   }
 
   @Override

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java?rev=1244179&r1=1244178&r2=1244179&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
(original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
Tue Feb 14 19:25:17 2012
@@ -117,6 +117,33 @@ public abstract class BaseToolFactory {
     return theFactory;
   }
 
+  public static BaseToolFactory create(Class<? extends BaseToolFactory> factoryClass,
+      ArtifactProvider artifactProvider) throws InvalidFormatException {
+    BaseToolFactory theFactory = null;
+    if (factoryClass != null) {
+      try {
+        Constructor<?> constructor = null;
+        constructor = factoryClass.getConstructor(ArtifactProvider.class);
+        theFactory = (BaseToolFactory) constructor
+            .newInstance(artifactProvider);
+      } catch (NoSuchMethodException e) {
+        String msg = "Could not instantiate the "
+            + factoryClass.getCanonicalName()
+            + ". The mandatry constructor (ArtifactProvider) is missing.";
+        System.err.println(msg);
+        throw new IllegalArgumentException(msg);
+      } catch (Exception e) {
+        String msg = "Could not instantiate the "
+            + factoryClass.getCanonicalName()
+            + ". The constructor (ArtifactProvider) throw an exception.";
+        System.err.println(msg);
+        e.printStackTrace();
+        throw new InvalidFormatException(msg);
+      }
+    }
+    return theFactory;
+  }
+  
   @SuppressWarnings("unchecked")
   protected
   static Class<? extends BaseToolFactory> loadSubclass(

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java?rev=1244179&r1=1244178&r2=1244179&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
(original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
Tue Feb 14 19:25:17 2012
@@ -30,7 +30,6 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
-import opennlp.tools.postag.POSTaggerFactory;
 import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
 import opennlp.tools.util.Version;
@@ -139,7 +138,11 @@ public abstract class BaseModel implemen
       artifactMap.putAll(factory.createArtifactMap());
     }
     
-    initializeFactory();
+    try {
+      initializeFactory();
+    } catch (InvalidFormatException e) {
+      throw new IllegalArgumentException("Could not initialize tool factory. " + e.getMessage());
+    }
     loadArtifactSerializers();
   }
 
@@ -195,18 +198,18 @@ public abstract class BaseModel implemen
     finishLoadingArtifacts();
     checkArtifactMap();
   }
-  
-  /**
-   * 
-   */
-  protected void initializeFactory() {
+
+  private void initializeFactory() throws InvalidFormatException {
     String factoryName = getManifestProperty(FACTORY_NAME);
     if (factoryName == null) {
       // load the default factory
-      this.toolFactory = new POSTaggerFactory(this);
+      Class<? extends BaseToolFactory> factoryClass = getDefaultFactory();
+      if(factoryClass != null) {
+        this.toolFactory = BaseToolFactory.create(factoryClass, this);
+      }
     } else {
       try {
-        this.toolFactory = POSTaggerFactory.create(factoryName, this);
+        this.toolFactory = BaseToolFactory.create(factoryName, this);
       } catch (InvalidFormatException e) {
         throw new IllegalArgumentException(e.getMessage());
       }
@@ -214,6 +217,16 @@ public abstract class BaseModel implemen
   }
   
   /**
+   * Sub-classes should override this method if their module has a default
+   * BaseToolFactory sub-class.
+   * 
+   * @return the default {@link BaseToolFactory} for the module, or null if none.
+   */
+  protected Class<? extends BaseToolFactory> getDefaultFactory() {
+    return null;
+  }
+  
+  /**
    * Loads the artifact serializers.
    */
   private void loadArtifactSerializers() {



Mime
View raw message