src/Pure/PIDE/protocol.scala
author wenzelm
Thu Apr 05 14:14:51 2012 +0200 (2012-04-05 ago)
changeset 47343 b8aeab386414
parent 46938 cda018294515
child 47346 cd3ab7625519
permissions -rw-r--r--
less aggressive discontinue_execution before document update, to avoid unstable execs that need to be re-assigned;
     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
    15   {
    16     def unapply(text: String): Option[(Document.Version_ID, Document.Assign)] =
    17       try {
    18         import XML.Decode._
    19         val body = YXML.parse_body(text)
    20         Some(pair(long, pair(list(pair(long, option(long))), list(pair(string, option(long)))))(body))
    21       }
    22       catch {
    23         case ERROR(_) => None
    24         case _: XML.XML_Atom => None
    25         case _: XML.XML_Body => None
    26       }
    27   }
    28 
    29   object Removed
    30   {
    31     def unapply(text: String): Option[List[Document.Version_ID]] =
    32       try {
    33         import XML.Decode._
    34         Some(list(long)(YXML.parse_body(text)))
    35       }
    36       catch {
    37         case ERROR(_) => None
    38         case _: XML.XML_Atom => None
    39         case _: XML.XML_Body => None
    40       }
    41   }
    42 
    43 
    44   /* command status */
    45 
    46   object Status
    47   {
    48     val init = Status()
    49   }
    50 
    51   sealed case class Status(
    52     private val parsed: Boolean = false,
    53     private val finished: Boolean = false,
    54     private val failed: Boolean = false,
    55     forks: Int = 0)
    56   {
    57     def is_unprocessed: Boolean = parsed && !finished && !failed && forks == 0
    58     def is_running: Boolean = forks != 0
    59     def is_finished: Boolean = finished && forks == 0
    60     def is_failed: Boolean = failed && forks == 0
    61     def + (that: Status): Status =
    62       Status(parsed || that.parsed, finished || that.finished,
    63         failed || that.failed, forks + that.forks)
    64   }
    65 
    66   val command_status_markup: Set[String] =
    67     Set(Isabelle_Markup.PARSED, Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED,
    68       Isabelle_Markup.FORKED, Isabelle_Markup.JOINED)
    69 
    70   def command_status(status: Status, markup: Markup): Status =
    71     markup match {
    72       case Markup(Isabelle_Markup.PARSED, _) => status.copy(parsed = true)
    73       case Markup(Isabelle_Markup.FINISHED, _) => status.copy(finished = true)
    74       case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
    75       case Markup(Isabelle_Markup.FORKED, _) => status.copy(forks = status.forks + 1)
    76       case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    77       case _ => status
    78     }
    79 
    80   def command_status(markups: List[Markup]): Status =
    81     (Status.init /: markups)(command_status(_, _))
    82 
    83 
    84   /* node status */
    85 
    86   sealed case class Node_Status(
    87     unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
    88   {
    89     def total: Int = unprocessed + running + finished + warned + failed
    90   }
    91 
    92   def node_status(
    93     state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
    94   {
    95     var unprocessed = 0
    96     var running = 0
    97     var finished = 0
    98     var warned = 0
    99     var failed = 0
   100     node.commands.foreach(command =>
   101       {
   102         val st = state.command_state(version, command)
   103         val status = command_status(st.status)
   104         if (status.is_running) running += 1
   105         else if (status.is_finished) {
   106           if (st.results.exists(p => is_warning(p._2))) warned += 1
   107           else finished += 1
   108         }
   109         else if (status.is_failed) failed += 1
   110         else unprocessed += 1
   111       })
   112     Node_Status(unprocessed, running, finished, warned, failed)
   113   }
   114 
   115 
   116   /* result messages */
   117 
   118   def clean_message(body: XML.Body): XML.Body =
   119     body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
   120       { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
   121 
   122   def message_reports(msg: XML.Tree): List[XML.Elem] =
   123     msg match {
   124       case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
   125       case XML.Elem(_, body) => body.flatMap(message_reports)
   126       case XML.Text(_) => Nil
   127     }
   128 
   129 
   130   /* specific messages */
   131 
   132  def is_tracing(msg: XML.Tree): Boolean =
   133     msg match {
   134       case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
   135       case _ => false
   136     }
   137 
   138   def is_warning(msg: XML.Tree): Boolean =
   139     msg match {
   140       case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
   141       case _ => false
   142     }
   143 
   144   def is_error(msg: XML.Tree): Boolean =
   145     msg match {
   146       case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
   147       case _ => false
   148     }
   149 
   150   def is_state(msg: XML.Tree): Boolean =
   151     msg match {
   152       case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
   153         List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
   154       case _ => false
   155     }
   156 
   157 
   158   /* reported positions */
   159 
   160   private val include_pos =
   161     Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
   162       Isabelle_Markup.POSITION)
   163 
   164   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   165   {
   166     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   167       tree match {
   168         case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
   169         if include_pos(name) && id == command.id =>
   170           val range = command.decode(raw_range).restrict(command.range)
   171           body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   172         case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
   173         case _ => set
   174       }
   175     val set = positions(Set.empty, message)
   176     if (set.isEmpty && !is_state(message))
   177       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   178     else set
   179   }
   180 }
   181 
   182 
   183 trait Protocol extends Isabelle_Process
   184 {
   185   /* commands */
   186 
   187   def define_command(command: Command): Unit =
   188     input("Document.define_command",
   189       Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
   190 
   191 
   192   /* document versions */
   193 
   194   def discontinue_execution() { input("Document.discontinue_execution") }
   195 
   196   def cancel_execution() { input("Document.cancel_execution") }
   197 
   198   def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
   199     name: Document.Node.Name, perspective: Command.Perspective)
   200   {
   201     val ids =
   202     { import XML.Encode._
   203       list(long)(perspective.commands.map(_.id)) }
   204 
   205     input("Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
   206       name.node, YXML.string_of_body(ids))
   207   }
   208 
   209   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   210     edits: List[Document.Edit_Command])
   211   {
   212     val edits_yxml =
   213     { import XML.Encode._
   214       def id: T[Command] = (cmd => long(cmd.id))
   215       def symbol_string: T[String] = (s => string(Symbol.encode(s)))
   216       def encode_edit(name: Document.Node.Name)
   217           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   218         variant(List(
   219           { case Document.Node.Clear() => (Nil, Nil) },
   220           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   221           { case Document.Node.Header(Exn.Res(deps)) =>
   222               val dir = Isabelle_System.posix_path(name.dir)
   223               val imports = deps.imports.map(_.node)
   224               // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
   225               val uses = deps.uses
   226               (Nil,
   227                 pair(pair(pair(pair(symbol_string, symbol_string), list(symbol_string)),
   228                   list(pair(symbol_string, option(pair(symbol_string, list(symbol_string)))))),
   229                     list(pair(symbol_string, bool)))(
   230                 (((dir, name.theory), imports), deps.keywords), uses)) },
   231           { case Document.Node.Header(Exn.Exn(e)) => (List(Symbol.encode(Exn.message(e))), Nil) },
   232           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   233       def encode: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   234       {
   235         val (name, edit) = node_edit
   236         pair(string, encode_edit(name))(name.node, edit)
   237       })
   238       YXML.string_of_body(encode(edits)) }
   239     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   240   }
   241 
   242   def remove_versions(versions: List[Document.Version])
   243   {
   244     val versions_yxml =
   245       { import XML.Encode._
   246         YXML.string_of_body(list(long)(versions.map(_.id))) }
   247     input("Document.remove_versions", versions_yxml)
   248   }
   249 
   250 
   251   /* method invocation service */
   252 
   253   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   254   {
   255     input("Document.invoke_scala", id, tag.toString, res)
   256   }
   257 }