Source code

    001/*
    002# Licensed Materials - Property of IBM
    003# Copyright IBM Corp. 2020
    004 */
    005package simple;
    006
    007import java.util.Arrays;
    008import java.util.Random;
    009import java.util.List;
    010import java.util.concurrent.TimeUnit;
    011import com.ibm.streamsx.topology.TStream;
    012import com.ibm.streamsx.topology.Topology;
    013import com.ibm.streamsx.topology.context.StreamsContextFactory;
    014import com.ibm.streamsx.topology.function.Function;
    015import com.ibm.streamsx.topology.function.Supplier;
    016import com.ibm.streamsx.topology.TWindow;
    017
    018/**
    019 * Sample topology application. This Java application builds a
    020 * simple topology that demonstrates the usage of submission time parameter for time based window. <BR>
    021 * The application implements the typical pattern of code that declares a
    022 * topology followed by submission of the topology to a Streams context
    023 * {@code com.ibm.streamsx.topology.context.StreamsContext}.
    024 * <BR>
    025 * This demonstrates the mechanics of declaring a topology with submission time parameter and executing it.
    026 * <P>
    027 * This may be executed from the {@code samples/java/functional} directory as:
    028 * <UL>
    029 * <LI>{@code ant run.temperaturesensor} - Using Apache Ant, this will run in standalone
    030 * mode.</LI>
    031 * <LI>{@code ant run.temperaturesensor.bundle} - Using Apache Ant, this will create the bundle only.</LI>
    032 * <LI>
    033 * {@code java -jar funcsamples.jar:../com.ibm.streamsx.topology/lib/com.ibm.streamsx.topology.jar:$STREAMS_INSTALL/lib/com.ibm.streams.operator.samples.jar
    034 *  simple.TemperatureSensor [CONTEXT_TYPE]
    035 * } - Run directly from the command line.
    036 * </LI>
    037 * If no arguments are provided then the topology is executed in bundle mode.
    038 * <BR>
    039 * <i>CONTEXT_TYPE</i> is one of:
    040 * <UL>
    041 * <LI>{@code DISTRIBUTED} - Run as an IBM Streams distributed
    042 * application.</LI>
    043 * <LI>{@code STANDALONE} - Run as an IBM Streams standalone
    044 * application.</LI>
    045 * <LI>{@code BUNDLE} - Create an IBM Streams application bundle.</LI>
    046 * <LI>{@code TOOLKIT} - Create an IBM Streams application toolkit.</LI>
    047 * </UL>
    048 * </LI>
    049 * <LI>
    050 * An application execution within your IDE once you set the class path to include the correct jars.</LI>
    051 * </UL>
    052 * </P>
    053 */
    054public class TemperatureSensor {
    055
    056    /**
    057     * Sample topology application.
    058     * This Java application builds a simple topology
    059     * that demonstrates the usage of submission time parameter for time based window
    060     * and finally prints the aggregates values to standard output.
    061     * <BR>
    062     * The application implements the typical pattern
    063     * of code that declares a topology followed by
    064     * submission of the topology to a Streams context
    065     * (@code com.ibm.streamsx.topology.context.StreamsContext}.
    066
    067     * @param args
    068     * @throws Exception
    069     */
    070    public static void main(String[] args) throws Exception {
    071
    072        /*
    073         * Create the container for the topology that will
    074         * hold the streams of tuples.
    075         */
    076        Topology topology = new Topology("TemperatureSensor");
    077
    078        Random random = new Random();
    079
    080        @SuppressWarnings("unchecked")
    081        TStream<Double> readings = topology.endlessSource(new Supplier<Double>(){
    082            @Override
    083            public Double get() {
    084                return random.nextGaussian();
    085            }
    086
    087        });
    088
    089        Supplier<Integer> time = topology.createSubmissionParameter("time", 5);
    090        TWindow<Double, ?> lastNSeconds = readings.lastSeconds(time);
    091
    092        TStream<Double> maxTemp = lastNSeconds.aggregate(new Function<List<Double>, Double>(){
    093            @Override
    094            public Double apply(List<Double> temps) {
    095                Double max = temps.get(0);
    096                for(Double temp : temps){
    097                    if(temp > max)
    098                        max = temp;
    099                }
    100                return max;
    101            }
    102
    103        });
    104        maxTemp.print();
    105
    106        if (args.length == 0)
    107            StreamsContextFactory.getStreamsContext("BUNDLE").submit(topology).get();
    108        else
    109            StreamsContextFactory.getStreamsContext(args[0]).submit(topology)
    110                    .get();
    111    }
    112}