use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.
the class SubscribeSPLDictTest method testSubscribeSPLDictMap.
@Test
public void testSubscribeSPLDictMap() throws Exception {
Topology topology = new Topology("testSubscribeMap");
// Publish a stream with all the SPL types supported by Python including sets
SPLStream tuples = PythonFunctionalOperatorsTest.testTupleStream(topology, true);
tuples = addStartupDelay(tuples);
tuples.publish("pytest/spl/map");
SPLStream viaSPL = SPL.invokeOperator("spl.relational::Functor", tuples, tuples.getSchema(), null);
// Python that subscribes to the SPL tuple stream and then republishes as Json.
includePythonApp(topology, "spl_map_json.py", "spl_map_json::spl_map_json");
TStream<JSONObject> viaPythonJson = topology.subscribe("pytest/spl/map/result", JSONObject.class);
SPLStream viaPythonJsonSpl = JSONStreams.toSPL(viaPythonJson.isolate());
Tester tester = topology.getTester();
Condition<Long> expectedCount = tester.tupleCount(viaPythonJsonSpl, TUPLE_COUNT);
Condition<Long> expectedCountSpl = tester.tupleCount(viaSPL, TUPLE_COUNT);
Condition<List<Tuple>> viaSPLResult = tester.tupleContents(viaSPL);
Condition<List<Tuple>> viaPythonResult = tester.tupleContents(viaPythonJsonSpl);
complete(tester, allConditions(expectedCount, expectedCountSpl), 60, TimeUnit.SECONDS);
System.out.println(expectedCount.getResult());
System.out.println(expectedCountSpl.getResult());
assertTrue(expectedCount.valid());
assertTrue(expectedCountSpl.valid());
List<Tuple> splResults = viaSPLResult.getResult();
List<Tuple> pyJsonResults = viaPythonResult.getResult();
assertEquals(TUPLE_COUNT, splResults.size());
assertEquals(TUPLE_COUNT, pyJsonResults.size());
for (int i = 0; i < TUPLE_COUNT; i++) {
Tuple spl = splResults.get(i);
JSONObject json = (JSONObject) JSON.parse(pyJsonResults.get(i).getString("jsonString"));
System.out.println(spl);
System.out.println(pyJsonResults.get(i).getString("jsonString"));
assertEquals(spl.getBoolean("b"), ((Boolean) json.get("b")).booleanValue());
assertEquals(spl.getInt("i8"), ((Number) json.get("i8")).intValue());
assertEquals(spl.getInt("i16"), ((Number) json.get("i16")).intValue());
assertEquals(spl.getInt("i32"), ((Number) json.get("i32")).intValue());
assertEquals(spl.getLong("i64"), ((Number) json.get("i64")).longValue());
assertEquals(spl.getString("r"), json.get("r").toString());
assertEquals(spl.getDouble("f32"), ((Number) json.get("f32")).doubleValue(), 0.1);
assertEquals(spl.getDouble("f64"), ((Number) json.get("f64")).doubleValue(), 0.1);
{
List<?> ex = spl.getList("li32");
JSONArray pya = (JSONArray) json.get("li32");
assertEquals(ex.size(), pya.size());
for (int j = 0; j < ex.size(); j++) {
assertEquals(ex.get(j), ((Number) pya.get(j)).intValue());
}
}
{
Set<?> ex = spl.getSet("si32");
JSONArray pya = (JSONArray) json.get("si32");
assertEquals(ex.size(), pya.size());
setAssertHelper(ex, pya);
}
}
}
use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.
the class PythonFunctionalOperatorsTest method testSourceWithClass.
@Test
public void testSourceWithClass() throws Exception {
Topology topology = new Topology("testSourceWithClass");
addTestToolkit(topology);
StreamSchema outSchema = Type.Factory.getStreamSchema("tuple<int32 seq>");
int count = new Random().nextInt(200) + 10;
SPLStream pysrc = SPL.invokeSource(topology, "com.ibm.streamsx.topology.pysamples.sources::Range", Collections.singletonMap("count", count), outSchema);
Tester tester = topology.getTester();
Condition<Long> expectedCount = tester.tupleCount(pysrc, count);
Condition<List<Tuple>> outTuples = tester.tupleContents(pysrc);
// getConfig().put(ContextProperties.TRACING_LEVEL, TraceLevel.DEBUG);
complete(tester, expectedCount, 20, TimeUnit.SECONDS);
assertTrue(expectedCount.valid());
List<Tuple> result = outTuples.getResult();
assertEquals(count, result.size());
for (int i = 0; i < count; i++) assertEquals(i, result.get(i).getInt("seq"));
}
use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.
the class SimpleStandaloneTest method testTwoStreams.
@Test
public void testTwoStreams() throws Exception {
Topology topology = new Topology("testTwoStreams");
TStream<String> hw = topology.strings("Hello", "World!", "Test!!");
SPLStream hws = SPLStreams.stringToSPLStream(hw);
TStream<String> hw2 = StringStreams.contains(hw, "e");
SPLStream hw2s = SPLStreams.stringToSPLStream(hw2);
Tester tester = topology.getTester();
StreamCounter<Tuple> counter = tester.splHandler(hws, new StreamCounter<Tuple>());
StreamCollector<LinkedList<Tuple>, Tuple> collector = tester.splHandler(hws, StreamCollector.newLinkedListCollector());
StreamCounter<Tuple> counter2 = tester.splHandler(hw2s, new StreamCounter<Tuple>());
StreamCollector<LinkedList<Tuple>, Tuple> collector2 = tester.splHandler(hw2s, StreamCollector.newLinkedListCollector());
StreamsContextFactory.getStreamsContext(StreamsContext.Type.STANDALONE_TESTER).submit(topology).get();
assertEquals(3, counter.getTupleCount());
assertEquals("Hello", collector.getTuples().get(0).getString(0));
assertEquals("World!", collector.getTuples().get(1).getString(0));
assertEquals("Test!!", collector.getTuples().get(2).getString(0));
assertEquals(2, counter2.getTupleCount());
assertEquals("Hello", collector2.getTuples().get(0).getString(0));
assertEquals("Test!!", collector2.getTuples().get(1).getString(0));
}
use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.
the class SlidingSetAggregator method aggregate.
protected void aggregate(Object partition, LinkedList<I> tuples) throws Exception {
final Function<List<I>, O> aggregator = aggregatorHandler.getLogic();
O aggregation = aggregator.apply(tuples);
if (aggregation != null) {
Tuple splTuple = outputMapping.convertTo(aggregation);
output.submit(splTuple);
}
}
use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.
the class SlidingJoin method port1Join.
public void port1Join(Tuple splTuple) throws Exception {
final BiFunction<T, List<U>, J> joiner = joinerHandler.getLogic();
J jTuple;
synchronized (this) {
T tTuple = input1Mapping.convertFrom(splTuple);
LinkedList<U> tuples = getPartitionState(getPort1PartitionKey(tTuple));
jTuple = joiner.apply(tTuple, tuples);
}
if (jTuple != null) {
Tuple splOutTuple = outputMapping.convertTo(jTuple);
output.submit(splOutTuple);
}
}
Aggregations