Esempio n. 1
0
  /**
   * Loads any custom operators and annotation accessors into the ConstraintFactory.
   *
   * @throws ResourceInstantiationException
   */
  protected void initCustomConstraints() throws ResourceInstantiationException {
    // Load operators
    if (operators != null) {
      for (String opName : operators) {
        Class<? extends ConstraintPredicate> clazz = null;
        try {
          clazz =
              Class.forName(opName, true, Gate.getClassLoader())
                  .asSubclass(ConstraintPredicate.class);
        } catch (ClassNotFoundException e) {
          // if couldn't find it that way, try with current thread class loader
          try {
            clazz =
                Class.forName(opName, true, Thread.currentThread().getContextClassLoader())
                    .asSubclass(ConstraintPredicate.class);
          } catch (ClassNotFoundException e1) {
            throw new ResourceInstantiationException(
                "Cannot load class for operator: " + opName, e1);
          }
        } catch (ClassCastException cce) {
          throw new ResourceInstantiationException(
              "Operator class '" + opName + "' must implement ConstraintPredicate");
        }

        // instantiate an instance of the class so can get the operator string
        try {
          ConstraintPredicate predicate = clazz.newInstance();
          String opSymbol = predicate.getOperator();
          // now store it in ConstraintFactory
          Factory.getConstraintFactory().addOperator(opSymbol, clazz);
        } catch (Exception e) {
          throw new ResourceInstantiationException(
              "Cannot instantiate class for operator: " + opName, e);
        }
      }
    }

    // Load annotationAccessors
    if (annotationAccessors != null) {
      for (String accessorName : annotationAccessors) {
        Class<? extends AnnotationAccessor> clazz = null;
        try {
          clazz =
              Class.forName(accessorName, true, Gate.getClassLoader())
                  .asSubclass(AnnotationAccessor.class);
        } catch (ClassNotFoundException e) {
          // if couldn't find it that way, try with current thread class loader
          try {
            clazz =
                Class.forName(accessorName, true, Thread.currentThread().getContextClassLoader())
                    .asSubclass(AnnotationAccessor.class);
          } catch (ClassNotFoundException e1) {
            throw new ResourceInstantiationException(
                "Cannot load class for accessor: " + accessorName, e1);
          }
        } catch (ClassCastException cce) {
          throw new ResourceInstantiationException(
              "Operator class '" + accessorName + "' must implement AnnotationAccessor");
        }

        // instantiate an instance of the class so can get the meta-property name string
        try {
          AnnotationAccessor aa = clazz.newInstance();
          String accSymbol = (String) aa.getKey();
          // now store it in ConstraintFactory
          Factory.getConstraintFactory().addMetaProperty(accSymbol, clazz);
        } catch (Exception e) {
          throw new ResourceInstantiationException(
              "Cannot instantiate class for accessor: " + accessorName, e);
        }
      }
    }
  }
Esempio n. 2
0
 protected int convertPredicate(String annotationType, ConstraintPredicate oldPredicate)
     throws ResourceInstantiationException {
   Predicate newPredicate = new Predicate();
   newPredicate.annotationAccessor = oldPredicate.getAccessor();
   String operator = oldPredicate.getOperator();
   if (operator == ConstraintPredicate.EQUAL) {
     newPredicate.type = PredicateType.EQ;
   } else if (operator == ConstraintPredicate.GREATER) {
     newPredicate.type = PredicateType.GT;
   } else if (operator == ConstraintPredicate.GREATER_OR_EQUAL) {
     newPredicate.type = PredicateType.GE;
   } else if (operator == ConstraintPredicate.LESSER) {
     newPredicate.type = PredicateType.LT;
   } else if (operator == ConstraintPredicate.LESSER_OR_EQUAL) {
     newPredicate.type = PredicateType.LE;
   } else if (operator == ConstraintPredicate.NOT_EQUAL) {
     newPredicate.type = PredicateType.NOT_EQ;
   } else if (operator == ConstraintPredicate.NOT_REGEXP_FIND) {
     newPredicate.type = PredicateType.REGEX_NOT_FIND;
   } else if (operator == ConstraintPredicate.NOT_REGEXP_MATCH) {
     newPredicate.type = PredicateType.REGEX_NOT_MATCH;
   } else if (operator == ConstraintPredicate.REGEXP_FIND) {
     newPredicate.type = PredicateType.REGEX_FIND;
   } else if (operator == ConstraintPredicate.REGEXP_MATCH) {
     newPredicate.type = PredicateType.REGEX_MATCH;
   } else if (operator == ContainsPredicate.OPERATOR) {
     newPredicate.type = PredicateType.CONTAINS;
   } else if (operator == WithinPredicate.OPERATOR) {
     newPredicate.type = PredicateType.WITHIN;
   } else {
     // make it into a custom predicate
     newPredicate.type = PredicateType.CUSTOM;
     newPredicate.featureValue = oldPredicate;
   }
   if (newPredicate.type == PredicateType.CONTAINS) {
     String containedAnnType = null;
     List<Integer> containedPredicates = new LinkedList<Integer>();
     // convert the value
     ContainsPredicate contPredicate = (ContainsPredicate) oldPredicate;
     Object value = oldPredicate.getValue();
     if (value == null) {
       // just annotation type
       containedAnnType = contPredicate.getAnnotType();
     } else if (value instanceof String) {
       // a simple annotation type
       containedAnnType = (String) value;
     } else if (value instanceof Constraint) {
       Constraint constraint = (Constraint) value;
       containedAnnType = constraint.getAnnotType();
       for (ConstraintPredicate pred : constraint.getAttributeSeq()) {
         containedPredicates.add(convertPredicate(containedAnnType, pred));
       }
     }
     int[] newPredValue = new int[2 + containedPredicates.size()];
     newPredValue[0] = annotationTypes.indexOf(containedAnnType);
     if (newPredValue[0] == -1) {
       annotationTypes.add(containedAnnType);
       newPredValue[0] = annotationTypes.size() - 1;
     }
     // contains predicates are always positive
     newPredValue[1] = 1;
     int predIdx = 2;
     for (Integer predId : containedPredicates) {
       newPredValue[predIdx++] = predId;
     }
     newPredicate.featureValue = newPredValue;
   } else if (newPredicate.type == PredicateType.WITHIN) {
     String containedAnnType = null;
     List<Integer> containedPredicates = new LinkedList<Integer>();
     // convert the value
     WithinPredicate contPredicate = (WithinPredicate) oldPredicate;
     Object value = oldPredicate.getValue();
     if (value == null) {
       // just annotation type
       containedAnnType = contPredicate.getAnnotType();
     } else if (value instanceof String) {
       // a simple annotation type
       containedAnnType = (String) value;
     } else if (value instanceof Constraint) {
       Constraint constraint = (Constraint) value;
       containedAnnType = constraint.getAnnotType();
       for (ConstraintPredicate pred : constraint.getAttributeSeq()) {
         containedPredicates.add(convertPredicate(containedAnnType, pred));
       }
     }
     int[] newPredValue = new int[2 + containedPredicates.size()];
     newPredValue[0] = annotationTypes.indexOf(containedAnnType);
     if (newPredValue[0] == -1) {
       annotationTypes.add(containedAnnType);
       newPredValue[0] = annotationTypes.size() - 1;
     }
     // contains predicates are always positive
     newPredValue[1] = 1;
     int predIdx = 2;
     for (Integer predId : containedPredicates) {
       newPredValue[predIdx++] = predId;
     }
     newPredicate.featureValue = newPredValue;
   } else if (newPredicate.type == PredicateType.CUSTOM) {
     // do nothing
   } else {
     // for all other types of predicates, copy the value
     newPredicate.featureValue = (Serializable) oldPredicate.getValue();
   }
   // now see if this is a new predicate or not
   List<Predicate> predsOfType = predicatesByType.get(annotationType);
   if (predsOfType == null) {
     predsOfType = new ArrayList<Predicate>();
     predicatesByType.put(annotationType, predsOfType);
   }
   for (int i = 0; i < predsOfType.size(); i++) {
     if (predsOfType.get(i).equals(newPredicate)) {
       return i;
     }
   }
   // we have a new predicate
   newPredicate.alsoFalse = new int[0];
   newPredicate.alsoTrue = new int[0];
   newPredicate.converselyFalse = new int[0];
   newPredicate.converselyTrue = new int[0];
   predsOfType.add(newPredicate);
   return predsOfType.size() - 1;
 }