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