src/Pure/PIDE/protocol.scala
author wenzelm
Thu Mar 27 10:43:43 2014 +0100 (2014-03-27)
changeset 56299 8201790fdeb9
parent 56295 a40e67ce4f84
child 56306 0fc032898b05
permissions -rw-r--r--
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
more standard Markup_Tree merge, including trivial cases;
     1 /*  Title:      Pure/PIDE/protocol.scala
     2     Author:     Makarius
     3 
     4 Protocol message formats for interactive proof documents.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 object Protocol
    11 {
    12   /* document editing */
    13 
    14   object Assign_Update
    15   {
    16     def unapply(text: String): Option[(Document_ID.Version, Document.Assign_Update)] =
    17       try {
    18         import XML.Decode._
    19         val body = YXML.parse_body(text)
    20         Some(pair(long, list(pair(long, list(long))))(body))
    21       }
    22       catch {
    23         case ERROR(_) => None
    24         case _: XML.Error => None
    25       }
    26   }
    27 
    28   object Removed
    29   {
    30     def unapply(text: String): Option[List[Document_ID.Version]] =
    31       try {
    32         import XML.Decode._
    33         Some(list(long)(YXML.parse_body(text)))
    34       }
    35       catch {
    36         case ERROR(_) => None
    37         case _: XML.Error => None
    38       }
    39   }
    40 
    41 
    42   /* command status */
    43 
    44   object Status
    45   {
    46     val init = Status()
    47   }
    48 
    49   sealed case class Status(
    50     private val touched: Boolean = false,
    51     private val accepted: Boolean = false,
    52     private val failed: Boolean = false,
    53     forks: Int = 0,
    54     runs: Int = 0)
    55   {
    56     def + (that: Status): Status =
    57       Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
    58         forks + that.forks, runs + that.runs)
    59 
    60     def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
    61     def is_running: Boolean = runs != 0
    62     def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
    63     def is_failed: Boolean = failed
    64   }
    65 
    66   def command_status(status: Status, markup: Markup): Status =
    67     markup match {
    68       case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
    69       case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
    70       case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    71       case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
    72       case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
    73       case Markup(Markup.FAILED, _) => status.copy(failed = true)
    74       case _ => status
    75     }
    76 
    77   def command_status(markups: List[Markup]): Status =
    78     (Status.init /: markups)(command_status(_, _))
    79 
    80   val command_status_elements =
    81     Document.Elements(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
    82       Markup.FINISHED, Markup.FAILED)
    83 
    84   val status_elements =
    85     command_status_elements + Markup.WARNING + Markup.ERROR
    86 
    87 
    88   /* command timing */
    89 
    90   object Command_Timing
    91   {
    92     def unapply(props: Properties.T): Option[(Document_ID.Generic, isabelle.Timing)] =
    93       props match {
    94         case (Markup.FUNCTION, Markup.COMMAND_TIMING) :: args =>
    95           (args, args) match {
    96             case (Position.Id(id), Markup.Timing_Properties(timing)) => Some((id, timing))
    97             case _ => None
    98           }
    99         case _ => None
   100       }
   101   }
   102 
   103 
   104   /* node status */
   105 
   106   sealed case class Node_Status(
   107     unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
   108   {
   109     def total: Int = unprocessed + running + finished + warned + failed
   110   }
   111 
   112   def node_status(
   113     state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
   114   {
   115     var unprocessed = 0
   116     var running = 0
   117     var finished = 0
   118     var warned = 0
   119     var failed = 0
   120     for {
   121       command <- node.commands
   122       st <- state.command_states(version, command)
   123       status = command_status(st.status)
   124     } {
   125       if (status.is_running) running += 1
   126       else if (status.is_finished) {
   127         if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
   128         else finished += 1
   129       }
   130       else if (status.is_failed) failed += 1
   131       else unprocessed += 1
   132     }
   133     Node_Status(unprocessed, running, finished, warned, failed)
   134   }
   135 
   136 
   137   /* node timing */
   138 
   139   sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
   140 
   141   val empty_node_timing = Node_Timing(0.0, Map.empty)
   142 
   143   def node_timing(
   144     state: Document.State,
   145     version: Document.Version,
   146     node: Document.Node,
   147     threshold: Double): Node_Timing =
   148   {
   149     var total = 0.0
   150     var commands = Map.empty[Command, Double]
   151     for {
   152       command <- node.commands.iterator
   153       st <- state.command_states(version, command)
   154       command_timing =
   155         (0.0 /: st.status)({
   156           case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
   157           case (timing, _) => timing
   158         })
   159     } {
   160       total += command_timing
   161       if (command_timing >= threshold) commands += (command -> command_timing)
   162     }
   163     Node_Timing(total, commands)
   164   }
   165 
   166 
   167   /* result messages */
   168 
   169   private val clean_elements =
   170     Document.Elements(Markup.REPORT, Markup.NO_REPORT)
   171 
   172   def clean_message(body: XML.Body): XML.Body =
   173     body filter {
   174       case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean_elements(name)
   175       case XML.Elem(Markup(name, _), _) => !clean_elements(name)
   176       case _ => true
   177     } map {
   178       case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
   179       case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
   180       case t => t
   181     }
   182 
   183   def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
   184     body flatMap {
   185       case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
   186         List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
   187       case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
   188         List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
   189       case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
   190       case XML.Elem(_, ts) => message_reports(props, ts)
   191       case XML.Text(_) => Nil
   192     }
   193 
   194 
   195   /* specific messages */
   196 
   197   def is_inlined(msg: XML.Tree): Boolean =
   198     !(is_result(msg) || is_tracing(msg) || is_state(msg))
   199 
   200   def is_result(msg: XML.Tree): Boolean =
   201     msg match {
   202       case XML.Elem(Markup(Markup.RESULT, _), _) => true
   203       case _ => false
   204     }
   205 
   206   def is_tracing(msg: XML.Tree): Boolean =
   207     msg match {
   208       case XML.Elem(Markup(Markup.TRACING, _), _) => true
   209       case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
   210       case _ => false
   211     }
   212 
   213   def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
   214     msg match {
   215       case XML.Elem(Markup(Markup.WRITELN, _),
   216         List(XML.Elem(markup, _))) => markup.name == name
   217       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   218         List(XML.Elem(markup, _))) => markup.name == name
   219       case _ => false
   220     }
   221 
   222   def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
   223   def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
   224 
   225   def is_warning(msg: XML.Tree): Boolean =
   226     msg match {
   227       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   228       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   229       case _ => false
   230     }
   231 
   232   def is_error(msg: XML.Tree): Boolean =
   233     msg match {
   234       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   235       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   236       case _ => false
   237     }
   238 
   239 
   240   /* dialogs */
   241 
   242   object Dialog_Args
   243   {
   244     def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
   245       (props, props, props) match {
   246         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   247           Some((id, serial, result))
   248         case _ => None
   249       }
   250   }
   251 
   252   object Dialog
   253   {
   254     def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
   255       tree match {
   256         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   257           Some((id, serial, result))
   258         case _ => None
   259       }
   260   }
   261 
   262   object Dialog_Result
   263   {
   264     def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
   265     {
   266       val props = Position.Id(id) ::: Markup.Serial(serial)
   267       XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
   268     }
   269 
   270     def unapply(tree: XML.Tree): Option[String] =
   271       tree match {
   272         case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
   273         case _ => None
   274       }
   275   }
   276 
   277 
   278   /* reported positions */
   279 
   280   private val position_elements =
   281     Document.Elements(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   282 
   283   def message_positions(
   284     valid_id: Document_ID.Generic => Boolean,
   285     chunk: Command.Chunk,
   286     message: XML.Elem): Set[Text.Range] =
   287   {
   288     def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
   289       props match {
   290         case Position.Reported(id, file_name, symbol_range)
   291         if valid_id(id) && file_name == chunk.file_name =>
   292           chunk.incorporate(symbol_range) match {
   293             case Some(range) => set + range
   294             case _ => set
   295           }
   296         case _ => set
   297       }
   298 
   299     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   300       tree match {
   301         case XML.Wrapped_Elem(Markup(name, props), _, body) =>
   302           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   303         case XML.Elem(Markup(name, props), body) =>
   304           body.foldLeft(if (position_elements(name)) elem_positions(props, set) else set)(positions)
   305         case XML.Text(_) => set
   306       }
   307 
   308     val set = positions(Set.empty, message)
   309     if (set.isEmpty) elem_positions(message.markup.properties, set)
   310     else set
   311   }
   312 }
   313 
   314 
   315 trait Protocol extends Isabelle_Process
   316 {
   317   /* inlined files */
   318 
   319   def define_blob(blob: Document.Blob): Unit =
   320     protocol_command_raw(
   321       "Document.define_blob", Bytes(blob.bytes.sha1_digest.toString), blob.bytes)
   322 
   323 
   324   /* commands */
   325 
   326   def define_command(command: Command): Unit =
   327   {
   328     val blobs_yxml =
   329     { import XML.Encode._
   330       val encode_blob: T[Command.Blob] =
   331         variant(List(
   332           { case Exn.Res((a, b)) =>
   333               (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
   334           { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
   335       YXML.string_of_body(list(encode_blob)(command.blobs))
   336     }
   337     protocol_command("Document.define_command",
   338       Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
   339   }
   340 
   341 
   342   /* execution */
   343 
   344   def discontinue_execution(): Unit =
   345     protocol_command("Document.discontinue_execution")
   346 
   347   def cancel_exec(id: Document_ID.Exec): Unit =
   348     protocol_command("Document.cancel_exec", Document_ID(id))
   349 
   350 
   351   /* document versions */
   352 
   353   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   354     edits: List[Document.Edit_Command])
   355   {
   356     val edits_yxml =
   357     { import XML.Encode._
   358       def id: T[Command] = (cmd => long(cmd.id))
   359       def encode_edit(name: Document.Node.Name)
   360           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   361         variant(List(
   362           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   363           { case Document.Node.Deps(header) =>
   364               val master_dir = Isabelle_System.posix_path(name.master_dir)
   365               val imports = header.imports.map(_.node)
   366               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   367               (Nil,
   368                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   369                   pair(list(pair(Encode.string,
   370                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   371                   list(Encode.string)))))(
   372                 (master_dir, (name.theory, (imports, (keywords, header.errors)))))) },
   373           { case Document.Node.Perspective(a, b, c) =>
   374               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   375                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   376       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   377       {
   378         val (name, edit) = node_edit
   379         pair(string, encode_edit(name))(name.node, edit)
   380       })
   381       YXML.string_of_body(encode_edits(edits)) }
   382     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   383   }
   384 
   385   def remove_versions(versions: List[Document.Version])
   386   {
   387     val versions_yxml =
   388       { import XML.Encode._
   389         YXML.string_of_body(list(long)(versions.map(_.id))) }
   390     protocol_command("Document.remove_versions", versions_yxml)
   391   }
   392 
   393 
   394   /* dialog via document content */
   395 
   396   def dialog_result(serial: Long, result: String): Unit =
   397     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   398 }