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