src/Pure/PIDE/protocol.scala
author wenzelm
Mon Apr 29 15:47:42 2013 +0200 (2013-04-29)
changeset 51818 517f232e867d
parent 51533 3f6280aedbcc
child 51987 7d8e0e3c553b
permissions -rw-r--r--
clarified module dependencies: avoid Properties and Document introding minimal "PIDE";
wenzelm@45709
     1
/*  Title:      Pure/PIDE/protocol.scala
wenzelm@38412
     2
    Author:     Makarius
wenzelm@38412
     3
wenzelm@45709
     4
Protocol message formats for interactive proof documents.
wenzelm@38412
     5
*/
wenzelm@38412
     6
wenzelm@38412
     7
package isabelle
wenzelm@38412
     8
wenzelm@38412
     9
wenzelm@45709
    10
object Protocol
wenzelm@38412
    11
{
wenzelm@38567
    12
  /* document editing */
wenzelm@38412
    13
wenzelm@44476
    14
  object Assign
wenzelm@44476
    15
  {
wenzelm@44661
    16
    def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
wenzelm@44661
    17
      try {
wenzelm@44661
    18
        import XML.Decode._
wenzelm@44661
    19
        val body = YXML.parse_body(text)
wenzelm@47388
    20
        Some(pair(long, list(pair(long, option(long))))(body))
wenzelm@44661
    21
      }
wenzelm@44661
    22
      catch {
wenzelm@44661
    23
        case ERROR(_) => None
wenzelm@44661
    24
        case _: XML.XML_Atom => None
wenzelm@44661
    25
        case _: XML.XML_Body => None
wenzelm@38412
    26
      }
wenzelm@38412
    27
  }
wenzelm@38567
    28
wenzelm@44676
    29
  object Removed
wenzelm@44676
    30
  {
wenzelm@44676
    31
    def unapply(text: String): Option[List[Document.Version_ID]] =
wenzelm@44676
    32
      try {
wenzelm@44676
    33
        import XML.Decode._
wenzelm@44676
    34
        Some(list(long)(YXML.parse_body(text)))
wenzelm@44676
    35
      }
wenzelm@44676
    36
      catch {
wenzelm@44676
    37
        case ERROR(_) => None
wenzelm@44676
    38
        case _: XML.XML_Atom => None
wenzelm@44676
    39
        case _: XML.XML_Body => None
wenzelm@44676
    40
      }
wenzelm@44676
    41
  }
wenzelm@44676
    42
wenzelm@38567
    43
wenzelm@46209
    44
  /* command status */
wenzelm@38567
    45
wenzelm@46227
    46
  object Status
wenzelm@46227
    47
  {
wenzelm@46227
    48
    val init = Status()
wenzelm@46227
    49
  }
wenzelm@46227
    50
wenzelm@46166
    51
  sealed case class Status(
wenzelm@49036
    52
    private val touched: Boolean = false,
wenzelm@47395
    53
    private val accepted: Boolean = false,
wenzelm@46166
    54
    private val failed: Boolean = false,
wenzelm@49036
    55
    forks: Int = 0,
wenzelm@49036
    56
    runs: Int = 0)
wenzelm@38567
    57
  {
wenzelm@46227
    58
    def + (that: Status): Status =
wenzelm@49036
    59
      Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
wenzelm@49036
    60
        forks + that.forks, runs + that.runs)
wenzelm@49036
    61
wenzelm@49036
    62
    def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
wenzelm@49036
    63
    def is_running: Boolean = runs != 0
wenzelm@49039
    64
    def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
wenzelm@49036
    65
    def is_failed: Boolean = failed
wenzelm@46166
    66
  }
wenzelm@46166
    67
wenzelm@46166
    68
  val command_status_markup: Set[String] =
wenzelm@50201
    69
    Set(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
wenzelm@50201
    70
      Markup.FINISHED, Markup.FAILED)
wenzelm@46166
    71
wenzelm@46166
    72
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    73
    markup match {
wenzelm@50201
    74
      case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@50201
    75
      case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
wenzelm@50201
    76
      case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@50201
    77
      case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
wenzelm@50201
    78
      case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
wenzelm@50201
    79
      case Markup(Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    80
      case _ => status
wenzelm@38567
    81
    }
wenzelm@46166
    82
wenzelm@46166
    83
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    84
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    85
wenzelm@46209
    86
wenzelm@51818
    87
  /* command timing */
wenzelm@51818
    88
wenzelm@51818
    89
  object Command_Timing
wenzelm@51818
    90
  {
wenzelm@51818
    91
    def unapply(props: Properties.T): Option[(Document.ID, isabelle.Timing)] =
wenzelm@51818
    92
      props match {
wenzelm@51818
    93
        case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
wenzelm@51818
    94
          (args, args) match {
wenzelm@51818
    95
            case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
wenzelm@51818
    96
            case _ => None
wenzelm@51818
    97
          }
wenzelm@51818
    98
        case _ => None
wenzelm@51818
    99
      }
wenzelm@51818
   100
  }
wenzelm@51818
   101
wenzelm@51818
   102
wenzelm@46209
   103
  /* node status */
wenzelm@46209
   104
wenzelm@46688
   105
  sealed case class Node_Status(
wenzelm@46688
   106
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
   107
  {
wenzelm@46688
   108
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
   109
  }
wenzelm@44613
   110
wenzelm@44613
   111
  def node_status(
wenzelm@44613
   112
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
   113
  {
wenzelm@44613
   114
    var unprocessed = 0
wenzelm@44613
   115
    var running = 0
wenzelm@44613
   116
    var finished = 0
wenzelm@46688
   117
    var warned = 0
wenzelm@44613
   118
    var failed = 0
wenzelm@44613
   119
    node.commands.foreach(command =>
wenzelm@46166
   120
      {
wenzelm@46688
   121
        val st = state.command_state(version, command)
wenzelm@46688
   122
        val status = command_status(st.status)
wenzelm@46166
   123
        if (status.is_running) running += 1
wenzelm@46688
   124
        else if (status.is_finished) {
wenzelm@50507
   125
          if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   126
          else finished += 1
wenzelm@46688
   127
        }
wenzelm@46166
   128
        else if (status.is_failed) failed += 1
wenzelm@46166
   129
        else unprocessed += 1
wenzelm@44613
   130
      })
wenzelm@46688
   131
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   132
  }
wenzelm@44613
   133
wenzelm@38887
   134
wenzelm@51533
   135
  /* node timing */
wenzelm@51533
   136
wenzelm@51533
   137
  sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
wenzelm@51533
   138
wenzelm@51533
   139
  val empty_node_timing = Node_Timing(0.0, Map.empty)
wenzelm@51533
   140
wenzelm@51533
   141
  def node_timing(
wenzelm@51533
   142
    state: Document.State,
wenzelm@51533
   143
    version: Document.Version,
wenzelm@51533
   144
    node: Document.Node,
wenzelm@51533
   145
    threshold: Double): Node_Timing =
wenzelm@51533
   146
  {
wenzelm@51533
   147
    var total = 0.0
wenzelm@51533
   148
    var commands = Map.empty[Command, Double]
wenzelm@51533
   149
    for {
wenzelm@51533
   150
      command <- node.commands.iterator
wenzelm@51533
   151
      st = state.command_state(version, command)
wenzelm@51533
   152
      command_timing =
wenzelm@51533
   153
        (0.0 /: st.status)({
wenzelm@51533
   154
          case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
wenzelm@51533
   155
          case (timing, _) => timing
wenzelm@51533
   156
        })
wenzelm@51533
   157
    } {
wenzelm@51533
   158
      total += command_timing
wenzelm@51533
   159
      if (command_timing >= threshold) commands += (command -> command_timing)
wenzelm@51533
   160
    }
wenzelm@51533
   161
    Node_Timing(total, commands)
wenzelm@51533
   162
  }
wenzelm@51533
   163
wenzelm@51533
   164
wenzelm@39441
   165
  /* result messages */
wenzelm@39439
   166
wenzelm@50450
   167
  private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   168
wenzelm@39439
   169
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   170
    body filter {
wenzelm@50450
   171
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
wenzelm@50450
   172
      case XML.Elem(Markup(name, _), _) => !clean(name)
wenzelm@49445
   173
      case _ => true
wenzelm@49650
   174
    } map {
wenzelm@49650
   175
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   176
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   177
      case t => t
wenzelm@49650
   178
    }
wenzelm@39439
   179
wenzelm@49445
   180
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   181
    body flatMap {
wenzelm@50450
   182
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   183
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   184
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   185
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   186
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   187
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   188
      case XML.Text(_) => Nil
wenzelm@39441
   189
    }
wenzelm@39441
   190
wenzelm@39439
   191
wenzelm@39511
   192
  /* specific messages */
wenzelm@39511
   193
wenzelm@50500
   194
  def is_inlined(msg: XML.Tree): Boolean =
wenzelm@50500
   195
    !(is_result(msg) || is_tracing(msg) || is_state(msg))
wenzelm@50500
   196
wenzelm@50500
   197
  def is_result(msg: XML.Tree): Boolean =
wenzelm@50500
   198
    msg match {
wenzelm@50500
   199
      case XML.Elem(Markup(Markup.RESULT, _), _) => true
wenzelm@50500
   200
      case _ => false
wenzelm@50500
   201
    }
wenzelm@50500
   202
wenzelm@50157
   203
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   204
    msg match {
wenzelm@50201
   205
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   206
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   207
      case _ => false
wenzelm@39622
   208
    }
wenzelm@39622
   209
wenzelm@50500
   210
  def is_state(msg: XML.Tree): Boolean =
wenzelm@50500
   211
    msg match {
wenzelm@50500
   212
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@50500
   213
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   214
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@50500
   215
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50500
   216
      case _ => false
wenzelm@50500
   217
    }
wenzelm@50500
   218
wenzelm@39511
   219
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   220
    msg match {
wenzelm@50201
   221
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   222
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   223
      case _ => false
wenzelm@39511
   224
    }
wenzelm@39511
   225
wenzelm@39511
   226
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   227
    msg match {
wenzelm@50201
   228
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   229
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   230
      case _ => false
wenzelm@39511
   231
    }
wenzelm@38887
   232
wenzelm@50500
   233
wenzelm@50500
   234
  /* dialogs */
wenzelm@50500
   235
wenzelm@50500
   236
  object Dialog_Args
wenzelm@50500
   237
  {
wenzelm@50500
   238
    def unapply(props: Properties.T): Option[(Document.ID, Long, String)] =
wenzelm@50500
   239
      (props, props, props) match {
wenzelm@50500
   240
        case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
wenzelm@50500
   241
          Some((id, serial, result))
wenzelm@50500
   242
        case _ => None
wenzelm@50500
   243
      }
wenzelm@50500
   244
  }
wenzelm@50500
   245
wenzelm@50500
   246
  object Dialog
wenzelm@50500
   247
  {
wenzelm@50500
   248
    def unapply(tree: XML.Tree): Option[(Document.ID, Long, String)] =
wenzelm@50500
   249
      tree match {
wenzelm@50500
   250
        case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
wenzelm@50500
   251
          Some((id, serial, result))
wenzelm@50500
   252
        case _ => None
wenzelm@50500
   253
      }
wenzelm@50500
   254
  }
wenzelm@50500
   255
wenzelm@50500
   256
  object Dialog_Result
wenzelm@50500
   257
  {
wenzelm@50501
   258
    def apply(id: Document.ID, serial: Long, result: String): XML.Elem =
wenzelm@50501
   259
    {
wenzelm@50501
   260
      val props = Position.Id(id) ::: Markup.Serial(serial)
wenzelm@50501
   261
      XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
wenzelm@50501
   262
    }
wenzelm@50500
   263
wenzelm@50501
   264
    def unapply(tree: XML.Tree): Option[String] =
wenzelm@50500
   265
      tree match {
wenzelm@50501
   266
        case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
wenzelm@50500
   267
        case _ => None
wenzelm@50500
   268
      }
wenzelm@50500
   269
  }
wenzelm@39170
   270
wenzelm@39511
   271
wenzelm@39511
   272
  /* reported positions */
wenzelm@39511
   273
wenzelm@50201
   274
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   275
wenzelm@39441
   276
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   277
  {
wenzelm@49650
   278
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   279
      : Set[Text.Range] =
wenzelm@49650
   280
    {
wenzelm@49650
   281
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   282
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   283
    }
wenzelm@50157
   284
wenzelm@39441
   285
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   286
      tree match {
wenzelm@49650
   287
        case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
wenzelm@49650
   288
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   289
wenzelm@49650
   290
        case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
wenzelm@49650
   291
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   292
wenzelm@49650
   293
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   294
wenzelm@49650
   295
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   296
wenzelm@39042
   297
        case _ => set
wenzelm@38887
   298
      }
wenzelm@50157
   299
wenzelm@39441
   300
    val set = positions(Set.empty, message)
wenzelm@50500
   301
    if (set.isEmpty)
wenzelm@39172
   302
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   303
    else set
wenzelm@38887
   304
  }
wenzelm@38412
   305
}
wenzelm@38412
   306
wenzelm@38412
   307
wenzelm@45709
   308
trait Protocol extends Isabelle_Process
wenzelm@38412
   309
{
wenzelm@38412
   310
  /* commands */
wenzelm@38412
   311
wenzelm@44644
   312
  def define_command(command: Command): Unit =
wenzelm@45709
   313
    input("Document.define_command",
wenzelm@48705
   314
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   315
wenzelm@38412
   316
wenzelm@38417
   317
  /* document versions */
wenzelm@38412
   318
wenzelm@47343
   319
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   320
wenzelm@47343
   321
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   322
wenzelm@44481
   323
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   324
    edits: List[Document.Edit_Command])
wenzelm@38412
   325
  {
wenzelm@44157
   326
    val edits_yxml =
wenzelm@43767
   327
    { import XML.Encode._
wenzelm@44383
   328
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   329
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   330
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   331
        variant(List(
wenzelm@48755
   332
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   333
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   334
          { case Document.Node.Deps(header) =>
wenzelm@46770
   335
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   336
              val imports = header.imports.map(_.node)
wenzelm@50128
   337
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@44979
   338
              (Nil,
wenzelm@48707
   339
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   340
                  pair(list(pair(Encode.string,
wenzelm@48864
   341
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@51294
   342
                  list(Encode.string)))))(
wenzelm@51294
   343
                (dir, (name.theory, (imports, (keywords, header.errors)))))) },
wenzelm@44979
   344
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   345
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   346
      {
wenzelm@44979
   347
        val (name, edit) = node_edit
wenzelm@46737
   348
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   349
      })
wenzelm@48705
   350
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   351
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   352
  }
wenzelm@43748
   353
wenzelm@44673
   354
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   355
  {
wenzelm@44673
   356
    val versions_yxml =
wenzelm@44673
   357
      { import XML.Encode._
wenzelm@44673
   358
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   359
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   360
  }
wenzelm@44673
   361
wenzelm@43748
   362
wenzelm@50498
   363
  /* dialog via document content */
wenzelm@50498
   364
wenzelm@50500
   365
  def dialog_result(serial: Long, result: String)
wenzelm@50498
   366
  {
wenzelm@50500
   367
    input("Document.dialog_result", Properties.Value.Long(serial), result)
wenzelm@50498
   368
  }
wenzelm@50498
   369
wenzelm@50498
   370
wenzelm@43748
   371
  /* method invocation service */
wenzelm@43748
   372
wenzelm@43748
   373
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   374
  {
wenzelm@45709
   375
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   376
  }
wenzelm@38412
   377
}