Search in sources :

Example 6 with Tuple

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);
        }
    }
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Set(java.util.Set) TreeSet(java.util.TreeSet) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) JSONObject(com.ibm.json.java.JSONObject) List(java.util.List) Tuple(com.ibm.streams.operator.Tuple) PythonFunctionalOperatorsTest(com.ibm.streamsx.topology.test.splpy.PythonFunctionalOperatorsTest) Test(org.junit.Test)

Example 7 with Tuple

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"));
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Random(java.util.Random) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) OutputTuple(com.ibm.streams.operator.OutputTuple) Tuple(com.ibm.streams.operator.Tuple) Test(org.junit.Test)

Example 8 with Tuple

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));
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Tuple(com.ibm.streams.operator.Tuple) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 9 with Tuple

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);
    }
}
Also used : List(java.util.List) LinkedList(java.util.LinkedList) Tuple(com.ibm.streams.operator.Tuple)

Example 10 with Tuple

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);
    }
}
Also used : List(java.util.List) LinkedList(java.util.LinkedList) Tuple(com.ibm.streams.operator.Tuple)

Aggregations

Tuple (com.ibm.streams.operator.Tuple)27 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)16 OutputTuple (com.ibm.streams.operator.OutputTuple)15 Topology (com.ibm.streamsx.topology.Topology)13 TestTopology (com.ibm.streamsx.topology.test.TestTopology)12 Tester (com.ibm.streamsx.topology.tester.Tester)12 Test (org.junit.Test)12 LinkedList (java.util.LinkedList)9 List (java.util.List)9 StreamSchema (com.ibm.streams.operator.StreamSchema)5 Condition (com.ibm.streamsx.topology.tester.Condition)3 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 JSONArray (com.ibm.json.java.JSONArray)1 JSONArtifact (com.ibm.json.java.JSONArtifact)1 JSONObject (com.ibm.json.java.JSONObject)1 StreamHandler (com.ibm.streams.flow.handlers.StreamHandler)1 Attribute (com.ibm.streams.operator.Attribute)1 TupleType (com.ibm.streams.operator.meta.TupleType)1 RString (com.ibm.streams.operator.types.RString)1