-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerator.java
More file actions
111 lines (81 loc) · 3.09 KB
/
Copy pathGenerator.java
File metadata and controls
111 lines (81 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package BitcoinEcon;
import java.util.Collections;
import GenCol.*;
import model.modeling.*;
import view.modeling.ViewableAtomic;
public class Generator extends ViewableAtomic {
protected Queue transQ;
protected double numBitcoin;
protected double marketPrice;
protected double updatePriceTime;
public Generator() {
this("Generator", Market.INITIAL_BITCOIN_PRICE);
}
public Generator(String name, double marketPrice) {
super(name);
transQ = new Queue();
addInport("inStop");
addInport("inTransactions");
addOutport("outPriceBitcoin");
this.marketPrice = marketPrice;
}
public void initialize() {
super.initialize();
phase = "active";
sigma = 1;
transQ.clear();
}
public void deltext(double e, message x) {
Continue(e);
for (int i = 0; i < x.getLength(); i++)
if (messageOnPort(x, "inTransactions", i)) {
if (phaseIs("active")) {
// Retrieve Transaction
TransactionEntity message = (TransactionEntity) x.getValOnPort("inTransactions", i);
Transaction trans = message.getv();
transQ.add(trans);
}
} else if (messageOnPort(x, "inStop", i)) {
passivate();
}
// Process all the transactions to get the weighted average price
if (phaseIs("active") && !transQ.isEmpty()) {
double weightedSumOfPrice = 0;
double sumOfBitcoins = 0;
while (!transQ.isEmpty()) {
Transaction transaction = (Transaction) transQ.first();
transQ.remove();
// Make sure this transaction is not about expired orders
if ((transaction.buyOrder != null) && (transaction.sellOrder != null)) {
double transPrice = transaction.price;
double transBitcoin = transaction.bitcoinAmount;
sumOfBitcoins += transBitcoin;
weightedSumOfPrice += transPrice * transBitcoin;
}
}
if (sumOfBitcoins > 0)
marketPrice = weightedSumOfPrice / sumOfBitcoins;
holdIn("active", 1);
}
}
public void deltint(message x) {
// Price of Bitcoin from messages is summed with weight --> sumOfPrice
// Weighted number of transactions --> weights
// priceBitcoin = sumOfPrice / numBitcoin
}
public void deltcon(double e, message x) {
System.out.println("confluent");
deltint();
deltext(0, x);
}
public message out() {
message m = new message();
content con = makeContent("outPriceBitcoin", new entity(String.valueOf(marketPrice)));
m.add(con);
System.out.println("Generator: Bitcoin price " + marketPrice);
return m;
}
public void showState() {
super.showState();
}
}