Search in sources :

Example 21 with Tuple

use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.

the class PythonFunctionalOperatorsTest method testPositionalSampleSimpleFilterUsingSPLType.

@Test
public void testPositionalSampleSimpleFilterUsingSPLType() throws Exception {
    Topology topology = new Topology("testPositionalSampleSimpleFilterUsingSPLType");
    SPLStream tuples = sampleFilterStream(topology);
    addTestToolkit(tuples);
    SPLStream viaPython = SPL.invokeOperator("testspl::SF", tuples, tuples.getSchema(), null);
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(viaPython, 2);
    // first attribute is the sum of the first and second input attributes
    // others are copied across from in to out.
    Tuple r1 = TEST_SCHEMA_SF.getTuple(new Object[] { 32, (short) 25, 34535L });
    Tuple r2 = TEST_SCHEMA_SF.getTuple(new Object[] { 5, (short) 3, 654932L });
    Condition<List<Tuple>> viaPythonResult = tester.tupleContents(viaPython, r1, r2);
    complete(tester, expectedCount, 10, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(viaPythonResult.toString(), viaPythonResult.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) OutputTuple(com.ibm.streams.operator.OutputTuple) Tuple(com.ibm.streams.operator.Tuple) Test(org.junit.Test)

Example 22 with Tuple

use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.

the class PythonFunctionalOperatorsTest method testTupleStream.

public static SPLStream testTupleStream(Topology topology, boolean withSets) {
    TStream<Long> beacon = BeaconStreams.longBeacon(topology, TUPLE_COUNT);
    SPLStream tuples = SPLStreams.convertStream(beacon, new BiFunction<Long, OutputTuple, OutputTuple>() {

        private static final long serialVersionUID = 1L;

        private transient TupleType type;

        private transient Random rand;

        @Override
        public OutputTuple apply(Long v1, OutputTuple v2) {
            if (type == null) {
                type = Type.Factory.getTupleType(getPythonTypesSchema(withSets).getLanguageType());
                rand = new Random();
            }
            Tuple randTuple = (Tuple) type.randomValue(rand);
            v2.assign(randTuple);
            return v2;
        }
    }, getPythonTypesSchema(withSets));
    return tuples;
}
Also used : Random(java.util.Random) TupleType(com.ibm.streams.operator.meta.TupleType) OutputTuple(com.ibm.streams.operator.OutputTuple) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) OutputTuple(com.ibm.streams.operator.OutputTuple) Tuple(com.ibm.streams.operator.Tuple)

Example 23 with Tuple

use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.

the class SPLStreamsTest method testMaintainSPLStream.

@Test
public void testMaintainSPLStream() {
    assumeTrue(isMainRun());
    Topology t = new Topology();
    SPLStream splStreamA = testTupleStream(t);
    assertSPLStream(splStreamA, TEST_SCHEMA);
    SPLStream splStreamB = splStreamA.filter(new AllowAll<Tuple>());
    assertSPLStream(splStreamB, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamA = splStreamB.sample(1.0);
    assertSPLStream(splStreamA, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamB = splStreamA.throttle(1, TimeUnit.MICROSECONDS);
    assertSPLStream(splStreamB, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamA = splStreamB.lowLatency();
    assertSPLStream(splStreamA, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamA = splStreamA.filter(new AllowAll<Tuple>());
    splStreamB = splStreamA.endLowLatency();
    assertSPLStream(splStreamB, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamA = splStreamB.parallel(3);
    assertSPLStream(splStreamA, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamA = splStreamA.filter(new AllowAll<Tuple>());
    splStreamB = splStreamA.endParallel();
    assertSPLStream(splStreamB, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamB = splStreamB.filter(new AllowAll<Tuple>());
    splStreamA = splStreamB.parallel(of(2), Routing.ROUND_ROBIN);
    assertSPLStream(splStreamA, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamA = splStreamA.filter(new AllowAll<Tuple>());
    splStreamB = splStreamA.endParallel();
    assertSPLStream(splStreamB, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamB = splStreamB.filter(new AllowAll<Tuple>());
    splStreamA = splStreamB.isolate();
    assertSPLStream(splStreamA, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
    splStreamB = splStreamA.autonomous();
    assertSPLStream(splStreamB, TEST_SCHEMA);
    assertNotSame(splStreamA, splStreamB);
}
Also used : AllowAll(com.ibm.streamsx.topology.test.AllowAll) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) OutputTuple(com.ibm.streams.operator.OutputTuple) Tuple(com.ibm.streams.operator.Tuple) Test(org.junit.Test)

Example 24 with Tuple

use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.

the class SlidingSet method handleEvent.

@Override
public final synchronized void handleEvent(StreamWindowEvent<Tuple> event) throws Exception {
    final Object partition = event.getPartition();
    LinkedList<I> tuples = getPartitionState(partition);
    switch(event.getType()) {
        case INSERTION:
            for (Tuple splTuple : event.getTuples()) {
                I tuple = inputMapping.convertFrom(splTuple);
                tuples.addLast(tuple);
            }
            break;
        case EVICTION:
            // means any eviction is always the oldest N tuples.
            for (@SuppressWarnings("unused") Tuple splTuple : event.getTuples()) {
                tuples.removeFirst();
            }
            break;
        case PARTITION_EVICTION:
            op.getnPartitions().incrementValue(-1);
            break;
        default:
            break;
    }
    postSetUpdate(event, partition, tuples);
}
Also used : Tuple(com.ibm.streams.operator.Tuple)

Example 25 with Tuple

use of com.ibm.streams.operator.Tuple in project streamsx.topology by IBMStreams.

the class SPLStreams method toStringStream.

/**
     * Convert an {@link SPLStream} to a TStream&lt;String>
     * by taking a specific attribute.
     * The returned stream will contain a {@code String} tuple for
     * each tuple {@code T} on {@code stream}, the value of the
     * {@code String} tuple is {@code T.getString(attributeName)}.
     * A runtime error will occur if the attribute
     * can not be converted using {@code Tuple.getString(attributeName)}.
     * @param stream Stream to be converted to a TStream&lt;String>.
     * @return Stream that will contain a single attribute of tuples from {@code stream}
     */
public static TStream<String> toStringStream(SPLStream stream, String attributeName) {
    Attribute attribute = stream.getSchema().getAttribute(attributeName);
    if (attribute == null) {
        throw new IllegalArgumentException("Atttribute " + attributeName + " not present in SPL schema " + stream.getSchema().getLanguageType());
    }
    final int attributeIndex = attribute.getIndex();
    return stream.convert(new Function<Tuple, String>() {

        private static final long serialVersionUID = 1L;

        @Override
        public String apply(Tuple tuple) {
            return tuple.getString(attributeIndex);
        }
    });
}
Also used : Attribute(com.ibm.streams.operator.Attribute) Tuple(com.ibm.streams.operator.Tuple) OutputTuple(com.ibm.streams.operator.OutputTuple)

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