src/Pure/PIDE/protocol.scala
author wenzelm
Wed Nov 21 20:15:25 2012 +0100 (2012-11-21 ago)
changeset 50157 76efdb6daab2
parent 50128 599c935aac82
child 50201 c26369c9eda6
permissions -rw-r--r--
tuned whitespace;
     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(Isabelle_Markup.ACCEPTED, Isabelle_Markup.FORKED, Isabelle_Markup.JOINED,
    70       Isabelle_Markup.RUNNING, Isabelle_Markup.FINISHED, Isabelle_Markup.FAILED)
    71 
    72   def command_status(status: Status, markup: Markup): Status =
    73     markup match {
    74       case Markup(Isabelle_Markup.ACCEPTED, _) => status.copy(accepted = true)
    75       case Markup(Isabelle_Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
    76       case Markup(Isabelle_Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    77       case Markup(Isabelle_Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
    78       case Markup(Isabelle_Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
    79       case Markup(Isabelle_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   def clean_message(body: XML.Body): XML.Body =
   122     body filter {
   123       case XML.Elem(Markup(Isabelle_Markup.REPORT, _), _) => false
   124       case XML.Elem(Markup(Isabelle_Markup.NO_REPORT, _), _) => false
   125       case _ => true
   126     } map {
   127       case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
   128       case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
   129       case t => t
   130     }
   131 
   132   def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
   133     body flatMap {
   134       case XML.Elem(Markup(Isabelle_Markup.REPORT, ps), ts) =>
   135         List(XML.Elem(Markup(Isabelle_Markup.REPORT, props ::: ps), ts))
   136       case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
   137       case XML.Elem(_, ts) => message_reports(props, ts)
   138       case XML.Text(_) => Nil
   139     }
   140 
   141 
   142   /* specific messages */
   143 
   144   def is_tracing(msg: XML.Tree): Boolean =
   145     msg match {
   146       case XML.Elem(Markup(Isabelle_Markup.TRACING, _), _) => true
   147       case XML.Elem(Markup(Isabelle_Markup.TRACING_MESSAGE, _), _) => true
   148       case _ => false
   149     }
   150 
   151   def is_warning(msg: XML.Tree): Boolean =
   152     msg match {
   153       case XML.Elem(Markup(Isabelle_Markup.WARNING, _), _) => true
   154       case XML.Elem(Markup(Isabelle_Markup.WARNING_MESSAGE, _), _) => true
   155       case _ => false
   156     }
   157 
   158   def is_error(msg: XML.Tree): Boolean =
   159     msg match {
   160       case XML.Elem(Markup(Isabelle_Markup.ERROR, _), _) => true
   161       case XML.Elem(Markup(Isabelle_Markup.ERROR_MESSAGE, _), _) => true
   162       case _ => false
   163     }
   164 
   165   def is_state(msg: XML.Tree): Boolean =
   166     msg match {
   167       case XML.Elem(Markup(Isabelle_Markup.WRITELN, _),
   168         List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
   169       case XML.Elem(Markup(Isabelle_Markup.WRITELN_MESSAGE, _),
   170         List(XML.Elem(Markup(Isabelle_Markup.STATE, _), _))) => true
   171       case _ => false
   172     }
   173 
   174 
   175   /* reported positions */
   176 
   177   private val include_pos =
   178     Set(Isabelle_Markup.BINDING, Isabelle_Markup.ENTITY, Isabelle_Markup.REPORT,
   179       Isabelle_Markup.POSITION)
   180 
   181   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   182   {
   183     def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
   184       : Set[Text.Range] =
   185     {
   186       val range = command.decode(raw_range).restrict(command.range)
   187       body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   188     }
   189 
   190     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   191       tree match {
   192         case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
   193         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   194 
   195         case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
   196         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   197 
   198         case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
   199 
   200         case XML.Elem(_, body) => body.foldLeft(set)(positions)
   201 
   202         case _ => set
   203       }
   204 
   205     val set = positions(Set.empty, message)
   206     if (set.isEmpty && !is_state(message))
   207       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   208     else set
   209   }
   210 }
   211 
   212 
   213 trait Protocol extends Isabelle_Process
   214 {
   215   /* commands */
   216 
   217   def define_command(command: Command): Unit =
   218     input("Document.define_command",
   219       Document.ID(command.id), encode(command.name), encode(command.source))
   220 
   221 
   222   /* document versions */
   223 
   224   def discontinue_execution() { input("Document.discontinue_execution") }
   225 
   226   def cancel_execution() { input("Document.cancel_execution") }
   227 
   228   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   229     edits: List[Document.Edit_Command])
   230   {
   231     val edits_yxml =
   232     { import XML.Encode._
   233       def id: T[Command] = (cmd => long(cmd.id))
   234       def encode_edit(name: Document.Node.Name)
   235           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   236         variant(List(
   237           { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
   238           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   239           { case Document.Node.Deps(header) =>
   240               val dir = Isabelle_System.posix_path(name.dir)
   241               val imports = header.imports.map(_.node)
   242               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   243               // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
   244               val uses = header.uses
   245               (Nil,
   246                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   247                   pair(list(pair(Encode.string,
   248                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   249                   pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
   250                 (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
   251           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   252       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   253       {
   254         val (name, edit) = node_edit
   255         pair(string, encode_edit(name))(name.node, edit)
   256       })
   257       YXML.string_of_body(encode_edits(edits)) }
   258     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   259   }
   260 
   261   def remove_versions(versions: List[Document.Version])
   262   {
   263     val versions_yxml =
   264       { import XML.Encode._
   265         YXML.string_of_body(list(long)(versions.map(_.id))) }
   266     input("Document.remove_versions", versions_yxml)
   267   }
   268 
   269 
   270   /* method invocation service */
   271 
   272   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   273   {
   274     input("Document.invoke_scala", id, tag.toString, res)
   275   }
   276 }