kaleidl 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +284 -0
- data/lib/kaleidl/character_sequence.rb +35 -0
- data/lib/kaleidl/content_element.rb +51 -0
- data/lib/kaleidl/errors.rb +16 -0
- data/lib/kaleidl/label.rb +30 -0
- data/lib/kaleidl/label_context.rb +24 -0
- data/lib/kaleidl/label_context_condition.rb +39 -0
- data/lib/kaleidl/label_style.rb +28 -0
- data/lib/kaleidl/label_template.rb +27 -0
- data/lib/kaleidl/labeller.rb +44 -0
- data/lib/kaleidl/lml/document.rb +30 -0
- data/lib/kaleidl/lml/instance.rb +22 -0
- data/lib/kaleidl/lml/parser.rb +388 -0
- data/lib/kaleidl/lml.rb +11 -0
- data/lib/kaleidl/numeric_sequence.rb +43 -0
- data/lib/kaleidl/profile.rb +66 -0
- data/lib/kaleidl/profile_loader.rb +176 -0
- data/lib/kaleidl/runtime/defaults.rb +35 -0
- data/lib/kaleidl/runtime/engine.rb +301 -0
- data/lib/kaleidl/runtime/renderer.rb +112 -0
- data/lib/kaleidl/runtime/resolver.rb +175 -0
- data/lib/kaleidl/runtime/result.rb +61 -0
- data/lib/kaleidl/runtime.rb +11 -0
- data/lib/kaleidl/sequences/formatters.rb +104 -0
- data/lib/kaleidl/sequences/registry.rb +61 -0
- data/lib/kaleidl/sequences.rb +100 -0
- data/lib/kaleidl/version.rb +5 -0
- data/lib/kaleidl/yaml_loader.rb +157 -0
- data/lib/kaleidl.rb +64 -0
- metadata +136 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
module Lml
|
|
5
|
+
# Parser for the subset of the LutaML instance syntax that MN 112
|
|
6
|
+
# profiles are written in:
|
|
7
|
+
#
|
|
8
|
+
# // comment
|
|
9
|
+
# instance Name < BaseClass {
|
|
10
|
+
# attr = "string"
|
|
11
|
+
# ref = BareReference
|
|
12
|
+
# count = 3
|
|
13
|
+
# list = ["a", OtherRef, 3]
|
|
14
|
+
# condition = { type = "x" threshold = 1 }
|
|
15
|
+
# definition { free text
|
|
16
|
+
# * with bullets }
|
|
17
|
+
# definition "one line"
|
|
18
|
+
# }
|
|
19
|
+
#
|
|
20
|
+
# The MN 112 profile syntax uses unquoted instance names and `<`
|
|
21
|
+
# inheritance, which the released LutaML grammar (lutaml-lml) does
|
|
22
|
+
# not accept; this parser covers that draft syntax plus the generic
|
|
23
|
+
# value forms above.
|
|
24
|
+
class Parser
|
|
25
|
+
def self.parse(text)
|
|
26
|
+
new(text).parse
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def initialize(text)
|
|
30
|
+
@tokens = Lexer.new(text).tokens
|
|
31
|
+
@pos = 0
|
|
32
|
+
@pending_isa = nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def parse
|
|
36
|
+
document = Document.new
|
|
37
|
+
until (@pos >= @tokens.size || peek.nil? || peek == [:punct, ""])
|
|
38
|
+
document.add_instance(parse_instance)
|
|
39
|
+
end
|
|
40
|
+
document
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def peek
|
|
46
|
+
@tokens[@pos]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def advance
|
|
50
|
+
token = @tokens[@pos]
|
|
51
|
+
@pos += 1
|
|
52
|
+
token
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def expect(type, value = nil)
|
|
56
|
+
token = advance
|
|
57
|
+
unless token && token[0] == type && (value.nil? || token[1] == value)
|
|
58
|
+
raise ParseError,
|
|
59
|
+
"expected #{value || type} at token #{@pos - 1}, got " \
|
|
60
|
+
"#{token ? token.inspect : "end of input"}"
|
|
61
|
+
end
|
|
62
|
+
token
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def parse_instance
|
|
66
|
+
token = advance
|
|
67
|
+
raise ParseError, "expected an instance declaration, got #{token.inspect}" unless token&.first == :ident
|
|
68
|
+
|
|
69
|
+
if token == [:ident, "instance"]
|
|
70
|
+
parse_typed_instance(nil)
|
|
71
|
+
elsif peek && peek.first == :string
|
|
72
|
+
# MN 112 LML realization form: `Type "Name" { ... }`
|
|
73
|
+
parse_typed_instance(token[1])
|
|
74
|
+
else
|
|
75
|
+
# lutaml-lml form: `Type { isa Base; ... }` — the isa body
|
|
76
|
+
# statement supplies the base class.
|
|
77
|
+
expect(:punct, "{")
|
|
78
|
+
@current_refs = []
|
|
79
|
+
attributes = parse_body
|
|
80
|
+
expect(:punct, "}")
|
|
81
|
+
base = @pending_isa
|
|
82
|
+
@pending_isa = nil
|
|
83
|
+
raise Kaleidl::ParseError,
|
|
84
|
+
"instance #{token[1].inspect} declares no base class (use " \
|
|
85
|
+
"`instance Name < Base`, `Type \"Name\"`, or an `isa` statement)" unless base
|
|
86
|
+
|
|
87
|
+
Instance.new(name: token[1], base: base, attributes: attributes,
|
|
88
|
+
references: @current_refs.uniq)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# `instance Name [< Base] { ... }` (draft) and, when `type` is
|
|
93
|
+
# given, the MN 112 realization form `Type "Name" { ... }`.
|
|
94
|
+
def parse_typed_instance(type)
|
|
95
|
+
name = parse_name
|
|
96
|
+
base = type
|
|
97
|
+
if base.nil? && peek && peek == [:punct, "<"]
|
|
98
|
+
advance
|
|
99
|
+
base = expect(:ident)[1]
|
|
100
|
+
end
|
|
101
|
+
expect(:punct, "{")
|
|
102
|
+
@current_refs = []
|
|
103
|
+
attributes = parse_body
|
|
104
|
+
expect(:punct, "}")
|
|
105
|
+
# lutaml-lml spells inheritance as a body statement (`isa Base;`)
|
|
106
|
+
# rather than the MN 112 `< Base` form; both are accepted.
|
|
107
|
+
base ||= @pending_isa
|
|
108
|
+
@pending_isa = nil
|
|
109
|
+
Instance.new(name: name, base: base, attributes: attributes,
|
|
110
|
+
references: @current_refs.uniq)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def parse_name
|
|
114
|
+
token = peek
|
|
115
|
+
case token&.first
|
|
116
|
+
when :ident
|
|
117
|
+
advance[1]
|
|
118
|
+
when :string
|
|
119
|
+
advance[1]
|
|
120
|
+
else
|
|
121
|
+
raise ParseError, "expected instance name, got #{token.inspect}"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def parse_body
|
|
126
|
+
attributes = {}
|
|
127
|
+
until (@pos >= @tokens.size || peek == [:punct, "}"])
|
|
128
|
+
parse_body_entry(attributes)
|
|
129
|
+
end
|
|
130
|
+
attributes
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def parse_body_entry(attributes)
|
|
134
|
+
token = advance
|
|
135
|
+
raise ParseError, "unexpected token #{token.inspect} in instance body" unless token
|
|
136
|
+
|
|
137
|
+
if token == [:ident, "definition"]
|
|
138
|
+
attributes["definition"] = parse_definition
|
|
139
|
+
elsif token == [:ident, "isa"]
|
|
140
|
+
@pending_isa = parse_name
|
|
141
|
+
advance if peek && peek == [:punct, ";"]
|
|
142
|
+
else
|
|
143
|
+
raise ParseError, "expected attribute name, got #{token.inspect}" unless token[0] == :ident
|
|
144
|
+
|
|
145
|
+
attributes[token[1]] = parse_assignment_value
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def parse_assignment_value
|
|
150
|
+
expect(:punct, "=")
|
|
151
|
+
parse_value
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def parse_definition
|
|
155
|
+
token = peek
|
|
156
|
+
case token&.first
|
|
157
|
+
when :raw_block
|
|
158
|
+
advance[1]
|
|
159
|
+
when :string
|
|
160
|
+
advance[1]
|
|
161
|
+
else
|
|
162
|
+
expect(:string)[1]
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def parse_value
|
|
167
|
+
token = peek
|
|
168
|
+
case token&.first
|
|
169
|
+
when :string
|
|
170
|
+
advance[1]
|
|
171
|
+
when :number
|
|
172
|
+
advance[1]
|
|
173
|
+
when :ident
|
|
174
|
+
ident = advance[1]
|
|
175
|
+
case ident
|
|
176
|
+
when "true" then true
|
|
177
|
+
when "false" then false
|
|
178
|
+
when "null" then nil
|
|
179
|
+
when "ref"
|
|
180
|
+
# MN 112 LML realization reference: ref:(Name) — equivalent
|
|
181
|
+
# to the draft syntax's bare identifier, recorded as a
|
|
182
|
+
# reference either way.
|
|
183
|
+
expect(:punct, ":")
|
|
184
|
+
expect(:punct, "(")
|
|
185
|
+
name = parse_name
|
|
186
|
+
expect(:punct, ")")
|
|
187
|
+
@current_refs << name
|
|
188
|
+
name
|
|
189
|
+
else
|
|
190
|
+
# Draft syntax bare reference (e.g. `sequence = Foo`).
|
|
191
|
+
@current_refs << ident
|
|
192
|
+
ident
|
|
193
|
+
end
|
|
194
|
+
when :punct
|
|
195
|
+
if token == [:punct, "["]
|
|
196
|
+
parse_list
|
|
197
|
+
elsif token == [:punct, "{"]
|
|
198
|
+
parse_map
|
|
199
|
+
else
|
|
200
|
+
raise ParseError, "unexpected token #{token.inspect} in value position"
|
|
201
|
+
end
|
|
202
|
+
else
|
|
203
|
+
raise ParseError, "expected value, got #{token.inspect}"
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def parse_list
|
|
208
|
+
expect(:punct, "[")
|
|
209
|
+
items = []
|
|
210
|
+
until (peek == [:punct, "]"])
|
|
211
|
+
items << parse_value
|
|
212
|
+
if peek == [:punct, ","]
|
|
213
|
+
advance
|
|
214
|
+
else
|
|
215
|
+
break
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
expect(:punct, "]")
|
|
219
|
+
items
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def parse_map
|
|
223
|
+
expect(:punct, "{")
|
|
224
|
+
attributes = parse_body
|
|
225
|
+
expect(:punct, "}")
|
|
226
|
+
attributes
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Turns `.lml` source text into a flat token stream. `//` line
|
|
230
|
+
# comments and `/* */` block comments are dropped.
|
|
231
|
+
class Lexer
|
|
232
|
+
def initialize(text)
|
|
233
|
+
@text = text.to_s
|
|
234
|
+
@pos = 0
|
|
235
|
+
@size = @text.length
|
|
236
|
+
@pending = []
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def tokens
|
|
240
|
+
result = []
|
|
241
|
+
until eos?
|
|
242
|
+
result << next_token
|
|
243
|
+
end
|
|
244
|
+
result
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
private
|
|
248
|
+
|
|
249
|
+
def eos?
|
|
250
|
+
@pos >= @size
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def next_token
|
|
254
|
+
return @pending.shift unless @pending.empty?
|
|
255
|
+
|
|
256
|
+
skip_whitespace_and_comments
|
|
257
|
+
return [:punct, ""] if eos?
|
|
258
|
+
|
|
259
|
+
ch = @text[@pos]
|
|
260
|
+
case ch
|
|
261
|
+
when "{", "}", "[", "]", "=", ",", "<", ";", ":", "(", ")"
|
|
262
|
+
@pos += 1
|
|
263
|
+
[:punct, ch]
|
|
264
|
+
when '"'
|
|
265
|
+
lex_string
|
|
266
|
+
when /\d/
|
|
267
|
+
lex_number
|
|
268
|
+
when /[A-Za-z_]/
|
|
269
|
+
lex_ident
|
|
270
|
+
when "-"
|
|
271
|
+
lex_number
|
|
272
|
+
else
|
|
273
|
+
raise ParseError,
|
|
274
|
+
"unexpected character #{ch.inspect} at offset #{@pos}"
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def skip_whitespace_and_comments
|
|
279
|
+
until eos?
|
|
280
|
+
ch = @text[@pos]
|
|
281
|
+
if ch =~ /\s/
|
|
282
|
+
@pos += 1
|
|
283
|
+
elsif @text[@pos, 2] == "//"
|
|
284
|
+
skip_until_newline
|
|
285
|
+
elsif @text[@pos, 2] == "/*"
|
|
286
|
+
skip_block_comment
|
|
287
|
+
else
|
|
288
|
+
break
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def skip_until_newline
|
|
294
|
+
while @pos < @size && @text[@pos] != "\n"
|
|
295
|
+
@pos += 1
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def skip_block_comment
|
|
300
|
+
@pos += 2
|
|
301
|
+
until eos? || @text[@pos, 2] == "*/"
|
|
302
|
+
@pos += 1
|
|
303
|
+
end
|
|
304
|
+
@pos += 2 unless eos?
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def lex_string
|
|
308
|
+
@pos += 1
|
|
309
|
+
buffer = +""
|
|
310
|
+
until eos? || @text[@pos] == '"'
|
|
311
|
+
ch = @text[@pos]
|
|
312
|
+
if ch == "\\"
|
|
313
|
+
@pos += 1
|
|
314
|
+
buffer << unescape
|
|
315
|
+
else
|
|
316
|
+
buffer << ch
|
|
317
|
+
@pos += 1
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
raise ParseError, "unterminated string at offset #{@pos}" if eos?
|
|
321
|
+
|
|
322
|
+
@pos += 1
|
|
323
|
+
[:string, buffer]
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def unescape
|
|
327
|
+
raise ParseError, "unterminated escape at offset #{@pos}" if eos?
|
|
328
|
+
|
|
329
|
+
ch = @text[@pos]
|
|
330
|
+
@pos += 1
|
|
331
|
+
{ "n" => "\n", "t" => "\t", '"' => '"', "\\" => "\\" }.fetch(ch, ch)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def lex_number
|
|
335
|
+
start = @pos
|
|
336
|
+
@pos += 1 if @text[@pos] == "-"
|
|
337
|
+
@pos += 1 while !eos? && @text[@pos] =~ /\d/
|
|
338
|
+
[:number, Integer(@text[start...@pos])]
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def lex_ident
|
|
342
|
+
start = @pos
|
|
343
|
+
@pos += 1 while !eos? && @text[@pos] =~ /[A-Za-z0-9_]/
|
|
344
|
+
ident = @text[start...@pos]
|
|
345
|
+
if ident == "definition" && next_nonspace_char == "{"
|
|
346
|
+
@pending << [:raw_block, scan_raw_block]
|
|
347
|
+
[:ident, ident]
|
|
348
|
+
else
|
|
349
|
+
[:ident, ident]
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
def next_nonspace_char
|
|
354
|
+
i = @pos
|
|
355
|
+
i += 1 while i < @size && @text[i] =~ /\s/
|
|
356
|
+
@text[i]
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
# `definition { ... }` blocks contain free text (bullets, nested
|
|
360
|
+
# braces, slashes, newlines); it is consumed verbatim up to the
|
|
361
|
+
# matching closing brace.
|
|
362
|
+
def scan_raw_block
|
|
363
|
+
i = @pos
|
|
364
|
+
i += 1 while i < @size && @text[i] =~ /\s/
|
|
365
|
+
raise ParseError, "unterminated definition block" if i >= @size || @text[i] != "{"
|
|
366
|
+
|
|
367
|
+
i += 1
|
|
368
|
+
depth = 1
|
|
369
|
+
start = i
|
|
370
|
+
while i < @size
|
|
371
|
+
ch = @text[i]
|
|
372
|
+
if ch == "{"
|
|
373
|
+
depth += 1
|
|
374
|
+
elsif ch == "}"
|
|
375
|
+
depth -= 1
|
|
376
|
+
break if depth.zero?
|
|
377
|
+
end
|
|
378
|
+
i += 1
|
|
379
|
+
end
|
|
380
|
+
raise ParseError, "unterminated definition block" if depth.positive?
|
|
381
|
+
|
|
382
|
+
@pos = i + 1
|
|
383
|
+
@text[start...i].strip
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
end
|
|
388
|
+
end
|
data/lib/kaleidl/lml.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
# Parsing of LADL profile instances written in the LutaML instance
|
|
5
|
+
# syntax used by MN 112 (`.lml` documents).
|
|
6
|
+
module Lml
|
|
7
|
+
autoload :Parser, "kaleidl/lml/parser"
|
|
8
|
+
autoload :Document, "kaleidl/lml/document"
|
|
9
|
+
autoload :Instance, "kaleidl/lml/instance"
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
# MN 112 "NumericSequence" — defines a monotonic sequence of numerical
|
|
5
|
+
# values.
|
|
6
|
+
#
|
|
7
|
+
# The `format` attribute is a runtime extension of this engine: it
|
|
8
|
+
# names the labeling format used to render a position (one of the
|
|
9
|
+
# kinds registered in Kaleidl::Sequences::Formatters). It carries the
|
|
10
|
+
# MN 112 default labeling sequences (`DecimalNumericSequence`,
|
|
11
|
+
# `RomanUpperNumericSequence`, ...) as profile-loadable data.
|
|
12
|
+
#
|
|
13
|
+
# The `base_sequence` attribute holds the instance name of the base
|
|
14
|
+
# sequence this sequence builds upon; it is resolved at runtime
|
|
15
|
+
# against the registry and the profile-defined sequences.
|
|
16
|
+
class NumericSequence < Lutaml::Model::Serializable
|
|
17
|
+
attribute :char_direction, :string, default: "ltr"
|
|
18
|
+
attribute :initial_value, :integer, default: 1
|
|
19
|
+
attribute :increment, :integer, default: 1
|
|
20
|
+
attribute :cycle_length, :integer
|
|
21
|
+
attribute :base_sequence, :string
|
|
22
|
+
attribute :format, :string
|
|
23
|
+
attribute :definition, :string
|
|
24
|
+
|
|
25
|
+
hsh do
|
|
26
|
+
map "char_direction", to: :char_direction
|
|
27
|
+
map "initial_value", to: :initial_value
|
|
28
|
+
map "increment", to: :increment
|
|
29
|
+
map "cycle_length", to: :cycle_length
|
|
30
|
+
map "base_sequence", to: :base_sequence
|
|
31
|
+
map "format", to: :format
|
|
32
|
+
map "definition", to: :definition
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# MN 112 Sequence#value_for_position — provides the value for a given
|
|
36
|
+
# order in the sequence. Delegates the computation to
|
|
37
|
+
# Kaleidl::Sequences.value_for; `lookup` resolves `base_sequence`
|
|
38
|
+
# references against the runtime registry.
|
|
39
|
+
def value_for_position(position, lookup: nil)
|
|
40
|
+
Kaleidl::Sequences.value_for(self, position, lookup: lookup)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
# A loaded LADL profile: the LabelStyle model described by the profile
|
|
5
|
+
# document, together with the instance tables of its labellers,
|
|
6
|
+
# contexts, templates, sequences, content elements and labels, keyed by
|
|
7
|
+
# instance name.
|
|
8
|
+
class Profile
|
|
9
|
+
attr_reader :style
|
|
10
|
+
|
|
11
|
+
def initialize(style:, labellers: {}, contexts: {}, templates: {},
|
|
12
|
+
sequences: {}, elements: {}, labels: {})
|
|
13
|
+
@style = style
|
|
14
|
+
@labellers = labellers
|
|
15
|
+
@contexts = contexts
|
|
16
|
+
@templates = templates
|
|
17
|
+
@sequences = sequences
|
|
18
|
+
@elements = elements
|
|
19
|
+
@labels = labels
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def labeller(name)
|
|
23
|
+
@labellers[name]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def labeller_by_id(id)
|
|
27
|
+
@labellers.values.find { |l| l.id == id }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def context(name)
|
|
31
|
+
@contexts[name]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def template(name)
|
|
35
|
+
@templates[name]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def sequence(name)
|
|
39
|
+
@sequences[name]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def element(name)
|
|
43
|
+
@elements[name]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def label(name)
|
|
47
|
+
@labels[name]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def labeller_names
|
|
51
|
+
@labellers.keys
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def context_names
|
|
55
|
+
@contexts.keys
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def template_names
|
|
59
|
+
@templates.keys
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def sequence_names
|
|
63
|
+
@sequences.keys
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kaleidl
|
|
4
|
+
# Loads LADL profiles written in the LutaML `.lml` instance syntax
|
|
5
|
+
# (MN 112 worked profile format) into typed Kaleidl profile models.
|
|
6
|
+
#
|
|
7
|
+
# Every instance is hydrated through the lutaml-model framework
|
|
8
|
+
# (Model.from_hash) against the hash mappings declared on the model
|
|
9
|
+
# classes; this class only groups instances by their base class and
|
|
10
|
+
# substitutes labeller template references with the referenced
|
|
11
|
+
# template instances.
|
|
12
|
+
class ProfileLoader
|
|
13
|
+
CLASS_FOR_BASE = {
|
|
14
|
+
"LabelStyle" => -> { Kaleidl::LabelStyle },
|
|
15
|
+
"LabelContext" => -> { Kaleidl::LabelContext },
|
|
16
|
+
"Labeller" => -> { Kaleidl::Labeller },
|
|
17
|
+
"LabelTemplate" => -> { Kaleidl::LabelTemplate },
|
|
18
|
+
"NumericSequence" => -> { Kaleidl::NumericSequence },
|
|
19
|
+
"CharacterSequence" => -> { Kaleidl::CharacterSequence },
|
|
20
|
+
"ContentElement" => -> { Kaleidl::ContentElement },
|
|
21
|
+
"Label" => -> { Kaleidl::Label },
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
BUILTIN_SEQUENCE_NAMES = Sequences::Registry.defaults.keys.freeze
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def load_lml(text)
|
|
28
|
+
new.load_lml(text)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def load_yaml(text)
|
|
32
|
+
new.load_yaml(text)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def load_file(path)
|
|
36
|
+
new.load_file(path)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def load_file(path)
|
|
41
|
+
case File.extname(path).downcase
|
|
42
|
+
when ".yaml", ".yml"
|
|
43
|
+
load_yaml(File.read(path, encoding: "UTF-8"))
|
|
44
|
+
else
|
|
45
|
+
load_lml(File.read(path, encoding: "UTF-8"))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def load_lml(text)
|
|
50
|
+
document = Lml::Parser.parse(text)
|
|
51
|
+
instances = document.instances
|
|
52
|
+
check_duplicate_names(instances)
|
|
53
|
+
build_profile(instances)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def load_yaml(text)
|
|
57
|
+
require_relative "yaml_loader"
|
|
58
|
+
YamlLoader.load(text)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Shared pipeline for the realizations: index the parsed instances,
|
|
62
|
+
# verify references resolve, and build the Profile.
|
|
63
|
+
def load_instances(instances, declared_names:)
|
|
64
|
+
check_duplicate_names(instances)
|
|
65
|
+
check_references(instances, declared_names)
|
|
66
|
+
build_profile(instances)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def check_duplicate_names(instances)
|
|
72
|
+
names = instances.map(&:name)
|
|
73
|
+
repeated = names.tally.select { |_, count| count > 1 }
|
|
74
|
+
raise Kaleidl::ParseError,
|
|
75
|
+
"duplicate instance name in profile: #{repeated.keys.join(", ")}" unless repeated.empty?
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# MN 112 "Label style realizations": a reference resolves to a
|
|
79
|
+
# declared instance or a built-in sequence. Refs are recorded at
|
|
80
|
+
# parse/conversion time — a bare LML identifier and a YAML
|
|
81
|
+
# `{ref:}` mapping are references; quoted strings never are.
|
|
82
|
+
def check_references(instances, declared_names)
|
|
83
|
+
instances.each do |instance|
|
|
84
|
+
instance.references.each do |ref|
|
|
85
|
+
next if declared_names.include?(ref)
|
|
86
|
+
|
|
87
|
+
raise Kaleidl::ResolutionError,
|
|
88
|
+
"instance #{instance.name.inspect} references undefined " \
|
|
89
|
+
"instance #{ref.inspect}"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def build_profile(instances)
|
|
95
|
+
@index = {}
|
|
96
|
+
instances.each { |instance| @index[instance.name] = instance }
|
|
97
|
+
built = {}
|
|
98
|
+
@index.each do |name, instance|
|
|
99
|
+
built[name] = hydrate(name, instance)
|
|
100
|
+
end
|
|
101
|
+
style = built.values.find { |m| m.is_a?(Kaleidl::LabelStyle) }
|
|
102
|
+
raise Kaleidl::ParseError, "profile defines no LabelStyle instance" unless style
|
|
103
|
+
|
|
104
|
+
Profile.new(
|
|
105
|
+
style: style,
|
|
106
|
+
labellers: select_models(built, Kaleidl::Labeller),
|
|
107
|
+
contexts: select_models(built, Kaleidl::LabelContext),
|
|
108
|
+
templates: select_models(built, Kaleidl::LabelTemplate),
|
|
109
|
+
sequences: select_models(built, Kaleidl::NumericSequence, Kaleidl::CharacterSequence),
|
|
110
|
+
elements: select_models(built, Kaleidl::ContentElement),
|
|
111
|
+
labels: select_models(built, Kaleidl::Label),
|
|
112
|
+
)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def select_models(built, *types)
|
|
116
|
+
built.select { |_, model| types.any? { |t| model.is_a?(t) } }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def hydrate(name, instance)
|
|
120
|
+
klass = class_for_base(name, instance)
|
|
121
|
+
attrs = prepared_attributes(name, instance)
|
|
122
|
+
klass.from_hash(attrs)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def class_for_base(name, instance)
|
|
126
|
+
unless instance.base
|
|
127
|
+
raise Kaleidl::ParseError,
|
|
128
|
+
"instance #{name.inspect} declares no base class"
|
|
129
|
+
end
|
|
130
|
+
unless (klass = CLASS_FOR_BASE[instance.base]&.call)
|
|
131
|
+
raise Kaleidl::ParseError,
|
|
132
|
+
"instance #{name.inspect} extends unknown base class " \
|
|
133
|
+
"#{instance.base.inspect}"
|
|
134
|
+
end
|
|
135
|
+
klass
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Labeller template references are substituted with the referenced
|
|
139
|
+
# template instances' attribute hashes so that from_hash hydrates
|
|
140
|
+
# nested Kaleidl::LabelTemplate objects (MN 112 labeller/template
|
|
141
|
+
# references are by instance name).
|
|
142
|
+
def prepared_attributes(_name, instance)
|
|
143
|
+
attrs = instance.attributes.dup
|
|
144
|
+
substitute_template_ref(attrs, "template")
|
|
145
|
+
substitute_template_ref(attrs, "global_template")
|
|
146
|
+
attrs
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def substitute_template_ref(attrs, key)
|
|
150
|
+
return unless attrs.key?(key)
|
|
151
|
+
|
|
152
|
+
value = attrs[key]
|
|
153
|
+
attrs[key] = resolve_template_list(value)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def resolve_template_list(value)
|
|
157
|
+
refs = value.is_a?(Array) ? value : [value]
|
|
158
|
+
refs.map do |ref|
|
|
159
|
+
case ref
|
|
160
|
+
when String
|
|
161
|
+
template_instance = @index[ref]
|
|
162
|
+
unless template_instance
|
|
163
|
+
raise Kaleidl::ResolutionError,
|
|
164
|
+
"labeller references undefined template instance #{ref.inspect}"
|
|
165
|
+
end
|
|
166
|
+
template_instance.attributes.dup
|
|
167
|
+
when Hash
|
|
168
|
+
ref.dup
|
|
169
|
+
else
|
|
170
|
+
raise Kaleidl::ParseError,
|
|
171
|
+
"invalid template reference: #{ref.inspect}"
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|