Code Examples
A repository of 155 code examples for BeepBeep
TickerFeed.java
1 package stockticker;
2 
3 import ca.uqac.lif.cep.Processor;
4 import ca.uqac.lif.cep.tmf.Source;
5 import java.util.Queue;
6 import java.util.Random;
7 
8 public class TickerFeed extends Source
9 {
10  public static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11 
12  protected static final String[] m_realNames = {"MSFT", "GOGL", "APPL", "AMZN", "RHT", "IBM", "DVMT"};
13 
14  protected String[] m_symbolNames;
15 
16  protected float[] m_lastValues;
17 
18  protected Random m_random;
19 
20  protected int m_computeCount;
21 
22  protected int m_maxDays = 0;
23 
24  public TickerFeed(int num_companies, int num_days)
25  {
26  super(1);
27  m_random = new Random();
28  m_maxDays = num_days;
29  m_symbolNames = new String[num_companies];
30  m_lastValues = new float[num_companies];
31  for (int i = 0; i < num_companies; i++)
32  {
33  if (i < m_realNames.length)
34  {
35  m_symbolNames[i] = m_realNames[i];
36  }
37  else
38  {
39  m_symbolNames[i] = randomString(4);
40  }
41  m_lastValues[i] = m_random.nextInt(1000);
42  }
43  m_computeCount = 0;
44  }
45 
46  public TickerFeed()
47  {
48  this(10, 10);
49  }
50 
51  /**
52  * Generates a random string of given length
53  * @param length The length of the string
54  * @return The string
55  */
56  protected String randomString(int length)
57  {
58  StringBuilder sb = new StringBuilder();
59  for (int i = 0; i < length; i++)
60  {
61  sb.append(upper.charAt(m_random.nextInt(25)));
62  }
63  return sb.toString();
64  }
65 
66  @Override
67  protected boolean compute(Object[] inputs, Queue<Object[]> outputs)
68  {
69  int num_day = m_computeCount / m_symbolNames.length;
70  if (num_day >= m_maxDays)
71  {
72  return false;
73  }
74  int pos = m_computeCount % m_symbolNames.length;
75  Object[] out = new Object[3];
76  out[0] = num_day;
77  out[1] = m_symbolNames[pos];
78  float val = m_lastValues[pos] + 30f * (m_random.nextFloat() - 0.5f);
79  out[2] = val;
80  m_lastValues[pos] = val;
81  outputs.add(new Object[] {out});
82  m_computeCount++;
83  return true;
84  }
85 
86  @Override
87  public Processor duplicate(boolean with_state)
88  {
89  // Don't need to implement this
90  return null;
91  }
92 }
String randomString(int length)
Generates a random string of given length.
Definition: TickerFeed.java:56