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;
}
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);
}
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);
}
}
}
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);
}
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;
}
Aggregations