Code Examples
A repository of 155 code examples for BeepBeep
IncorrectPiping.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 basic;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.Connector.ConnectorException;
22 import ca.uqac.lif.cep.Processor;
23 import ca.uqac.lif.cep.functions.ApplyFunction;
24 import ca.uqac.lif.cep.tmf.QueueSource;
25 import ca.uqac.lif.cep.util.Booleans;
26 import ca.uqac.lif.cep.util.Numbers;
27 
28 /**
29  * Pipe processors with non-matching event types. Doing this in BeepBeep
30  * causes a {@link ConnectorException} to be thrown. This example shows what
31  * happens in that case. The chain of processors in this example can be represented
32  * graphically as:
33  * <p>
34  * <img src="./doc-files/basic/IncorrectPiping.png" alt="Processor graph">
35  * <p>
36  * The expected output of this program should look like this:
37  * <pre>
38  * Exception in thread "main" ca.uqac.lif.cep.Connector$IncompatibleTypesException:
39  * Cannot connect output 0 of ABS to input 0 of ¬: incompatible types
40  * at ca.uqac.lif.cep.Connector.checkForException(Connector.java:357)
41  * at ca.uqac.lif.cep.Connector.connect(Connector.java:148)
42  * at ca.uqac.lif.cep.Connector.connect(Connector.java:279)
43  * at ca.uqac.lif.cep.Connector.connect(Connector.java:251)
44  * at basic.IncorrectPiping.main(IncorrectPiping.java:42)
45  * </pre>
46  * If we refer to the picture, the exception is raised when attempting to
47  * connect the output of the first function processor (turquoise, representing
48  * a number) to the input of the second function processor (light blue,
49  * representing a Boolean).
50  *
51  * @author Sylvain Hallé
52  * @difficulty Easy
53  */
54 public class IncorrectPiping
55 {
56  public static void main(String[] args)
57  {
58  /// Create a simple source with a single numerical event
59  QueueSource source = new QueueSource();
60  source.setEvents(3);
61 
62  /* Create a processor that computes the absolute value of a number
63  * and connect it to that source. */
64  Processor av = new ApplyFunction(Numbers.absoluteValue);
65  Connector.connect(source, av);
66 
67  /* Attempt to connect a processor that computes the negation of a
68  * Boolean value to the av processor defined above. This will cause an
69  * exception to be thrown: the output type of av is Number, while the
70  * input type of neg is Boolean. */
71  Processor neg = new ApplyFunction(Booleans.not);
72  Connector.connect(av, neg); // Will throw an exception
73 
74  /* As a result, this line of the program should not be reached, as the
75  * execution will stop with the throwing of a ConnectorException on
76  * the line above. */
77  System.out.println("This line will not be reached");
78  ///
79  }
80 }
Pipe processors with non-matching event types.
static void main(String[] args)