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