Code Examples
A repository of 155 code examples for BeepBeep
LineReaderExample.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 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 java.io.InputStream;
21 
22 import ca.uqac.lif.cep.Connector;
23 import ca.uqac.lif.cep.Pullable;
24 import ca.uqac.lif.cep.functions.ApplyFunction;
25 import ca.uqac.lif.cep.io.ReadLines;
26 import ca.uqac.lif.cep.util.Numbers;
27 
28 /**
29  * Read lines from a text file and convert them into numbers. This illustrates
30  * the use of the {@link ca.uqac.lif.cep.io.ReadLines ReadLines} processor.
31  * <p>
32  * The expected output of the program is:
33  * <pre>
34  * 3,Integer
35  * 1,Integer
36  * 4,Integer
37  * 1,Integer
38  * 5,Integer
39  * 9,Integer
40  * 2.2,Float
41  * Exception in thread "main" java.util.NoSuchElementException
42  * at ca.uqac.lif.cep.UniformProcessor$UnaryPullable.pull(UniformProcessor.java:269)
43  * at io.LineReaderExample.main(LineReaderExample.java:43)
44  * </pre>
45  * @author Sylvain HallĂ©
46  * @difficulty Easy
47  */
48 public class LineReaderExample
49 {
50  public static void main(String[] args)
51  {
52  /// We obtain an InputStream from a file. Since we refer to a file
53  // that lies within the source code's folders, we can access it using
54  // the getResourceAsStream method. You could replace this with any
55  // other instruction that returns an InputStream.
56  InputStream is = LineReaderExample.class.getResourceAsStream("pi.txt");
57 
58  // We then instantiate a ReadLines processor, and instruct it to read
59  // from the input stream we created
60  ReadLines reader = new ReadLines(is);
61 
62  // The output of ReadLines are String events. We apply the function
63  // NumberCast on these strings to convert them into number objects.
64  ApplyFunction cast = new ApplyFunction(Numbers.numberCast);
65 
66  // The reader and cast processors are connected
67  Connector.connect(reader, cast);
68 
69  // We then pull repeatedly on cast. Note the use of hasNext: since we
70  // are reading from a file, eventually we reach the end of the file
71  // and no more events can be produced.
72  Pullable p = cast.getPullableOutput();
73  while (p.hasNext())
74  {
75  Number n = (Number) p.next();
76  System.out.println(n + "," + n.getClass().getSimpleName());
77  }
78  // Just for fun, let us try to pull one last time on p. This will
79  // throw an exception
80  p.pull();
81  ///
82  }
83 
84 }
Read lines from a text file and convert them into numbers.
static void main(String[] args)