src/Pure/PIDE/document_status.scala
author wenzelm
Sat, 18 Aug 2018 13:40:23 +0200
changeset 68760 0626cae56b6f
parent 68759 4247e91fa21d
child 68761 8bb51b3de39f
permissions -rw-r--r--
simplified (cf. dcd69422b953);
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
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    10
object Document_Status
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    11
{
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    12
  /* command status */
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    13
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    14
  object Command_Status
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    15
  {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    16
    val proper_elements: Markup.Elements =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    17
      Markup.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    18
        Markup.FINISHED, Markup.FAILED)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    19
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    20
    val liberal_elements: Markup.Elements =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    21
      proper_elements + Markup.WARNING + Markup.LEGACY + Markup.ERROR
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    22
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    23
    def make(markup_iterator: Iterator[Markup]): Command_Status =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    24
    {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    25
      var touched = false
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    26
      var accepted = false
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    27
      var warned = false
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    28
      var failed = false
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    29
      var forks = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    30
      var runs = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    31
      for (markup <- markup_iterator) {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    32
        markup.name match {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    33
          case Markup.ACCEPTED => accepted = true
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    34
          case Markup.FORKED => touched = true; forks += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    35
          case Markup.JOINED => forks -= 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    36
          case Markup.RUNNING => touched = true; runs += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    37
          case Markup.FINISHED => runs -= 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    38
          case Markup.WARNING | Markup.LEGACY => warned = true
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    39
          case Markup.FAILED | Markup.ERROR => failed = true
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    40
          case _ =>
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    41
        }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    42
      }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    43
      Command_Status(touched, accepted, warned, failed, forks, runs)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    44
    }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    45
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    46
    val empty = make(Iterator.empty)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    47
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    48
    def merge(status_iterator: Iterator[Command_Status]): Command_Status =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    49
      if (status_iterator.hasNext) {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    50
        val status0 = status_iterator.next
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    51
        (status0 /: status_iterator)(_ + _)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    52
      }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    53
      else empty
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    54
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    55
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    56
  sealed case class Command_Status(
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    57
    private val touched: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    58
    private val accepted: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    59
    private val warned: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    60
    private val failed: Boolean,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    61
    forks: Int,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    62
    runs: Int)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    63
  {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    64
    def + (that: Command_Status): Command_Status =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    65
      Command_Status(
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    66
        touched || that.touched,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    67
        accepted || that.accepted,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    68
        warned || that.warned,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    69
        failed || that.failed,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    70
        forks + that.forks,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    71
        runs + that.runs)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    72
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    73
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    74
    def is_running: Boolean = runs != 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    75
    def is_warned: Boolean = warned
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    76
    def is_failed: Boolean = failed
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    77
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    78
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    79
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    80
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    81
  /* node status */
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    82
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    83
  object Node_Status
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    84
  {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    85
    def make(
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    86
      state: Document.State,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    87
      version: Document.Version,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    88
      name: Document.Node.Name): Node_Status =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    89
    {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    90
      val node = version.nodes(name)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    91
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    92
      var unprocessed = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    93
      var running = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    94
      var warned = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    95
      var failed = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    96
      var finished = 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    97
      for (command <- node.commands.iterator) {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    98
        val states = state.command_states(version, command)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
    99
        val status = Command_Status.merge(states.iterator.map(_.document_status))
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   100
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   101
        if (status.is_running) running += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   102
        else if (status.is_failed) failed += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   103
        else if (status.is_warned) warned += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   104
        else if (status.is_finished) finished += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   105
        else unprocessed += 1
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   106
      }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   107
      val initialized = state.node_initialized(version, name)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   108
      val consolidated = state.node_consolidated(version, name)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   109
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   110
      Node_Status(unprocessed, running, warned, failed, finished, initialized, consolidated)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   111
    }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   112
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   113
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   114
  sealed case class Node_Status(
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   115
    unprocessed: Int, running: Int, warned: Int, failed: Int, finished: Int,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   116
    initialized: Boolean, consolidated: Boolean)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   117
  {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   118
    def ok: Boolean = failed == 0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   119
    def total: Int = unprocessed + running + warned + failed + finished
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   120
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   121
    def json: JSON.Object.T =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   122
      JSON.Object("ok" -> ok, "total" -> total, "unprocessed" -> unprocessed,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   123
        "running" -> running, "warned" -> warned, "failed" -> failed, "finished" -> finished,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   124
        "initialized" -> initialized, "consolidated" -> consolidated)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   125
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   126
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   127
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   128
  /* node timing */
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   129
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   130
  object Node_Timing
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   131
  {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   132
    val empty: Node_Timing = Node_Timing(0.0, Map.empty)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   133
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   134
    def make(
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   135
      state: Document.State,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   136
      version: Document.Version,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   137
      node: Document.Node,
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   138
      threshold: Double): Node_Timing =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   139
    {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   140
      var total = 0.0
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   141
      var commands = Map.empty[Command, Double]
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   142
      for {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   143
        command <- node.commands.iterator
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   144
        st <- state.command_states(version, command)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   145
      } {
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   146
        val command_timing =
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   147
          (0.0 /: st.status)({
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   148
            case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   149
            case (timing, _) => timing
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   150
          })
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   151
        total += command_timing
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   152
        if (command_timing >= threshold) commands += (command -> command_timing)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   153
      }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   154
      Node_Timing(total, commands)
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   155
    }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   156
  }
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   157
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   158
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
68759
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   159
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   160
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   161
  /* nodes status */
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   162
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   163
  object Overall_Node_Status extends Enumeration
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   164
  {
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   165
    val ok, failed, pending = Value
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   166
  }
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   167
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   168
  object Nodes_Status
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   169
  {
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   170
    val empty: Nodes_Status = new Nodes_Status(Map.empty)
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   171
  }
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   172
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   173
  final class Nodes_Status private(private val rep: Map[Document.Node.Name, Node_Status])
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   174
  {
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   175
    def defined(name: Document.Node.Name): Boolean = rep.isDefinedAt(name)
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   176
    def get(name: Document.Node.Name): Option[Node_Status] = rep.get(name)
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   177
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   178
    def overall_node_status(name: Document.Node.Name): Overall_Node_Status.Value =
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   179
      rep.get(name) match {
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   180
        case Some(st) if st.consolidated =>
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   181
          if (st.ok) Overall_Node_Status.ok else Overall_Node_Status.failed
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   182
        case _ => Overall_Node_Status.pending
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   183
      }
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   184
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   185
    def + (entry: (Document.Node.Name, Node_Status)): Nodes_Status =
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   186
      new Nodes_Status(rep + entry)
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   187
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   188
    override def hashCode: Int = rep.hashCode
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   189
    override def equals(that: Any): Boolean =
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   190
      that match {
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   191
        case other: Nodes_Status => rep == other.rep
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   192
        case _ => false
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   193
      }
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   194
    override def toString: String = rep.iterator.mkString("Nodes_Status(", ", ", ")")
4247e91fa21d clarified modules;
wenzelm
parents: 68758
diff changeset
   195
  }
68758
a110e7e24e55 clarified modules;
wenzelm
parents:
diff changeset
   196
}