Search in sources :

Example 6 with Topology

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

the class RegexGrep method main.

@SuppressWarnings("serial")
public static void main(String[] args) throws Exception {
    String contextType = args[0];
    String directory = args[1];
    final Pattern pattern = Pattern.compile(args[2]);
    // Define the topology
    Topology topology = new Topology("RegexGrep");
    // All streams with tuples that are Java String objects
    TStream<String> files = FileStreams.directoryWatcher(topology, directory);
    TStream<String> lines = FileStreams.textFileReader(files);
    /*
         * Functional filter using an anonymous class to define the
         * filtering logic, in this case execution of a regular
         * expression against each input String tuple (each line
         * of the files in the directory).
         */
    TStream<String> filtered = lines.filter(new Predicate<String>() {

        @Override
        public boolean test(String v1) {
            // regular expression pattern
            return matcher.reset(v1).matches();
        }

        // Recreate the matcher (which is not serializable)
        // when the object is deserialized using readResolve.
        transient Matcher matcher;

        /*
             * Since the constructor is no invoked after serialization
             * we use readResolve as a hook to execute initialization
             * code, in this case creating the matcher from the
             * pattern. 
             * The alternative would be to create it on its first use,
             * which would require an if statement in the test method.
             */
        private Object readResolve() throws ObjectStreamException {
            matcher = pattern.matcher("");
            return this;
        }
    });
    // For debugging just print out the tuples
    filtered.print();
    // Execute the topology, just like Grep.
    Future<?> future = StreamsContextFactory.getStreamsContext(contextType).submit(topology);
    Thread.sleep(30000);
    future.cancel(true);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Topology(com.ibm.streamsx.topology.Topology) ObjectStreamException(java.io.ObjectStreamException)

Example 7 with Topology

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

the class AutoClosableTest method testCloseIsCalled.

/**
     * Test close is called for various transformations.
     * @throws Exception
     */
@Test
public void testCloseIsCalled() throws Exception {
    Topology topology = newTopology();
    TStream<String> stream = topology.source(new CloseSupplier());
    stream = stream.filter(new ClosePredicate());
    stream = stream.modify(new CloseUnary());
    stream = stream.multiTransform(new CloseMultiTransform());
    stream = stream.last().aggregate(new CloseAggregate());
    stream.sink(new CloseConsumer());
    this.getTesterContext().submit(topology).get();
    assertTrue(CloseSupplier.seenClose.get());
    assertTrue(ClosePredicate.seenClose.get());
    assertTrue(CloseUnary.seenClose.get());
    assertTrue(CloseMultiTransform.seenClose.get());
    assertTrue(CloseAggregate.seenClose.get());
    assertTrue(CloseConsumer.seenClose.get());
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 8 with Topology

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

the class AutoTypeTest method _testAutoSourceList.

private static void _testAutoSourceList() {
    Topology t = newTopology();
    TStream<List<String>> stream = t.source(new Supplier<Iterable<List<String>>>() {

        @Override
        public Iterable<List<String>> get() {
            return Collections.singletonList(Collections.singletonList("testAutoSourceList"));
        }
    });
    assertEquals(null, stream.getTupleClass());
    assertTrue(stream.getTupleType() instanceof ParameterizedType);
    assertEquals(List.class, ((ParameterizedType) stream.getTupleType()).getRawType());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology)

Example 9 with Topology

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

the class AutoTypeTest method testStringListTyped.

@Test
public void testStringListTyped() throws Exception {
    Topology t = newTopology();
    TStream<String> strings = t.constants(Collections.nCopies(10, "hello")).asType(String.class);
    assertEquals(String.class, strings.getTupleClass());
    assertEquals(String.class, strings.getTupleType());
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 10 with Topology

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

the class AutoTypeTest method _testAutoEndlessSourceClass.

private static void _testAutoEndlessSourceClass() {
    Topology t = newTopology();
    TStream<Integer> ints = t.endlessSource(new Supplier<Integer>() {

        @Override
        public Integer get() {
            return 3;
        }
    });
    assertEquals(Integer.class, ints.getTupleClass());
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology)

Aggregations

Topology (com.ibm.streamsx.topology.Topology)267 TestTopology (com.ibm.streamsx.topology.test.TestTopology)235 Test (org.junit.Test)213 List (java.util.List)66 Tester (com.ibm.streamsx.topology.tester.Tester)61 ArrayList (java.util.ArrayList)50 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)39 JSONObject (com.ibm.json.java.JSONObject)25 HashMap (java.util.HashMap)21 Message (com.ibm.streamsx.topology.tuple.Message)20 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)20 Random (java.util.Random)19 File (java.io.File)15 Value (com.ibm.streamsx.topology.logic.Value)14 Tuple (com.ibm.streams.operator.Tuple)13 MqttStreams (com.ibm.streamsx.topology.messaging.mqtt.MqttStreams)13 TSink (com.ibm.streamsx.topology.TSink)12 KafkaConsumer (com.ibm.streamsx.topology.messaging.kafka.KafkaConsumer)12 AllowAll (com.ibm.streamsx.topology.test.AllowAll)12 BeaconTuple (com.ibm.streamsx.topology.tuple.BeaconTuple)11