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