author | wenzelm |
Mon, 14 Oct 2019 21:00:04 +0200 | |
changeset 70867 | 4c8e28dabbc4 |
parent 70658 | 4655897b8287 |
child 71601 | 97ccf48c2f0c |
permissions | -rw-r--r-- |
68758 | 1 |
/* Title: Pure/PIDE/document_status.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Document status based on markup information. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Document_Status |
|
11 |
{ |
|
12 |
/* command status */ |
|
13 |
||
14 |
object Command_Status |
|
15 |
{ |
|
16 |
val proper_elements: Markup.Elements = |
|
17 |
Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING, |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
18 |
Markup.FINISHED, Markup.FAILED, Markup.CANCELED) |
68758 | 19 |
|
20 |
val liberal_elements: Markup.Elements = |
|
21 |
proper_elements + Markup.WARNING + Markup.LEGACY + Markup.ERROR |
|
22 |
||
23 |
def make(markup_iterator: Iterator[Markup]): Command_Status = |
|
24 |
{ |
|
25 |
var touched = false |
|
26 |
var accepted = false |
|
27 |
var warned = false |
|
28 |
var failed = false |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
29 |
var canceled = false |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
30 |
var finalized = false |
68758 | 31 |
var forks = 0 |
32 |
var runs = 0 |
|
33 |
for (markup <- markup_iterator) { |
|
34 |
markup.name match { |
|
35 |
case Markup.ACCEPTED => accepted = true |
|
36 |
case Markup.FORKED => touched = true; forks += 1 |
|
37 |
case Markup.JOINED => forks -= 1 |
|
38 |
case Markup.RUNNING => touched = true; runs += 1 |
|
39 |
case Markup.FINISHED => runs -= 1 |
|
40 |
case Markup.WARNING | Markup.LEGACY => warned = true |
|
41 |
case Markup.FAILED | Markup.ERROR => failed = true |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
42 |
case Markup.CANCELED => canceled = true |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
43 |
case Markup.FINALIZED => finalized = true |
68758 | 44 |
case _ => |
45 |
} |
|
46 |
} |
|
68887 | 47 |
Command_Status( |
48 |
touched = touched, |
|
49 |
accepted = accepted, |
|
50 |
warned = warned, |
|
51 |
failed = failed, |
|
52 |
canceled = canceled, |
|
53 |
finalized = finalized, |
|
54 |
forks = forks, |
|
55 |
runs = runs) |
|
68758 | 56 |
} |
57 |
||
58 |
val empty = make(Iterator.empty) |
|
59 |
||
60 |
def merge(status_iterator: Iterator[Command_Status]): Command_Status = |
|
61 |
if (status_iterator.hasNext) { |
|
62 |
val status0 = status_iterator.next |
|
63 |
(status0 /: status_iterator)(_ + _) |
|
64 |
} |
|
65 |
else empty |
|
66 |
} |
|
67 |
||
68 |
sealed case class Command_Status( |
|
69 |
private val touched: Boolean, |
|
70 |
private val accepted: Boolean, |
|
71 |
private val warned: Boolean, |
|
72 |
private val failed: Boolean, |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
73 |
private val canceled: Boolean, |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
74 |
private val finalized: Boolean, |
68758 | 75 |
forks: Int, |
76 |
runs: Int) |
|
77 |
{ |
|
78 |
def + (that: Command_Status): Command_Status = |
|
79 |
Command_Status( |
|
68887 | 80 |
touched = touched || that.touched, |
81 |
accepted = accepted || that.accepted, |
|
82 |
warned = warned || that.warned, |
|
83 |
failed = failed || that.failed, |
|
84 |
canceled = canceled || that.canceled, |
|
85 |
finalized = finalized || that.finalized, |
|
86 |
forks = forks + that.forks, |
|
87 |
runs = runs + that.runs) |
|
68758 | 88 |
|
89 |
def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0)) |
|
90 |
def is_running: Boolean = runs != 0 |
|
91 |
def is_warned: Boolean = warned |
|
92 |
def is_failed: Boolean = failed |
|
93 |
def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0 |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
94 |
def is_canceled: Boolean = canceled |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
95 |
def is_finalized: Boolean = finalized |
68881 | 96 |
def is_terminated: Boolean = canceled || touched && forks == 0 && runs == 0 |
68758 | 97 |
} |
98 |
||
99 |
||
100 |
/* node status */ |
|
101 |
||
102 |
object Node_Status |
|
103 |
{ |
|
104 |
def make( |
|
105 |
state: Document.State, |
|
106 |
version: Document.Version, |
|
107 |
name: Document.Node.Name): Node_Status = |
|
108 |
{ |
|
109 |
val node = version.nodes(name) |
|
110 |
||
111 |
var unprocessed = 0 |
|
112 |
var running = 0 |
|
113 |
var warned = 0 |
|
114 |
var failed = 0 |
|
115 |
var finished = 0 |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
116 |
var canceled = false |
68892 | 117 |
var terminated = true |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
118 |
var finalized = false |
68758 | 119 |
for (command <- node.commands.iterator) { |
120 |
val states = state.command_states(version, command) |
|
121 |
val status = Command_Status.merge(states.iterator.map(_.document_status)) |
|
122 |
||
123 |
if (status.is_running) running += 1 |
|
124 |
else if (status.is_failed) failed += 1 |
|
125 |
else if (status.is_warned) warned += 1 |
|
126 |
else if (status.is_finished) finished += 1 |
|
127 |
else unprocessed += 1 |
|
68871
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
128 |
|
f5c76072db55
more explicit status for "canceled" command within theory node;
wenzelm
parents:
68770
diff
changeset
|
129 |
if (status.is_canceled) canceled = true |
68892 | 130 |
if (!status.is_terminated) terminated = false |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
131 |
if (status.is_finalized) finalized = true |
68758 | 132 |
} |
133 |
val initialized = state.node_initialized(version, name) |
|
134 |
val consolidated = state.node_consolidated(version, name) |
|
135 |
||
68887 | 136 |
Node_Status( |
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
137 |
is_suppressed = version.nodes.is_suppressed(name), |
68887 | 138 |
unprocessed = unprocessed, |
139 |
running = running, |
|
140 |
warned = warned, |
|
141 |
failed = failed, |
|
142 |
finished = finished, |
|
143 |
canceled = canceled, |
|
144 |
terminated = terminated, |
|
68889 | 145 |
initialized = initialized, |
68887 | 146 |
finalized = finalized, |
147 |
consolidated = consolidated) |
|
68758 | 148 |
} |
149 |
} |
|
150 |
||
151 |
sealed case class Node_Status( |
|
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
152 |
is_suppressed: Boolean, |
68887 | 153 |
unprocessed: Int, |
154 |
running: Int, |
|
155 |
warned: Int, |
|
156 |
failed: Int, |
|
157 |
finished: Int, |
|
158 |
canceled: Boolean, |
|
159 |
terminated: Boolean, |
|
68889 | 160 |
initialized: Boolean, |
68887 | 161 |
finalized: Boolean, |
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
162 |
consolidated: Boolean) |
68758 | 163 |
{ |
164 |
def ok: Boolean = failed == 0 |
|
165 |
def total: Int = unprocessed + running + warned + failed + finished |
|
166 |
||
69844
b21ddfa7042b
clarified quasi_consolidated status after 5f160df596c1 -- relevant for headless PIDE session (e.g. "isabelle dump");
wenzelm
parents:
69818
diff
changeset
|
167 |
def quasi_consolidated: Boolean = !is_suppressed && !finalized && terminated |
68897 | 168 |
|
68898 | 169 |
def percentage: Int = |
170 |
if (consolidated) 100 |
|
171 |
else if (total == 0) 0 |
|
172 |
else (((total - unprocessed).toDouble / total) * 100).toInt min 99 |
|
173 |
||
68758 | 174 |
def json: JSON.Object.T = |
175 |
JSON.Object("ok" -> ok, "total" -> total, "unprocessed" -> unprocessed, |
|
176 |
"running" -> running, "warned" -> warned, "failed" -> failed, "finished" -> finished, |
|
68898 | 177 |
"canceled" -> canceled, "consolidated" -> consolidated, |
178 |
"percentage" -> percentage) |
|
68758 | 179 |
} |
180 |
||
181 |
||
69863 | 182 |
/* overall timing */ |
68758 | 183 |
|
69863 | 184 |
object Overall_Timing |
68758 | 185 |
{ |
69863 | 186 |
val empty: Overall_Timing = Overall_Timing(0.0, Map.empty) |
68758 | 187 |
|
188 |
def make( |
|
189 |
state: Document.State, |
|
190 |
version: Document.Version, |
|
69863 | 191 |
commands: Iterable[Command], |
69864 | 192 |
threshold: Double = 0.0): Overall_Timing = |
68758 | 193 |
{ |
194 |
var total = 0.0 |
|
69863 | 195 |
var command_timings = Map.empty[Command, Double] |
68758 | 196 |
for { |
69863 | 197 |
command <- commands.iterator |
68758 | 198 |
st <- state.command_states(version, command) |
199 |
} { |
|
200 |
val command_timing = |
|
201 |
(0.0 /: st.status)({ |
|
202 |
case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds |
|
203 |
case (timing, _) => timing |
|
204 |
}) |
|
205 |
total += command_timing |
|
69864 | 206 |
if (command_timing > 0.0 && command_timing >= threshold) { |
207 |
command_timings += (command -> command_timing) |
|
208 |
} |
|
68758 | 209 |
} |
69863 | 210 |
Overall_Timing(total, command_timings) |
68758 | 211 |
} |
212 |
} |
|
213 |
||
69863 | 214 |
sealed case class Overall_Timing(total: Double, command_timings: Map[Command, Double]) |
69864 | 215 |
{ |
216 |
def command_timing(command: Command): Double = |
|
217 |
command_timings.getOrElse(command, 0.0) |
|
218 |
} |
|
68759 | 219 |
|
220 |
||
221 |
/* nodes status */ |
|
222 |
||
223 |
object Overall_Node_Status extends Enumeration |
|
224 |
{ |
|
225 |
val ok, failed, pending = Value |
|
226 |
} |
|
227 |
||
228 |
object Nodes_Status |
|
229 |
{ |
|
68903 | 230 |
val empty: Nodes_Status = new Nodes_Status(Map.empty, Document.Nodes.empty) |
68759 | 231 |
} |
232 |
||
68903 | 233 |
final class Nodes_Status private( |
234 |
private val rep: Map[Document.Node.Name, Node_Status], |
|
235 |
nodes: Document.Nodes) |
|
68759 | 236 |
{ |
68770
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68769
diff
changeset
|
237 |
def is_empty: Boolean = rep.isEmpty |
add44e2b8cb0
optional notification of nodes_status (via progress);
wenzelm
parents:
68769
diff
changeset
|
238 |
def apply(name: Document.Node.Name): Node_Status = rep(name) |
68759 | 239 |
def get(name: Document.Node.Name): Option[Node_Status] = rep.get(name) |
240 |
||
69818
60d0ee8f2ddb
more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents:
69817
diff
changeset
|
241 |
def present: List[(Document.Node.Name, Node_Status)] = |
69817
5f160df596c1
clarified Node_Status vs. is_suppressed, e.g. relevant for purged nodes in Theories_Dockable after 0626cae56b6f;
wenzelm
parents:
69255
diff
changeset
|
242 |
(for { |
5f160df596c1
clarified Node_Status vs. is_suppressed, e.g. relevant for purged nodes in Theories_Dockable after 0626cae56b6f;
wenzelm
parents:
69255
diff
changeset
|
243 |
name <- nodes.topological_order.iterator |
5f160df596c1
clarified Node_Status vs. is_suppressed, e.g. relevant for purged nodes in Theories_Dockable after 0626cae56b6f;
wenzelm
parents:
69255
diff
changeset
|
244 |
node_status <- get(name) |
5f160df596c1
clarified Node_Status vs. is_suppressed, e.g. relevant for purged nodes in Theories_Dockable after 0626cae56b6f;
wenzelm
parents:
69255
diff
changeset
|
245 |
} yield (name, node_status)).toList |
68903 | 246 |
|
68884
9b97d0b20d95
clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents:
68883
diff
changeset
|
247 |
def quasi_consolidated(name: Document.Node.Name): Boolean = |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68881
diff
changeset
|
248 |
rep.get(name) match { |
68897 | 249 |
case Some(st) => st.quasi_consolidated |
68883
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68881
diff
changeset
|
250 |
case None => false |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68881
diff
changeset
|
251 |
} |
3653b3ad729e
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents:
68881
diff
changeset
|
252 |
|
68759 | 253 |
def overall_node_status(name: Document.Node.Name): Overall_Node_Status.Value = |
254 |
rep.get(name) match { |
|
255 |
case Some(st) if st.consolidated => |
|
256 |
if (st.ok) Overall_Node_Status.ok else Overall_Node_Status.failed |
|
257 |
case _ => Overall_Node_Status.pending |
|
258 |
} |
|
259 |
||
68763 | 260 |
def update( |
69255
800b1ce96fce
more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents:
68904
diff
changeset
|
261 |
resources: Resources, |
68763 | 262 |
state: Document.State, |
263 |
version: Document.Version, |
|
68769 | 264 |
domain: Option[Set[Document.Node.Name]] = None, |
68903 | 265 |
trim: Boolean = false): (Boolean, Nodes_Status) = |
68763 | 266 |
{ |
68903 | 267 |
val nodes1 = version.nodes |
68763 | 268 |
val update_iterator = |
269 |
for { |
|
68903 | 270 |
name <- domain.getOrElse(nodes1.domain).iterator |
69817
5f160df596c1
clarified Node_Status vs. is_suppressed, e.g. relevant for purged nodes in Theories_Dockable after 0626cae56b6f;
wenzelm
parents:
69255
diff
changeset
|
271 |
if !resources.is_hidden(name) && !resources.session_base.loaded_theory(name) |
68763 | 272 |
st = Document_Status.Node_Status.make(state, version, name) |
273 |
if !rep.isDefinedAt(name) || rep(name) != st |
|
274 |
} yield (name -> st) |
|
275 |
val rep1 = rep ++ update_iterator |
|
68903 | 276 |
val rep2 = if (trim) rep1 -- rep1.keysIterator.filterNot(nodes1.domain) else rep1 |
68764 | 277 |
|
68903 | 278 |
(rep != rep2, new Nodes_Status(rep2, nodes1)) |
68763 | 279 |
} |
68761 | 280 |
|
68759 | 281 |
override def hashCode: Int = rep.hashCode |
282 |
override def equals(that: Any): Boolean = |
|
283 |
that match { |
|
284 |
case other: Nodes_Status => rep == other.rep |
|
285 |
case _ => false |
|
286 |
} |
|
68765 | 287 |
|
288 |
override def toString: String = |
|
289 |
{ |
|
290 |
var ok = 0 |
|
291 |
var failed = 0 |
|
292 |
var pending = 0 |
|
68873 | 293 |
var canceled = 0 |
68765 | 294 |
for (name <- rep.keysIterator) { |
295 |
overall_node_status(name) match { |
|
296 |
case Overall_Node_Status.ok => ok += 1 |
|
297 |
case Overall_Node_Status.failed => failed += 1 |
|
298 |
case Overall_Node_Status.pending => pending += 1 |
|
299 |
} |
|
68873 | 300 |
if (apply(name).canceled) canceled += 1 |
68765 | 301 |
} |
68873 | 302 |
"Nodes_Status(ok = " + ok + ", failed = " + failed + ", pending = " + pending + |
303 |
", canceled = " + canceled + ")" |
|
68765 | 304 |
} |
68759 | 305 |
} |
68758 | 306 |
} |