List of usage examples for com.google.gson JsonObject get
public JsonElement get(String memberName)
From source file:basedefense.client.version.ModificationVersionCheck.java
License:Apache License
/** * Checks the modification version.//from w ww . ja va2 s . c om * * @return The version. */ public Optional<String> check() { // We will only run this check once as this check may take a lot of time to process if (this.latestVersion != null) return this.latestVersion; InputStreamReader inputStreamReader = null; try { HttpGet getRequest = new HttpGet(API_URL + String.format(API_PATTERN, "LordAkkarin", "BaseDefense2")); HttpResponse response = this.httpClient.execute(getRequest); Preconditions.checkState(response.getStatusLine().getStatusCode() == 200, "Expected status code 200 but received %s", response.getStatusLine()); HttpEntity entity = response.getEntity(); inputStreamReader = new InputStreamReader(entity.getContent()); Gson gson = new Gson(); JsonObject object = gson.fromJson(inputStreamReader, JsonObject.class); Preconditions.checkState(object.has("tag_name"), "No valid version found."); this.latestVersion = Optional.of(object.get("tag_name").getAsString()); } catch (Exception ex) { BaseDefenseModification.getInstance().getLogger() .warn("Unable to retrieve version information: " + ex.getMessage(), ex); this.latestVersion = Optional.empty(); } finally { IOUtils.closeQuietly(inputStreamReader); } return this.latestVersion; }
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)); }/*from ww w .j a 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.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 w w.j a v a 2 s.co m*/ 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;/*from w ww . j a v a 2 s. c o m*/ } 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.dataset.DatasetConfigurator.java
License:Open Source License
private void parseDatasetConfiguration(File f) { try {//from w w w . ja va 2s .c o m // parse any adapter configurations from JSON and apply config? JsonParser parser = new JsonParser(); JsonObject json = parser.parse(new JsonReader(new FileReader(f))).getAsJsonObject(); String name = json.get("name").getAsString(); if (name == null) return; // should have a name Hashtable<String, Object> props = new Hashtable<>(); String dir = f.getParentFile().getAbsolutePath(); props.put("dir", dir); String pid = null; if (json.has("adapter")) { String adapter = json.get("adapter").getAsString(); pid = adapter.contains(".") ? adapter : "be.iminds.iot.dianne.dataset.adapters." + adapter; // in case of adapter, set Dataset target: the dataset it is adapting String dataset = json.get("dataset").getAsString(); props.put("Dataset.target", "(name=" + dataset + ")"); } else if (json.has("type")) { String type = json.get("type").getAsString(); pid = "be.iminds.iot.dianne.dataset." + type; } else { // some hard coded pids if (name.startsWith("MNIST")) { pid = "be.iminds.iot.dianne.dataset.MNIST"; } else if (name.startsWith("CIFAR-100")) { pid = "be.iminds.iot.dianne.dataset.CIFAR100"; } else if (name.startsWith("CIFAR-10")) { pid = "be.iminds.iot.dianne.dataset.CIFAR10"; } else if (name.startsWith("STL-10")) { pid = "be.iminds.iot.dianne.dataset.STL10"; } else if (name.startsWith("SVHN")) { pid = "be.iminds.iot.dianne.dataset.SVHN"; } else if (name.equalsIgnoreCase("ImageNetValidation")) { pid = "be.iminds.iot.dianne.dataset.ImageNet.validation"; } else if (name.equalsIgnoreCase("ImageNetTraining")) { pid = "be.iminds.iot.dianne.dataset.ImageNet.training"; } else { pid = "be.iminds.iot.dianne.dataset." + name; } } // set an aiolos instance id using the dataset name to treat // equally named datasets as single instance in the network props.put("aiolos.instance.id", name); // combine all offered interfaces (might be SequenceDataset or ExperiencePool) props.put("aiolos.combine", "*"); // TODO use object conversion from JSON here? Configuration config = ca.createFactoryConfiguration(pid, null); json.entrySet().stream().forEach(e -> { if (e.getValue().isJsonArray()) { JsonArray a = e.getValue().getAsJsonArray(); String[] val = new String[a.size()]; for (int i = 0; i < val.length; i++) { val[i] = a.get(i).getAsString(); } props.put(e.getKey(), val); } else { props.put(e.getKey(), e.getValue().getAsString()); } }); config.update(props); } catch (Exception e) { System.err.println("Error parsing Dataset config file: " + f.getAbsolutePath()); e.printStackTrace(); } }
From source file:be.iminds.iot.dianne.jsonrpc.DianneRequestHandler.java
License:Open Source License
@Override public void handleRequest(JsonObject request, JsonWriter writer) throws IOException { String i = "null"; if (request.has("id")) { i = request.get("id").getAsString(); }/*w w w .j a v a2 s . c o m*/ final String id = i; if (!request.has("jsonrpc")) { writeError(writer, id, -32600, "Invalid JSONRPC request"); return; } if (!request.get("jsonrpc").getAsString().equals("2.0")) { writeError(writer, id, -32600, "Wrong JSONRPC version: " + request.get("jsonrpc").getAsString()); return; } if (!request.has("method")) { writeError(writer, id, -32600, "No method specified"); return; } String method = request.get("method").getAsString(); // TODO use a more generic approach here? switch (method) { case "deploy": try { String description = null; UUID runtimeId = null; String[] tags = null; NeuralNetworkInstanceDTO nni; JsonArray params = request.get("params").getAsJsonArray(); if (params.size() > 1) { description = params.get(1).getAsString(); } if (params.size() > 2) { runtimeId = UUID.fromString(params.get(2).getAsString()); } if (params.size() > 3) { tags = new String[params.size() - 3]; for (int t = 3; t < params.size(); t++) { tags[t - 3] = params.get(t).getAsString(); } } if (params.get(0).isJsonPrimitive()) { String nnName = params.get(0).getAsString(); nni = platform.deployNeuralNetwork(nnName, description, runtimeId, tags); } else { NeuralNetworkDTO nn = DianneJSONConverter.parseJSON(params.get(0).getAsJsonObject()); nni = platform.deployNeuralNetwork(nn, description, runtimeId, tags); } writeResult(writer, id, nni.id.toString()); } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } break; case "undeploy": try { JsonArray params = request.get("params").getAsJsonArray(); if (params.get(0).isJsonPrimitive()) { String s = params.get(0).getAsString(); UUID nnId = UUID.fromString(s); platform.undeployNeuralNetwork(nnId); writeResult(writer, id, nnId); } } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } break; case "forward": try { JsonArray params = request.get("params").getAsJsonArray(); if (params.size() != 2) { throw new Exception("2 parameters expected"); } if (!params.get(0).isJsonPrimitive()) throw new Exception("first parameter should be neural network instance id"); if (!params.get(1).isJsonArray()) throw new Exception("second parameter should be input data"); String s = params.get(0).getAsString(); UUID nnId = UUID.fromString(s); NeuralNetworkInstanceDTO nni = platform.getNeuralNetworkInstance(nnId); if (nni == null) { writeError(writer, id, -32603, "Neural network with id " + nnId + " does not exist."); return; } NeuralNetwork nn = dianne.getNeuralNetwork(nni).getValue(); JsonArray in = params.get(1).getAsJsonArray(); Tensor input = asTensor(in); nn.forward(null, null, input).then(p -> { int argmax = TensorOps.argmax(p.getValue().tensor); writeResult(writer, id, nn.getOutputLabels()[argmax]); return null; }, p -> { writeError(writer, id, -32603, "Error during forward: " + p.getFailure().getMessage()); }); } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } break; case "learn": case "eval": case "act": String[] nnName = null; NeuralNetworkDTO nn = null; String dataset; Map<String, String> config; try { JsonArray params = request.get("params").getAsJsonArray(); if (params.get(0).isJsonPrimitive()) { nnName = params.get(0).getAsString().split(","); for (int k = 0; k < nnName.length; k++) { nnName[k] = nnName[k].trim(); } } else { nn = DianneJSONConverter.parseJSON(params.get(0).getAsJsonObject()); } dataset = params.get(1).getAsString(); config = params.get(2).getAsJsonObject().entrySet().stream() .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getAsString())); } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } // call coordinator if (method.equals("learn")) { // learn Promise<LearnResult> result = null; if (nnName != null) { result = coordinator.learn(dataset, config, nnName); } else { result = coordinator.learn(dataset, config, nn); } try { result.then(p -> { writeResult(writer, id, p.getValue()); return null; }, p -> { writeError(writer, id, -32603, "Error during learning: " + p.getFailure().getMessage()); }).getValue(); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } else if (method.equals("eval")) { // eval Promise<EvaluationResult> result = null; if (nnName != null) { result = coordinator.eval(dataset, config, nnName); } else { result = coordinator.eval(dataset, config, nn); } try { result.then(p -> { writeResult(writer, id, p.getValue()); return null; }, p -> { writeError(writer, id, -32603, "Error during learning: " + p.getFailure().getMessage()); }).getValue(); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } else if (method.equals("act")) { Promise<AgentResult> result = null; if (nnName != null) { result = coordinator.act(dataset, config, nnName); } else { result = coordinator.act(dataset, config, nn); } try { result.then(p -> { writeResult(writer, id, null); return null; }, p -> { writeError(writer, id, -32603, "Error during learning: " + p.getFailure().getMessage()); }).getValue(); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } break; case "learnResult": case "evaluationResult": case "agentResult": case "job": case "stop": UUID jobId = null; try { JsonArray params = request.get("params").getAsJsonArray(); if (params.get(0).isJsonPrimitive()) { String s = params.get(0).getAsString(); jobId = UUID.fromString(s); } } catch (Exception e) { writeError(writer, id, -32602, "Incorrect parameters provided: " + e.getMessage()); return; } if (method.equals("learnResult")) { writeResult(writer, id, coordinator.getLearnResult(jobId)); } else if (method.equals("evaluationResult")) { writeResult(writer, id, coordinator.getEvaluationResult(jobId)); } else if (method.equals("agentResult")) { writeResult(writer, id, coordinator.getAgentResult(jobId)); } else if (method.equals("stop")) { try { coordinator.stop(jobId); } catch (Exception e) { writeError(writer, id, -32603, "Error stopping job: " + e.getMessage()); } } else { writeResult(writer, id, coordinator.getJob(jobId)); } break; case "availableNeuralNetworks": writeResult(writer, id, platform.getAvailableNeuralNetworks()); break; case "availableDatasets": writeResult(writer, id, datasets.getDatasets().stream().map(d -> d.name).collect(Collectors.toList())); break; case "queuedJobs": writeResult(writer, id, coordinator.queuedJobs()); break; case "runningJobs": writeResult(writer, id, coordinator.runningJobs()); break; case "finishedJobs": writeResult(writer, id, coordinator.finishedJobs()); break; case "notifications": writeResult(writer, id, coordinator.getNotifications()); break; case "status": writeResult(writer, id, coordinator.getStatus()); break; case "devices": writeResult(writer, id, coordinator.getDevices()); break; default: writeError(writer, id, -32601, "Method " + method + " not found"); } }
From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java
License:Open Source License
public static NeuralNetworkDTO parseJSON(JsonObject json) { String name = null;/*from ww w.jav a 2s .com*/ List<ModuleDTO> modules = new ArrayList<ModuleDTO>(); if (json.has("name")) { name = json.get("name").getAsString(); } // could be either a nice NeuralNetworkDTO or just a bunch of modules JsonObject jsonModules = json; if (json.has("modules")) { jsonModules = json.get("modules").getAsJsonObject(); } for (Entry<String, JsonElement> module : jsonModules.entrySet()) { JsonObject moduleJson = (JsonObject) module.getValue(); modules.add(parseModuleJSON(moduleJson)); } return new NeuralNetworkDTO(name, modules); }
From source file:be.iminds.iot.dianne.nn.util.DianneJSONConverter.java
License:Open Source License
private static ModuleDTO parseModuleJSON(JsonObject jsonModule) { UUID id = UUID.fromString(jsonModule.get("id").getAsString()); String type = jsonModule.get("type").getAsString(); UUID[] next = null, prev = null; Map<String, String> properties = new HashMap<String, String>(); if (jsonModule.has("next")) { if (jsonModule.get("next").isJsonArray()) { JsonArray jsonNext = jsonModule.get("next").getAsJsonArray(); next = new UUID[jsonNext.size()]; int i = 0; Iterator<JsonElement> it = jsonNext.iterator(); while (it.hasNext()) { JsonElement e = it.next(); next[i++] = UUID.fromString(e.getAsString()); }//from w ww . j av a 2 s. com } else { next = new UUID[1]; next[0] = UUID.fromString(jsonModule.get("next").getAsString()); } } if (jsonModule.has("prev")) { if (jsonModule.get("prev").isJsonArray()) { JsonArray jsonPrev = jsonModule.get("prev").getAsJsonArray(); prev = new UUID[jsonPrev.size()]; int i = 0; Iterator<JsonElement> it = jsonPrev.iterator(); while (it.hasNext()) { JsonElement e = it.next(); prev[i++] = UUID.fromString(e.getAsString()); } } else { prev = new UUID[1]; prev[0] = UUID.fromString(jsonModule.get("prev").getAsString()); } } // TODO this uses the old model where properties where just stored as flatmap for (Entry<String, JsonElement> property : jsonModule.entrySet()) { String key = property.getKey(); if (key.equals("id") || key.equals("type") || key.equals("prev") || key.equals("next")) { continue; // this is only for module-specific properties } properties.put(property.getKey(), property.getValue().getAsString()); } // TODO evolve to a separate "properties" item if (jsonModule.has("properties")) { JsonObject jsonProperties = jsonModule.get("properties").getAsJsonObject(); for (Entry<String, JsonElement> jsonProperty : jsonProperties.entrySet()) { String key = jsonProperty.getKey(); String value = jsonProperty.getValue().getAsString(); properties.put(key, value); } } ModuleDTO dto = new ModuleDTO(id, type, next, prev, properties); return dto; }
From source file:be.jacobsvanroy.LotterDraws.java
License:Open Source License
private List<Integer> mapDrawsToList(String result) { JsonObject root = (JsonObject) new JsonParser().parse(result); JsonArray jsonArray = root.get("d").getAsJsonArray(); List<Integer> draws = new ArrayList<>(); jsonArray.forEach(jsonEl -> draws.add(jsonEl.getAsJsonObject().get("DrawRef").getAsInt())); return draws; }
From source file:be.ugent.tiwi.sleroux.newsrec.recommendationstester.CustomDeserializer.java
@Override public NewsItemCluster deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jo = json.getAsJsonObject(); RecommendedNewsItem[] items = context.deserialize(jo.get("items"), RecommendedNewsItem[].class); RecommendedNewsItem rep = context.deserialize(jo.get("representative"), RecommendedNewsItem.class); NewsItemCluster cluster = new NewsItemCluster(); cluster.setItems(Arrays.asList(items)); cluster.setRepresentative(rep);/*from ww w . j a va 2s.c o m*/ return cluster; }