ConstraintFactory.java
001 /*
002  *  Constraint Factory - transducer class
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Eric Sword, 03/09/08
013  *
014  *  $Id$
015  */
016 
017 package gate.jape.constraint;
018 
019 import java.util.*;
020 
021 import gate.jape.Constraint;
022 
023 /**
024  * Creates Jape {@link Constraint}s and associated
025  {@link ConstraintPredicate}s.
026  *
027  @version $Revision$
028  @author esword
029  */
030 public class ConstraintFactory {
031 
032   protected Map<String, Class<? extends ConstraintPredicate>> operatorImplMap =
033     new HashMap<String, Class<? extends ConstraintPredicate>>();
034 
035   protected Map<String, Class<? extends AnnotationAccessor>> metaPropertyMap =
036     new HashMap<String, Class<? extends AnnotationAccessor>>();
037 
038   public ConstraintFactory() {
039     initOperatorMap();
040     initMetaPropertyMap();
041   }
042 
043   protected void initOperatorMap() {
044     addOperator(ConstraintPredicate.EQUAL, EqualPredicate.class);
045     addOperator(ConstraintPredicate.NOT_EQUAL, NotEqualPredicate.class);
046     addOperator(ConstraintPredicate.GREATER, GreaterPredicate.class);
047     addOperator(ConstraintPredicate.LESSER, LesserPredicate.class);
048     addOperator(ConstraintPredicate.GREATER_OR_EQUAL,
049             GreaterEqualPredicate.class);
050     addOperator(ConstraintPredicate.LESSER_OR_EQUAL, LesserEqualPredicate.class);
051     addOperator(ConstraintPredicate.REGEXP_FIND, RegExpFindPredicate.class);
052     addOperator(ConstraintPredicate.NOT_REGEXP_FIND, NotRegExpFindPredicate.class);
053     addOperator(ConstraintPredicate.REGEXP_MATCH, RegExpMatchPredicate.class);
054     addOperator(ConstraintPredicate.NOT_REGEXP_MATCH, NotRegExpMatchPredicate.class);
055     addOperator(ContainsPredicate.OPERATOR, ContainsPredicate.class);
056     addOperator(WithinPredicate.OPERATOR, WithinPredicate.class);
057   }
058 
059   protected void initMetaPropertyMap() {
060     addMetaProperty("string", StringAccessor.class);
061     addMetaProperty("cleanString", CleanStringAccessor.class);
062     addMetaProperty("length", LengthAccessor.class);
063   }
064 
065   public void addOperator(String operator,
066           Class<? extends ConstraintPredicate> clazz) {
067     operatorImplMap.put(operator, clazz);
068   }
069 
070   public void addMetaProperty(String metaProperty,
071           Class<? extends AnnotationAccessor> clazz) {
072     metaPropertyMap.put(metaProperty, clazz);
073   }
074 
075   /**
076    * Create a new constraint for the given annotation type
077    *
078    @param annotType
079    @return
080    */
081   public Constraint createConstraint(String annotType) {
082     return new Constraint(annotType);
083   }
084 
085   public AnnotationAccessor createDefaultAccessor(Object key) {
086     return new AnnotationFeatureAccessor(key);
087   }
088 
089   public AnnotationAccessor createMetaPropertyAccessor(String propName) {
090     AnnotationAccessor retVal = null;
091     Class<?> clazz = metaPropertyMap.get(propName);
092     if(clazz == null)
093       throw new IllegalArgumentException(
094               "No meta property associated with name: " + propName);
095 
096     try {
097       retVal = (AnnotationAccessor)clazz.newInstance();
098     }
099     catch(Exception e) {
100       throw new RuntimeException("Could not create accessor for name '"
101               + propName + "'", e);
102     }
103 
104     return retVal;
105   }
106 
107   /**
108    * Create a constraint predicate using the default equals predicate.
109    *
110    @param name feature name associated with the predicate
111    @param value value associated with the predicate
112    @return
113    */
114   public ConstraintPredicate createPredicate(String name, Object value) {
115     return createPredicate(createDefaultAccessor(name), value);
116   }
117 
118   public ConstraintPredicate createPredicate(AnnotationAccessor accessor,
119           Object value) {
120     return createPredicate(ConstraintPredicate.EQUAL, accessor, value);
121   }
122 
123   public ConstraintPredicate createPredicate(String operator,
124           AnnotationAccessor accessor, Object value) {
125     ConstraintPredicate retVal = null;
126     Class<?> clazz = operatorImplMap.get(operator);
127     if(clazz == null)
128       throw new IllegalArgumentException(
129               "No predicate associated with operator: " + operator);
130 
131     try {
132       retVal = (ConstraintPredicate)clazz.newInstance();
133       retVal.setAccessor(accessor);
134       retVal.setValue(value);
135     }
136     catch(Exception e) {
137       throw new RuntimeException("Could not create predicate for operator '"
138               + operator + "' with accessor '" + accessor + "' and value '"
139               + value + "'", e);
140     }
141 
142     return retVal;
143   }
144 
145 }