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