latticerb 0.4.0-x86_64-linux

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.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +176 -0
  3. data/README.md +163 -0
  4. data/exe/lattice +7 -0
  5. data/ext/lattice_native/rustfmt.toml +1 -0
  6. data/lib/lattice/3.2/lattice_native.so +0 -0
  7. data/lib/lattice/3.3/lattice_native.so +0 -0
  8. data/lib/lattice/3.4/lattice_native.so +0 -0
  9. data/lib/lattice/4.0/lattice_native.so +0 -0
  10. data/lib/lattice/agent.rb +204 -0
  11. data/lib/lattice/cli.rb +469 -0
  12. data/lib/lattice/constraints.rb +265 -0
  13. data/lib/lattice/formatters.rb +193 -0
  14. data/lib/lattice/ipog.rb +103 -0
  15. data/lib/lattice/model.rb +83 -0
  16. data/lib/lattice/native.rb +10 -0
  17. data/lib/lattice/parser.rb +427 -0
  18. data/lib/lattice/resources/claude/lattice-workflow/SKILL.md +144 -0
  19. data/lib/lattice/resources/claude/lattice-workflow/references/modeling-rules.md +52 -0
  20. data/lib/lattice/resources/claude/lattice-workflow/references/worked-examples.md +59 -0
  21. data/lib/lattice/resources/codex/lattice-workflow/SKILL.md +144 -0
  22. data/lib/lattice/resources/codex/lattice-workflow/agents/openai.yaml +7 -0
  23. data/lib/lattice/resources/codex/lattice-workflow/references/modeling-rules.md +52 -0
  24. data/lib/lattice/resources/codex/lattice-workflow/references/worked-examples.md +59 -0
  25. data/lib/lattice/resources/generic/lattice-workflow/SKILL.md +152 -0
  26. data/lib/lattice/resources/generic/lattice-workflow/references/modeling-rules.md +52 -0
  27. data/lib/lattice/resources/generic/lattice-workflow/references/worked-examples.md +59 -0
  28. data/lib/lattice/schema_text.rb +153 -0
  29. data/lib/lattice/version.rb +5 -0
  30. data/lib/lattice.rb +88 -0
  31. data/lib/latticerb.rb +4 -0
  32. metadata +99 -0
@@ -0,0 +1,265 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ require_relative "model"
6
+ require_relative "native"
7
+
8
+ module Lattice
9
+ class ConstraintError < StandardError; end
10
+
11
+ # Compiles a model's constraints to the engine's primitives (invalid pairs,
12
+ # requirements, higher-order exclusions, forced interactions), checks the
13
+ # compiled closure for contradictions, and asks the Rust engine whether the
14
+ # model and its forced interactions are satisfiable. Python's
15
+ # lattice.constraints does the same, so both report the same errors.
16
+ # Assignments are `[parameter_index, value_index]` pairs.
17
+ class ConstraintEngine
18
+ EMPTY_SET = Set.new.freeze
19
+
20
+ attr_reader :model, :forced_interactions, :native
21
+
22
+ def initialize(model)
23
+ @model = model
24
+ @parameter_index = model.parameters.each_with_index.to_h { |parameter, index| [parameter.name, index] }
25
+ @value_index = {}
26
+ model.parameters.each_with_index do |parameter, parameter_idx|
27
+ parameter.values.each_with_index do |value, value_idx|
28
+ @value_index[[parameter_idx, value]] = value_idx
29
+ end
30
+ end
31
+ @invalid_pairs = {}
32
+ @requires = {}
33
+ @higher_order = []
34
+ @forced_interactions = []
35
+ compile_constraints
36
+ compute_implied_pairs
37
+ validate_compiled_constraints
38
+ @native = native_model(model.strength)
39
+ @satisfiable = check_feasibility
40
+ end
41
+
42
+ # Whether any row satisfies every constraint.
43
+ def satisfiable?
44
+ @satisfiable
45
+ end
46
+
47
+ # Whether some valid row contains every assignment in `partial`
48
+ # (a `{parameter_index => value_index}` hash).
49
+ def can_complete?(partial)
50
+ native.can_complete?(partial.sort)
51
+ end
52
+
53
+ def native_model(strength)
54
+ compiled = engine_constraints
55
+ Engine::Model.new(
56
+ model.parameters.map { |parameter| parameter.values.length },
57
+ model.parameters.map(&:normalized_weights),
58
+ strength,
59
+ compiled.fetch(:invalid_pairs),
60
+ compiled.fetch(:requires),
61
+ compiled.fetch(:higher_order),
62
+ compiled.fetch(:forced)
63
+ )
64
+ end
65
+
66
+ def conflicts?(left, right)
67
+ invalid_pairs_for(to_ref(left)).include?(to_ref(right))
68
+ end
69
+
70
+ # The compiled constraints as the generation engine takes them: invalid
71
+ # pairs and requirements as sorted [[p, v], [q, w]] lists, higher-order
72
+ # exclusions and forced interactions in compile order. Python's
73
+ # lattice.ipog builds the same lists, so the engine gets identical input.
74
+ def engine_constraints
75
+ {
76
+ invalid_pairs: ref_pairs(@invalid_pairs),
77
+ requires: ref_pairs(@requires),
78
+ higher_order: @higher_order,
79
+ forced: @forced_interactions
80
+ }
81
+ end
82
+
83
+ def implied_requirements(assignment)
84
+ requires_for(to_ref(assignment)).map { |ref| from_ref(ref) }.sort
85
+ end
86
+
87
+ private
88
+
89
+ def compile_constraints
90
+ model.constraints.each do |constraint|
91
+ case constraint
92
+ when InvalidPairConstraint
93
+ add_invalid_pair(to_ref(constraint.if_assignment), to_ref(constraint.then_not))
94
+ when BidirectionalConstraint
95
+ left_ref, right_ref = constraint.pair.map { |assignment| to_ref(assignment) }
96
+ add_requirement(left_ref, right_ref)
97
+ add_requirement(right_ref, left_ref)
98
+ exclude_other_values(left_ref, right_ref)
99
+ exclude_other_values(right_ref, left_ref)
100
+ when ForwardDependencyConstraint
101
+ source_ref = to_ref(constraint.source)
102
+ target_ref = to_ref(constraint.target)
103
+ add_requirement(source_ref, target_ref)
104
+ exclude_other_values(source_ref, target_ref)
105
+ when ConditionalConstraint
106
+ compile_conditional(constraint)
107
+ when ForcedConstraint
108
+ @forced_interactions << constraint.assignments.map { |assignment| to_ref(assignment) }
109
+ when HigherOrderConstraint
110
+ antecedents = constraint.if_all.map { |assignment| to_ref(assignment) }
111
+ @higher_order << [antecedents, to_ref(constraint.then_not)]
112
+ end
113
+ end
114
+ end
115
+
116
+ def compile_conditional(constraint)
117
+ parent_index = @parameter_index.fetch(constraint.parent)
118
+ child_index = @parameter_index.fetch(constraint.parameter)
119
+ parent = model.parameters[parent_index]
120
+ child = model.parameters[child_index]
121
+ applicable_values = Set.new(constraint.applies_when)
122
+ na_ref = [child_index, @value_index.fetch([child_index, NA_VALUE])]
123
+
124
+ parent.values.each_with_index do |parent_value, parent_value_index|
125
+ parent_ref = [parent_index, parent_value_index]
126
+ if applicable_values.include?(parent_value)
127
+ add_invalid_pair(parent_ref, na_ref)
128
+ next
129
+ end
130
+
131
+ child.values.each_with_index do |child_value, child_value_index|
132
+ next if child_value == NA_VALUE
133
+
134
+ add_invalid_pair(parent_ref, [child_index, child_value_index])
135
+ end
136
+ end
137
+ end
138
+
139
+ def compute_implied_pairs
140
+ changed = true
141
+ all_assignments = all_assignment_refs
142
+ while changed
143
+ changed = false
144
+
145
+ all_assignments.each do |assignment|
146
+ implied = requires_for(assignment).dup
147
+ implied.to_a.each do |required|
148
+ implied.merge(requires_for(required))
149
+ end
150
+ unless (implied - requires_for(assignment)).empty?
151
+ (@requires[assignment] ||= Set.new).merge(implied)
152
+ changed = true
153
+ end
154
+ end
155
+
156
+ all_assignments.each do |assignment|
157
+ additions = Set.new
158
+ invalid_pairs_for(assignment).to_a.each do |conflicting|
159
+ additions.merge(requires_for(conflicting))
160
+ end
161
+ requires_for(assignment).to_a.each do |required|
162
+ additions.merge(invalid_pairs_for(required))
163
+ end
164
+ additions.delete(assignment)
165
+ additions.each do |addition|
166
+ unless invalid_pairs_for(assignment).include?(addition)
167
+ add_invalid_pair(assignment, addition)
168
+ changed = true
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ def validate_compiled_constraints
176
+ all_assignment_refs.each do |assignment|
177
+ if invalid_pairs_for(assignment).include?(assignment)
178
+ readable = from_ref(assignment)
179
+ raise ConstraintError, "Constraint closure makes `#{readable.parameter}=#{readable.value}` impossible."
180
+ end
181
+
182
+ requires_for(assignment).each do |required|
183
+ if assignment[0] == required[0] && assignment[1] != required[1]
184
+ left = from_ref(assignment)
185
+ right = from_ref(required)
186
+ raise ConstraintError,
187
+ "Constraint closure requires incompatible values `#{left.parameter}=#{left.value}` and `#{right.parameter}=#{right.value}`."
188
+ end
189
+ if invalid_pairs_for(assignment).include?(required)
190
+ left = from_ref(assignment)
191
+ right = from_ref(required)
192
+ raise ConstraintError,
193
+ "Constraint closure makes `#{left.parameter}=#{left.value}` conflict with its own requirement `#{right.parameter}=#{right.value}`."
194
+ end
195
+ end
196
+ end
197
+
198
+ end
199
+
200
+ # Raises for the first impossible forced interaction; otherwise returns
201
+ # whether the model has any valid row.
202
+ def check_feasibility
203
+ native.feasibility
204
+ true
205
+ rescue Engine::ForcedInfeasible => e
206
+ assignments = @forced_interactions.fetch(Integer(e.message)).map do |parameter_index, value_index|
207
+ parameter = model.parameters[parameter_index]
208
+ "#{parameter.name}=#{parameter.values[value_index]}"
209
+ end.join(", ")
210
+ raise ConstraintError, "Forced interaction is not feasible: #{assignments}."
211
+ rescue Engine::NoValidAssignments
212
+ false
213
+ end
214
+
215
+ def exclude_other_values(source, required)
216
+ target_parameter = model.parameters[required[0]]
217
+ (0...target_parameter.values.length).each do |value_index|
218
+ next if value_index == required[1]
219
+
220
+ add_invalid_pair(source, [required[0], value_index])
221
+ end
222
+ end
223
+
224
+ def add_invalid_pair(left, right)
225
+ (@invalid_pairs[left] ||= Set.new).add(right)
226
+ (@invalid_pairs[right] ||= Set.new).add(left)
227
+ end
228
+
229
+ def add_requirement(source, target)
230
+ (@requires[source] ||= Set.new).add(target)
231
+ end
232
+
233
+ def invalid_pairs_for(ref)
234
+ @invalid_pairs[ref] || EMPTY_SET
235
+ end
236
+
237
+ def ref_pairs(table)
238
+ table.flat_map { |source, targets| targets.map { |target| [source, target] } }.sort
239
+ end
240
+
241
+ def requires_for(ref)
242
+ @requires[ref] || EMPTY_SET
243
+ end
244
+
245
+ def all_assignment_refs
246
+ refs = []
247
+ model.parameters.each_with_index do |parameter, parameter_index|
248
+ (0...parameter.values.length).each do |value_index|
249
+ refs << [parameter_index, value_index]
250
+ end
251
+ end
252
+ refs
253
+ end
254
+
255
+ def to_ref(assignment)
256
+ parameter_index = @parameter_index.fetch(assignment.parameter)
257
+ [parameter_index, @value_index.fetch([parameter_index, assignment.value])]
258
+ end
259
+
260
+ def from_ref(ref)
261
+ parameter = model.parameters[ref[0]]
262
+ Assignment.new(parameter: parameter.name, value: parameter.values[ref[1]])
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csv"
4
+ require "json"
5
+
6
+ require_relative "ipog"
7
+ require_relative "model"
8
+
9
+ module Lattice
10
+ # Output rendering. Table, JSON, CSV, summary, and YAML formats are
11
+ # byte-identical to the Python implementation's output for the same model
12
+ # and result.
13
+ module Formatters
14
+ extend self
15
+
16
+ def render_output(model, result, output_format:, seed:)
17
+ case output_format
18
+ when "table" then format_table(model, result)
19
+ when "json" then format_json(model, result, seed:)
20
+ when "csv" then format_csv(model, result)
21
+ when "summary" then format_summary(model, result)
22
+ when "yaml" then format_yaml(model, result, seed:)
23
+ else
24
+ raise ArgumentError, "Unknown format `#{output_format}`."
25
+ end
26
+ end
27
+
28
+ def format_table(model, result)
29
+ rows = named_rows(model, result)
30
+ headers = ["#"] + model.parameter_names
31
+ body = rows.each_with_index.map do |row, index|
32
+ [(index + 1).to_s] + model.parameter_names.map { |name| row[name] }
33
+ end
34
+ widths = headers.each_index.map do |column|
35
+ ([headers[column].length] + body.map { |record| record[column].length }).max
36
+ end
37
+
38
+ lines = [headline(model, result), ""]
39
+ lines.concat(milestone_lines(result))
40
+ lines << ""
41
+ lines << headers.each_index.map do |column|
42
+ column.zero? ? headers[column].rjust(widths[column]) : headers[column].ljust(widths[column])
43
+ end.join(" | ")
44
+ lines << widths.map { |width| "-" * width }.join("-|-")
45
+
46
+ body.each do |record|
47
+ lines << record.each_index.map do |column|
48
+ column.zero? ? record[column].rjust(widths[column]) : record[column].ljust(widths[column])
49
+ end.join(" | ")
50
+ end
51
+ lines.join("\n")
52
+ end
53
+
54
+ def format_summary(model, result)
55
+ lines = [headline(model, result), ""]
56
+ lines.concat(milestone_lines(result))
57
+ lines.join("\n")
58
+ end
59
+
60
+ def format_json(model, result, seed:)
61
+ last_point = result.coverage_curve.last
62
+ payload = {
63
+ "meta" => {
64
+ "model_name" => model.model_name,
65
+ "strength" => model.strength,
66
+ "test_count" => result.rows.length,
67
+ "exhaustive_count" => model.exhaustive_count,
68
+ "reduction" => reduction_label(model.exhaustive_count, result.rows.length),
69
+ "coverage" => last_point ? format("%.1f%%", last_point.cumulative_pct) : "0.0%",
70
+ "seed" => seed
71
+ },
72
+ "coverage_curve" => result.coverage_curve.map do |point|
73
+ { "scenario" => point.scenario, "cumulative_pct" => point.cumulative_pct }
74
+ end,
75
+ "scenarios" => named_rows(model, result).each_with_index.map do |row, index|
76
+ {
77
+ "id" => index + 1,
78
+ "values" => model.parameter_names.to_h { |name| [name, row[name]] }
79
+ }
80
+ end
81
+ }
82
+ # Python's json.dumps also escapes DEL, which Ruby's ascii_only leaves raw.
83
+ JSON.pretty_generate(payload, ascii_only: true).gsub("\u007f", "\\u007f")
84
+ end
85
+
86
+ # Same content as format_json, written by hand in one fixed style so Ruby
87
+ # and Python emit identical bytes (Psych and PyYAML each quote and wrap
88
+ # differently). Every string is double-quoted, so no value can be read
89
+ # back as a boolean, null, number or date.
90
+ def format_yaml(model, result, seed:)
91
+ last_point = result.coverage_curve.last
92
+ lines = [
93
+ "meta:",
94
+ " model_name: #{yaml_string(model.model_name)}",
95
+ " strength: #{model.strength}",
96
+ " test_count: #{result.rows.length}",
97
+ " exhaustive_count: #{model.exhaustive_count}",
98
+ " reduction: #{yaml_string(reduction_label(model.exhaustive_count, result.rows.length))}",
99
+ " coverage: #{yaml_string(last_point ? format('%.1f%%', last_point.cumulative_pct) : '0.0%')}",
100
+ " seed: #{seed.nil? ? 'null' : seed}"
101
+ ]
102
+
103
+ if result.coverage_curve.empty?
104
+ lines << "coverage_curve: []"
105
+ else
106
+ lines << "coverage_curve:"
107
+ result.coverage_curve.each do |point|
108
+ lines << " - scenario: #{point.scenario}"
109
+ lines << " cumulative_pct: #{JSON.generate(point.cumulative_pct)}"
110
+ end
111
+ end
112
+
113
+ rows = named_rows(model, result)
114
+ if rows.empty?
115
+ lines << "scenarios: []"
116
+ else
117
+ lines << "scenarios:"
118
+ rows.each_with_index do |row, index|
119
+ lines << " - id: #{index + 1}"
120
+ lines << " values:"
121
+ model.parameter_names.each do |name|
122
+ lines << " #{yaml_string(name)}: #{yaml_string(row[name])}"
123
+ end
124
+ end
125
+ end
126
+ lines.join("\n")
127
+ end
128
+
129
+ YAML_ESCAPES = {
130
+ "\"" => "\\\"", "\\" => "\\\\", "\b" => "\\b", "\t" => "\\t", "\n" => "\\n", "\f" => "\\f", "\r" => "\\r"
131
+ }.freeze
132
+
133
+ # Characters escaped inside a double-quoted YAML string: the quote and
134
+ # backslash, C0 controls, DEL, C1 controls (U+0085 would otherwise fold to
135
+ # a space), the Unicode line and paragraph separators, the byte-order mark,
136
+ # and the two noncharacters YAML cannot hold. Everything else is written as
137
+ # UTF-8, so accented letters and emoji stay readable.
138
+ YAML_ESCAPED_CHARS = /["\\\u0000-\u001f\u007f-\u009f\u2028\u2029\ufeff\ufffe\uffff]/
139
+
140
+ def yaml_string(value)
141
+ escaped = value.gsub(YAML_ESCAPED_CHARS) do |char|
142
+ YAML_ESCAPES.fetch(char) { format("\\u%04x", char.ord) }
143
+ end
144
+ "\"#{escaped}\""
145
+ end
146
+
147
+ # Matches Python's csv.writer: empty fields are written bare, except a row
148
+ # made of a single empty field, which is written as "" so it isn't a blank
149
+ # line.
150
+ def format_csv(model, result)
151
+ records = [model.parameter_names] + named_rows(model, result).map do |row|
152
+ model.parameter_names.map { |name| row[name] }
153
+ end
154
+ records.map do |record|
155
+ record == [""] ? "\"\"\r\n" : CSV.generate_line(record, row_sep: "\r\n", quote_empty: false)
156
+ end.join.strip
157
+ end
158
+
159
+ def headline(model, result)
160
+ "Lattice — #{result.rows.length} scenarios (from #{thousands(model.exhaustive_count)} possible)"
161
+ end
162
+
163
+ def milestone_lines(result)
164
+ [50.0, 75.0, 90.0, 100.0].filter_map do |threshold|
165
+ point = result.coverage_curve.find { |candidate| candidate.cumulative_pct >= threshold }
166
+ " #{threshold.to_i}% coverage at scenario ##{point.scenario}" if point
167
+ end
168
+ end
169
+
170
+ def named_rows(model, result)
171
+ result.rows.map do |row|
172
+ model.parameters.each_with_index.to_h do |parameter, index|
173
+ [parameter.name, parameter.values[row[index]]]
174
+ end
175
+ end
176
+ end
177
+
178
+ def reduction_label(exhaustive_count, scenario_count)
179
+ return "inf" if scenario_count.zero?
180
+
181
+ reduction = exhaustive_count.to_f / scenario_count
182
+ if (reduction - reduction.round).abs < 1e-9
183
+ "#{reduction.round}x"
184
+ else
185
+ format("%.1fx", reduction)
186
+ end
187
+ end
188
+
189
+ def thousands(count)
190
+ count.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "constraints"
4
+
5
+ module Lattice
6
+ CoveragePoint = Data.define(:scenario, :cumulative_pct)
7
+ GenerationResult = Data.define(:rows, :coverage_curve, :total_valid_tuples)
8
+ GenerationProgress = Data.define(:event, :scenario, :covered, :total, :cumulative_pct, :uncovered)
9
+
10
+ # Scenario generation, backed by the Rust engine (Lattice::Engine, built from
11
+ # ext/lattice_native on the lattice-engine crate). The engine builds a covering
12
+ # array greedily, choosing each row from several candidates, then shrinks it
13
+ # with tabu search, and returns the rows ordered so each adds the most new
14
+ # coverage. ConstraintEngine compiles the model into the engine's input; this
15
+ # module applies max_rows and stop_after_coverage by cutting the ordered rows
16
+ # short. Python's lattice.ipog does the same, so both give identical rows.
17
+ module IPOG
18
+ extend self
19
+
20
+ DEFAULT_CANDIDATES = 16
21
+ SHRINK_STEPS = 1_000_000
22
+ PATIENCE = 10_000
23
+ SEED_MODULUS = 2**64
24
+
25
+ # `pool_size` is the number of candidate rows built per greedy step.
26
+ def generate_covering_array(
27
+ model,
28
+ strength: nil,
29
+ seed: 42,
30
+ pool_size: DEFAULT_CANDIDATES,
31
+ progress: nil,
32
+ max_rows: nil,
33
+ stop_after_coverage: nil
34
+ )
35
+ actual_strength = strength || model.strength
36
+ raise ArgumentError, "`pool_size` must be at least 1." if pool_size < 1
37
+ raise ArgumentError, "`max_rows` must be at least 1." if !max_rows.nil? && max_rows < 1
38
+
39
+ coverage_target = coverage_target_for(stop_after_coverage)
40
+
41
+ engine = ConstraintEngine.new(model)
42
+ raise ArgumentError, "Model has no valid full assignments." unless engine.satisfiable?
43
+
44
+ native = actual_strength == model.strength ? engine.native : engine.native_model(actual_strength)
45
+ rows, new_coverage, total = native.generate(seed % SEED_MODULUS, pool_size, SHRINK_STEPS, PATIENCE)
46
+
47
+ report(progress, "enumerated", 0, 0, total)
48
+ kept = []
49
+ curve = []
50
+ covered = 0
51
+ rows.zip(new_coverage) do |row, gained|
52
+ kept << row.freeze
53
+ covered += gained
54
+ cumulative_pct = coverage_pct(covered, total)
55
+ curve << CoveragePoint.new(scenario: kept.length, cumulative_pct:)
56
+ report(progress, "scenario", kept.length, covered, total)
57
+ next unless should_stop?(kept.length, cumulative_pct, max_rows, coverage_target)
58
+
59
+ report(progress, "stopped", kept.length, covered, total) if kept.length < rows.length
60
+ break
61
+ end
62
+
63
+ GenerationResult.new(rows: kept.freeze, coverage_curve: curve.freeze, total_valid_tuples: total)
64
+ end
65
+
66
+ private
67
+
68
+ def report(progress, event, scenario, covered, total)
69
+ progress&.call(
70
+ GenerationProgress.new(
71
+ event:,
72
+ scenario:,
73
+ covered:,
74
+ total:,
75
+ cumulative_pct: coverage_pct(covered, total),
76
+ uncovered: total - covered
77
+ )
78
+ )
79
+ end
80
+
81
+ def coverage_target_for(stop_after_coverage)
82
+ return nil if stop_after_coverage.nil?
83
+
84
+ unless stop_after_coverage.positive? && stop_after_coverage <= 100
85
+ raise ArgumentError, "`stop_after_coverage` must be in the range (0, 100]."
86
+ end
87
+
88
+ stop_after_coverage
89
+ end
90
+
91
+ def should_stop?(row_count, cumulative_pct, max_rows, coverage_target)
92
+ return true if !max_rows.nil? && row_count >= max_rows
93
+ return true if !coverage_target.nil? && cumulative_pct >= coverage_target
94
+
95
+ false
96
+ end
97
+
98
+ # A satisfiable model always has valid tuples, so total_count is positive.
99
+ def coverage_pct(covered_count, total_count)
100
+ ((covered_count.to_f / total_count) * 100).round(1, half: :even)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Lattice
6
+ NA_VALUE = "N/A"
7
+
8
+ def self.normalize_scalar(value)
9
+ value.nil? ? "null" : value.to_s
10
+ end
11
+
12
+ Assignment = Data.define(:parameter, :value) do
13
+ include Comparable
14
+
15
+ def <=>(other)
16
+ [parameter, value] <=> [other.parameter, other.value]
17
+ end
18
+ end
19
+
20
+ class Parameter
21
+ attr_reader :name, :values, :weights
22
+
23
+ def initialize(name:, values:, weights: nil)
24
+ @name = name
25
+ @values = values.to_a.freeze
26
+ @weights = weights&.to_a&.freeze
27
+ end
28
+
29
+ def value_set
30
+ @value_set ||= Set.new(values).freeze
31
+ end
32
+
33
+ # Weight normalization mirrors the Python implementation, including its
34
+ # naive left-to-right float summation, so scores tie-break identically.
35
+ def normalized_weights
36
+ @normalized_weights ||=
37
+ if weights.nil?
38
+ Array.new(values.length, 1.0 / values.length).freeze
39
+ else
40
+ total = weights.inject(0.0) { |acc, weight| acc + weight }
41
+ if total <= 0
42
+ Array.new(values.length, 1.0 / values.length).freeze
43
+ else
44
+ weights.map { |weight| weight / total }.freeze
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ InvalidPairConstraint = Data.define(:if_assignment, :then_not)
51
+ BidirectionalConstraint = Data.define(:pair)
52
+ ForwardDependencyConstraint = Data.define(:source, :target)
53
+ ConditionalConstraint = Data.define(:parameter, :values, :parent, :applies_when)
54
+ ForcedConstraint = Data.define(:assignments)
55
+ HigherOrderConstraint = Data.define(:if_all, :then_not)
56
+
57
+ class Model
58
+ attr_reader :model_name, :parameters, :constraints, :strength
59
+
60
+ def initialize(model_name:, parameters:, constraints:, strength: 2)
61
+ @model_name = model_name
62
+ @parameters = parameters.to_a.freeze
63
+ @constraints = constraints.to_a.freeze
64
+ @strength = strength
65
+ end
66
+
67
+ def parameter_names
68
+ @parameter_names ||= parameters.map(&:name).freeze
69
+ end
70
+
71
+ def parameter_lookup
72
+ @parameter_lookup ||= parameters.to_h { |parameter| [parameter.name, parameter] }.freeze
73
+ end
74
+
75
+ def exhaustive_count
76
+ @exhaustive_count ||= parameters.inject(1) { |total, parameter| total * parameter.values.length }
77
+ end
78
+
79
+ def with(strength:)
80
+ Model.new(model_name:, parameters:, constraints:, strength:)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Loads the Rust engine (Lattice::Engine). Precompiled gems carry one build per
4
+ # Ruby version (lib/lattice/3.4/...); a gem compiled on install has a single one
5
+ # next to this file.
6
+ begin
7
+ require_relative "#{RUBY_VERSION[/\A\d+\.\d+/]}/lattice_native"
8
+ rescue LoadError
9
+ require_relative "lattice_native"
10
+ end