Beispiel #1
0
 protected void optimisePredicates() {
   for (List<Predicate> preds : predicatesByType.values()) {
     IntArrayList[] alsoTrue = new IntArrayList[preds.size()];
     IntArrayList[] alsoFalse = new IntArrayList[preds.size()];
     IntArrayList[] convTrue = new IntArrayList[preds.size()];
     IntArrayList[] convFalse = new IntArrayList[preds.size()];
     for (int i = 0; i < preds.size(); i++) {
       alsoTrue[i] = new IntArrayList(preds.size());
       alsoFalse[i] = new IntArrayList(preds.size());
       convTrue[i] = new IntArrayList(preds.size());
       convFalse[i] = new IntArrayList(preds.size());
     }
     for (int i = 0; i < preds.size() - 1; i++) {
       Predicate one = preds.get(i);
       for (int j = i + 1; j < preds.size(); j++) {
         Predicate other = preds.get(j);
         switch (one.type) {
           case EQ:
             switch (other.type) {
               case EQ:
                 if (one.annotationAccessor.equals(other.annotationAccessor)) {
                   if (one.featureValue.equals(other.featureValue)) {
                     alsoTrue[i].add(j);
                     alsoTrue[j].add(i);
                   } else {
                     convFalse[i].add(j);
                     convFalse[j].add(i);
                   }
                 }
                 break;
               default:
             }
             break;
           default:
         }
       } // for j
     } // for i
     for (int i = 0; i < preds.size(); i++) {
       Predicate pred = preds.get(i);
       pred.alsoTrue = Arrays.copyOfRange(alsoTrue[i].elements(), 0, alsoTrue[i].size());
       pred.alsoFalse = Arrays.copyOfRange(alsoFalse[i].elements(), 0, alsoFalse[i].size());
       pred.converselyTrue = Arrays.copyOfRange(convTrue[i].elements(), 0, convTrue[i].size());
       pred.converselyFalse = Arrays.copyOfRange(convFalse[i].elements(), 0, convFalse[i].size());
     }
   } // for preds
 }
Beispiel #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;
 }