author | wenzelm |
Sat, 14 Aug 2010 11:52:24 +0200 | |
changeset 38373 | e8197eea3cd0 |
parent 38370 | 8b15d0f98962 |
child 38374 | 7eb0f6991e25 |
permissions | -rw-r--r-- |
36676 | 1 |
/* Title: Pure/PIDE/document.scala |
2 |
Author: Makarius |
|
3 |
||
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
4 |
Document as collection of named nodes, each consisting of an editable |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
5 |
list of commands. |
36676 | 6 |
*/ |
34407 | 7 |
|
34871
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents:
34868
diff
changeset
|
8 |
package isabelle |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
9 |
|
34760 | 10 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
11 |
import scala.collection.mutable |
37073 | 12 |
import scala.annotation.tailrec |
13 |
||
14 |
||
34823 | 15 |
object Document |
34483 | 16 |
{ |
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
17 |
/* formal identifiers */ |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
18 |
|
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
19 |
type ID = Long |
38373 | 20 |
type Version_ID = ID |
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
21 |
type Command_ID = ID |
38373 | 22 |
type Exec_ID = ID |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
23 |
|
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
24 |
val NO_ID: ID = 0 |
38355
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38227
diff
changeset
|
25 |
|
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
26 |
def parse_id(s: String): ID = java.lang.Long.parseLong(s) |
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
27 |
def print_id(id: ID): String = id.toString |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
28 |
|
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
29 |
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
30 |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
31 |
/** named document nodes **/ |
38151 | 32 |
|
33 |
object Node |
|
34 |
{ |
|
35 |
val empty: Node = new Node(Linear_Set()) |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
36 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
37 |
def command_starts(commands: Iterator[Command], offset: Int = 0): Iterator[(Command, Int)] = |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
38 |
{ |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
39 |
var i = offset |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
40 |
for (command <- commands) yield { |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
41 |
val start = i |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
42 |
i += command.length |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
43 |
(command, start) |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
44 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
45 |
} |
38151 | 46 |
} |
47 |
||
48 |
class Node(val commands: Linear_Set[Command]) |
|
49 |
{ |
|
50 |
/* command ranges */ |
|
51 |
||
52 |
def command_starts: Iterator[(Command, Int)] = |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
53 |
Node.command_starts(commands.iterator) |
38151 | 54 |
|
55 |
def command_start(cmd: Command): Option[Int] = |
|
56 |
command_starts.find(_._1 == cmd).map(_._2) |
|
57 |
||
58 |
def command_range(i: Int): Iterator[(Command, Int)] = |
|
59 |
command_starts dropWhile { case (cmd, start) => start + cmd.length <= i } |
|
60 |
||
61 |
def command_range(i: Int, j: Int): Iterator[(Command, Int)] = |
|
62 |
command_range(i) takeWhile { case (_, start) => start < j } |
|
63 |
||
64 |
def command_at(i: Int): Option[(Command, Int)] = |
|
65 |
{ |
|
66 |
val range = command_range(i) |
|
67 |
if (range.hasNext) Some(range.next) else None |
|
68 |
} |
|
69 |
||
70 |
def proper_command_at(i: Int): Option[Command] = |
|
71 |
command_at(i) match { |
|
72 |
case Some((command, _)) => |
|
73 |
commands.reverse_iterator(command).find(cmd => !cmd.is_ignored) |
|
74 |
case None => None |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
||
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
79 |
/* initial document */ |
34485 | 80 |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
81 |
val init: Document = new Document(NO_ID, Map().withDefaultValue(Node.empty)) |
34660 | 82 |
|
34859 | 83 |
|
84 |
||
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
85 |
/** changes of plain text, eventually resulting in document edits **/ |
38151 | 86 |
|
87 |
type Node_Text_Edit = (String, List[Text_Edit]) // FIXME None: remove |
|
88 |
||
89 |
type Edit[C] = |
|
90 |
(String, // node name |
|
91 |
Option[List[(Option[C], Option[C])]]) // None: remove, Some: insert/remove commands |
|
34859 | 92 |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
93 |
abstract class Snapshot |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
94 |
{ |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
95 |
val document: Document |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
96 |
val node: Document.Node |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
97 |
val is_outdated: Boolean |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
98 |
def convert(offset: Int): Int |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
99 |
def revert(offset: Int): Int |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
100 |
def lookup_command(id: Command_ID): Option[Command] |
38361 | 101 |
def state(command: Command): Command.State |
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
102 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
103 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
104 |
object Change |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
105 |
{ |
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
106 |
val init = new Change(Future.value(Document.init), Nil, Future.value(Nil, Document.init)) |
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
107 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
108 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
109 |
class Change( |
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
110 |
val prev: Future[Document], |
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
111 |
val edits: List[Node_Text_Edit], |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
112 |
val result: Future[(List[Edit[Command]], Document)]) |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
113 |
{ |
38366
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
114 |
val document: Future[Document] = result.map(_._2) |
fea82d1add74
simplified/clarified Change: transition prev --edits--> result, based on futures;
wenzelm
parents:
38365
diff
changeset
|
115 |
def is_finished: Boolean = prev.is_finished && document.is_finished |
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
116 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
117 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
118 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
119 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
120 |
/** editing **/ |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
121 |
|
38364
827b90f18ff4
Change: eliminated id, which is merely the resulting document id and is only required in joined state anyway;
wenzelm
parents:
38363
diff
changeset
|
122 |
def text_edits(session: Session, old_doc: Document, edits: List[Node_Text_Edit]) |
827b90f18ff4
Change: eliminated id, which is merely the resulting document id and is only required in joined state anyway;
wenzelm
parents:
38363
diff
changeset
|
123 |
: (List[Edit[Command]], Document) = |
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
124 |
{ |
34859 | 125 |
/* phase 1: edit individual command source */ |
126 |
||
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
127 |
@tailrec def edit_text(eds: List[Text_Edit], |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
128 |
commands: Linear_Set[Command]): Linear_Set[Command] = |
34859 | 129 |
{ |
130 |
eds match { |
|
131 |
case e :: es => |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
132 |
Node.command_starts(commands.iterator).find { |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
133 |
case (cmd, cmd_start) => |
37685
305c326db33b
more efficient document model/view -- avoid repeated iteration over commands from start, prefer bulk operations;
wenzelm
parents:
37186
diff
changeset
|
134 |
e.can_edit(cmd.source, cmd_start) || |
305c326db33b
more efficient document model/view -- avoid repeated iteration over commands from start, prefer bulk operations;
wenzelm
parents:
37186
diff
changeset
|
135 |
e.is_insert && e.start == cmd_start + cmd.length |
34863 | 136 |
} match { |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
137 |
case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) => |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
138 |
val (rest, text) = e.edit(cmd.source, cmd_start) |
38367 | 139 |
val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd |
34863 | 140 |
edit_text(rest.toList ::: es, new_commands) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
141 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
142 |
case Some((cmd, cmd_start)) => |
38367 | 143 |
edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text))) |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
144 |
|
34859 | 145 |
case None => |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
146 |
require(e.is_insert && e.start == 0) |
38367 | 147 |
edit_text(es, commands.insert_after(None, Command.unparsed(e.text))) |
34859 | 148 |
} |
34863 | 149 |
case Nil => commands |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
150 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
151 |
} |
34582 | 152 |
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
153 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
154 |
/* phase 2: recover command spans */ |
34859 | 155 |
|
37073 | 156 |
@tailrec def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] = |
34859 | 157 |
{ |
38367 | 158 |
commands.iterator.find(_.is_unparsed) match { |
34859 | 159 |
case Some(first_unparsed) => |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
160 |
val first = |
37072
9105c8237c7a
renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents:
37071
diff
changeset
|
161 |
commands.reverse_iterator(first_unparsed).find(_.is_command) getOrElse commands.head |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
162 |
val last = |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
163 |
commands.iterator(first_unparsed).find(_.is_command) getOrElse commands.last |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
164 |
val range = |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
165 |
commands.iterator(first).takeWhile(_ != last).toList ::: List(last) |
34859 | 166 |
|
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
167 |
val sources = range.flatMap(_.span.map(_.source)) |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
168 |
val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString)) |
34485 | 169 |
|
34859 | 170 |
val (before_edit, spans1) = |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
171 |
if (!spans0.isEmpty && first.is_command && first.span == spans0.head) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
172 |
(Some(first), spans0.tail) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
173 |
else (commands.prev(first), spans0) |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
174 |
|
34859 | 175 |
val (after_edit, spans2) = |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
176 |
if (!spans1.isEmpty && last.is_command && last.span == spans1.last) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
177 |
(Some(last), spans1.take(spans1.length - 1)) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
178 |
else (commands.next(last), spans1) |
34859 | 179 |
|
34863 | 180 |
val inserted = spans2.map(span => new Command(session.create_id(), span)) |
181 |
val new_commands = |
|
182 |
commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted) |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
183 |
parse_spans(new_commands) |
34485 | 184 |
|
34863 | 185 |
case None => commands |
34485 | 186 |
} |
187 |
} |
|
34739 | 188 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
189 |
|
38373 | 190 |
/* resulting document edits */ |
34859 | 191 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
192 |
{ |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
193 |
val doc_edits = new mutable.ListBuffer[Edit[Command]] |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
194 |
var nodes = old_doc.nodes |
34660 | 195 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
196 |
for ((name, text_edits) <- edits) { |
38151 | 197 |
val commands0 = nodes(name).commands |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
198 |
val commands1 = edit_text(text_edits, commands0) |
38220 | 199 |
val commands2 = parse_spans(commands1) // FIXME somewhat slow |
34863 | 200 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
201 |
val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
202 |
val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
203 |
|
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
204 |
val cmd_edits = |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
205 |
removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) ::: |
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
206 |
inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd))) |
34660 | 207 |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
208 |
doc_edits += (name -> Some(cmd_edits)) |
38151 | 209 |
nodes += (name -> new Node(commands2)) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
210 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
211 |
(doc_edits.toList, new Document(session.create_id(), nodes)) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
212 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
213 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
214 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
215 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
216 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
217 |
/** global state -- accumulated prover results **/ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
218 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
219 |
object State |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
220 |
{ |
38373 | 221 |
class Fail(state: State) extends Exception |
222 |
||
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
223 |
val init = State().define_document(Document.init, Map()).assign(Document.init.id, Nil) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
224 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
225 |
class Assignment(former_assignment: Map[Command, Exec_ID]) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
226 |
{ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
227 |
@volatile private var tmp_assignment = former_assignment |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
228 |
private val promise = Future.promise[Map[Command, Exec_ID]] |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
229 |
def is_finished: Boolean = promise.is_finished |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
230 |
def join: Map[Command, Exec_ID] = promise.join |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
231 |
def assign(command_execs: List[(Command, Exec_ID)]) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
232 |
{ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
233 |
promise.fulfill(tmp_assignment ++ command_execs) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
234 |
tmp_assignment = Map() |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
235 |
} |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
236 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
237 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
238 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
239 |
case class State( |
38373 | 240 |
val documents: Map[Version_ID, Document] = Map(), |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
241 |
val commands: Map[Command_ID, Command.State] = Map(), |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
242 |
val execs: Map[Exec_ID, (Command.State, Set[Document])] = Map(), |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
243 |
val assignments: Map[Document, State.Assignment] = Map(), |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
244 |
val disposed: Set[ID] = Set()) // FIXME unused!? |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
245 |
{ |
38373 | 246 |
private def fail[A]: A = throw new State.Fail(this) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
247 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
248 |
def define_document(document: Document, former_assignment: Map[Command, Exec_ID]): State = |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
249 |
{ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
250 |
val id = document.id |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
251 |
if (documents.isDefinedAt(id) || disposed(id)) fail |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
252 |
copy(documents = documents + (id -> document), |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
253 |
assignments = assignments + (document -> new State.Assignment(former_assignment))) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
254 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
255 |
|
38373 | 256 |
def define_command(command: Command): State = |
257 |
{ |
|
258 |
val id = command.id |
|
259 |
if (commands.isDefinedAt(id) || disposed(id)) fail |
|
260 |
copy(commands = commands + (id -> command.empty_state)) |
|
261 |
} |
|
262 |
||
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
263 |
def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command) |
38373 | 264 |
|
265 |
def the_document(id: Version_ID): Document = documents.getOrElse(id, fail) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
266 |
def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
267 |
def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1 |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
268 |
def the_assignment(document: Document): State.Assignment = assignments.getOrElse(document, fail) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
269 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
270 |
def accumulate(id: ID, message: XML.Tree): (Command.State, State) = |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
271 |
execs.get(id) match { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
272 |
case Some((st, docs)) => |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
273 |
val new_st = st.accumulate(message) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
274 |
(new_st, copy(execs = execs + (id -> (new_st, docs)))) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
275 |
case None => |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
276 |
commands.get(id) match { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
277 |
case Some(st) => |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
278 |
val new_st = st.accumulate(message) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
279 |
(new_st, copy(commands = commands + (id -> new_st))) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
280 |
case None => fail |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
281 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
282 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
283 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
284 |
def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State = |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
285 |
{ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
286 |
val doc = the_document(id) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
287 |
val docs = Set(doc) // FIXME unused (!?) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
288 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
289 |
var new_execs = execs |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
290 |
val assigned_execs = |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
291 |
for ((cmd_id, exec_id) <- edits) yield { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
292 |
val st = the_command(cmd_id) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
293 |
if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
294 |
new_execs += (exec_id -> (st, docs)) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
295 |
(st.command, exec_id) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
296 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
297 |
the_assignment(doc).assign(assigned_execs) // FIXME explicit value instead of promise (!?) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
298 |
copy(execs = new_execs) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
299 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
300 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
301 |
def is_assigned(document: Document): Boolean = |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
302 |
assignments.get(document) match { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
303 |
case Some(assgn) => assgn.is_finished |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
304 |
case None => false |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
305 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
306 |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
307 |
def command_state(document: Document, command: Command): Command.State = |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
308 |
{ |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
309 |
val assgn = the_assignment(document) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
310 |
require(assgn.is_finished) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
311 |
the_exec_state(assgn.join.getOrElse(command, fail)) |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
312 |
} |
34485 | 313 |
} |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
314 |
} |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
315 |
|
34855
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
316 |
|
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
317 |
class Document( |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
318 |
val id: Document.Version_ID, |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
319 |
val nodes: Map[String, Document.Node]) |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
320 |