Search in sources :

Example 31 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class StreamImpl method split.

@Override
public List<TStream<T>> split(int n, ToIntFunction<T> splitter) {
    if (n <= 0)
        throw new IllegalArgumentException("n");
    List<TStream<T>> l = new ArrayList<>(n);
    String opName = LogicUtils.functionName(splitter);
    BOperatorInvocation bop = JavaFunctional.addFunctionalOperator(this, opName, JavaFunctionalOps.SPLIT_KIND, splitter).layoutKind("Split");
    SourceInfo.setSourceInfo(bop, StreamImpl.class);
    connectTo(bop, true, null);
    Type outputType = refineType(ToIntFunction.class, 0, splitter);
    for (int i = 0; i < n; i++) {
        TStream<T> splitOutput = JavaFunctional.addJavaOutput(this, bop, outputType, false);
        l.add(splitOutput);
    }
    return l;
}
Also used : TStream(com.ibm.streamsx.topology.TStream) Type(java.lang.reflect.Type) CONSISTENT(com.ibm.streamsx.topology.generator.operator.OpProperties.CONSISTENT) ArrayList(java.util.ArrayList) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) Print(com.ibm.streamsx.topology.internal.logic.Print)

Example 32 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class StreamImpl method union.

@SuppressWarnings("unchecked")
@Override
public TStream<T> union(Set<TStream<T>> others) {
    if (others.isEmpty())
        return this;
    Set<TStream<T>> allStreams = new HashSet<>();
    allStreams.addAll(others);
    allStreams.add(this);
    // Check we don't have just a single stream.
    if (allStreams.size() == 1)
        return this;
    List<TStream<T>> sourceStreams = new ArrayList<>();
    sourceStreams.addAll(allStreams);
    String schema = output()._type();
    Type tupleType = getTupleType();
    // Unwrap all streams so that we do not add the same stream twice
    // in multiple unions or explicitly and in a union.
    Set<BOutput> outputs = new HashSet<>();
    for (int i = 0; i < sourceStreams.size(); i++) {
        TStream<T> s = sourceStreams.get(i);
        // E..g TStream<String>.
        if (!schema.equals(s.output()._type())) {
            if (s.getTupleClass() != null) {
                // This stream has the direct schema!
                schema = s.output()._type();
                assert getTupleClass() == null;
                tupleType = s.getTupleClass();
                if (i != 0) {
                    // Didn't discover it first
                    // reset to go through the list
                    // again. Note this assumes there
                    // are just two options for the schema
                    // generic or direct
                    // to get back to 0.
                    i = -1;
                    outputs.clear();
                    continue;
                }
            } else {
                assert tupleType instanceof Class;
                s = s.asType((Class<T>) tupleType);
                assert s.output()._type().equals(schema);
                sourceStreams.set(i, s);
            }
        }
        outputs.add(s.output());
    }
    BOutput unionOutput = builder().addUnion(outputs);
    return new StreamImpl<T>(this, unionOutput, tupleType, serializer);
}
Also used : ArrayList(java.util.ArrayList) Print(com.ibm.streamsx.topology.internal.logic.Print) TStream(com.ibm.streamsx.topology.TStream) Type(java.lang.reflect.Type) BOutput(com.ibm.streamsx.topology.builder.BOutput) CONSISTENT(com.ibm.streamsx.topology.generator.operator.OpProperties.CONSISTENT) HashSet(java.util.HashSet)

Example 33 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class EmbeddedTesterRuntime method setupEmbeddedTestHandlers.

private void setupEmbeddedTestHandlers(EmbeddedGraph eg) throws Exception {
    // Embedded does not support a StreamHandler against
    // a connected stream. In that case add a no-op intermediate stream.
    Set<TStream<?>> adds = new HashSet<>();
    // Find all that are connected.
    for (TStream<?> stream : handlers.keySet()) {
        if (stream.output().isConnected())
            adds.add(stream);
    }
    // Insert a filter for the handler.
    for (TStream<?> stream : adds) {
        TStream<?> filter = stream.filter(t -> true);
        Set<StreamHandler<Tuple>> handler = handlers.get(stream);
        handlers.remove(stream);
        handlers.put(filter, handler);
    }
    final JavaTestableGraph tg = eg.getExecutionGraph();
    for (TStream<?> stream : handlers.keySet()) {
        Set<StreamHandler<Tuple>> streamHandlers = handlers.get(stream);
        final BOutput output = stream.output();
        final OutputPortDeclaration portDecl = eg.getOutputPort(output.name());
        for (StreamHandler<Tuple> streamHandler : streamHandlers) {
            tg.registerStreamHandler(portDecl, streamHandler);
        }
    }
}
Also used : TStream(com.ibm.streamsx.topology.TStream) BOutput(com.ibm.streamsx.topology.builder.BOutput) StreamHandler(com.ibm.streams.flow.handlers.StreamHandler) OutputPortDeclaration(com.ibm.streams.flow.declare.OutputPortDeclaration) JavaTestableGraph(com.ibm.streams.flow.javaprimitives.JavaTestableGraph) Tuple(com.ibm.streams.operator.Tuple) HashSet(java.util.HashSet)

Example 34 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class RESTTesterRuntime method addConditionToStream.

/**
 * Add the conditions as for each operators that monitor the
 * condition and sets metrics.
 *
 * Then create a condition implementation that will monitor the
 * metrics using the REST api and link it to the user condition.
 */
@SuppressWarnings("unchecked")
private void addConditionToStream(TStream<?> stream, UserCondition<?> userCondition) {
    MetricCondition<?> condition = null;
    String name = null;
    Consumer<Object> fn = null;
    if (userCondition instanceof CounterUserCondition) {
        CounterUserCondition uc = (CounterUserCondition) userCondition;
        name = "count_" + id++;
        fn = new TupleCount<Object>(name, uc.getExpected(), uc.isExact());
        condition = new CounterMetricCondition(name, uc);
    } else if (userCondition instanceof ContentsUserCondition) {
        ContentsUserCondition<Object> uc = (ContentsUserCondition<Object>) userCondition;
        name = "contents_" + id++;
        fn = new TupleContents<Object>(name, uc.isOrdered(), uc.getExpected());
        condition = new MetricCondition<Object>(name, (UserCondition<Object>) userCondition);
    } else if (userCondition instanceof StringPredicateUserCondition) {
        StringPredicateUserCondition uc = (StringPredicateUserCondition) userCondition;
        name = "stringChecker_" + id++;
        fn = new StringPredicateChecker(name, uc.getPredicate());
        condition = new MetricCondition<Object>(name, (UserCondition<Object>) userCondition);
    }
    if (metricsChecker == null)
        throw new UnsupportedOperationException(userCondition.toString());
    TStream<Object> os = (TStream<Object>) stream;
    TSink end = os.forEach(fn);
    end.operator().layout().addProperty("hidden", true);
    if (os.isPlaceable())
        end.colocate(os);
    metricsChecker.addCondition(name, condition);
}
Also used : TupleContents(com.ibm.streamsx.topology.internal.tester.fns.TupleContents) TSink(com.ibm.streamsx.topology.TSink) UserCondition(com.ibm.streamsx.topology.internal.tester.conditions.UserCondition) CounterUserCondition(com.ibm.streamsx.topology.internal.tester.conditions.CounterUserCondition) StringPredicateUserCondition(com.ibm.streamsx.topology.internal.tester.conditions.StringPredicateUserCondition) ContentsUserCondition(com.ibm.streamsx.topology.internal.tester.conditions.ContentsUserCondition) ContentsUserCondition(com.ibm.streamsx.topology.internal.tester.conditions.ContentsUserCondition) TStream(com.ibm.streamsx.topology.TStream) StringPredicateUserCondition(com.ibm.streamsx.topology.internal.tester.conditions.StringPredicateUserCondition) JsonObject(com.google.gson.JsonObject) StringPredicateChecker(com.ibm.streamsx.topology.internal.tester.fns.StringPredicateChecker) CounterUserCondition(com.ibm.streamsx.topology.internal.tester.conditions.CounterUserCondition)

Example 35 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class Invoker method invokePrimitive.

/**
 * Invoke a functional operator consuming an arbitrary number of
 * input streams and producing an arbitrary number of output streams.
 *
 * @param te Topology element.
 * @param kind Java functional operator kind.
 * @param streams Input streams.
 * @param invokeInfo Operator invocation information.
 * @param logic Logic to invoke for each input tuple.
 * @param tupleTypes Tuple types for the output stream.
 * @param inputSerializers How input tuples are serialized.
 * @param outputSerializers How output tuples are serialized.
 * @param parameters Parameters for the operator invocation.
 *
 * @return Streams produced by the primitive operator.
 */
static List<TStream<?>> invokePrimitive(TopologyElement te, String kind, List<TStream<?>> streams, JsonObject invokeInfo, ObjIntConsumer<Object> logic, List<Type> tupleTypes, List<TupleSerializer> inputSerializers, List<TupleSerializer> outputSerializers, Map<String, Object> parameters) {
    parameters = copyParameters(parameters);
    if (inputSerializers != null && !inputSerializers.isEmpty()) {
        assert streams.size() == inputSerializers.size();
        String[] serializers = new String[inputSerializers.size()];
        for (int i = 0; i < serializers.length; i++) {
            TupleSerializer serializer = inputSerializers.get(i);
            if (serializer == null)
                serializers[i] = "";
            else
                serializers[i] = serializeLogic(serializer);
        }
        parameters.put("inputSerializer", serializers);
    }
    if (outputSerializers != null && !outputSerializers.isEmpty()) {
        assert tupleTypes.size() == outputSerializers.size();
        String[] serializers = new String[outputSerializers.size()];
        for (int i = 0; i < serializers.length; i++) {
            TupleSerializer serializer = outputSerializers.get(i);
            if (serializer == null)
                serializers[i] = "";
            else
                serializers[i] = serializeLogic(serializer);
        }
        parameters.put("outputSerializer", serializers);
    }
    BOperatorInvocation primitive = JavaFunctional.addFunctionalOperator(te, jstring(invokeInfo, "name"), kind, logic, parameters);
    if (inputSerializers != null) {
        for (TupleSerializer serializer : inputSerializers) if (serializer != null)
            JavaFunctional.addDependency(te, primitive, serializer.getClass());
    }
    // Extract any source location information from the config.
    SourceInfo.setInvocationInfo(primitive, invokeInfo);
    for (TStream<?> stream : streams) stream.connectTo(primitive, true, null);
    List<TStream<?>> outputs = null;
    if (tupleTypes != null) {
        outputs = new ArrayList<>(tupleTypes.size());
        for (int i = 0; i < tupleTypes.size(); i++) {
            Type tupleType = tupleTypes.get(i);
            outputs.add(JavaFunctional.addJavaOutput(te, primitive, tupleType, outputSerializers == null ? empty() : ofNullable(outputSerializers.get(i)), tupleTypes.size() == 1));
        }
    }
    return outputs;
}
Also used : TStream(com.ibm.streamsx.topology.TStream) TupleSerializer(com.ibm.streamsx.topology.spi.runtime.TupleSerializer) Type(java.lang.reflect.Type) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation)

Aggregations

TStream (com.ibm.streamsx.topology.TStream)52 Topology (com.ibm.streamsx.topology.Topology)44 Test (org.junit.Test)44 Tester (com.ibm.streamsx.topology.tester.Tester)41 List (java.util.List)36 TestTopology (com.ibm.streamsx.topology.test.TestTopology)30 Condition (com.ibm.streamsx.topology.tester.Condition)28 TimeUnit (java.util.concurrent.TimeUnit)27 HashSet (java.util.HashSet)25 HashMap (java.util.HashMap)23 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)19 StreamsContext (com.ibm.streamsx.topology.context.StreamsContext)17 ArrayList (java.util.ArrayList)17 SPLStreams (com.ibm.streamsx.topology.spl.SPLStreams)16 Map (java.util.Map)16 StreamsContextFactory (com.ibm.streamsx.topology.context.StreamsContextFactory)15 SPL (com.ibm.streamsx.topology.spl.SPL)15 Type (com.ibm.streamsx.topology.context.StreamsContext.Type)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 Assume.assumeTrue (org.junit.Assume.assumeTrue)14