Example usage for com.google.gson JsonObject toString

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:au.edu.unsw.cse.soc.federatedcloud.deployers.aws.s3.ClassForPaper.java

License:Open Source License

@Path("/undeployResource")
@POST//from  w  w  w  . jav a 2s  .c  o m
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response undeployResource(@QueryParam("resourceID") String resourceID) throws Exception {
    //Reading the credentials
    Properties properties = new Properties();
    properties.load(this.getClass().getResourceAsStream("/AwsCredentials.properties"));
    String accessKey = properties.getProperty("accessKey");
    String secretKey = properties.getProperty("secretKey");
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3Client client = new AmazonS3Client(credentials);

    client.deleteBucket(resourceID);

    JsonObject json = new JsonObject();
    json.addProperty("result", "Bucket:" + resourceID + "undeployed.");
    return Response.status(Response.Status.OK).entity(json.toString()).build();
}

From source file:bammerbom.ultimatecore.bukkit.resources.utils.JsonRepresentedObject.java

License:MIT License

/**
 * Deserializes a fancy message from its JSON representation. This JSON
 * representation is of the format of that returned by
 * {@link #toJSONString()}, and is compatible with vanilla inputs.
 *
 * @param json The JSON string which represents a fancy message.
 * @return A {@code MessageUtil} representing the parameterized JSON
 * message./*from w w w  . j  a  v a2  s.c o m*/
 */
public static MessageUtil deserialize(String json) {
    JsonObject serialized = _stringParser.parse(json).getAsJsonObject();
    JsonArray extra = serialized.getAsJsonArray("extra"); // Get the extra component
    MessageUtil returnVal = new MessageUtil();
    returnVal.messageParts.clear();
    for (JsonElement mPrt : extra) {
        MessagePart component = new MessagePart();
        JsonObject messagePart = mPrt.getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry : messagePart.entrySet()) {
            // Deserialize text
            if (TextualComponent.isTextKey(entry.getKey())) {
                // The map mimics the YAML serialization, which has a "key" field and one or more "value" fields
                Map<String, Object> serializedMapForm = new HashMap<>(); // Must be object due to Bukkit serializer API compliance
                serializedMapForm.put("key", entry.getKey());
                if (entry.getValue().isJsonPrimitive()) {
                    // Assume string
                    serializedMapForm.put("value", entry.getValue().getAsString());
                } else {
                    // Composite object, but we assume each element is a string
                    for (Map.Entry<String, JsonElement> compositeNestedElement : entry.getValue()
                            .getAsJsonObject().entrySet()) {
                        serializedMapForm.put("value." + compositeNestedElement.getKey(),
                                compositeNestedElement.getValue().getAsString());
                    }
                }
                component.text = TextualComponent.deserialize(serializedMapForm);
            } else if (MessagePart.stylesToNames.inverse().containsKey(entry.getKey())) {
                if (entry.getValue().getAsBoolean()) {
                    component.styles.add(MessagePart.stylesToNames.inverse().get(entry.getKey()));
                }
            } else if (entry.getKey().equals("color")) {
                component.color = ChatColor.valueOf(entry.getValue().getAsString().toUpperCase());
            } else if (entry.getKey().equals("clickEvent")) {
                JsonObject object = entry.getValue().getAsJsonObject();
                component.clickActionName = object.get("action").getAsString();
                component.clickActionData = object.get("value").getAsString();
            } else if (entry.getKey().equals("hoverEvent")) {
                JsonObject object = entry.getValue().getAsJsonObject();
                component.hoverActionName = object.get("action").getAsString();
                if (object.get("value").isJsonPrimitive()) {
                    // Assume string
                    component.hoverActionData = new JsonString(object.get("value").getAsString());
                } else {
                    // Assume composite type
                    // The only composite type we currently store is another MessageUtil
                    // Therefore, recursion time!
                    component.hoverActionData = deserialize(object.get("value")
                            .toString() /* This should properly serialize the JSON object as a JSON string */);
                }
            } else if (entry.getKey().equals("insertion")) {
                component.insertionData = entry.getValue().getAsString();
            } else if (entry.getKey().equals("with")) {
                for (JsonElement object : entry.getValue().getAsJsonArray()) {
                    if (object.isJsonPrimitive()) {
                        component.translationReplacements.add(new JsonString(object.getAsString()));
                    } else {
                        // Only composite type stored in this array is - again - MessageUtils
                        // Recurse within this function to parse this as a translation replacement
                        component.translationReplacements.add(deserialize(object.toString()));
                    }
                }
            }
        }
        returnVal.messageParts.add(component);
    }
    return returnVal;
}

From source file:be.iminds.iot.dianne.builder.DianneData.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    String action = request.getParameter("action");

    if (action.equals("available-datasets")) {
        JsonArray result = new JsonArray();
        synchronized (datasets) {
            for (DatasetDTO d : datasets.getDatasets()) {
                JsonObject r = new JsonObject();
                r.add("dataset", new JsonPrimitive(d.name));
                r.add("size", new JsonPrimitive(d.size));
                if (d.inputType != null)
                    r.add("inputType", new JsonPrimitive(d.inputType));
                if (d.targetType != null)
                    r.add("targetType", new JsonPrimitive(d.targetType));
                String[] ll = d.labels;
                if (ll != null) {
                    JsonArray labels = new JsonArray();
                    for (String l : ll) {
                        labels.add(new JsonPrimitive(l));
                    }/*w  ww . ja v a 2  s. c  o m*/
                    r.add("labels", labels);
                }
                result.add(r);
            }
        }
        response.getWriter().println(result.toString());
        response.getWriter().flush();
    } else if (action.equals("sample")) {
        String dataset = request.getParameter("dataset");
        Dataset d = datasets.getDataset(dataset);
        if (d != null && d.size() > 0) {
            int index = rand.nextInt(d.size());
            Sample s = d.getSample(index);
            JsonObject sample = converter.toJson(s.input);
            sample.add("index", new JsonPrimitive(index));
            String[] labels = d.getLabels();
            if (labels != null) {
                sample.add("target", new JsonPrimitive(labels[TensorOps.argmax(s.target)]));
            } else {
                if (s.target.size() < 10) {
                    JsonObject target = converter.toJson(s.target);
                    sample.add("target", target.get("data").getAsJsonArray());
                }
            }

            response.getWriter().println(sample.toString());
            response.getWriter().flush();
        }
    }
}

From source file:be.iminds.iot.dianne.builder.DianneDatasets.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String action = request.getParameter("action");

    if (action.equals("available-datasets")) {
        JsonArray result = new JsonArray();
        synchronized (datasets) {
            for (Dataset d : datasets.values()) {
                JsonObject r = new JsonObject();
                r.add("dataset", new JsonPrimitive(d.getName()));
                r.add("size", new JsonPrimitive(d.size()));
                JsonArray labels = new JsonArray();
                for (String l : d.getLabels()) {
                    labels.add(new JsonPrimitive(l));
                }//from  ww w. jav  a  2 s  .c om
                r.add("labels", labels);
                result.add(r);
            }
        }
        response.getWriter().println(result.toString());
        response.getWriter().flush();
    } else if (action.equals("sample")) {
        String dataset = request.getParameter("dataset");
        Dataset d = datasets.get(dataset);
        if (d != null) {
            JsonObject sample = new JsonObject();

            Tensor t = d.getInputSample(rand.nextInt(d.size()));

            if (t.dims().length == 3) {
                sample.add("channels", new JsonPrimitive(t.dims()[0]));
                sample.add("height", new JsonPrimitive(t.dims()[1]));
                sample.add("width", new JsonPrimitive(t.dims()[2]));
            } else {
                sample.add("channels", new JsonPrimitive(1));
                sample.add("height", new JsonPrimitive(t.dims()[0]));
                sample.add("width", new JsonPrimitive(t.dims()[1]));
            }
            sample.add("data", parser.parse(Arrays.toString(t.get())));
            response.getWriter().println(sample.toString());
            response.getWriter().flush();
        }
    }
}

From source file:be.iminds.iot.dianne.builder.DianneDeployer.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");

    String action = request.getParameter("action");
    if (action.equals("deploy")) {
        String id = request.getParameter("id");
        if (id == null) {
            id = UUID.randomUUID().toString();
        }/*  w  ww  .ja  va 2  s .  c  o  m*/
        String name = request.getParameter("name");
        if (name == null) {
            name = "unknown";
        }
        String[] tags = null;
        String t = request.getParameter("tags");
        if (t != null && !t.isEmpty()) {
            tags = t.split(",");
        }

        List<ModuleDTO> toDeploy = new ArrayList<ModuleDTO>();
        if (request.getParameter("modules") != null) {
            String modulesJsonString = request.getParameter("modules");
            NeuralNetworkDTO nn = DianneJSONConverter.parseJSON(modulesJsonString);
            toDeploy.addAll(nn.modules.values());
        } else if (request.getParameter("module") != null) {
            String moduleJsonString = request.getParameter("module");
            ModuleDTO module = DianneJSONConverter.parseModuleJSON(moduleJsonString);
            toDeploy.add(module);
        }

        try {
            String target = request.getParameter("target");
            if (target == null) {
                throw new Exception("No DIANNE runtime selected to deploy to");
            }
            UUID runtimeId = UUID.fromString(target);

            UUID nnId = UUID.fromString(id);
            List<ModuleInstanceDTO> moduleInstances = platform.deployModules(nnId, name, toDeploy, runtimeId,
                    tags);

            // return json object with deployment
            JsonObject result = new JsonObject();
            JsonObject deployment = deploymentToJSON(moduleInstances);
            result.add("id", new JsonPrimitive(id));
            result.add("deployment", deployment);
            try {
                response.getWriter().write(result.toString());
                response.getWriter().flush();
            } catch (IOException e) {
            }

        } catch (Exception ex) {
            System.out.println("Failed to deploy modules: " + ex.getMessage());
            JsonObject result = new JsonObject();
            result.add("error", new JsonPrimitive("Failed to deploy modules: " + ex.getMessage()));
            try {
                response.getWriter().write(result.toString());
                response.getWriter().flush();
            } catch (IOException e) {
            }
        }

    } else if (action.equals("undeploy")) {
        String id = request.getParameter("id");
        String moduleId = request.getParameter("moduleId");
        if (id != null) {
            NeuralNetworkInstanceDTO nn = platform.getNeuralNetworkInstance(UUID.fromString(id));
            if (nn != null) {
                ModuleInstanceDTO moduleInstance = nn.modules.get(UUID.fromString(moduleId));
                if (moduleInstance != null)
                    platform.undeployModules(Collections.singletonList(moduleInstance));

                response.getWriter().write(new JsonPrimitive(id).toString());
                response.getWriter().flush();
            }
        }
    } else if (action.equals("targets")) {
        JsonArray targets = new JsonArray();

        Map<UUID, String> runtimes = platform.getRuntimes();
        runtimes.entrySet().stream().forEach(e -> {
            JsonObject o = new JsonObject();
            o.add("id", new JsonPrimitive(e.getKey().toString()));
            o.add("name", new JsonPrimitive(e.getValue()));
            targets.add(o);
        });

        response.getWriter().write(targets.toString());
        response.getWriter().flush();
    } else if ("recover".equals(action)) {
        String id = request.getParameter("id");
        if (id == null) {
            // list all options
            JsonArray nns = new JsonArray();
            platform.getNeuralNetworkInstances().stream().forEach(nni -> {
                JsonObject o = new JsonObject();
                o.add("id", new JsonPrimitive(nni.id.toString()));
                o.add("name", new JsonPrimitive(nni.name));
                if (nni.description != null) {
                    o.add("description", new JsonPrimitive(nni.description));
                }
                nns.add(o);
            });
            response.getWriter().write(nns.toString());
            response.getWriter().flush();

        } else {
            NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(UUID.fromString(id));
            if (nni == null) {
                System.out.println("Failed to recover " + id + " , instance not found");
                return;
            }

            try {
                response.getWriter().write("{\"nn\":");
                NeuralNetworkDTO nn = repository.loadNeuralNetwork(nni.name);
                String s = DianneJSONConverter.toJsonString(nn);
                response.getWriter().write(s);
                response.getWriter().write(", \"layout\":");
                String layout = repository.loadLayout(nni.name);
                response.getWriter().write(layout);
                response.getWriter().write(", \"deployment\":");
                JsonObject deployment = deploymentToJSON(nni.modules.values());
                response.getWriter().write(deployment.toString());
                response.getWriter().write(", \"id\":");
                response.getWriter().write("\"" + id + "\"");
                response.getWriter().write("}");
                response.getWriter().flush();
            } catch (Exception e) {
                System.out.println("Failed to recover " + nni.name + " " + nni.id);
            }
        }
    }

}

From source file:be.iminds.iot.dianne.builder.DianneInput.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");

    String action = request.getParameter("action");
    if ("available-inputs".equals(action)) {
        JsonArray availableInputs = new JsonArray();
        synchronized (inputs) {
            for (DianneInputs i : inputs) {
                for (InputDescription input : i.getAvailableInputs()) {
                    JsonObject o = new JsonObject();
                    o.add("name", new JsonPrimitive(input.getName()));
                    o.add("type", new JsonPrimitive(input.getType()));
                    availableInputs.add(o);
                }/*from   w  w w.j av a2s. c  o  m*/
            }
        }
        response.getWriter().write(availableInputs.toString());
        response.getWriter().flush();
    } else if ("setinput".equals(action)) {
        String nnId = request.getParameter("nnId");
        String inputId = request.getParameter("inputId");
        String input = request.getParameter("input");

        synchronized (inputs) {
            // TODO select the right one instead of forwarding to all?
            for (DianneInputs i : inputs) {
                i.setInput(UUID.fromString(nnId), UUID.fromString(inputId), input);
            }

            ForwardListener inputListener = new ForwardListener() {
                @Override
                public void onForward(UUID moduleId, Tensor output, String... tags) {
                    if (sses.get(input) != null) {
                        JsonObject json = converter.toJson(output);
                        StringBuilder builder = new StringBuilder();
                        builder.append("data: ").append(json.toString()).append("\n\n");
                        String msg = builder.toString();

                        List<AsyncContext> list = sses.get(input);
                        Iterator<AsyncContext> it = list.iterator();
                        while (it.hasNext()) {
                            AsyncContext sse = it.next();
                            try {
                                PrintWriter writer = sse.getResponse().getWriter();
                                writer.write(msg);
                                writer.flush();
                                if (writer.checkError()) {
                                    System.err.println("Writer error: removing async endpoint.");
                                    writer.close();
                                    it.remove();
                                }
                            } catch (Exception e) {
                                try {
                                    sse.getResponse().getWriter().close();
                                } catch (Exception ignore) {
                                }
                                it.remove();
                                e.printStackTrace();
                            }
                        }
                    }
                }

                @Override
                public void onError(UUID moduleId, ModuleException e, String... tags) {
                    e.printStackTrace();
                }
            };
            Dictionary<String, Object> properties = new Hashtable<String, Object>();
            properties.put("targets", new String[] { nnId + ":" + inputId });
            properties.put("aiolos.unique", true);
            ServiceRegistration<ForwardListener> r = context.registerService(ForwardListener.class,
                    inputListener, properties);
            inputListeners.put(UUID.fromString(inputId), r);
        }
    } else if ("unsetinput".equals(action)) {
        String nnId = request.getParameter("nnId");
        String inputId = request.getParameter("inputId");
        String input = request.getParameter("input");

        synchronized (inputs) {
            for (DianneInputs i : inputs) {
                i.unsetInput(UUID.fromString(nnId), UUID.fromString(inputId), input);
            }
        }

        ServiceRegistration<ForwardListener> r = inputListeners.get(UUID.fromString(inputId));
        if (r != null) {
            r.unregister();
        }
    }
}

From source file:be.iminds.iot.dianne.builder.DianneLearner.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    String id = request.getParameter("id");
    if (id == null) {
        System.out.println("No neural network instance specified");
    }//  w ww . j ava 2  s . c om
    UUID nnId = UUID.fromString(id);
    NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(nnId);
    if (nni == null) {
        System.out.println("Neural network instance " + id + " not deployed");
        return;
    }

    String action = request.getParameter("action");
    if (action.equals("stop")) {
        learner.stop();
        return;
    }

    String target = request.getParameter("target");
    // this list consists of all ids of modules that are needed for trainer:
    // the input, output, trainable and preprocessor modules
    String configJsonString = request.getParameter("config");

    JsonObject configJson = parser.parse(configJsonString).getAsJsonObject();

    JsonObject learnerConfig = (JsonObject) configJson.get(target);

    for (Entry<String, JsonElement> configs : configJson.entrySet()) {
        JsonObject datasetConfig = (JsonObject) configs.getValue();
        if (datasetConfig.get("category").getAsString().equals("Dataset")
                && datasetConfig.get("input") != null) {
            String dataset = datasetConfig.get("dataset").getAsString();
            if (action.equals("learn")) {
                int start = 0;
                int end = datasetConfig.get("train").getAsInt();

                int batch = learnerConfig.get("batch").getAsInt();
                float learningRate = learnerConfig.get("learningRate").getAsFloat();
                float momentum = learnerConfig.get("momentum").getAsFloat();
                float regularization = learnerConfig.get("regularization").getAsFloat();
                String criterion = learnerConfig.get("loss").getAsString();
                String method = learnerConfig.get("method").getAsString();

                boolean clean = learnerConfig.get("clean").getAsBoolean();

                Map<String, String> config = new HashMap<>();
                config.put("range", "" + start + "," + end);
                config.put("batchSize", "" + batch);
                config.put("learningRate", "" + learningRate);
                config.put("momentum", "" + momentum);
                config.put("regularization", "" + regularization);
                config.put("criterion", criterion);
                config.put("method", method);
                config.put("syncInterval", "" + interval);
                config.put("clean", clean ? "true" : "false");
                config.put("trace", "true");

                try {
                    Dataset d = datasets.getDataset(dataset);
                    if (d != null) {
                        JsonObject labels = new JsonObject();
                        labels.add(target, new JsonPrimitive(Arrays.toString(d.getLabels())));
                        response.getWriter().write(labels.toString());
                        response.getWriter().flush();
                    }

                    learner.learn(dataset, config, nni);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else if (action.equals("evaluate")) {
                int start = datasetConfig.get("train").getAsInt();
                int end = start + datasetConfig.get("test").getAsInt();

                Map<String, String> config = new HashMap<>();
                config.put("range", "" + start + "," + end);

                try {
                    Evaluation result = evaluator.eval(dataset, config, nni);

                    JsonObject eval = new JsonObject();

                    eval.add("metric", new JsonPrimitive(result.metric()));
                    eval.add("time", new JsonPrimitive(result.time()));

                    if (result instanceof ErrorEvaluation) {
                        ErrorEvaluation ee = (ErrorEvaluation) result;
                        eval.add("error", new JsonPrimitive(ee.error()));
                    }

                    if (result instanceof ClassificationEvaluation) {
                        ClassificationEvaluation ce = (ClassificationEvaluation) result;
                        eval.add("accuracy", new JsonPrimitive(ce.accuracy() * 100));

                        Tensor confusionMatrix = ce.confusionMatrix();
                        JsonArray data = new JsonArray();
                        for (int i = 0; i < confusionMatrix.size(0); i++) {
                            for (int j = 0; j < confusionMatrix.size(1); j++) {
                                JsonArray element = new JsonArray();
                                element.add(new JsonPrimitive(i));
                                element.add(new JsonPrimitive(j));
                                element.add(new JsonPrimitive(confusionMatrix.get(i, j)));
                                data.add(element);
                            }
                        }
                        eval.add("confusionMatrix", data);
                    }

                    response.getWriter().write(eval.toString());
                    response.getWriter().flush();
                } catch (Exception e) {
                    e.printStackTrace();
                    JsonObject eval = new JsonObject();
                    eval.add("error", new JsonPrimitive(e.getCause().getMessage()));
                    response.getWriter().write(eval.toString());
                    response.getWriter().flush();
                }
            }
            break;
        }
    }
}

From source file:be.iminds.iot.dianne.builder.DianneRunner.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    String id = request.getParameter("id");
    if (id == null) {
        System.out.println("No neural network instance specified");
        return;/*  w ww.ja v a  2  s . com*/
    }
    UUID nnId = UUID.fromString(id);
    NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(nnId);
    if (nni == null) {
        System.out.println("Neural network instance " + id + " not deployed");
        return;
    }

    NeuralNetwork nn = null;
    try {
        nn = dianne.getNeuralNetwork(nni).getValue();
    } catch (Exception e) {
    }
    if (nn == null) {
        System.out.println("Neural network instance " + id + " not available");
        return;
    }

    if (request.getParameter("forward") != null) {
        String inputId = request.getParameter("input");

        JsonObject sample = parser.parse(request.getParameter("forward")).getAsJsonObject();
        Tensor t = jsonConverter.fromJson(sample);

        start = System.currentTimeMillis();
        nn.forward(UUID.fromString(inputId), null, t, "ui");

    } else if (request.getParameter("url") != null) {
        String url = request.getParameter("url");
        String inputId = request.getParameter("input");

        Tensor t = null;
        try {
            URL u = new URL(url);
            BufferedImage img = ImageIO.read(u);
            t = imageConverter.fromImage(img);
        } catch (Exception e) {
            System.out.println("Failed to read image from url " + url);
            return;
        }

        start = System.currentTimeMillis();
        nn.forward(UUID.fromString(inputId), null, t, "ui");

    } else if (request.getParameter("mode") != null) {
        String mode = request.getParameter("mode");
        String targetId = request.getParameter("target");

        Module m = nn.getModules().get(UUID.fromString(targetId));
        if (m != null) {
            m.setMode(EnumSet.of(Mode.valueOf(mode)));
        }
    } else if (request.getParameter("dataset") != null) {
        String dataset = request.getParameter("dataset");
        if (datasets == null) {
            System.out.println("Datasets service not available");
            return;
        }
        Dataset d = datasets.getDataset(dataset);
        if (d == null) {
            System.out.println("Dataset " + dataset + " not available");
            return;
        }

        Sample s = d.getSample(rand.nextInt(d.size()));

        String inputId = request.getParameter("input");

        if (inputId != null) {
            start = System.currentTimeMillis();
            nn.forward(UUID.fromString(inputId), null, s.input, "ui");
        }

        JsonObject sample = jsonConverter.toJson(s.input);
        String[] labels = d.getLabels();
        if (labels != null) {
            sample.add("target", new JsonPrimitive(labels[TensorOps.argmax(s.target)]));
        } else {
            if (s.target.size() < 10) {
                JsonObject target = jsonConverter.toJson(s.target);
                sample.add("target", target.get("data").getAsJsonArray());
            }
        }

        response.getWriter().println(sample.toString());
        response.getWriter().flush();
    }
}

From source file:be.iminds.iot.dianne.builder.DianneRunner.java

License:Open Source License

private String outputSSEMessage(UUID outputId, String[] outputLabels, Tensor output, long time,
        String... tags) {//from  w  w  w. java 2s  .co  m
    JsonObject data = jsonConverter.toJson(output);

    float max = TensorOps.max(output);
    if (outputLabels != null || isProbability(output)) {
        // format output as [['label', val],['label2',val2],...] for in highcharts
        String[] labels;
        float[] values;
        if (output.size() > 10) {
            // if more than 10 outputs, only send top-10 results
            Integer[] indices = new Integer[output.size()];
            for (int i = 0; i < output.size(); i++) {
                indices[i] = i;
            }
            Arrays.sort(indices, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    float v1 = output.get(o1);
                    float v2 = output.get(o2);
                    // inverse order to have large->small order
                    return v1 > v2 ? -1 : (v1 < v2 ? 1 : 0);
                }
            });
            labels = new String[10];
            values = new float[10];
            for (int i = 0; i < 10; i++) {
                labels[i] = outputLabels != null ? outputLabels[indices[i]] : "" + indices[i];
                values[i] = output.get(indices[i]);
            }
        } else {
            labels = outputLabels;
            if (labels == null) {
                labels = new String[output.size()];
                for (int i = 0; i < labels.length; i++) {
                    labels[i] = "" + i;
                }
            }
            values = output.get();
        }

        JsonArray probabilities = new JsonArray();
        for (int i = 0; i < values.length; i++) {
            // if negative values for classification - assume log probabilities
            // take exp to return probability
            if (max < 0) {
                values[i] = (float) Math.exp(values[i]);
            }
            probabilities.add(new JsonPrimitive(values[i]));
        }
        data.add("probabilities", probabilities);

        JsonArray l = new JsonArray();
        for (int i = 0; i < labels.length; i++) {
            l.add(new JsonPrimitive(labels[i]));
        }
        data.add("labels", l);
    }

    if (time > 0) {
        data.add("time", new JsonPrimitive(time));
    }

    if (tags != null) {
        JsonArray ta = new JsonArray();
        for (String tt : tags) {
            if (tt.equals("ui") || tt.startsWith("_")) // ignore the ui tag
                continue;

            ta.add(new JsonPrimitive(tt));
        }
        data.add("tags", ta);
    }

    data.add("id", new JsonPrimitive(outputId.toString()));

    StringBuilder builder = new StringBuilder();
    builder.append("data: ").append(data.toString()).append("\n\n");
    return builder.toString();
}

From source file:be.iminds.iot.dianne.builder.DianneRunner.java

License:Open Source License

private String errorSSEMessage(ModuleException exception) {
    JsonObject data = new JsonObject();

    data.add("id", new JsonPrimitive(exception.moduleId.toString()));
    data.add("error", new JsonPrimitive(exception.getMessage()));

    StringBuilder builder = new StringBuilder();
    builder.append("data: ").append(data.toString()).append("\n\n");
    return builder.toString();
}