protected void callEngine()

in ruta-core/src/main/java/org/apache/uima/ruta/action/CallAction.java [97:205]


  protected void callEngine(MatchContext context, InferenceCrowd crowd,
          AnalysisEngine targetEngine, RutaStream stream) throws ResourceInitializationException,
          AnalysisEngineProcessException {

    RuleElement element = context.getElement();
    RuleMatch match = context.getRuleMatch();

    List<AnnotationFS> matchedAnnotations = match.getMatchedAnnotations(null,
            element.getContainer());
    for (AnnotationFS matchedAnnotation : matchedAnnotations) {

      StringBuilder newDocument = new StringBuilder();
      RutaStream windowStream = stream.getWindowStream(matchedAnnotation,
              stream.getDocumentAnnotationType());
      windowStream.moveToFirst();

      CAS newCAS = targetEngine.newCAS();
      List<Type> types = newCAS.getTypeSystem()
              .getProperlySubsumedTypes(newCAS.getAnnotationType());

      Collection<AnnotationFS> fsToAdd = new HashSet<AnnotationFS>();

      Map<Integer, Integer> new2oldBegin = new TreeMap<Integer, Integer>();
      Map<Integer, Integer> new2oldEnd = new TreeMap<Integer, Integer>();
      Map<Integer, Integer> old2newBegin = new TreeMap<Integer, Integer>();
      Map<Integer, Integer> old2newEnd = new TreeMap<Integer, Integer>();

      int localBegin = 0;
      int localEnd = 0;
      while (windowStream.isValid()) {
        FeatureStructure fs = windowStream.get();
        if (fs instanceof RutaBasic) {
          RutaBasic basic = (RutaBasic) fs;
          for (Type type : types) {
            Collection<AnnotationFS> beginAnchors = basic.getBeginAnchors(type);
            for (AnnotationFS a : beginAnchors) {
              if (a != null && !a.getType().getName().equals(CAS.TYPE_NAME_DOCUMENT_ANNOTATION)
                      && !(a instanceof RutaBasic)) {
                fsToAdd.add(a);
              }
            }
          }
          int length = basic.getEnd() - basic.getBegin();
          localEnd = localBegin + length;

          new2oldBegin.put(localBegin, basic.getBegin());
          old2newBegin.put(basic.getBegin(), localBegin);
          new2oldEnd.put(localEnd, basic.getEnd());
          old2newEnd.put(basic.getEnd(), localEnd);

          newDocument.append(basic.getCoveredText());

          localBegin += length;
        }
        windowStream.moveToNext();
      }

      String string = newDocument.toString();
      newCAS.setDocumentText(string);
      for (AnnotationFS each : fsToAdd) {
        int beginOld = each.getBegin();
        int endOld = each.getEnd();

        Integer beginNew = old2newBegin.get(beginOld);
        Integer endNew = old2newEnd.get(endOld);
        if (endNew == null && beginNew != null) {
          int delta = endOld - beginOld;
          endNew = beginNew + delta;
        } else if (endNew != null && beginNew == null) {
          int delta = endOld - beginOld;
          beginNew = endNew - delta;
        } else if (endNew == null && beginNew == null) {
          int index;
          int deltaBefore = 0;
          int deltaAfter = 0;
          Integer valueBegin = null;
          index = beginOld;
          while (valueBegin == null) {
            valueBegin = new2oldBegin.get(++index);
            deltaBefore++;
          }
          Integer valueEnd = null;
          index = endOld;
          while (valueEnd == null) {
            valueEnd = new2oldEnd.get(--index);
            deltaAfter++;
          }
          beginNew = valueBegin - deltaBefore;
          endNew = valueEnd + deltaAfter;
        }

        String typeName = each.getType().getName();
        Type type = newCAS.getTypeSystem().getType(typeName);
        FeatureStructure newAnnotation = newCAS.createAnnotation(type, beginNew, endNew);
        newCAS.addFsToIndexes(newAnnotation);
      }

      targetEngine.process(newCAS);

      for (Type type : types) {
        FSIterator<AnnotationFS> iterator = newCAS.getAnnotationIndex(type).iterator();
        while (iterator.isValid()) {
          AnnotationFS each = iterator.get();
          transform(each, new2oldBegin, new2oldEnd, fsToAdd, stream, match);
          iterator.moveToNext();
        }
      }
    }
  }