Search in sources :

Example 76 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project streamsx.topology by IBMStreams.

the class OperatorGenerator method getHostPoolName.

/**
     * Gets or creates a host pool at the graphConfig level corresponding to the
     * unique set of tags.
     */
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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) HashSet(java.util.HashSet)

Example 77 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project streamsx.topology by IBMStreams.

the class Optimizer method pyPassByRef.

/**
     * Setup Python operators to allow pass by reference.
     * 
     * 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;
        // 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();
            // 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;
                }
                // by reference.
                if (!kind(connectedOp).endsWith("::Map") && !kind(connectedOp).endsWith("::ForEach") && !kind(connectedOp).endsWith("::FlatMap")) {
                    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);
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 78 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project streamsx.topology by IBMStreams.

the class SubmissionTimeValue method addMainCompositeParam.

/**
     * Create a main composite to represent a submission parameter.
     * 
     * A parameter with name width,  type int32 default 3 is mapped to in the main composite:
     * 
     * expression<int32> $__spl_stv_width : (int32) getSubmissionTimeValue("width", "3");
     * 
     */
private void addMainCompositeParam(JsonObject params, JsonObject param) {
    JsonObject spv = jobject(param, "value");
    String spname = jstring(spv, "name");
    String metaType = jstring(spv, "metaType");
    String cpname = mkCompositeParamName(spname);
    JsonObject cp = new JsonObject();
    cp.addProperty("type", TYPE_COMPOSITE_PARAMETER);
    JsonObject cpv = new JsonObject();
    cpv.addProperty("name", cpname);
    cpv.addProperty("metaType", metaType);
    String splspname = SPLGenerator.stringLiteral(spname);
    String splType = Types.metaTypeToSPL(metaType);
    String cpdv;
    if (!spv.has("defaultValue")) {
        cpdv = String.format("(%s) getSubmissionTimeValue(%s)", splType, splspname);
    } else {
        JsonPrimitive defaultValueJson = spv.get("defaultValue").getAsJsonPrimitive();
        String defaultValue;
        if (metaType.startsWith("UINT")) {
            StringBuilder sbunsigned = new StringBuilder();
            sbunsigned.append("(rstring) ");
            SPLGenerator.numberLiteral(sbunsigned, defaultValueJson, metaType);
            defaultValue = sbunsigned.toString();
        } else {
            defaultValue = SPLGenerator.stringLiteral(defaultValueJson.getAsString());
        }
        cpdv = String.format("(%s) getSubmissionTimeValue(%s, %s)", splType, splspname, defaultValue);
    }
    cpv.addProperty("defaultValue", cpdv);
    cp.add("value", cpv);
    params.add(cpname, cp);
    submissionMainCompositeParams.put(spname, cp);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject)

Example 79 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project streamsx.topology by IBMStreams.

the class SubmissionTimeValue method generateMainDef.

/**
     * Generate a submission time value SPL param value definition
     * in a main composite.
     * <p>
     * e.g.,
     * <pre>{@code
     *  param
     *      expression<uint32> $__jaa_stv_foo : (uint32) getSubmissionTimeValue("foo", 3)
     * }</pre>
     * @param spval JSONObject for the submission parameter's value
     * @param sb
     */
void generateMainDef(JsonObject spval, StringBuilder sb) {
    String paramName = generateCompositeParamReference(spval);
    String spName = SPLGenerator.stringLiteral(jstring(spval, "name"));
    String metaType = jstring(spval, "metaType");
    String splType = Types.metaTypeToSPL(metaType);
    sb.append(String.format("expression<%s> %s : ", splType, paramName));
    if (!spval.has("defaultValue")) {
        sb.append(String.format("(%s) getSubmissionTimeValue(%s)", splType, spName));
    } else {
        JsonPrimitive defaultValueJson = spval.get("defaultValue").getAsJsonPrimitive();
        String defaultValue;
        if (metaType.startsWith("UINT")) {
            StringBuilder sbunsigned = new StringBuilder();
            sbunsigned.append("(rstring) ");
            SPLGenerator.numberLiteral(sbunsigned, defaultValueJson, metaType);
            defaultValue = sbunsigned.toString();
        } else {
            defaultValue = SPLGenerator.stringLiteral(defaultValueJson.getAsString());
        }
        sb.append(String.format("(%s) getSubmissionTimeValue(%s, %s)", splType, spName, defaultValue));
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive)

Example 80 with JsonPrimitive

use of com.google.gson.JsonPrimitive in project intellij-plugins by JetBrains.

the class RemoteAnalysisServerImpl method processResponse.

private void processResponse(JsonObject response) throws Exception {
    // handle notification
    if (processNotification(response)) {
        return;
    }
    // prepare ID
    JsonPrimitive idJsonPrimitive = (JsonPrimitive) response.get("id");
    if (idJsonPrimitive == null) {
        return;
    }
    String idString = idJsonPrimitive.getAsString();
    // prepare consumer
    Consumer consumer;
    synchronized (consumerMapLock) {
        consumer = consumerMap.get(idString);
    }
    JsonObject errorObject = (JsonObject) response.get("error");
    RequestError requestError = null;
    if (errorObject != null) {
        requestError = processErrorResponse(errorObject);
        listener.requestError(requestError);
    }
    // handle result
    JsonObject resultObject = (JsonObject) response.get("result");
    //
    if (consumer instanceof UpdateContentConsumer) {
        ((UpdateContentConsumer) consumer).onResponse();
    } else //
    if (consumer instanceof GetSuggestionsConsumer) {
        new CompletionIdProcessor((GetSuggestionsConsumer) consumer).process(resultObject, requestError);
    } else //
    if (consumer instanceof FindElementReferencesConsumer) {
        new FindElementReferencesProcessor((FindElementReferencesConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof FindMemberDeclarationsConsumer) {
        new FindMemberDeclarationsProcessor((FindMemberDeclarationsConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof FindMemberReferencesConsumer) {
        new FindMemberReferencesProcessor((FindMemberReferencesConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof FindTopLevelDeclarationsConsumer) {
        new FindTopLevelDeclarationsProcessor((FindTopLevelDeclarationsConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetTypeHierarchyConsumer) {
        new TypeHierarchyProcessor((GetTypeHierarchyConsumer) consumer).process(resultObject, requestError);
    } else //
    if (consumer instanceof FormatConsumer) {
        new FormatProcessor((FormatConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetHoverConsumer) {
        new HoverProcessor((GetHoverConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetRefactoringConsumer) {
        new GetRefactoringProcessor(requestToRefactoringKindMap, (GetRefactoringConsumer) consumer).process(idString, resultObject, requestError);
    } else if (consumer instanceof GetAssistsConsumer) {
        new AssistsProcessor((GetAssistsConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetFixesConsumer) {
        new FixesProcessor((GetFixesConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetStatementCompletionConsumer) {
        new StatementCompletionProcessor((GetStatementCompletionConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetLibraryDependenciesConsumer) {
        new LibraryDependenciesProcessor((GetLibraryDependenciesConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetNavigationConsumer) {
        new GetNavigationProcessor((GetNavigationConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetAvailableRefactoringsConsumer) {
        new RefactoringGetAvailableProcessor((GetAvailableRefactoringsConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof GetErrorsConsumer) {
        new AnalysisErrorsProcessor((GetErrorsConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof OrganizeDirectivesConsumer) {
        new OrganizeDirectivesProcessor((OrganizeDirectivesConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof SortMembersConsumer) {
        new SortMembersProcessor((SortMembersConsumer) consumer).process(resultObject, requestError);
    } else //
    if (consumer instanceof CreateContextConsumer) {
        new CreateContextProcessor((CreateContextConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof MapUriConsumer) {
        new MapUriProcessor((MapUriConsumer) consumer).process(resultObject, requestError);
    } else //
    if (consumer instanceof GetServerPortConsumer) {
        new GetServerPortProcessor((GetServerPortConsumer) consumer).process(resultObject, requestError);
    } else //
    if (consumer instanceof GetVersionConsumer) {
        new VersionProcessor((GetVersionConsumer) consumer).process(resultObject, requestError);
    } else if (consumer instanceof BasicConsumer) {
        ((BasicConsumer) consumer).received();
    }
    synchronized (consumerMapLock) {
        consumerMap.remove(idString);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) JsonPrimitive(com.google.gson.JsonPrimitive) RequestError(org.dartlang.analysis.server.protocol.RequestError)

Aggregations

JsonPrimitive (com.google.gson.JsonPrimitive)230 JsonObject (com.google.gson.JsonObject)137 JsonArray (com.google.gson.JsonArray)118 JsonElement (com.google.gson.JsonElement)106 Test (org.testng.annotations.Test)56 Test (org.junit.Test)12 Map (java.util.Map)10 Matchers.anyString (org.mockito.Matchers.anyString)8 JsonProcessorInjectionMap (com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap)7 Gson (com.google.gson.Gson)6 HashMap (java.util.HashMap)5 GsonBuilder (com.google.gson.GsonBuilder)4 JsonParser (com.google.gson.JsonParser)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Type (java.lang.reflect.Type)3 HashSet (java.util.HashSet)3 Matcher (java.util.regex.Matcher)3 LobWrapper (angularBeans.io.LobWrapper)2 DatasetCreationSpec (co.cask.cdap.internal.dataset.DatasetCreationSpec)2