SharedDefaultGazetteer.java
01 package gate.creole.gazetteer;
02 
03 import gate.Factory;
04 import gate.Resource;
05 import gate.creole.CustomDuplication;
06 import gate.creole.ResourceInstantiationException;
07 
08 /**
09  * Provides a way to efficiently multi-thread a DefaultGazetteer.
10  *
11  * The reccommended way to multithread a gate pipeline is to have a seperate
12  * instance per thread (or a resource pool).  This is not ideal when using
13  * large Gazetteers as these can take a long time to initialise and take up a
14  * lot of memory. This class provides a way to bootstrap a new gazetteer
15  * instance off of an existing gazetteer instance while still maintaining all
16  * thread level variables.  {@link DefaultGazetteer} implements
17  {@link CustomDuplication} using this class, so the easiest way to build
18  * multiple copies of a {@link DefaultGazetteer} PR that share a single FSM
19  * is to create one in the usual way and then use
20  {@link Factory#duplicate(Resource)} to copy it.
21  *
22  * NOTE: It is (probably) impossible to use this class from within either the
23  * Gaze user interface or from a .gapp application file. You should only use
24  * this PR when embedding GATE within another application and initialise it
25  * specifically. There is no reason a DefaultGazetteer loaded via a .gapp file
26  * cannot be used to bootstrap this PR however.
27  *
28  @author Matt Nathan
29  */
30 public class SharedDefaultGazetteer extends DefaultGazetteer {
31 
32     public static final String SDEF_GAZ_BOOTSTRAP_GAZETTEER_PROPERTY_NAME = "bootstrapGazetteer";
33 
34     /**
35      * The existing DefaultGazetteer instance whose FSM we will share.
36      */
37     protected DefaultGazetteer bootstrapGazetteer;
38 
39     /**
40      * Copy the references to the shareable state (i.e. the FSM) from the
41      * existing gazetteer.  Note that this method <i>deliberately</i> does not
42      * call <code>super.init()</code> as to do so would cause the lists to be
43      * reloaded.
44      */
45     @Override
46     public Resource init() throws ResourceInstantiationException {
47         if (bootstrapGazetteer == null) {
48             throw new ResourceInstantiationException(
49                     "No gazetteer provided to bootstrap this gazetteer creation!");
50         }
51         this.annotationSetName = bootstrapGazetteer.annotationSetName;
52         this.caseSensitive = bootstrapGazetteer.caseSensitive;
53         this.definition = bootstrapGazetteer.definition;
54         this.encoding = bootstrapGazetteer.encoding;
55         this.fsmStates = bootstrapGazetteer.fsmStates;
56         this.gazetteerFeatureSeparator = bootstrapGazetteer.gazetteerFeatureSeparator;
57         this.initialState = bootstrapGazetteer.initialState;
58         this.listsByNode = bootstrapGazetteer.listsByNode;
59         this.listsURL = bootstrapGazetteer.listsURL;
60         this.longestMatchOnly = bootstrapGazetteer.longestMatchOnly;
61         this.mappingDefinition = bootstrapGazetteer.mappingDefinition;
62         this.wholeWordsOnly = bootstrapGazetteer.wholeWordsOnly;
63 
64         this.getFeatures().putAll(bootstrapGazetteer.getFeatures());
65         return this;
66     }
67 
68     public DefaultGazetteer getBootstrapGazetteer() {
69         return bootstrapGazetteer;
70     }
71 
72     public void setBootstrapGazetteer(DefaultGazetteer bootstrapGazetteer) {
73         this.bootstrapGazetteer = bootstrapGazetteer;
74     }
75 }