Example usage for com.google.gson JsonArray get

List of usage examples for com.google.gson JsonArray get

Introduction

In this page you can find the example usage for com.google.gson JsonArray get.

Prototype

public JsonElement get(int i) 

Source Link

Document

Returns the ith element of the array.

Usage

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

License:Open Source License

/**
 * When we create a composite, operators need to create connections with the composite's input port.
 * @param graph/* ww  w.ja va 2 s.c  o  m*/
 * @param startsEndsAndOperators
 * @param opDefinition
 */
private void fixCompositeInputNaming(JsonObject graph, List<List<JsonObject>> startsEndsAndOperators,
        JsonObject opDefinition) {
    // For each start
    // We iterate like this because we need to also index into the operatorDefinition's inputNames list.
    for (int i = 0; i < startsEndsAndOperators.get(0).size(); i++) {
        JsonObject start = startsEndsAndOperators.get(0).get(i);

        //If the start is a source, the name doesn't need fixing.
        // Only input ports that now connect to the Composite input need to be fixed.
        if (start.has("config") && (hasAny(object(start, "config"), compOperatorStarts)))
            continue;

        // Given its output port name
        // Region markers like $Parallel$ only have one input and output
        String outputPortName = GsonUtilities
                .jstring(start.get("outputs").getAsJsonArray().get(0).getAsJsonObject(), "name");

        // for each operator downstream from this start
        for (JsonObject downstream : GraphUtilities.getDownstream(start, graph)) {
            // for each input in the downstream operator
            JsonArray inputs = array(downstream, "inputs");
            for (JsonElement inputObj : inputs) {
                JsonObject input = inputObj.getAsJsonObject();
                // for each connection in that input
                JsonArray connections = array(input, "connections");
                for (int j = 0; j < connections.size(); j++) {

                    // Replace the connection with the composite input port name if the 
                    // port has a connection to the start operator. 
                    if (connections.get(j).getAsString().equals(outputPortName)) {
                        connections.set(j, GsonUtilities.array(opDefinition, "inputNames").get(i));
                    }
                }
            }
        }
    }

}

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

License:Open Source License

private void generateMainCompConfig(JsonObject graphConfig, StringBuilder sb) {
    JsonArray hostPools = array(graphConfig, "__spl_hostPools");
    boolean hasHostPools = hostPools != null && hostPools.size() != 0;

    JsonObject checkpoint = GsonUtilities.jobject(graphConfig, "checkpoint");

    boolean hasCheckpoint = checkpoint != null;

    if (hasHostPools || hasCheckpoint)
        sb.append("  config\n");

    if (hasHostPools) {
        boolean seenOne = false;
        for (JsonElement hpo : hostPools) {
            if (!seenOne) {
                sb.append("    hostPool:\n");
                seenOne = true;/*from   www  . j  a v a  2  s  . com*/
            } else {
                sb.append(",");
            }
            JsonObject hp = hpo.getAsJsonObject();
            String name = jstring(hp, "name");
            JsonArray resourceTags = array(hp, "resourceTags");

            sb.append("    ");
            sb.append(name);
            sb.append("=createPool({tags=[");
            for (int i = 0; i < resourceTags.size(); i++) {
                if (i != 0)
                    sb.append(",");
                stringLiteral(sb, resourceTags.get(i).getAsString());
            }
            sb.append("]}, Sys.Shared)");
        }
        sb.append(";\n");
    }

    if (hasCheckpoint) {
        TimeUnit unit = TimeUnit.valueOf(jstring(checkpoint, "unit"));
        long period = checkpoint.get("period").getAsLong();

        // SPL works in seconds, including fractions.
        long periodMs = unit.toMillis(period);
        double periodSec = ((double) periodMs) / 1000.0;
        sb.append("    checkpoint: periodic(");
        sb.append(periodSec);
        sb.append(");\n");
    }
}

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

License:Open Source License

/**
 * Add an arbitrary SPL value./* w  w w.  ja v a  2  s  .  c om*/
 * JsonObject has a type and a value. 
 */
static void value(StringBuilder sb, JsonObject tv) {

    JsonElement value = tv.get("value");

    String type = JParamTypes.TYPE_SPL_EXPRESSION;
    if (tv.has("type")) {
        type = tv.get("type").getAsString();
    } else {
        if (value.isJsonPrimitive()) {
            JsonPrimitive pv = value.getAsJsonPrimitive();
            if (pv.isString())
                type = "RSTRING";
        } else if (value.isJsonArray()) {
            type = "RSTRING";
        }
    }

    if (value.isJsonArray()) {
        JsonArray array = value.getAsJsonArray();

        for (int i = 0; i < array.size(); i++) {
            if (i != 0)
                sb.append(", ");
            value(sb, type, array.get(i));
        }
    } else {
        value(sb, type, value);
    }
}

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

License:Open Source License

@SuppressWarnings("serial")
static void preProcessThreadedPorts(final JsonObject graph) {
    // Remove the threaded port configuration from the operator and its 
    // params if:
    // 1) The operator has a lowLatencyTag assigned
    // 2) The upstream operator has a different colocationTag as the 
    //    the operator.

    // Added threaded port configuration if the operator is non-functional
    // and it has a threaded port.

    Set<JsonObject> starts = GraphUtilities.findStarts(graph);
    GraphUtilities.visitOnce(starts, null, graph, new Consumer<JsonObject>() {

        @Override//w w  w .  j av a  2  s .com
        public void accept(JsonObject op) {
            // These booleans will be used to determine whether to delete the
            // threaded port from the operator.         
            boolean regionTagExists = false;
            boolean differentColocationThanParent = false;
            boolean functional = false;

            JsonArray inputs = array(op, "inputs");

            // Currently, threadedPorts are only supported on operators
            // with one input port.
            if (inputs == null || inputs.size() != 1) {
                return;
            }

            JsonObject input = inputs.get(0).getAsJsonObject();
            JsonObject queue = jobject(input, "queue");
            // If the queue is null, simply return. Nothing to be done.
            if (queue == null) {
                return;
            }

            // If the operator is not functional, the we don't have to 
            // remove anything from the operator's params.
            functional = jboolean(queue, "functional");

            JsonObject placement = jobject(op, OpProperties.PLACEMENT);

            // See if operator is in a lowLatency region
            String regionTag = null;
            if (placement != null) {
                regionTag = jstring(placement, OpProperties.PLACEMENT_LOW_LATENCY_REGION_ID);
            }
            if (regionTag != null && !regionTag.isEmpty()) {
                regionTagExists = true;
            }

            // See if operator has different colocation tag than any of 
            // its parents.

            String colocTag = null;
            if (placement != null) {
                colocTag = jstring(placement, OpProperties.PLACEMENT_ISOLATE_REGION_ID);
            }

            for (JsonObject parent : getUpstream(op, graph)) {
                JsonObject parentPlacement = nestedObject(parent, OpProperties.CONFIG, OpProperties.PLACEMENT);
                String parentColocTag = null;
                if (parentPlacement != null)
                    parentColocTag = jstring(parentPlacement, OpProperties.PLACEMENT_ISOLATE_REGION_ID);
                // Test whether colocation tags are different. If they are,
                // don't insert a threaded port.
                if (!colocTag.equals(parentColocTag)) {
                    differentColocationThanParent = true;
                }
            }

            // Remove the threaded port if necessary
            if (differentColocationThanParent || regionTagExists) {
                input.remove("queue");
                if (functional) {
                    JsonObject params = jobject(op, "parameters");
                    params.remove("queueSize");
                }
            }

            if (functional && !(differentColocationThanParent || regionTagExists)) {
                return;
            }

            // Add to SPL operator config if necessary
            if (!functional && !(differentColocationThanParent || regionTagExists)) {
                JsonObject newQueue = objectCreate(op, OpProperties.CONFIG, "queue");
                newQueue.addProperty("queueSize", new Integer(100));
                newQueue.addProperty("inputPortName", input.get("name").getAsString());
                newQueue.addProperty("congestionPolicy", "Sys.Wait");
            }
        }

    });
}

From source file:com.ibm.streamsx.topology.internal.context.remote.BuildServiceRemoteRESTWrapper.java

License:Open Source License

void remoteBuildAndSubmit(JsonObject submission, File archive) throws ClientProtocolException, IOException {
    JsonObject deploy = DeployKeys.deploy(submission);
    JsonObject graph = object(submission, "graph");
    String graphBuildName = jstring(graph, "name");

    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*from   w w  w  .  j  a v a 2s  .c  o  m*/
        String serviceName = jstring(service, "name");
        RemoteContext.REMOTE_LOGGER.info("Streaming Analytics service (" + serviceName + "): Checking status");
        RestUtils.checkInstanceStatus(httpclient, this.service);

        String apiKey = RestUtils.getAPIKey(credentials);

        // Perform initial post of the archive
        String buildName = graphBuildName + "_" + randomHex(16);
        buildName = URLEncoder.encode(buildName, StandardCharsets.UTF_8.name());
        RemoteContext.REMOTE_LOGGER
                .info("Streaming Analytics service (" + serviceName + "): submitting build " + buildName);
        JsonObject jso = doUploadBuildArchivePost(httpclient, apiKey, archive, buildName);

        JsonObject build = object(jso, "build");
        String buildId = jstring(build, "id");
        String outputId = jstring(build, "output_id");

        // Loop until built
        String status = buildStatusGet(buildId, httpclient, apiKey);
        while (!status.equals("built")) {
            // 'building', 'notBuilt', and 'waiting' are all states which can eventualy result in 'built'
            // sleep and continue to monitor
            if (status.equals("building") || status.equals("notBuilt") || status.equals("waiting")) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                status = buildStatusGet(buildId, httpclient, apiKey);
                continue;
            }
            // The remaining possible states are 'failed', 'timeout', 'canceled', 'canceling', and 'unknown', none of which can lead to a state of 'built', so we throw an error.
            else {
                RemoteContext.REMOTE_LOGGER
                        .severe("Streaming Analytics service (" + serviceName + "): The submitted archive "
                                + archive.getName() + " failed to build with status " + status + ".");
                JsonObject output = getBuildOutput(buildId, outputId, httpclient, apiKey);
                String strOutput = "";
                if (output != null)
                    strOutput = prettyPrintOutput(output);
                throw new IllegalStateException("Error submitting archive for compilation: \n" + strOutput);
            }
        }

        // Now perform archive put
        build = getBuild(buildId, httpclient, apiKey);

        JsonArray artifacts = array(build, "artifacts");
        if (artifacts == null || artifacts.size() == 0) {
            throw new IllegalStateException("No artifacts associated with build " + buildId);
        }

        // TODO: support multiple artifacts associated with a single build.
        String artifactId = jstring(artifacts.get(0).getAsJsonObject(), "id");
        RemoteContext.REMOTE_LOGGER
                .info("Streaming Analytics service (" + serviceName + "): submitting job request.");
        JsonObject response = doSubmitJobFromBuildArtifactPut(httpclient, deploy, apiKey, artifactId);

        // Pass back to Python
        final JsonObject submissionResult = GsonUtilities.objectCreate(submission,
                RemoteContext.SUBMISSION_RESULTS);
        GsonUtilities.addAll(submissionResult, response);
    } finally {
        httpclient.close();

    }
}

From source file:com.ibm.streamsx.topology.internal.context.remote.ToolkitRemoteContext.java

License:Open Source License

/**
 * Create a Job Config Overlays structure if it does not exist.
 * Set the deployment from the graph config.
 *//*from  w  w w .  ja va 2  s. com*/
private void setupJobConfigOverlays(JsonObject deploy, JsonObject graph) {
    JsonArray jcos = array(deploy, JOB_CONFIG_OVERLAYS);
    if (jcos == null) {
        deploy.add(JOB_CONFIG_OVERLAYS, jcos = new JsonArray());
        jcos.add(new JsonObject());
    }
    JsonObject jco = jcos.get(0).getAsJsonObject();

    JsonObject graphDeployment = GsonUtilities.object(graph, "config", DEPLOYMENT_CONFIG);

    if (!jco.has(DEPLOYMENT_CONFIG)) {
        jco.add(DEPLOYMENT_CONFIG, graphDeployment);
        return;
    }

    JsonObject deployment = object(jco, DEPLOYMENT_CONFIG);

    // Need to merge with the graph taking precedence.
    addAll(deployment, graphDeployment);

    if ("legacy".equals(GsonUtilities.jstring(deployment, "fusionScheme "))) {
        if (deployment.has("fusionTargetPeCount"))
            deployment.remove("fusionTargetPeCount");
    }
}

From source file:com.ibm.streamsx.topology.internal.context.remote.ToolkitRemoteContext.java

License:Open Source License

/**
 * Looks for "includes" in the graph config which will be
 * a list of JSON object representing files or directories to copy
 * into the toolkit, with source being the file or directory path
 * and target being the target directory relative to toolkitRoot.
 * @param toolkitRoot//  w  w w .j  a v  a  2s . com
 * @param json
 * @throws IOException
 * 
 * TODO add support for directories
 */
private void copyIncludes(File toolkitRoot, JsonObject graph) throws IOException {

    JsonObject config = object(graph, "config");

    if (!config.has("includes"))
        return;

    JsonArray includes = array(config, "includes");

    for (int i = 0; i < includes.size(); i++) {
        JsonObject inc = includes.get(i).getAsJsonObject();

        String target = jstring(inc, "target");
        File targetDir = new File(toolkitRoot, target);
        if (!targetDir.exists())
            targetDir.mkdirs();

        // Simple copy of a file or directory
        if (inc.has("source")) {
            String source = jstring(inc, "source");
            File srcFile = new File(source);
            if (srcFile.isFile())
                copyFile(srcFile, targetDir);
            else if (srcFile.isDirectory())
                copyDirectoryToDirectory(srcFile, targetDir);
        }
        // Create a jar from a classes directory.
        if (inc.has("classes")) {
            String classes = jstring(inc, "classes");
            String name = jstring(inc, "name");
            createJarFile(classes, name, targetDir);
        }
    }
}

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

License:Open Source License

/**
 * Get a JobConfig from the JOB_CONFIG_OVERLAYS
 * object in the deploy object. The deploy object
 * always uses JOB_CONFIG_OVERLAYS style even if
 * the Streams version doesn't support overlays (pre 4.2).
 * In that case individual items are taken from the 
 * JobConfig created from the overlay./*from   www.jav a2 s . c o m*/
 */
public static JobConfig fromFullOverlay(JsonObject deploy) {

    JobConfig jobConfig;
    JsonArray jcos = array(deploy, JOB_CONFIG_OVERLAYS);
    if (jcos == null || jcos.size() == 0) {
        jobConfig = new JobConfig();
        // assume a single config, only one supported in 4.2
    } else {

        JsonObject jco = jcos.get(0).getAsJsonObject();

        if (jco.has(JOB_CONFIG))
            jobConfig = gson().fromJson(object(jco, JOB_CONFIG), JobConfig.class);
        else
            jobConfig = new JobConfig();

        if (jco.has(OPERATION_CONFIG)) {
            JsonObject operationConfig = object(jco, OPERATION_CONFIG);
            if (operationConfig != null) {
                if (operationConfig.has(OVERRIDE_RESOURCE_LOAD_PROTECTION)) {
                    jobConfig.setOverrideResourceLoadProtection(
                            jboolean(operationConfig, OVERRIDE_RESOURCE_LOAD_PROTECTION));
                }
            }
        }
    }

    return jobConfig;
}

From source file:com.ibm.twa.bluemix.samples.managers.WorkloadSchedulerManager.java

License:Open Source License

public String getAppURI() {
    String uri = null;//ww  w. j  av a 2 s.c o m
    String vcapJSONString = System.getenv("VCAP_APPLICATION");
    if (vcapJSONString != null) {
        JsonObject app = new JsonParser().parse(vcapJSONString).getAsJsonObject();
        JsonArray uris = app.get("application_uris").getAsJsonArray();
        uri = uris.get(0).getAsString();
    }
    return uri;
}