Code Examples
A repository of 155 code examples for BeepBeep
PointDistance.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 static ca.uqac.lif.cep.Connector.INPUT;
21 import static ca.uqac.lif.cep.Connector.LEFT;
22 import static ca.uqac.lif.cep.Connector.OUTPUT;
23 import static ca.uqac.lif.cep.Connector.RIGHT;
24 import static ca.uqac.lif.cep.Connector.connect;
25 import ca.uqac.lif.cep.Pullable;
26 import ca.uqac.lif.cep.functions.BinaryFunction;
27 import ca.uqac.lif.cep.functions.ApplyFunction;
28 import ca.uqac.lif.cep.tmf.Fork;
29 import ca.uqac.lif.cep.tmf.QueueSource;
30 import ca.uqac.lif.cep.tmf.Trim;
31 
32 /**
33  * Calculate the Euclidean distance of each two successive points in an
34  * input trace of (x,y) coordinates. For two points (<i>x</i><sub>1</sub>,<i>y</i><sub>1</sub>)
35  * and (<i>x</i><sub>2</sub>,<i>y</i><sub>2</sub>), the Euclidean distance is
36  * defined as the square root of (<i>x</i><sub>1</sub>-<i>x</i><sub>2</sub>)<sup>2</sup> +
37  * (<i>y</i><sub>1</sub>-<i>y</i><sub>2</sub>)<sup>2</sup>. The chain of
38  * processors in this example can be represented graphically as:
39  * <p>
40  * <img src="./doc-files/basic/PointDistance.png" alt="Processor graph">
41  * <p>
42  * In this picture, light green pipes correspond to streams of {@link Point}
43  * objects. The processor with a ruler and a "d" is the {@link Distance}
44  * processor defined below.
45  * <p>
46  * On an input stream made of points (2,7), (1,8), (2,8), (1,8), &hellip;, the
47  * expected output of this program should look like:
48  * <pre>
49  * 1.4142135
50  * 1.0
51  * 1.0
52  * 1.0
53  * 3.6055512
54  * &hellip;
55  * </pre>
56  *
57  * @author Sylvain HallĂ©
58  * @difficulty Easy
59  */
60 public class PointDistance
61 {
62  /* We create a new type of event: Point. A point is just
63  * an "x" and a "y" value. This shows that in BeepBeep, anything can be an
64  * event, including user-defined objects. */
65  public static class Point
66  {
67  public float x;
68  public float y;
69 
70  public Point(float x, float y)
71  {
72  this.x = x;
73  this.y = y;
74  }
75  }
76 
77  /**
78  * We create a new function: Distance. This function takes two points
79  * as its input, and returns their Euclidean distance as its output.
80  * It is therefore a 2:1 function.
81  *
82  * @see functions.AddNumbers#AddNumbers()
83  */
84  public static class Distance extends BinaryFunction<Point,Point,Float>
85  {
86  public Distance()
87  {
88  super(Point.class, Point.class, Float.class);
89  }
90 
91  @Override
92  public Float getValue(Point p1, Point p2)
93  {
94  return (float) Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
95  }
96  }
97 
98  /*
99  * A small main to illustrate the concept.
100  */
101  public static void main(String[] args)
102  {
103  /* Create a new source made of arbitrary Point objects. */
104  QueueSource point_source = new QueueSource(1);
105  point_source.setEvents(getListOfPoints());
106 
107  /* Duplicate the output of source along two paths. */
108  Fork fork = new Fork(2);
109  connect(point_source, OUTPUT, fork, INPUT);
110 
111  /* The first path is plugged directly as the first argument of a
112  * function processor that computes the Euclidean distance between
113  * two points. */
114  ApplyFunction distance_proc = new ApplyFunction(new Distance());
115  connect(fork, LEFT, distance_proc, LEFT);
116 
117  /* The second path is trimmed of its first event... */
118  Trim trim = new Trim(1);
119  connect(fork, RIGHT, trim, INPUT);
120 
121  /* And is then plugged as the second input of the distance processor.
122  * This has for effect that distance_proc will compute the distance
123  * between each point and the next. */
124  connect(trim, OUTPUT, distance_proc, RIGHT);
125 
126  /* Pull and print the first 10 events from the output. */
127  Pullable p = distance_proc.getPullableOutput(OUTPUT);
128  for (int i = 0; i < 10; i++)
129  {
130  float d = (Float) p.pull();
131  System.out.println(d);
132  }
133  }
134 
135  /**
136  * Creates a dummy array of points
137  * @return An array of points
138  */
139  public static Object[] getListOfPoints()
140  {
141  return new Object[]{
142  new Point(2, 7),
143  new Point(1, 8),
144  new Point(2, 8),
145  new Point(1, 8),
146  new Point(2, 8),
147  new Point(4, 5),
148  new Point(9, 0),
149  new Point(4, 5),
150  new Point(2, 3),
151  new Point(5, 3),
152  new Point(6, 0),
153  new Point(2, 8)
154  };
155  }
156 }
static Object [] getListOfPoints()
Creates a dummy array of points.
Calculate the Euclidean distance of each two successive points in an input trace of (x...