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