mml 2.4.7 → 2.4.8
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 +4 -4
- data/.gitattributes +12 -0
- data/.github/workflows/corpus.yml +106 -0
- data/.github/workflows/portable-models.yml +32 -0
- data/.github/workflows/release.yml +10 -10
- data/.rubocop.yml +5 -0
- data/README.adoc +14 -0
- data/lib/mml/opal.rb +1 -0
- data/lib/mml/portable.rb +53 -0
- data/lib/mml/version.rb +1 -1
- data/lib/mml.rb +1 -0
- data/mml.gemspec +2 -1
- data/model/portable/manifest.json +31 -0
- data/model/portable/mathml-v3.bundle.json +1 -0
- data/model/portable/mathml-v4.bundle.json +1 -0
- data/scripts/export_portable_models.rb +66 -0
- data/scripts/replay_corpus.rb +457 -0
- data/scripts/verify_portable_models.rb +38 -0
- metadata +12 -3
- data/CLAUDE.md +0 -145
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Export the actual versioned model graphs through the upstream LML API.
|
|
4
|
+
require "json"
|
|
5
|
+
require "digest"
|
|
6
|
+
require "fileutils"
|
|
7
|
+
require "open3"
|
|
8
|
+
$stdout.sync = true
|
|
9
|
+
|
|
10
|
+
ROOT = File.expand_path("..", __dir__)
|
|
11
|
+
FRAMEWORK_REVISION = "fd2989fd8478b1ecb2508d98fa2d66792c09c72b"
|
|
12
|
+
MODEL_SOURCE_REVISION = "a88197810737e7eb809bbb5f887703a25beac3db"
|
|
13
|
+
framework = ENV.fetch("LUTAML_MODEL_SOURCE") do
|
|
14
|
+
abort "set LUTAML_MODEL_SOURCE to the pinned upstream source checkout"
|
|
15
|
+
end
|
|
16
|
+
revision, status = Open3.capture2("git", "-C", framework, "rev-parse", "HEAD")
|
|
17
|
+
abort "unexpected model framework revision" unless status.success? && revision.strip == FRAMEWORK_REVISION
|
|
18
|
+
changes, status = Open3.capture2("git", "-C", framework, "status", "--porcelain")
|
|
19
|
+
abort "the model framework checkout must be clean" unless status.success? && changes.empty?
|
|
20
|
+
$LOAD_PATH.unshift(File.join(File.expand_path(framework), "lib"))
|
|
21
|
+
$LOAD_PATH.unshift(File.join(ROOT, "lib"))
|
|
22
|
+
require "mml"
|
|
23
|
+
require "mml/version"
|
|
24
|
+
require "lutaml/model/lml"
|
|
25
|
+
|
|
26
|
+
abort "usage: ruby scripts/export_portable_models.rb OUTPUT_DIRECTORY" unless ARGV.length == 1
|
|
27
|
+
abort "the portable LML bundle API is unavailable" unless Lutaml::Model::Lml.respond_to?(:export_bundle)
|
|
28
|
+
if framework
|
|
29
|
+
source = Lutaml::Model::Lml.method(:export_bundle).source_location.first
|
|
30
|
+
expected = File.join(File.expand_path(framework), "lib/lutaml/model/lml.rb")
|
|
31
|
+
abort "a different model framework was loaded" unless File.realpath(source) == File.realpath(expected)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
output = File.expand_path(ARGV.first)
|
|
35
|
+
FileUtils.mkdir_p(output)
|
|
36
|
+
manifest = {
|
|
37
|
+
"format" => "mml-portable-models/1",
|
|
38
|
+
"source_repository" => "https://github.com/plurimath/mml",
|
|
39
|
+
"source_revision" => MODEL_SOURCE_REVISION,
|
|
40
|
+
"source_version" => Mml::VERSION,
|
|
41
|
+
"framework_repository" => "https://github.com/lutaml/lutaml-model",
|
|
42
|
+
"framework_revision" => FRAMEWORK_REVISION,
|
|
43
|
+
"framework_status" => "open candidate; not a released dependency",
|
|
44
|
+
"versions" => {},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
[3, 4].each do |version|
|
|
48
|
+
namespace = Mml.const_get("V#{version}")
|
|
49
|
+
config = namespace::Configuration
|
|
50
|
+
config.populate_context! unless config.context
|
|
51
|
+
register = config.context_id
|
|
52
|
+
bundle = Lutaml::Model::Lml.export_bundle(namespace::Math, register: register)
|
|
53
|
+
bytes = "#{JSON.generate(bundle.to_h)}\n"
|
|
54
|
+
filename = "mathml-v#{version}.bundle.json"
|
|
55
|
+
File.write(File.join(output, filename), bytes)
|
|
56
|
+
manifest["versions"][version.to_s] = {
|
|
57
|
+
"path" => filename,
|
|
58
|
+
"sha256" => Digest::SHA256.hexdigest(bytes),
|
|
59
|
+
"bundle_checksum" => bundle.checksum,
|
|
60
|
+
"models" => bundle.to_h.fetch("models").size,
|
|
61
|
+
"root" => bundle.to_h.fetch("roots").first,
|
|
62
|
+
"capabilities" => bundle.to_h.fetch("capabilities"),
|
|
63
|
+
}
|
|
64
|
+
puts "MathML #{version}: #{manifest['versions'][version.to_s]['models']} models, #{bundle.checksum}"
|
|
65
|
+
end
|
|
66
|
+
File.write(File.join(output, "manifest.json"), "#{JSON.pretty_generate(manifest)}\n")
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Replays the plurimath-testsuite mathml stage generation pinned in
|
|
4
|
+
# spec/fixtures/testsuite-pin.yml at the gem's own boundary, model-roundtrip.
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# bundle exec ruby scripts/replay_corpus.rb --testsuite PATH [--pin FILE] \
|
|
8
|
+
# [--language mathml] [--generation N] [--report FILE]
|
|
9
|
+
#
|
|
10
|
+
# Before any row runs it checks the pin: its schema and fields, that the
|
|
11
|
+
# checkout is at the pinned commit with no uncommitted stage files, that the
|
|
12
|
+
# ledger has the pinned generation with the pinned snapshot digest, that the
|
|
13
|
+
# digest recomputed from the generation's files is the same and that every
|
|
14
|
+
# payload has its recorded checksum. --language and --generation, when given,
|
|
15
|
+
# must equal the pin's. Then every row of every model-roundtrip payload is
|
|
16
|
+
# parsed with Mml.parse, its model written in the canonical tagged form that
|
|
17
|
+
# the testsuite's scripts/generate-xml-stage-cases.rb writes, serialized with
|
|
18
|
+
# to_xml, and the outcome (or the refusal) must equal the row's expected
|
|
19
|
+
# value. Payloads at other boundaries (the facade-output rows) are skipped.
|
|
20
|
+
# Any pin failure, any difference or a generation with no model-roundtrip
|
|
21
|
+
# rows exits 1.
|
|
22
|
+
|
|
23
|
+
require "bigdecimal"
|
|
24
|
+
require "digest"
|
|
25
|
+
require "fileutils"
|
|
26
|
+
require "json"
|
|
27
|
+
require "open3"
|
|
28
|
+
require "optparse"
|
|
29
|
+
require "yaml"
|
|
30
|
+
require "mml"
|
|
31
|
+
|
|
32
|
+
module MmlReplayCorpus
|
|
33
|
+
class Error < StandardError; end
|
|
34
|
+
|
|
35
|
+
ROOT = File.expand_path("..", __dir__)
|
|
36
|
+
DEFAULT_PIN = File.join(ROOT, "spec", "fixtures", "testsuite-pin.yml")
|
|
37
|
+
PIN_SCHEMA = "plurimath-stage/pin/1"
|
|
38
|
+
PIN_KEYS = %w[schema repository commit language generation
|
|
39
|
+
snapshot_digest].freeze
|
|
40
|
+
REPOSITORY = "https://github.com/plurimath/plurimath-testsuite"
|
|
41
|
+
LANGUAGE = "mathml"
|
|
42
|
+
BOUNDARY = "model-roundtrip"
|
|
43
|
+
RUN_OPTIONS = %i[language generation report].freeze
|
|
44
|
+
OPTIONS = [
|
|
45
|
+
[:testsuite, "--testsuite PATH"], [:pin, "--pin FILE"],
|
|
46
|
+
[:language, "--language NAME"], [:generation, "--generation N", Integer],
|
|
47
|
+
[:report, "--report FILE"]
|
|
48
|
+
].freeze
|
|
49
|
+
|
|
50
|
+
# Each pin field's check, in order, with the message when it fails.
|
|
51
|
+
PIN_CHECKS = [
|
|
52
|
+
["schema", ->(v) { v == PIN_SCHEMA },
|
|
53
|
+
->(_) { "schema is not #{PIN_SCHEMA}" }],
|
|
54
|
+
["repository", ->(v) { v == REPOSITORY },
|
|
55
|
+
->(_) { "repository is not #{REPOSITORY}" }],
|
|
56
|
+
["commit", ->(v) { v.is_a?(String) && v.match?(/\A\h{40}\z/) },
|
|
57
|
+
->(_) { "commit is not a 40-hex commit" }],
|
|
58
|
+
["language", ->(v) { v == LANGUAGE },
|
|
59
|
+
->(v) { "language #{v.inspect} is not #{LANGUAGE}" }],
|
|
60
|
+
["generation", ->(v) { v.is_a?(Integer) && v >= 1 },
|
|
61
|
+
->(_) { "generation is not a positive integer" }],
|
|
62
|
+
["snapshot_digest",
|
|
63
|
+
->(v) { v.is_a?(String) && v.match?(/\Asha256:\h{64}\z/) },
|
|
64
|
+
->(_) { "snapshot_digest is not sha256:<64 hex>" }],
|
|
65
|
+
].freeze
|
|
66
|
+
|
|
67
|
+
Pin = Struct.new(:commit, :language, :generation, :digest,
|
|
68
|
+
keyword_init: true)
|
|
69
|
+
|
|
70
|
+
module_function
|
|
71
|
+
|
|
72
|
+
# The pin at +path+, its fields checked against plurimath-stage/pin/1.
|
|
73
|
+
def load_pin(path)
|
|
74
|
+
doc = YAML.safe_load_file(path, aliases: false)
|
|
75
|
+
raise Error, "#{path}: not a mapping" unless doc.is_a?(Hash)
|
|
76
|
+
|
|
77
|
+
check_pin_keys(path, doc)
|
|
78
|
+
check_pin_fields(path, doc)
|
|
79
|
+
Pin.new(commit: doc["commit"], language: doc["language"],
|
|
80
|
+
generation: doc["generation"], digest: doc["snapshot_digest"])
|
|
81
|
+
rescue Psych::Exception, Errno::ENOENT => e
|
|
82
|
+
raise Error, "#{path}: #{e.message}"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def check_pin_fields(path, doc)
|
|
86
|
+
PIN_CHECKS.each do |key, valid, message|
|
|
87
|
+
next if valid.call(doc[key])
|
|
88
|
+
|
|
89
|
+
raise Error, "#{path}: #{message.call(doc[key])}"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def check_pin_keys(path, doc)
|
|
94
|
+
missing = PIN_KEYS - doc.keys
|
|
95
|
+
extra = doc.keys - PIN_KEYS
|
|
96
|
+
raise Error, "#{path}: missing #{missing.join(', ')}" unless missing.empty?
|
|
97
|
+
raise Error, "#{path}: unknown keys #{extra.join(', ')}" unless extra.empty?
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def git(dir, *args)
|
|
101
|
+
out, err, status = Open3.capture3("git", "-C", dir, *args)
|
|
102
|
+
unless status.success?
|
|
103
|
+
raise Error, "git #{args.join(' ')} in #{dir}: #{err.strip}"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
out.strip
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The testsuite's snapshot digest of a generation's { path => bytes }.
|
|
110
|
+
def snapshot_digest(files)
|
|
111
|
+
lines = files.sort_by { |path, _| path.b }.map do |path, bytes|
|
|
112
|
+
"#{Digest::SHA256.hexdigest(bytes)} #{path}\n"
|
|
113
|
+
end
|
|
114
|
+
"sha256:#{Digest::SHA256.hexdigest(lines.join.encode('UTF-8'))}"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# [generation dir, { payload path => bytes }] once the checkout matches +pin+.
|
|
118
|
+
def verify(testsuite, pin)
|
|
119
|
+
check_checkout(testsuite, pin)
|
|
120
|
+
entry = ledger_entry(testsuite, pin)
|
|
121
|
+
files = generation_files(testsuite, entry, pin)
|
|
122
|
+
listed = check_provenance(entry, files)
|
|
123
|
+
[entry["path"], listed.to_h { |p| [p["path"], files.fetch(p["path"])] }]
|
|
124
|
+
rescue KeyError, Psych::Exception => e
|
|
125
|
+
raise Error, e.message
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def check_checkout(testsuite, pin)
|
|
129
|
+
head = git(testsuite, "rev-parse", "HEAD")
|
|
130
|
+
unless head == pin.commit
|
|
131
|
+
raise Error, "pin commit #{pin.commit} is not the checkout's HEAD #{head}"
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
dirty = git(testsuite, "status", "--porcelain", "--", "stages",
|
|
135
|
+
"generations")
|
|
136
|
+
return if dirty.empty?
|
|
137
|
+
|
|
138
|
+
raise Error, "testsuite checkout has uncommitted stage files:\n#{dirty}"
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def ledger_entry(testsuite, pin)
|
|
142
|
+
ledger_path = File.join(testsuite, "generations", "#{pin.language}.yaml")
|
|
143
|
+
raise Error, "#{ledger_path} does not exist" unless File.file?(ledger_path)
|
|
144
|
+
|
|
145
|
+
entries = YAML.safe_load_file(ledger_path, aliases: false).fetch("entries")
|
|
146
|
+
entry = entries.find { |e| e["generation"] == pin.generation }
|
|
147
|
+
unless entry
|
|
148
|
+
raise Error, "generations/#{pin.language}.yaml has no generation " \
|
|
149
|
+
"#{pin.generation}"
|
|
150
|
+
end
|
|
151
|
+
check_ledger_digest(entry, pin)
|
|
152
|
+
entry
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def check_ledger_digest(entry, pin)
|
|
156
|
+
return if entry["snapshot_digest"] == pin.digest
|
|
157
|
+
|
|
158
|
+
raise Error, "ledger digest #{entry['snapshot_digest']} of generation " \
|
|
159
|
+
"#{pin.generation} is not the expected #{pin.digest}"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# { path => bytes } of the generation's files, its digest checked.
|
|
163
|
+
def generation_files(testsuite, entry, pin)
|
|
164
|
+
dir = File.join(testsuite, entry.fetch("path"))
|
|
165
|
+
files = generation_paths(dir, entry).to_h do |p|
|
|
166
|
+
[p, File.binread(File.join(dir, p))]
|
|
167
|
+
end
|
|
168
|
+
digest = snapshot_digest(files)
|
|
169
|
+
return files if digest == pin.digest
|
|
170
|
+
|
|
171
|
+
raise Error, "#{entry['path']}: digest #{digest} does not match the " \
|
|
172
|
+
"ledger's #{pin.digest}"
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# The generation's file paths, none of them a symlink.
|
|
176
|
+
def generation_paths(dir, entry)
|
|
177
|
+
paths = Dir.glob("**/*", File::FNM_DOTMATCH, base: dir)
|
|
178
|
+
paths = paths.reject { |p| File.directory?(File.join(dir, p)) }
|
|
179
|
+
paths.each do |p|
|
|
180
|
+
next unless File.symlink?(File.join(dir, p))
|
|
181
|
+
|
|
182
|
+
raise Error, "#{entry['path']}/#{p} is a symlink"
|
|
183
|
+
end
|
|
184
|
+
paths
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# The provenance's payload list, checked against the generation's files.
|
|
188
|
+
def check_provenance(entry, files)
|
|
189
|
+
provenance = YAML.safe_load(files.fetch("provenance.yaml") do
|
|
190
|
+
raise Error, "#{entry['path']} has no provenance.yaml"
|
|
191
|
+
end)
|
|
192
|
+
listed = provenance.fetch("payloads")
|
|
193
|
+
check_listed(entry, files, listed)
|
|
194
|
+
listed.each { |p| check_checksum(entry, files, p) }
|
|
195
|
+
listed
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def check_listed(entry, files, listed)
|
|
199
|
+
payload_paths = (files.keys - ["provenance.yaml"]).sort
|
|
200
|
+
return if listed.map { |p| p["path"] }.sort == payload_paths
|
|
201
|
+
|
|
202
|
+
raise Error, "#{entry['path']}: provenance does not list exactly the " \
|
|
203
|
+
"payload files"
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def check_checksum(entry, files, payload)
|
|
207
|
+
actual = Digest::SHA256.hexdigest(files.fetch(payload["path"]))
|
|
208
|
+
return if actual == payload["sha256"]
|
|
209
|
+
|
|
210
|
+
raise Error, "#{entry['path']}/#{payload['path']}: sha256 #{actual} " \
|
|
211
|
+
"is not #{payload['sha256']}"
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Every model-roundtrip row of the generation, with its payload path.
|
|
215
|
+
def roundtrip_rows(payloads)
|
|
216
|
+
payloads.flat_map do |rel, bytes|
|
|
217
|
+
doc = YAML.safe_load(bytes, aliases: false)
|
|
218
|
+
next [] unless doc["boundary"] == BOUNDARY
|
|
219
|
+
|
|
220
|
+
doc.fetch("cases").map { |row| [rel, row] }
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def decode(row, where)
|
|
225
|
+
value = row.fetch("input")
|
|
226
|
+
unless canonical?(value)
|
|
227
|
+
raise Error, "#{where}: input is not a canonical string"
|
|
228
|
+
end
|
|
229
|
+
unless row.fetch("options", {}).empty?
|
|
230
|
+
raise Error,
|
|
231
|
+
"#{where}: unsupported options #{row['options'].keys.join(', ')}"
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
value["str"]
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def canonical?(value)
|
|
238
|
+
value.is_a?(Hash) && value.keys == ["str"] && value["str"].is_a?(String)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
SCALAR_TAGS = {
|
|
242
|
+
NilClass => ->(_) { { "nil" => true } },
|
|
243
|
+
TrueClass => ->(v) { { "bool" => v } },
|
|
244
|
+
FalseClass => ->(v) { { "bool" => v } },
|
|
245
|
+
Integer => ->(v) { { "int" => v.to_s } },
|
|
246
|
+
Float => ->(v) { { "float" => v.inspect } },
|
|
247
|
+
BigDecimal => ->(v) { { "decimal" => v.to_s } },
|
|
248
|
+
Symbol => ->(v) { { "sym" => v.to_s } },
|
|
249
|
+
}.freeze
|
|
250
|
+
|
|
251
|
+
# The canonical tagged form of docs/tree-parity-contract.adoc, as
|
|
252
|
+
# scripts/generate-xml-stage-cases.rb writes it.
|
|
253
|
+
def tag(value, path = [])
|
|
254
|
+
case value
|
|
255
|
+
when String then tag_string(value)
|
|
256
|
+
when Array then { "array" => value.map { |v| tag(v, path) } }
|
|
257
|
+
when Hash then tag_hash(value, path)
|
|
258
|
+
when Lutaml::Model::Serializable then tag_model(value, path)
|
|
259
|
+
else tag_scalar(value)
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def tag_scalar(value)
|
|
264
|
+
_, tagger = SCALAR_TAGS.find { |klass, _| value.is_a?(klass) }
|
|
265
|
+
raise Error, "no tagged form for #{value.class}" unless tagger
|
|
266
|
+
|
|
267
|
+
tagger.call(value)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def tag_hash(value, path)
|
|
271
|
+
{ "hash" => value.map { |k, v| [tag(k, path), tag(v, path)] } }
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def tag_string(value)
|
|
275
|
+
if value.encoding == Encoding::UTF_8 && value.valid_encoding?
|
|
276
|
+
{ "str" => value.to_s }
|
|
277
|
+
else
|
|
278
|
+
{ "bytes" => value.unpack1("H*"), "encoding" => value.encoding.name }
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def tag_model(model, path)
|
|
283
|
+
if path.any? { |seen| seen.equal?(model) }
|
|
284
|
+
raise Error, "cycle at #{model.class}"
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
fields = model_fields(model, path + [model])
|
|
288
|
+
order = element_order(model)
|
|
289
|
+
fields << ["@element_order", order] if order
|
|
290
|
+
{ "node" => model.class.name, "fields" => fields }
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
def element_order(model)
|
|
294
|
+
order = model.element_order if model.respond_to?(:element_order)
|
|
295
|
+
return if order.nil? || order.empty?
|
|
296
|
+
|
|
297
|
+
{ "array" => order.map { |e| tag_order(e) } }
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def model_fields(model, inner)
|
|
301
|
+
model.class.attributes.filter_map do |name, _attribute|
|
|
302
|
+
next if model.using_default?(name)
|
|
303
|
+
|
|
304
|
+
[name.to_s, tag(model.public_send(name), inner)]
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def tag_order(entry)
|
|
309
|
+
pairs = [["type", entry.type], ["name", entry.name]]
|
|
310
|
+
pairs << ["text", entry.text_content] if entry.node_type == :text
|
|
311
|
+
{ "hash" => pairs.map { |k, v| [{ "str" => k }, tag(v)] } }
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def refusal(category, error)
|
|
315
|
+
{ "refusal" => { "category" => category,
|
|
316
|
+
"error_class" => error.class.name } }
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# The row's outcome, in the form the stage payloads record.
|
|
320
|
+
def outcome(input)
|
|
321
|
+
begin
|
|
322
|
+
model = Mml.parse(input)
|
|
323
|
+
rescue StandardError => e
|
|
324
|
+
return refusal("parse_error", e)
|
|
325
|
+
end
|
|
326
|
+
serialized_outcome(model)
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def serialized_outcome(model)
|
|
330
|
+
begin
|
|
331
|
+
output = model.to_xml
|
|
332
|
+
rescue StandardError => e
|
|
333
|
+
return refusal("serialize_error", e)
|
|
334
|
+
end
|
|
335
|
+
{ "model" => tag(model), "output" => tag_string(output) }
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
def replay(rows)
|
|
339
|
+
rows.filter_map do |rel, row|
|
|
340
|
+
actual = outcome(decode(row, "#{rel} #{row['id']}"))
|
|
341
|
+
next if actual == row.fetch("expected")
|
|
342
|
+
|
|
343
|
+
{ "id" => row["id"], "payload" => rel, "expected" => row["expected"],
|
|
344
|
+
"actual" => actual }
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# Options: language:, generation: (each must equal the pin's) and
|
|
349
|
+
# report: (a file to write the summary to).
|
|
350
|
+
def run(testsuite:, pin: DEFAULT_PIN, log: $stderr, **options)
|
|
351
|
+
check_run_options(options)
|
|
352
|
+
pinned = load_pin(pin)
|
|
353
|
+
check_expected(pinned, options)
|
|
354
|
+
rows, payloads = pinned_rows(testsuite, pinned)
|
|
355
|
+
log.puts "pin ok: #{pinned.language} generation #{pinned.generation} " \
|
|
356
|
+
"at #{pinned.commit}, #{pinned.digest}"
|
|
357
|
+
summary = summarize(pinned, rows, payloads, replay(rows))
|
|
358
|
+
write_report(options[:report], summary)
|
|
359
|
+
finish(log, rows, summary)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def check_run_options(options)
|
|
363
|
+
unknown = options.keys - RUN_OPTIONS
|
|
364
|
+
return if unknown.empty?
|
|
365
|
+
|
|
366
|
+
raise ArgumentError, "unknown keywords: #{unknown.join(', ')}"
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def check_expected(pinned, options)
|
|
370
|
+
language = options[:language]
|
|
371
|
+
generation = options[:generation]
|
|
372
|
+
if language && language != pinned.language
|
|
373
|
+
raise Error, "--language #{language} is not the pinned #{pinned.language}"
|
|
374
|
+
end
|
|
375
|
+
return unless generation && generation != pinned.generation
|
|
376
|
+
|
|
377
|
+
raise Error,
|
|
378
|
+
"--generation #{generation} is not the pinned #{pinned.generation}"
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def pinned_rows(testsuite, pinned)
|
|
382
|
+
_dir, payloads = verify(File.expand_path(testsuite), pinned)
|
|
383
|
+
rows = roundtrip_rows(payloads)
|
|
384
|
+
return [rows, payloads] unless rows.empty?
|
|
385
|
+
|
|
386
|
+
raise Error, "#{pinned.language} generation #{pinned.generation} has no " \
|
|
387
|
+
"#{BOUNDARY} rows"
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def summarize(pinned, rows, payloads, failures)
|
|
391
|
+
replayed = rows.map(&:first).uniq.size
|
|
392
|
+
{
|
|
393
|
+
"commit" => pinned.commit, "language" => pinned.language,
|
|
394
|
+
"generation" => pinned.generation, "snapshot_digest" => pinned.digest,
|
|
395
|
+
"gem_version" => Mml::VERSION,
|
|
396
|
+
"#{BOUNDARY}_payloads" => replayed, "#{BOUNDARY}_rows" => rows.size,
|
|
397
|
+
"refusal_rows" => rows.count { |_, row| row["expected"].key?("refusal") },
|
|
398
|
+
"skipped_payloads" => payloads.size - replayed,
|
|
399
|
+
"mismatched_rows" => failures.size, "failures" => failures
|
|
400
|
+
}
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def write_report(report, summary)
|
|
404
|
+
return unless report
|
|
405
|
+
|
|
406
|
+
FileUtils.mkdir_p(File.dirname(report))
|
|
407
|
+
File.binwrite(report, "#{JSON.pretty_generate(summary, max_nesting: false)}\n")
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def finish(log, rows, summary)
|
|
411
|
+
failures = summary["failures"]
|
|
412
|
+
log.puts "#{rows.size} #{BOUNDARY} rows (#{summary['refusal_rows']} " \
|
|
413
|
+
"refusals) from #{summary["#{BOUNDARY}_payloads"]} payloads, " \
|
|
414
|
+
"#{summary['skipped_payloads']} other payloads skipped: " \
|
|
415
|
+
"#{failures.size} differ"
|
|
416
|
+
return summary if failures.empty?
|
|
417
|
+
|
|
418
|
+
raise Error, "#{failures.size} rows differ: " \
|
|
419
|
+
"#{failures.map { |f| f['id'] }.join(', ')}"
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
def main(argv)
|
|
423
|
+
options = parse_options(argv)
|
|
424
|
+
run(**options)
|
|
425
|
+
0
|
|
426
|
+
rescue OptionParser::ParseError => e
|
|
427
|
+
warn e.message
|
|
428
|
+
2
|
|
429
|
+
rescue Error => e
|
|
430
|
+
warn "replay_corpus: #{e.message}"
|
|
431
|
+
1
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def parse_options(argv)
|
|
435
|
+
options = {}
|
|
436
|
+
option_parser(options).parse!(argv)
|
|
437
|
+
unless options[:testsuite]
|
|
438
|
+
raise OptionParser::MissingArgument, "--testsuite"
|
|
439
|
+
end
|
|
440
|
+
raise OptionParser::InvalidOption, argv.join(" ") unless argv.empty?
|
|
441
|
+
|
|
442
|
+
options
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def option_parser(options)
|
|
446
|
+
OptionParser.new do |o|
|
|
447
|
+
o.banner = "usage: scripts/replay_corpus.rb --testsuite PATH " \
|
|
448
|
+
"[--pin FILE] [--language NAME] [--generation N] " \
|
|
449
|
+
"[--report FILE]"
|
|
450
|
+
OPTIONS.each do |key, *switch|
|
|
451
|
+
o.on(*switch) { |v| options[key] = v }
|
|
452
|
+
end
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
exit MmlReplayCorpus.main(ARGV) if $PROGRAM_NAME == __FILE__
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
framework = ENV.fetch("LUTAML_MODEL_SOURCE")
|
|
5
|
+
$LOAD_PATH.unshift(File.join(File.expand_path(framework), "lib"))
|
|
6
|
+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
|
|
7
|
+
require "mml"
|
|
8
|
+
require "lutaml/model/lml"
|
|
9
|
+
|
|
10
|
+
Lutaml::Model::Config.xml_adapter_type = :nokogiri
|
|
11
|
+
namespace = "http://www.w3.org/1998/Math/MathML"
|
|
12
|
+
inputs = {
|
|
13
|
+
"mi-mixed" => ["Mi", %(<mi xmlns="#{namespace}">x<malignmark/>y<mglyph src="g.png" alt="g"/>z</mi>)],
|
|
14
|
+
"mi-prefixed" => ["Mi", %(<m:mi xmlns:m="#{namespace}" mathvariant="bold">alpha</m:mi>)],
|
|
15
|
+
"math-nested" => ["Math", %(<math xmlns="#{namespace}" display="block"><mrow><mi>a</mi><mo>+</mo><mfrac><mi>x<malignmark/>y</mi><mrow><mn>1</mn><mo>+</mo><mn>2</mn></mrow></mfrac><mi>b</mi></mrow><mi>c</mi></math>)],
|
|
16
|
+
}
|
|
17
|
+
executed = []
|
|
18
|
+
[3, 4].each do |version|
|
|
19
|
+
mod = Mml.const_get("V#{version}")
|
|
20
|
+
config = mod::Configuration
|
|
21
|
+
config.populate_context! unless config.context
|
|
22
|
+
register = config.context_id
|
|
23
|
+
bundle = Lutaml::Model::Lml::Importer.bundle(Mml::Portable.bundle(version: version))
|
|
24
|
+
classes = Lutaml::Model::Lml.import_bundle(bundle)
|
|
25
|
+
inputs.each do |name, (class_name, input)|
|
|
26
|
+
source = mod.const_get(class_name).from_xml(input, register: register).to_xml(register: register)
|
|
27
|
+
klass = classes.fetch("Mml::V#{version}::#{class_name}@#{register}")
|
|
28
|
+
actual = klass.from_xml(input, register: register).to_xml(register: register)
|
|
29
|
+
abort "v#{version}-#{name}: imported writer differs from source" unless source == actual
|
|
30
|
+
repeated = klass.from_xml(actual, register: register).to_xml(register: register)
|
|
31
|
+
abort "v#{version}-#{name}: writer roundtrip is unstable" unless repeated == actual
|
|
32
|
+
|
|
33
|
+
executed << "v#{version}-#{name}"
|
|
34
|
+
puts "PASS #{executed.last}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
abort "expected exactly 6 model/writer cases" unless executed.size == 6
|
|
38
|
+
puts JSON.generate(expected: 6, executed: executed.size, failed: 0, case_ids: executed)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.4.
|
|
4
|
+
version: 2.4.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: lutaml-model
|
|
@@ -47,8 +47,11 @@ executables: []
|
|
|
47
47
|
extensions: []
|
|
48
48
|
extra_rdoc_files: []
|
|
49
49
|
files:
|
|
50
|
+
- ".gitattributes"
|
|
51
|
+
- ".github/workflows/corpus.yml"
|
|
50
52
|
- ".github/workflows/notify-mml-js.yml"
|
|
51
53
|
- ".github/workflows/opal.yml"
|
|
54
|
+
- ".github/workflows/portable-models.yml"
|
|
52
55
|
- ".github/workflows/rake.yml"
|
|
53
56
|
- ".github/workflows/release.yml"
|
|
54
57
|
- ".gitignore"
|
|
@@ -56,7 +59,6 @@ files:
|
|
|
56
59
|
- ".rspec"
|
|
57
60
|
- ".rubocop.yml"
|
|
58
61
|
- ".rubocop_todo.yml"
|
|
59
|
-
- CLAUDE.md
|
|
60
62
|
- CODE_OF_CONDUCT.md
|
|
61
63
|
- Gemfile
|
|
62
64
|
- README.adoc
|
|
@@ -172,6 +174,7 @@ files:
|
|
|
172
174
|
- lib/mml/context_options.rb
|
|
173
175
|
- lib/mml/namespace.rb
|
|
174
176
|
- lib/mml/opal.rb
|
|
177
|
+
- lib/mml/portable.rb
|
|
175
178
|
- lib/mml/v2.rb
|
|
176
179
|
- lib/mml/v2/annotation.rb
|
|
177
180
|
- lib/mml/v2/annotation_xml.rb
|
|
@@ -397,6 +400,9 @@ files:
|
|
|
397
400
|
- lib/mml/version.rb
|
|
398
401
|
- lib/mml/versioned_parser.rb
|
|
399
402
|
- mml.gemspec
|
|
403
|
+
- model/portable/manifest.json
|
|
404
|
+
- model/portable/mathml-v3.bundle.json
|
|
405
|
+
- model/portable/mathml-v4.bundle.json
|
|
400
406
|
- reference-docs/mathml-source/pubtext/mathmlspec.dtd
|
|
401
407
|
- reference-docs/mathml-source/pubtext/xmlspec.dtd
|
|
402
408
|
- reference-docs/mathml-source/readme.txt
|
|
@@ -477,6 +483,9 @@ files:
|
|
|
477
483
|
- schemas/mathml4/mathml4.rnc
|
|
478
484
|
- schemas/mathml4/mathml4.rnc.1
|
|
479
485
|
- schemas/mathml4/mathml4.rng
|
|
486
|
+
- scripts/export_portable_models.rb
|
|
487
|
+
- scripts/replay_corpus.rb
|
|
488
|
+
- scripts/verify_portable_models.rb
|
|
480
489
|
- sig/mml.rbs
|
|
481
490
|
homepage: https://github.com/plurimath/mml
|
|
482
491
|
licenses:
|