src/Pure/PIDE/protocol.scala
author wenzelm
Thu Dec 13 18:00:24 2012 +0100 (2012-12-13 ago)
changeset 50503 50f141b34bb7
parent 50501 6f41f1646617
child 50507 9605b0d93d1e
permissions -rw-r--r--
enable Isabelle/ML to produce uninterpreted result messages as well;
     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(Markup.ACCEPTED, Markup.FORKED, Markup.JOINED, Markup.RUNNING,
    70       Markup.FINISHED, Markup.FAILED)
    71 
    72   def command_status(status: Status, markup: Markup): Status =
    73     markup match {
    74       case Markup(Markup.ACCEPTED, _) => status.copy(accepted = true)
    75       case Markup(Markup.FORKED, _) => status.copy(touched = true, forks = status.forks + 1)
    76       case Markup(Markup.JOINED, _) => status.copy(forks = status.forks - 1)
    77       case Markup(Markup.RUNNING, _) => status.copy(touched = true, runs = status.runs + 1)
    78       case Markup(Markup.FINISHED, _) => status.copy(runs = status.runs - 1)
    79       case Markup(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   private val clean = Set(Markup.REPORT, Markup.NO_REPORT)
   122 
   123   def clean_message(body: XML.Body): XML.Body =
   124     body filter {
   125       case XML.Wrapped_Elem(Markup(name, _), _, _) => !clean(name)
   126       case XML.Elem(Markup(name, _), _) => !clean(name)
   127       case _ => true
   128     } map {
   129       case XML.Wrapped_Elem(markup, body, ts) => XML.Wrapped_Elem(markup, body, clean_message(ts))
   130       case XML.Elem(markup, ts) => XML.Elem(markup, clean_message(ts))
   131       case t => t
   132     }
   133 
   134   def message_reports(props: Properties.T, body: XML.Body): List[XML.Elem] =
   135     body flatMap {
   136       case XML.Wrapped_Elem(Markup(Markup.REPORT, ps), body, ts) =>
   137         List(XML.Wrapped_Elem(Markup(Markup.REPORT, props ::: ps), body, ts))
   138       case XML.Elem(Markup(Markup.REPORT, ps), ts) =>
   139         List(XML.Elem(Markup(Markup.REPORT, props ::: ps), ts))
   140       case XML.Wrapped_Elem(_, _, ts) => message_reports(props, ts)
   141       case XML.Elem(_, ts) => message_reports(props, ts)
   142       case XML.Text(_) => Nil
   143     }
   144 
   145 
   146   /* specific messages */
   147 
   148   def is_inlined(msg: XML.Tree): Boolean =
   149     !(is_result(msg) || is_tracing(msg) || is_state(msg))
   150 
   151   def is_result(msg: XML.Tree): Boolean =
   152     msg match {
   153       case XML.Elem(Markup(Markup.RESULT, _), _) => true
   154       case _ => false
   155     }
   156 
   157   def is_tracing(msg: XML.Tree): Boolean =
   158     msg match {
   159       case XML.Elem(Markup(Markup.TRACING, _), _) => true
   160       case XML.Elem(Markup(Markup.TRACING_MESSAGE, _), _) => true
   161       case _ => false
   162     }
   163 
   164   def is_state(msg: XML.Tree): Boolean =
   165     msg match {
   166       case XML.Elem(Markup(Markup.WRITELN, _),
   167         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   168       case XML.Elem(Markup(Markup.WRITELN_MESSAGE, _),
   169         List(XML.Elem(Markup(Markup.STATE, _), _))) => true
   170       case _ => false
   171     }
   172 
   173   def is_warning(msg: XML.Tree): Boolean =
   174     msg match {
   175       case XML.Elem(Markup(Markup.WARNING, _), _) => true
   176       case XML.Elem(Markup(Markup.WARNING_MESSAGE, _), _) => true
   177       case _ => false
   178     }
   179 
   180   def is_error(msg: XML.Tree): Boolean =
   181     msg match {
   182       case XML.Elem(Markup(Markup.ERROR, _), _) => true
   183       case XML.Elem(Markup(Markup.ERROR_MESSAGE, _), _) => true
   184       case _ => false
   185     }
   186 
   187 
   188   /* dialogs */
   189 
   190   object Dialog_Args
   191   {
   192     def unapply(props: Properties.T): Option[(Document.ID, Long, String)] =
   193       (props, props, props) match {
   194         case (Position.Id(id), Markup.Serial(serial), Markup.Result(result)) =>
   195           Some((id, serial, result))
   196         case _ => None
   197       }
   198   }
   199 
   200   object Dialog
   201   {
   202     def unapply(tree: XML.Tree): Option[(Document.ID, Long, String)] =
   203       tree match {
   204         case XML.Elem(Markup(Markup.DIALOG, Dialog_Args(id, serial, result)), _) =>
   205           Some((id, serial, result))
   206         case _ => None
   207       }
   208   }
   209 
   210   object Dialog_Result
   211   {
   212     def apply(id: Document.ID, serial: Long, result: String): XML.Elem =
   213     {
   214       val props = Position.Id(id) ::: Markup.Serial(serial)
   215       XML.Elem(Markup(Markup.RESULT, props), List(XML.Text(result)))
   216     }
   217 
   218     def unapply(tree: XML.Tree): Option[String] =
   219       tree match {
   220         case XML.Elem(Markup(Markup.RESULT, _), List(XML.Text(result))) => Some(result)
   221         case _ => None
   222       }
   223   }
   224 
   225 
   226   /* reported positions */
   227 
   228   private val include_pos = Set(Markup.BINDING, Markup.ENTITY, Markup.REPORT, Markup.POSITION)
   229 
   230   def message_positions(command: Command, message: XML.Elem): Set[Text.Range] =
   231   {
   232     def elem_positions(raw_range: Text.Range, set: Set[Text.Range], body: XML.Body)
   233       : Set[Text.Range] =
   234     {
   235       val range = command.decode(raw_range).restrict(command.range)
   236       body.foldLeft(if (range.is_singularity) set else set + range)(positions)
   237     }
   238 
   239     def positions(set: Set[Text.Range], tree: XML.Tree): Set[Text.Range] =
   240       tree match {
   241         case XML.Wrapped_Elem(Markup(name, Position.Id_Range(id, range)), _, body)
   242         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   243 
   244         case XML.Elem(Markup(name, Position.Id_Range(id, range)), body)
   245         if include_pos(name) && id == command.id => elem_positions(range, set, body)
   246 
   247         case XML.Wrapped_Elem(_, _, body) => body.foldLeft(set)(positions)
   248 
   249         case XML.Elem(_, body) => body.foldLeft(set)(positions)
   250 
   251         case _ => set
   252       }
   253 
   254     val set = positions(Set.empty, message)
   255     if (set.isEmpty)
   256       set ++ Position.Range.unapply(message.markup.properties).map(command.decode(_))
   257     else set
   258   }
   259 }
   260 
   261 
   262 trait Protocol extends Isabelle_Process
   263 {
   264   /* commands */
   265 
   266   def define_command(command: Command): Unit =
   267     input("Document.define_command",
   268       Document.ID(command.id), encode(command.name), encode(command.source))
   269 
   270 
   271   /* document versions */
   272 
   273   def discontinue_execution() { input("Document.discontinue_execution") }
   274 
   275   def cancel_execution() { input("Document.cancel_execution") }
   276 
   277   def update(old_id: Document.Version_ID, new_id: Document.Version_ID,
   278     edits: List[Document.Edit_Command])
   279   {
   280     val edits_yxml =
   281     { import XML.Encode._
   282       def id: T[Command] = (cmd => long(cmd.id))
   283       def encode_edit(name: Document.Node.Name)
   284           : T[Document.Node.Edit[(Option[Command], Option[Command]), Command.Perspective]] =
   285         variant(List(
   286           { case Document.Node.Clear() => (Nil, Nil) },  // FIXME unused !?
   287           { case Document.Node.Edits(a) => (Nil, list(pair(option(id), option(id)))(a)) },
   288           { case Document.Node.Deps(header) =>
   289               val dir = Isabelle_System.posix_path(name.dir)
   290               val imports = header.imports.map(_.node)
   291               val keywords = header.keywords.map({ case (a, b, _) => (a, b) })
   292               // FIXME val uses = deps.uses.map(p => (Isabelle_System.posix_path(p._1), p._2))
   293               val uses = header.uses
   294               (Nil,
   295                 pair(Encode.string, pair(Encode.string, pair(list(Encode.string),
   296                   pair(list(pair(Encode.string,
   297                     option(pair(pair(Encode.string, list(Encode.string)), list(Encode.string))))),
   298                   pair(list(pair(Encode.string, bool)), list(Encode.string))))))(
   299                 (dir, (name.theory, (imports, (keywords, (uses, header.errors))))))) },
   300           { case Document.Node.Perspective(a) => (a.commands.map(c => long_atom(c.id)), Nil) }))
   301       def encode_edits: T[List[Document.Edit_Command]] = list((node_edit: Document.Edit_Command) =>
   302       {
   303         val (name, edit) = node_edit
   304         pair(string, encode_edit(name))(name.node, edit)
   305       })
   306       YXML.string_of_body(encode_edits(edits)) }
   307     input("Document.update", Document.ID(old_id), Document.ID(new_id), edits_yxml)
   308   }
   309 
   310   def remove_versions(versions: List[Document.Version])
   311   {
   312     val versions_yxml =
   313       { import XML.Encode._
   314         YXML.string_of_body(list(long)(versions.map(_.id))) }
   315     input("Document.remove_versions", versions_yxml)
   316   }
   317 
   318 
   319   /* dialog via document content */
   320 
   321   def dialog_result(serial: Long, result: String)
   322   {
   323     input("Document.dialog_result", Properties.Value.Long(serial), result)
   324   }
   325 
   326 
   327   /* method invocation service */
   328 
   329   def invoke_scala(id: String, tag: Invoke_Scala.Tag.Value, res: String)
   330   {
   331     input("Document.invoke_scala", id, tag.toString, res)
   332   }
   333 }