1   /*
2    *  Copyright (c) 1998-2005, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Valentin Tablan 11 Apr 2002
10   *
11   *  $Id: AnalyserRunningStrategy.java,v 1.6 2005/01/11 13:51:31 ian Exp $
12   */
13  package gate.creole;
14  
15  import gate.*;
16  import gate.util.GateRuntimeException;
17  
18  /**
19   * A type running strategy that decides whether the associated PR needs to be
20   * run based on the value of a specified feature on the document that the PR
21   * needs to be run on.
22   * It can only be used for {@link LanguageAnalyser}s because it needs the
23   * document the PR will run on.
24   */
25  
26  public class AnalyserRunningStrategy implements RunningStrategy{
27  
28    public AnalyserRunningStrategy(LanguageAnalyser pr, int runMode,
29                                   String featureName, String featureValue){
30      this.pr = pr;
31      this.runMode = runMode;
32      this.featureName = featureName;
33      this.featureValue = featureValue;
34    }
35  
36    /**
37     * If the runMode is {@link #RUN_ALWAYS} returns true.
38     * If the runMode is {@link #RUN_NEVER} returns false.
39     * If the runMode is {@link #RUN_CONDITIONAL}:
40     * <ul>
41     * <li>if the document is null returns false</li>
42     * <li>if the document features are null
43     *  <ul>
44     *  <li>if {@link #featureName} is null returns true</li>
45     *  <li>if {@link #featureName} is not null returns false</li>
46     *  </ul></li>
47     * <li>if the document features are not null
48     * <ul>
49     *  <li>if {@link #featureName} is null returns true</li>
50     *  <li>if {@link #featureName} is not null and the document features contain
51     *  such a feature returns true if the value of the feature is
52     *  {@link #featureValue} and false otherwise.</li>
53     * </ul></li>
54     * </ul>
55     * @return a <tt>boolean</tt> value.
56     */
57    public boolean shouldRun() {
58      if(runMode == RUN_ALWAYS) return true;
59      if(runMode == RUN_NEVER) return false;
60      if(runMode == RUN_CONDITIONAL){
61        if(featureName == null || featureName.length() == 0) return true;
62        Document doc = pr.getDocument();
63        if(doc != null){
64          FeatureMap fm = doc.getFeatures();
65          if(fm != null){
66            Object actualValue = fm.get(featureName);
67            return (actualValue == null && featureValue == null)
68                    ||
69                   (actualValue != null && actualValue.equals(featureValue));
70          }else return featureName == null;
71        }else return false;
72      }
73      throw new GateRuntimeException("Unknown run mode!");
74    }
75  
76    public int getRunMode() {
77      return runMode;
78    }
79  
80    public void setRunMode(int mode){
81      this.runMode = mode;
82    }
83  
84    public void setFeatureName(String name){
85      this.featureName = name;
86    }
87  
88    public void setFeatureValue(String value){
89      this.featureValue = value;
90    }
91  
92    public String getFeatureName() {
93      return featureName;
94    }
95  
96    public String getFeatureValue() {
97      return featureValue;
98    }
99  
100   public ProcessingResource getPR() {
101     return pr;
102   }
103 
104   public void setProcessingResource(ProcessingResource pr){
105     if(pr instanceof LanguageAnalyser){
106       this.pr = (LanguageAnalyser)pr;
107     }else throw new GateRuntimeException(
108       getClass().getName() + " can only be used for " +
109       LanguageAnalyser.class.getName() + "!\n" +
110       pr.getClass().getName() + " is not a " +
111       LanguageAnalyser.class.getName() + "!");
112   }
113 
114   protected LanguageAnalyser pr;
115   protected int runMode = RUN_ALWAYS;
116   protected String featureName;
117   protected String featureValue;
118 }