001/*
002# Licensed Materials - Property of IBM
003# Copyright IBM Corp. 2015  
004 */
005package simple;
006
007import com.ibm.streamsx.topology.TStream;
008import com.ibm.streamsx.topology.Topology;
009import com.ibm.streamsx.topology.context.StreamsContextFactory;
010import com.ibm.streamsx.topology.function.Predicate;
011
012/**
013* Sample filtering echo topology application. This Java application builds a
014* simple topology that echos its command line arguments to standard output.
015* <BR>
016* The application implements the typical pattern of code that declares a
017* topology followed by submission of the topology to a Streams context
018* {@code com.ibm.streamsx.topology.context.StreamsContext}.
019* <BR>
020* This demonstrates use of Java functional logic to filter the tuples.
021* An in-line anonymous class implements the filtering logic, in this
022* case only echo tuples that start with the letter {@code d}.
023* <P>
024* This topology is always executed in embedded mode,
025* within this JVM.
026* <BR>
027* This may be executed from the {@code samples/java/functional} directory as:
028* <UL>
029* <LI>{@code ant run.filterecho} - Using Apache Ant, this will run in embedded
030* mode.</LI>
031* <LI>
032* {@code java -cp functionalsamples.jar:../../../com.ibm.streamsx.topology/lib/com.ibm.streamsx.topology.jar:$STREAMS_INSTALL/lib/com.ibm.streams.operator.samples.jar
033*  simple.FilterEcho "d print this" "omit this"
034* } - Run directly from the command line in embedded mode.
035* <LI>
036* An application execution within your IDE once you set the class path to include the correct jars.</LI>
037* </UL>
038* </P>
039*/
040public class FilterEcho {
041
042    /**
043    * Sample filtering echo topology application. This Java application builds a
044    * simple topology that echos its command line arguments to standard output.
045    * <BR>
046    * The application implements the typical pattern of code that declares a
047    * topology followed by submission of the topology to a Streams context (@code
048    * com.ibm.streamsx.topology.context.StreamsContext}.
049    *
050    */
051    @SuppressWarnings("serial")
052    public static void main(String[] args) throws Exception {
053
054        Topology topology = new Topology("FilterEcho");
055
056        TStream<String> echo = topology.strings(args);
057        
058        /*
059         * Declare a stream that will execute functional logic
060         * against tuples on the echo stream.
061         * For each tuple that will appear on echo, the below
062         * test(tuple) method will be called, it it returns
063         * true then the tuple will appear on the filtered
064         * stream, otherwise the tuple is discarded.
065         */
066        TStream<String> filtered = echo.filter(new Predicate<String>() {
067
068            @Override
069            public boolean test(String tuple) {
070                return tuple.startsWith("d");
071            }
072        });
073
074        filtered.print();
075        
076        /*
077         * At this point the topology is declared with a single
078         * stream that is printed to System.out.
079         */
080
081        /*
082         * Now execute the topology by submitting to an
083         * embedded (within this JVM) StreamsContext.
084         */
085
086        StreamsContextFactory.getEmbedded().submit(topology).get();
087    }
088}