src/Pure/PIDE/protocol.scala
author wenzelm
Tue Jul 09 13:17:22 2013 +0200 (2013-07-09)
changeset 52563 f9a20c2c3b70
parent 52531 21f8e0e151f5
child 52582 31467a4b1466
permissions -rw-r--r--
tuned protocol terminology;
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   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_state(msg: XML.Tree): Boolean =
   209     msg match {
   210       case XML.Elem(Markup(Markup.WRITELN, _),
   211         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   212       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   213         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   214       case _ => false
   215     }
   216 
   217   def is_warning(msg: XML.Tree): Boolean =
   218     msg match {
   219       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   220       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   221       case _ => false
   222     }
   223 
   224   def is_error(msg: XML.Tree): Boolean =
   225     msg match {
   226       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   227       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   228       case _ => false
   229     }
   230 
   231 
   232   /* dialogs */
   233 
   234   object Dialog_Args
   235   {
   236     def unapply(props: Properties.T): Option[(Document_ID.Generic, Long, String)] =
   237       (props, props, props) match {
   238         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   239           Some((id, serial, result))
   240         case _ => None
   241       }
   242   }
   243 
   244   object Dialog
   245   {
   246     def unapply(tree: XML.Tree): Option[(Document_ID.Generic, Long, String)] =
   247       tree match {
   248         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   249           Some((id, serial, result))
   250         case _ => None
   251       }
   252   }
   253 
   254   object Dialog_Result
   255   {
   256     def apply(id: Document_ID.Generic, serial: Long, result: String): XML.Elem =
   257     {
   258       val props = Position.Id(id) ::: Markup.Serial(serial)
   259       XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
   260     }
   261 
   262     def unapply(tree: XML.Tree): Option[String] =
   263       tree match {
   264         case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
   265         case _ => None
   266       }
   267   }
   268 
   269 
   270   /* reported positions */
   271 
   272   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   273 
   274   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   275   {
   276     def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
   277       : Set[Text.Range] =
   278     {
   279       val range = command.decode(raw_range).restrict(command.range)
   280       body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   281     }
   282 
   283     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   284       tree match {
   285         case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
   286         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   287 
   288         case XML.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.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
   292 
   293         case XML.Elem(_, body) => body.foldLeft(set)(positions)
   294 
   295         case _ => set
   296       }
   297 
   298     val set = positions(Set.empty, message)
   299     if (set.isEmpty)
   300       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   301     else set
   302   }
   303 }
   304 
   305 
   306 trait Protocol extends Isabelle_Process
   307 {
   308   /* commands */
   309 
   310   def define_command(command: Command): Unit =
   311     input("Document.define_command",
   312       Document_ID(command.id), encode(command.name), encode(command.source))
   313 
   314 
   315   /* document versions */
   316 
   317   def discontinue_execution() { input("Document.discontinue_execution") }
   318 
   319   def cancel_execution() { input("Document.cancel_execution") }
   320 
   321   def update(old_id: Document_ID.Version, new_id: Document_ID.Version,
   322     edits: List[Document.Edit_Command])
   323   {
   324     val edits_yxml =
   325     { import XML.Encode._
   326       def id: T[Command] = (cmd => long(cmd.id))
   327       def encode_edit(name: Document.Node.Name)
   328           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   329         variant(List(
   330           { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
   331           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   332           { case Document.Node.Deps(header) =>
   333               val dir = Isabelle_System.posix_path(name.dir)
   334               val imports = header.imports.map(_.node)
   335               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   336               (Nil,
   337                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   338                   pair(list(pair(Encode.string,
   339                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   340                   list(Encode.string)))))(
   341                 (dir, (name.theory, (imports, (keywords, header.errors)))))) },
   342           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   343       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   344       {
   345         val (name, edit) = node_edit
   346         pair(string, encode_edit(name))(name.node, edit)
   347       })
   348       YXML.string_of_body(encode_edits(edits)) }
   349     input("Document.update", Document_ID(old_id), Document_ID(new_id), edits_yxml)
   350   }
   351 
   352   def remove_versions(versions: List[Document.Version])
   353   {
   354     val versions_yxml =
   355       { import XML.Encode._
   356         YXML.string_of_body(list(long)(versions.map(_.id))) }
   357     input("Document.remove_versions", versions_yxml)
   358   }
   359 
   360 
   361   /* dialog via document content */
   362 
   363   def dialog_result(serial: Long, result: String)
   364   {
   365     input("Document.dialog_result", Properties.Value.Long(serial), result)
   366   }
   367 }