Class: Circuitdata::CompatibilityChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/circuitdata/compatibility_checker.rb

Instance Method Summary collapse

Constructor Details

#initialize(product_file, check_file, validate_origins) ⇒ CompatibilityChecker

Returns a new instance of CompatibilityChecker.



2
3
4
5
6
7
8
9
10
11
# File 'lib/circuitdata/compatibility_checker.rb', line 2

def initialize(product_file, check_file, validate_origins)
  require 'json'
  require 'json-schema'

  @product_file = product_file
  @check_file = check_file
  @validate_origins = validate_origins
  # Final hash
  @fh = {error: false, message: nil, errors: {validation: {}, restricted: {}, enforced: {}, capabilities: {}}}
end

Instance Method Details

#perform_comparison(product_data, check_data, schema, type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/circuitdata/compatibility_checker.rb', line 40

def perform_comparison(product_data, check_data, schema, type)

  docu = Circuitdata::Tools.new()
  ra = docu.create_structure

  case type
    when 'restricted'
      check_hash = check_data.dig(:open_trade_transfer_package, :profiles, :restricted, :printed_circuits_fabrication_data)
    when 'enforced'
      check_hash = check_data.dig(:open_trade_transfer_package, :profiles, :enforced, :printed_circuits_fabrication_data)
    when 'capabilities'
      check_hash = check_data.dig(:open_trade_transfer_package, :capabilities, :printed_circuits_fabrication_data)
    else
      check_hash = {}
  end

  common_hash = schema.dig(:properties, :open_trade_transfer_package, :properties, :products, :patternProperties, :'^(?!generic$).*', :properties, :printed_circuits_fabrication_data, :properties)
  check_hash.each do |k, v|
    v.each do |kl1, vl1| # level 1
      unless [:dielectric, :soldermask, :stiffener].include? k.to_sym
        common_hash[k.to_sym]||= {:type => 'object', :properties => {}}
        #common_hash[:stackup][:properties][:specified][:properties][k.to_sym] ||= {:type => 'object', :properties => {}}
      end
      enum = []
      required = []
      case type
        when 'restricted'
          case vl1[0].class.name
          when 'String'
            vl1.each {|enumvalue| enum << enumvalue.strip}
            new_hash = {:not => {:anyOf => [{ :enum => enum }]}}
          when 'Boolean', 'FalseClass', 'TrueClass'
            vl1.each do |enumvalue|
              required << kl1.to_s if enumvalue == false
              enum << enumvalue
            end
            new_hash = {:not => {:anyOf => [{ :enum => enum }]}}
          when 'Numeric', 'Float'
            if ra.dig(:structured, :elements, k.to_sym, :elements, kl1.to_sym, :enum).nil?
              new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[1] }]}
            else
              vl1.each {|enumvalue| enum << enumvalue}
              new_hash = {:not => {:anyOf => [{ :enum => enum }]}}
            end
          end
        when 'enforced'
          enum << vl1
          new_hash = eval("{:enum => #{enum}}")
        when 'capabilities'
          case vl1[0].class.name
          when 'String'
            vl1.each {|enumvalue| enum << enumvalue.strip}
            new_hash = {:anyOf => [{ :enum => enum }]}
          when 'Boolean', 'FalseClass', 'TrueClass'
            vl1.each do |enumvalue|
              required << kl1.to_s if enumvalue == true
              enum << enumvalue
            end
            new_hash = {:anyOf => [{ :enum => enum }]}
          when 'Numeric'
            vl1.each {|enumvalue| enum << enumvalue.strip}
            new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[1] }]}
          when 'Integer', 'Float'
            if vl1.length == 1
              new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[0] }]}
            elsif vl1.length == 2
              new_hash = {:anyOf => [{ :minimum => vl1[0], :maximum => vl1[1] }]}
            else
              fail StandardError, "Unhandled array length=#{vl1.length} key=#{kl1}"
            end
          else
            fail StandardError, "Unhandled type=#{vl1[1].class.name} key=#{kl1}"
          end
      end
      case k.to_sym
      when :dielectric
        schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:dielectrics][:items][:properties][kl1.to_sym] = new_hash
        schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:dielectrics][:items][:required] = required
      when :soldermask
        schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:soldermasks][:items][:properties][kl1.to_sym] = new_hash
        schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:soldermasks][:items][:required] = required
      when :stiffener
        schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:stiffeners][:items][:properties][kl1.to_sym] = new_hash
        schema[:properties][:open_trade_transfer_package][:properties][:custom][:properties][:materials][:properties][:printed_circuits_fabrication_data][:properties][:stiffeners][:items][:required] = required
      else
        common_hash[k.to_sym][:properties][kl1.to_sym] = new_hash
        common_hash[k.to_sym][:required] = required
      end
      #common_hash[:stackup][:properties][:specified][:properties][k.to_sym][:properties][kl1.to_sym] = new_hash
      #common_hash[:stackup][:properties][:specified][:properties][k.to_sym][:properties][kl1.to_sym] = new_hash
    end if v.is_a? Hash

    common_hash[k.to_sym][:additionalProperties] = false if (v.is_a?(Hash) && type == 'capabilities')
  end
  # perform validations
  begin
    validation_errors = JSON::Validator.fully_validate(schema.to_json, product_data, :errors_as_objects => true)

    if validation_errors.any?
      @fh[:error] = true
      @fh[:message] = 'The product to check did not meet the requirements'

      validation_errors.each do |error|
        error_array = []
        begin
          error_array << error[:message].match("^(The\\sproperty\\s\\'[\\s\\S]*\\'\\s)([\\s\\S]*)(\\sin\\sschema[\\s\\S]*)$").captures[1]
        rescue
          error_array << error[:message]
        end
        @fh[:errors][type.to_sym][error[:fragment]] = error_array
      end
    end
  rescue JSON::Schema::ReadFailed
    @fh[:error] = true
    @fh[:message] = "Could not read the submitted `#{type}` schema" # enforced_schema
  rescue JSON::Schema::SchemaError
    @fh[:error] = true
    @fh[:message] = "Something was wrong with the submitted `#{type}` schema" # enforced_schema
  end
end

#start_checkObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/circuitdata/compatibility_checker.rb', line 13

def start_check
  # Initialize & validate
  @fh[:error], @fh[:message], product_data = Circuitdata.read_json(@product_file)
  return @fh if @fh[:error]
  @fh[:error], @fh[:message], @fh[:errors][:validation] = Circuitdata.validate(product_data)
  return @fh if @fh[:error]
  if @check_file.present?
    @fh[:error], @fh[:message], check_data = Circuitdata.read_json(@check_file)
    return @fh if @fh[:error]
    @fh[:error], @fh[:message], @fh[:errors][:validation] = Circuitdata.validate(check_data)
    return @fh if @fh[:error]
    f2_types = Circuitdata.get_data_summary(check_data)[1]

    # read the schema
    schema_path = File.join(File.dirname(__FILE__), 'schema_files/v1/ottp_circuitdata_skeleton_schema.json')
    schema = JSON.parse(File.read(schema_path), symbolize_names: true)
    deref_schema = Circuitdata::Dereferencer.dereference(schema, File.dirname(Circuitdata::SCHEMA_FULL_PATH))
    restricted_schema = enforced_schema = capability_schema = deref_schema
    # Compare the content
    perform_comparison(product_data, check_data, restricted_schema, 'restricted') if f2_types.include? 'profile_restricted'
    perform_comparison(product_data, check_data, enforced_schema, 'enforced') if f2_types.include? 'profile_enforced'
    perform_comparison(product_data, check_data, capability_schema, 'capabilities') if f2_types.include? 'capabilities'
  end

  @fh
end