lutaml-lml 0.3.5 → 0.3.7
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/lib/lutaml-lml.rb +5 -0
- data/lib/lutaml_lml/version.rb +6 -0
- metadata +12 -164
- data/exe/lutaml-lml +0 -6
- data/exe/lutaml-lml-lsp +0 -11
- data/lib/lutaml/lml/association_label_resolver.rb +0 -52
- data/lib/lutaml/lml/cli.rb +0 -489
- data/lib/lutaml/lml/data_processor/attribute_processing.rb +0 -133
- data/lib/lutaml/lml/data_processor/collection_processing.rb +0 -37
- data/lib/lutaml/lml/data_processor/instance_processing.rb +0 -77
- data/lib/lutaml/lml/data_processor/value_declaration_processing.rb +0 -70
- data/lib/lutaml/lml/data_processor/value_processing.rb +0 -103
- data/lib/lutaml/lml/data_processor/view_processing.rb +0 -26
- data/lib/lutaml/lml/data_processor.rb +0 -59
- data/lib/lutaml/lml/diagnostic.rb +0 -149
- data/lib/lutaml/lml/document_builder.rb +0 -481
- data/lib/lutaml/lml/entity_types.rb +0 -30
- data/lib/lutaml/lml/executor/adapter_helpers.rb +0 -45
- data/lib/lutaml/lml/executor/condition_evaluator.rb +0 -169
- data/lib/lutaml/lml/executor/csv_adapter.rb +0 -88
- data/lib/lutaml/lml/executor/format_adapter.rb +0 -54
- data/lib/lutaml/lml/executor/xml_adapter.rb +0 -102
- data/lib/lutaml/lml/executor.rb +0 -96
- data/lib/lutaml/lml/format/adapter/document.rb +0 -11
- data/lib/lutaml/lml/format/adapter/mapping.rb +0 -31
- data/lib/lutaml/lml/format/adapter/standard_adapter.rb +0 -162
- data/lib/lutaml/lml/format/adapter/transform.rb +0 -11
- data/lib/lutaml/lml/format.rb +0 -29
- data/lib/lutaml/lml/formatter/base.rb +0 -79
- data/lib/lutaml/lml/formatter/canonical.rb +0 -736
- data/lib/lutaml/lml/formatter/elk.rb +0 -45
- data/lib/lutaml/lml/formatter/graphviz/document_formatter.rb +0 -89
- data/lib/lutaml/lml/formatter/graphviz/html_builder.rb +0 -72
- data/lib/lutaml/lml/formatter/graphviz/node_formatter.rb +0 -89
- data/lib/lutaml/lml/formatter/graphviz/relationship_formatter.rb +0 -130
- data/lib/lutaml/lml/formatter/graphviz.rb +0 -90
- data/lib/lutaml/lml/formatter/svg_pdf/path_parser.rb +0 -134
- data/lib/lutaml/lml/formatter/svg_pdf.rb +0 -285
- data/lib/lutaml/lml/formatter.rb +0 -12
- data/lib/lutaml/lml/grammar/lml.artifact.json +0 -14993
- data/lib/lutaml/lml/grammar/lml.parg +0 -389
- data/lib/lutaml/lml/grammar.rb +0 -60
- data/lib/lutaml/lml/has_attributes.rb +0 -14
- data/lib/lutaml/lml/import_resolver.rb +0 -96
- data/lib/lutaml/lml/layout/elk_engine.rb +0 -15
- data/lib/lutaml/lml/layout/elk_graph_builder.rb +0 -129
- data/lib/lutaml/lml/layout/elk_svg_markers.rb +0 -45
- data/lib/lutaml/lml/layout/elk_svg_renderer.rb +0 -119
- data/lib/lutaml/lml/layout/engine.rb +0 -17
- data/lib/lutaml/lml/layout/graph_viz_engine.rb +0 -19
- data/lib/lutaml/lml/layout.rb +0 -12
- data/lib/lutaml/lml/literal_value.rb +0 -32
- data/lib/lutaml/lml/lsp/conversions.rb +0 -78
- data/lib/lutaml/lml/lsp/keywords.rb +0 -43
- data/lib/lutaml/lml/lsp/server.rb +0 -378
- data/lib/lutaml/lml/lsp/store.rb +0 -68
- data/lib/lutaml/lml/lsp/transport.rb +0 -53
- data/lib/lutaml/lml/lsp.rb +0 -16
- data/lib/lutaml/lml/model_compiler/enums.rb +0 -172
- data/lib/lutaml/lml/model_compiler/hydration.rb +0 -123
- data/lib/lutaml/lml/model_compiler/serialization_mappings.rb +0 -188
- data/lib/lutaml/lml/model_compiler/string_formats.rb +0 -42
- data/lib/lutaml/lml/model_compiler/validation.rb +0 -65
- data/lib/lutaml/lml/model_compiler.rb +0 -323
- data/lib/lutaml/lml/models/action.rb +0 -10
- data/lib/lutaml/lml/models/association.rb +0 -29
- data/lib/lutaml/lml/models/cardinality.rb +0 -10
- data/lib/lutaml/lml/models/collection.rb +0 -11
- data/lib/lutaml/lml/models/constraint.rb +0 -23
- data/lib/lutaml/lml/models/data_type.rb +0 -41
- data/lib/lutaml/lml/models/derived_field.rb +0 -14
- data/lib/lutaml/lml/models/diagram.rb +0 -20
- data/lib/lutaml/lml/models/document.rb +0 -84
- data/lib/lutaml/lml/models/enum.rb +0 -38
- data/lib/lutaml/lml/models/fidelity.rb +0 -10
- data/lib/lutaml/lml/models/group.rb +0 -11
- data/lib/lutaml/lml/models/instance.rb +0 -26
- data/lib/lutaml/lml/models/instance_collection.rb +0 -16
- data/lib/lutaml/lml/models/instances_export.rb +0 -10
- data/lib/lutaml/lml/models/instances_import.rb +0 -11
- data/lib/lutaml/lml/models/mapping.rb +0 -15
- data/lib/lutaml/lml/models/mapping_rule.rb +0 -16
- data/lib/lutaml/lml/models/member_field.rb +0 -14
- data/lib/lutaml/lml/models/namespace.rb +0 -16
- data/lib/lutaml/lml/models/operation.rb +0 -26
- data/lib/lutaml/lml/models/operation_parameter.rb +0 -12
- data/lib/lutaml/lml/models/package.rb +0 -26
- data/lib/lutaml/lml/models/primitive_type.rb +0 -33
- data/lib/lutaml/lml/models/reference.rb +0 -23
- data/lib/lutaml/lml/models/serialization_mapping.rb +0 -19
- data/lib/lutaml/lml/models/string_format.rb +0 -16
- data/lib/lutaml/lml/models/top_element_attribute.rb +0 -116
- data/lib/lutaml/lml/models/uml_class.rb +0 -85
- data/lib/lutaml/lml/models/value.rb +0 -15
- data/lib/lutaml/lml/models/view_filter.rb +0 -11
- data/lib/lutaml/lml/models/view_import.rb +0 -11
- data/lib/lutaml/lml/parser.rb +0 -19
- data/lib/lutaml/lml/pipeline.rb +0 -73
- data/lib/lutaml/lml/positioned.rb +0 -13
- data/lib/lutaml/lml/preprocessor.rb +0 -130
- data/lib/lutaml/lml/quoting.rb +0 -60
- data/lib/lutaml/lml/source.rb +0 -49
- data/lib/lutaml/lml/source_position.rb +0 -20
- data/lib/lutaml/lml/symbol_index.rb +0 -200
- data/lib/lutaml/lml/transform.rb +0 -119
- data/lib/lutaml/lml/types/text_type.rb +0 -28
- data/lib/lutaml/lml/types.rb +0 -10
- data/lib/lutaml/lml/validator.rb +0 -458
- data/lib/lutaml/lml/version.rb +0 -7
- data/lib/lutaml/lml/view_resolution.rb +0 -32
- data/lib/lutaml/lml/view_resolver.rb +0 -48
- data/lib/lutaml/lml/yaml_parser.rb +0 -17
- data/lib/lutaml/lml.rb +0 -130
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
class Executor
|
|
6
|
-
# Evaluates collection validation conditions against instance data.
|
|
7
|
-
#
|
|
8
|
-
# Conditions are simple DSL strings like "count >= 3" or
|
|
9
|
-
# "all? { |i| i.components.count > 0 }". The evaluator parses a safe
|
|
10
|
-
# subset of forms — it never calls `eval` on arbitrary Ruby.
|
|
11
|
-
#
|
|
12
|
-
# Supported condition forms:
|
|
13
|
-
# "count >= N" — collection size comparison
|
|
14
|
-
# "count == N", "count <= N", etc.
|
|
15
|
-
# "all? { |i| i.path OP literal }" — every instance matches
|
|
16
|
-
# "any? { |i| i.path OP literal }" — at least one instance matches
|
|
17
|
-
#
|
|
18
|
-
# The block predicate supports a single comparison of an instance
|
|
19
|
-
# attribute path (e.g. `i.components.count`, `i.name`) against a
|
|
20
|
-
# literal (number, quoted string, true/false/nil) using one of
|
|
21
|
-
# `>, >=, <, <=, ==, !=`.
|
|
22
|
-
#
|
|
23
|
-
class ConditionEvaluator
|
|
24
|
-
ConditionError = Class.new(StandardError)
|
|
25
|
-
|
|
26
|
-
BLOCK_PREFIX = /\A\s*(all|any)\?/.freeze
|
|
27
|
-
BLOCK_VAR = /\A\s*\|(\w+)\|/.freeze
|
|
28
|
-
COMPARISON = /\A\s*(.+?)\s*(>=|<=|==|!=|>|<)\s*(.+?)\s*\z/.freeze
|
|
29
|
-
|
|
30
|
-
# Evaluate all validation conditions against a collection of instances.
|
|
31
|
-
# Returns an array of error strings (empty if all pass).
|
|
32
|
-
def self.evaluate(collection, instances)
|
|
33
|
-
return [] unless collection.is_a?(Collection)
|
|
34
|
-
return [] unless collection.validations&.any?
|
|
35
|
-
|
|
36
|
-
new(instances).evaluate_all(collection.validations)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def initialize(instances)
|
|
40
|
-
@instances = instances
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def evaluate_all(conditions)
|
|
44
|
-
conditions.filter_map do |condition|
|
|
45
|
-
evaluate(condition)
|
|
46
|
-
rescue ConditionError => e
|
|
47
|
-
"Validation failed: #{e.message}"
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
private
|
|
52
|
-
|
|
53
|
-
def evaluate(condition)
|
|
54
|
-
return evaluate_block(condition) if condition.match?(/\A\s*(all|any)\?\s*\{/)
|
|
55
|
-
return evaluate_count(condition) if condition.match?(/\bcount\s*[<>=]+/)
|
|
56
|
-
|
|
57
|
-
raise ConditionError, "Unsupported condition: #{condition}"
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def evaluate_count(condition)
|
|
61
|
-
match = condition.match(/\bcount\s*(>=|<=|==|>|<)\s*(\d+)/)
|
|
62
|
-
raise ConditionError, "Invalid count condition: #{condition}" unless match
|
|
63
|
-
|
|
64
|
-
operator = match[1]
|
|
65
|
-
expected = match[2].to_i
|
|
66
|
-
actual = @instances.length
|
|
67
|
-
|
|
68
|
-
compare(actual, operator, expected) ||
|
|
69
|
-
raise(ConditionError, "#{condition} (got #{actual})")
|
|
70
|
-
nil
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def evaluate_block(condition)
|
|
74
|
-
quantifier, predicate = parse_block(condition)
|
|
75
|
-
raise ConditionError, "Invalid block condition: #{condition}" if quantifier.nil?
|
|
76
|
-
|
|
77
|
-
comparator = build_predicate(predicate)
|
|
78
|
-
satisfied = @instances.public_send("#{quantifier}?") do |instance|
|
|
79
|
-
comparator.call(instance)
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
return nil if satisfied
|
|
83
|
-
|
|
84
|
-
raise ConditionError, "#{condition} (failed for #{quantifier == "all" ? "at least one" : "all"} instance)"
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def parse_block(condition)
|
|
88
|
-
stripped = condition.strip
|
|
89
|
-
prefix = stripped.match(BLOCK_PREFIX)
|
|
90
|
-
return [nil, nil] unless prefix
|
|
91
|
-
|
|
92
|
-
open_idx = stripped.index("{")
|
|
93
|
-
close_idx = stripped.rindex("}")
|
|
94
|
-
return [nil, nil] unless open_idx && close_idx && close_idx > open_idx
|
|
95
|
-
|
|
96
|
-
body = stripped[(open_idx + 1)...close_idx].strip
|
|
97
|
-
var_match = body.match(BLOCK_VAR)
|
|
98
|
-
return [nil, nil] unless var_match
|
|
99
|
-
|
|
100
|
-
predicate = body[var_match[0].length..].strip
|
|
101
|
-
[prefix[1], predicate]
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# Parses a single comparison predicate "i.path OP literal" into a
|
|
105
|
-
# callable that, given an instance, returns true/false.
|
|
106
|
-
def build_predicate(predicate)
|
|
107
|
-
match = predicate.match(COMPARISON)
|
|
108
|
-
raise ConditionError, "Unsupported predicate: #{predicate}" unless match
|
|
109
|
-
|
|
110
|
-
lhs = match[1]
|
|
111
|
-
operator = match[2]
|
|
112
|
-
rhs = match[3]
|
|
113
|
-
|
|
114
|
-
getter = attribute_getter(lhs)
|
|
115
|
-
expected = parse_literal(rhs)
|
|
116
|
-
|
|
117
|
-
lambda do |instance|
|
|
118
|
-
actual = getter.call(instance)
|
|
119
|
-
compare(actual, operator, expected)
|
|
120
|
-
rescue NoMethodError
|
|
121
|
-
false
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
# Parses "i.attr.subattr..." into a callable on an instance.
|
|
126
|
-
def attribute_getter(path)
|
|
127
|
-
parts = path.strip.split(".")
|
|
128
|
-
raise ConditionError, "Invalid attribute path: #{path}" if parts.empty?
|
|
129
|
-
|
|
130
|
-
unless parts.first == "i"
|
|
131
|
-
raise ConditionError, "Block predicate must reference i: #{path}"
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
chain = parts.drop(1)
|
|
135
|
-
lambda do |instance|
|
|
136
|
-
chain.reduce(instance) { |receiver, method| receiver.public_send(method) }
|
|
137
|
-
end
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# Parses a single literal value from the predicate RHS.
|
|
141
|
-
def parse_literal(token)
|
|
142
|
-
stripped = token.strip
|
|
143
|
-
case stripped
|
|
144
|
-
when /\A-?\d+\z/ then stripped.to_i
|
|
145
|
-
when /\A-?\d+\.\d+\z/ then stripped.to_f
|
|
146
|
-
when /\A".*"\z/, /\A'.*'\z/ then stripped[1..-2]
|
|
147
|
-
when "true" then true
|
|
148
|
-
when "false" then false
|
|
149
|
-
when "nil", "null" then nil
|
|
150
|
-
else
|
|
151
|
-
raise ConditionError, "Unsupported literal: #{token}"
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
def compare(actual, operator, expected)
|
|
156
|
-
case operator
|
|
157
|
-
when ">=" then actual.to_f >= expected.to_f
|
|
158
|
-
when "<=" then actual.to_f <= expected.to_f
|
|
159
|
-
when "==" then actual == expected
|
|
160
|
-
when "!=" then actual != expected
|
|
161
|
-
when ">" then actual.to_f > expected.to_f
|
|
162
|
-
when "<" then actual.to_f < expected.to_f
|
|
163
|
-
else false
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
end
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "csv"
|
|
4
|
-
|
|
5
|
-
module Lutaml
|
|
6
|
-
module Lml
|
|
7
|
-
class Executor
|
|
8
|
-
# CSV format adapter. Reads CSV files and maps rows to hydrated
|
|
9
|
-
# compiled-class instances using column mappings from the import
|
|
10
|
-
# definition. Exports instances back to CSV.
|
|
11
|
-
#
|
|
12
|
-
# Registered for format "csv" via FormatAdapter::BUILTIN_ADAPTERS.
|
|
13
|
-
class CsvAdapter
|
|
14
|
-
extend AdapterHelpers
|
|
15
|
-
|
|
16
|
-
# Read a CSV file and map rows to compiled-class instances.
|
|
17
|
-
# Returns an array of hydrated objects.
|
|
18
|
-
#
|
|
19
|
-
# The import definition's attributes provide column mappings:
|
|
20
|
-
# attribute name = "csv_column_header"
|
|
21
|
-
def self.import(imp, compiled:)
|
|
22
|
-
return [] unless imp.file
|
|
23
|
-
return [] unless imp.attributes&.any?
|
|
24
|
-
|
|
25
|
-
mappings = build_column_mappings(imp.attributes)
|
|
26
|
-
target_class = resolve_target_class(imp.attributes, compiled)
|
|
27
|
-
return [] unless target_class
|
|
28
|
-
|
|
29
|
-
path = imp.file
|
|
30
|
-
return [] unless File.exist?(path)
|
|
31
|
-
|
|
32
|
-
rows = CSV.read(path, headers: true)
|
|
33
|
-
rows.map { |row| build_instance(row, mappings, target_class) }
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# Write instances to a CSV file.
|
|
37
|
-
def self.export(exp, instances, compiled:)
|
|
38
|
-
return if instances.empty?
|
|
39
|
-
|
|
40
|
-
path = attribute_value(exp.attributes, "file")
|
|
41
|
-
return unless path && !path.empty?
|
|
42
|
-
|
|
43
|
-
first_instance = instances.first
|
|
44
|
-
class_name, target_class = find_class_for_instance(first_instance, compiled)
|
|
45
|
-
return unless target_class
|
|
46
|
-
|
|
47
|
-
fields = target_class.attributes.keys
|
|
48
|
-
rows = instances.map do |inst|
|
|
49
|
-
fields.map { |f| extract_attribute_value(inst, f) }
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
File.write(path, generate_csv(fields, rows))
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
class << self
|
|
56
|
-
private
|
|
57
|
-
|
|
58
|
-
def build_column_mappings(attributes)
|
|
59
|
-
Array(attributes).each_with_object({}) do |attr, map|
|
|
60
|
-
next if attr.name == "map_to"
|
|
61
|
-
map[attr.name.to_sym] = attr.value.to_s
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def build_instance(row, mappings, target_class)
|
|
66
|
-
attrs = mappings.each_with_object({}) do |(attr_name, col_name), hash|
|
|
67
|
-
hash[attr_name] = row[col_name]
|
|
68
|
-
end
|
|
69
|
-
target_class.new(**attrs)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def extract_attribute_value(instance, field_name)
|
|
73
|
-
instance.public_send(field_name)
|
|
74
|
-
rescue NoMethodError
|
|
75
|
-
nil
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def generate_csv(headers, rows)
|
|
79
|
-
CSV.generate do |csv|
|
|
80
|
-
csv << headers
|
|
81
|
-
rows.each { |row| csv << row }
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
end
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
class Executor
|
|
6
|
-
# Pluggable format adapter registry. Adapters handle the actual
|
|
7
|
-
# I/O for a given format (CSV, XML, etc.).
|
|
8
|
-
#
|
|
9
|
-
# Each adapter must implement:
|
|
10
|
-
# .import(imp, compiled:) — read external data, return array of instances
|
|
11
|
-
# .export(exp, instances, compiled:) — write instances to external format
|
|
12
|
-
#
|
|
13
|
-
module FormatAdapter
|
|
14
|
-
class AdapterNotFoundError < StandardError; end
|
|
15
|
-
|
|
16
|
-
@adapters = {}
|
|
17
|
-
|
|
18
|
-
# Built-in adapters resolved by referencing the constant under
|
|
19
|
-
# Executor, which triggers autoload on first access. External
|
|
20
|
-
# adapters can still register via `register`.
|
|
21
|
-
BUILTIN_ADAPTERS = {
|
|
22
|
-
"csv" => :CsvAdapter,
|
|
23
|
-
"xml" => :XmlAdapter
|
|
24
|
-
}.freeze
|
|
25
|
-
|
|
26
|
-
# Register an adapter for a format name.
|
|
27
|
-
def self.register(format_name, adapter)
|
|
28
|
-
@adapters[format_name.to_s] = adapter
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Look up the adapter for a format name.
|
|
32
|
-
def self.resolve(format_name)
|
|
33
|
-
key = format_name.to_s
|
|
34
|
-
return @adapters[key] if @adapters.key?(key)
|
|
35
|
-
|
|
36
|
-
builtin = BUILTIN_ADAPTERS[key]
|
|
37
|
-
if builtin
|
|
38
|
-
adapter = Executor.const_get(builtin)
|
|
39
|
-
register(key, adapter)
|
|
40
|
-
return adapter
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
raise(AdapterNotFoundError,
|
|
44
|
-
"No adapter registered for format '#{format_name}'")
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Returns all registered format names.
|
|
48
|
-
def self.registered_formats
|
|
49
|
-
@adapters.keys
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
end
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "moxml"
|
|
4
|
-
|
|
5
|
-
module Lutaml
|
|
6
|
-
module Lml
|
|
7
|
-
class Executor
|
|
8
|
-
# XML format adapter. Reads XML files and maps elements to hydrated
|
|
9
|
-
# compiled-class instances, and writes instances back to XML.
|
|
10
|
-
#
|
|
11
|
-
# Registered for format "xml" via FormatAdapter::BUILTIN_ADAPTERS.
|
|
12
|
-
#
|
|
13
|
-
# Element-to-attribute mapping is provided by the lutaml-model XML
|
|
14
|
-
# mapping generated on each compiled class by ModelCompiler. This
|
|
15
|
-
# adapter only selects which XML elements correspond to records
|
|
16
|
-
# (via the `where` XPath from the import definition) and hands
|
|
17
|
-
# each one to `target_class.from_xml` for deserialization.
|
|
18
|
-
#
|
|
19
|
-
# Import shape (from LML `import { xml "..." { map_to X; where "/y" } }`):
|
|
20
|
-
# - imp.file = path to XML file
|
|
21
|
-
# - imp.attributes = TopElementAttribute list including:
|
|
22
|
-
# map_to: TargetClass (compiled-class key)
|
|
23
|
-
# where: XPath selector for each record element
|
|
24
|
-
#
|
|
25
|
-
# Export shape (from LML `export { format xml { file "..."; root "X" } }`):
|
|
26
|
-
# - exp.attributes = TopElementAttribute list including:
|
|
27
|
-
# file: output path
|
|
28
|
-
# root: root tag name (defaults to compiled class name)
|
|
29
|
-
# indent: "true" / "false" (default true)
|
|
30
|
-
# encoding: encoding string (default UTF-8)
|
|
31
|
-
class XmlAdapter
|
|
32
|
-
extend AdapterHelpers
|
|
33
|
-
|
|
34
|
-
DEFAULT_ENCODING = "UTF-8"
|
|
35
|
-
DEFAULT_SELECTOR = "/*/*"
|
|
36
|
-
DEFAULT_ROOT_SUFFIX = "s"
|
|
37
|
-
|
|
38
|
-
class << self
|
|
39
|
-
# Read an XML file and map elements to compiled-class instances.
|
|
40
|
-
# Returns an array of hydrated objects.
|
|
41
|
-
def import(imp, compiled:)
|
|
42
|
-
return [] unless imp.file
|
|
43
|
-
return [] unless imp.attributes&.any?
|
|
44
|
-
|
|
45
|
-
target_class = resolve_target_class(imp.attributes, compiled)
|
|
46
|
-
return [] unless target_class
|
|
47
|
-
|
|
48
|
-
path = imp.file
|
|
49
|
-
return [] unless File.exist?(path)
|
|
50
|
-
|
|
51
|
-
doc = Moxml.parse(File.read(path))
|
|
52
|
-
selector = attribute_value(imp.attributes, "where") || DEFAULT_SELECTOR
|
|
53
|
-
|
|
54
|
-
doc.xpath(selector).map do |element|
|
|
55
|
-
target_class.from_xml(element.to_xml)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Write instances to an XML file.
|
|
60
|
-
def export(exp, instances, compiled:)
|
|
61
|
-
return if instances.empty?
|
|
62
|
-
|
|
63
|
-
path = attribute_value(exp.attributes, "file")
|
|
64
|
-
return unless path && !path.empty?
|
|
65
|
-
|
|
66
|
-
class_name, target_class = find_class_for_instance(instances.first, compiled)
|
|
67
|
-
return unless target_class
|
|
68
|
-
|
|
69
|
-
options = export_options(exp, class_name)
|
|
70
|
-
File.write(path, build_export_xml(instances, options))
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
private
|
|
74
|
-
|
|
75
|
-
def export_options(exp, class_name)
|
|
76
|
-
{
|
|
77
|
-
root: attribute_value(exp.attributes, "root") || "#{class_name}#{DEFAULT_ROOT_SUFFIX}",
|
|
78
|
-
indent: attribute_value(exp.attributes, "indent") != "false",
|
|
79
|
-
encoding: attribute_value(exp.attributes, "encoding") || DEFAULT_ENCODING
|
|
80
|
-
}
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# Builds the export XML by parsing each instance's `to_xml`
|
|
84
|
-
# output, then grafting its root element under a single
|
|
85
|
-
# configurable root. lutaml-model handles per-record
|
|
86
|
-
# serialization via the generated XML mapping.
|
|
87
|
-
def build_export_xml(instances, options)
|
|
88
|
-
doc = Moxml.parse("<#{options[:root]}/>")
|
|
89
|
-
root = doc.root
|
|
90
|
-
|
|
91
|
-
instances.each do |inst|
|
|
92
|
-
record_doc = Moxml.parse(inst.to_xml)
|
|
93
|
-
root.add_child(record_doc.root)
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
doc.to_xml(indent: options[:indent] ? 2 : 0)
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
end
|
data/lib/lutaml/lml/executor.rb
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "forwardable"
|
|
4
|
-
|
|
5
|
-
module Lutaml
|
|
6
|
-
module Lml
|
|
7
|
-
# Orchestrates instance data I/O: import external data, validate
|
|
8
|
-
# collections, and export to external formats.
|
|
9
|
-
#
|
|
10
|
-
# Format-specific I/O is delegated to registered adapters via the
|
|
11
|
-
# FormatAdapter registry. The core executor only handles the
|
|
12
|
-
# orchestration — it knows *what* to do, adapters know *how*.
|
|
13
|
-
#
|
|
14
|
-
# Usage:
|
|
15
|
-
# compiled = ModelCompiler.new.compile(models_file)
|
|
16
|
-
# doc = Lutaml::Lml.parse_document(instances_file)
|
|
17
|
-
# result = Executor.run(doc, compiled: compiled)
|
|
18
|
-
# result.instances # array of hydrated instance objects
|
|
19
|
-
# result.errors # array of validation error strings
|
|
20
|
-
#
|
|
21
|
-
class Executor
|
|
22
|
-
autoload :FormatAdapter, "lutaml/lml/executor/format_adapter"
|
|
23
|
-
autoload :AdapterHelpers, "lutaml/lml/executor/adapter_helpers"
|
|
24
|
-
autoload :CsvAdapter, "lutaml/lml/executor/csv_adapter"
|
|
25
|
-
autoload :XmlAdapter, "lutaml/lml/executor/xml_adapter"
|
|
26
|
-
autoload :ConditionEvaluator, "lutaml/lml/executor/condition_evaluator"
|
|
27
|
-
|
|
28
|
-
# Result of running the executor: hydrated instances plus any
|
|
29
|
-
# validation errors collected along the way. Delegates array-like
|
|
30
|
-
# methods to instances so existing callers that treated the run
|
|
31
|
-
# return value as an array continue to work.
|
|
32
|
-
Result = Struct.new(:instances, :errors) do
|
|
33
|
-
extend Forwardable
|
|
34
|
-
|
|
35
|
-
def_delegators :instances, :length, :each, :map, :[], :empty?
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
attr_reader :compiled
|
|
39
|
-
|
|
40
|
-
def initialize(compiled:)
|
|
41
|
-
@compiled = compiled
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# Run the full import/validate/export cycle on a parsed document.
|
|
45
|
-
# Returns a Result with hydrated instances and validation errors.
|
|
46
|
-
def self.run(doc, compiled:)
|
|
47
|
-
new(compiled: compiled).run(doc)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def run(doc)
|
|
51
|
-
instances = run_imports(doc)
|
|
52
|
-
errors = validate_collections(doc, instances)
|
|
53
|
-
run_exports(doc, instances)
|
|
54
|
-
Result.new(instances, errors)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
# --- Import ---
|
|
60
|
-
|
|
61
|
-
def run_imports(doc)
|
|
62
|
-
return [] unless doc.instances&.imports&.any?
|
|
63
|
-
|
|
64
|
-
doc.instances.imports.flat_map { |imp| import_one(imp) }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def import_one(imp)
|
|
68
|
-
FormatAdapter.resolve(imp.format_type).import(imp, compiled: @compiled)
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# --- Collection validation ---
|
|
72
|
-
|
|
73
|
-
def validate_collections(doc, instances)
|
|
74
|
-
return [] unless doc.instances&.collections
|
|
75
|
-
|
|
76
|
-
Array(doc.instances.collections).flat_map do |collection|
|
|
77
|
-
ConditionEvaluator.evaluate(collection, instances)
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
# --- Export ---
|
|
82
|
-
|
|
83
|
-
def run_exports(doc, instances)
|
|
84
|
-
return unless doc.instances&.exports&.any?
|
|
85
|
-
|
|
86
|
-
doc.instances.exports.each do |exp|
|
|
87
|
-
export_one(exp, instances)
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def export_one(exp, instances)
|
|
92
|
-
FormatAdapter.resolve(exp.format_type).export(exp, instances, compiled: @compiled)
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
end
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lutaml
|
|
4
|
-
module Lml
|
|
5
|
-
module Format
|
|
6
|
-
module Adapter
|
|
7
|
-
class Mapping < Lutaml::KeyValue::Mapping
|
|
8
|
-
def initialize
|
|
9
|
-
super(:lml)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def dup_instance
|
|
13
|
-
self.class.new
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
# RS 3001 §String values: a present value — including a
|
|
17
|
-
# zero-length string — is distinct from an absent one, so the
|
|
18
|
-
# lml format emits empty values. Nil stays omitted: absence is
|
|
19
|
-
# the unassigned line, not a rendered null.
|
|
20
|
-
def map(name = nil, to: nil, render_empty: true, **options, &block)
|
|
21
|
-
super(name, to: to, render_empty: render_empty, **options, &block)
|
|
22
|
-
end
|
|
23
|
-
# The parent's `alias map_element map` binds the alias to the
|
|
24
|
-
# parent method at definition time, so it bypasses the map
|
|
25
|
-
# override above; default_mappings goes through map_element.
|
|
26
|
-
alias map_element map
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|