Code Examples
A repository of 155 code examples for BeepBeep
ReadTokens.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package io;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.ProcessorException;
22 import ca.uqac.lif.cep.io.Print;
23 import ca.uqac.lif.cep.io.ReadStringStream;
24 import ca.uqac.lif.cep.tmf.Pump;
25 import ca.uqac.lif.cep.util.FindPattern;
26 
27 /**
28  * Read complete comma-separated tokens from the standard input.
29  * You should first understand how {@link ReadStdin} works before look at this
30  * example. In this example, we pipe the character strings of the
31  * {@link ca.uqac.lif.cep.io.StreamReader StreamReader} into a
32  * {@link ca.uqac.lif.cep.input.CsvFeeder CsvFeeder} before sending its output
33  * into the <tt>Print</tt> processor.
34  * <p>
35  * Graphically, the processor chain is represented as follows:
36  * <p>
37  * <img
38  * src="./doc-files/io/ReadTokens.png"
39  * alt="Processor graph">
40  * <p>
41  * The main difference of this chain is that the <tt>CsvFeeder</tt> only
42  * outputs <em>complete</em> tokens, which are delimited by a comma. Suppose
43  * you compile this program as a runnable JAR (for example,
44  * <tt>read-tokens.jar</tt>) and run it in the same way as {@link ReadStdin},
45  * using the named pipe <tt>mypipe</tt>.
46  * <pre>
47  * $ cat mypipe &gt; java -jar read-tokens.jar
48  * </pre>
49  * From a second command line window, you can now push strings to the program
50  * by <tt>echo</tt>ing them to <tt>mypipe</tt>:
51  * <pre>
52  * $ echo "abc,def," &gt; mypipe
53  * </pre>
54  * The program will output
55  * <pre>
56  * abc
57  * def
58  * </pre>
59  * Note here how each of <tt>abc</tt> and <tt>def</tt> have been printed on
60  * <em>two</em> separate lines. This is because the CSV feeder broke the input
61  * string into two events, since there are two commas that indicate the
62  * presence of two tokens. This also means that the feeder waits until the
63  * comma before outputting an event; hence writing:
64  * <pre>
65  * $ echo "gh" &gt; mypipe
66  * </pre>
67  * will result in no output. The CSV feeder buffers the character string until
68  * it sees the token delimiter. Typing:
69  * <pre>
70  * $ echo "i,jkl," &gt; mypipe
71  * </pre>
72  * will produce
73  * <pre>
74  * ghi
75  * jkl
76  * </pre>
77  * The usefulness of the CsvFeeder (or of other token feeders available in
78  * BeepBeep) is to reconstruct complete tokens out of character strings.
79  * Processors such as the {@link ca.uqac.lif.cep.io.StreamReader StreamReader}
80  * read their input into chunks in such a way that a token can be split
81  * across two successive chunks (its beginning in the first chunk, and its
82  * end in the second).
83  *
84  * @see ReadStdin
85  * @author Sylvain HallĂ©
86  * @difficulty Easy
87  */
88 public class ReadTokens
89 {
90 
91  public static void main(String[] args) throws ProcessorException, InterruptedException
92  {
93  /* Read from stdin using a StringStreamReader */
94  ///
95  ReadStringStream reader = new ReadStringStream(System.in);
96  reader.setIsFile(false);
97 
98  /* We connect the reader to a pump, which will periodically ask
99  * the reader to read new characters from the input stream */
100  Pump pump = new Pump(100);
101  Thread pump_thread = new Thread(pump);
102  Connector.connect(reader, pump);
103 
104  /* Create a CSV token feeder */
105  FindPattern feeder = new FindPattern("(.*?),");
106  Connector.connect(pump, feeder);
107 
108  /* Print the output of the feeder */
109  Print print = new Print().setSeparator("\n");
110  Connector.connect(feeder, print);
111 
112  /* Start the reader and enter an idle loop */
113  pump_thread.start();
114  while (true)
115  {
116  Thread.sleep(10000);
117  }
118  ///
119 
120  }
121 
122 }
Read complete comma-separated tokens from the standard input.
Definition: ReadTokens.java:88