src/Pure/PIDE/protocol.scala
author wenzelm
Mon Aug 20 14:09:09 2012 +0200 (2012-08-20)
changeset 48864 3ee314ae1e0a
parent 48755 393a37003851
child 49009 15381ea111ec
permissions -rw-r--r--
added keyword kind "thy_load" (with optional list of file extensions);
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@47395
    52
    private val accepted: Boolean = false,
wenzelm@46166
    53
    private val finished: Boolean = false,
wenzelm@46166
    54
    private val failed: Boolean = false,
wenzelm@46166
    55
    forks: Int = 0)
wenzelm@38567
    56
  {
wenzelm@47395
    57
    def is_unprocessed: Boolean = accepted && !finished && !failed && forks == 0
wenzelm@46166
    58
    def is_running: Boolean = forks != 0
wenzelm@46166
    59
    def is_finished: Boolean = finished && forks == 0
wenzelm@46166
    60
    def is_failed: Boolean = failed && forks == 0
wenzelm@46227
    61
    def + (that: Status): Status =
wenzelm@47395
    62
      Status(accepted || that.accepted, finished || that.finished,
wenzelm@46910
    63
        failed || that.failed, forks + that.forks)
wenzelm@46166
    64
  }
wenzelm@46166
    65
wenzelm@46166
    66
  val command_status_markup: Set[String] =
wenzelm@47395
    67
    Set(Isabelle_Markup.ACCEPTED, Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED,
wenzelm@46166
    68
      Isabelle_Markup.FORKED, Isabelle_Markup.JOINED)
wenzelm@46166
    69
wenzelm@46166
    70
  def command_status(status: Status, markup: Markup): Status =
wenzelm@46166
    71
    markup match {
wenzelm@47395
    72
      case Markup(Isabelle_Markup.ACCEPTED, _) => status.copy(accepted = true)
wenzelm@46166
    73
      case Markup(Isabelle_Markup.FINISHED, _) => status.copy(finished = true)
wenzelm@46166
    74
      case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
wenzelm@46166
    75
      case Markup(Isabelle_Markup.FORKED, _) => status.copy(forks = status.forks + 1)
wenzelm@46166
    76
      case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
wenzelm@46166
    77
      case _ => status
wenzelm@38567
    78
    }
wenzelm@46166
    79
wenzelm@46166
    80
  def command_status(markups: List[Markup]): Status =
wenzelm@46227
    81
    (Status.init /: markups)(command_status(_, _))
wenzelm@38887
    82
wenzelm@46209
    83
wenzelm@46209
    84
  /* node status */
wenzelm@46209
    85
wenzelm@46688
    86
  sealed case class Node_Status(
wenzelm@46688
    87
    unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
wenzelm@44866
    88
  {
wenzelm@46688
    89
    def total: Int = unprocessed + running + finished + warned + failed
wenzelm@44866
    90
  }
wenzelm@44613
    91
wenzelm@44613
    92
  def node_status(
wenzelm@44613
    93
    state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
wenzelm@44613
    94
  {
wenzelm@44613
    95
    var unprocessed = 0
wenzelm@44613
    96
    var running = 0
wenzelm@44613
    97
    var finished = 0
wenzelm@46688
    98
    var warned = 0
wenzelm@44613
    99
    var failed = 0
wenzelm@44613
   100
    node.commands.foreach(command =>
wenzelm@46166
   101
      {
wenzelm@46688
   102
        val st = state.command_state(version, command)
wenzelm@46688
   103
        val status = command_status(st.status)
wenzelm@46166
   104
        if (status.is_running) running += 1
wenzelm@46688
   105
        else if (status.is_finished) {
wenzelm@46688
   106
          if (st.results.exists(p => is_warning(p._2))) warned += 1
wenzelm@46688
   107
          else finished += 1
wenzelm@46688
   108
        }
wenzelm@46166
   109
        else if (status.is_failed) failed += 1
wenzelm@46166
   110
        else unprocessed += 1
wenzelm@44613
   111
      })
wenzelm@46688
   112
    Node_Status(unprocessed, running, finished, warned, failed)
wenzelm@44613
   113
  }
wenzelm@44613
   114
wenzelm@38887
   115
wenzelm@39441
   116
  /* result messages */
wenzelm@39439
   117
wenzelm@39439
   118
  def clean_message(body: XML.Body): XML.Body =
wenzelm@45666
   119
    body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
wenzelm@39439
   120
      { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
wenzelm@39439
   121
wenzelm@39441
   122
  def message_reports(msg: XML.Tree): List[XML.Elem] =
wenzelm@39441
   123
    msg match {
wenzelm@45666
   124
      case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
wenzelm@39441
   125
      case XML.Elem(_, body) => body.flatMap(message_reports)
wenzelm@39441
   126
      case XML.Text(_) => Nil
wenzelm@39441
   127
    }
wenzelm@39441
   128
wenzelm@39439
   129
wenzelm@39511
   130
  /* specific messages */
wenzelm@39511
   131
wenzelm@39625
   132
 def is_tracing(msg: XML.Tree): Boolean =
wenzelm@39622
   133
    msg match {
wenzelm@45666
   134
      case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
wenzelm@39622
   135
      case _ => false
wenzelm@39622
   136
    }
wenzelm@39622
   137
wenzelm@39511
   138
  def is_warning(msg: XML.Tree): Boolean =
wenzelm@39511
   139
    msg match {
wenzelm@45666
   140
      case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
wenzelm@39511
   141
      case _ => false
wenzelm@39511
   142
    }
wenzelm@39511
   143
wenzelm@39511
   144
  def is_error(msg: XML.Tree): Boolean =
wenzelm@39511
   145
    msg match {
wenzelm@45666
   146
      case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
wenzelm@39511
   147
      case _ => false
wenzelm@39511
   148
    }
wenzelm@38887
   149
wenzelm@39441
   150
  def is_state(msg: XML.Tree): Boolean =
wenzelm@39170
   151
    msg match {
wenzelm@45666
   152
      case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
wenzelm@45666
   153
        List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
wenzelm@39170
   154
      case _ => false
wenzelm@39170
   155
    }
wenzelm@39170
   156
wenzelm@47542
   157
  object Sendback
wenzelm@47542
   158
  {
wenzelm@47542
   159
    def unapply(msg: Any): Option[XML.Body] =
wenzelm@47542
   160
      msg match {
wenzelm@47542
   161
        case XML.Elem(Markup(Isabelle_Markup.SENDBACK, _), body) => Some(body)
wenzelm@47542
   162
        case _ => None
wenzelm@47542
   163
      }
wenzelm@47542
   164
  }
wenzelm@47542
   165
wenzelm@39511
   166
wenzelm@39511
   167
  /* reported positions */
wenzelm@39511
   168
wenzelm@39627
   169
  private val include_pos =
wenzelm@45666
   170
    Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
wenzelm@45666
   171
      Isabelle_Markup.POSITION)
wenzelm@39441
   172
wenzelm@39441
   173
  def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
wenzelm@38887
   174
  {
wenzelm@39441
   175
    def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
wenzelm@38887
   176
      tree match {
wenzelm@39172
   177
        case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
wenzelm@39172
   178
        if include_pos(name) && id == command.id =>
wenzelm@39172
   179
          val range = command.decode(raw_range).restrict(command.range)
wenzelm@39441
   180
          body.foldLeft(if (range.is_singularity) set else set + range)(positions)
wenzelm@39441
   181
        case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
wenzelm@39042
   182
        case _ => set
wenzelm@38887
   183
      }
wenzelm@39441
   184
    val set = positions(Set.empty, message)
wenzelm@39170
   185
    if (set.isEmpty && !is_state(message))
wenzelm@39172
   186
      set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
wenzelm@39042
   187
    else set
wenzelm@38887
   188
  }
wenzelm@38412
   189
}
wenzelm@38412
   190
wenzelm@38412
   191
wenzelm@45709
   192
trait Protocol extends Isabelle_Process
wenzelm@38412
   193
{
wenzelm@38412
   194
  /* commands */
wenzelm@38412
   195
wenzelm@44644
   196
  def define_command(command: Command): Unit =
wenzelm@45709
   197
    input("Document.define_command",
wenzelm@48705
   198
      Document.ID(command.id), encode(command.name), encode(command.source))
wenzelm@38412
   199
wenzelm@38412
   200
wenzelm@38417
   201
  /* document versions */
wenzelm@38412
   202
wenzelm@47343
   203
  def discontinue_execution() { input("Document.discontinue_execution") }
wenzelm@47343
   204
wenzelm@47343
   205
  def cancel_execution() { input("Document.cancel_execution") }
wenzelm@44612
   206
wenzelm@44481
   207
  def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
wenzelm@44383
   208
    edits: List[Document.Edit_Command])
wenzelm@38412
   209
  {
wenzelm@44157
   210
    val edits_yxml =
wenzelm@43767
   211
    { import XML.Encode._
wenzelm@44383
   212
      def id: T[Command] = (cmd => long(cmd.id))
wenzelm@46737
   213
      def encode_edit(name: Document.Node.Name)
wenzelm@44979
   214
          : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
wenzelm@44979
   215
        variant(List(
wenzelm@48755
   216
          { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
wenzelm@44979
   217
          { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
wenzelm@48707
   218
          { case Document.Node.Deps(header) =>
wenzelm@46770
   219
              val dir = Isabelle_System.posix_path(name.dir)
wenzelm@48707
   220
              val imports = header.imports.map(_.node)
wenzelm@46770
   221
              // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
wenzelm@48707
   222
              val uses = header.uses
wenzelm@44979
   223
              (Nil,
wenzelm@48707
   224
                pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
wenzelm@48864
   225
                  pair(list(pair(Encode.string,
wenzelm@48864
   226
                    option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
wenzelm@48707
   227
                  pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
wenzelm@48707
   228
                (dir, (name.theory, (imports, (header.keywords, (uses, header.errors))))))) },
wenzelm@44979
   229
          { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
wenzelm@48705
   230
      def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
wenzelm@44979
   231
      {
wenzelm@44979
   232
        val (name, edit) = node_edit
wenzelm@46737
   233
        pair(string, encode_edit(name))(name.node, edit)
wenzelm@44979
   234
      })
wenzelm@48705
   235
      YXML.string_of_body(encode_edits(edits)) }
wenzelm@45709
   236
    input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
wenzelm@38412
   237
  }
wenzelm@43748
   238
wenzelm@44673
   239
  def remove_versions(versions: List[Document.Version])
wenzelm@44673
   240
  {
wenzelm@44673
   241
    val versions_yxml =
wenzelm@44673
   242
      { import XML.Encode._
wenzelm@44673
   243
        YXML.string_of_body(list(long)(versions.map(_.id))) }
wenzelm@45709
   244
    input("Document.remove_versions", versions_yxml)
wenzelm@44673
   245
  }
wenzelm@44673
   246
wenzelm@43748
   247
wenzelm@43748
   248
  /* method invocation service */
wenzelm@43748
   249
wenzelm@43748
   250
  def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
wenzelm@43748
   251
  {
wenzelm@45709
   252
    input("Document.invoke_scala", id, tag.toString, res)
wenzelm@43748
   253
  }
wenzelm@38412
   254
}