-
Notifications
You must be signed in to change notification settings - Fork 1
/
astateconsume.js
189 lines (158 loc) · 4.79 KB
/
astateconsume.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const DistillationResult = {
WORKING : "working",
DISTILLED : "distilled",
STOPNOW : "stopnow",
STOPPED : "stopped",
STARTNOW : "startnow",
DONTCARE : "dontcare"//does not affect the gate list or the display of the distillation activity
}
/*
* Methods for availability and consumption of A states
*/
var availableDistilledAStates = 0;
var reachedTimeStep = -1;
var aStatesData = {};
var currentlyAGenerated = 0;
var maxNrAToGenerate = -1;
var lastDistillEndTime = -1;
//the current state of the distillery is stored
var currentState = DistillationResult.WORKING;
function isDistillationTime(timeStep)
{
var ret = ((timeStep - lastDistillEndTime) % toolParameters.distillationLength) == 0;
return ret;
}
/**
* Returns true if distillation should stop
* @param timeStep
*/
function updateAvailableDistilledStates(timeStep)
{
if(reachedTimeStep == timeStep)
{
//the currentState can be changed potentially only by consume
return currentState;
}
//was <=
else if(timeStep < reachedTimeStep)
{
//cannot go back in time
return DistillationResult.DONTCARE;
}
else if(timeStep > reachedTimeStep)
{
//store the current state with append
if(reachedTimeStep != -1)
appendChartData(reachedTimeStep, currentState, false);
currentState = DistillationResult.WORKING;
if(evaluateCloseDistillery())
{
//distillery is closed forever
currentState = DistillationResult.STOPPED;
console.log("THIS IS STOPPED!");
}
else
{
if (!evaluateStopCondition())
{
if (isDistillationTime(timeStep)) {
availableDistilledAStates++;
currentlyAGenerated++;
currentState = DistillationResult.DISTILLED;
}
}
if(evaluateStopCondition()) {
if (currentState == DistillationResult.DISTILLED)
currentState = DistillationResult.STOPNOW;
else
currentState = DistillationResult.STOPPED;
}
}
reachedTimeStep = timeStep;
}
return currentState;
}
/**
* Returns true if the stop condition is fulfilled. False, otherwise.
*/
function evaluateStopCondition()
{
return (availableDistilledAStates == toolParameters.maximumAAvailable);
}
/**
* Returns true if all the necessary T states were distilled, and the distillery
* is not required for the rest of the circuit. False, otherwise.
*/
function evaluateCloseDistillery()
{
return currentlyAGenerated == maxNrAToGenerate;
}
/**
Returns true if distillation is allowed to proceed
*/
function consumeDistilledState(timeStep)
{
var ret = DistillationResult.DONTCARE;
if(currentState == DistillationResult.STOPPED || currentState == DistillationResult.STOPNOW)
{
if(!evaluateCloseDistillery()) {
//once all the distillations were performed
//it is useless to start the distillery again
ret = DistillationResult.STARTNOW;
lastDistillEndTime = timeStep;
}
if(!evaluateCloseDistillery())
{
if (currentState == DistillationResult.STOPPED) {
//can start one step before?
lastDistillEndTime--;
currentState = DistillationResult.WORKING;
}
if (currentState == DistillationResult.STOPNOW) {
currentState = DistillationResult.DISTILLED;
}
}
}
availableDistilledAStates--;
return ret;//already allowed to proceed
}
function checkAvailableDistilledState()
{
return availableDistilledAStates > 0;
}
function resetAvailableDistilledState()
{
availableDistilledAStates = 0;
reachedTimeStep = -1;
currentlyAGenerated = 0;
maxNrAToGenerate = -1;
lastDistillEndTime = -1;
currentState = DistillationResult.WORKING;
resetChartData();
}
function resetChartData()
{
aStatesData.steps = [];
aStatesData.nra = [];
aStatesData.separators = [];
}
function appendChartData(timeStep, activityState, allowOverwrite)
{
var existingIndex = aStatesData.steps.indexOf(timeStep);
//???
var writeState = activityState;
if(activityState == DistillationResult.STOPNOW)
writeState = DistillationResult.DISTILLED;
if(existingIndex == -1)
{
aStatesData.steps.push(timeStep);
aStatesData.nra.push(availableDistilledAStates);
aStatesData.separators.push(writeState);
}
if(existingIndex != -1 && allowOverwrite)
{
aStatesData.steps[existingIndex] = timeStep;
aStatesData.nra[existingIndex] = availableDistilledAStates;
aStatesData.separators[existingIndex] = writeState;
}
}