src/Pure/PIDE/protocol.scala
author wenzelm
Wed Dec 12 21:50:42 2012 +0100 (2012-12-12 ago)
changeset 50498 6647ba2775c1
parent 50450 358b6020f8b6
child 50500 c94bba7906d2
permissions -rw-r--r--
support dialog via document content;
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@46209
    87
  /* node status */
wenzelm@46209
    88
wenzelm@46688
    89
  sealed case class Node_Status(
wenzelm@46688
    90
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
    91
  {
wenzelm@46688
    92
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
    93
  }
wenzelm@44613
    94
wenzelm@44613
    95
  def node_status(
wenzelm@44613
    96
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    97
  {
wenzelm@44613
    98
    var unprocessed = 0
wenzelm@44613
    99
    var running = 0
wenzelm@44613
   100
    var finished = 0
wenzelm@46688
   101
    var warned = 0
wenzelm@44613
   102
    var failed = 0
wenzelm@44613
   103
    node.commands.foreach(command =>
wenzelm@46166
   104
      {
wenzelm@46688
   105
        val st = state.command_state(version, command)
wenzelm@46688
   106
        val status = command_status(st.status)
wenzelm@46166
   107
        if (status.is_running) running += 1
wenzelm@46688
   108
        else if (status.is_finished) {
wenzelm@46688
   109
          if (st.results.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   110
          else finished += 1
wenzelm@46688
   111
        }
wenzelm@46166
   112
        else if (status.is_failed) failed += 1
wenzelm@46166
   113
        else unprocessed += 1
wenzelm@44613
   114
      })
wenzelm@46688
   115
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   116
  }
wenzelm@44613
   117
wenzelm@38887
   118
wenzelm@39441
   119
  /* result messages */
wenzelm@39439
   120
wenzelm@50450
   121
  private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
wenzelm@50450
   122
wenzelm@39439
   123
  def clean_message(body: XML.Body): XML.Body =
wenzelm@49445
   124
    body filter {
wenzelm@50450
   125
      case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
wenzelm@50450
   126
      case XML.Elem(Markup(name, _), _) => !clean(name)
wenzelm@49445
   127
      case _ => true
wenzelm@49650
   128
    } map {
wenzelm@49650
   129
      case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
wenzelm@49650
   130
      case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
wenzelm@49650
   131
      case t => t
wenzelm@49650
   132
    }
wenzelm@39439
   133
wenzelm@49445
   134
  def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
wenzelm@49445
   135
    body flatMap {
wenzelm@50450
   136
      case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
wenzelm@50450
   137
        List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
wenzelm@50201
   138
      case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
wenzelm@50201
   139
        List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
wenzelm@49650
   140
      case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
wenzelm@49445
   141
      case XML.Elem(_, ts) => message_reports(props, ts)
wenzelm@39441
   142
      case XML.Text(_) => Nil
wenzelm@39441
   143
    }
wenzelm@39441
   144
wenzelm@39439
   145
wenzelm@39511
   146
  /* specific messages */
wenzelm@39511
   147
wenzelm@50157
   148
  def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   149
    msg match {
wenzelm@50201
   150
      case XML.Elem(Markup(Markup.TRACING, _), _) => true
wenzelm@50201
   151
      case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
wenzelm@39622
   152
      case _ => false
wenzelm@39622
   153
    }
wenzelm@39622
   154
wenzelm@39511
   155
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   156
    msg match {
wenzelm@50201
   157
      case XML.Elem(Markup(Markup.WARNING, _), _) => true
wenzelm@50201
   158
      case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
wenzelm@39511
   159
      case _ => false
wenzelm@39511
   160
    }
wenzelm@39511
   161
wenzelm@39511
   162
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   163
    msg match {
wenzelm@50201
   164
      case XML.Elem(Markup(Markup.ERROR, _), _) => true
wenzelm@50201
   165
      case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
wenzelm@39511
   166
      case _ => false
wenzelm@39511
   167
    }
wenzelm@38887
   168
wenzelm@39441
   169
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   170
    msg match {
wenzelm@50201
   171
      case XML.Elem(Markup(Markup.WRITELN, _),
wenzelm@50201
   172
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@50201
   173
      case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
wenzelm@50201
   174
        List(XML.Elem(Markup(Markup.STATE, _), _))) => true
wenzelm@39170
   175
      case _ => false
wenzelm@39170
   176
    }
wenzelm@39170
   177
wenzelm@39511
   178
wenzelm@39511
   179
  /* reported positions */
wenzelm@39511
   180
wenzelm@50201
   181
  private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
wenzelm@39441
   182
wenzelm@39441
   183
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   184
  {
wenzelm@49650
   185
    def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
wenzelm@49650
   186
      : Set[Text.Range] =
wenzelm@49650
   187
    {
wenzelm@49650
   188
      val range = command.decode(raw_range).restrict(command.range)
wenzelm@49650
   189
      body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@49650
   190
    }
wenzelm@50157
   191
wenzelm@39441
   192
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   193
      tree match {
wenzelm@49650
   194
        case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
wenzelm@49650
   195
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   196
wenzelm@49650
   197
        case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
wenzelm@49650
   198
        if include_pos(name) && id == command.id => elem_positions(range, set, body)
wenzelm@49650
   199
wenzelm@49650
   200
        case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
wenzelm@49650
   201
wenzelm@49650
   202
        case XML.Elem(_, body) => body.foldLeft(set)(positions)
wenzelm@49650
   203
wenzelm@39042
   204
        case _ => set
wenzelm@38887
   205
      }
wenzelm@50157
   206
wenzelm@39441
   207
    val set = positions(Set.empty, message)
wenzelm@39170
   208
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   209
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   210
    else set
wenzelm@38887
   211
  }
wenzelm@38412
   212
}
wenzelm@38412
   213
wenzelm@38412
   214
wenzelm@45709
   215
trait Protocol extends Isabelle_Process
wenzelm@38412
   216
{
wenzelm@38412
   217
  /* commands */
wenzelm@38412
   218
wenzelm@44644
   219
  def define_command(command: Command): Unit =
wenzelm@45709
   220
    input("Document.define_command",
wenzelm@48705
   221
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   222
wenzelm@38412
   223
wenzelm@38417
   224
  /* document versions */
wenzelm@38412
   225
wenzelm@47343
   226
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   227
wenzelm@47343
   228
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   229
wenzelm@44481
   230
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   231
    edits: List[Document.Edit_Command])
wenzelm@38412
   232
  {
wenzelm@44157
   233
    val edits_yxml =
wenzelm@43767
   234
    { import XML.Encode._
wenzelm@44383
   235
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   236
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   237
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   238
        variant(List(
wenzelm@48755
   239
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   240
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   241
          { case Document.Node.Deps(header) =>
wenzelm@46770
   242
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   243
              val imports = header.imports.map(_.node)
wenzelm@50128
   244
              val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
wenzelm@46770
   245
              // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
wenzelm@48707
   246
              val uses = header.uses
wenzelm@44979
   247
              (Nil,
wenzelm@48707
   248
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   249
                  pair(list(pair(Encode.string,
wenzelm@48864
   250
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@48707
   251
                  pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
wenzelm@50128
   252
                (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
wenzelm@44979
   253
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   254
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   255
      {
wenzelm@44979
   256
        val (name, edit) = node_edit
wenzelm@46737
   257
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   258
      })
wenzelm@48705
   259
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   260
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   261
  }
wenzelm@43748
   262
wenzelm@44673
   263
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   264
  {
wenzelm@44673
   265
    val versions_yxml =
wenzelm@44673
   266
      { import XML.Encode._
wenzelm@44673
   267
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   268
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   269
  }
wenzelm@44673
   270
wenzelm@43748
   271
wenzelm@50498
   272
  /* dialog via document content */
wenzelm@50498
   273
wenzelm@50498
   274
  def dialog_result(name: String, result: String)
wenzelm@50498
   275
  {
wenzelm@50498
   276
    input("Document.dialog_result", name, result)
wenzelm@50498
   277
  }
wenzelm@50498
   278
wenzelm@50498
   279
wenzelm@43748
   280
  /* method invocation service */
wenzelm@43748
   281
wenzelm@43748
   282
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   283
  {
wenzelm@45709
   284
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   285
  }
wenzelm@38412
   286
}