001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Valentin Tablan 11 Apr 2002
011 *
012 * $Id: AnalyserRunningStrategy.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013 */
014 package gate.creole;
015
016 import gate.*;
017 import gate.util.GateRuntimeException;
018
019 /**
020 * A type running strategy that decides whether the associated PR needs to be
021 * run based on the value of a specified feature on the document that the PR
022 * needs to be run on.
023 * It can only be used for {@link LanguageAnalyser}s because it needs the
024 * document the PR will run on.
025 */
026
027 public class AnalyserRunningStrategy implements RunningStrategy{
028
029 public AnalyserRunningStrategy(LanguageAnalyser pr, int runMode,
030 String featureName, String featureValue){
031 this.pr = pr;
032 this.runMode = runMode;
033 this.featureName = featureName;
034 this.featureValue = featureValue;
035 }
036
037 /**
038 * If the runMode is {@link #RUN_ALWAYS} returns true.
039 * If the runMode is {@link #RUN_NEVER} returns false.
040 * If the runMode is {@link #RUN_CONDITIONAL}:
041 * <ul>
042 * <li>if the document is null returns false</li>
043 * <li>if the document features are null
044 * <ul>
045 * <li>if {@link #featureName} is null returns true</li>
046 * <li>if {@link #featureName} is not null returns false</li>
047 * </ul></li>
048 * <li>if the document features are not null
049 * <ul>
050 * <li>if {@link #featureName} is null returns true</li>
051 * <li>if {@link #featureName} is not null and the document features contain
052 * such a feature returns true if the value of the feature is
053 * {@link #featureValue} and false otherwise.</li>
054 * </ul></li>
055 * </ul>
056 * @return a <tt>boolean</tt> value.
057 */
058 public boolean shouldRun() {
059 if(runMode == RUN_ALWAYS) return true;
060 if(runMode == RUN_NEVER) return false;
061 if(runMode == RUN_CONDITIONAL){
062 if(featureName == null || featureName.length() == 0) return true;
063 Document doc = pr.getDocument();
064 if(doc != null){
065 FeatureMap fm = doc.getFeatures();
066 if(fm != null){
067 Object actualValue = fm.get(featureName);
068 return (actualValue == null && featureValue == null)
069 ||
070 (actualValue != null && actualValue.equals(featureValue));
071 }else return featureName == null;
072 }else return false;
073 }
074 throw new GateRuntimeException("Unknown run mode!");
075 }
076
077 public int getRunMode() {
078 return runMode;
079 }
080
081 public void setRunMode(int mode){
082 this.runMode = mode;
083 }
084
085 public void setFeatureName(String name){
086 this.featureName = name;
087 }
088
089 public void setFeatureValue(String value){
090 this.featureValue = value;
091 }
092
093 public String getFeatureName() {
094 return featureName;
095 }
096
097 public String getFeatureValue() {
098 return featureValue;
099 }
100
101 public ProcessingResource getPR() {
102 return pr;
103 }
104
105 public void setProcessingResource(ProcessingResource pr){
106 if(pr instanceof LanguageAnalyser){
107 this.pr = (LanguageAnalyser)pr;
108 }else throw new GateRuntimeException(
109 getClass().getName() + " can only be used for " +
110 LanguageAnalyser.class.getName() + "!\n" +
111 pr.getClass().getName() + " is not a " +
112 LanguageAnalyser.class.getName() + "!");
113 }
114
115 protected LanguageAnalyser pr;
116 protected int runMode = RUN_ALWAYS;
117 protected String featureName;
118 protected String featureValue;
119 }
|