src/Pure/PIDE/protocol.scala
author wenzelm
Mon Jan 09 23:11:28 2012 +0100 (2012-01-09 ago)
changeset 46166 4beb2f41ed93
parent 46121 30a69cd8a9a0
child 46207 e76b77356ed6
permissions -rw-r--r--
command status color via regular markup;
     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   /* toplevel transactions */
    45 
    46   sealed case class Status(
    47     private val finished: Boolean = false,
    48     private val failed: Boolean = false,
    49     forks: Int = 0)
    50   {
    51     def is_unprocessed: Boolean = !finished && !failed && forks == 0
    52     def is_running: Boolean = forks != 0
    53     def is_finished: Boolean = finished && forks == 0
    54     def is_failed: Boolean = failed && forks == 0
    55   }
    56 
    57   val command_status_markup: Set[String] =
    58     Set(Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED,
    59       Isabelle_Markup.FORKED, Isabelle_Markup.JOINED)
    60 
    61   def command_status(status: Status, markup: Markup): Status =
    62     markup match {
    63       case Markup(Isabelle_Markup.FINISHED, _) => status.copy(finished = true)
    64       case Markup(Isabelle_Markup.FAILED, _) => status.copy(failed = true)
    65       case Markup(Isabelle_Markup.FORKED, _) => status.copy(forks = status.forks + 1)
    66       case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    67       case _ => status
    68     }
    69 
    70   def command_status(markups: List[Markup]): Status =
    71     (Status() /: markups)(command_status(_, _))
    72 
    73   sealed case class Node_Status(unprocessed: Int, running: Int, finished: Int, failed: Int)
    74   {
    75     def total: Int = unprocessed + running + finished + failed
    76   }
    77 
    78   def node_status(
    79     state: Document.State, version: Document.Version, node: Document.Node): Node_Status =
    80   {
    81     var unprocessed = 0
    82     var running = 0
    83     var finished = 0
    84     var failed = 0
    85     node.commands.foreach(command =>
    86       {
    87         val status = command_status(state.command_state(version, command).status)
    88         if (status.is_running) running += 1
    89         else if (status.is_finished) finished += 1
    90         else if (status.is_failed) failed += 1
    91         else unprocessed += 1
    92       })
    93     Node_Status(unprocessed, running, finished, failed)
    94   }
    95 
    96 
    97   /* result messages */
    98 
    99   def clean_message(body: XML.Body): XML.Body =
   100     body filter { case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false case _ => true } map
   101       { case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts)) case t => t }
   102 
   103   def message_reports(msg: XML.Tree): List[XML.Elem] =
   104     msg match {
   105       case elem @ XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => List(elem)
   106       case XML.Elem(_, body) => body.flatMap(message_reports)
   107       case XML.Text(_) => Nil
   108     }
   109 
   110 
   111   /* specific messages */
   112 
   113  def is_tracing(msg: XML.Tree): Boolean =
   114     msg match {
   115       case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
   116       case _ => false
   117     }
   118 
   119   def is_warning(msg: XML.Tree): Boolean =
   120     msg match {
   121       case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
   122       case _ => false
   123     }
   124 
   125   def is_error(msg: XML.Tree): Boolean =
   126     msg match {
   127       case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
   128       case _ => false
   129     }
   130 
   131   def is_state(msg: XML.Tree): Boolean =
   132     msg match {
   133       case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
   134         List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
   135       case _ => false
   136     }
   137 
   138 
   139   /* reported positions */
   140 
   141   private val include_pos =
   142     Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
   143       Isabelle_Markup.POSITION)
   144 
   145   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   146   {
   147     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   148       tree match {
   149         case XML.Elem(Markup(name, Position.Id_Range(id, raw_range)), body)
   150         if include_pos(name) && id == command.id =>
   151           val range = command.decode(raw_range).restrict(command.range)
   152           body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   153         case XML.Elem(Markup(name, _), body) => body.foldLeft(set)(positions)
   154         case _ => set
   155       }
   156     val set = positions(Set.empty, message)
   157     if (set.isEmpty && !is_state(message))
   158       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   159     else set
   160   }
   161 }
   162 
   163 
   164 trait Protocol extends Isabelle_Process
   165 {
   166   /* commands */
   167 
   168   def define_command(command: Command): Unit =
   169     input("Document.define_command",
   170       Document.ID(command.id), Symbol.encode(command.name), Symbol.encode(command.source))
   171 
   172 
   173   /* document versions */
   174 
   175   def cancel_execution()
   176   {
   177     input("Document.cancel_execution")
   178   }
   179 
   180   def update_perspective(old_id: Document.Version_ID, new_id: Document.Version_ID,
   181     name: Document.Node.Name, perspective: Command.Perspective)
   182   {
   183     val ids =
   184     { import XML.Encode._
   185       list(long)(perspective.commands.map(_.id)) }
   186 
   187     input("Document.update_perspective", Document.ID(old_id), Document.ID(new_id),
   188       name.node, YXML.string_of_body(ids))
   189   }
   190 
   191   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   192     edits: List[Document.Edit_Command])
   193   {
   194     val edits_yxml =
   195     { import XML.Encode._
   196       def id: T[Command] = (cmd => long(cmd.id))
   197       def encode_edit(dir: String)
   198           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   199         variant(List(
   200           { case Document.Node.Clear() => (Nil, Nil) },
   201           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   202           { case Document.Node.Header(Exn.Res(Thy_Header(a, b, c))) =>
   203               (Nil,
   204                 triple(pair(string, string), list(string), list(pair(string, bool)))(
   205                   (dir, a), b, c)) },
   206           { case Document.Node.Header(Exn.Exn(e)) => (List(Exn.message(e)), Nil) },
   207           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   208       def encode: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   209       {
   210         val (name, edit) = node_edit
   211         val dir = Isabelle_System.posix_path(name.dir)
   212         pair(string, encode_edit(dir))(name.node, edit)
   213       })
   214       YXML.string_of_body(encode(edits)) }
   215     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   216   }
   217 
   218   def remove_versions(versions: List[Document.Version])
   219   {
   220     val versions_yxml =
   221       { import XML.Encode._
   222         YXML.string_of_body(list(long)(versions.map(_.id))) }
   223     input("Document.remove_versions", versions_yxml)
   224   }
   225 
   226 
   227   /* method invocation service */
   228 
   229   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   230   {
   231     input("Document.invoke_scala", id, tag.toString, res)
   232   }
   233 }