src/Pure/PIDE/protocol.scala
author wenzelm
Tue Feb 18 14:05:08 2014 +0100 (2014-02-18)
changeset 55548 a645277885cf
parent 55433 d2960d67f163
child 55646 ec4651c697e3
permissions -rw-r--r--
more uniform/robust restriction of reported positions, e.g. relevant for "bad" markup due to unclosed comment in ML file;
     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(
   278     command_id: Document_ID.Command,
   279     alt_id: Document_ID.Generic,
   280     chunk: Command.Chunk,
   281     message: XML.Elem): Set[Text.Range] =
   282   {
   283     def elem_positions(props: Properties.T, set: Set[Text.Range]): Set[Text.Range] =
   284       props match {
   285         case Position.Reported(id, file_name, raw_range)
   286         if (id == command_id || id == alt_id) && file_name == chunk.file_name =>
   287           chunk.decode(raw_range).try_restrict(chunk.range) match {
   288             case Some(range) if !range.is_singularity => set + range
   289             case _ => set
   290           }
   291         case _ => set
   292       }
   293 
   294     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   295       tree match {
   296         case XML.Wrapped_Elem(Markup(name, props), _, body) =>
   297           body.foldLeft(if (include_pos(name)) elem_positions(props, set) else set)(positions)
   298         case XML.Elem(Markup(name, props), body) =>
   299           body.foldLeft(if (include_pos(name)) elem_positions(props, set) else set)(positions)
   300         case XML.Text(_) => set
   301       }
   302 
   303     val set = positions(Set.empty, message)
   304     if (set.isEmpty) elem_positions(message.markup.properties, set)
   305     else set
   306   }
   307 }
   308 
   309 
   310 trait Protocol extends Isabelle_Process
   311 {
   312   /* inlined files */
   313 
   314   def define_blob(blob: Bytes): Unit =
   315     protocol_command_raw("Document.define_blob", Bytes(blob.sha1_digest.toString), blob)
   316 
   317 
   318   /* commands */
   319 
   320   def define_command(command: Command): Unit =
   321   {
   322     val blobs_yxml =
   323     { import XML.Encode._
   324       val encode_blob: T[Command.Blob] =
   325         variant(List(
   326           { case Exn.Res((a, b)) =>
   327               (Nil, pair(string, option(string))((a.node, b.map(p => p._1.toString)))) },
   328           { case Exn.Exn(e) => (Nil, string(Exn.message(e))) }))
   329       YXML.string_of_body(list(encode_blob)(command.blobs))
   330     }
   331     protocol_command("Document.define_command",
   332       Document_ID(command.id), encode(command.name), blobs_yxml, encode(command.source))
   333   }
   334 
   335 
   336   /* execution */
   337 
   338   def discontinue_execution(): Unit =
   339     protocol_command("Document.discontinue_execution")
   340 
   341   def cancel_exec(id: Document_ID.Exec): Unit =
   342     protocol_command("Document.cancel_exec", Document_ID(id))
   343 
   344 
   345   /* document versions */
   346 
   347   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   348     edits: List[Document.Edit_Command])
   349   {
   350     val edits_yxml =
   351     { import XML.Encode._
   352       def id: T[Command] = (cmd => long(cmd.id))
   353       def encode_edit(name: Document.Node.Name)
   354           : T[Document.Node.Edit[Command.Edit, Command.Perspective]] =
   355         variant(List(
   356           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   357           { case Document.Node.Deps(header) =>
   358               val master_dir = Isabelle_System.posix_path(name.master_dir)
   359               val imports = header.imports.map(_.node)
   360               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   361               (Nil,
   362                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   363                   pair(list(pair(Encode.string,
   364                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   365                   list(Encode.string)))))(
   366                 (master_dir, (name.theory, (imports, (keywords, header.errors)))))) },
   367           { case Document.Node.Perspective(a, b, c) =>
   368               (bool_atom(a) :: b.commands.map(cmd => long_atom(cmd.id)),
   369                 list(pair(id, pair(Encode.string, list(Encode.string))))(c.dest)) }))
   370       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   371       {
   372         val (name, edit) = node_edit
   373         pair(string, encode_edit(name))(name.node, edit)
   374       })
   375       YXML.string_of_body(encode_edits(edits)) }
   376     protocol_command("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   377   }
   378 
   379   def remove_versions(versions: List[Document.Version])
   380   {
   381     val versions_yxml =
   382       { import XML.Encode._
   383         YXML.string_of_body(list(long)(versions.map(_.id))) }
   384     protocol_command("Document.remove_versions", versions_yxml)
   385   }
   386 
   387 
   388   /* dialog via document content */
   389 
   390   def dialog_result(serial: Long, result: String): Unit =
   391     protocol_command("Document.dialog_result", Properties.Value.Long(serial), result)
   392 }