Skip to content

Ruby scripts

Philippe DUL edited this page Jan 4, 2023 · 1 revision

Ruby

require 'nokogiri'
require 'json'

in_doc = File.open(ARGV[0]) do |f|
  Nokogiri::XML(f)
end

in_doc.remove_namespaces!
for function in in_doc.xpath("//*[@type]") do
	function["type"] = function["type"].split(":")[1]
end

Map of all elements by id

resultById = Hash[in_doc.xpath("//*[@id]").map {|v| [v.xpath('@id').text,v]}]

List of all Function and allocating components

hash = {}

for allocation in in_doc.xpath("//*[@type='ComponentFunctionalAllocation']") do
  result = {
    cpt: resultById[allocation.xpath('@sourceElement').text[1..-1]],
    fct: resultById[allocation.xpath('@targetElement').text[1..-1]],
  }
  hash[result[:fct].xpath('@id').text] = resultById[allocation.xpath('@sourceElement').text[1..-1]]
end


in_doc.xpath("//*[@type='PhysicalComponent']")

for function in in_doc.xpath("//*[@type='PhysicalFunction']") do
  result = {
    name:  function.xpath('@name'),
    type:  function.xpath('@type'),
    allocatingComponent: hash[function.xpath('@id').text],
  }
  print result.to_json, ",\n"
end

Clone this wiki locally