1 # $Id: abstraction.rb,v 1.12 2012/01/23 11:14:44 machan Exp $
2
3 require 'tmdoc/tmstd'
4 require 'tmdoc/constant'
5 require 'tmdoc/model/abstraction'
6 require 'tmdoc/model/document/path'
7
8
9 module TmDoc
10
11 module Model::Document::Abstraction
12
13 class Document < Model::Abstraction::GenericDocument; end
14
15
16
17 class SeqOfDocument < Model::Abstraction::SeqOfGenericDocument
18 LSM_ELEMENT_CLASS = Document
19 end
20
21
22
23 module Link
24
25 class Abstract < Document
26 attr_reader :name
27
28
29 def initialize(name)
30 ASSERT.kind_of name, String
31
32 super()
33
34 @name = name
35 end
36
37
38 def to_s
39 ASSERT.kind_of self.name, String
40 end
41 end
42
43
44
45 class Source < Abstract; end
46
47
48
49 class Logical < Abstract; end
50
51 end # TmDoc::Model::Document::Abstraction:Link
52
53
54
55 module Module
56
57 class Property < Document
58 attr_reader :name,
59 :a_link_to_line,
60 :a_link_to_file,
61 :uniq_num,
62 :max_uniq_num
63
64
65 def initialize(
66 name,
67 a_link_to_line,
68 a_link_to_file,
69 uniq_num,
70 max_uniq_num
71 )
72 ASSERT.kind_of name, String
73 ASSERT.kind_of a_link_to_line, MDL::Line
74 ASSERT.kind_of a_link_to_file, MDL::File
75 ASSERT.kind_of uniq_num, Integer
76 ASSERT.kind_of max_uniq_num, Integer
77
78 @name = name
79 @a_link_to_line = a_link_to_line
80 @a_link_to_file = a_link_to_file
81 @uniq_num = uniq_num
82 @max_uniq_num = max_uniq_num
83
84 super()
85 end
86
87
88 def to_s(opt_str = nil)
89 str = format(
90 "%s %s %s -- %s (%s)",
91 self.name,
92
93 if self.max_uniq_num > 0
94 format "uniq(%d/%d)", self.uniq_num, self.max_uniq_num
95 else
96 ''
97 end,
98
99 if opt_str
100 opt_str
101 else
102 ''
103 end,
104
105 self.a_link_to_line.to_s,
106
107 self.a_link_to_file.to_s
108 )
109
110 ASSERT.kind_of str, String
111 end
112 end
113
114
115
116 class SeqOfProperty < SeqOfDocument
117 LSM_ELEMENT_CLASS = Property
118 end
119
120 end
121
122
123
124 module InfoBox
125
126 module EntryRow
127
128 class Abstract < Document; end
129
130
131
132 class SeqOfEntryRow < SeqOfDocument
133 LSM_ELEMENT_CLASS = Abstract
134 end
135
136
137
138 class NamedEntryRow < Abstract
139 include Comparable
140
141 attr_reader :name
142
143
144 def initialize(name)
145 ASSERT.kind_of name, String
146
147 @name = name
148
149 super()
150 end
151
152
153 def to_s(opt_str = nil)
154 str = self.name + (if opt_str then '(' + opt_str + ')' else '' end)
155
156 ASSERT.kind_of str, String
157 end
158
159
160 def <=>(other)
161 ASSERT.kind_of other, Abstraction::EntryRow
162
163 result = self.name <=> other.name
164
165 ASSERT.boolean result
166 end
167 end
168
169
170
171 class Property < NamedEntryRow
172 attr_reader :tag, :a_link_to_line, :uniq_num, :max_uniq_num
173
174 def initialize(
175 name, tag, a_link_to_line, uniq_num = 0, max_uniq_num = 0
176 )
177 ASSERT.kind_of name, String
178 ASSERT.kind_of tag, Symbol
179 ASSERT.kind_of a_link_to_line, MDL::Line
180 ASSERT.kind_of uniq_num, Integer
181 ASSERT.kind_of max_uniq_num, Integer
182
183 super(name)
184
185 @tag = tag
186 @a_link_to_line = a_link_to_line
187 @uniq_num = uniq_num
188 @max_uniq_num = max_uniq_num
189 end
190
191
192 def to_s(opt_str = nil)
193 str = super(
194 format("%s #%d%s%s",
195 self.tag,
196
197 self.a_link_to_line.line_num,
198
199 if self.max_uniq_num > 0
200 format " uniq(%d/%d)", self.uniq_num, self.max_uniq_num
201 else
202 ''
203 end,
204
205 if opt_str
206 ' ' + opt_str
207 else
208 ''
209 end
210 )
211 )
212
213 ASSERT.kind_of str, String
214 end
215 end
216
217 end # TmDoc::Model::Document::Abstraction:InfoBox::EntryRow
218
219
220
221 class Row < Document
222 attr_reader :entry_rows
223
224
225 def initialize(entry_rows)
226 ASSERT.kind_of entry_rows, EntryRow::SeqOfEntryRow
227
228 @entry_rows = entry_rows
229 end
230 end
231
232
233
234 class SeqOfRow < SeqOfDocument
235 LSM_ELEMENT_CLASS = Row
236 end
237
238
239
240 class Abstract < Document
241 attr_reader :rows
242
243
244 def initialize(rows)
245 ASSERT.kind_of rows, SeqOfRow
246
247 @rows = rows
248 end
249 end
250
251 end # TmDoc::Model::Document::Abstraction:InfoBox
252
253
254
255 module Node
256
257 class Abstract < Document
258 include TmStd::Treeable
259
260 attr_reader :a_link, :elements
261
262 alias children elements
263
264
265 def initialize(a_link, elements = EMPTY_NODE, is_focused = false)
266 ASSERT.kind_of a_link, Link::Abstract
267 ASSERT.kind_of elements, SeqOfNode
268 ASSERT.boolean is_focused
269
270 @a_link = a_link
271 @elements = elements
272 @is_focused = is_focused
273 end
274
275
276 def empty?
277 ASSERT.boolean self.elements.empty?
278 end
279
280
281 def focused?
282 ASSERT.boolean @is_focused
283 end
284
285
286 def to_s(opt_str = nil)
287 ASSERT.opt_kind_of opt_str, String
288
289 str = format(
290 "<%s>%s%s",
291 self.a_link.to_s,
292 if self.focused? then ' [focus]' else '' end,
293 if opt_str then ' ' + opt_str else '' end
294 )
295
296 ASSERT.kind_of str, String
297 end
298 end
299
300
301
302 class SeqOfNode < SeqOfDocument; end
303
304 EMPTY_NODE = SeqOfNode.new
305
306
307
308 class LogicalStructure < Node::Abstract
309 NODE_LINK_CLASS = Link::Logical
310
311 alias a_link_to_module a_link
312
313 attr_reader :a_infobox,
314 :links_to_line
315
316
317 def initialize(
318 a_link,
319 elements = EMPTY_NODE,
320 is_focused = false,
321 a_infobox = nil,
322 links_to_line = nil
323 )
324 ASSERT.kind_of a_link, Link::Logical
325 ASSERT.kind_of elements, SeqOfLogical
326 ASSERT.boolean is_focused
327 ASSERT.opt_kind_of a_infobox, InfoBox::Abstract
328 ASSERT.opt_kind_of links_to_line, MDL::SeqOfLine
329
330 @a_infobox = a_infobox
331 @links_to_line = links_to_line
332
333 super(a_link, elements, is_focused)
334 end
335
336
337 def all_elements_are_leaf?
338 result = self.elements.all? { |elem_node|
339 if elem_node.elements.empty?
340 if elem_node.a_infobox
341 elem_node.a_infobox.rows.empty?
342 else
343 true
344 end
345 else
346 false
347 end
348 }
349
350 ASSERT.boolean result
351 end
352 end
353
354
355
356 class SeqOfLogical < SeqOfNode
357 LSM_ELEMENT_CLASS = LogicalStructure
358 end
359
360 end
361
362 end # TmDoc::Model::Document::Abstraction
363
364 end # TmDoc