/**
   * Converts all model blocks of the given subsystem.
   *
   * @param subSys sub system which model references should be converted
   * @param builder builder to use
   */
  protected void convertDeltaModelBlocks(String subSys, DeltaMontiArcModelBuilder builder) {
    String[] modelReferences = getModelReference(subSys);
    for (String modelRef : modelReferences) {
      DeltaOperation op = getDeltaOperationForElement(modelRef);
      String simpleName = matlab.doGetSimpleNameLimiterBased(modelRef);
      switch (op) {
        case ADD:
          builder.printOperation(op);
          String type = getModelType(modelRef);
          // type = MontiArcStringBuilder.EXPORT_PACKAGE + "." + type.trim();
          builder.createComponentReference(type, simpleName);

          break;
        case REPLACE:
          int withIndex = simpleName.indexOf(DeltaSimulinkKeyWords.REPLACE_WITH);
          if (!simpleName.startsWith(DeltaSimulinkKeyWords.REPLACE) || withIndex == -1) {
            handler.addError("Unable to parse replace statement!", simpleName);
          } else {

            String toReplace =
                simpleName.substring(DeltaSimulinkKeyWords.REPLACE.length(), withIndex);
            String with =
                simpleName.substring(withIndex + DeltaSimulinkKeyWords.REPLACE_WITH.length());
            // with = MontiArcStringBuilder.EXPORT_PACKAGE + "." + with.trim();
            builder.createReplaceStatement(toReplace, with);
          }
          break;
        default:
          break;
      }
    }
  }
  protected void convertSubSystemsAndRoot(
      String rootElement, MontiArcStringBuilder builder, boolean instantiate) {
    String simpleName = matlab.doGetSimpleNameLimiterBased(rootElement);

    if (getDeltaOperationForElement(rootElement) == DeltaOperation.REPLACE) {
      simpleName =
          simpleName.substring(
              simpleName.indexOf(DeltaSimulinkKeyWords.REPLACE_WITH)
                  + DeltaSimulinkKeyWords.REPLACE_WITH.length());
    }
    simpleName = simpleName.trim();
    if (instantiate) {
      builder.startCreateInnerComponent(simpleName, simpleName);
    } else {
      builder.startCreateComponent(simpleName);
    }
    convertPorts(rootElement, builder);

    convertSubSystems(rootElement, builder);

    convertModelReferences(rootElement, builder);

    convertSignals(rootElement, builder);

    builder.endCreateInnerComponent();
  }
  /** @param deltaModelName */
  protected void convertDeltaSubSystems(String deltaModelName, DeltaMontiArcModelBuilder builder) {
    List<String> subSystems = loadSubSystems(deltaModelName);

    for (String subSys : subSystems) {
      DeltaOperation op = getDeltaOperationForElement(subSys);
      String simpleName = matlab.doGetSimpleNameLimiterBased(subSys);

      // modify block
      if (op == DeltaOperation.MODIFY) {
        modifySubSystemBlock(builder, subSys, simpleName);
      }
      // add subsystem block
      else if (op == DeltaOperation.ADD) {
        builder.printOperation(op);
        convertSubSystemsAndRoot(subSys, builder, true);
      }
      // replace subsystem block
      else if (op == DeltaOperation.REPLACE) {
        int withIndex = simpleName.indexOf(DeltaSimulinkKeyWords.REPLACE_WITH);
        if (!simpleName.startsWith(DeltaSimulinkKeyWords.REPLACE) || withIndex == -1) {
          handler.addError("Unable to parse replace statement!", simpleName);
        } else {
          String toReplace =
              simpleName.substring(DeltaSimulinkKeyWords.REPLACE.length(), withIndex);

          String newName = matlab.doGetSimpleNameLimiterBased(subSys);

          newName =
              newName.substring(
                  newName.indexOf(DeltaSimulinkKeyWords.REPLACE_WITH)
                      + DeltaSimulinkKeyWords.REPLACE_WITH.length());
          newName = newName.trim();

          StringBuffer typeName = new StringBuffer();
          String sep = "";
          for (String part : currentComponentName) {
            typeName.append(sep);
            sep = ".";
            typeName.append(part);
          }
          typeName.append(sep);
          typeName.append(newName);

          // remove old component definition
          builder.addSingleLineComment("replace subsystem " + toReplace);

          builder.printOperation(DeltaOperation.ADD);
          convertSubSystemsAndRoot(subSys, builder, false);
          builder.createReplaceStatement(toReplace, typeName.toString() + " " + newName);
        }
      }
    }
  }
  protected String convertConnectorElement(String blockName, String portName) {
    String result = blockName;
    if (result.startsWith(DeltaSimulinkKeyWords.MODIFY)) {
      result = result.substring(DeltaSimulinkKeyWords.MODIFY.length());
    } else if (result.startsWith(DeltaSimulinkKeyWords.REPLACE)
        && result.contains(DeltaSimulinkKeyWords.REPLACE_WITH)) {
      result =
          result
              .substring(
                  result.lastIndexOf(DeltaSimulinkKeyWords.REPLACE_WITH)
                      + DeltaSimulinkKeyWords.REPLACE_WITH.length())
              .trim();

      int spaceIndex = result.indexOf(" ");
      // model block
      if (spaceIndex != -1) {
        result = result.substring(spaceIndex + 1);
      }
    }
    if (portName != null) {
      result = result + "." + portName;
    }
    return result;
  }