DelegatingSemanticAnnotationHelper.java

  1. /*
  2.  *  DelegatingSemanticAnnotationHelper.java
  3.  *
  4.  *  Copyright (c) 2007-2011, The University of Sheffield.
  5.  *
  6.  *  This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html),
  7.  *  and is free software, licenced under the GNU Lesser General Public License,
  8.  *  Version 3, June 2007 (also included with this distribution as file
  9.  *  LICENCE-LGPL3.html).
  10.  *
  11.  *  Ian Roberts, 28 Mar 2011
  12.  *  
  13.  *  $Id: DelegatingSemanticAnnotationHelper.java 18571 2015-02-12 18:18:17Z ian_roberts $
  14.  */
  15. package gate.mimir.util;

  16. import java.util.List;
  17. import java.util.Map;

  18. import gate.Annotation;
  19. import gate.Document;
  20. import gate.mimir.AbstractSemanticAnnotationHelper;
  21. import gate.mimir.Constraint;
  22. import gate.mimir.MimirIndex;
  23. import gate.mimir.SemanticAnnotationHelper;
  24. import gate.mimir.index.AtomicAnnotationIndex;
  25. import gate.mimir.index.Mention;
  26. import gate.mimir.search.QueryEngine;

  27. /**
  28.  * <p>{@link SemanticAnnotationHelper} that simply delegates all method calls to
  29.  * another helper object. Use this as a base class for helpers that provide
  30.  * search enhancements (converting higher-level search expressions into
  31.  * appropriate low-level constraints) on top of any generic helper.</p>
  32.  *
  33.  * <p>The default implementation of the "business" methods init/close,
  34.  * documentStart/End and getMentions/getMentionUris is to simply call
  35.  * the delegate's equivalent method.  
  36.  *
  37.  * The logic for the "info" methods
  38.  * getNominal/Integer/Float/Text/UriFeatureNames is:
  39.  * <ul>
  40.  *   <li>if the required value is known locally (e.g. an explicit value was set
  41.  *   by a setter), then simply return it;</li>
  42.  *   <li>if no explicit value is set, and if the delegate helper is an instance
  43.  *   of {@link AbstractSemanticAnnotationHelper} (which can supply appropriate
  44.  *   information), then the value is obtained from the delegate, stored
  45.  *   locally, and returned. Future calls to the same method will use the locally
  46.  *   stored value.</li>
  47.  *   <li>if no local value exists, and the delegate cannot supply the
  48.  *   information, then <code>null</code> is returned.</li>
  49.  * </ul>
  50.  * This allows sub-classes of this class to override the appropriate getters so
  51.  * that they change the set of supported features. This can be useful in order
  52.  * to claim support for additional features not provided by the delegate, and/or
  53.  * to hide features that the delegate supports if, at search time the helper
  54.  * will be faking these features in terms of other features.
  55.  *
  56.  * <p><b>Note</b> this class does <b>not</b> override the convenience method
  57.  * {@link AbstractSemanticAnnotationHelper#getMentions(String, Map, QueryEngine)},
  58.  * so this method is implemented as a call to
  59.  * <code>this.getMentions(String, List&lt;Constraint&gt;, QueryEngine)</code>,
  60.  * not to <code>delegate.getMentions(String, Map, QueryEngine)</code>.
  61.  */
  62. public abstract class DelegatingSemanticAnnotationHelper extends
  63.                                                         AbstractSemanticAnnotationHelper {
  64.   private static final long serialVersionUID = 458089145672457600L;

  65.   /**
  66.    * Map key for the Groovy-friendly constructor in subclasses.
  67.    */
  68.   public static final String DELEGATE_KEY = "delegate";

  69.   protected SemanticAnnotationHelper delegate;

  70.   public SemanticAnnotationHelper getDelegate() {
  71.     return delegate;
  72.   }

  73.   public void setDelegate(SemanticAnnotationHelper delegate) {
  74.     this.delegate = delegate;
  75.     this.mode = delegate.getMode();
  76.   }

  77.  
  78.  
  79.   /* (non-Javadoc)
  80.    * @see gate.mimir.AbstractSemanticAnnotationHelper#getAnnotationType()
  81.    */
  82.   @Override
  83.   public String getAnnotationType() {
  84.     if(annotationType == null) {
  85.       // not explicitly set -> calculate it
  86.       if(delegate instanceof AbstractSemanticAnnotationHelper) {
  87.         annotationType =
  88.             ((AbstractSemanticAnnotationHelper)delegate).getAnnotationType();
  89.       }
  90.     }
  91.     return annotationType;
  92.   }
  93.  

  94.   /* (non-Javadoc)
  95.    * @see gate.mimir.AbstractSemanticAnnotationHelper#getNominalFeatures()
  96.    */
  97.   @Override
  98.   public String[] getNominalFeatures() {
  99.     if(nominalFeatureNames == null) {
  100.       // not explicitly set -> calculate the value
  101.       if(delegate instanceof AbstractSemanticAnnotationHelper) {
  102.         nominalFeatureNames =
  103.             ((AbstractSemanticAnnotationHelper)delegate).getNominalFeatures();
  104.       }
  105.     }
  106.     return nominalFeatureNames;
  107.   }

  108.   /* (non-Javadoc)
  109.    * @see gate.mimir.AbstractSemanticAnnotationHelper#getIntegerFeatures()
  110.    */
  111.   @Override
  112.   public String[] getIntegerFeatures() {
  113.     if(integerFeatureNames == null) {
  114.       // not explicitly set -> calculate the value
  115.       if(delegate instanceof AbstractSemanticAnnotationHelper) {
  116.         integerFeatureNames =
  117.             ((AbstractSemanticAnnotationHelper)delegate).getIntegerFeatures();
  118.       }
  119.     }
  120.     return integerFeatureNames;
  121.   }

  122.   /* (non-Javadoc)
  123.    * @see gate.mimir.AbstractSemanticAnnotationHelper#getFloatFeatures()
  124.    */
  125.   @Override
  126.   public String[] getFloatFeatures() {
  127.     if(floatFeatureNames == null) {
  128.       // not explicitly set -> calculate the value
  129.       if(delegate instanceof AbstractSemanticAnnotationHelper) {
  130.         floatFeatureNames =
  131.             ((AbstractSemanticAnnotationHelper)delegate).getFloatFeatures();
  132.       }
  133.     }
  134.     return floatFeatureNames;  
  135.   }

  136.   /* (non-Javadoc)
  137.    * @see gate.mimir.AbstractSemanticAnnotationHelper#getTextFeatures()
  138.    */
  139.   @Override
  140.   public String[] getTextFeatures() {
  141.     if(textFeatureNames == null) {
  142.       // not explicitly set -> calculate the value
  143.       if(delegate instanceof AbstractSemanticAnnotationHelper) {
  144.         textFeatureNames =
  145.             ((AbstractSemanticAnnotationHelper)delegate).getTextFeatures();
  146.       }
  147.     }
  148.     return textFeatureNames;
  149.   }

  150.   /* (non-Javadoc)
  151.    * @see gate.mimir.AbstractSemanticAnnotationHelper#getUriFeatures()
  152.    */
  153.   @Override
  154.   public String[] getUriFeatures() {
  155.     if(uriFeatureNames == null) {
  156.       // not explicitly set -> calculate the value
  157.       if(delegate instanceof AbstractSemanticAnnotationHelper) {
  158.         uriFeatureNames =
  159.             ((AbstractSemanticAnnotationHelper)delegate).getUriFeatures();
  160.       }
  161.     }
  162.     return uriFeatureNames;
  163.   }

  164.  
  165.   /**
  166.    * Always return the delegate's mode, as it makes no sense for a delegating
  167.    * helper to operate in a different mode from its underlying delegate.
  168.    */
  169.   @Override
  170.   public Mode getMode() {
  171.     return delegate.getMode();
  172.   }

  173.   @Override
  174.   public void init(AtomicAnnotationIndex index) {
  175.     super.init(index);
  176.     delegate.init(index);
  177.   }

  178.   @Override
  179.   public void documentStart(Document document) {
  180.     delegate.documentStart(document);
  181.   }

  182.   @Override
  183.   public String[] getMentionUris(Annotation annotation, int length,
  184.       AtomicAnnotationIndex index) {
  185.     return delegate.getMentionUris(annotation, length, index);
  186.   }

  187.   @Override
  188.   public List<Mention> getMentions(String annotationType,
  189.           List<Constraint> constraints, QueryEngine engine) {
  190.     return delegate.getMentions(annotationType, constraints, engine);
  191.   }

  192.  
  193.   /* (non-Javadoc)
  194.    * @see gate.mimir.AbstractSemanticAnnotationHelper#describeMention(java.lang.String)
  195.    */
  196.   @Override
  197.   public String describeMention(String mentionUri) {
  198.     return delegate.describeMention(mentionUri);
  199.   }
  200.  
  201.   /* (non-Javadoc)
  202.    * @see gate.mimir.SemanticAnnotationHelper#isMentionUri(java.lang.String)
  203.    */
  204.   @Override
  205.   public boolean isMentionUri(String mentionUri) {
  206.     return delegate.isMentionUri(mentionUri);
  207.   }

  208.   @Override
  209.   public void documentEnd() {
  210.     delegate.documentEnd();
  211.   }


  212.   @Override
  213.   public void close(AtomicAnnotationIndex index) {
  214.     delegate.close(index);
  215.   }

  216.   @Override
  217.   public void close(QueryEngine qEngine) {
  218.     delegate.close(qEngine);
  219.   }
  220.  
  221. }