src/Pure/PIDE/protocol.scala
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 50498 6647ba2775c1
child 50500 c94bba7906d2
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
     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, list(pair(long, 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 touched: Boolean = false,
    53     private val accepted: Boolean = false,
    54     private val failed: Boolean = false,
    55     forks: Int = 0,
    56     runs: Int = 0)
    57   {
    58     def + (that: Status): Status =
    59       Status(touched || that.touched, accepted || that.accepted, failed || that.failed,
    60         forks + that.forks, runs + that.runs)
    61 
    62     def is_unprocessed: Boolean = accepted && !failed && (!touched || (forks != 0 && runs == 0))
    63     def is_running: Boolean = runs != 0
    64     def is_finished: Boolean = !failed && touched && forks == 0 && runs == 0
    65     def is_failed: Boolean = failed
    66   }
    67 
    68   val command_status_markup: Set[String] =
    69     Set(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
    70       Markup.FINISHED, Markup.FAILED)
    71 
    72   def command_status(status: Status, markup: Markup): Status =
    73     markup match {
    74       case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
    75       case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
    76       case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    77       case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
    78       case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
    79       case Markup(Markup.FAILED, _) => status.copy(failed = true)
    80       case _ => status
    81     }
    82 
    83   def command_status(markups: List[Markup]): Status =
    84     (Status.init /: markups)(command_status(_, _))
    85 
    86 
    87   /* node status */
    88 
    89   sealed case class Node_Status(
    90     unprocessed: Int, running: Int, finished: Int, warned: Int, failed: Int)
    91   {
    92     def total: Int = unprocessed + running + finished + warned + failed
    93   }
    94 
    95   def node_status(
    96     state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
    97   {
    98     var unprocessed = 0
    99     var running = 0
   100     var finished = 0
   101     var warned = 0
   102     var failed = 0
   103     node.commands.foreach(command =>
   104       {
   105         val st = state.command_state(version, command)
   106         val status = command_status(st.status)
   107         if (status.is_running) running += 1
   108         else if (status.is_finished) {
   109           if (st.results.exists(p => is_warning(p._2))) warned += 1
   110           else finished += 1
   111         }
   112         else if (status.is_failed) failed += 1
   113         else unprocessed += 1
   114       })
   115     Node_Status(unprocessed, running, finished, warned, failed)
   116   }
   117 
   118 
   119   /* result messages */
   120 
   121   private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
   122 
   123   def clean_message(body: XML.Body): XML.Body =
   124     body filter {
   125       case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
   126       case XML.Elem(Markup(name, _), _) => !clean(name)
   127       case _ => true
   128     } map {
   129       case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
   130       case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
   131       case t => t
   132     }
   133 
   134   def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
   135     body flatMap {
   136       case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
   137         List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
   138       case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
   139         List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
   140       case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
   141       case XML.Elem(_, ts) => message_reports(props, ts)
   142       case XML.Text(_) => Nil
   143     }
   144 
   145 
   146   /* specific messages */
   147 
   148   def is_tracing(msg: XML.Tree): Boolean =
   149     msg match {
   150       case XML.Elem(Markup(Markup.TRACING, _), _) => true
   151       case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
   152       case _ => false
   153     }
   154 
   155   def is_warning(msg: XML.Tree): Boolean =
   156     msg match {
   157       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   158       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   159       case _ => false
   160     }
   161 
   162   def is_error(msg: XML.Tree): Boolean =
   163     msg match {
   164       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   165       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   166       case _ => false
   167     }
   168 
   169   def is_state(msg: XML.Tree): Boolean =
   170     msg match {
   171       case XML.Elem(Markup(Markup.WRITELN, _),
   172         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   173       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   174         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   175       case _ => false
   176     }
   177 
   178 
   179   /* reported positions */
   180 
   181   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   182 
   183   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   184   {
   185     def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
   186       : Set[Text.Range] =
   187     {
   188       val range = command.decode(raw_range).restrict(command.range)
   189       body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   190     }
   191 
   192     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   193       tree match {
   194         case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
   195         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   196 
   197         case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
   198         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   199 
   200         case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
   201 
   202         case XML.Elem(_, body) => body.foldLeft(set)(positions)
   203 
   204         case _ => set
   205       }
   206 
   207     val set = positions(Set.empty, message)
   208     if (set.isEmpty && !is_state(message))
   209       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   210     else set
   211   }
   212 }
   213 
   214 
   215 trait Protocol extends Isabelle_Process
   216 {
   217   /* commands */
   218 
   219   def define_command(command: Command): Unit =
   220     input("Document.define_command",
   221       Document.ID(command.id), encode(command.name), encode(command.source))
   222 
   223 
   224   /* document versions */
   225 
   226   def discontinue_execution() { input("Document.discontinue_execution") }
   227 
   228   def cancel_execution() { input("Document.cancel_execution") }
   229 
   230   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   231     edits: List[Document.Edit_Command])
   232   {
   233     val edits_yxml =
   234     { import XML.Encode._
   235       def id: T[Command] = (cmd => long(cmd.id))
   236       def encode_edit(name: Document.Node.Name)
   237           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   238         variant(List(
   239           { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
   240           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   241           { case Document.Node.Deps(header) =>
   242               val dir = Isabelle_System.posix_path(name.dir)
   243               val imports = header.imports.map(_.node)
   244               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   245               // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
   246               val uses = header.uses
   247               (Nil,
   248                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   249                   pair(list(pair(Encode.string,
   250                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   251                   pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
   252                 (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
   253           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   254       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   255       {
   256         val (name, edit) = node_edit
   257         pair(string, encode_edit(name))(name.node, edit)
   258       })
   259       YXML.string_of_body(encode_edits(edits)) }
   260     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   261   }
   262 
   263   def remove_versions(versions: List[Document.Version])
   264   {
   265     val versions_yxml =
   266       { import XML.Encode._
   267         YXML.string_of_body(list(long)(versions.map(_.id))) }
   268     input("Document.remove_versions", versions_yxml)
   269   }
   270 
   271 
   272   /* dialog via document content */
   273 
   274   def dialog_result(name: String, result: String)
   275   {
   276     input("Document.dialog_result", name, result)
   277   }
   278 
   279 
   280   /* method invocation service */
   281 
   282   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   283   {
   284     input("Document.invoke_scala", id, tag.toString, res)
   285   }
   286 }