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