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