src/Pure/PIDE/protocol.scala
author wenzelm
Thu Dec 13 13:52:18 2012 +0100 (2012-12-13 ago)
changeset 50500 c94bba7906d2
parent 50498 6647ba2775c1
child 50501 6f41f1646617
permissions -rw-r--r--
identify dialogs via official serial and maintain as result message;
clarified Protocol.is_inlined: suppress result/tracing/state messages uniformly;
cumulate_markup/select_markup depending on command state;
explicit Rendering.output_messages;
tuned source structure;
     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_inlined(msg: XML.Tree): Boolean =
   149     !(is_result(msg) || is_tracing(msg) || is_state(msg))
   150 
   151   def is_result(msg: XML.Tree): Boolean =
   152     msg match {
   153       case XML.Elem(Markup(Markup.RESULT, _), _) => true
   154       case _ => false
   155     }
   156 
   157   def is_tracing(msg: XML.Tree): Boolean =
   158     msg match {
   159       case XML.Elem(Markup(Markup.TRACING, _), _) => true
   160       case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
   161       case _ => false
   162     }
   163 
   164   def is_state(msg: XML.Tree): Boolean =
   165     msg match {
   166       case XML.Elem(Markup(Markup.WRITELN, _),
   167         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   168       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   169         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   170       case _ => false
   171     }
   172 
   173   def is_warning(msg: XML.Tree): Boolean =
   174     msg match {
   175       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   176       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   177       case _ => false
   178     }
   179 
   180   def is_error(msg: XML.Tree): Boolean =
   181     msg match {
   182       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   183       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   184       case _ => false
   185     }
   186 
   187 
   188   /* dialogs */
   189 
   190   object Dialog_Args
   191   {
   192     def unapply(props: Properties.T): Option[(Document.ID, Long, String)] =
   193       (props, props, props) match {
   194         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   195           Some((id, serial, result))
   196         case _ => None
   197       }
   198   }
   199 
   200   object Dialog
   201   {
   202     def unapply(tree: XML.Tree): Option[(Document.ID, Long, String)] =
   203       tree match {
   204         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   205           Some((id, serial, result))
   206         case _ => None
   207       }
   208   }
   209 
   210   object Dialog_Result
   211   {
   212     def apply(serial: Long, result: String): XML.Elem =
   213       XML.Elem(Markup(Markup.RESULT, Markup.Serial(serial)), List(XML.Text(result)))
   214 
   215     def unapply(tree: XML.Tree): Option[(Long, String)] =
   216       tree match {
   217         case XML.Elem(Markup(Markup.RESULT, Markup.Serial(serial)), List(XML.Text(result))) =>
   218           Some((serial, result))
   219         case _ => None
   220       }
   221   }
   222 
   223 
   224   /* reported positions */
   225 
   226   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   227 
   228   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   229   {
   230     def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
   231       : Set[Text.Range] =
   232     {
   233       val range = command.decode(raw_range).restrict(command.range)
   234       body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   235     }
   236 
   237     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   238       tree match {
   239         case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
   240         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   241 
   242         case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
   243         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   244 
   245         case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
   246 
   247         case XML.Elem(_, body) => body.foldLeft(set)(positions)
   248 
   249         case _ => set
   250       }
   251 
   252     val set = positions(Set.empty, message)
   253     if (set.isEmpty)
   254       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   255     else set
   256   }
   257 }
   258 
   259 
   260 trait Protocol extends Isabelle_Process
   261 {
   262   /* commands */
   263 
   264   def define_command(command: Command): Unit =
   265     input("Document.define_command",
   266       Document.ID(command.id), encode(command.name), encode(command.source))
   267 
   268 
   269   /* document versions */
   270 
   271   def discontinue_execution() { input("Document.discontinue_execution") }
   272 
   273   def cancel_execution() { input("Document.cancel_execution") }
   274 
   275   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   276     edits: List[Document.Edit_Command])
   277   {
   278     val edits_yxml =
   279     { import XML.Encode._
   280       def id: T[Command] = (cmd => long(cmd.id))
   281       def encode_edit(name: Document.Node.Name)
   282           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   283         variant(List(
   284           { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
   285           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   286           { case Document.Node.Deps(header) =>
   287               val dir = Isabelle_System.posix_path(name.dir)
   288               val imports = header.imports.map(_.node)
   289               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   290               // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
   291               val uses = header.uses
   292               (Nil,
   293                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   294                   pair(list(pair(Encode.string,
   295                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   296                   pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
   297                 (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
   298           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   299       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   300       {
   301         val (name, edit) = node_edit
   302         pair(string, encode_edit(name))(name.node, edit)
   303       })
   304       YXML.string_of_body(encode_edits(edits)) }
   305     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   306   }
   307 
   308   def remove_versions(versions: List[Document.Version])
   309   {
   310     val versions_yxml =
   311       { import XML.Encode._
   312         YXML.string_of_body(list(long)(versions.map(_.id))) }
   313     input("Document.remove_versions", versions_yxml)
   314   }
   315 
   316 
   317   /* dialog via document content */
   318 
   319   def dialog_result(serial: Long, result: String)
   320   {
   321     input("Document.dialog_result", Properties.Value.Long(serial), result)
   322   }
   323 
   324 
   325   /* method invocation service */
   326 
   327   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   328   {
   329     input("Document.invoke_scala", id, tag.toString, res)
   330   }
   331 }