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