src/Pure/Thy/thy_resources.scala
author wenzelm
Sun Sep 02 21:22:52 2018 +0200 (15 months ago ago)
changeset 68883 3653b3ad729e
parent 68771 7e1978b9a4d1
child 68884 9b97d0b20d95
permissions -rw-r--r--
clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm@67054
     1
/*  Title:      Pure/Thy/thy_resources.scala
wenzelm@67054
     2
    Author:     Makarius
wenzelm@67054
     3
wenzelm@67892
     4
PIDE resources for theory files: load/unload theories via PIDE document updates.
wenzelm@67054
     5
*/
wenzelm@67054
     6
wenzelm@67054
     7
package isabelle
wenzelm@67054
     8
wenzelm@67054
     9
wenzelm@67925
    10
import java.io.{File => JFile}
wenzelm@67925
    11
wenzelm@67925
    12
wenzelm@67054
    13
object Thy_Resources
wenzelm@67054
    14
{
wenzelm@67061
    15
  /* PIDE session */
wenzelm@67061
    16
wenzelm@67061
    17
  def start_session(
wenzelm@67061
    18
    options: Options,
wenzelm@67061
    19
    session_name: String,
wenzelm@67061
    20
    session_dirs: List[Path] = Nil,
wenzelm@67948
    21
    include_sessions: List[String] = Nil,
wenzelm@67062
    22
    session_base: Option[Sessions.Base] = None,
wenzelm@67869
    23
    print_mode: List[String] = Nil,
wenzelm@67869
    24
    progress: Progress = No_Progress,
wenzelm@67061
    25
    log: Logger = No_Logger): Session =
wenzelm@67061
    26
  {
wenzelm@67062
    27
    val base =
wenzelm@67062
    28
      session_base getOrElse
wenzelm@67948
    29
        Sessions.base_info(options, session_name, include_sessions = include_sessions,
wenzelm@67948
    30
          progress = progress, dirs = session_dirs).check_base
wenzelm@67062
    31
    val resources = new Thy_Resources(base, log = log)
wenzelm@67945
    32
    val session = new Session(session_name, options, resources)
wenzelm@67061
    33
wenzelm@67061
    34
    val session_error = Future.promise[String]
wenzelm@67061
    35
    var session_phase: Session.Consumer[Session.Phase] = null
wenzelm@67061
    36
    session_phase =
wenzelm@67061
    37
      Session.Consumer(getClass.getName) {
wenzelm@67061
    38
        case Session.Ready =>
wenzelm@67061
    39
          session.phase_changed -= session_phase
wenzelm@67061
    40
          session_error.fulfill("")
wenzelm@67061
    41
        case Session.Terminated(result) if !result.ok =>
wenzelm@67061
    42
          session.phase_changed -= session_phase
wenzelm@67869
    43
          session_error.fulfill("Session start failed: return code " + result.rc)
wenzelm@67061
    44
        case _ =>
wenzelm@67061
    45
      }
wenzelm@67061
    46
    session.phase_changed += session_phase
wenzelm@67061
    47
wenzelm@67877
    48
    progress.echo("Starting " + session_name + " ...")
wenzelm@67061
    49
    Isabelle_Process.start(session, options,
wenzelm@67869
    50
      logic = session_name, dirs = session_dirs, modes = print_mode)
wenzelm@67061
    51
wenzelm@67061
    52
    session_error.join match {
wenzelm@67061
    53
      case "" => session
wenzelm@67061
    54
      case msg => session.stop(); error(msg)
wenzelm@67061
    55
    }
wenzelm@67061
    56
  }
wenzelm@67061
    57
wenzelm@68366
    58
  class Theories_Result private[Thy_Resources](
wenzelm@67883
    59
    val state: Document.State,
wenzelm@67889
    60
    val version: Document.Version,
wenzelm@68758
    61
    val nodes: List[(Document.Node.Name, Document_Status.Node_Status)])
wenzelm@67879
    62
  {
wenzelm@68320
    63
    def node_names: List[Document.Node.Name] = nodes.map(_._1)
wenzelm@67883
    64
    def ok: Boolean = nodes.forall({ case (_, st) => st.ok })
wenzelm@67897
    65
wenzelm@68366
    66
    def snapshot(node_name: Document.Node.Name): Document.Snapshot =
wenzelm@67897
    67
    {
wenzelm@68366
    68
      val snapshot = state.snapshot(node_name)
wenzelm@68366
    69
      assert(version.id == snapshot.version.id)
wenzelm@68366
    70
      snapshot
wenzelm@68109
    71
    }
wenzelm@67879
    72
  }
wenzelm@67879
    73
wenzelm@68770
    74
  val default_check_delay: Double = 0.5
wenzelm@68770
    75
  val default_nodes_status_delay: Double = -1.0
wenzelm@68694
    76
wenzelm@68694
    77
wenzelm@67063
    78
  class Session private[Thy_Resources](
wenzelm@67945
    79
    session_name: String,
wenzelm@67064
    80
    session_options: Options,
wenzelm@67064
    81
    override val resources: Thy_Resources) extends isabelle.Session(session_options, resources)
wenzelm@67063
    82
  {
wenzelm@67063
    83
    session =>
wenzelm@67063
    84
wenzelm@67925
    85
    val tmp_dir: JFile = Isabelle_System.tmp_dir("server_session")
wenzelm@67946
    86
    val tmp_dir_name: String = File.path(tmp_dir).implode
wenzelm@67925
    87
wenzelm@67945
    88
    override def toString: String = session_name
wenzelm@67945
    89
wenzelm@67925
    90
    override def stop(): Process_Result =
wenzelm@67925
    91
    {
wenzelm@67925
    92
      try { super.stop() }
wenzelm@67925
    93
      finally { Isabelle_System.rm_tree(tmp_dir) }
wenzelm@67925
    94
    }
wenzelm@67925
    95
wenzelm@67936
    96
wenzelm@67936
    97
    /* theories */
wenzelm@67936
    98
wenzelm@67064
    99
    def use_theories(
wenzelm@67940
   100
      theories: List[String],
wenzelm@67064
   101
      qualifier: String = Sessions.DRAFT,
wenzelm@67881
   102
      master_dir: String = "",
wenzelm@68770
   103
      check_delay: Time = Time.seconds(default_check_delay),
wenzelm@68694
   104
      check_limit: Int = 0,
wenzelm@68770
   105
      nodes_status_delay: Time = Time.seconds(default_nodes_status_delay),
wenzelm@67885
   106
      id: UUID = UUID(),
wenzelm@67881
   107
      progress: Progress = No_Progress): Theories_Result =
wenzelm@67064
   108
    {
wenzelm@67887
   109
      val dep_theories =
wenzelm@67884
   110
        resources.load_theories(session, id, theories, qualifier = qualifier,
wenzelm@67946
   111
          master_dir = proper_string(master_dir) getOrElse tmp_dir_name, progress = progress)
wenzelm@68770
   112
      val dep_theories_set = dep_theories.toSet
wenzelm@67064
   113
wenzelm@68883
   114
      val nodes_status_update = Synchronized(Document_Status.Nodes_Status.empty_update)
wenzelm@68883
   115
wenzelm@67879
   116
      val result = Future.promise[Theories_Result]
wenzelm@67064
   117
wenzelm@68694
   118
      def check_state(beyond_limit: Boolean = false)
wenzelm@67064
   119
      {
wenzelm@67064
   120
        val state = session.current_state()
wenzelm@67064
   121
        state.stable_tip_version match {
wenzelm@68694
   122
          case Some(version) =>
wenzelm@68883
   123
            if (beyond_limit ||
wenzelm@68883
   124
                dep_theories.forall(name =>
wenzelm@68883
   125
                  state.node_consolidated(version, name) ||
wenzelm@68883
   126
                  nodes_status_update.value._1.is_terminated(name)))
wenzelm@68883
   127
            {
wenzelm@68694
   128
              val nodes =
wenzelm@68694
   129
                for (name <- dep_theories)
wenzelm@68758
   130
                yield (name -> Document_Status.Node_Status.make(state, version, name))
wenzelm@68694
   131
              try { result.fulfill(new Theories_Result(state, version, nodes)) }
wenzelm@68694
   132
              catch { case _: IllegalStateException => }
wenzelm@68694
   133
            }
wenzelm@68694
   134
          case None =>
wenzelm@67064
   135
        }
wenzelm@67064
   136
      }
wenzelm@67064
   137
wenzelm@67894
   138
      val check_progress =
wenzelm@68694
   139
      {
wenzelm@68694
   140
        var check_count = 0
wenzelm@68694
   141
        Event_Timer.request(Time.now(), repeat = Some(check_delay))
wenzelm@68694
   142
          {
wenzelm@68694
   143
            if (progress.stopped) result.cancel
wenzelm@68694
   144
            else {
wenzelm@68694
   145
              check_count += 1
wenzelm@68694
   146
              check_state(check_limit > 0 && check_count > check_limit)
wenzelm@68694
   147
            }
wenzelm@68694
   148
          }
wenzelm@68694
   149
      }
wenzelm@67894
   150
wenzelm@68331
   151
      val theories_progress = Synchronized(Set.empty[Document.Node.Name])
wenzelm@68331
   152
wenzelm@68770
   153
      val delay_nodes_status =
wenzelm@68770
   154
        Standard_Thread.delay_first(nodes_status_delay max Time.zero) {
wenzelm@68770
   155
          val (nodes_status, names) = nodes_status_update.value
wenzelm@68770
   156
          progress.nodes_status(names.map(name => (name -> nodes_status(name))))
wenzelm@68770
   157
        }
wenzelm@68770
   158
wenzelm@67064
   159
      val consumer =
wenzelm@67064
   160
        Session.Consumer[Session.Commands_Changed](getClass.getName) {
wenzelm@68331
   161
          case changed =>
wenzelm@68770
   162
            if (changed.nodes.exists(dep_theories_set)) {
wenzelm@68770
   163
              val snapshot = session.snapshot()
wenzelm@68770
   164
              val state = snapshot.state
wenzelm@68770
   165
              val version = snapshot.version
wenzelm@68770
   166
wenzelm@68883
   167
              nodes_status_update.change(
wenzelm@68883
   168
                { case upd @ (nodes_status, _) =>
wenzelm@68883
   169
                    val domain =
wenzelm@68883
   170
                      if (nodes_status.is_empty) dep_theories_set
wenzelm@68883
   171
                      else changed.nodes.iterator.filter(dep_theories_set).toSet
wenzelm@68883
   172
                    val upd1 =
wenzelm@68883
   173
                      nodes_status.update(resources.session_base, state, version,
wenzelm@68883
   174
                        domain = Some(domain), trim = changed.assignment).getOrElse(upd)
wenzelm@68883
   175
                    if (nodes_status_delay >= Time.zero && upd != upd1)
wenzelm@68883
   176
                      delay_nodes_status.invoke
wenzelm@68883
   177
wenzelm@68883
   178
                    upd1
wenzelm@68883
   179
                })
wenzelm@68331
   180
wenzelm@68331
   181
              val check_theories =
wenzelm@68770
   182
                (for {
wenzelm@68770
   183
                  command <- changed.commands.iterator
wenzelm@68770
   184
                  if dep_theories_set(command.node_name) && command.potentially_initialized
wenzelm@68770
   185
                } yield command.node_name).toSet
wenzelm@68331
   186
wenzelm@68331
   187
              if (check_theories.nonEmpty) {
wenzelm@68331
   188
                val initialized =
wenzelm@68331
   189
                  theories_progress.change_result(theories =>
wenzelm@68331
   190
                  {
wenzelm@68331
   191
                    val initialized =
wenzelm@68331
   192
                      (check_theories -- theories).toList.filter(name =>
wenzelm@68770
   193
                        Document_Status.Node_Status.make(state, version, name).initialized)
wenzelm@68331
   194
                    (initialized, theories ++ initialized)
wenzelm@68331
   195
                  })
wenzelm@68331
   196
                initialized.map(_.theory).sorted.foreach(progress.theory("", _))
wenzelm@68331
   197
              }
wenzelm@68331
   198
wenzelm@68694
   199
              check_state()
wenzelm@68331
   200
            }
wenzelm@67064
   201
        }
wenzelm@67064
   202
wenzelm@67892
   203
      try {
wenzelm@67892
   204
        session.commands_changed += consumer
wenzelm@67892
   205
        result.join_result
wenzelm@67894
   206
        check_progress.cancel
wenzelm@67892
   207
        session.commands_changed -= consumer
wenzelm@67892
   208
      }
wenzelm@67892
   209
      finally {
wenzelm@67892
   210
        resources.unload_theories(session, id, dep_theories)
wenzelm@67892
   211
      }
wenzelm@67884
   212
wenzelm@67879
   213
      result.join
wenzelm@67064
   214
    }
wenzelm@67936
   215
wenzelm@67939
   216
    def purge_theories(
wenzelm@67939
   217
        theories: List[String],
wenzelm@67939
   218
        qualifier: String = Sessions.DRAFT,
wenzelm@67939
   219
        master_dir: String = "",
wenzelm@67943
   220
        all: Boolean = false): (List[Document.Node.Name], List[Document.Node.Name]) =
wenzelm@67939
   221
      resources.purge_theories(session, theories = theories, qualifier = qualifier,
wenzelm@67946
   222
        master_dir = proper_string(master_dir) getOrElse tmp_dir_name, all = all)
wenzelm@67063
   223
  }
wenzelm@67063
   224
wenzelm@67061
   225
wenzelm@67054
   226
  /* internal state */
wenzelm@67054
   227
wenzelm@67884
   228
  sealed case class State(
wenzelm@67887
   229
    required: Multi_Map[Document.Node.Name, UUID] = Multi_Map.empty,
wenzelm@67887
   230
    theories: Map[Document.Node.Name, Theory] = Map.empty)
wenzelm@67884
   231
  {
wenzelm@67887
   232
    def is_required(name: Document.Node.Name): Boolean = required.isDefinedAt(name)
wenzelm@67887
   233
wenzelm@67887
   234
    def insert_required(id: UUID, names: List[Document.Node.Name]): State =
wenzelm@67887
   235
      copy(required = (required /: names)(_.insert(_, id)))
wenzelm@67887
   236
wenzelm@67887
   237
    def remove_required(id: UUID, names: List[Document.Node.Name]): State =
wenzelm@67887
   238
      copy(required = (required /: names)(_.remove(_, id)))
wenzelm@67887
   239
wenzelm@67887
   240
    def update_theories(update: List[(Document.Node.Name, Theory)]): State =
wenzelm@67887
   241
      copy(theories =
wenzelm@67887
   242
        (theories /: update)({ case (thys, (name, thy)) =>
wenzelm@67887
   243
          thys.get(name) match {
wenzelm@67887
   244
            case Some(thy1) if thy1 == thy => thys
wenzelm@67887
   245
            case _ => thys + (name -> thy)
wenzelm@67887
   246
          }
wenzelm@67887
   247
        }))
wenzelm@67936
   248
wenzelm@67936
   249
    def remove_theories(remove: List[Document.Node.Name]): State =
wenzelm@67936
   250
    {
wenzelm@67936
   251
      require(remove.forall(name => !is_required(name)))
wenzelm@67936
   252
      copy(theories = theories -- remove)
wenzelm@67936
   253
    }
wenzelm@67936
   254
wenzelm@67936
   255
    lazy val theories_graph: Graph[Document.Node.Name, Unit] =
wenzelm@67936
   256
    {
wenzelm@67936
   257
      val entries =
wenzelm@67936
   258
        for ((name, theory) <- theories.toList)
wenzelm@67936
   259
        yield ((name, ()), theory.node_header.imports.map(_._1).filter(theories.isDefinedAt(_)))
wenzelm@67936
   260
      Graph.make(entries, symmetric = true)(Document.Node.Name.Ordering)
wenzelm@67936
   261
    }
wenzelm@67884
   262
  }
wenzelm@67058
   263
wenzelm@67058
   264
  final class Theory private[Thy_Resources](
wenzelm@67058
   265
    val node_name: Document.Node.Name,
wenzelm@67058
   266
    val node_header: Document.Node.Header,
wenzelm@67884
   267
    val text: String,
wenzelm@67884
   268
    val node_required: Boolean)
wenzelm@67056
   269
  {
wenzelm@67058
   270
    override def toString: String = node_name.toString
wenzelm@67058
   271
wenzelm@67058
   272
    def node_perspective: Document.Node.Perspective_Text =
wenzelm@67884
   273
      Document.Node.Perspective(node_required, Text.Perspective.empty, Document.Node.Overlays.empty)
wenzelm@67058
   274
wenzelm@67936
   275
    def make_edits(text_edits: List[Text.Edit]): List[Document.Edit_Text] =
wenzelm@67936
   276
      List(node_name -> Document.Node.Deps(node_header),
wenzelm@67936
   277
        node_name -> Document.Node.Edits(text_edits),
wenzelm@67936
   278
        node_name -> node_perspective)
wenzelm@67936
   279
wenzelm@67058
   280
    def node_edits(old: Option[Theory]): List[Document.Edit_Text] =
wenzelm@67058
   281
    {
wenzelm@67884
   282
      val (text_edits, old_required) =
wenzelm@67884
   283
        if (old.isEmpty) (Text.Edit.inserts(0, text), false)
wenzelm@67884
   284
        else (Text.Edit.replace(0, old.get.text, text), old.get.node_required)
wenzelm@67058
   285
wenzelm@67884
   286
      if (text_edits.isEmpty && node_required == old_required) Nil
wenzelm@67936
   287
      else make_edits(text_edits)
wenzelm@67058
   288
    }
wenzelm@67884
   289
wenzelm@67936
   290
    def purge_edits: List[Document.Edit_Text] =
wenzelm@67936
   291
      make_edits(Text.Edit.removes(0, text))
wenzelm@67936
   292
wenzelm@67887
   293
    def required(required: Boolean): Theory =
wenzelm@67887
   294
      if (required == node_required) this
wenzelm@67887
   295
      else new Theory(node_name, node_header, text, required)
wenzelm@67056
   296
  }
wenzelm@67054
   297
}
wenzelm@67054
   298
wenzelm@67056
   299
class Thy_Resources(session_base: Sessions.Base, log: Logger = No_Logger)
wenzelm@67054
   300
  extends Resources(session_base, log = log)
wenzelm@67054
   301
{
wenzelm@67056
   302
  resources =>
wenzelm@67056
   303
wenzelm@67054
   304
  private val state = Synchronized(Thy_Resources.State())
wenzelm@67056
   305
wenzelm@67056
   306
  def load_theories(
wenzelm@67056
   307
    session: Session,
wenzelm@67885
   308
    id: UUID,
wenzelm@67940
   309
    theories: List[String],
wenzelm@67946
   310
    qualifier: String,
wenzelm@67946
   311
    master_dir: String,
wenzelm@67946
   312
    progress: Progress): List[Document.Node.Name] =
wenzelm@67056
   313
  {
wenzelm@67940
   314
    val import_names = theories.map(thy => import_name(qualifier, master_dir, thy) -> Position.none)
wenzelm@67881
   315
    val dependencies = resources.dependencies(import_names, progress = progress).check_errors
wenzelm@67887
   316
    val dep_theories = dependencies.theories
wenzelm@67888
   317
wenzelm@67888
   318
    val loaded_theories =
wenzelm@67888
   319
      for (node_name <- dep_theories)
wenzelm@67888
   320
      yield {
wenzelm@67888
   321
        val path = node_name.path
wenzelm@67888
   322
        if (!node_name.is_theory) error("Not a theory file: " + path)
wenzelm@67888
   323
wenzelm@67888
   324
        progress.expose_interrupt()
wenzelm@67888
   325
        val text = File.read(path)
wenzelm@67888
   326
        val node_header = resources.check_thy_reader(node_name, Scan.char_reader(text))
wenzelm@67888
   327
        new Thy_Resources.Theory(node_name, node_header, text, true)
wenzelm@67888
   328
      }
wenzelm@67056
   329
wenzelm@67893
   330
    state.change(st =>
wenzelm@67893
   331
      {
wenzelm@67893
   332
        val st1 = st.insert_required(id, dep_theories)
wenzelm@67893
   333
        val theory_edits =
wenzelm@67893
   334
          for (theory <- loaded_theories)
wenzelm@67893
   335
          yield {
wenzelm@67893
   336
            val node_name = theory.node_name
wenzelm@67893
   337
            val theory1 = theory.required(st1.is_required(node_name))
wenzelm@67893
   338
            val edits = theory1.node_edits(st1.theories.get(node_name))
wenzelm@67893
   339
            (edits, (node_name, theory1))
wenzelm@67893
   340
          }
wenzelm@67893
   341
        session.update(Document.Blobs.empty, theory_edits.flatMap(_._1))
wenzelm@67893
   342
        st1.update_theories(theory_edits.map(_._2))
wenzelm@67893
   343
      })
wenzelm@67056
   344
wenzelm@67887
   345
    dep_theories
wenzelm@67056
   346
  }
wenzelm@67884
   347
wenzelm@67887
   348
  def unload_theories(session: Session, id: UUID, dep_theories: List[Document.Node.Name])
wenzelm@67884
   349
  {
wenzelm@67893
   350
    state.change(st =>
wenzelm@67893
   351
      {
wenzelm@67893
   352
        val st1 = st.remove_required(id, dep_theories)
wenzelm@67893
   353
        val theory_edits =
wenzelm@67893
   354
          for {
wenzelm@67893
   355
            node_name <- dep_theories
wenzelm@67893
   356
            theory <- st1.theories.get(node_name)
wenzelm@67893
   357
          }
wenzelm@67893
   358
          yield {
wenzelm@67893
   359
            val theory1 = theory.required(st1.is_required(node_name))
wenzelm@67893
   360
            val edits = theory1.node_edits(Some(theory))
wenzelm@67893
   361
            (edits, (node_name, theory1))
wenzelm@67893
   362
          }
wenzelm@67893
   363
        session.update(Document.Blobs.empty, theory_edits.flatMap(_._1))
wenzelm@67893
   364
        st1.update_theories(theory_edits.map(_._2))
wenzelm@67893
   365
      })
wenzelm@67884
   366
  }
wenzelm@67936
   367
wenzelm@67939
   368
  def purge_theories(session: Session,
wenzelm@67939
   369
    theories: List[String],
wenzelm@67946
   370
    qualifier: String,
wenzelm@67946
   371
    master_dir: String,
wenzelm@67946
   372
    all: Boolean): (List[Document.Node.Name], List[Document.Node.Name]) =
wenzelm@67936
   373
  {
wenzelm@67939
   374
    val nodes = theories.map(import_name(qualifier, master_dir, _))
wenzelm@67939
   375
wenzelm@67936
   376
    state.change_result(st =>
wenzelm@67936
   377
      {
wenzelm@67936
   378
        val graph = st.theories_graph
wenzelm@67936
   379
        val all_nodes = graph.topological_order
wenzelm@67936
   380
wenzelm@67936
   381
        val purge =
wenzelm@67939
   382
          (if (all) all_nodes else nodes.filter(graph.defined(_))).
wenzelm@67936
   383
            filterNot(st.is_required(_)).toSet
wenzelm@67943
   384
wenzelm@67943
   385
        val retain = graph.all_preds(all_nodes.filterNot(purge)).toSet
wenzelm@67943
   386
        val (retained, purged) = all_nodes.partition(retain)
wenzelm@67936
   387
wenzelm@67936
   388
        val purge_edits = purged.flatMap(name => st.theories(name).purge_edits)
wenzelm@67936
   389
        session.update(Document.Blobs.empty, purge_edits)
wenzelm@67936
   390
wenzelm@67943
   391
        ((purged, retained), st.remove_theories(purged))
wenzelm@67936
   392
      })
wenzelm@67936
   393
  }
wenzelm@67054
   394
}