public AnnotationFS getAnnotation()

in ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java [73:154]


  public AnnotationFS getAnnotation(MatchContext context, RutaStream stream) {
    if (!initialized) {
      initialize(context, stream);
    }
    if (annotationExpression != null) {
      AnnotationFS annotation = annotationExpression.getAnnotation(context, stream);
      if (featureExpression != null) {
        Collection<? extends AnnotationFS> result = featureExpression
                .getAnnotations(asList(annotation), true, context, stream);
        if (result != null && !result.isEmpty()) {
          return result.iterator().next();
        }
        return null;
      } else {
        return annotation;
      }
    } else if (annotationListExpression != null) {
      List<AnnotationFS> annotations = annotationListExpression.getAnnotationList(context, stream);

      if (annotations != null && !annotations.isEmpty()) {
        if (featureExpression != null) {
          Collection<? extends AnnotationFS> result = featureExpression.getAnnotations(annotations,
                  true, context, stream);
          if (result.isEmpty()) {
            return null;
          }
          annotations = new ArrayList<>(result);
        }
        if (context.getDirection()) {
          return annotations.get(annotations.size() - 1);
        } else {
          return annotations.get(0);
        }
      }
    } else {

      List<Type> types = null;
      if (typeListExpression != null) {
        types = typeListExpression.getTypeList(context, stream);
      } else {
        Type type = getType(context, stream);
        types = type != null ? asList(type) : emptyList();
      }

      for (Type type : types) {

        if (type != null) {

          List<AnnotationFS> bestGuessedAnnotations = null;

          if (featureExpression instanceof FeatureMatchExpression) {
            // allow more matches for feature matches
            bestGuessedAnnotations = stream.getAnnotationsByTypeInContext(type, context);
          } else if (featureExpression != null) {
            bestGuessedAnnotations = stream.getBestGuessedAnnotationsAt(context.getAnnotation(),
                    type);
          } else {
            bestGuessedAnnotations = stream.getBestGuessedAnnotationsAt(context.getAnnotation(),
                    type);
            if (bestGuessedAnnotations.isEmpty()) {
              bestGuessedAnnotations = asList(
                      stream.getSingleAnnotationByTypeInContext(type, context));
            }
          }

          if (featureExpression != null) {
            Collection<AnnotationFS> annotations = new ArrayList<>();
            annotations.addAll(bestGuessedAnnotations);
            Collection<? extends AnnotationFS> featureAnnotations = featureExpression
                    .getAnnotations(annotations, true, context, stream);
            if (featureAnnotations != null && !featureAnnotations.isEmpty()) {
              return featureAnnotations.iterator().next();
            }
          }
          if (bestGuessedAnnotations != null && !bestGuessedAnnotations.isEmpty()) {
            return bestGuessedAnnotations.get(0);
          }
        }
      }
    }
    return null;
  }