Module: IPXACT

Defined in:
lib/ipxact.rb

Overview

Root and commodity module for the IPXACT Ruby library. In particular, this module should be used for loading the component design files.

Examples:

Default initialization process

component_docs = IPXACT::load_components("my/component/path")
design_docs = IPXACT::load_designs("my/design/path")
platform = IPXACT::Parser::PlatformData::parse_platform("platform_id",
                                          component_docs, design_docs)

Author:

  • Guillaume Godet-Bar

Defined Under Namespace

Modules: Parser Classes: Component, GraphPathFinder, Platform

Constant Summary collapse

DESIGN_SCHEMA_PATH =

Path to the design.xsd file

File.join File.dirname(__FILE__), '../schemas/design.xsd'
COMPONENT_SCHEMA_PATH =

Path to the component.xsd file

File.join File.dirname(__FILE__), '../schemas/component.xsd'

Class Method Summary collapse

Class Method Details

.identifier(path) ⇒ Hash

Returns the component’s identifier (i.e., name, library, vendor, version).

Parameters:

  • path (String)

    The path to the component’s XML file

Returns:

  • (Hash)

    The component’s identifier or nil if the path is invalid



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ipxact.rb', line 86

def self.identifier(path)
  if is_valid_design?(path) || is_valid_component?(path)
    ipxact_file = File.open(path)
    ipxact_doc = Nokogiri::XML(ipxact_file)
    ipxact_file.close

    {
      :name     => ipxact_doc.xpath("./*/spirit:name").first.text,
      :vendor   => ipxact_doc.xpath("./*/spirit:vendor").first.text,
      :library  => ipxact_doc.xpath("./*/spirit:library").first.text,
      :version  => ipxact_doc.xpath("./*/spirit:version").first.text
    }
  else
    nil
  end
end

.is_most_recent_version?(version_a, version_b) ⇒ Boolean

Decides whether version_a is strictly more recent than version_b. Accepts any relaxed version of semantic versioning markup (i.e., X(.Y(.Z)?)?).

Parameters:

  • version_a (String)

    a version number

  • version_b (String)

    a version number

Returns:

  • (Boolean)

    a Boolean that states if version_a > version_b



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ipxact.rb', line 201

def self.is_most_recent_version?(version_a, version_b)
  split_a = version_a.split('.')
  split_b = version_b.split('.')

  split_a.each_with_index do |el,i|
    return true if split_b.size <= i && el.to_i != 0
    return true if el.to_i > split_b[i].to_i
  end

  false
end

.is_same_version?(version_a, version_b) ⇒ Boolean

Decides whether version_a and version_b are the same version. Accepts any relaxed version of the semantic versioning markup (i.e., X(.Y(.Z)?)?). When comparing versions with different lengths, the method will ‘pad’ the shortest version string with 0s.

Examples:

IPXACT::is_same_version('1.0', '1.1')
# => false

IPXACT::is_same_version('1.0', '1.0')
# => true

IPXACT::is_same_version('1.0.0', '1.0')
# => true

IPXACT::is_same_version('1.0.1', '1.0')
# => false

Returns:

  • (Boolean)

    a Boolean that states if version_a == version_b (relaxed equivalence)



234
235
236
# File 'lib/ipxact.rb', line 234

def self.is_same_version?(version_a, version_b)
  return !(self.is_most_recent_version?(version_a, version_b) || self.is_most_recent_version?(version_b, version_a))
end

.is_valid_component?(path) ⇒ Boolean

Returns whether the XML file is an IPXACT component (i.e., it is consistent with the component.xsd schema.

Parameters:

  • file_path (String)

    path to the file that should be tested

Returns:

  • (Boolean)

    whether the file is a valid IPXACT component

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ipxact.rb', line 45

def self.is_valid_component?(path)
  raise ArgumentError unless path.is_a? String

  component_schema_file = File.open(COMPONENT_SCHEMA_PATH)
  component_schema = Nokogiri::XML::Schema(component_schema_file)
  component_schema_file.close

  component_file = File.open(path)
  component_doc = Nokogiri::XML(component_file)
  component_file.close

  component_schema.validate(component_doc).size == 0
end

.is_valid_design?(path) ⇒ Boolean

Returns whether the XML file is an IPXACT design (i.e., it is consistent with the design.xsd schema).

Parameters:

  • file_path (String)

    path to the file that should be tested

Returns:

  • (Boolean)

    whether the file is a valid IPXACT design

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ipxact.rb', line 66

def self.is_valid_design?(path)
  raise ArgumentError unless path.is_a? String

  design_schema_file = File.open(DESIGN_SCHEMA_PATH)
  design_schema = Nokogiri::XML::Schema design_schema_file
  design_schema_file.close

  design_file = File.open(path)
  design_doc = Nokogiri::XML(design_file)
  design_file.close

  design_schema.validate(design_doc).size == 0
end

.load_components(components_path) ⇒ Hash

Loads the components from an IPXACT XML specification.

Parameters:

  • components_path (String)

    path to the components of the target platform. All subdirectories will be scanned and all valid files (i.e., from an XML and component.xsd schema point of view) are loaded into the resulting structure.

Returns:

  • (Hash)

    a Hash that associates the name and version of the component with the IPXACT XML (Nokogiri) structure of the component. No post-processing is made on this structure, which should not be used for any serious work (i.e.,its values may be modified when instantiating any design file linked to the components).

Raises:

  • a generic exception if no component could be found.



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
# File 'lib/ipxact.rb', line 120

def self.load_components(components_path)
  component_schema_file = File.open(COMPONENT_SCHEMA_PATH)
  component_schema = Nokogiri::XML::Schema(component_schema_file)
  component_schema_file.close

  xml_files = Dir.glob(File.join(components_path, "**", "*.xml"))
  
  component_docs = xml_files.inject({}) do |acc, file|
    component_file = File.open(file)
    component_doc = Nokogiri::XML(component_file)
    component_file.close

    version_tag = component_doc.xpath("/spirit:component/spirit:version")
    version = version_tag.first.nil? ? "0" : version_tag.first.text

    name_tag = component_doc.xpath("/spirit:component/spirit:name")
    name = name_tag.first.nil? ? nil : name_tag.first.text
   
    unless name.nil?
      key = [component_doc.xpath("/spirit:component/spirit:name").first.text, version]
      acc[key] = component_doc  \
        if component_schema.validate(component_doc).size == 0
    end
    acc
  end

  raise 'No valid component was found!' if component_docs.keys.empty?
  
  component_docs
end

.load_designs(designs_path) ⇒ Hash

Loads the designs from an IPXACT XML specification.

with the IPXACT XML (Nokogiri) structure of the design. Should not be used, as this is ‘raw’ IPXACT data.

Parameters:

  • designs_path (String)

    a path to the design instances of the target platform. All subdirectories will be scanned and all valid files (i.e., from an XML and design.xsd schema point of view) are loaded into the resulting structure.

Returns:

  • (Hash)

    a Hash that associates the name and version of the design

Raises:

  • a generic exception if no design document could be found.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ipxact.rb', line 164

def self.load_designs(designs_path)
  design_schema_file = File.open(DESIGN_SCHEMA_PATH)
  design_schema = Nokogiri::XML::Schema design_schema_file
  design_schema_file.close

  xml_files = Dir.glob(File.join(designs_path, "**", "*.xml"))

  design_docs = xml_files.inject({}) do |acc, xml_file|
    design_file = File.open(xml_file)
    design_doc = Nokogiri::XML(design_file)
    design_file.close

    if design_schema.validate(design_doc).size == 0

      design_name = design_doc.xpath("./spirit:design/spirit:name").text
      design_version = design_doc.xpath("./spirit:design/spirit:version").text

      acc[[design_name, design_version]] = design_doc
    end

    acc
  end

  raise 'No valid design could be found!' if design_docs.keys.empty?

  design_docs

end

.parse_reference(reference) ⇒ Hash

Parses an IPXACT reference element (i.e., any spirit:hierarchyRef or spirit:componentRef).

Parameters:

  • reference (Nokogiri::Node)

    The IPXACT reference, as an XML (Nokogiri) fragment

Returns:

  • (Hash)

    a Hash with the following keys: :library the referenced element’s library; :name the referenced element’s name; :vendor the referenced element’s vendor; :version the referenced element’s version.



250
251
252
253
254
255
256
257
# File 'lib/ipxact.rb', line 250

def self.parse_reference(reference)
  {
    :library => reference['library'],
    :name => reference['name'],
    :vendor => reference['vendor'],
    :version => reference['version']
  }
end