Class: Ibex::Codegen::Explain

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/codegen/explain.rb

Overview

Renders a selected conflict explanation from Automaton IR and counterexamples. rubocop:disable Metrics/ClassLength -- inline contracts and two stable output formats stay near selection policy.

Constant Summary collapse

SCHEMA_VERSION =

: Integer

1

Instance Method Summary collapse

Constructor Details

#initialize(automaton, state: nil, token: nil, max_tokens: LALR::Counterexample::DEFAULT_MAX_TOKENS, max_configurations: LALR::Counterexample::DEFAULT_MAX_CONFIGURATIONS) ⇒ Explain

Returns a new instance of Explain.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ibex/codegen/explain.rb', line 28

def initialize(automaton, state: nil, token: nil, max_tokens: LALR::Counterexample::DEFAULT_MAX_TOKENS,
               max_configurations: LALR::Counterexample::DEFAULT_MAX_CONFIGURATIONS)
  @automaton = automaton
  @grammar = automaton.grammar
  @state_selector = state
  @token_query = token
  @max_tokens = max_tokens
  @max_configurations = max_configurations
  @labels = SymbolLabels.build(@grammar)
  @token_selector = resolve_token(token)
  validate_state!
  @entries = select_entries
end

Instance Method Details

#render_textObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ibex/codegen/explain.rb', line 64

def render_text
  lines = [
    "Ibex conflict explanation v#{SCHEMA_VERSION}",
    "Algorithm: #{@automaton.algorithm}",
    "State selector: #{@state_selector || 'all'}",
    "Token selector: #{@token_selector ? token_label(@token_selector, query: @token_query) : 'all'}",
    "Search budget: #{@max_tokens} tokens, #{@max_configurations} configurations",
    "Matched conflicts: #{@entries.length}",
    ""
  ]
  if @entries.empty?
    lines << "No conflicts matched the selectors."
  else
    @entries.each_with_index { |entry, index| append_conflict(lines, entry, index + 1) }
  end
  "#{lines.join("\n")}\n"
end

#to_hObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ibex/codegen/explain.rb', line 43

def to_h
  unifying = @entries.count { |entry| entry.fetch(:example).fetch(:unifying) }
  {
    ibex_explain: "conflicts",
    schema_version: SCHEMA_VERSION,
    algorithm: @automaton.algorithm,
    selectors: {
      state: @state_selector,
      token: @token_selector && token_reference(@token_selector, query: @token_query)
    },
    search: { max_tokens: @max_tokens, max_configurations: @max_configurations },
    summary: {
      matched_conflicts: @entries.length,
      unifying_counterexamples: unifying,
      nonunifying_witnesses: @entries.length - unifying
    },
    conflicts: @entries.map { |entry| conflict_document(entry) }
  }
end