Java Examples for javax.json.JsonArrayBuilder

The following java examples will help you to understand the usage of javax.json.JsonArrayBuilder. These source code samples are taken from different open source projects.

Example 1
Project: HAP-Java-master  File: BaseCharacteristic.java View source code
/**
	 * Creates the JSON serialized form of the accessory for use over the Homekit Accessory Protocol.
	 * 
	 * @param instanceId the static id of the accessory.
	 * @return a future that will complete with the JSON builder for the object.
	 */
protected CompletableFuture<JsonObjectBuilder> makeBuilder(int instanceId) {
    return getValue().exceptionally( t -> {
        logger.error("Could not retrieve value " + this.getClass().getName(), t);
        return null;
    }).thenApply( value -> {
        JsonArrayBuilder perms = Json.createArrayBuilder();
        if (isWritable) {
            perms.add("pw");
        }
        if (isReadable) {
            perms.add("pr");
        }
        if (isEventable) {
            perms.add("ev");
        }
        JsonObjectBuilder builder = Json.createObjectBuilder().add("iid", instanceId).add("type", type).add("perms", perms.build()).add("format", format).add("events", false).add("bonjour", false).add("description", description);
        setJsonValue(builder, value);
        return builder;
    });
}
Example 2
Project: affablebean-angularjs-ee7-master  File: DepartmentService.java View source code
@GET
@Path("{id}")
@Produces("application/json")
public JsonArray getDepartmentProducts(@PathParam("id") Short categoryId) {
    JsonArrayBuilder jb = Json.createArrayBuilder();
    Category selectedCategory = categoryFacade.find(categoryId);
    if (selectedCategory != null) {
        for (Product p : selectedCategory.getProductCollection()) {
            JsonObjectBuilder jpb = Json.createObjectBuilder().add("id", p.getId()).add("name", p.getName()).add("price", p.getPrice());
            jb.add(jpb);
        }
    }
    return jb.build();
}
Example 3
Project: XCoLab-master  File: ModelInputGroupDisplayItem.java View source code
@Override
public JsonObjectBuilder toJson() {
    final JsonObjectBuilder jsonBuilder = super.toJson().add("groupType", getGroupType().name()).add("groupId", getGroupId()).add("parentGroupId", getParentGroupId());
    JsonArrayBuilder childInputs = Json.createArrayBuilder();
    for (ModelInputDisplayItem input : getAllItems()) {
        childInputs.add(input.toJson());
    }
    return jsonBuilder.add("inputs", childInputs);
}
Example 4
Project: cargotracker-master  File: CargoMonitoringService.java View source code
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonArray getAllCargo() {
    List<Cargo> cargos = cargoRepository.findAll();
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (Cargo cargo : cargos) {
        builder.add(Json.createObjectBuilder().add("trackingId", cargo.getTrackingId().getIdString()).add("routingStatus", cargo.getDelivery().getRoutingStatus().toString()).add("misdirected", cargo.getDelivery().isMisdirected()).add("transportStatus", cargo.getDelivery().getTransportStatus().toString()).add("atDestination", cargo.getDelivery().isUnloadedAtDestination()).add("origin", cargo.getOrigin().getUnLocode().getIdString()).add("lastKnownLocation", cargo.getDelivery().getLastKnownLocation().getUnLocode().getIdString().equals("XXXXX") ? "Unknown" : cargo.getDelivery().getLastKnownLocation().getUnLocode().getIdString()));
    }
    return builder.build();
}
Example 5
Project: PonySDK-master  File: PAddOn.java View source code
protected void callTerminalMethod(final String methodName, final Object... args) {
    final JsonObjectBuilder builder = Json.createObjectBuilder();
    builder.add(METHOD_PROPERTY_NAME, methodName);
    if (args.length > 0) {
        final JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
        for (final Object object : args) {
            if (object != null) {
                if (object instanceof JsonValue) {
                    arrayBuilder.add((JsonValue) object);
                } else if (object instanceof Number) {
                    final Number number = (Number) object;
                    if (object instanceof Byte || object instanceof Short || object instanceof Integer)
                        arrayBuilder.add(number.intValue());
                    else if (object instanceof Long)
                        arrayBuilder.add(number.longValue());
                    else if (object instanceof Float || object instanceof Double)
                        arrayBuilder.add(number.doubleValue());
                    else if (object instanceof BigInteger)
                        arrayBuilder.add((BigInteger) object);
                    else if (object instanceof BigDecimal)
                        arrayBuilder.add((BigDecimal) object);
                    else
                        arrayBuilder.add(number.doubleValue());
                } else if (object instanceof Boolean) {
                    arrayBuilder.add((Boolean) object);
                } else if (object instanceof JsonArrayBuilder) {
                    arrayBuilder.add(((JsonArrayBuilder) object).build());
                } else if (object instanceof JsonObjectBuilder) {
                    arrayBuilder.add(((JsonObjectBuilder) object).build());
                } else if (object instanceof Collection) {
                    throw new IllegalArgumentException("Collections are not supported for PAddOn, you need to convert it to JsonArray on primitive array");
                } else {
                    arrayBuilder.add(object.toString());
                }
            } else {
                arrayBuilder.addNull();
            }
        }
        builder.add(ARGUMENTS_PROPERTY_NAME, arrayBuilder);
    }
    saveUpdate( writer -> writer.write(ServerToClientModel.NATIVE, builder.build()));
}
Example 6
Project: Reminders-master  File: ListListWriter.java View source code
@Override
public void writeTo(java.util.List<List> lists, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    JsonArrayBuilder jsonListList = Json.createArrayBuilder();
    for (List list : lists) {
        JsonObjectBuilder jsonList = Json.createObjectBuilder();
        jsonList.add("id", list.getId());
        jsonList.add("title", list.getTitle());
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("RemindersPU");
        EntityManager em = emf.createEntityManager();
        Query q = em.createNamedQuery("List.findSize").setParameter("list", list);
        jsonList.add("size", (Long) q.getSingleResult());
        em.close();
        emf.close();
        jsonListList.add(jsonList);
    }
    try (JsonWriter writer = Json.createWriter(entityStream)) {
        writer.writeArray(jsonListList.build());
    }
}
Example 7
Project: jaxrs-analyzer-master  File: SchemaBuilder.java View source code
@Override
public void visit(final TypeRepresentation.EnumTypeRepresentation representation) {
    builder.add("type", "string");
    if (!representation.getEnumValues().isEmpty()) {
        final JsonArrayBuilder array = representation.getEnumValues().stream().sorted().collect(Json::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::add);
        builder.add("enum", array);
    }
}
Example 8
Project: oshi-master  File: NetworkIF.java View source code
/**
     * {@inheritDoc}
     */
@Override
public JsonObject toJSON(Properties properties) {
    JsonObjectBuilder json = NullAwareJsonObjectBuilder.wrap(this.jsonFactory.createObjectBuilder());
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.name")) {
        json.add("name", getName());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.displayName")) {
        json.add("displayName", getDisplayName());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.mac")) {
        json.add("mac", getMacaddr());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.ipv4")) {
        JsonArrayBuilder ipv4ArrayBuilder = this.jsonFactory.createArrayBuilder();
        for (String ipv4 : getIPv4addr()) {
            ipv4ArrayBuilder.add(ipv4);
        }
        json.add("ipv4", ipv4ArrayBuilder.build());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.ipv6")) {
        JsonArrayBuilder ipv6ArrayBuilder = this.jsonFactory.createArrayBuilder();
        for (String ipv6 : getIPv6addr()) {
            ipv6ArrayBuilder.add(ipv6);
        }
        json.add("ipv6", ipv6ArrayBuilder.build());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.mtu")) {
        json.add("mtu", getMTU());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.bytesRecv")) {
        json.add("bytesRecv", getBytesRecv());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.bytesSent")) {
        json.add("bytesSent", getBytesSent());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.packetsRecv")) {
        json.add("packetsRecv", getPacketsRecv());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.packetsSent")) {
        json.add("packetsSent", getPacketsSent());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.inErrors")) {
        json.add("inErrors", getInErrors());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.outErrors")) {
        json.add("outErrors", getOutErrors());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.speed")) {
        json.add("speed", getSpeed());
    }
    if (PropertiesUtil.getBoolean(properties, "hardware.networks.timeStamp")) {
        json.add("timeStamp", getSpeed());
    }
    return json.build();
}
Example 9
Project: Pregelix_Social_Graph-master  File: GraphTypeAccessor.java View source code
private String assembleFields() {
    JsonObjectBuilder model = Json.createObjectBuilder().add("source_node", getSourceNode());
    Iterator<Integer> it = target_nodes.iterator();
    JsonArrayBuilder targets = Json.createArrayBuilder();
    while (it.hasNext()) {
        targets.add(it.next());
    }
    // requirement for ordered list
    targets.addNull();
    model.add("target_nodes", targets);
    Iterator<Double> dt = weight.iterator();
    JsonArrayBuilder w = Json.createArrayBuilder();
    while (dt.hasNext()) {
        w.add(dt.next());
    }
    w.addNull();
    model.add("weight", w);
    return model.build().toString();
}
Example 10
Project: quickstart-master  File: BiddingEncoder.java View source code
@Override
public String encode(Bidding bidding) throws EncodeException {
    // It uses the JSON-P API to create a JSON representation
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder().add("item", Json.createObjectBuilder().add("buyNowPrice", bidding.getItem().getBuyNowPrice()).add("description", bidding.getItem().getDescription()).add("imagePath", bidding.getItem().getImagePath()).add("title", bidding.getItem().getTitle()).build()).add("bidStatus", bidding.getBidStatus().toString()).add("currentPrice", bidding.getCurrentPrice()).add("secondsLeft", 0);
    if (bidding.getDueDate() != null) {
        jsonBuilder.add("dueDate", bidding.getDueDate().getTime());
    }
    if (bidding.getSecondsLeft() != null) {
        jsonBuilder.add("secondsLeft", bidding.getSecondsLeft());
    }
    JsonArrayBuilder jsonBidArray = Json.createArrayBuilder();
    for (Bid bid : bidding.getBids()) {
        jsonBidArray.add(Json.createObjectBuilder().add("dateTime", bid.getDateTime().getTime()).add("value", bid.getValue()).add("id", bid.getId()).build());
    }
    jsonBuilder.add("bids", jsonBidArray);
    StringWriter stWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(stWriter);
    jsonWriter.writeObject(jsonBuilder.build());
    jsonWriter.close();
    return stWriter.toString();
}
Example 11
Project: takes-master  File: RsJsonTest.java View source code
/**
     * RsJSON can build a big JSON response.
     * @throws IOException If some problem inside
     */
@Test
public void buildsBigJsonResponse() throws IOException {
    final int size = 100000;
    final JsonArrayBuilder builder = Json.createArrayBuilder();
    for (int idx = 0; idx < size; ++idx) {
        builder.add(Json.createObjectBuilder().add("number", "212 555-1234"));
    }
    MatcherAssert.assertThat(Json.createReader(new RsJson(builder.build()).body()).readArray().size(), Matchers.equalTo(size));
}
Example 12
Project: activemq-artemis-master  File: ServerSessionImpl.java View source code
@Override
public void describeProducersInfo(JsonArrayBuilder array) throws Exception {
    Map<SimpleString, Pair<Object, AtomicLong>> targetCopy = cloneTargetAddresses();
    for (Map.Entry<SimpleString, Pair<Object, AtomicLong>> entry : targetCopy.entrySet()) {
        String uuid = null;
        if (entry.getValue().getA() != null) {
            uuid = entry.getValue().getA().toString();
        }
        JsonObjectBuilder producerInfo = JsonLoader.createObjectBuilder().add("connectionID", this.getConnectionID().toString()).add("sessionID", this.getName()).add("destination", entry.getKey().toString()).add("lastUUIDSent", nullSafe(uuid)).add("msgSent", entry.getValue().getB().longValue());
        array.add(producerInfo);
    }
}
Example 13
Project: cxf-master  File: Catalog.java View source code
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonArray getBooks() throws IOException {
    final IndexReader reader = DirectoryReader.open(directory);
    final IndexSearcher searcher = new IndexSearcher(reader);
    final JsonArrayBuilder builder = Json.createArrayBuilder();
    try {
        final Query query = new MatchAllDocsQuery();
        for (final ScoreDoc scoreDoc : searcher.search(query, 1000).scoreDocs) {
            final DocumentStoredFieldVisitor fieldVisitor = new DocumentStoredFieldVisitor(LuceneDocumentMetadata.SOURCE_FIELD);
            reader.document(scoreDoc.doc, fieldVisitor);
            builder.add(fieldVisitor.getDocument().getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue());
        }
        return builder.build();
    } finally {
        reader.close();
    }
}
Example 14
Project: dataverse-master  File: SearchIT.java View source code
private static Response createDataverse(TestDataverse dataverseToCreate, TestUser creator) {
    JsonArrayBuilder contactArrayBuilder = Json.createArrayBuilder();
    contactArrayBuilder.add(Json.createObjectBuilder().add("contactEmail", creator.getEmail()));
    JsonArrayBuilder subjectArrayBuilder = Json.createArrayBuilder();
    subjectArrayBuilder.add("Other");
    JsonObject dvData = Json.createObjectBuilder().add("alias", dataverseToCreate.alias).add("name", dataverseToCreate.name).add("dataverseContacts", contactArrayBuilder).add("dataverseSubjects", subjectArrayBuilder).build();
    Response createDataverseResponse = given().body(dvData.toString()).contentType(ContentType.JSON).when().post("/api/dataverses/:root?key=" + creator.apiToken);
    return createDataverseResponse;
}
Example 15
Project: eclipselink.runtime-master  File: JSONTestCases.java View source code
public void marshalToArrayBuilderResult() throws Exception {
    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
    JsonArrayBuilderResult result = new JsonArrayBuilderResult(jsonArrayBuilder);
    jsonMarshaller.marshal(getWriteControlObject(), result);
    JsonArray jsonArray = jsonArrayBuilder.build();
    StringWriter sw = new StringWriter();
    JsonWriter writer = Json.createWriter(sw);
    writer.writeArray(jsonArray);
    writer.close();
    log(sw.toString());
    compareStringToControlFile("**testJSONMarshalToBuilderResult**", sw.toString());
}
Example 16
Project: jersey-master  File: DocumentFilteringResource.java View source code
@POST
public JsonArray filter(final JsonArray properties) {
    final JsonArrayBuilder documents = Json.createArrayBuilder();
    final List<JsonString> propertyList = properties.getValuesAs(JsonString.class);
    for (final JsonObject jsonObject : DocumentStorage.getAll()) {
        final JsonObjectBuilder documentBuilder = Json.createObjectBuilder();
        for (final JsonString property : propertyList) {
            final String key = property.getString();
            if (jsonObject.containsKey(key)) {
                documentBuilder.add(key, jsonObject.get(key));
            }
        }
        final JsonObject document = documentBuilder.build();
        if (!document.isEmpty()) {
            documents.add(document);
        }
    }
    return documents.build();
}
Example 17
Project: jersey2-restfull-examples-master  File: StudentRestServiceResource.java View source code
@GET
@Path("/all")
@Produces({ MediaType.APPLICATION_JSON })
public JsonArray getAll() {
    JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
    JsonObject jsonStudentObject;
    JsonObject jsonClassRoomObject;
    Student student;
    ClassRoom classRoom;
    for (int i = 1; i <= 100; i++) {
        student = new Student();
        classRoom = new ClassRoom(i, "classRoom" + i);
        student.setSid(i);
        student.setName("student" + i);
        student.setDate(Calendar.getInstance().getTime());
        student.setClassRoom(classRoom);
        jsonClassRoomObject = Json.createObjectBuilder().add("cid", student.getClassRoom().getCid()).add("cname", student.getClassRoom().getName()).build();
        jsonStudentObject = Json.createObjectBuilder().add("sid", student.getSid()).add("sname", student.getName()).add("date", new SimpleDateFormat().format(student.getDate())).add("classroom", jsonClassRoomObject).build();
        jsonArrayBuilder.add(jsonStudentObject);
    }
    return jsonArrayBuilder.build();
}
Example 18
Project: jsonp-master  File: JsonParserImpl.java View source code
private JsonArray getArray(JsonArrayBuilder builder) {
    while (hasNext()) {
        JsonParser.Event e = next();
        if (e == JsonParser.Event.END_ARRAY) {
            return builder.build();
        }
        builder.add(getValue());
    }
    throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
}
Example 19
Project: JTL-FIleService-master  File: MongoDBController.java View source code
/*-----------------------------------------------------------
	 * Performance Results Methods ------------------------------
	 * ----------------------------------------------------------
	 * */
public JsonArray addPerfResults(String perfResults) {
    DBCollection coll = db.getCollection("performance");
    BasicDBObject newPerfResults = (BasicDBObject) JSON.parse(perfResults);
    newPerfResults.append("_id", newPerfResults.getString("name"));
    BasicDBObject query = new BasicDBObject();
    query.append("name", newPerfResults.getString("name"));
    BasicDBObject removeId = new BasicDBObject("_id", 0);
    DBCursor cursor = coll.find(query, removeId);
    JsonObjectBuilder objBuild = Json.createObjectBuilder();
    JsonArrayBuilder arrBuild = Json.createArrayBuilder();
    JsonObject json = objBuild.build();
    if (cursor.count() > 0) {
        LOG.info("Performance Results Found: ");
        BasicDBObject found = (BasicDBObject) cursor.next();
        json = Json.createReader(new StringReader(found.toString())).readObject();
        arrBuild.add(json);
    } else {
        LOG.info("New Performance Results Created: " + coll.insert(newPerfResults));
        json = Json.createReader(new StringReader(newPerfResults.toString())).readObject();
        arrBuild.add(json);
    }
    LOG.info("----------------------------------- ARR BUILD -------------------------------------------------\n" + arrBuild.build().toString());
    return arrBuild.build();
}
Example 20
Project: lightfish-master  File: SnapshotsSocketJSON.java View source code
public JsonObject toJson(Object object) {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    Method methods[] = object.getClass().getDeclaredMethods();
    try {
        for (int i = 0; i < methods.length; i++) {
            String method = methods[i].getName();
            if (method.startsWith("get")) {
                Object result = methods[i].invoke(object);
                String property = method.replaceFirst("get", "");
                property = Character.toLowerCase(property.charAt(0)) + property.substring(1);
                if (methods[i].getReturnType().equals(List.class)) {
                    List resultList = (List) result;
                    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
                    for (Object entry : resultList) {
                        if (entry instanceof String) {
                            arrayBuilder.add((String) entry);
                        } else {
                            JsonObject toJson = toJson(entry);
                            arrayBuilder.add(toJson);
                        }
                    }
                    objectBuilder.add(property, arrayBuilder.build());
                } else // for nicer JSON message
                if (result instanceof Double) {
                    objectBuilder.add(property, (double) result);
                } else if (result instanceof Integer) {
                    objectBuilder.add(property, (int) result);
                } else if (result instanceof Boolean) {
                    objectBuilder.add(property, (boolean) result);
                } else if (result instanceof Long) {
                    objectBuilder.add(property, (long) result);
                } else if (result instanceof Date) {
                    objectBuilder.add(property, ((Date) result).getTime());
                } else {
                    objectBuilder.add(property, "" + result);
                }
            }
        }
        return objectBuilder.build();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Example 21
Project: loadimpact-sdk-java-master  File: UserScenario.java View source code
public JsonObject toJSON() {
    JsonBuilderFactory f = Json.createBuilderFactory(null);
    JsonObjectBuilder json = f.createObjectBuilder();
    if (name != null)
        json.add("name", name);
    if (type != null)
        json.add("script_type", type);
    if (loadScript != null)
        json.add("load_script", loadScript);
    if (created != null)
        json.add("created", DateUtils.toIso8601(created));
    if (updated != null)
        json.add("updated", DateUtils.toIso8601(updated));
    if (dataStores != null && !dataStores.isEmpty()) {
        JsonArrayBuilder dataStoresJson = f.createArrayBuilder();
        for (int d : dataStores) {
            dataStoresJson.add(d);
        }
        json.add("data_stores", dataStoresJson);
    }
    return json.build();
}
Example 22
Project: nifi-master  File: SiteToSiteStatusReportingTask.java View source code
@Override
public void onTrigger(final ReportingContext context) {
    final boolean isClustered = context.isClustered();
    final String nodeId = context.getClusterNodeIdentifier();
    if (nodeId == null && isClustered) {
        getLogger().debug("This instance of NiFi is configured for clustering, but the Cluster Node Identifier is not yet available. " + "Will wait for Node Identifier to be established.");
        return;
    }
    componentTypeFilter = Pattern.compile(context.getProperty(COMPONENT_TYPE_FILTER_REGEX).evaluateAttributeExpressions().getValue());
    componentNameFilter = Pattern.compile(context.getProperty(COMPONENT_NAME_FILTER_REGEX).evaluateAttributeExpressions().getValue());
    final ProcessGroupStatus procGroupStatus = context.getEventAccess().getControllerStatus();
    final String rootGroupName = procGroupStatus == null ? null : procGroupStatus.getName();
    final String nifiUrl = context.getProperty(INSTANCE_URL).evaluateAttributeExpressions().getValue();
    URL url;
    try {
        url = new URL(nifiUrl);
    } catch (final MalformedURLException e1) {
        throw new AssertionError();
    }
    final String hostname = url.getHost();
    final String platform = context.getProperty(PLATFORM).evaluateAttributeExpressions().getValue();
    final Map<String, ?> config = Collections.emptyMap();
    final JsonBuilderFactory factory = Json.createBuilderFactory(config);
    final DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
    df.setTimeZone(TimeZone.getTimeZone("Z"));
    final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
    serializeProcessGroupStatus(arrayBuilder, factory, procGroupStatus, df, hostname, rootGroupName, platform, null, new Date());
    final JsonArray jsonArray = arrayBuilder.build();
    final int batchSize = context.getProperty(BATCH_SIZE).asInteger();
    int fromIndex = 0;
    int toIndex = Math.min(batchSize, jsonArray.size());
    List<JsonValue> jsonBatch = jsonArray.subList(fromIndex, toIndex);
    while (!jsonBatch.isEmpty()) {
        // Send the JSON document for the current batch
        try {
            long start = System.nanoTime();
            final Transaction transaction = getClient().createTransaction(TransferDirection.SEND);
            if (transaction == null) {
                getLogger().debug("All destination nodes are penalized; will attempt to send data later");
                return;
            }
            final Map<String, String> attributes = new HashMap<>();
            final String transactionId = UUID.randomUUID().toString();
            attributes.put("reporting.task.transaction.id", transactionId);
            attributes.put("mime.type", "application/json");
            JsonArrayBuilder jsonBatchArrayBuilder = factory.createArrayBuilder();
            for (JsonValue jsonValue : jsonBatch) {
                jsonBatchArrayBuilder.add(jsonValue);
            }
            final JsonArray jsonBatchArray = jsonBatchArrayBuilder.build();
            final byte[] data = jsonBatchArray.toString().getBytes(StandardCharsets.UTF_8);
            transaction.send(data, attributes);
            transaction.confirm();
            transaction.complete();
            final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
            getLogger().info("Successfully sent {} Status Records to destination in {} ms; Transaction ID = {}", new Object[] { jsonArray.size(), transferMillis, transactionId });
            fromIndex = toIndex;
            toIndex = Math.min(fromIndex + batchSize, jsonArray.size());
            jsonBatch = jsonArray.subList(fromIndex, toIndex);
        } catch (final IOException e) {
            throw new ProcessException("Failed to send Provenance Events to destination due to IOException:" + e.getMessage(), e);
        }
    }
}
Example 23
Project: sling-master  File: TopologyRequestValidator.java View source code
/**
     * Encodes a request returning the encoded body
     *
     * @param body
     * @return the encoded body.
     * @throws IOException
     */
public String encodeMessage(String body) throws IOException {
    checkActive();
    if (encryptionEnabled) {
        try {
            JsonObjectBuilder json = Json.createObjectBuilder();
            JsonArrayBuilder array = Json.createArrayBuilder();
            for (String value : encrypt(body)) {
                array.add(value);
            }
            json.add("payload", array);
            StringWriter writer = new StringWriter();
            Json.createGenerator(writer).write(json.build()).close();
            return writer.toString();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (BadPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (JsonException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidKeySpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidParameterSpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        }
    }
    return body;
}
Example 24
Project: Teaching-HEIGVD-AMT-Example-MVC-master  File: JsonServlet.java View source code
/**
   * Handles the HTTP <code>GET</code> method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /*
     Firstly, set the Content-type header to the right value
     */
    response.setContentType("application/json;charset=UTF-8");
    /*
     Secondly, build a JSON object with a "fluent" API
     */
    JsonObjectBuilder builder = Json.createObjectBuilder().add("people", Json.createArrayBuilder());
    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
    int numberOfPeople = 2 + (int) (Math.random() * 5);
    for (int i = 0; i < numberOfPeople; i++) {
        arrayBuilder.add(Json.createObjectBuilder().add("firstName", Chance.randomFirstName()).add("lastName", Chance.randomLastName()).add("id", (int) (Math.random() * 10000)));
    }
    JsonObject model = builder.add("people", arrayBuilder).build();
    /*
     Thirdly, serialize the JSON object to a string value
     */
    StringWriter stWriter = new StringWriter();
    JsonWriter jsonWriter = Json.createWriter(stWriter);
    jsonWriter.writeObject(model);
    jsonWriter.close();
    String jsonPayload = stWriter.toString();
    /*
     Finally, send the serialized JSON payload to the client
     */
    try (PrintWriter out = response.getWriter()) {
        out.println(jsonPayload);
    }
}
Example 25
Project: cloutree-modelevaluator-master  File: PmmlPredictiveModelResult.java View source code
/* (non-Javadoc)
	 * @see com.cloutree.modelevaluator.PredictiveModelResult#toJSON()
	 */
@Deprecated
@Override
public String toJSON() {
    JsonObjectBuilder builder = Json.createObjectBuilder();
    JsonArrayBuilder outputBuilder = Json.createArrayBuilder();
    // Output Values
    Set<String> outputKeys = this.outputValues.keySet();
    for (String outputKey : outputKeys) {
        Object val = this.outputValues.get(outputKey);
        if (val != null) {
            outputBuilder.add(Json.createObjectBuilder().add(outputKey, val.toString()));
        }
    }
    builder.add("outputValues", outputBuilder);
    JsonArrayBuilder predictedBuilder = Json.createArrayBuilder();
    Set<String> predictedKeys = this.predictedValues.keySet();
    for (String predictedKey : predictedKeys) {
        Object val = this.predictedValues.get(predictedKey);
        if (val != null) {
            predictedBuilder.add(Json.createObjectBuilder().add(predictedKey, val.toString()));
        }
    }
    builder.add("predictedValues", predictedBuilder);
    JsonArrayBuilder errorBuilder = Json.createArrayBuilder();
    for (String error : this.errors) {
        errorBuilder.add(error);
    }
    builder.add("errors", errorBuilder);
    JsonObject jsonObject = builder.build();
    return jsonObject.toString();
}
Example 26
Project: jackson-javax-json-master  File: CompatibilityTest.java View source code
@Test
public void handleArrayWithArbitraryImpl() throws Exception {
    JsonString jsonString = new MyJsonString("zeString");
    StringWriter writer = new StringWriter();
    JsonWriter jsonWriter = PROVIDER.createWriter(writer);
    JsonArrayBuilder array = PROVIDER.createArrayBuilder().add(jsonString);
    jsonWriter.write(array.build());
    jsonWriter.close();
    assertThat(writer.toString(), equalTo("[\"zeString\"]"));
}
Example 27
Project: picketlink-master  File: JWKBuilder.java View source code
/**
     * The key_ops (key operations) member identifies the operation(s) that the key is intended to be used for. The key_ops
     * parameter is intended for use cases in which public, private, or symmetric keys may be present.
     *
     * Its value is an array of key operation values. Values defined by this specification are:
     *
     * <ul>
     * <li>sign (compute signature or MAC)</li>
     * <li>verify (verify signature or MAC)</li>
     * <li>encrypt (encrypt content)</li>
     * <li>decrypt (decrypt content and validate decryption, if applicable)</li>
     * <li>wrapKey (encrypt key)</li>
     * <li>unwrapKey (decrypt key and validate decryption, if applicable)</li>
     * <li>deriveKey (derive key)</li>
     * <li>deriveBits (derive bits not to be used as a key).</li>
     * </ul>
     *
     * <p>
     * Other values MAY be used. Key operation values can be registered in the IANA JSON Web Key Operations registry defined in
     * Section 8.3. The key operation values are case-sensitive strings. Duplicate key operation values MUST NOT be present in
     * the array.
     *
     * <p>
     * Multiple unrelated key operations SHOULD NOT be specified for a key because of the potential vulnerabilities associated
     * with using the same key with multiple algorithms. Thus, the combinations sign with verify, encrypt with decrypt, and
     * wrapKey with unwrapKey are permitted, but other combinations SHOULD NOT be used.The use and key_ops JWK members SHOULD
     * NOT be used together.
     *
     * @param keyOperations the key operations
     * @return
     */
public JWKBuilder<T, B> keyOperations(String... keyOperations) {
    if (keyOperations.length == 1) {
        keyParameter(KEY_OPERATIONS, keyOperations[0]);
    } else if (keyOperations.length > 1) {
        JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
        for (String operation : keyOperations) {
            arrayBuilder.add(operation);
        }
        this.keyParametersBuilder.add(KEY_OPERATIONS, arrayBuilder);
    }
    return this;
}
Example 28
Project: torodb-master  File: ToroIndexConverter.java View source code
@Override
public String to(NamedToroIndex userObject) {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    objectBuilder.add(NAME_KEY, userObject.getName());
    if (userObject.isUnique()) {
        objectBuilder.add(UNIQUE_KEY, true);
    }
    JsonArrayBuilder attsBuilder = Json.createArrayBuilder();
    JsonArrayBuilder descBuilder = Json.createArrayBuilder();
    int attPosition = 0;
    boolean hasDescending = false;
    for (Map.Entry<AttributeReference, IndexType> entry : userObject.getAttributes().entrySet()) {
        attsBuilder.add(entry.getKey().toString());
        if (IndexType.desc.equals(entry.getValue())) {
            descBuilder.add(attPosition);
            hasDescending = true;
        }
        attPosition++;
    }
    objectBuilder.add(ATTS_KEY, attsBuilder);
    if (hasDescending) {
        objectBuilder.add(DESCENDING, descBuilder);
    }
    StringWriter stringWriter = new StringWriter(200);
    JsonWriter jsonWriter = Json.createWriter(stringWriter);
    jsonWriter.writeObject(objectBuilder.build());
    return stringWriter.toString();
}
Example 29
Project: CoreNLP-master  File: ConstantsAndVariables.java View source code
//  public Map<String, Counter<CandidatePhrase>> getLearnedWords() {
//    return Counters.flatten(learnedWordsEachIter);
//  }
//public void setLearnedWords(Counter<CandidatePhrase> words, String label) {
//  this.learnedWords.put(label, words);
//}
public String getLearnedWordsAsJson() {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    for (String label : getLabels()) {
        Counter<CandidatePhrase> learnedWords = getLearnedWords(label);
        JsonArrayBuilder arr = Json.createArrayBuilder();
        for (CandidatePhrase k : learnedWords.keySet()) arr.add(k.getPhrase());
        obj.add(label, arr);
    }
    return obj.build().toString();
}
Example 30
Project: diirt-master  File: JsonVTypeBuilder.java View source code
public JsonVTypeBuilder addListColumnType(String string, List<Class<?>> ls) {
    JsonArrayBuilder b = Json.createArrayBuilder();
    for (Class<?> element : ls) {
        if (element.equals(String.class)) {
            b.add("String");
        } else if (element.equals(double.class)) {
            b.add("double");
        } else if (element.equals(float.class)) {
            b.add("float");
        } else if (element.equals(long.class)) {
            b.add("long");
        } else if (element.equals(int.class)) {
            b.add("int");
        } else if (element.equals(short.class)) {
            b.add("short");
        } else if (element.equals(byte.class)) {
            b.add("byte");
        } else if (element.equals(Instant.class)) {
            b.add("Timestamp");
        } else {
            throw new IllegalArgumentException("Column type " + element + " not supported");
        }
    }
    add(string, b);
    return this;
}
Example 31
Project: genson-master  File: JSR353Bundle.java View source code
public JsonValue deserArray(ObjectReader reader, Context ctx) {
    JsonArrayBuilder builder = factory.createArrayBuilder();
    reader.beginArray();
    while (reader.hasNext()) {
        com.owlike.genson.stream.ValueType type = reader.next();
        if (com.owlike.genson.stream.ValueType.STRING == type) {
            builder.add(reader.valueAsString());
        } else if (com.owlike.genson.stream.ValueType.BOOLEAN == type) {
            builder.add(reader.valueAsBoolean());
        } else if (com.owlike.genson.stream.ValueType.NULL == type) {
            builder.addNull();
        } else if (com.owlike.genson.stream.ValueType.INTEGER == type) {
            builder.add(reader.valueAsLong());
        } else if (com.owlike.genson.stream.ValueType.DOUBLE == type) {
            builder.add(reader.valueAsDouble());
        } else
            builder.add(deserialize(reader, ctx));
    }
    reader.endArray();
    return builder.build();
}
Example 32
Project: josm-master  File: Preferences.java View source code
@SuppressWarnings("rawtypes")
private static String multiMapToJson(MultiMap map) {
    StringWriter stringWriter = new StringWriter();
    try (JsonWriter writer = Json.createWriter(stringWriter)) {
        JsonObjectBuilder object = Json.createObjectBuilder();
        for (Object o : map.entrySet()) {
            Entry e = (Entry) o;
            Set evalue = (Set) e.getValue();
            JsonArrayBuilder a = Json.createArrayBuilder();
            for (Object evo : evalue) {
                a.add(evo.toString());
            }
            object.add(e.getKey().toString(), a.build());
        }
        writer.writeObject(object.build());
    }
    return stringWriter.toString();
}
Example 33
Project: semiot-platform-master  File: TSDBQueryServiceImpl.java View source code
@Override
public Observable<Response> remove(ResultSet observationsIdRS, ResultSet commandresultsIdRS) {
    JsonArrayBuilder observationsId = Json.createArrayBuilder();
    while (observationsIdRS.hasNext()) {
        QuerySolution qs = observationsIdRS.next();
        Literal system_id = qs.getLiteral("system_id");
        Literal sensor_id = qs.getLiteral("sensor_id");
        JsonObject observationId = Json.createObjectBuilder().add("system_id", system_id.toString()).add("sensor_id", sensor_id.toString()).build();
        observationsId.add(observationId);
    }
    JsonArrayBuilder commandresultsId = Json.createArrayBuilder();
    while (commandresultsIdRS.hasNext()) {
        QuerySolution qs = commandresultsIdRS.next();
        Literal system_id = qs.getLiteral("system_id");
        Literal sensor_id = qs.getLiteral("process_id");
        JsonObject commandresultId = Json.createObjectBuilder().add("system_id", system_id.toString()).add("process_id", sensor_id.toString()).build();
        commandresultsId.add(commandresultId);
    }
    JsonObject json = Json.createObjectBuilder().add("observations", observationsId).add("commandresults", commandresultsId).build();
    return Rx.newClient(RxObservableInvoker.class, mes).target(UriBuilder.fromPath(config.tsdbEndpoint()).path(QUERY_REMOVE)).request().rx().post(Entity.entity(json.toString(), MediaType.TEXT_PLAIN));
}
Example 34
Project: Stanford-NLP-master  File: ConstantsAndVariables.java View source code
//  public Map<String, Counter<CandidatePhrase>> getLearnedWords() {
//    return Counters.flatten(learnedWordsEachIter);
//  }
//public void setLearnedWords(Counter<CandidatePhrase> words, String label) {
//  this.learnedWords.put(label, words);
//}
public String getLearnedWordsAsJson() {
    JsonObjectBuilder obj = Json.createObjectBuilder();
    for (String label : getLabels()) {
        Counter<CandidatePhrase> learnedWords = getLearnedWords(label);
        JsonArrayBuilder arr = Json.createArrayBuilder();
        for (CandidatePhrase k : learnedWords.keySet()) arr.add(k.getPhrase());
        obj.add(label, arr);
    }
    return obj.build().toString();
}
Example 35
Project: swagger4j-master  File: SwaggerGenerator.java View source code
@Override
public void finish() throws IOException {
    if (arrayObjects != null) {
        for (String name : arrayObjects.keySet()) {
            JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
            for (SwaggerJsonGenerator w : arrayObjects.get(name)) {
                w.finish();
                arrayBuilder.add(w.getObjectBuilder());
            }
            builder.add(name, arrayBuilder);
        }
    }
    if (objects != null) {
        for (String name : objects.keySet()) {
            SwaggerJsonGenerator w = objects.get(name);
            w.finish();
            builder.add(name, w.getObjectBuilder());
        }
    }
    if (writer != null) {
        JsonObject jsonObject = builder.build();
        JsonWriterFactory factory = Json.createWriterFactory(Collections.singletonMap(PRETTY_PRINTING, "true"));
        javax.json.JsonWriter jsonWriter = factory.createWriter(writer);
        jsonWriter.writeObject(jsonObject);
        jsonWriter.close();
    }
}
Example 36
Project: wildfly-elytron-master  File: OAuth2TokenSecurityRealmTest.java View source code
@Test
public void testAttributesFromTokenMetadata() throws Exception {
    configureReplayTokenIntrospection();
    TokenSecurityRealm securityRealm = TokenSecurityRealm.builder().validator(OAuth2IntrospectValidator.builder().clientId("wildfly-elytron").clientSecret("dont_tell_me").tokenIntrospectionUrl(new URL("http://as.test.org/oauth2/token/introspect")).build()).build();
    JsonObjectBuilder tokenBuilder = Json.createObjectBuilder();
    tokenBuilder.add("active", true);
    tokenBuilder.add("username", "elytron@jboss.org");
    tokenBuilder.add("attribute1", "value1");
    tokenBuilder.add("attribute2", "value2");
    tokenBuilder.add("attribute3", true);
    tokenBuilder.add("attribute4", false);
    tokenBuilder.add("attribute5", 10);
    JsonArrayBuilder jsonArray = Json.createArrayBuilder();
    jsonArray.add(1).add(2).add(3).add(4);
    tokenBuilder.add("attribute6", jsonArray.build());
    tokenBuilder.add("attribute7", Json.createObjectBuilder().add("objField1", "value1").add("objectField2", "value2"));
    RealmIdentity realmIdentity = securityRealm.getRealmIdentity(new BearerTokenEvidence(tokenBuilder.build().toString()));
    AuthorizationIdentity authorizationIdentity = realmIdentity.getAuthorizationIdentity();
    Attributes attributes = authorizationIdentity.getAttributes();
    assertEquals("value1", attributes.getFirst("attribute1"));
    assertEquals("value2", attributes.getFirst("attribute2"));
    assertEquals("true", attributes.getFirst("attribute3"));
    assertEquals("false", attributes.getFirst("attribute4"));
    assertEquals("10", attributes.getFirst("attribute5"));
    Attributes.Entry attribute6 = attributes.get("attribute6");
    assertEquals(4, attribute6.size());
    assertTrue(attribute6.containsAll(Arrays.asList("1", "2", "3", "4")));
    assertEquals("{\"objField1\":\"value1\",\"objectField2\":\"value2\"}", attributes.getFirst("attribute7"));
}
Example 37
Project: wildfly-security-master  File: OAuth2TokenSecurityRealmTest.java View source code
@Test
public void testAttributesFromTokenMetadata() throws Exception {
    configureReplayTokenIntrospection();
    TokenSecurityRealm securityRealm = TokenSecurityRealm.builder().validator(OAuth2IntrospectValidator.builder().clientId("wildfly-elytron").clientSecret("dont_tell_me").tokenIntrospectionUrl(new URL("http://as.test.org/oauth2/token/introspect")).build()).build();
    JsonObjectBuilder tokenBuilder = Json.createObjectBuilder();
    tokenBuilder.add("active", true);
    tokenBuilder.add("username", "elytron@jboss.org");
    tokenBuilder.add("attribute1", "value1");
    tokenBuilder.add("attribute2", "value2");
    tokenBuilder.add("attribute3", true);
    tokenBuilder.add("attribute4", false);
    tokenBuilder.add("attribute5", 10);
    JsonArrayBuilder jsonArray = Json.createArrayBuilder();
    jsonArray.add(1).add(2).add(3).add(4);
    tokenBuilder.add("attribute6", jsonArray.build());
    tokenBuilder.add("attribute7", Json.createObjectBuilder().add("objField1", "value1").add("objectField2", "value2"));
    RealmIdentity realmIdentity = securityRealm.getRealmIdentity(new BearerTokenEvidence(tokenBuilder.build().toString()));
    AuthorizationIdentity authorizationIdentity = realmIdentity.getAuthorizationIdentity();
    Attributes attributes = authorizationIdentity.getAttributes();
    assertEquals("value1", attributes.getFirst("attribute1"));
    assertEquals("value2", attributes.getFirst("attribute2"));
    assertEquals("true", attributes.getFirst("attribute3"));
    assertEquals("false", attributes.getFirst("attribute4"));
    assertEquals("10", attributes.getFirst("attribute5"));
    Attributes.Entry attribute6 = attributes.get("attribute6");
    assertEquals(4, attribute6.size());
    assertTrue(attribute6.containsAll(Arrays.asList("1", "2", "3", "4")));
    assertEquals("{\"objField1\":\"value1\",\"objectField2\":\"value2\"}", attributes.getFirst("attribute7"));
}
Example 38
Project: mysql_perf_analyzer-master  File: SNMPSettings.java View source code
private boolean writeToJson(Writer writer) {
    javax.json.JsonWriter wr = null;
    try {
        JsonObjectBuilder settingBuilder = Json.createObjectBuilder();
        settingBuilder.add("site", toJson(this.siteSetting));
        JsonArrayBuilder groupBuilder = Json.createArrayBuilder();
        for (Map.Entry<String, SNMPSetting> e : this.groupSettings.entrySet()) {
            JsonObjectBuilder grp = toJson(e.getValue());
            grp.add("dbgroup", e.getKey());
            groupBuilder.add(grp);
        }
        settingBuilder.add("group", groupBuilder);
        JsonArrayBuilder hostBuilder = Json.createArrayBuilder();
        for (Map.Entry<String, SNMPSetting> e : this.hostSettings.entrySet()) {
            JsonObjectBuilder host = toJson(e.getValue());
            host.add("dbhost", e.getKey());
            hostBuilder.add(host);
        }
        settingBuilder.add("host", hostBuilder);
        wr = javax.json.Json.createWriter(writer);
        JsonObject jsonObj = settingBuilder.build();
        wr.writeObject(jsonObj);
        wr.close();
        wr = null;
        return true;
    } catch (Exception ex) {
        logger.log(Level.INFO, "Failed to store snmp json", ex);
    } finally {
        if (wr != null)
            try {
                wr.close();
            } catch (Exception ex) {
            }
    }
    return false;
}
Example 39
Project: OpenDolphin-master  File: PHRProxy.java View source code
private void createConversation(String[] participants, boolean distinct) {
    JsonArrayBuilder arr = Json.createArrayBuilder();
    for (String p : participants) {
        arr.add(p);
    }
    String json = Json.createObjectBuilder().add("participants", arr).add("distinct", distinct).build().toString();
    String path = "/conversations";
    Response response = postEasy(path, session_token, json);
    int status = response.getStatus();
    String entity = response.readEntity(String.class);
    log(status, entity);
    response.close();
    if (status == 401) {
        session_token = authenticationChallenge(entity);
        createConversation(participants, distinct);
    }
}
Example 40
Project: antlr4-master  File: SwiftTarget.java View source code
//added by janyou -->
public String serializeTojson(ATN atn) {
    JsonObjectBuilder builder = Json.createObjectBuilder();
    builder.add("version", ATNDeserializer.SERIALIZED_VERSION);
    builder.add("uuid", ATNDeserializer.SERIALIZED_UUID.toString());
    // convert grammar type to ATN const to avoid dependence on ANTLRParser
    builder.add("grammarType", atn.grammarType.ordinal());
    builder.add("maxTokenType", atn.maxTokenType);
    //states
    int nedges = 0;
    Map<IntervalSet, Integer> setIndices = new HashMap<IntervalSet, Integer>();
    List<IntervalSet> sets = new ArrayList<IntervalSet>();
    JsonArrayBuilder statesBuilder = Json.createArrayBuilder();
    IntegerList nonGreedyStates = new IntegerList();
    IntegerList precedenceStates = new IntegerList();
    for (ATNState s : atn.states) {
        JsonObjectBuilder stateBuilder = Json.createObjectBuilder();
        if (s == null) {
            // might be optimized away
            statesBuilder.addNull();
            continue;
        }
        int stateType = s.getStateType();
        stateBuilder.add("stateType", stateType);
        //stateBuilder.add("stateNumber",s.stateNumber);
        stateBuilder.add("ruleIndex", s.ruleIndex);
        if (s instanceof DecisionState && ((DecisionState) s).nonGreedy) {
            nonGreedyStates.add(s.stateNumber);
        }
        if (s instanceof RuleStartState && ((RuleStartState) s).isLeftRecursiveRule) {
            precedenceStates.add(s.stateNumber);
        }
        if (s.getStateType() == ATNState.LOOP_END) {
            stateBuilder.add("detailStateNumber", ((LoopEndState) s).loopBackState.stateNumber);
        } else if (s instanceof BlockStartState) {
            stateBuilder.add("detailStateNumber", ((BlockStartState) s).endState.stateNumber);
        }
        if (s.getStateType() != ATNState.RULE_STOP) {
            // the deserializer can trivially derive these edges, so there's no need to serialize them
            nedges += s.getNumberOfTransitions();
        }
        for (int i = 0; i < s.getNumberOfTransitions(); i++) {
            Transition t = s.transition(i);
            int edgeType = Transition.serializationTypes.get(t.getClass());
            if (edgeType == Transition.SET || edgeType == Transition.NOT_SET) {
                SetTransition st = (SetTransition) t;
                if (!setIndices.containsKey(st.set)) {
                    sets.add(st.set);
                    setIndices.put(st.set, sets.size() - 1);
                }
            }
        }
        statesBuilder.add(stateBuilder);
    }
    builder.add("states", statesBuilder);
    // non-greedy states
    JsonArrayBuilder nonGreedyStatesBuilder = Json.createArrayBuilder();
    for (int i = 0; i < nonGreedyStates.size(); i++) {
        nonGreedyStatesBuilder.add(nonGreedyStates.get(i));
    }
    builder.add("nonGreedyStates", nonGreedyStatesBuilder);
    // precedence states
    JsonArrayBuilder precedenceStatesBuilder = Json.createArrayBuilder();
    for (int i = 0; i < precedenceStates.size(); i++) {
        precedenceStatesBuilder.add(precedenceStates.get(i));
    }
    builder.add("precedenceStates", precedenceStatesBuilder);
    JsonArrayBuilder ruleToStartStateBuilder = Json.createArrayBuilder();
    int nrules = atn.ruleToStartState.length;
    for (int r = 0; r < nrules; r++) {
        JsonObjectBuilder stateBuilder = Json.createObjectBuilder();
        ATNState ruleStartState = atn.ruleToStartState[r];
        stateBuilder.add("stateNumber", ruleStartState.stateNumber);
        if (atn.grammarType == ATNType.LEXER) {
            //				if (atn.ruleToTokenType[r] == Token.EOF) {
            //					//data.add(Character.MAX_VALUE);
            //					stateBuilder.add("ruleToTokenType",-1);
            //				}
            //				else {
            //					//data.add(atn.ruleToTokenType[r]);
            stateBuilder.add("ruleToTokenType", atn.ruleToTokenType[r]);
        //				}
        }
        ruleToStartStateBuilder.add(stateBuilder);
    }
    builder.add("ruleToStartState", ruleToStartStateBuilder);
    JsonArrayBuilder modeToStartStateBuilder = Json.createArrayBuilder();
    int nmodes = atn.modeToStartState.size();
    if (nmodes > 0) {
        for (ATNState modeStartState : atn.modeToStartState) {
            modeToStartStateBuilder.add(modeStartState.stateNumber);
        }
    }
    builder.add("modeToStartState", modeToStartStateBuilder);
    JsonArrayBuilder nsetsBuilder = Json.createArrayBuilder();
    int nsets = sets.size();
    //data.add(nsets);
    builder.add("nsets", nsets);
    for (IntervalSet set : sets) {
        JsonObjectBuilder setBuilder = Json.createObjectBuilder();
        boolean containsEof = set.contains(Token.EOF);
        if (containsEof && set.getIntervals().get(0).b == Token.EOF) {
            //data.add(set.getIntervals().size() - 1);
            setBuilder.add("size", set.getIntervals().size() - 1);
        } else {
            //data.add(set.getIntervals().size());
            setBuilder.add("size", set.getIntervals().size());
        }
        setBuilder.add("containsEof", containsEof ? 1 : 0);
        JsonArrayBuilder IntervalsBuilder = Json.createArrayBuilder();
        for (Interval I : set.getIntervals()) {
            JsonObjectBuilder IntervalBuilder = Json.createObjectBuilder();
            if (I.a == Token.EOF) {
                if (I.b == Token.EOF) {
                    continue;
                } else {
                    IntervalBuilder.add("a", 0);
                //data.add(0);
                }
            } else {
                IntervalBuilder.add("a", I.a);
            //data.add(I.a);
            }
            IntervalBuilder.add("b", I.b);
            IntervalsBuilder.add(IntervalBuilder);
        }
        setBuilder.add("Intervals", IntervalsBuilder);
        nsetsBuilder.add(setBuilder);
    }
    builder.add("IntervalSet", nsetsBuilder);
    //builder.add("nedges",nedges);
    JsonArrayBuilder allTransitionsBuilder = Json.createArrayBuilder();
    for (ATNState s : atn.states) {
        if (s == null) {
            // might be optimized away
            continue;
        }
        if (s.getStateType() == ATNState.RULE_STOP) {
            continue;
        }
        JsonArrayBuilder transitionsBuilder = Json.createArrayBuilder();
        for (int i = 0; i < s.getNumberOfTransitions(); i++) {
            JsonObjectBuilder transitionBuilder = Json.createObjectBuilder();
            Transition t = s.transition(i);
            if (atn.states.get(t.target.stateNumber) == null) {
                throw new IllegalStateException("Cannot serialize a transition to a removed state.");
            }
            int src = s.stateNumber;
            int trg = t.target.stateNumber;
            int edgeType = Transition.serializationTypes.get(t.getClass());
            int arg1 = 0;
            int arg2 = 0;
            int arg3 = 0;
            switch(edgeType) {
                case Transition.RULE:
                    trg = ((RuleTransition) t).followState.stateNumber;
                    arg1 = ((RuleTransition) t).target.stateNumber;
                    arg2 = ((RuleTransition) t).ruleIndex;
                    arg3 = ((RuleTransition) t).precedence;
                    break;
                case Transition.PRECEDENCE:
                    PrecedencePredicateTransition ppt = (PrecedencePredicateTransition) t;
                    arg1 = ppt.precedence;
                    break;
                case Transition.PREDICATE:
                    PredicateTransition pt = (PredicateTransition) t;
                    arg1 = pt.ruleIndex;
                    arg2 = pt.predIndex;
                    arg3 = pt.isCtxDependent ? 1 : 0;
                    break;
                case Transition.RANGE:
                    arg1 = ((RangeTransition) t).from;
                    arg2 = ((RangeTransition) t).to;
                    if (arg1 == Token.EOF) {
                        arg1 = 0;
                        arg3 = 1;
                    }
                    break;
                case Transition.ATOM:
                    arg1 = ((AtomTransition) t).label;
                    if (arg1 == Token.EOF) {
                        arg1 = 0;
                        arg3 = 1;
                    }
                    break;
                case Transition.ACTION:
                    ActionTransition at = (ActionTransition) t;
                    arg1 = at.ruleIndex;
                    arg2 = at.actionIndex;
                    //						if (arg2 == -1) {
                    //							arg2 = 0xFFFF;
                    //						}
                    arg3 = at.isCtxDependent ? 1 : 0;
                    break;
                case Transition.SET:
                    arg1 = setIndices.get(((SetTransition) t).set);
                    break;
                case Transition.NOT_SET:
                    arg1 = setIndices.get(((SetTransition) t).set);
                    break;
                case Transition.WILDCARD:
                    break;
            }
            transitionBuilder.add("src", src);
            transitionBuilder.add("trg", trg);
            transitionBuilder.add("edgeType", edgeType);
            transitionBuilder.add("arg1", arg1);
            transitionBuilder.add("arg2", arg2);
            transitionBuilder.add("arg3", arg3);
            transitionsBuilder.add(transitionBuilder);
        }
        allTransitionsBuilder.add(transitionsBuilder);
    }
    builder.add("allTransitionsBuilder", allTransitionsBuilder);
    int ndecisions = atn.decisionToState.size();
    //data.add(ndecisions);
    JsonArrayBuilder decisionToStateBuilder = Json.createArrayBuilder();
    for (DecisionState decStartState : atn.decisionToState) {
        //data.add(decStartState.stateNumber);
        decisionToStateBuilder.add(decStartState.stateNumber);
    }
    builder.add("decisionToState", decisionToStateBuilder);
    //
    // LEXER ACTIONS
    //
    JsonArrayBuilder lexerActionsBuilder = Json.createArrayBuilder();
    if (atn.grammarType == ATNType.LEXER) {
        //data.add(atn.lexerActions.length);
        for (LexerAction action : atn.lexerActions) {
            JsonObjectBuilder lexerActionBuilder = Json.createObjectBuilder();
            lexerActionBuilder.add("actionType", action.getActionType().ordinal());
            //data.add(action.getActionType().ordinal());
            switch(action.getActionType()) {
                case CHANNEL:
                    int channel = ((LexerChannelAction) action).getChannel();
                    lexerActionBuilder.add("a", channel);
                    lexerActionBuilder.add("b", 0);
                    break;
                case CUSTOM:
                    int ruleIndex = ((LexerCustomAction) action).getRuleIndex();
                    int actionIndex = ((LexerCustomAction) action).getActionIndex();
                    lexerActionBuilder.add("a", ruleIndex);
                    lexerActionBuilder.add("b", actionIndex);
                    break;
                case MODE:
                    int mode = ((LexerModeAction) action).getMode();
                    lexerActionBuilder.add("a", mode);
                    lexerActionBuilder.add("b", 0);
                    break;
                case MORE:
                    lexerActionBuilder.add("a", 0);
                    lexerActionBuilder.add("b", 0);
                    break;
                case POP_MODE:
                    lexerActionBuilder.add("a", 0);
                    lexerActionBuilder.add("b", 0);
                    break;
                case PUSH_MODE:
                    mode = ((LexerPushModeAction) action).getMode();
                    lexerActionBuilder.add("a", mode);
                    lexerActionBuilder.add("b", 0);
                    break;
                case SKIP:
                    lexerActionBuilder.add("a", 0);
                    lexerActionBuilder.add("b", 0);
                    break;
                case TYPE:
                    int type = ((LexerTypeAction) action).getType();
                    lexerActionBuilder.add("a", type);
                    lexerActionBuilder.add("b", 0);
                    break;
                default:
                    String message = String.format(Locale.getDefault(), "The specified lexer action type %s is not valid.", action.getActionType());
                    throw new IllegalArgumentException(message);
            }
            lexerActionsBuilder.add(lexerActionBuilder);
        }
    }
    builder.add("lexerActions", lexerActionsBuilder);
    // don't adjust the first value since that's the version number
    //		for (int i = 1; i < data.size(); i++) {
    //			if (data.get(i) < Character.MIN_VALUE || data.get(i) > Character.MAX_VALUE) {
    //				throw new UnsupportedOperationException("Serialized ATN data element out of range.");
    //			}
    //
    //			int value = (data.get(i) + 2) & 0xFFFF;
    //			data.set(i, value);
    //		}
    JsonObject data = builder.build();
    //  System.out.print(data.toString());
    return data.toString();
}
Example 41
Project: jcypher-master  File: AbstractEmbeddedDBAccess.java View source code
private void initJsonBuilderContext(JsonBuilderContext builderContext) {
    builderContext.resultObject = Json.createObjectBuilder();
    builderContext.resultsArray = Json.createArrayBuilder();
    builderContext.errorsArray = Json.createArrayBuilder();
    builderContext.innerResultsObjects = new ArrayList<JsonObjectBuilder>();
    builderContext.dataArrays = new ArrayList<JsonArrayBuilder>();
}
Example 42
Project: stan-cn-com-master  File: GetPatternsFromDataMultiClass.java View source code
@SuppressWarnings({ "unchecked" })
public Counter<SurfacePattern> getPatterns(String label, Set<SurfacePattern> alreadyIdentifiedPatterns, SurfacePattern p0, Counter<String> p0Set, Set<SurfacePattern> ignorePatterns) throws InterruptedException, ExecutionException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    TwoDimensionalCounter<SurfacePattern, String> patternsandWords4Label = new TwoDimensionalCounter<SurfacePattern, String>();
    TwoDimensionalCounter<SurfacePattern, String> negPatternsandWords4Label = new TwoDimensionalCounter<SurfacePattern, String>();
    TwoDimensionalCounter<SurfacePattern, String> posnegPatternsandWords4Label = new TwoDimensionalCounter<SurfacePattern, String>();
    TwoDimensionalCounter<SurfacePattern, String> unLabeledPatternsandWords4Label = new TwoDimensionalCounter<SurfacePattern, String>();
    TwoDimensionalCounter<SurfacePattern, String> negandUnLabeledPatternsandWords4Label = new TwoDimensionalCounter<SurfacePattern, String>();
    TwoDimensionalCounter<SurfacePattern, String> allPatternsandWords4Label = new TwoDimensionalCounter<SurfacePattern, String>();
    if (!constVars.batchProcessSents) {
        // if not batch processing
        if (this.patternsForEachToken == null) {
            // if patterns for each token null
            if (constVars.computeAllPatterns) {
                Redwood.log(Redwood.DBG, "Computing all patterns");
                this.patternsForEachToken = createPats.getAllPatterns(label, Data.sents);
                constVars.computeAllPatterns = false;
            } else {
                // read from the saved file
                this.patternsForEachToken = IOUtils.readObjectFromFile(constVars.allPatternsFile);
                Redwood.log(ConstantsAndVariables.minimaldebug, "Read all patterns from " + constVars.allPatternsFile);
            }
        }
        this.calculateSufficientStats(Data.sents, patternsForEachToken, label, patternsandWords4Label, posnegPatternsandWords4Label, allPatternsandWords4Label, negPatternsandWords4Label, unLabeledPatternsandWords4Label, negandUnLabeledPatternsandWords4Label);
    } else // batch processing sentences
    {
        for (File f : Data.sentsFiles) {
            Redwood.log(Redwood.DBG, (constVars.computeAllPatterns ? "Creating patterns and " : "") + "calculating sufficient statistics from " + f);
            Map<String, List<CoreLabel>> sents = IOUtils.readObjectFromFile(f);
            Map<String, Map<Integer, Triple<Set<SurfacePattern>, Set<SurfacePattern>, Set<SurfacePattern>>>> pats4File = null;
            if (constVars.computeAllPatterns) {
                if (this.patternsForEachToken == null)
                    this.patternsForEachToken = new HashMap<String, Map<Integer, Triple<Set<SurfacePattern>, Set<SurfacePattern>, Set<SurfacePattern>>>>();
                pats4File = createPats.getAllPatterns(label, sents);
                this.patternsForEachToken.putAll(pats4File);
            } else {
                if (this.patternsForEachToken == null) {
                    // read only for the first time
                    this.patternsForEachToken = IOUtils.readObjectFromFile(constVars.allPatternsFile);
                    Redwood.log(ConstantsAndVariables.minimaldebug, "Read all patterns from " + constVars.allPatternsFile);
                }
                pats4File = this.patternsForEachToken;
            }
            this.calculateSufficientStats(sents, pats4File, label, patternsandWords4Label, posnegPatternsandWords4Label, allPatternsandWords4Label, negPatternsandWords4Label, unLabeledPatternsandWords4Label, negandUnLabeledPatternsandWords4Label);
        }
    }
    if (constVars.computeAllPatterns && constVars.allPatternsFile != null) {
        IOUtils.writeObjectToFile(this.patternsForEachToken, constVars.allPatternsFile);
    }
    if (patternsandWords == null)
        patternsandWords = new HashMap<String, TwoDimensionalCounter<SurfacePattern, String>>();
    if (allPatternsandWords == null)
        allPatternsandWords = new HashMap<String, TwoDimensionalCounter<SurfacePattern, String>>();
    if (currentPatternWeights == null)
        currentPatternWeights = new HashMap<String, Counter<SurfacePattern>>();
    Counter<SurfacePattern> currentPatternWeights4Label = new ClassicCounter<SurfacePattern>();
    Set<SurfacePattern> removePats = enforceMinSupportRequirements(patternsandWords4Label, unLabeledPatternsandWords4Label);
    Counters.removeKeys(patternsandWords4Label, removePats);
    Counters.removeKeys(unLabeledPatternsandWords4Label, removePats);
    Counters.removeKeys(negandUnLabeledPatternsandWords4Label, removePats);
    Counters.removeKeys(allPatternsandWords4Label, removePats);
    Counters.removeKeys(posnegPatternsandWords4Label, removePats);
    Counters.removeKeys(negPatternsandWords4Label, removePats);
    // Redwood.log(ConstantsAndVariables.extremedebug,
    // "Patterns around positive words in the label " + label + " are " +
    // patternsandWords4Label);
    ScorePatterns scorePatterns;
    Class<?> patternscoringclass = getPatternScoringClass(constVars.patternScoring);
    if (patternscoringclass != null && patternscoringclass.equals(ScorePatternsF1.class)) {
        scorePatterns = new ScorePatternsF1(constVars, constVars.patternScoring, label, patternsandWords4Label, negPatternsandWords4Label, unLabeledPatternsandWords4Label, negandUnLabeledPatternsandWords4Label, allPatternsandWords4Label, props, p0Set, p0);
        Counter<SurfacePattern> finalPat = scorePatterns.score();
        Counters.removeKeys(finalPat, alreadyIdentifiedPatterns);
        Counters.retainNonZeros(finalPat);
        Counters.retainTop(finalPat, 1);
        if (Double.isNaN(Counters.max(finalPat)))
            throw new RuntimeException("how is the value NaN");
        Redwood.log(ConstantsAndVariables.minimaldebug, "Selected Pattern: " + finalPat);
        return finalPat;
    } else if (patternscoringclass != null && patternscoringclass.equals(ScorePatternsRatioModifiedFreq.class)) {
        scorePatterns = new ScorePatternsRatioModifiedFreq(constVars, constVars.patternScoring, label, patternsandWords4Label, negPatternsandWords4Label, unLabeledPatternsandWords4Label, negandUnLabeledPatternsandWords4Label, allPatternsandWords4Label, phInPatScoresCache, scorePhrases, props);
    } else if (patternscoringclass != null && patternscoringclass.equals(ScorePatternsFreqBased.class)) {
        scorePatterns = new ScorePatternsFreqBased(constVars, constVars.patternScoring, label, patternsandWords4Label, negPatternsandWords4Label, unLabeledPatternsandWords4Label, negandUnLabeledPatternsandWords4Label, allPatternsandWords4Label, props);
    } else if (constVars.patternScoring.equals(PatternScoring.kNN)) {
        try {
            Class<? extends ScorePatterns> clazz = (Class<? extends ScorePatterns>) Class.forName("edu.stanford.nlp.patterns.surface.ScorePatternsKNN");
            Constructor<? extends ScorePatterns> ctor = clazz.getConstructor(ConstantsAndVariables.class, PatternScoring.class, String.class, TwoDimensionalCounter.class, TwoDimensionalCounter.class, TwoDimensionalCounter.class, TwoDimensionalCounter.class, TwoDimensionalCounter.class, ScorePhrases.class, Properties.class);
            scorePatterns = ctor.newInstance(constVars, constVars.patternScoring, label, patternsandWords4Label, negPatternsandWords4Label, unLabeledPatternsandWords4Label, negandUnLabeledPatternsandWords4Label, allPatternsandWords4Label, scorePhrases, props);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("kNN pattern scoring is not released yet. Stay tuned.");
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("newinstance of kNN not created", e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException("newinstance of kNN not created", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("newinstance of kNN not created", e);
        } catch (InstantiationException e) {
            throw new RuntimeException("newinstance of kNN not created", e);
        }
    } else {
        throw new RuntimeException(constVars.patternScoring + " is not implemented (check spelling?). ");
    }
    scorePatterns.setUp(props);
    currentPatternWeights4Label = scorePatterns.score();
    Redwood.log(ConstantsAndVariables.extremedebug, "patterns counter size is " + currentPatternWeights4Label.size());
    if (ignorePatterns != null && !ignorePatterns.isEmpty()) {
        Counters.removeKeys(currentPatternWeights4Label, ignorePatterns);
        Redwood.log(ConstantsAndVariables.extremedebug, "Removing patterns from ignorePatterns of size  " + ignorePatterns.size() + ". New patterns size " + currentPatternWeights4Label.size());
    }
    if (alreadyIdentifiedPatterns != null && !alreadyIdentifiedPatterns.isEmpty()) {
        Counters.removeKeys(currentPatternWeights4Label, alreadyIdentifiedPatterns);
        Redwood.log(ConstantsAndVariables.extremedebug, "Removing already identified patterns of size  " + alreadyIdentifiedPatterns.size() + ". New patterns size " + currentPatternWeights4Label.size());
    }
    PriorityQueue<SurfacePattern> q = Counters.toPriorityQueue(currentPatternWeights4Label);
    int num = 0;
    Counter<SurfacePattern> chosenPat = new ClassicCounter<SurfacePattern>();
    Set<SurfacePattern> removePatterns = new HashSet<SurfacePattern>();
    Set<SurfacePattern> removeIdentifiedPatterns = null;
    while (num < constVars.numPatterns && !q.isEmpty()) {
        SurfacePattern pat = q.removeFirst();
        if (currentPatternWeights4Label.getCount(pat) < constVars.thresholdSelectPattern) {
            Redwood.log(Redwood.DBG, "The max weight of candidate patterns is " + df.format(currentPatternWeights4Label.getCount(pat)) + " so not adding anymore patterns");
            break;
        }
        boolean notchoose = false;
        if (!unLabeledPatternsandWords4Label.containsFirstKey(pat) || unLabeledPatternsandWords4Label.getCounter(pat).isEmpty()) {
            Redwood.log(ConstantsAndVariables.extremedebug, "Removing pattern " + pat + " because it has no unlab support; pos words: " + patternsandWords4Label.getCounter(pat) + " and all words " + allPatternsandWords4Label.getCounter(pat));
            notchoose = true;
            continue;
        }
        Set<SurfacePattern> removeChosenPats = null;
        if (!notchoose) {
            if (alreadyIdentifiedPatterns != null) {
                for (SurfacePattern p : alreadyIdentifiedPatterns) {
                    if (SurfacePattern.subsumes(pat, p)) {
                        // if (pat.getNextContextStr().contains(p.getNextContextStr()) &&
                        // pat.getPrevContextStr().contains(p.getPrevContextStr())) {
                        Redwood.log(ConstantsAndVariables.extremedebug, "Not choosing pattern " + pat + " because it is contained in or contains the already chosen pattern " + p);
                        notchoose = true;
                        break;
                    }
                    int rest = pat.equalContext(p);
                    // the contexts dont match
                    if (rest == Integer.MAX_VALUE)
                        continue;
                    // if pat is less restrictive, remove p and add pat!
                    if (rest < 0) {
                        if (removeIdentifiedPatterns == null)
                            removeIdentifiedPatterns = new HashSet<SurfacePattern>();
                        removeIdentifiedPatterns.add(p);
                    } else {
                        notchoose = true;
                        break;
                    }
                }
            }
        }
        // In this iteration:
        if (!notchoose) {
            for (SurfacePattern p : chosenPat.keySet()) {
                boolean removeChosenPatFlag = false;
                if (SurfacePattern.sameGenre(pat, p)) {
                    if (SurfacePattern.subsumes(pat, p)) {
                        Redwood.log(ConstantsAndVariables.extremedebug, "Not choosing pattern " + pat + " because it is contained in or contains the already chosen pattern " + p);
                        notchoose = true;
                        break;
                    } else if (SurfacePattern.subsumes(p, pat)) {
                        //subsume is true even if equal context
                        //check if equal context
                        int rest = pat.equalContext(p);
                        // the contexts do not match
                        if (rest == Integer.MAX_VALUE) {
                            Redwood.log(ConstantsAndVariables.extremedebug, "Not choosing pattern " + p + " because it is contained in or contains another chosen pattern in this iteration " + pat);
                            removeChosenPatFlag = true;
                        } else // add pat!
                        if (rest < 0) {
                            removeChosenPatFlag = true;
                        } else {
                            notchoose = true;
                            break;
                        }
                    }
                    if (removeChosenPatFlag) {
                        if (removeChosenPats == null)
                            removeChosenPats = new HashSet<SurfacePattern>();
                        removeChosenPats.add(p);
                        num--;
                    }
                }
            }
        }
        if (notchoose) {
            Redwood.log(Redwood.DBG, "Not choosing " + pat + " for whatever reason!");
            continue;
        }
        if (removeChosenPats != null) {
            Redwood.log(ConstantsAndVariables.extremedebug, "Removing already chosen patterns in this iteration " + removeChosenPats + " in favor of " + pat);
            Counters.removeKeys(chosenPat, removeChosenPats);
        }
        if (removeIdentifiedPatterns != null) {
            Redwood.log(ConstantsAndVariables.extremedebug, "Removing already identified patterns " + removeIdentifiedPatterns + " in favor of " + pat);
            removePatterns.addAll(removeIdentifiedPatterns);
        }
        chosenPat.setCount(pat, currentPatternWeights4Label.getCount(pat));
        num++;
    }
    this.removeLearnedPatterns(label, removePatterns);
    Redwood.log(Redwood.DBG, "final size of the patterns is " + chosenPat.size());
    Redwood.log(ConstantsAndVariables.minimaldebug, "## Selected Patterns ## \n");
    List<Pair<SurfacePattern, Double>> chosenPatSorted = Counters.toSortedListWithCounts(chosenPat);
    for (Pair<SurfacePattern, Double> en : chosenPatSorted) Redwood.log(ConstantsAndVariables.minimaldebug, en.first().toStringToWrite() + ":" + df.format(en.second) + "\n");
    if (constVars.outDir != null && !constVars.outDir.isEmpty()) {
        CollectionValuedMap<SurfacePattern, String> posWords = new CollectionValuedMap<SurfacePattern, String>();
        for (Entry<SurfacePattern, ClassicCounter<String>> en : patternsandWords4Label.entrySet()) {
            posWords.addAll(en.getKey(), en.getValue().keySet());
        }
        CollectionValuedMap<SurfacePattern, String> negWords = new CollectionValuedMap<SurfacePattern, String>();
        for (Entry<SurfacePattern, ClassicCounter<String>> en : negPatternsandWords4Label.entrySet()) {
            negWords.addAll(en.getKey(), en.getValue().keySet());
        }
        CollectionValuedMap<SurfacePattern, String> unlabWords = new CollectionValuedMap<SurfacePattern, String>();
        for (Entry<SurfacePattern, ClassicCounter<String>> en : unLabeledPatternsandWords4Label.entrySet()) {
            unlabWords.addAll(en.getKey(), en.getValue().keySet());
        }
        String outputdir = constVars.outDir + "/" + constVars.identifier + "/" + label;
        Redwood.log(ConstantsAndVariables.minimaldebug, "Saving output in " + outputdir);
        IOUtils.ensureDir(new File(outputdir));
        String filename = outputdir + "/patterns" + ".json";
        JsonArrayBuilder obj = Json.createArrayBuilder();
        if (writtenPatInJustification.containsKey(label) && writtenPatInJustification.get(label)) {
            JsonReader jsonReader = Json.createReader(new BufferedInputStream(new FileInputStream(filename)));
            JsonArray objarr = jsonReader.readArray();
            jsonReader.close();
            for (JsonValue o : objarr) obj.add(o);
        } else
            obj = Json.createArrayBuilder();
        JsonObjectBuilder objThisIter = Json.createObjectBuilder();
        for (Pair<SurfacePattern, Double> pat : chosenPatSorted) {
            JsonObjectBuilder o = Json.createObjectBuilder();
            JsonArrayBuilder pos = Json.createArrayBuilder();
            JsonArrayBuilder neg = Json.createArrayBuilder();
            JsonArrayBuilder unlab = Json.createArrayBuilder();
            for (String w : posWords.get(pat.first())) pos.add(w);
            for (String w : negWords.get(pat.first())) neg.add(w);
            for (String w : unlabWords.get(pat.first())) unlab.add(w);
            o.add("Positive", pos);
            o.add("Negative", neg);
            o.add("Unlabeled", unlab);
            o.add("Score", pat.second());
            objThisIter.add(pat.first().toStringSimple(), o);
        }
        obj.add(objThisIter.build());
        IOUtils.ensureDir(new File(filename).getParentFile());
        IOUtils.writeStringToFile(obj.build().toString(), filename, "utf8");
        writtenPatInJustification.put(label, true);
    }
    if (constVars.justify) {
        Redwood.log(Redwood.DBG, "Justification for Patterns:");
        for (SurfacePattern key : chosenPat.keySet()) {
            Redwood.log(Redwood.DBG, "\nPattern: " + key.toStringToWrite());
            Redwood.log(Redwood.DBG, "Positive Words:" + Counters.toSortedString(patternsandWords4Label.getCounter(key), patternsandWords4Label.getCounter(key).size(), "%1$s:%2$f", ";"));
            Redwood.log(Redwood.DBG, "Negative Words:" + Counters.toSortedString(negPatternsandWords4Label.getCounter(key), negPatternsandWords4Label.getCounter(key).size(), "%1$s:%2$f", ";"));
            Redwood.log(Redwood.DBG, "Unlabeled Words:" + Counters.toSortedString(unLabeledPatternsandWords4Label.getCounter(key), unLabeledPatternsandWords4Label.getCounter(key).size(), "%1$s:%2$f", ";"));
        }
    }
    allPatternsandWords.put(label, allPatternsandWords4Label);
    patternsandWords.put(label, patternsandWords4Label);
    currentPatternWeights.put(label, currentPatternWeights4Label);
    return chosenPat;
}
Example 43
Project: tool.lars-master  File: DataModelSerializer.java View source code
/**
     * Use reflection to look inside Object to find fields that should be serialized into the JSON.
     * The fields are collected into an appropriate JsonStructure (either JsonObject, or JsonArray), and
     * returned for aggregation before serialization.
     *
     * @param o the Object to query, if o is an instance of Collection, then a JsonArray will be returned.
     * @return JSONArtrifactPair built from POJO.
     */
@SuppressWarnings("unchecked")
private static JSONArtrifactPair findFieldsToSerialize(Object o) {
    // thing in the List.
    if (o instanceof Collection) {
        Collection<? extends Object> listOfO = (Collection<? extends Object>) o;
        JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
        for (Object listObject : listOfO) {
            //currently, we support lists of strings, or lists of POJOs.
            if (listObject instanceof String) {
                arrayBuilder.add((String) listObject);
            } else if (listObject.getClass().getName().startsWith(DATA_MODEL_PACKAGE)) {
                arrayBuilder.add(findFieldsToSerialize(listObject).mainObject);
            } else {
                throw new IllegalStateException("Data Model Error: serialization only supported for Collections of String, or other Data Model elements");
            }
        }
        JsonArray result = arrayBuilder.build();
        return new JSONArtrifactPair(result, null);
    }
    // object wasn't a collection.. better see what we can do with it.
    JsonObjectBuilder mainObjectBuilder = Json.createObjectBuilder();
    SortedMap<String, Method> gettersFromO = new TreeMap<String, Method>();
    Class<? extends Object> classOfO = o.getClass();
    // See if we have any breaking changes that need to go into a separate object
    JsonObjectBuilder incompatibleFieldsObjectBuilder = null;
    Collection<String> fieldsToPutInIncompatibleObject = null;
    boolean haveIncompatibleFields = false;
    if (HasBreakingChanges.class.isAssignableFrom(classOfO)) {
        fieldsToPutInIncompatibleObject = ((HasBreakingChanges) o).attributesThatCauseBreakingChanges();
        if (!fieldsToPutInIncompatibleObject.isEmpty()) {
            incompatibleFieldsObjectBuilder = Json.createObjectBuilder();
            haveIncompatibleFields = true;
        }
    }
    for (Method methodFromO : classOfO.getMethods()) {
        if (methodFromO.getName().startsWith("get") && methodFromO.getName().length() > 3 && methodFromO.getParameterTypes().length == 0) {
            Method old;
            if ((old = gettersFromO.put(methodFromO.getName(), methodFromO)) != null) {
                throw new IllegalStateException("Data Model Error: duplicate getter for " + methodFromO + "(" + old + ") on " + o.getClass().getName());
            }
        }
    }
    for (Map.Entry<String, Method> entry : gettersFromO.entrySet()) {
        String getterName = entry.getKey();
        //not all getters are really for us ;p
        if ("getClass".equals(getterName)) {
            continue;
        }
        // If the field is marked as JSONIgnore then ignore it
        if (entry.getValue().isAnnotationPresent(JSONIgnore.class)) {
            continue;
        }
        String nameOfField = new StringBuilder().append(getterName.substring(3, 4).toLowerCase()).append(getterName.substring(4)).toString();
        if (haveIncompatibleFields && fieldsToPutInIncompatibleObject.contains(nameOfField)) {
            addFieldToJsonObject(entry.getValue(), o, nameOfField, incompatibleFieldsObjectBuilder);
        } else {
            addFieldToJsonObject(entry.getValue(), o, nameOfField, mainObjectBuilder);
        }
    }
    JsonObject mainObject = mainObjectBuilder.build();
    JsonObject incompatibleFieldsObject = null;
    if (incompatibleFieldsObjectBuilder != null) {
        incompatibleFieldsObject = incompatibleFieldsObjectBuilder.build();
    }
    return new JSONArtrifactPair(mainObject, incompatibleFieldsObject);
}
Example 44
Project: btc4j-daemon-master  File: BtcDaemon.java View source code
@Override
public String addMultiSignatureAddress(long required, List<String> keys, String account) throws BtcException {
    if (required < 1) {
        required = 1;
    }
    if (keys == null) {
        keys = new ArrayList<String>();
    }
    if (required > keys.size()) {
        required = keys.size();
    }
    JsonArrayBuilder keysParam = Json.createArrayBuilder();
    for (String key : keys) {
        keysParam.add(BtcUtil.notNull(key));
    }
    JsonArray parameters = Json.createArrayBuilder().add(required).add(keysParam).add(BtcUtil.notNull(account)).build();
    return jsonString(invoke(BTCAPI_ADD_MULTISIGNATURE_ADDRESS, parameters));
}
Example 45
Project: Resteasy-master  File: NumbersResource.java View source code
@GET
@Produces("application/json")
public JsonArray numbers() {
    JsonArrayBuilder array = Json.createArrayBuilder();
    Stream<String> numberStream = Stream.generate(System::currentTimeMillis).map(String::valueOf).limit(10);
    numberStream.forEach(array::add);
    return array.build();
}
Example 46
Project: 101simplejava-master  File: Cut.java View source code
/**
	 * @param arr
	 *            input JSON array
	 * @return output JSON array
	 */
public static JsonArray cut(JsonArray arr) {
    JsonArrayBuilder builder = Json.createArrayBuilder();
    for (JsonValue item : arr) cut(builder, item);
    return builder.build();
}
Example 47
Project: e2ftp-master  File: HooksResource.java View source code
@GET
public JsonArray all() {
    JsonArrayBuilder builder = Json.createArrayBuilder();
    List<Hook> allHooks = hr.all();
    for (Hook hook : allHooks) {
        builder.add(convert(hook));
    }
    return builder.build();
}
Example 48
Project: hawkular-metrics-master  File: HawkularJson.java View source code
private static <T> JsonArray metricsJson(Long timestamp, Map<String, T> metricsPoints, BiFunction<Long, T, JsonObject> bf) {
    final JsonArrayBuilder builder = Json.createArrayBuilder();
    metricsPoints.entrySet().stream().map( e -> metricJson(e.getKey(), bf.apply(timestamp, e.getValue()))).forEach(builder::add);
    return builder.build();
}
Example 49
Project: javaee-bce-pom-master  File: Registrations.java View source code
public JsonArray allAsJson() {
    Collector<JsonObject, ?, JsonArrayBuilder> jsonCollector = Collector.of(Json::createArrayBuilder, JsonArrayBuilder::add, ( left,  right) -> {
        left.add(right);
        return left;
    });
    return all().stream().map(this::convert).collect(jsonCollector).build();
}
Example 50
Project: geronimo-specs-master  File: JsonProviderTest.java View source code
@Override
public JsonArrayBuilder createArrayBuilder() {
    return null;
}