src/Pure/PIDE/headless.scala
author wenzelm
Sun, 03 Mar 2019 19:12:28 +0100
changeset 69857 a4b430ad848a
parent 69843 edda2d14c108
child 69920 79c8ff387ed1
permissions -rw-r--r--
clarified signature; suppress already loaded theories;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
     1
/*  Title:      Pure/PIDE/headless.scala
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     3
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
     4
Headless PIDE session and resources from file-system.
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     5
*/
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     6
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     7
package isabelle
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     8
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
     9
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    10
import java.io.{File => JFile}
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    11
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
    12
import scala.annotation.tailrec
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
    13
import scala.collection.mutable
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
    14
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    15
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
    16
object Headless
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
    17
{
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
    18
  /** session **/
67061
2efa25302f34 synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents: 67059
diff changeset
    19
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    20
  private def stable_snapshot(
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    21
    state: Document.State, version: Document.Version, name: Document.Node.Name): Document.Snapshot =
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    22
  {
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    23
    val snapshot = state.snapshot(name)
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    24
    assert(version.id == snapshot.version.id)
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    25
    snapshot
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    26
  }
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    27
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
    28
  class Use_Theories_Result private[Headless](
67883
171e7735ce25 support for "use_theories";
wenzelm
parents: 67881
diff changeset
    29
    val state: Document.State,
67889
wenzelm
parents: 67888
diff changeset
    30
    val version: Document.Version,
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    31
    val nodes: List[(Document.Node.Name, Document_Status.Node_Status)],
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    32
    val nodes_committed: List[(Document.Node.Name, Document_Status.Node_Status)])
67879
e4903b803b8b clarified signature;
wenzelm
parents: 67877
diff changeset
    33
  {
69032
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    34
    def nodes_pending: List[(Document.Node.Name, Document_Status.Node_Status)] =
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    35
    {
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    36
      val committed = nodes_committed.iterator.map(_._1).toSet
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    37
      nodes.filter(p => !committed(p._1))
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    38
    }
90bb4cabe1e8 clarified errors: no result from forced session.stop, check pending theories;
wenzelm
parents: 69013
diff changeset
    39
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    40
    def snapshot(name: Document.Node.Name): Document.Snapshot =
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    41
      stable_snapshot(state, version, name)
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    42
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    43
    def ok: Boolean =
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    44
      (nodes.iterator ++ nodes_committed.iterator).forall({ case (_, st) => st.ok })
67879
e4903b803b8b clarified signature;
wenzelm
parents: 67877
diff changeset
    45
  }
e4903b803b8b clarified signature;
wenzelm
parents: 67877
diff changeset
    46
69520
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    47
  class Session private[Headless](
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    48
    session_name: String,
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    49
    _session_options: => Options,
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    50
    override val resources: Resources) extends isabelle.Session(_session_options, resources)
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    51
  {
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    52
    session =>
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
    53
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
    54
69520
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    55
    /* options */
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    56
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    57
    def default_check_delay: Time = session_options.seconds("headless_check_delay")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    58
    def default_check_limit: Int = session_options.int("headless_check_limit")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    59
    def default_nodes_status_delay: Time = session_options.seconds("headless_nodes_status_delay")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    60
    def default_watchdog_timeout: Time = session_options.seconds("headless_watchdog_timeout")
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
    61
    def default_commit_cleanup_delay: Time = session_options.seconds("headless_commit_cleanup_delay")
67063
10d608cc7470 clarified modules;
wenzelm
parents: 67062
diff changeset
    62
68922
wenzelm
parents: 68916
diff changeset
    63
wenzelm
parents: 68916
diff changeset
    64
    /* temporary directory */
wenzelm
parents: 68916
diff changeset
    65
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    66
    val tmp_dir: JFile = Isabelle_System.tmp_dir("server_session")
67946
e1e57c288e45 session tmp_dir is default master_dir;
wenzelm
parents: 67945
diff changeset
    67
    val tmp_dir_name: String = File.path(tmp_dir).implode
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    68
68923
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
    69
    def master_directory(master_dir: String): String =
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
    70
      proper_string(master_dir) getOrElse tmp_dir_name
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
    71
67945
984c3dc46cc0 tuned output;
wenzelm
parents: 67943
diff changeset
    72
    override def toString: String = session_name
984c3dc46cc0 tuned output;
wenzelm
parents: 67943
diff changeset
    73
67925
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    74
    override def stop(): Process_Result =
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    75
    {
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    76
      try { super.stop() }
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    77
      finally { Isabelle_System.rm_tree(tmp_dir) }
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    78
    }
74dce5658d4c provide tmp_dir for server session;
wenzelm
parents: 67923
diff changeset
    79
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
    80
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
    81
    /* theories */
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
    82
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    83
    private sealed case class Use_Theories_State(
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    84
      last_update: Time = Time.now(),
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    85
      nodes_status: Document_Status.Nodes_Status = Document_Status.Nodes_Status.empty,
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
    86
      already_committed: Map[Document.Node.Name, Document_Status.Node_Status] = Map.empty,
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
    87
      result: Promise[Use_Theories_Result] = Future.promise[Use_Theories_Result])
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    88
    {
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    89
      def update(new_nodes_status: Document_Status.Nodes_Status): Use_Theories_State =
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    90
        copy(last_update = Time.now(), nodes_status = new_nodes_status)
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    91
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    92
      def watchdog(watchdog_timeout: Time): Boolean =
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    93
        watchdog_timeout > Time.zero && Time.now() - last_update > watchdog_timeout
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
    94
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    95
      def cancel_result { result.cancel }
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
    96
      def finished_result: Boolean = result.is_finished
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    97
      def await_result { result.join_result }
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
    98
      def join_result: Use_Theories_Result = result.join
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
    99
      def check_result(
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   100
          state: Document.State,
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   101
          version: Document.Version,
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   102
          dep_theories: List[Document.Node.Name],
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   103
          beyond_limit: Boolean,
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   104
          watchdog_timeout: Time,
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   105
          commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit])
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   106
        : Use_Theories_State =
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   107
      {
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   108
        val already_committed1 =
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   109
          if (commit.isDefined) {
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   110
            (already_committed /: dep_theories)({ case (committed, name) =>
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   111
              def parents_committed: Boolean =
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   112
                version.nodes(name).header.imports.forall({ case (parent, _) =>
69594
1d340f7f8dce proper session base foundations (amending e848328cb2c1);
wenzelm
parents: 69562
diff changeset
   113
                  resources.session_base.loaded_theory(parent) || committed.isDefinedAt(parent)
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   114
                })
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   115
              if (!committed.isDefinedAt(name) && parents_committed &&
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   116
                  state.node_consolidated(version, name))
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   117
              {
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   118
                val snapshot = stable_snapshot(state, version, name)
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   119
                val status = Document_Status.Node_Status.make(state, version, name)
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   120
                commit.get.apply(snapshot, status)
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   121
                committed + (name -> status)
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   122
              }
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   123
              else committed
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   124
            })
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   125
          }
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   126
          else already_committed
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   127
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   128
        if (beyond_limit || watchdog(watchdog_timeout) ||
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   129
          dep_theories.forall(name =>
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   130
            already_committed1.isDefinedAt(name) ||
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   131
            state.node_consolidated(version, name) ||
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   132
            nodes_status.quasi_consolidated(name)))
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   133
        {
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   134
          val nodes =
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   135
            for (name <- dep_theories)
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   136
            yield { (name -> Document_Status.Node_Status.make(state, version, name)) }
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   137
          val nodes_committed =
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   138
            for {
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   139
              name <- dep_theories
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   140
              status <- already_committed1.get(name)
68925
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   141
            } yield (name -> status)
76ce16eefab9 record status of already committed nodes;
wenzelm
parents: 68923
diff changeset
   142
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
   143
          try { result.fulfill(new Use_Theories_Result(state, version, nodes, nodes_committed)) }
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   144
          catch { case _: IllegalStateException => }
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   145
        }
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   146
69035
d75cd481f8d9 proper status after commit;
wenzelm
parents: 69032
diff changeset
   147
        copy(already_committed = already_committed1)
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   148
      }
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   149
    }
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   150
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   151
    def use_theories(
67940
b4e80f062fbf clarified signature -- eliminated somewhat pointless positions;
wenzelm
parents: 67939
diff changeset
   152
      theories: List[String],
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   153
      qualifier: String = Sessions.DRAFT,
67881
812ed06dadec interruptible exploration of dependencies;
wenzelm
parents: 67879
diff changeset
   154
      master_dir: String = "",
68943
e564605d4cac tuned signature;
wenzelm
parents: 68936
diff changeset
   155
      check_delay: Time = default_check_delay,
69520
16779868de1f clarified defaults via system options;
wenzelm
parents: 69458
diff changeset
   156
      check_limit: Int = default_check_limit,
68947
ea804c814693 clarified defaults;
wenzelm
parents: 68943
diff changeset
   157
      watchdog_timeout: Time = default_watchdog_timeout,
68943
e564605d4cac tuned signature;
wenzelm
parents: 68936
diff changeset
   158
      nodes_status_delay: Time = default_nodes_status_delay,
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   159
      id: UUID.T = UUID.random(),
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   160
      // commit: must not block, must not fail
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   161
      commit: Option[(Document.Snapshot, Document_Status.Node_Status) => Unit] = None,
68981
30daac7848b9 tuned signature;
wenzelm
parents: 68962
diff changeset
   162
      commit_cleanup_delay: Time = default_commit_cleanup_delay,
69013
bb4e4c253ebe tuned signature;
wenzelm
parents: 69012
diff changeset
   163
      progress: Progress = No_Progress): Use_Theories_Result =
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   164
    {
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   165
      val dependencies =
68894
1dbdad1b57a5 more robust: load_theories after consumer is installed;
wenzelm
parents: 68888
diff changeset
   166
      {
1dbdad1b57a5 more robust: load_theories after consumer is installed;
wenzelm
parents: 68888
diff changeset
   167
        val import_names =
68923
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   168
          theories.map(thy =>
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   169
            resources.import_name(qualifier, master_directory(master_dir), thy) -> Position.none)
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   170
        resources.dependencies(import_names, progress = progress).check_errors
68894
1dbdad1b57a5 more robust: load_theories after consumer is installed;
wenzelm
parents: 68888
diff changeset
   171
      }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   172
      val dep_theories = dependencies.theories
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   173
      val dep_files =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   174
        dependencies.loaded_files(false).flatMap(_._2).
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   175
          map(path => Document.Node.Name(resources.append("", path)))
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   176
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   177
      val use_theories_state = Synchronized(Use_Theories_State())
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   178
69843
wenzelm
parents: 69818
diff changeset
   179
      def check_result_state(beyond_limit: Boolean = false)
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   180
      {
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   181
        val state = session.current_state()
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   182
        state.stable_tip_version match {
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   183
          case Some(version) =>
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   184
            use_theories_state.change(
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   185
              _.check_result(state, version, dep_theories, beyond_limit, watchdog_timeout, commit))
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   186
          case None =>
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   187
        }
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   188
      }
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   189
67894
fee080c4045f more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents: 67893
diff changeset
   190
      val check_progress =
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   191
      {
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   192
        var check_count = 0
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   193
        Event_Timer.request(Time.now(), repeat = Some(check_delay))
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   194
          {
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   195
            if (progress.stopped) use_theories_state.value.cancel_result
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   196
            else {
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   197
              check_count += 1
69843
wenzelm
parents: 69818
diff changeset
   198
              check_result_state(check_limit > 0 && check_count > check_limit)
68694
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   199
            }
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   200
          }
03e104be99af added check_delay / check_limit for more robust treatment of structurally broken theory sources (or genuine non-termination);
wenzelm
parents: 68365
diff changeset
   201
      }
67894
fee080c4045f more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents: 67893
diff changeset
   202
68906
wenzelm
parents: 68905
diff changeset
   203
      val consumer =
wenzelm
parents: 68905
diff changeset
   204
      {
wenzelm
parents: 68905
diff changeset
   205
        val delay_nodes_status =
wenzelm
parents: 68905
diff changeset
   206
          Standard_Thread.delay_first(nodes_status_delay max Time.zero) {
69818
60d0ee8f2ddb more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents: 69817
diff changeset
   207
            progress.nodes_status(use_theories_state.value.nodes_status)
68906
wenzelm
parents: 68905
diff changeset
   208
          }
68770
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   209
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   210
        val delay_commit_clean =
68981
30daac7848b9 tuned signature;
wenzelm
parents: 68962
diff changeset
   211
          Standard_Thread.delay_first(commit_cleanup_delay max Time.zero) {
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   212
            val clean = use_theories_state.value.already_committed.keySet
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   213
            resources.clean_theories(session, id, clean)
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   214
          }
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   215
68952
wenzelm
parents: 68951
diff changeset
   216
        val dep_theories_set = dep_theories.toSet
wenzelm
parents: 68951
diff changeset
   217
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   218
        Session.Consumer[Session.Commands_Changed](getClass.getName) {
68330
d7920eb7de54 report theory progress via PIDE node status;
wenzelm
parents: 68321
diff changeset
   219
          case changed =>
68770
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   220
            if (changed.nodes.exists(dep_theories_set)) {
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   221
              val snapshot = session.snapshot()
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   222
              val state = snapshot.state
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   223
              val version = snapshot.version
add44e2b8cb0 optional notification of nodes_status (via progress);
wenzelm
parents: 68758
diff changeset
   224
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   225
              val theory_progress =
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   226
                use_theories_state.change_result(st =>
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   227
                  {
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   228
                    val domain =
68914
51bd9e9501fb clarified Use_Theories_State;
wenzelm
parents: 68908
diff changeset
   229
                      if (st.nodes_status.is_empty) dep_theories_set
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   230
                      else changed.nodes.iterator.filter(dep_theories_set).toSet
68899
b15b03c13dbb more detailed progress;
wenzelm
parents: 68894
diff changeset
   231
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   232
                    val (nodes_status_changed, nodes_status1) =
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69035
diff changeset
   233
                      st.nodes_status.update(resources, state, version,
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   234
                        domain = Some(domain), trim = changed.assignment)
68899
b15b03c13dbb more detailed progress;
wenzelm
parents: 68894
diff changeset
   235
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   236
                    if (nodes_status_delay >= Time.zero && nodes_status_changed) {
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   237
                      delay_nodes_status.invoke
68899
b15b03c13dbb more detailed progress;
wenzelm
parents: 68894
diff changeset
   238
                    }
68883
3653b3ad729e clarified Thy_Resources.Session.use_theories: "terminated" node status is sufficient;
wenzelm
parents: 68771
diff changeset
   239
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   240
                    val theory_progress =
68905
90a6b714aca3 tuned -- prefer immutable data;
wenzelm
parents: 68904
diff changeset
   241
                      (for {
69818
60d0ee8f2ddb more robust: avoid potentially unrelated snapshot for the sake of is_suppressed;
wenzelm
parents: 69817
diff changeset
   242
                        (name, node_status) <- nodes_status1.present.iterator
68959
d4223afddd47 clarified message;
wenzelm
parents: 68958
diff changeset
   243
                        if changed.nodes.contains(name) && !st.already_committed.isDefinedAt(name)
68962
50676b0ab970 tuned message (again);
wenzelm
parents: 68959
diff changeset
   244
                        p1 = node_status.percentage
50676b0ab970 tuned message (again);
wenzelm
parents: 68959
diff changeset
   245
                        if p1 > 0 && Some(p1) != st.nodes_status.get(name).map(_.percentage)
50676b0ab970 tuned message (again);
wenzelm
parents: 68959
diff changeset
   246
                      } yield Progress.Theory(name.theory, percentage = Some(p1))).toList
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   247
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   248
                    (theory_progress, st.update(nodes_status1))
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   249
                  })
68330
d7920eb7de54 report theory progress via PIDE node status;
wenzelm
parents: 68321
diff changeset
   250
68957
eef4e983fd9d clarified theory progress;
wenzelm
parents: 68952
diff changeset
   251
              theory_progress.foreach(progress.theory(_))
68903
58525b08eed1 clarified Nodes_Status;
wenzelm
parents: 68899
diff changeset
   252
69843
wenzelm
parents: 69818
diff changeset
   253
              check_result_state()
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   254
68981
30daac7848b9 tuned signature;
wenzelm
parents: 68962
diff changeset
   255
              if (commit.isDefined && commit_cleanup_delay > Time.zero) {
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   256
                if (use_theories_state.value.finished_result)
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   257
                  delay_commit_clean.revoke
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   258
                else delay_commit_clean.invoke
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   259
              }
68330
d7920eb7de54 report theory progress via PIDE node status;
wenzelm
parents: 68321
diff changeset
   260
            }
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   261
        }
68906
wenzelm
parents: 68905
diff changeset
   262
      }
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   263
67892
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   264
      try {
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   265
        session.commands_changed += consumer
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   266
        resources.load_theories(session, id, dep_theories, dep_files, progress)
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   267
        use_theories_state.value.await_result
67894
fee080c4045f more robust check_state loop, even without session activity (e.g. idempotent use_theories);
wenzelm
parents: 67893
diff changeset
   268
        check_progress.cancel
67892
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   269
      }
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   270
      finally {
68907
wenzelm
parents: 68906
diff changeset
   271
        session.commands_changed -= consumer
67892
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   272
        resources.unload_theories(session, id, dep_theories)
25e2b621bdcb more interruptible use_theories;
wenzelm
parents: 67889
diff changeset
   273
      }
67884
43af581d7d8e unload_theories after consolidation -- reset node_required;
wenzelm
parents: 67883
diff changeset
   274
68916
2a1583baaaa0 support dynamic commit of consilidated nodes;
wenzelm
parents: 68915
diff changeset
   275
      use_theories_state.value.join_result
67064
fb487246ef4f synchronous use_theories, based on consolidated_state;
wenzelm
parents: 67063
diff changeset
   276
    }
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   277
67939
544a7a21298e clarified signature -- more like use_theories;
wenzelm
parents: 67936
diff changeset
   278
    def purge_theories(
68915
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   279
      theories: List[String],
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   280
      qualifier: String = Sessions.DRAFT,
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   281
      master_dir: String = "",
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   282
      all: Boolean = false): (List[Document.Node.Name], List[Document.Node.Name]) =
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   283
    {
68923
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   284
      val nodes =
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   285
        if (all) None
59d2eab3f8b9 tuned signature;
wenzelm
parents: 68922
diff changeset
   286
        else Some(theories.map(resources.import_name(qualifier, master_directory(master_dir), _)))
68915
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   287
      resources.purge_theories(session, nodes)
634768c0bd22 tuned signature;
wenzelm
parents: 68914
diff changeset
   288
    }
67063
10d608cc7470 clarified modules;
wenzelm
parents: 67062
diff changeset
   289
  }
10d608cc7470 clarified modules;
wenzelm
parents: 67062
diff changeset
   290
67061
2efa25302f34 synchronous session start (similar to isabelle.vscode.Server);
wenzelm
parents: 67059
diff changeset
   291
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
   292
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   293
  /** resources **/
68922
wenzelm
parents: 68916
diff changeset
   294
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   295
  object Resources
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   296
  {
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   297
    def apply(base_info: Sessions.Base_Info, log: Logger = No_Logger): Resources =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   298
      new Resources(base_info, log = log)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   299
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   300
    def make(
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   301
      options: Options,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   302
      session_name: String,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   303
      session_dirs: List[Path] = Nil,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   304
      include_sessions: List[String] = Nil,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   305
      progress: Progress = No_Progress,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   306
      log: Logger = No_Logger): Resources =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   307
    {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   308
      val base_info =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   309
        Sessions.base_info(options, session_name, dirs = session_dirs,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   310
          include_sessions = include_sessions, progress = progress)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   311
      apply(base_info, log = log)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   312
    }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   313
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   314
    final class Theory private[Headless](
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   315
      val node_name: Document.Node.Name,
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   316
      val node_header: Document.Node.Header,
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   317
      val text: String,
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   318
      val node_required: Boolean)
68922
wenzelm
parents: 68916
diff changeset
   319
    {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   320
      override def toString: String = node_name.toString
68922
wenzelm
parents: 68916
diff changeset
   321
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   322
      def node_perspective: Document.Node.Perspective_Text =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   323
        Document.Node.Perspective(node_required, Text.Perspective.empty, Document.Node.Overlays.empty)
68922
wenzelm
parents: 68916
diff changeset
   324
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   325
      def make_edits(text_edits: List[Text.Edit]): List[Document.Edit_Text] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   326
        List(node_name -> Document.Node.Deps(node_header),
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   327
          node_name -> Document.Node.Edits(text_edits),
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   328
          node_name -> node_perspective)
68922
wenzelm
parents: 68916
diff changeset
   329
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   330
      def node_edits(old: Option[Theory]): List[Document.Edit_Text] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   331
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   332
        val (text_edits, old_required) =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   333
          if (old.isEmpty) (Text.Edit.inserts(0, text), false)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   334
          else (Text.Edit.replace(0, old.get.text, text), old.get.node_required)
67887
a4d5342898b1 unload_theories: actually observe required state;
wenzelm
parents: 67885
diff changeset
   335
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   336
        if (text_edits.isEmpty && node_required == old_required) Nil
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   337
        else make_edits(text_edits)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   338
      }
67887
a4d5342898b1 unload_theories: actually observe required state;
wenzelm
parents: 67885
diff changeset
   339
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   340
      def purge_edits: List[Document.Edit_Text] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   341
        make_edits(Text.Edit.removes(0, text))
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   342
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   343
      def required(required: Boolean): Theory =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   344
        if (required == node_required) this
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   345
        else new Theory(node_name, node_header, text, required)
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   346
    }
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   347
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   348
    sealed case class State(
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   349
      blobs: Map[Document.Node.Name, Document.Blob] = Map.empty,
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   350
      theories: Map[Document.Node.Name, Theory] = Map.empty,
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   351
      required: Multi_Map[Document.Node.Name, UUID.T] = Multi_Map.empty)
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   352
    {
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   353
      /* blobs */
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   354
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   355
      def doc_blobs: Document.Blobs = Document.Blobs(blobs)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   356
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   357
      def update_blobs(names: List[Document.Node.Name]): (Document.Blobs, State) =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   358
      {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   359
        val new_blobs =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   360
          names.flatMap(name =>
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   361
          {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   362
            val bytes = Bytes.read(name.path)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   363
            def new_blob: Document.Blob =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   364
            {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   365
              val text = bytes.text
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   366
              Document.Blob(bytes, text, Symbol.Text_Chunk(text), changed = true)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   367
            }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   368
            blobs.get(name) match {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   369
              case Some(blob) => if (blob.bytes == bytes) None else Some(name -> new_blob)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   370
              case None => Some(name -> new_blob)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   371
            }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   372
          })
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   373
        val blobs1 = (blobs /: new_blobs)(_ + _)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   374
        val blobs2 = (blobs /: new_blobs)({ case (map, (a, b)) => map + (a -> b.unchanged) })
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   375
        (Document.Blobs(blobs1), copy(blobs = blobs2))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   376
      }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   377
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   378
      def blob_edits(name: Document.Node.Name, old_blob: Option[Document.Blob])
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   379
        : List[Document.Edit_Text] =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   380
      {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   381
        val blob = blobs.getOrElse(name, error("Missing blob " + quote(name.toString)))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   382
        val text_edits =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   383
          old_blob match {
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   384
            case None => List(Text.Edit.insert(0, blob.source))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   385
            case Some(blob0) => Text.Edit.replace(0, blob0.source, blob.source)
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   386
          }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   387
        if (text_edits.isEmpty) Nil
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   388
        else List(name -> Document.Node.Blob(blob), name -> Document.Node.Edits(text_edits))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   389
      }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   390
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   391
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   392
      /* theories */
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   393
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   394
      lazy val theory_graph: Graph[Document.Node.Name, Unit] =
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   395
      {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   396
        val entries =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   397
          for ((name, theory) <- theories.toList)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   398
          yield ((name, ()), theory.node_header.imports.map(_._1).filter(theories.isDefinedAt(_)))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   399
        Graph.make(entries, symmetric = true)(Document.Node.Name.Ordering)
67888
3bf62a6e50e7 more interruptible;
wenzelm
parents: 67887
diff changeset
   400
      }
67056
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67054
diff changeset
   401
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   402
      def is_required(name: Document.Node.Name): Boolean = required.isDefinedAt(name)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   403
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   404
      def insert_required(id: UUID.T, names: List[Document.Node.Name]): State =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   405
        copy(required = (required /: names)(_.insert(_, id)))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   406
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   407
      def remove_required(id: UUID.T, names: List[Document.Node.Name]): State =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   408
        copy(required = (required /: names)(_.remove(_, id)))
68958
9199f9da512a tuned message;
wenzelm
parents: 68957
diff changeset
   409
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   410
      def update_theories(update: List[(Document.Node.Name, Theory)]): State =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   411
        copy(theories =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   412
          (theories /: update)({ case (thys, (name, thy)) =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   413
            thys.get(name) match {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   414
              case Some(thy1) if thy1 == thy => thys
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   415
              case _ => thys + (name -> thy)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   416
            }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   417
          }))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   418
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   419
      def remove_theories(remove: List[Document.Node.Name]): State =
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   420
      {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   421
        require(remove.forall(name => !is_required(name)))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   422
        copy(theories = theories -- remove)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   423
      }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   424
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   425
      def unload_theories(session: Session, id: UUID.T, dep_theories: List[Document.Node.Name])
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   426
        : State =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   427
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   428
        val st1 = remove_required(id, dep_theories)
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   429
        val theory_edits =
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   430
          for {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   431
            node_name <- dep_theories
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   432
            theory <- st1.theories.get(node_name)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   433
          }
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   434
          yield {
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   435
            val theory1 = theory.required(st1.is_required(node_name))
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   436
            val edits = theory1.node_edits(Some(theory))
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   437
            (edits, (node_name, theory1))
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   438
          }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   439
        session.update(doc_blobs, theory_edits.flatMap(_._1))
67893
c854e50c2114 synchronized Session.update;
wenzelm
parents: 67892
diff changeset
   440
        st1.update_theories(theory_edits.map(_._2))
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   441
      }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   442
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   443
      def purge_theories(session: Session, nodes: List[Document.Node.Name])
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   444
        : ((List[Document.Node.Name], List[Document.Node.Name]), State) =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   445
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   446
        val all_nodes = theory_graph.topological_order
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   447
        val purge = nodes.filterNot(is_required(_)).toSet
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   448
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   449
        val retain = theory_graph.all_preds(all_nodes.filterNot(purge)).toSet
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   450
        val (retained, purged) = all_nodes.partition(retain)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   451
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   452
        val purge_edits = purged.flatMap(name => theories(name).purge_edits)
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   453
        session.update(doc_blobs, purge_edits)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   454
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   455
        ((purged, retained), remove_theories(purged))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   456
      }
67884
43af581d7d8e unload_theories after consolidation -- reset node_required;
wenzelm
parents: 67883
diff changeset
   457
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   458
      def frontier_theories(clean: Set[Document.Node.Name]): Set[Document.Node.Name] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   459
      {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   460
        @tailrec def frontier(base: List[Document.Node.Name], front: Set[Document.Node.Name])
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   461
          : Set[Document.Node.Name] =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   462
        {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   463
          val add = base.filter(b => theory_graph.imm_succs(b).forall(front))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   464
          if (add.isEmpty) front
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   465
          else {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   466
            val pre_add = add.map(theory_graph.imm_preds)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   467
            val base1 = (pre_add.head /: pre_add.tail)(_ ++ _).toList.filter(clean)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   468
            frontier(base1, front ++ add)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   469
          }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   470
        }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   471
        frontier(theory_graph.maximals.filter(clean), Set.empty)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   472
      }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   473
    }
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   474
  }
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   475
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   476
  class Resources private[Headless](
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   477
      val session_base_info: Sessions.Base_Info,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   478
      log: Logger = No_Logger)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   479
    extends isabelle.Resources(session_base_info.check_base, log = log)
67884
43af581d7d8e unload_theories after consolidation -- reset node_required;
wenzelm
parents: 67883
diff changeset
   480
  {
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   481
    resources =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   482
69538
faf547d2834c clarified signature, notably cascade of dump_options, deps, resources, session;
wenzelm
parents: 69536
diff changeset
   483
    def options: Options = session_base_info.options
faf547d2834c clarified signature, notably cascade of dump_options, deps, resources, session;
wenzelm
parents: 69536
diff changeset
   484
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   485
69857
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   486
    /* dependencies */
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   487
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   488
    def used_theories(
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   489
      deps: Sessions.Deps, progress: Progress = No_Progress): List[Document.Node.Name] =
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   490
    {
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   491
      for {
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   492
        (_, name) <- deps.used_theories_condition(options, progress = progress)
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   493
        if !session_base.loaded_theory(name)
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   494
      } yield name
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   495
    }
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   496
a4b430ad848a clarified signature;
wenzelm
parents: 69843
diff changeset
   497
69536
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   498
    /* session */
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   499
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   500
    def start_session(print_mode: List[String] = Nil, progress: Progress = No_Progress): Session =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   501
    {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   502
      val session = new Session(session_base_info.session, options, resources)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   503
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   504
      val session_error = Future.promise[String]
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   505
      var session_phase: Session.Consumer[Session.Phase] = null
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   506
      session_phase =
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   507
        Session.Consumer(getClass.getName) {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   508
          case Session.Ready =>
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   509
            session.phase_changed -= session_phase
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   510
            session_error.fulfill("")
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   511
          case Session.Terminated(result) if !result.ok =>
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   512
            session.phase_changed -= session_phase
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   513
            session_error.fulfill("Session start failed: return code " + result.rc)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   514
          case _ =>
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   515
        }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   516
      session.phase_changed += session_phase
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   517
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   518
      progress.echo("Starting session " + session_base_info.session + " ...")
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   519
      Isabelle_Process.start(session, options,
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   520
        logic = session_base_info.session, dirs = session_base_info.dirs, modes = print_mode)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   521
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   522
      session_error.join match {
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   523
        case "" => session
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   524
        case msg => session.stop(); error(msg)
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   525
      }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   526
    }
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   527
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   528
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   529
    /* theories */
892b68f932f9 clarified signature;
wenzelm
parents: 69520
diff changeset
   530
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   531
    private val state = Synchronized(Resources.State())
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   532
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   533
    def load_theories(
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   534
      session: Session,
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   535
      id: UUID.T,
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   536
      dep_theories: List[Document.Node.Name],
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   537
      dep_files: List[Document.Node.Name],
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   538
      progress: Progress)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   539
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   540
      val loaded_theories =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   541
        for (node_name <- dep_theories)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   542
        yield {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   543
          val path = node_name.path
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   544
          if (!node_name.is_theory) error("Not a theory file: " + path)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   545
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   546
          progress.expose_interrupt()
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   547
          val text = File.read(path)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   548
          val node_header = resources.check_thy_reader(node_name, Scan.char_reader(text))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   549
          new Resources.Theory(node_name, node_header, text, true)
68936
90c08c7bab9c continuously clean frontier of already committed theories: much less resource requirements;
wenzelm
parents: 68935
diff changeset
   550
        }
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   551
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   552
      val loaded = loaded_theories.length
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   553
      if (loaded > 1) progress.echo("Loading " + loaded + " theories ...")
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   554
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   555
      state.change(st =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   556
        {
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   557
          val (doc_blobs1, st1) = st.insert_required(id, dep_theories).update_blobs(dep_files)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   558
          val theory_edits =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   559
            for (theory <- loaded_theories)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   560
            yield {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   561
              val node_name = theory.node_name
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   562
              val theory1 = theory.required(st1.is_required(node_name))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   563
              val edits = theory1.node_edits(st1.theories.get(node_name))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   564
              (edits, (node_name, theory1))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   565
            }
69562
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   566
          val file_edits =
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   567
            for { node_name <- dep_files if doc_blobs1.changed(node_name) }
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   568
            yield st1.blob_edits(node_name, st.blobs.get(node_name))
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   569
636b3c03a61a include loaded_files as doc_blobs (without purging);
wenzelm
parents: 69538
diff changeset
   570
          session.update(doc_blobs1, theory_edits.flatMap(_._1) ::: file_edits.flatten)
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   571
          st1.update_theories(theory_edits.map(_._2))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   572
        })
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   573
    }
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   574
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   575
    def unload_theories(session: Session, id: UUID.T, dep_theories: List[Document.Node.Name])
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   576
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   577
      state.change(_.unload_theories(session, id, dep_theories))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   578
    }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   579
69458
5655af3ea5bd clarified modules and signature;
wenzelm
parents: 69255
diff changeset
   580
    def clean_theories(session: Session, id: UUID.T, clean: Set[Document.Node.Name])
69012
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   581
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   582
      state.change(st =>
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   583
        {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   584
          val frontier = st.frontier_theories(clean).toList
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   585
          if (frontier.isEmpty) st
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   586
          else {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   587
            val st1 = st.unload_theories(session, id, frontier)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   588
            val (_, st2) = st1.purge_theories(session, frontier)
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   589
            st2
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   590
          }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   591
        })
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   592
    }
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   593
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   594
    def purge_theories(session: Session, nodes: Option[List[Document.Node.Name]])
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   595
      : (List[Document.Node.Name], List[Document.Node.Name]) =
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   596
    {
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   597
      state.change_result(st => st.purge_theories(session, nodes getOrElse st.theory_graph.keys))
c91d14ab065f clarified modules;
wenzelm
parents: 68986
diff changeset
   598
    }
67936
141a93b93aa6 support for purge_theories;
wenzelm
parents: 67925
diff changeset
   599
  }
67054
9498b7522a99 PIDE resources for theory files;
wenzelm
parents:
diff changeset
   600
}