private static View copyChildView(
      GroupByView groupView,
      String[] propertyNames,
      Object groupByValues,
      AgentInstanceViewFactoryChainContext agentInstanceContext,
      View originalChildView) {
    if (originalChildView instanceof MergeView) {
      String message = "Unexpected merge view as child of group-by view";
      log.fatal(".copySubViews " + message);
      throw new EPException(message);
    }

    if (!(originalChildView instanceof CloneableView)) {
      throw new EPException(
          "Unexpected error copying subview " + originalChildView.getClass().getName());
    }
    CloneableView cloneableView = (CloneableView) originalChildView;

    // Copy child node
    View copyChildView = cloneableView.cloneView();
    copyChildView.setParent(groupView);

    // Make the sub views for child copying from the original to the child
    copySubViews(
        groupView.getCriteriaExpressions(),
        propertyNames,
        groupByValues,
        originalChildView,
        copyChildView,
        agentInstanceContext);

    return copyChildView;
  }
  private static void writeView(View view, JsonObject json) {
    writeString("viewType", view.getClass().getCanonicalName(), json);

    writeString("viewName", view.getViewName(), json);

    writeString("generator", view.getViewGenerator(), json);

    if (view.getScore() != 0) writeDouble("score", view.getScore(), json);

    List<Constituent> constituents = view.getConstituents();

    if (constituents.size() > 0) {
      JsonArray cJson = new JsonArray();
      for (int i = 0; i < view.getNumberOfConstituents(); i++) {

        Constituent constituent = constituents.get(i);
        JsonObject c = new JsonObject();
        writeConstituent(constituent, c);

        cJson.add(c);
      }

      json.add("constituents", cJson);
    }

    List<Relation> relations = view.getRelations();

    if (relations.size() > 0) {

      JsonArray rJson = new JsonArray();

      for (Relation r : relations) {
        Constituent src = r.getSource();
        Constituent tgt = r.getTarget();

        int srcId = constituents.indexOf(src);
        int tgtId = constituents.indexOf(tgt);

        JsonObject rJ = new JsonObject();

        writeString("relationName", r.getRelationName(), rJ);

        if (r.getScore() != 0) writeDouble("score", r.getScore(), rJ);
        writeInt("srcConstituent", srcId, rJ);
        writeInt("targetConstituent", tgtId, rJ);
        writeAttributes(r, rJ);

        Map<String, Double> labelsToScores = r.getLabelsToScores();

        if (null != labelsToScores) writeLabelsToScores(labelsToScores, rJ);

        rJson.add(rJ);
      }

      json.add("relations", rJson);
    }
  }