Ejemplo n.º 1
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;
 }