Example usage for com.google.gson JsonObject entrySet

List of usage examples for com.google.gson JsonObject entrySet

Introduction

In this page you can find the example usage for com.google.gson JsonObject entrySet.

Prototype

public Set<Map.Entry<String, JsonElement>> entrySet() 

Source Link

Document

Returns a set of members of this object.

Usage

From source file:com.ibm.streamsx.topology.generator.spl.OperatorGenerator.java

License:Open Source License

private void paramClause(JsonObject graphConfig, JsonObject op, StringBuilder sb) {

    // VMArgs only apply to Java SPL operators.
    boolean isJavaOp = OpProperties.LANGUAGE_JAVA.equals(jstring(op, OpProperties.LANGUAGE));

    JsonArray vmArgs = null;/*from  www. j  av  a 2 s  .c o m*/
    if (isJavaOp && graphConfig.has(ContextProperties.VMARGS))
        vmArgs = GsonUtilities.array(graphConfig, ContextProperties.VMARGS);

    // determine if we need to inject submission param names and values
    // info.
    boolean addSPInfo = false;
    ParamsInfo stvOpParamInfo = stvHelper.getSplInfo();
    if (stvOpParamInfo != null) {
        Map<String, JsonObject> functionalOps = stvHelper.getFunctionalOps();
        if (functionalOps.containsKey(op.get("name").getAsString()))
            addSPInfo = true;
    }

    JsonObject params = jobject(op, "parameters");
    if (vmArgs == null && GsonUtilities.jisEmpty(params) && !addSPInfo) {
        return;
    }

    sb.append("    param\n");

    for (Entry<String, JsonElement> on : params.entrySet()) {
        String name = on.getKey();
        JsonObject param = on.getValue().getAsJsonObject();
        if ("vmArg".equals(name)) {
            JsonArray fullVmArgs = new JsonArray();
            fullVmArgs.addAll(GsonUtilities.array(param, "value"));
            if (vmArgs != null)
                fullVmArgs.addAll(vmArgs);
            // stringArray(param, "value", v -> fullVmArgs.);
            // objectArray(graphConfig, ContextProperties.VMARGS, v ->
            // fullVmArgs.add(v));
            vmArgs = fullVmArgs;
            continue;
        }
        sb.append("      ");
        sb.append(name);
        sb.append(": ");
        splValueSupportingSubmission(param, sb);
        sb.append(";\n");
    }

    if (vmArgs != null) {
        JsonObject tmpVMArgParam = new JsonObject();
        tmpVMArgParam.add("value", vmArgs);
        sb.append("      ");
        sb.append("vmArg");
        sb.append(": ");

        splValueSupportingSubmission(tmpVMArgParam, sb);
        sb.append(";\n");
    }

    if (addSPInfo) {
        sb.append("      ");
        sb.append(FunctionalOpProperties.NAME_SUBMISSION_PARAM_NAMES);
        sb.append(": ");
        sb.append(stvOpParamInfo.names);
        sb.append(";\n");

        sb.append("      ");
        sb.append(FunctionalOpProperties.NAME_SUBMISSION_PARAM_VALUES);
        sb.append(": ");
        sb.append(stvOpParamInfo.values);
        sb.append(";\n");
    }
}

From source file:com.ibm.streamsx.topology.generator.spl.OperatorGenerator.java

License:Open Source License

private void outputAssignmentClause(JsonObject graphConfig, JsonObject op, StringBuilder sb) {

    StringBuilder allAssignmentsSb = new StringBuilder();

    objectArray(op, "outputs", output -> {

        if (!output.has("assigns"))
            return;

        JsonObject assigns = object(output, "assigns");

        if (GsonUtilities.jisEmpty(assigns))
            return;

        StringBuilder assignsSb = new StringBuilder();
        String name = jstring(output, "name");
        name = getSPLCompatibleName(name);
        assignsSb.append(name);/*from  ww w  . j av  a 2s. co  m*/
        assignsSb.append(":\n");

        AtomicBoolean seenOne = new AtomicBoolean();
        for (Entry<String, JsonElement> a : assigns.entrySet()) {
            String attr = a.getKey();
            JsonObject value = a.getValue().getAsJsonObject();
            if (seenOne.getAndSet(true))
                assignsSb.append(",\n");
            assignsSb.append("  ");
            assignsSb.append(attr);
            assignsSb.append("=");
            splValueSupportingSubmission(value, assignsSb);

        }
        assignsSb.append(";\n");

        allAssignmentsSb.append(assignsSb);
    });

    if (allAssignmentsSb.length() != 0) {
        sb.append(" output\n");
        sb.append(allAssignmentsSb);
    }
}

From source file:com.ibm.streamsx.topology.generator.spl.OperatorGenerator.java

License:Open Source License

static void configClause(JsonObject graphConfig, JsonObject op, StringBuilder sb) {

    if (!op.has(OpProperties.CONFIG))
        return;/* w  w  w  .  j a  va2 s  . com*/

    JsonObject config = jobject(op, OpProperties.CONFIG);

    StringBuilder sbConfig = new StringBuilder();

    if (config.has("streamViewability")) {
        sbConfig.append("    streamViewability: ");
        sbConfig.append(jboolean(config, "streamViewability"));
        sbConfig.append(";\n");
    }

    if (config.has("queue")) {
        JsonObject queue = jobject(config, "queue");
        if (!queue.entrySet().isEmpty()) {
            sbConfig.append("    threadedPort: queue(");
            sbConfig.append(jstring(queue, "inputPortName") + ", ");
            sbConfig.append(jstring(queue, "congestionPolicy") + ",");
            sbConfig.append(jstring(queue, "queueSize"));
            sbConfig.append(");\n");
        }
    }

    if (config.has(PLACEMENT)) {
        JsonObject placement = jobject(config, PLACEMENT);
        StringBuilder sbPlacement = new StringBuilder();

        // Explicit placement takes precedence.
        String colocationKey = jstring(placement, OpProperties.PLACEMENT_COLOCATE_KEY);
        if (colocationKey != null) {
            JsonObject mapping = object(graphConfig, CFG_COLOCATE_TAG_MAPPING);
            String colocationTag = jstring(mapping, colocationKey);

            sbPlacement.append("      partitionColocation(");
            stringLiteral(sbPlacement, colocationTag);
            sbPlacement.append(")\n");
        }

        Set<String> uniqueResourceTags = new HashSet<>();
        GsonUtilities.stringArray(placement, OpProperties.PLACEMENT_RESOURCE_TAGS, tag -> {
            if (!tag.isEmpty())
                uniqueResourceTags.add(tag);
        });
        if (!uniqueResourceTags.isEmpty()) {
            String hostPool = getHostPoolName(graphConfig, uniqueResourceTags);
            if (sbPlacement.length() != 0)
                sbPlacement.append(",");
            sbPlacement.append("      host(");
            sbPlacement.append(hostPool);
            sbPlacement.append(")\n");
        }

        if (sbPlacement.length() != 0) {
            sbConfig.append("   placement: ");
            sbConfig.append(sbPlacement);
            sbConfig.append("    ;\n");
        }
    }

    if (sbConfig.length() != 0) {
        sb.append("  config\n");
        sb.append(sbConfig);
    }
}

From source file:com.ibm.streamsx.topology.generator.spl.SPLGenerator.java

License:Open Source License

private void generateCompParams(JsonObject graph, StringBuilder sb) {
    JsonObject jparams = GsonUtilities.jobject(graph, "parameters");
    if (jparams != null && jparams.entrySet().size() > 0) {
        sb.append("param\n");
        for (Entry<String, JsonElement> on : jparams.entrySet()) {
            String name = on.getKey();
            JsonObject param = on.getValue().getAsJsonObject();
            String type = jstring(param, "type");

            if (TYPE_COMPOSITE_PARAMETER.equals(type)) {
                JsonObject value = param.get("value").getAsJsonObject();

                sb.append("  ");
                String metaType = jstring(value, "metaType");
                String splType = Types.metaTypeToSPL(metaType);

                sb.append(String.format("expression<%s> $%s", splType, name));
                if (value.has("defaultValue")) {
                    sb.append(" : ");
                    sb.append(value.get("defaultValue").getAsString());
                }//from   www  . jav  a 2s. c om

                sb.append(";\n");
            } else if (TYPE_SUBMISSION_PARAMETER.equals(type))
                ; // ignore - as it was converted to a TYPE_COMPOSITE_PARAMETER
            else
                throw new IllegalArgumentException("Unhandled param name=" + name + " jo=" + param);
        }
    }
}

From source file:com.ibm.streamsx.topology.generator.spl.SubmissionTimeValue.java

License:Open Source License

/**
 * Get a collection of all of the submission parameters used in the graph.
 * @param graph/*from  ww  w .  j  a  va  2s  .c  om*/
 * @return {@code map<spOpParamName,spParam>}
 */
private void createMainCompositeParamsForAllSubmissionValues(JsonObject graph) {
    Map<String, JsonObject> all = this.allSubmissionParams;

    JsonObject params = GsonUtilities.jobject(graph, "parameters");

    if (params != null) {
        for (Entry<String, JsonElement> e : params.entrySet()) {
            JsonObject param = e.getValue().getAsJsonObject();
            if (TYPE_SUBMISSION_PARAMETER.equals(jstring(param, "type"))) {
                JsonObject sp = jobject(param, "value");
                all.put(jstring(sp, "name"), param);
            }
        }
    }

    for (JsonObject param : all.values())
        addMainCompositeParam(params, param);
}

From source file:com.ibm.streamsx.topology.generator.spl.SubmissionTimeValue.java

License:Open Source License

/**
 * Enrich the json composite operator definition's parameters
 * to include parameters for submission parameters.
 * <p>/*  w  w  w  . j a va 2  s. c om*/
 * The composite is augmented with a TYPE_SUBMISSION_PARAMETER parameter
 * for each submission parameter used within the composite - e.g, as
 * a parallel width value or SPL operator parameter value.
 * <p>
 * If the composite has any functional operator children, enrich
 * the composite to have declarations for all submission parameters.
 * Also accumulate the functional children and make them available via
 * {@link #getFunctionalOps()}.
 * 
 * @param composite the composite definition
 */

void addJsonParamDefs(JsonObject composite) {
    // scan immediate children ops for submission param use
    // and add corresponding param definitions to the composite.
    // Also, if the op has functional logic, enrich the op too...
    // and further enrich the composite.

    if (allSubmissionParams.isEmpty())
        return;

    // scan for spParams
    JsonObject spParams = new JsonObject();
    AtomicBoolean addedAll = new AtomicBoolean();
    GsonUtilities.objectArray(composite, "operators", op -> {
        JsonObject params = jobject(op, "parameters");
        if (params != null) {
            boolean addAll = false;
            for (Entry<String, JsonElement> p : params.entrySet()) {
                // if functional logic add "submissionParameters" param
                if (params.has(FUNCTIONAL_LOGIC_PARAM)) {
                    functionalOps.put(jstring(op, "name"), op);
                    addAll = true;
                    break;
                } else {
                    JsonObject param = p.getValue().getAsJsonObject();
                    String type = jstring(param, "type");
                    if (TYPE_SUBMISSION_PARAMETER.equals(type)) {
                        addInnerCompositeParameter(spParams, param);
                    }
                }
            }
            if (addAll && !addedAll.getAndSet(true)) {
                for (String name : allSubmissionParams.keySet()) {
                    addInnerCompositeParameter(spParams, allSubmissionParams.get(name));
                }
            }
        }
        boolean isParallel = jboolean(op, "parallelOperator");
        if (isParallel) {
            JsonElement width = op.get("parallelInfo").getAsJsonObject().get("width");
            if (width.isJsonObject()) {
                JsonObject jwidth = width.getAsJsonObject();
                String type = jstring(jwidth, "type");
                if (TYPE_SUBMISSION_PARAMETER.equals(type)) {
                    addInnerCompositeParameter(spParams, jwidth);
                }
            }
        }
    });

    // augment the composite's parameters
    JsonObject params = jobject(composite, "parameters");
    if (params == null && !GsonUtilities.jisEmpty(spParams)) {
        params = new JsonObject();
        composite.add("parameters", params);
    }
    for (Entry<String, JsonElement> p : spParams.entrySet()) {
        String pname = p.getKey();
        if (!params.has(pname))
            params.add(pname, spParams.get(pname));
    }

    // make the results of our efforts available to addJsonInstanceParams
    composite.add(OP_ATTR_SPL_SUBMISSION_PARAMS, spParams);
}

From source file:com.ibm.streamsx.topology.generator.spl.SubmissionTimeValue.java

License:Open Source License

/**
 * Akin to addJsonParamDefs(), enrich the json composite operator instance's
 * invocation parameters with submission parameter references.
 * @param compInstance the composite instance
 * @param composite the composite definition
 *//*  ww  w. j  av a  2s  .c o  m*/
void addJsonInstanceParams(JsonObject compInstance, JsonObject composite) {
    JsonObject spParams = jobject(composite, OP_ATTR_SPL_SUBMISSION_PARAMS);
    if (spParams != null) {
        JsonObject opParams = jobject(compInstance, "parameters");
        if (opParams == null) {
            compInstance.add("parameters", opParams = new JsonObject());
        }
        for (Entry<String, JsonElement> p : spParams.entrySet()) {
            JsonObject spParam = p.getValue().getAsJsonObject();

            // need to end up generating: __spl_stv_foo : $__spl_stv_foo;
            opParams.add(p.getKey(), compositeParameterReference(spParam));
        }
    }
}

From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java

License:Open Source License

/**
 * Return is empty meaning null, JSON null or an empty object.
 */// w ww. jav  a 2  s. co  m
public static boolean jisEmpty(JsonObject object) {
    return object == null || object.isJsonNull() || object.entrySet().isEmpty();
}

From source file:com.ibm.streamsx.topology.internal.gson.GsonUtilities.java

License:Open Source License

/**
 * Add all the properties in the {@code source} JSON object into {@code target} JSON object. Existing properties will be overridden.
 * <p>//ww  w.  j  ava2s .  c  o m
 * E.g. if {@code target} contains:
 * <pre><code>
 *       { "t1": {}, "t2": {} }
 * </code></pre>
 * and {@code source} contains:
 * <pre><code>
 *       { "s1": {}, "s2": {}, "s3": {} }
 * </code></pre>
 * then {@code target} after the call, contains:
 * <pre><code>
 *       { "t1": {}, "t2": {}, "s1": {}, "s2": {}, "s3": {} }
 * </code></pre>
 * 
 * @param target JSON object to copy properties to
 * @param source JSON object to receive properties from
 * @return modified target JSON object
 */
public static JsonObject addAll(JsonObject target, JsonObject source) {
    for (Entry<String, JsonElement> entry : source.entrySet()) {
        target.add(entry.getKey(), entry.getValue());
    }
    return target;
}

From source file:com.ibm.streamsx.topology.internal.streams.JobConfigOverlay.java

License:Open Source License

/**
 * Add the job config overlays to the/*from  w w  w  . j av  a 2s.  c o m*/
 * top-level submission deployment object.
 */
public JsonObject fullOverlayAsJSON(JsonObject deploy) {

    JsonObject overlay = new JsonObject();

    // JobConfig
    JsonObject jsonJobConfig = gson().toJsonTree(jobConfig).getAsJsonObject();
    if (!jsonJobConfig.entrySet().isEmpty()) {
        overlay.add(JOB_CONFIG, jsonJobConfig);
    }

    if (jobConfig.getOverrideResourceLoadProtection() != null) {
        JsonObject operationConfig = new JsonObject();
        operationConfig.addProperty(OVERRIDE_RESOURCE_LOAD_PROTECTION,
                jobConfig.getOverrideResourceLoadProtection());
        overlay.add(OPERATION_CONFIG, operationConfig);
    }

    // Create the top-level structure.
    JsonArray jcos = new JsonArray();
    jcos.add(overlay);
    deploy.add(JOB_CONFIG_OVERLAYS, jcos);

    return deploy;
}