src/Pure/PIDE/protocol.scala
author wenzelm
Tue Nov 19 22:12:54 2013 +0100 (2013-11-19)
changeset 54524 14609d36cab8
parent 54519 5fed81762406
child 54526 92961f196d9e
permissions -rw-r--r--
more explicit indication of missing files;
     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   val command_status_markup: Set[String] =
    67     Set(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
    68       Markup.FINISHED, Markup.FAILED)
    69 
    70   def command_status(status: Status, markup: Markup): Status =
    71     markup match {
    72       case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
    73       case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
    74       case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    75       case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
    76       case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
    77       case Markup(Markup.FAILED, _) => status.copy(failed = true)
    78       case _ => status
    79     }
    80 
    81   def command_status(markups: List[Markup]): Status =
    82     (Status.init /: markups)(command_status(_, _))
    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     node.commands.foreach(command =>
   118       {
   119         val st = state.command_state(version, command)
   120         val status = command_status(st.status)
   121         if (status.is_running) running += 1
   122         else if (status.is_finished) {
   123           if (st.results.entries.exists(p => is_warning(p._2))) warned += 1
   124           else finished += 1
   125         }
   126         else if (status.is_failed) failed += 1
   127         else unprocessed += 1
   128       })
   129     Node_Status(unprocessed, running, finished, warned, failed)
   130   }
   131 
   132 
   133   /* node timing */
   134 
   135   sealed case class Node_Timing(total: Double, commands: Map[Command, Double])
   136 
   137   val empty_node_timing = Node_Timing(0.0, Map.empty)
   138 
   139   def node_timing(
   140     state: Document.State,
   141     version: Document.Version,
   142     node: Document.Node,
   143     threshold: Double): Node_Timing =
   144   {
   145     var total = 0.0
   146     var commands = Map.empty[Command, Double]
   147     for {
   148       command <- node.commands.iterator
   149       st = state.command_state(version, command)
   150       command_timing =
   151         (0.0 /: st.status)({
   152           case (timing, Markup.Timing(t)) => timing + t.elapsed.seconds
   153           case (timing, _) => timing
   154         })
   155     } {
   156       total += command_timing
   157       if (command_timing >= threshold) commands += (command -> command_timing)
   158     }
   159     Node_Timing(total, commands)
   160   }
   161 
   162 
   163   /* result messages */
   164 
   165   private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
   166 
   167   def clean_message(body: XML.Body): XML.Body =
   168     body filter {
   169       case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
   170       case XML.Elem(Markup(name, _), _) => !clean(name)
   171       case _ => true
   172     } map {
   173       case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
   174       case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
   175       case t => t
   176     }
   177 
   178   def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
   179     body flatMap {
   180       case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
   181         List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
   182       case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
   183         List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
   184       case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
   185       case XML.Elem(_, ts) => message_reports(props, ts)
   186       case XML.Text(_) => Nil
   187     }
   188 
   189 
   190   /* specific messages */
   191 
   192   def is_inlined(msg: XML.Tree): Boolean =
   193     !(is_result(msg) || is_tracing(msg) || is_state(msg))
   194 
   195   def is_result(msg: XML.Tree): Boolean =
   196     msg match {
   197       case XML.Elem(Markup(Markup.RESULT, _), _) => true
   198       case _ => false
   199     }
   200 
   201   def is_tracing(msg: XML.Tree): Boolean =
   202     msg match {
   203       case XML.Elem(Markup(Markup.TRACING, _), _) => true
   204       case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
   205       case _ => false
   206     }
   207 
   208   def is_writeln_markup(msg: XML.Tree, name: String): Boolean =
   209     msg match {
   210       case XML.Elem(Markup(Markup.WRITELN, _),
   211         List(XML.Elem(markup, _))) => markup.name == name
   212       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   213         List(XML.Elem(markup, _))) => markup.name == name
   214       case _ => false
   215     }
   216 
   217   def is_state(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.STATE)
   218   def is_information(msg: XML.Tree): Boolean = is_writeln_markup(msg, Markup.INFORMATION)
   219 
   220   def is_warning(msg: XML.Tree): Boolean =
   221     msg match {
   222       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   223       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   224       case _ => false
   225     }
   226 
   227   def is_error(msg: XML.Tree): Boolean =
   228     msg match {
   229       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   230       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   231       case _ => false
   232     }
   233 
   234 
   235   /* dialogs */
   236 
   237   object Dialog_Args
   238   {
   239     def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
   240       (props, props, props) match {
   241         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   242           Some((id, serial, result))
   243         case _ => None
   244       }
   245   }
   246 
   247   object Dialog
   248   {
   249     def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
   250       tree match {
   251         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   252           Some((id, serial, result))
   253         case _ => None
   254       }
   255   }
   256 
   257   object Dialog_Result
   258   {
   259     def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
   260     {
   261       val props = Position.Id(id) ::: Markup.Serial(serial)
   262       XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
   263     }
   264 
   265     def unapply(tree: XML.Tree): Option[String] =
   266       tree match {
   267         case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
   268         case _ => None
   269       }
   270   }
   271 
   272 
   273   /* reported positions */
   274 
   275   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   276 
   277   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   278   {
   279     def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
   280       : Set[Text.Range] =
   281     {
   282       val range = command.decode(raw_range).restrict(command.range)
   283       body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   284     }
   285 
   286     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   287       tree match {
   288         case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
   289         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   290 
   291         case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
   292         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   293 
   294         case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
   295 
   296         case XML.Elem(_, body) => body.foldLeft(set)(positions)
   297 
   298         case _ => set
   299       }
   300 
   301     val set = positions(Set.empty, message)
   302     if (set.isEmpty)
   303       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   304     else set
   305   }
   306 }
   307 
   308 
   309 trait Protocol extends Isabelle_Process
   310 {
   311   /* inlined files */
   312 
   313   def define_blob(blob: Bytes): Unit =
   314     protocol_command_raw("Document.define_blob", Bytes(blob.sha1_digest.toString), blob)
   315 
   316 
   317   /* commands */
   318 
   319   def define_command(command: Command): Unit =
   320   {
   321     val blobs_yxml =
   322     { import XML.Encode._
   323       val encode_blob: T[Command.Blob] =
   324         variant(List(
   325           { case Exn.Res((a, Some(b))) => (List(a.node, b.toString), Nil) },
   326           { case Exn.Res((a, None)) => (List("Missing file: " + quote(a.toString)), Nil)
   327             case Exn.Exn(e) => (List(Exn.message(e)), Nil) }))
   328       YXML.string_of_body(list(encode_blob)(command.blobs))
   329     }
   330     protocol_command("Document.define_command",
   331       Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
   332   }
   333 
   334 
   335   /* execution */
   336 
   337   def discontinue_execution(): Unit =
   338     protocol_command("Document.discontinue_execution")
   339 
   340   def cancel_exec(id: Document_ID.Exec): Unit =
   341     protocol_command("Document.cancel_exec", Document_ID(id))
   342 
   343 
   344   /* document versions */
   345 
   346   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   347     edits: List[Document.Edit_Command])
   348   {
   349     val edits_yxml =
   350     { import XML.Encode._
   351       def id: T[Command] = (cmd => long(cmd.id))
   352       def encode_edit(name: Document.Node.Name)
   353           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   354         variant(List(
   355           // FIXME Document.Node.Blob (!??)
   356           { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
   357           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   358           { case Document.Node.Deps(header) =>
   359               val master_dir = Isabelle_System.posix_path(name.master_dir)
   360               val imports = header.imports.map(_.node)
   361               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   362               (Nil,
   363                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   364                   pair(list(pair(Encode.string,
   365                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   366                   list(Encode.string)))))(
   367                 (master_dir, (name.theory, (imports, (keywords, header.errors)))))) },
   368           { case Document.Node.Perspective(a, b, c) =>
   369               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   370                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   371       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   372       {
   373         val (name, edit) = node_edit
   374         pair(string, encode_edit(name))(name.node, edit)
   375       })
   376       YXML.string_of_body(encode_edits(edits)) }
   377     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   378   }
   379 
   380   def remove_versions(versions: List[Document.Version])
   381   {
   382     val versions_yxml =
   383       { import XML.Encode._
   384         YXML.string_of_body(list(long)(versions.map(_.id))) }
   385     protocol_command("Document.remove_versions", versions_yxml)
   386   }
   387 
   388 
   389   /* dialog via document content */
   390 
   391   def dialog_result(serial: Long, result: String): Unit =
   392     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   393 }