use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.
the class GraphBuilder method complete.
@Override
public JSONObject complete() {
JSONObject json = json();
JSONArray oa = new JSONArray(ops.size());
for (BOperator op : ops) {
oa.add(op.complete());
}
json.put("operators", oa);
return json;
}
use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.
the class DependencyResolver method resolveDependencies.
/**
* Resolve the dependencies.
* Creates entries in the graph config that will
* result in files being copied into the toolkit.
*/
public void resolveDependencies() throws IOException, URISyntaxException {
JSONObject graphConfig = topology.builder().getConfig();
JSONArray includes = (JSONArray) graphConfig.get("includes");
if (includes == null)
graphConfig.put("includes", includes = new JSONArray());
for (BOperatorInvocation op : operatorToJarDependencies.keySet()) {
ArrayList<String> jars = new ArrayList<String>();
for (Path source : operatorToJarDependencies.get(op)) {
String jarName = resolveDependency(source, includes);
jars.add("impl/lib/" + jarName);
}
String[] jarPaths = jars.toArray(new String[jars.size()]);
op.setParameter("jar", jarPaths);
}
ArrayList<String> jars = new ArrayList<String>();
for (Path source : globalDependencies) {
String jarName = resolveDependency(source, includes);
jars.add("impl/lib/" + jarName);
}
List<BOperator> ops = topology.builder().getOps();
if (jars.size() != 0) {
for (BOperator op : ops) {
if (op instanceof BOperatorInvocation) {
BOperatorInvocation bop = (BOperatorInvocation) op;
if (Functional.class.isAssignableFrom(bop.op().getOperatorClass())) {
JSONObject params = (JSONObject) bop.json().get("parameters");
JSONObject op_jars = (JSONObject) params.get("jar");
if (null == op_jars) {
JSONObject val = new JSONObject();
val.put("value", new JSONArray());
params.put("jar", val);
op_jars = val;
}
JSONArray value = (JSONArray) op_jars.get("value");
for (String jar : jars) {
value.add(jar);
}
}
}
}
}
for (Artifact dep : globalFileDependencies) resolveFileDependency(dep, includes);
}
use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.
the class WindowTest method testContinuousAggregateLastSeconds.
/**
* Test a continuous aggregation.
*/
@Test
public void testContinuousAggregateLastSeconds() throws Exception {
final Topology t = newTopology();
TStream<String> source = t.periodicSource(new PeriodicStrings(), 100, TimeUnit.MILLISECONDS);
TStream<JSONObject> aggregate = source.last(3, TimeUnit.SECONDS).aggregate(new AggregateStrings());
TStream<String> strings = JSONStreams.serialize(aggregate);
Tester tester = t.getTester();
Condition<List<String>> contents = tester.stringContents(strings);
// 10 tuples per second, each is aggregated, so 15 seconds is around 150 tuples.
Condition<Long> ending = tester.atLeastTupleCount(strings, 150);
complete(tester, ending, 30, TimeUnit.SECONDS);
assertTrue(ending.valid());
long startTs = 0;
for (String output : contents.getResult()) {
JSONObject agg = JSONObject.parse(output);
JSONArray items = (JSONArray) agg.get("items");
long ts = (Long) agg.get("ts");
// Should see around 30 tuples per window, once we
// pass the first three seconds.
assertTrue("Number of tuples in window:" + items.size(), items.size() <= 45);
if (agg.containsKey("delta")) {
long delta = (Long) agg.get("delta");
assertTrue(delta >= 0);
if (startTs == 0) {
startTs = ts;
} else {
long diff = ts - startTs;
if (diff > 3000)
assertTrue("Number of tuples in window:" + items.size(), items.size() >= 25);
}
}
}
}
use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method testDeserializeArray.
/**
* Test that if the serialized value is
* an array, it ends up wrapped in an object.
*/
@Test
public void testDeserializeArray() throws Exception {
final String data = "[ 100, 500, false, 200, 400 ]";
final Topology t = new Topology();
TStream<String> array = t.strings(data);
TStream<JSONObject> json = JSONStreams.deserialize(array);
TStream<String> jsonString = JSONStreams.serialize(json);
JSONArray ja = (JSONArray) JSON.parse(data);
JSONObject jo = new JSONObject();
jo.put("payload", ja);
checkJsonOutput(jo, jsonString);
}
use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method testFlatten.
@Test
public void testFlatten() throws Exception {
final Topology t = new Topology();
final JSONObject value = new JSONObject();
final JSONArray array = new JSONArray();
JSONObject e1 = new JSONObject();
e1.put("val", "hello");
array.add(e1);
JSONObject e2 = new JSONObject();
e2.put("val", "goodbye");
array.add(e2);
JSONObject e3 = new JSONObject();
e3.put("val", "farewell");
array.add(e3);
value.put("greetings", array);
List<JSONObject> inputs = new ArrayList<>();
inputs.add(value);
// no list present
inputs.add(new JSONObject());
JSONObject emptyList = new JSONObject();
emptyList.put("greetings", new JSONArray());
inputs.add(emptyList);
TStream<JSONObject> s = t.constants(inputs);
TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
TStream<String> output = JSONStreams.serialize(jsonm);
completeAndValidate(output, 10, e1.toString(), e2.toString(), e3.toString());
}
Aggregations