Code Examples
A repository of 155 code examples for BeepBeep
PrintStdout.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 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.Pushable;
21 import ca.uqac.lif.cep.io.Print;
22 
23 /**
24  * Print events to the standard input. In this simple example,
25  * we push numbers 0 to 4 at one-second intervals. The expected output
26  * of this program should be:
27  * <pre>
28  * 0,1,2,3,4,
29  * </pre>
30  * The processor chain of this example is very simple, and consists of a
31  * single processor:
32  * <p>
33  * <img
34  * src="./doc-files/io/Print.png"
35  * alt="Processor graph">
36  * <p>
37  * Let us use this program from the command-line. Suppose you compile
38  * this program into a runnable JAR file called {@code printstdout.jar}.
39  * You can then run this program like any other Java JAR as follows:
40  * <pre>
41  * $ java -jar printstdout.jar
42  * </pre>
43  * However, since this program writes to the standard output, you can
44  * redirect it to a file like any other program too. For
45  * example, writing this to the command line will redirect ("pipe") the output
46  * to a file called {@code my-file.txt}:
47  * <pre>
48  * $ java -jar printstdout.jar &gt; my-file.txt
49  * </pre>
50  * Since the output events are produced at the rate of one per second, it will
51  * take five seconds for the file to be written. If you try to open it before
52  * (for example, in a text editor), you will only see the events that have
53  * been pushed to it so far.
54  * <p>
55  * The output can be redirected to other programs as well; for example, to
56  * redirect the output to the <a href="https://en.wikipedia.org/wiki/Wc_(Unix)">wc</a>
57  * command to count the number of characters in the output:
58  * <pre>
59  * $ java -jar printstdout.jar | wc -m
60  * </pre>
61  * Here, the standard output of the Java program is redirected into the
62  * standard input of the {@code wc} command.
63  *
64  * @author Sylvain HallĂ©
65  * @difficulty Easy
66  */
67 public class PrintStdout
68 {
69  public static void main(String[] args) throws InterruptedException
70  {
71  /* We create a print processor.
72  * By default, print sends its events to the computer's
73  * standard output (stdout) stream. */
74  Print print = new Print();
75 
76  /* We get a hold of the Pushable object to push events to the print
77  * processor. In this very simple example, we push numbers 0 to 4
78  * at 1-second intervals. */
79  Pushable p = print.getPushableInput();
80  for (int i = 0; i < 5; i++)
81  {
82  p.push(i);
83  Thread.sleep(1000); // Wait one second
84  }
85  }
86 }
Print events to the standard input.