src/Pure/PIDE/document_status.scala
author wenzelm
Sun, 21 Sep 2025 14:22:14 +0200
changeset 83205 99ce7933db6d
parent 83195 37e1d151be20
child 83206 ca24ee152c80
permissions -rw-r--r--
clarified signature: explicit domain for Nodes_Status operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/PIDE/document_status.scala
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     3
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     4
Document status based on markup information.
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     6
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     8
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    10
object Document_Status {
83161
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    11
  /* theory status: via 'theory' or 'end' commands */
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    12
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    13
  object Theory_Status extends Enumeration {
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    14
    val NONE, INITIALIZED, FINALIZED, CONSOLIDATING, CONSOLIDATED = Value
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    15
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    16
    def initialized(t: Value): Boolean = t >= INITIALIZED
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    17
    def finalized(t: Value): Boolean = t >= FINALIZED
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    18
    def consolidating(t: Value): Boolean = t >= CONSOLIDATING
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    19
    def consolidated(t: Value): Boolean = t >= CONSOLIDATED
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    20
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    21
    def merge(t1: Value, t2: Value): Value = if (t1 >= t2) t1 else t2
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    22
  }
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    23
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    24
  trait Theory_Status {
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    25
    def theory_status: Theory_Status.Value
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    26
    def initialized: Boolean = Theory_Status.initialized(theory_status)
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    27
    def finalized: Boolean = Theory_Status.finalized(theory_status)
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    28
    def consolidating: Boolean = Theory_Status.consolidating(theory_status)
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    29
    def consolidated: Boolean = Theory_Status.consolidated(theory_status)
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    30
  }
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    31
83161
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
    32
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    33
  /* command status */
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    34
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    35
  object Command_Status {
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    36
    val proper_elements: Markup.Elements =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    37
      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
    38
        Markup.FINISHED, Markup.FAILED, Markup.CANCELED)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    39
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    40
    val liberal_elements: Markup.Elements =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    41
      proper_elements + Markup.WARNING + Markup.LEGACY + Markup.ERROR
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    42
83157
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    43
    def make(
83185
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
    44
      markups: List[Markup] = Nil,
83157
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    45
      warned: Boolean = false,
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    46
      failed: Boolean = false
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    47
    ): Command_Status = {
83158
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    48
      var theory_status = Theory_Status.NONE
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    49
      var touched = false
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    50
      var accepted = false
83157
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    51
      var warned1 = warned
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    52
      var failed1 = failed
68871
f5c76072db55 more explicit status for "canceled" command within theory node;
wenzelm
parents: 68770
diff changeset
    53
      var canceled = false
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    54
      var forks = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    55
      var runs = 0
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
    56
      var timing = Timing.zero
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
    57
      for (markup <- markups) {
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    58
        markup.name match {
83158
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    59
          case Markup.INITIALIZED =>
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    60
            theory_status = Theory_Status.merge(theory_status, Theory_Status.INITIALIZED)
83160
bc86832bd2fd clarified Theory_Status.FINALIZED: it belongs to this linear row, too;
wenzelm
parents: 83158
diff changeset
    61
          case Markup.FINALIZED =>
bc86832bd2fd clarified Theory_Status.FINALIZED: it belongs to this linear row, too;
wenzelm
parents: 83158
diff changeset
    62
            theory_status = Theory_Status.merge(theory_status, Theory_Status.FINALIZED)
83158
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    63
          case Markup.CONSOLIDATING =>
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    64
            theory_status = Theory_Status.merge(theory_status, Theory_Status.CONSOLIDATING)
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    65
          case Markup.CONSOLIDATED =>
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    66
            theory_status = Theory_Status.merge(theory_status, Theory_Status.CONSOLIDATED)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    67
          case Markup.ACCEPTED => accepted = true
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    68
          case Markup.FORKED => touched = true; forks += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    69
          case Markup.JOINED => forks -= 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    70
          case Markup.RUNNING => touched = true; runs += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    71
          case Markup.FINISHED => runs -= 1
83157
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    72
          case Markup.WARNING | Markup.LEGACY => warned1 = true
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    73
          case Markup.FAILED | Markup.ERROR => failed1 = true
68871
f5c76072db55 more explicit status for "canceled" command within theory node;
wenzelm
parents: 68770
diff changeset
    74
          case Markup.CANCELED => canceled = true
83195
wenzelm
parents: 83189
diff changeset
    75
          case Markup.TIMING => timing += Markup.Timing_Properties.get(markup.properties)
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
    76
          case _ =>
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
    77
        }
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    78
      }
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
    79
      new Command_Status(
83158
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
    80
        theory_status = theory_status,
68887
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
    81
        touched = touched,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
    82
        accepted = accepted,
83157
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    83
        warned = warned1,
dc54c60492c3 minor performance tuning;
wenzelm
parents: 83156
diff changeset
    84
        failed = failed1,
68887
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
    85
        canceled = canceled,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
    86
        forks = forks,
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
    87
        runs = runs,
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
    88
        timing = timing)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    89
    }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    90
83183
6e03fb945baf more scalable Command.State.document_status: prefer incremental update;
wenzelm
parents: 83181
diff changeset
    91
    val empty: Command_Status = make()
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    92
83156
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
    93
    def merge(args: IterableOnce[Command_Status]): Command_Status =
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
    94
      args.iterator.foldLeft(empty)(_ + _)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    95
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    96
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
    97
  final class Command_Status private(
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
    98
    val theory_status: Theory_Status.Value,
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    99
    private val touched: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   100
    private val accepted: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   101
    private val warned: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   102
    private val failed: Boolean,
68871
f5c76072db55 more explicit status for "canceled" command within theory node;
wenzelm
parents: 68770
diff changeset
   103
    private val canceled: Boolean,
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
   104
    val forks: Int,
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   105
    val runs: Int,
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   106
    val timing: Timing
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
   107
  ) extends Theory_Status {
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
   108
    override def toString: String =
83156
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   109
      if (is_empty) "Command_Status.empty"
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   110
      else if (failed) "Command_Status(failed)"
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
   111
      else if (warned) "Command_Status(warned)"
83156
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   112
      else "Command_Status(...)"
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   113
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   114
    def is_empty: Boolean =
83161
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
   115
      !Theory_Status.initialized(theory_status) &&
83160
bc86832bd2fd clarified Theory_Status.FINALIZED: it belongs to this linear row, too;
wenzelm
parents: 83158
diff changeset
   116
      !touched && !accepted && !warned && !failed && !canceled &&
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   117
      forks == 0 && runs == 0 && timing.is_zero
83155
92063df67f2b clarified signature;
wenzelm
parents: 83154
diff changeset
   118
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   119
    def + (that: Command_Status): Command_Status =
83156
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   120
      if (is_empty) that
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   121
      else if (that.is_empty) this
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   122
      else {
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   123
        new Command_Status(
83161
87ceb18f23f3 clarified modules;
wenzelm
parents: 83160
diff changeset
   124
          theory_status = Theory_Status.merge(theory_status, that.theory_status),
83156
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   125
          touched = touched || that.touched,
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   126
          accepted = accepted || that.accepted,
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   127
          warned = warned || that.warned,
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   128
          failed = failed || that.failed,
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   129
          canceled = canceled || that.canceled,
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   130
          forks = forks + that.forks,
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   131
          runs = runs + that.runs,
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   132
          timing = timing + that.timing)
83156
6bc5835bc794 clarified signature, following e.g. Command.Markups;
wenzelm
parents: 83155
diff changeset
   133
      }
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   134
83185
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   135
    def update(
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   136
      markups: List[Markup] = Nil,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   137
      warned: Boolean = false,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   138
      failed: Boolean = false
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   139
    ): Command_Status = {
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   140
      if (markups.isEmpty) {
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   141
        val warned1 = this.warned || warned
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   142
        val failed1 = this.failed || failed
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   143
        if (this.warned == warned1 && this.failed == failed1) this
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   144
        else {
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   145
          new Command_Status(
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   146
            theory_status = theory_status,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   147
            touched = touched,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   148
            accepted = accepted,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   149
            warned = warned1,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   150
            failed = failed1,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   151
            canceled = canceled,
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   152
            forks = forks,
83186
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   153
            runs = runs,
887f1eac24d1 more scalable timing, as part of incremental document_status;
wenzelm
parents: 83185
diff changeset
   154
            timing = timing)
83185
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   155
        }
83184
9e05d3acd2b4 tuned signature: more explicit operations;
wenzelm
parents: 83183
diff changeset
   156
      }
83185
47edc6384e7a tuned signature: more explicit operations;
wenzelm
parents: 83184
diff changeset
   157
      else this + Command_Status.make(markups = markups, warned = warned, failed = failed)
83184
9e05d3acd2b4 tuned signature: more explicit operations;
wenzelm
parents: 83183
diff changeset
   158
    }
9e05d3acd2b4 tuned signature: more explicit operations;
wenzelm
parents: 83183
diff changeset
   159
83158
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
   160
    def maybe_consolidated: Boolean = touched && forks == 0 && runs == 0
7e94f31b6d6c clarified signature: more explicit type Theory_Status;
wenzelm
parents: 83157
diff changeset
   161
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   162
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   163
    def is_running: Boolean = runs != 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   164
    def is_warned: Boolean = warned
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   165
    def is_failed: Boolean = failed
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   166
    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
   167
    def is_canceled: Boolean = canceled
68881
d975449b266e more detailed node_status;
wenzelm
parents: 68873
diff changeset
   168
    def is_terminated: Boolean = canceled || touched && forks == 0 && runs == 0
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   169
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   170
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   171
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   172
  /* node status */
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   173
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   174
  object Node_Status {
83167
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   175
    val empty: Node_Status = Node_Status()
76716
a7602257a825 clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents: 76714
diff changeset
   176
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   177
    def make(
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   178
      state: Document.State,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   179
      version: Document.Version,
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   180
      name: Document.Node.Name,
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   181
      threshold: Time = Time.max
76714
95a926d483c5 tuned whitespace;
wenzelm
parents: 75393
diff changeset
   182
    ): Node_Status = {
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   183
      var unprocessed = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   184
      var running = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   185
      var warned = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   186
      var failed = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   187
      var finished = 0
68871
f5c76072db55 more explicit status for "canceled" command within theory node;
wenzelm
parents: 68770
diff changeset
   188
      var canceled = false
68892
dce6cbd3cafc proper polarity of terminated status;
wenzelm
parents: 68889
diff changeset
   189
      var terminated = true
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   190
      var total_time = Time.zero
83189
5e2076fb2eff more informative Node_Status;
wenzelm
parents: 83188
diff changeset
   191
      var max_time = Time.zero
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   192
      var command_timings = Map.empty[Command, Time]
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
   193
      var theory_status = Document_Status.Theory_Status.NONE
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   194
83178
952c1ca9b842 clarified signature: more uniform Node_Status.make vs. Overall_Timing.make;
wenzelm
parents: 83176
diff changeset
   195
      for (command <- version.nodes(name).commands.iterator) {
83164
851f3f9440ef tuned signature;
wenzelm
parents: 83163
diff changeset
   196
        val status = state.command_status(version, command)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   197
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   198
        if (status.is_running) running += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   199
        else if (status.is_failed) failed += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   200
        else if (status.is_warned) warned += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   201
        else if (status.is_finished) finished += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   202
        else unprocessed += 1
68871
f5c76072db55 more explicit status for "canceled" command within theory node;
wenzelm
parents: 68770
diff changeset
   203
f5c76072db55 more explicit status for "canceled" command within theory node;
wenzelm
parents: 68770
diff changeset
   204
        if (status.is_canceled) canceled = true
68892
dce6cbd3cafc proper polarity of terminated status;
wenzelm
parents: 68889
diff changeset
   205
        if (!status.is_terminated) terminated = false
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
   206
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   207
        val t = state.command_timing(version, command).elapsed
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   208
        total_time += t
83189
5e2076fb2eff more informative Node_Status;
wenzelm
parents: 83188
diff changeset
   209
        if (t > max_time) max_time = t
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   210
        if (t.is_notable(threshold)) command_timings += (command -> t)
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   211
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
   212
        theory_status = Theory_Status.merge(theory_status, status.theory_status)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   213
      }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   214
68887
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   215
      Node_Status(
83163
64c9d94478b8 clarified signature;
wenzelm
parents: 83161
diff changeset
   216
        suppressed = version.nodes.suppressed(name),
68887
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   217
        unprocessed = unprocessed,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   218
        running = running,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   219
        warned = warned,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   220
        failed = failed,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   221
        finished = finished,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   222
        canceled = canceled,
b07735ce02b3 tuned -- more robust against changes;
wenzelm
parents: 68885
diff changeset
   223
        terminated = terminated,
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   224
        total_time = total_time,
83189
5e2076fb2eff more informative Node_Status;
wenzelm
parents: 83188
diff changeset
   225
        max_time = max_time,
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   226
        threshold = threshold,
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   227
        command_timings = command_timings,
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
   228
        theory_status = theory_status)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   229
    }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   230
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   231
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   232
  sealed case class Node_Status(
83167
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   233
    suppressed: Boolean = false,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   234
    unprocessed: Int = 0,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   235
    running: Int = 0,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   236
    warned: Int = 0,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   237
    failed: Int = 0,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   238
    finished: Int = 0,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   239
    canceled: Boolean = false,
84b092fd0f7a clarified signature;
wenzelm
parents: 83165
diff changeset
   240
    terminated: Boolean = false,
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   241
    total_time: Time = Time.zero,
83189
5e2076fb2eff more informative Node_Status;
wenzelm
parents: 83188
diff changeset
   242
    max_time: Time = Time.zero,
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   243
    threshold: Time = Time.zero,
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   244
    command_timings: Map[Command, Time] = Map.empty,
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   245
    theory_status: Theory_Status.Value = Theory_Status.NONE,
83165
9f3f723938fc more uniform Command_Status.theory_status vs. Node_Status.theory_status: cover all command_states, without special cases;
wenzelm
parents: 83164
diff changeset
   246
  ) extends Theory_Status {
76716
a7602257a825 clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents: 76714
diff changeset
   247
    def is_empty: Boolean = this == Node_Status.empty
a7602257a825 clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents: 76714
diff changeset
   248
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   249
    def ok: Boolean = failed == 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   250
    def total: Int = unprocessed + running + warned + failed + finished
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   251
83163
64c9d94478b8 clarified signature;
wenzelm
parents: 83161
diff changeset
   252
    def quasi_consolidated: Boolean = !suppressed && !finalized && terminated
68897
bdc38f0fd68c tuned signature;
wenzelm
parents: 68892
diff changeset
   253
68898
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   254
    def percentage: Int =
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   255
      if (consolidated) 100
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   256
      else if (total == 0) 0
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   257
      else (((total - unprocessed).toDouble / total) * 100).toInt min 99
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   258
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   259
    def json: JSON.Object.T =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   260
      JSON.Object("ok" -> ok, "total" -> total, "unprocessed" -> unprocessed,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   261
        "running" -> running, "warned" -> warned, "failed" -> failed, "finished" -> finished,
68898
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   262
        "canceled" -> canceled, "consolidated" -> consolidated,
241d08beaf5c more informative node_status;
wenzelm
parents: 68897
diff changeset
   263
        "percentage" -> percentage)
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   264
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   265
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   266
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   267
  /* nodes status */
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   268
83169
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   269
  enum Overall_Status { case ok, failed, pending }
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   270
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   271
  object Nodes_Status {
83205
99ce7933db6d clarified signature: explicit domain for Nodes_Status operations;
wenzelm
parents: 83195
diff changeset
   272
    val empty: Nodes_Status = new Nodes_Status(Map.empty)
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   273
  }
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   274
83205
99ce7933db6d clarified signature: explicit domain for Nodes_Status operations;
wenzelm
parents: 83195
diff changeset
   275
  final class Nodes_Status private(private val rep: Map[Document.Node.Name, Node_Status]) {
68770
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68769
diff changeset
   276
    def is_empty: Boolean = rep.isEmpty
83187
2b9457f5ffef more robust: total "apply", with subtle change of semantics;
wenzelm
parents: 83186
diff changeset
   277
    def apply(name: Document.Node.Name): Node_Status = rep.getOrElse(name, Node_Status.empty)
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   278
    def get(name: Document.Node.Name): Option[Node_Status] = rep.get(name)
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   279
    def iterator: Iterator[(Document.Node.Name, Node_Status)] = rep.iterator
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   280
68884
9b97d0b20d95 clarified quasi_consolidated state: ensure that exports are present for ok nodes;
wenzelm
parents: 68883
diff changeset
   281
    def quasi_consolidated(name: Document.Node.Name): Boolean =
83170
d7796493edb5 tuned signature;
wenzelm
parents: 83169
diff changeset
   282
      get(name) match {
68897
bdc38f0fd68c tuned signature;
wenzelm
parents: 68892
diff changeset
   283
        case Some(st) => st.quasi_consolidated
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68881
diff changeset
   284
        case None => false
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68881
diff changeset
   285
      }
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68881
diff changeset
   286
83169
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   287
    def overall_status(name: Document.Node.Name): Overall_Status =
83170
d7796493edb5 tuned signature;
wenzelm
parents: 83169
diff changeset
   288
      get(name) match {
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   289
        case Some(st) if st.consolidated =>
83169
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   290
          if (st.ok) Overall_Status.ok else Overall_Status.failed
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   291
        case _ => Overall_Status.pending
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   292
      }
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   293
68763
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   294
    def update(
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 68904
diff changeset
   295
      resources: Resources,
68763
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   296
      state: Document.State,
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   297
      version: Document.Version,
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   298
      threshold: Time = Time.max,
68769
59fcff4f8b73 tuned signature;
wenzelm
parents: 68766
diff changeset
   299
      domain: Option[Set[Document.Node.Name]] = None,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   300
      trim: Boolean = false
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   301
    ): (Boolean, Nodes_Status) = {
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68898
diff changeset
   302
      val nodes1 = version.nodes
68763
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   303
      val update_iterator =
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   304
        for {
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68898
diff changeset
   305
          name <- domain.getOrElse(nodes1.domain).iterator
82918
af85ea5d9b20 tuned signature;
wenzelm
parents: 78600
diff changeset
   306
          if !Resources.hidden_node(name) && !resources.loaded_theory(name)
83188
481c3e1cb957 incorporate timing into Node_Status, while the default threshold leads to empty command_timings;
wenzelm
parents: 83187
diff changeset
   307
          st = Document_Status.Node_Status.make(state, version, name, threshold = threshold)
83187
2b9457f5ffef more robust: total "apply", with subtle change of semantics;
wenzelm
parents: 83186
diff changeset
   308
          if apply(name) != st
68763
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   309
        } yield (name -> st)
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   310
      val rep1 = rep ++ update_iterator
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68898
diff changeset
   311
      val rep2 = if (trim) rep1 -- rep1.keysIterator.filterNot(nodes1.domain) else rep1
68764
b523e903d6e4 clarified signature;
wenzelm
parents: 68763
diff changeset
   312
83205
99ce7933db6d clarified signature: explicit domain for Nodes_Status operations;
wenzelm
parents: 83195
diff changeset
   313
      (rep != rep2, new Nodes_Status(rep2))
68763
3c5857c6bc5b clarified signature;
wenzelm
parents: 68761
diff changeset
   314
    }
68761
8bb51b3de39f trim nodes_status: avoid potential memory leak;
wenzelm
parents: 68760
diff changeset
   315
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   316
    override def hashCode: Int = rep.hashCode
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   317
    override def equals(that: Any): Boolean =
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   318
      that match {
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   319
        case other: Nodes_Status => rep == other.rep
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   320
        case _ => false
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   321
      }
68765
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   322
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   323
    override def toString: String = {
68765
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   324
      var ok = 0
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   325
      var failed = 0
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   326
      var pending = 0
68873
13a984eba612 clarified message;
wenzelm
parents: 68871
diff changeset
   327
      var canceled = 0
68765
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   328
      for (name <- rep.keysIterator) {
83169
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   329
        overall_status(name) match {
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   330
          case Overall_Status.ok => ok += 1
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   331
          case Overall_Status.failed => failed += 1
f009f8ac4de1 clarified signature;
wenzelm
parents: 83167
diff changeset
   332
          case Overall_Status.pending => pending += 1
68765
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   333
        }
68873
13a984eba612 clarified message;
wenzelm
parents: 68871
diff changeset
   334
        if (apply(name).canceled) canceled += 1
68765
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   335
      }
68873
13a984eba612 clarified message;
wenzelm
parents: 68871
diff changeset
   336
      "Nodes_Status(ok = " + ok + ", failed = " + failed + ", pending = " + pending +
13a984eba612 clarified message;
wenzelm
parents: 68871
diff changeset
   337
        ", canceled = " + canceled + ")"
68765
be5f255a9943 tuned output;
wenzelm
parents: 68764
diff changeset
   338
    }
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   339
  }
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   340
}