Example usage for com.google.gson JsonPrimitive JsonPrimitive

List of usage examples for com.google.gson JsonPrimitive JsonPrimitive

Introduction

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

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

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

License:Open Source License

/**
 * Gets or creates a host pool at the graphConfig level corresponding to the
 * unique set of tags./* ww w . j  a v  a2s .  co  m*/
 */
private static String getHostPoolName(JsonObject graphConfig, Set<String> uniqueResourceTags) {
    JsonArray hostPools = array(graphConfig, "__spl_hostPools");
    if (hostPools == null) {
        graphConfig.add("__spl_hostPools", hostPools = new JsonArray());
    }

    // Look for a host pool matching this one
    for (JsonElement hpe : hostPools) {
        JsonObject hostPoolDef = hpe.getAsJsonObject();
        JsonArray rta = hostPoolDef.get("resourceTags").getAsJsonArray();
        Set<String> poolResourceTags = new HashSet<>();
        for (JsonElement tage : rta)
            poolResourceTags.add(tage.getAsString());
        if (uniqueResourceTags.equals(poolResourceTags)) {
            return jstring(hostPoolDef, "name");
        }
    }

    JsonObject hostPoolDef = new JsonObject();
    String hostPool;
    hostPoolDef.addProperty("name", hostPool = "__jaaHostPool" + hostPools.size());
    JsonArray rta = new JsonArray();
    for (String tag : uniqueResourceTags)
        rta.add(new JsonPrimitive(tag));
    hostPoolDef.add("resourceTags", rta);
    hostPools.add(hostPoolDef);
    return hostPool;
}

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

License:Open Source License

/**
 * Setup Python operators to allow pass by reference.
 * /*from  w  w  w .ja v  a 2 s  .c  o  m*/
 * 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);
        }
    }
}

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

License:Open Source License

private JsonObject createCompositeDefinition(List<List<JsonObject>> startsEndsAndOperators) {
    // Create a composite with a kind, input names, and output names  
    JsonObject compositeDefinition = new JsonObject();
    String compositeKind = "Composite" + this.numComposites++;

    compositeDefinition.addProperty(KIND, compositeKind);
    compositeDefinition.addProperty("public", false);

    // Add operators
    JsonArray operators = new JsonArray();
    for (JsonObject obj : startsEndsAndOperators.get(2))
        operators.add(obj);//from ww w .j  a v  a 2s  .  c  o  m

    // If it's a physical composite start operator, add it.
    for (JsonObject obj : startsEndsAndOperators.get(0))
        if (isPhysicalStartOperator(obj))
            operators.add(obj);

    if (operators.size() == 0) {
        throw new IllegalStateException("A region must contain at least one operator.");
    }

    compositeDefinition.add("operators", operators);

    // Create input names
    JsonArray inputNames = new JsonArray();
    for (int i = 0; i < startsEndsAndOperators.get(0).size(); i++) {
        JsonObject start = startsEndsAndOperators.get(0).get(i);
        // If it's not a source operator
        if (!isPhysicalStartOperator(start)) {
            inputNames.add(new JsonPrimitive("__In" + i));
        }
    }
    compositeDefinition.add("inputNames", inputNames);

    // Create output names
    JsonArray outputNames = new JsonArray();
    for (int i = 0; i < startsEndsAndOperators.get(1).size(); ++i)
        outputNames.add(new JsonPrimitive("__Out" + i));
    compositeDefinition.add("outputNames", outputNames);

    // Tag composite def so it can be identified as created during comp generation
    compositeDefinition.add("generated", new JsonPrimitive(true));

    return compositeDefinition;
}

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

License:Open Source License

@SuppressWarnings("unchecked")
private static JsonElement convertConfigValue(Object value) {

    if (value instanceof Boolean)
        return new JsonPrimitive((Boolean) value);
    else if (value instanceof Number)
        return new JsonPrimitive((Number) value);
    else if (value instanceof String) {
        return new JsonPrimitive((String) value);
    } else if (value instanceof JsonElement) {
        return (JsonElement) value;
    } else if (JSON4JBridge.isJson4J(value)) {
        return JSON4JBridge.fromJSON4J(value);
    } else if (value instanceof Collection) {
        JsonArray array = new JsonArray();
        for (Object e : (Collection<Object>) value) {
            array.add(convertConfigValue(e));
        }/*from  ww w .j a v a  2  s  .c o m*/
        return array;
    } else if (value instanceof File) {
        return new JsonPrimitive(((File) value).getAbsolutePath());
    }
    throw new IllegalArgumentException(value.getClass().getName());
}

From source file:com.ibm.streamsx.topology.internal.core.DependencyResolver.java

License:Open Source License

/**
 * Resolve the dependencies./*from w w  w. ja v a 2 s  . c  o  m*/
 * Creates entries in the graph config that will
 * result in files being copied into the toolkit.
 */
public void resolveDependencies() throws IOException, URISyntaxException {

    JsonObject graphConfig = topology.builder().getConfig();
    JsonArray includes = array(graphConfig, "includes");
    if (includes == null)
        graphConfig.add("includes", includes = new JsonArray());

    for (BOperatorInvocation op : operatorToJarDependencies.keySet()) {
        ArrayList<String> jars = new ArrayList<String>();

        for (Path source : operatorToJarDependencies.get(op)) {
            String jarName = resolveDependency(source, false, includes);
            jars.add(DEP_JAR_LOC + File.separator + jarName);
        }

        String[] jarPaths = jars.toArray(new String[jars.size()]);
        op.setParameter("jar", jarPaths);
    }

    ArrayList<String> jars = new ArrayList<String>();
    for (Path source : globalDependencies.keySet()) {
        boolean containsOperator = globalDependencies.get(source);
        String jarName = resolveDependency(source, containsOperator, includes);
        String location = depJarRoot(containsOperator);
        jars.add(location + File.separator + jarName);
    }

    List<BOperator> ops = topology.builder().getOps();
    if (jars.size() != 0) {
        for (BOperator op : ops) {
            if (JavaFunctionalOps.isFunctional(op)) {
                JsonArray value = arrayCreate(op._json(), "parameters", "jar", "value");
                for (String jar : jars) {
                    value.add(new JsonPrimitive(jar));
                }
            }
        }
    }

    for (Artifact dep : globalFileDependencies)
        resolveFileDependency(dep, includes);
}

From source file:com.ibm.streamsx.topology.internal.core.PlacementInfo.java

License:Open Source License

private static JsonArray setToArray(Set<String> set) {
    JsonArray array = new JsonArray();
    for (String item : set)
        if (!item.isEmpty())
            array.add(new JsonPrimitive(item));
    return array;
}

From source file:com.ibm.streamsx.topology.internal.core.StreamImpl.java

License:Open Source License

private TStream<T> _parallel(Supplier<Integer> width, Routing routing, Function<T, ?> keyer) {

    if (width == null)
        throw new IllegalArgumentException(PortProperties.WIDTH);
    Integer widthVal;/*  ww w.  jav a  2  s  .c o  m*/
    if (width.get() != null)
        widthVal = width.get();
    else if (width instanceof SubmissionParameter<?>)
        widthVal = ((SubmissionParameter<Integer>) width).getDefaultValue();
    else
        throw new IllegalArgumentException("Illegal width Supplier: width.get() returns null.");
    if (widthVal != null && widthVal <= 0)
        throw new IllegalArgumentException("The parallel width must be greater than or equal to 1.");

    BOutput toBeParallelized = output();
    boolean isPartitioned = false;
    if (keyer != null) {

        final ToIntFunction<T> hasher = new KeyFunctionHasher<>(keyer);

        BOperatorInvocation hashAdder = JavaFunctional.addFunctionalOperator(this, "HashAdder", HASH_ADDER_KIND,
                hasher);

        if (isPlaceable()) {
            BOperatorInvocation op = operator();

            JsonObject serializer = op.getRawParameter("outputSerializer");
            if (serializer != null) {
                hashAdder.setParameter("inputSerializer", serializer);
                JavaFunctional.copyDependencies(this, op, hashAdder);
            }
        }

        hashAdder.layout().addProperty("hidden", true);
        BInputPort ip = connectTo(hashAdder, true, null);

        String hashSchema = ObjectSchemas.schemaWithHash(ip._schema());
        toBeParallelized = hashAdder.addOutput(hashSchema);
        isPartitioned = true;
    }

    BOutput parallelOutput = builder().parallel(toBeParallelized, routing.name(), width);
    if (isPartitioned) {
        parallelOutput._json().addProperty(PortProperties.PARTITIONED, true);
        JsonArray partitionKeys = new JsonArray();
        partitionKeys.add(new JsonPrimitive("__spl_hash"));
        parallelOutput._json().add(PortProperties.PARTITION_KEYS, partitionKeys);
        // Add hash remover
        StreamImpl<T> parallelStream = new StreamImpl<T>(this, parallelOutput, getTupleType());
        BOperatorInvocation hashRemover = builder().addOperator("HashRemover", HASH_REMOVER_KIND, null);
        hashRemover.setModel(MODEL_SPL, LANGUAGE_JAVA);

        hashRemover.layout().addProperty("hidden", true);

        @SuppressWarnings("unused")
        BInputPort pip = parallelStream.connectTo(hashRemover, true, null);
        parallelOutput = hashRemover.addOutput(output._type());
    }

    return addMatchingStream(parallelOutput);
}

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

License:Open Source License

/**
 * Add value to o as property, converting as needed.
 * Supports JsonElment,String,Number,Boolean, Collection{String,Number,Boolean}
 *///  w ww  . j  av  a2 s .c  o m
public static void addToObject(JsonObject o, String property, Object value) {
    if (value instanceof JsonElement)
        o.add(property, (JsonElement) value);
    else if (value instanceof String)
        o.addProperty(property, (String) value);
    else if (value instanceof Number)
        o.addProperty(property, (Number) value);
    else if (value instanceof Boolean)
        o.addProperty(property, (Boolean) value);
    else if (value instanceof Collection) {
        JsonArray sa = new JsonArray();
        Collection<?> values = (Collection<?>) value;
        for (Object ov : values) {
            if (ov instanceof JsonElement)
                sa.add((JsonElement) ov);
            else if (ov instanceof String)
                sa.add(new JsonPrimitive((String) ov));
            else if (ov instanceof Number)
                sa.add(new JsonPrimitive((Number) ov));
            else if (ov instanceof Boolean)
                sa.add(new JsonPrimitive((Boolean) ov));
            else
                throw new UnsupportedOperationException("JSON:" + ov.getClass());
        }

        o.add(property, sa);
    } else
        throw new UnsupportedOperationException("JSON:" + value.getClass());
}

From source file:com.ibm.watson.developer_cloud.concept_expansion.v1.ConceptExpansion.java

License:Open Source License

/**
 * Creates a {@link Job}.//from w ww .java2s .  co m
 * 
 * @param label A conceptual classification of the seed terms.
 * @param seeds List of terms to be used as seeds
 * 
 * @return the {@link Job}
 */
public Job createJob(final String label, final String[] seeds) {
    Validate.notEmpty(seeds, "seeds cannot be null or empty");
    Validate.notNull(dataset, "dataset cannot be null");

    final JsonArray seedJsonArray = new JsonArray();
    for (final String seed : seeds) {
        seedJsonArray.add(new JsonPrimitive(seed));
    }

    final JsonObject payload = new JsonObject();
    payload.addProperty(LABEL, label);
    payload.addProperty(DATASET, dataset.getId());
    payload.add(SEEDS, seedJsonArray);

    final Request request = RequestBuilder.post(V1_UPLOAD).withBodyJson(payload).build();
    return executeRequest(request, Job.class);
}

From source file:com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights.java

License:Open Source License

/**
 * Performs a conceptual search within a corpus.
 * /*from   w  w w. j a v  a 2s .  com*/
 * @param corpus the corpus
 * @param parameters The parameters to be used in the service call ids is required.
 *        <ul>
 *        <li>RequestedFields concept_fields - Additional fields to be included in the concept
 *        objects.<br>
 *        <li>RequestedFields document_fields - Additional fields to be included in the document
 *        objects.<br>
 *        <li>String ids - JSON array of concept and/or document ids.<br>
 *        <li>Integer cursor - A number of items to skip.<br>
 *        <li>Integer limit - The maximum number of concepts to be returned.<br>
 *        </ul>
 * @return {@link QueryConcepts}
 */
public QueryConcepts conceptualSearch(Corpus corpus, Map<String, Object> parameters) {
    Validate.notNull(parameters.get(IDS), "ids cannot be null");
    final String corpusId = IDHelper.getCorpusId(corpus, getFirstAccountId());

    final Map<String, Object> queryParams = new HashMap<String, Object>();
    final String[] queryParameters = new String[] { CURSOR, LIMIT };

    for (final String param : queryParameters) {
        if (parameters.containsKey(param))
            queryParams.put(param, parameters.get(param));
    }

    final JsonArray IdsJsonArray = new JsonArray();
    @SuppressWarnings("unchecked")
    final List<String> ids = (List<String>) parameters.get(IDS);
    for (final String value : ids) {
        IdsJsonArray.add(new JsonPrimitive(value));
    }
    queryParams.put(IDS, IdsJsonArray.toString());

    if (parameters.get(CONCEPT_FIELDS) != null) {
        final RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
        if (fields != null && !fields.isEmpty())
            queryParams.put(CONCEPT_FIELDS, toJson(fields.getFields()));
    }

    if (parameters.get(DOCUMENT_FIELDS) != null) {
        final RequestedFields fields = (RequestedFields) parameters.get(DOCUMENT_FIELDS);
        if (fields != null && !fields.isEmpty())
            queryParams.put(DOCUMENT_FIELDS, toJson(fields.getFields()));
    }

    return executeRequest(API_VERSION + corpusId + CONCEPTUAL_SEARCH_PATH, queryParams, QueryConcepts.class);
}