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.GraphUtilities.java

License:Open Source License

static void removeOperators(Collection<JsonObject> operators, JsonObject graph) {
    for (JsonObject iso : operators) {

        // Get parents and children of operator
        Set<JsonObject> operatorParents = getUpstream(iso, graph);
        Set<JsonObject> operatorChildren = getDownstream(iso, graph);

        JsonArray operatorOutputs = array(iso, "outputs");

        // Get the output name of the operator
        String operatorOutName = "";
        if (operatorOutputs != null) {
            JsonObject operatorFirstOutput = operatorOutputs.get(0).getAsJsonObject();
            if (operatorFirstOutput != null) {
                operatorOutName = jstring(operatorFirstOutput, "name");
            }//www  .  ja  va  2 s  .c o m
        }

        // Also get input names
        List<String> operatorInNames = new ArrayList<>();
        inputs(iso, input -> operatorInNames.add(jstring(input, "name")));

        // Respectively, the names of the child and parent input and
        // output ports connected to the operator.
        List<String> childInputPortNames = new ArrayList<>();
        List<String> parentOutputPortNames = new ArrayList<>();

        // References to the list of connections for the parent and child
        // output and input ports that are connected to the $isolate$
        // operator.
        List<JsonArray> childConnections = new ArrayList<>();
        List<JsonArray> parentConnections = new ArrayList<>();

        // Get names of children's input ports that are connected to the
        // operator;
        for (JsonObject child : operatorChildren) {
            JsonArray inputs = child.get("inputs").getAsJsonArray();
            for (JsonElement inputObj : inputs) {
                JsonObject input = inputObj.getAsJsonObject();
                JsonArray connections = input.get("connections").getAsJsonArray();
                for (JsonElement connectionObj : connections) {
                    String connection = connectionObj.getAsString();
                    if (connection.equals(operatorOutName)) {
                        childInputPortNames.add(jstring(input, "name"));
                        childConnections.add(connections);
                        connections.remove(connectionObj);
                        break;
                    }
                }
            }
        }

        // Get names of parent's output ports that are connected to the
        // $Isolate$ operator;
        for (JsonObject parent : operatorParents) {
            JsonArray outputs = parent.get("outputs").getAsJsonArray();
            for (JsonElement outputObj : outputs) {
                JsonObject output = outputObj.getAsJsonObject();
                JsonArray connections = output.get("connections").getAsJsonArray();
                for (JsonElement connectionObj : connections) {
                    String connection = connectionObj.getAsString();
                    if (operatorInNames.contains(connection)) {
                        parentOutputPortNames.add(jstring(output, "name"));
                        parentConnections.add(connections);
                        connections.remove(connectionObj);
                        break;
                    }
                }
            }
        }

        // Connect child to parents
        for (JsonArray childConnection : childConnections) {
            for (String name : parentOutputPortNames)
                childConnection.add(new JsonPrimitive(name));
        }

        // Connect parent to children
        for (JsonArray parentConnection : parentConnections) {
            for (String name : childInputPortNames)
                parentConnection.add(new JsonPrimitive(name));
        }
        JsonArray ops = graph.get("operators").getAsJsonArray();
        ops.remove(iso);
    }
}

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

License:Open Source License

static String getInputPortName(JsonObject op, int index) {
    JsonArray inputs = op.get("inputs").getAsJsonArray();
    JsonObject input = inputs.get(index).getAsJsonObject();
    return jstring(input, "name");
}

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

License:Open Source License

static String getOutputPortName(JsonObject op, int index) {
    JsonArray outputs = op.get("output").getAsJsonArray();
    JsonObject output = outputs.get(index).getAsJsonObject();
    return jstring(output, "name");

}

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

License:Open Source License

static void insertOperatorBetweenPorts(JsonObject input, JsonObject output, JsonObject op) {
    String oportName = jstring(output, "name");
    String iportName = jstring(input, "name");

    JsonObject opInput = op.get("inputs").getAsJsonArray().get(0).getAsJsonObject();
    JsonObject opOutput = op.get("outputs").getAsJsonArray().get(0).getAsJsonObject();

    String opIportName = jstring(opInput, "name");
    String opOportName = jstring(opOutput, "name");

    // Attach op in inputs and outputs
    JsonArray opInputConns = opInput.get("connections").getAsJsonArray();
    JsonArray opOutputConns = opOutput.get("connections").getAsJsonArray();
    boolean add = true;
    for (JsonElement conn : opInputConns)
        if (conn.getAsString().equals(oportName)) {
            add = false;//  w ww  .  j  a v a 2s  .c o m
            break;
        }
    if (add)
        opInputConns.add(new JsonPrimitive(oportName));

    add = true;
    for (JsonElement conn : opOutputConns)
        if (conn.getAsString().equals(iportName)) {
            add = false;
            break;
        }
    opOutputConns.add(new JsonPrimitive(iportName));

    JsonArray outputConns = output.get("connections").getAsJsonArray();
    JsonArray inputConns = input.get("connections").getAsJsonArray();

    for (int i = 0; i < outputConns.size(); i++) {
        if (outputConns.get(i).getAsString().equals(iportName))
            outputConns.set(i, new JsonPrimitive(opIportName));
    }

    for (int i = 0; i < inputConns.size(); i++) {
        if (inputConns.get(i).getAsString().equals(oportName))
            inputConns.set(i, new JsonPrimitive(opOportName));
    }
}

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

License:Open Source License

private static void layoutNote(JsonObject op, StringBuilder sb) {
    final JsonObject layout = object(op, "layout");
    if (layout != null) {

        // Need to check for name mangling from provided names to SPL identifiers
        layoutMapName(layout, jstring(op, "name"));

        if (op.has("outputs")) {
            JsonArray outputs = array(op, "outputs");
            for (int i = 0; i < outputs.size(); i++) {
                JsonObject output = outputs.get(i).getAsJsonObject();
                String name = jstring(output, "name");
                if (name != null)
                    layoutMapName(layout, name);
            }/*from   w  w w. j ava  2  s  . c  o m*/
        }
        if (op.has("inputs")) {
            JsonArray inputs = array(op, "inputs");
            for (int i = 0; i < inputs.size(); i++) {
                JsonObject output = inputs.get(i).getAsJsonObject();
                String name = jstring(output, "name");
                if (name != null)
                    layoutMapName(layout, name);
            }
        }

        appendNoteAnnotation(sb, "__spl_layout", layout);
    }
}

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

License:Open Source License

private static void sourceLocationNote(JsonObject op, StringBuilder sb) throws IOException {

    JsonArray ja = array(op, SourceInfo.SOURCE_LOCATIONS);
    if (ja == null)
        return;//  w  w w .j  a  v a2  s.c  o  m

    JsonElement jsource = ja.size() == 1 ? (JsonElement) ja.get(0) : ja;

    appendNoteAnnotation(sb, "__spl_sourcelocation", jsource);
}

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

License:Open Source License

private void parallelAnnotation(JsonObject op, StringBuilder sb) {
    boolean parallel = jboolean(op, "parallelOperator");

    if (parallel) {
        boolean partitioned = jboolean(op, "partitioned");
        JsonObject parallelInfo = op.get("parallelInfo").getAsJsonObject();

        sb.append("@parallel(width=");
        JsonElement width = parallelInfo.get(OpProperties.WIDTH);
        if (width.isJsonPrimitive()) {
            sb.append(width.getAsString());
        } else {/*w ww.  j a  v a  2 s .c  om*/
            splValueSupportingSubmission(width.getAsJsonObject(), sb);
        }

        if (partitioned) {
            sb.append(", partitionBy=[");
            JsonArray partitionedPorts = array(parallelInfo, "partitionedPorts");
            for (int i = 0; i < partitionedPorts.size(); i++) {
                JsonObject partitionedPort = partitionedPorts.get(i).getAsJsonObject();

                if (i > 0)
                    sb.append(", ");

                sb.append("{port=");
                sb.append(getSPLCompatibleName(GsonUtilities.jstring(partitionedPort, "name")));
                sb.append(", attributes=[");
                JsonArray partitionKeys = partitionedPort.get("partitionedKeys").getAsJsonArray();
                for (int j = 0; j < partitionKeys.size(); j++) {
                    if (j != 0)
                        sb.append(", ");
                    sb.append(partitionKeys.get(j).getAsString());
                }
                sb.append("]}");
            }

            sb.append("]");
        }

        JsonArray broadcastPorts = parallelInfo.get("broadcastPorts").getAsJsonArray();
        if (broadcastPorts.size() > 0) {
            sb.append(", broadcast=[");
            for (int i = 0; i < broadcastPorts.size(); i++) {
                if (i != 0)
                    sb.append(", ");
                sb.append(getSPLCompatibleName(broadcastPorts.get(i).getAsString()));
            }
            sb.append("]");

        }

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

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

License:Open Source License

/**
 * Create the output port definitions./*from  w w w .  j  av a 2 s.c  om*/
 */
private static boolean outputPortClause(JsonObject op, StringBuilder sb) {

    boolean singlePortSingleName = false;
    if (op.has("outputs")) {
        JsonArray outputs = array(op, "outputs");
        if (outputs.size() == 1) {
            JsonObject output = outputs.get(0).getAsJsonObject();
            String name = jstring(output, "name");

            if (name.equals(jstring(op, "name")))
                singlePortSingleName = true;
        }
    }

    if (!singlePortSingleName)
        sb.append("  ( ");

    // effectively a mutable boolean
    AtomicBoolean first = new AtomicBoolean(true);

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

        String type = jstring(output, "type");
        if (type.startsWith("tuple<")) {
            // removes the 'tuple<..>' part of the type
            type = type.substring(6, type.length() - 1);
        }

        String name = jstring(output, "name");
        name = getSPLCompatibleName(name);

        if (!first.get()) {
            sb.append("; ");
        }
        first.set(false);

        sb.append("stream<");
        sb.append(type);
        sb.append("> ");
        sb.append(name);
    });

    if (!singlePortSingleName)
        sb.append(") ");

    return singlePortSingleName;
}

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

License:Open Source License

static void inputClause(JsonObject op, StringBuilder sb) {

    sb.append("  ( ");

    AtomicBoolean firstPort = new AtomicBoolean(true);

    objectArray(op, "inputs", input -> {

        if (!firstPort.getAndSet(false))
            sb.append("; ");

        final String portName = jstring(input, "name");

        // If a single input stream and its name 
        // is the same as the port name
        // then don't use an 'as'. Allows better logical names
        // where the user provided stream name is used consistently.
        boolean singleName = false;
        JsonArray connections = array(input, "connections");
        if (connections.size() == 1) {
            String connName = connections.get(0).getAsString();

            if (portName.equals(connName))
                singleName = true;//from  w  w w . ja  v  a2 s.c  o  m
        }

        AtomicBoolean firstStream = new AtomicBoolean(true);
        stringArray(input, "connections", name -> {
            if (!firstStream.getAndSet(false))
                sb.append(", ");
            sb.append(getSPLCompatibleName(name));
        });

        if (!singleName) {
            sb.append(" as ");
            sb.append(getSPLCompatibleName(portName));
        }
    });

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

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

License:Open Source License

/**
 * Setup Python operators to allow pass by reference.
 * //w  w w. j a  v  a  2 s. com
 * Finds Python functional operators and sets the outputConnections
 * parameter representing the number of connections.
 * 
 * If pass by reference cannot be used outputConnections will not be set.
 * 
 * Does not modify the structure of the graph.
 * Assumes the graph's structure will not be subsequently modified.
 */
private final void pyPassByRef() {
    Set<JsonObject> pyops = findOperatorsByKinds(graph, PY_FUNC_OPS);

    if (pyops.isEmpty())
        return;

    for (JsonObject pyop : pyops) {
        JsonArray outputs = array(pyop, "outputs");
        if (outputs == null || outputs.size() == 0)
            continue;

        // Currently only supporting a single output port
        // though mostly coded to support N.
        assert outputs.size() == 1;

        int[] connCounts = new int[outputs.size()];

        for (int port = 0; port < connCounts.length; port++) {
            connCounts[port] = -1;

            JsonObject output = outputs.get(port).getAsJsonObject();

            // Can't use the schema objects as we need to not depend on IBM Streams
            // classes.
            if (!"tuple<blob __spl_po>".equals(jstring(output, "type")))
                continue;

            JsonArray conns = array(output, "connections");
            if (conns == null || conns.size() == 0) {
                connCounts[port] = 0;
                continue;
            }

            boolean canPassByRef = true;
            // TOOD - downstream for a specific port
            Set<JsonObject> connected = getDownstream(pyop, graph);
            for (JsonObject connectedOp : connected) {
                if (!PY_FUNC_OPS.contains(kind(connectedOp))) {
                    canPassByRef = false;
                    break;
                }

                // TEMP
                // Currently only Map and ForEach completly handle
                // by reference.
                if (!kind(connectedOp).endsWith("::Map") && !kind(connectedOp).endsWith("::ForEach")
                        && !kind(connectedOp).endsWith("::FlatMap")
                        && !kind(connectedOp).endsWith("::Aggregate")) {
                    canPassByRef = false;
                    break;
                }
            }

            if (canPassByRef)
                connCounts[port] = conns.size();
        }

        boolean paramNeeded = false;
        for (int oc : connCounts) {
            if (oc != -1) {
                paramNeeded = true;
                break;
            }
        }

        if (paramNeeded) {
            JsonObject value = new JsonObject();
            if (connCounts.length == 1)
                value.addProperty("value", connCounts[0]);
            else {
                JsonArray ocs = new JsonArray();
                for (int oc : connCounts)
                    ocs.add(new JsonPrimitive(oc));
                value.add("value", ocs);
            }
            GraphUtilities.addOpParameter(pyop, "outputConnections", value);
        }
    }
}