Exemple #1
0
  public static List<NCube> getNCubesFromResource(String name) {
    String lastSuccessful = "";
    try {
      Object[] cubes = getJsonObjectFromResource(name);
      List<NCube> cubeList = new ArrayList<>(cubes.length);

      for (Object cube : cubes) {
        JsonObject ncube = (JsonObject) cube;
        String json = JsonWriter.objectToJson(ncube);
        NCube nCube = NCube.fromSimpleJson(json);
        nCube.sha1();
        addCube(nCube.getApplicationID(), nCube);
        lastSuccessful = nCube.getName();
        cubeList.add(nCube);
      }

      return cubeList;
    } catch (Exception e) {
      String s =
          "Failed to load cubes from resource: "
              + name
              + ", last successful cube: "
              + lastSuccessful;
      LOG.warn(s);
      throw new RuntimeException(s, e);
    }
  }
  /** {@inheritDoc} */
  @SuppressWarnings("rawtypes")
  @Override
  public JSONObject serialize(final BasicOperator operator, final int node_id)
      throws JSONException {
    final JSONObject json = new JSONObject();
    /*
     * store preceeding's and remove them from the subgraph, because they
     * are not to be serialized (but they are added later)
     */
    final List<BasicOperator> storePrecds =
        new LinkedList<BasicOperator>(((SubgraphContainer) operator).getPrecedingOperators());
    for (final BasicOperator op : storePrecds) {
      ((SubgraphContainer) operator).removePrecedingOperator(op);
    }
    /*
     * store succeeding's and remove them from the subgraph (see above)
     */
    final List<OperatorIDTuple> storeSuccs =
        new LinkedList<OperatorIDTuple>(((SubgraphContainer) operator).getSucceedingOperators());
    for (final OperatorIDTuple op : storeSuccs) {
      ((SubgraphContainer) operator).removeSucceedingOperator(op);
    }

    json.put("type", operator.getClass().getName());
    json.put("node_id", node_id);

    /*
     * now serialize the subgraph container
     */
    final SubgraphContainerFormatter serializer = new SubgraphContainerFormatter();
    final JSONObject serializedGraph =
        serializer.serialize(((SubgraphContainer) operator).getRootOfSubgraph(), 0);
    json.put("subgraph", serializedGraph);

    /*
     * now serialize the subgraph container's key
     */
    final Object key = ((SubgraphContainer) operator).getKey();
    try {
      final String sgKey = JsonWriter.objectToJson(key);
      json.put("key", sgKey);
    } catch (final IOException e) {
      json.put("key", "Not serializable.");
      propagate(
          new RuntimeException(
              String.format("The key of subgraphContainer %s is not serialzable.", operator)));
    }

    // now add the connections ... we have removed above.
    for (final BasicOperator op : storePrecds) {
      ((SubgraphContainer) operator).addPrecedingOperator(op);
    }
    for (final OperatorIDTuple op : storeSuccs) {
      ((SubgraphContainer) operator).addSucceedingOperator(op);
    }
    return json;
  }