<commit_before># == Schema Information
#
# Table name: decision_tables
#
#  id      :integer          not null, primary key
#  rule_id :integer
#  content :text
#

class DecisionTable < ApplicationRecord
  include PaperTrailed
  delegate :project_id, to: :rule
  delegate :program_id, to: :rule

  belongs_to :rule, inverse_of: :formulas

  validate :in_headers_belong_to_facts

  validate :in_activity_code_exists

  def in_headers_belong_to_facts
    in_headers.each do |header|
      unless %w[level_1 level_2 level_3 level_4 level_5 level_6 activity_code].include?(header)
        errors[:content] << "Not in available org unit facts !"
      end
    end
  end

  def in_activity_code_exists
    return unless in_headers.include?("activity_code")
    available_codes = rule.package.activities.map(&:code).compact
    invalid_rules = decision_table.rules.reject { |rule| available_codes.include?(rule["activity_code"]) }
    invalid_rules.each { |invalid_rule| errors[:content] << "#{invalid_rule} not in available package codes #{available_codes}!" }
  end

  def in_headers
    decision_table.headers(:in)
  end

  def out_headers
    decision_table.headers(:out)
  end

  def content=(content)
    super
    @decision_table = nil
  end

  def extra_facts(entity_facts)
    decision_table.find(entity_facts)
  end

  def decision_table
    @decision_table ||= Decision::Table.new(content || "in:nothing,out:nothing_too")
  end
end<commit_msg>correcting invalid index while validating decision table<commit_after># == Schema Information
#
# Table name: decision_tables
#
#  id      :integer          not null, primary key
#  rule_id :integer
#  content :text
#

class DecisionTable < ApplicationRecord
  include PaperTrailed
  delegate :project_id, to: :rule
  delegate :program_id, to: :rule

  belongs_to :rule, inverse_of: :formulas

  validate :in_headers_belong_to_facts

  validate :in_activity_code_exists

  def in_headers_belong_to_facts
    in_headers.each do |header|
      unless %w[level_1 level_2 level_3 level_4 level_5 level_6 activity_code].include?(header)
        errors[:content] << "Not in available org unit facts !"
      end
    end
  end

  def in_activity_code_exists
    return unless in_headers.include?("activity_code")
    available_codes = rule.package.activities.map(&:code).compact
    invalid_rules = decision_table.rules.reject { |rule| available_codes.include?(rule["in:activity_code"]) }
    invalid_rules.each { |invalid_rule| errors[:content] << "#{invalid_rule} not in available package codes #{available_codes}!" }
  end

  def in_headers
    decision_table.headers(:in)
  end

  def out_headers
    decision_table.headers(:out)
  end

  def content=(content)
    super
    @decision_table = nil
  end

  def extra_facts(entity_facts)
    decision_table.find(entity_facts)
  end

  def decision_table
    @decision_table ||= Decision::Table.new(content || "in:nothing,out:nothing_too")
  end
end
{'fraction_non_alphanumeric': 0.09149659863945578, 'fraction_numerical': 0.004081632653061225, 'mean_word_length': 4.2238010657193605, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5258430', 'n_tokens_mistral': 951, 'n_tokens_neox': 910, 'n_words': 219}