require "nokogiri" require "lutaml/uml/has_attributes" require "lutaml/uml/document" module Lutaml module XMI module Parsers # Class for parsing .xmi schema files into ::Lutaml::Uml::Document class XML LOVER_VALUE_MAPPINGS = { "0" => "C", "1" => "M", }.freeze attr_reader :main_model, :xmi_cache # @param [String] io - file object with path to .xmi file # [Hash] options - options for parsing # # @return [Lutaml::XMI::Model::Document] def self.parse(io, _options = {}) new.parse(Nokogiri::XML(io.read)) end def parse(xmi_doc) @xmi_cache = {} @main_model = xmi_doc ::Lutaml::Uml::Document .new(serialize_to_hash(xmi_doc)) end private def serialize_to_hash(xmi_doc) model = xmi_doc.xpath('//uml:Model[@xmi:type="uml:Model"]').first { name: model["name"], packages: serialize_model_packages(model), } end def serialize_model_packages(model) model.xpath('./packagedElement[@xmi:type="uml:Package"]').map do |package| { name: package["name"], packages: serialize_model_packages(package), classes: serialize_model_classes(package), enums: serialize_model_enums(package), } end end def serialize_model_classes(model) model.xpath('./packagedElement[@xmi:type="uml:Class"]').map do |klass| { xmi_id: klass["xmi:id"], xmi_uuid: klass["xmi:uuid"], name: klass["name"], attributes: serialize_class_attributes(klass), associations: serialize_model_associations(klass), constraints: serialize_class_constraints(klass), is_abstract: doc_node_attribute_value(klass, "isAbstract"), definition: doc_node_attribute_value(klass, "documentation"), stereotype: doc_node_attribute_value(klass, "stereotype"), } end end def serialize_model_enums(model) model.xpath('./packagedElement[@xmi:type="uml:Enumeration"]').map do |enum| attributes = enum .xpath('.//ownedLiteral[@xmi:type="uml:EnumerationLiteral"]') .map do |attribute| { # TODO: xmi_id # xmi_id: enum['xmi:id'], type: attribute["name"], } end { xmi_id: enum["xmi:id"], xmi_uuid: enum["xmi:uuid"], name: enum["name"], attributes: attributes, definition: doc_node_attribute_value(enum, "documentation"), stereotype: doc_node_attribute_value(enum, "stereotype"), } end end def serialize_model_associations(klass) xmi_id = klass["xmi:id"] main_model.xpath(%(//element[@xmi:idref="#{xmi_id}"]/links/*)).map do |link| member_end, member_end_type, member_end_cardinality, member_end_attribute_name = serialize_member_type(xmi_id, link) if member_end && member_end != klass["name"] { xmi_id: link["xmi:id"], member_end: member_end, member_end_type: member_end_type, member_end_cardinality: member_end_cardinality, member_end_attribute_name: member_end_attribute_name, } end end end def serialize_class_constraints(klass) class_element_metadata(klass).xpath("./constraints/constraint").map do |constraint| { xmi_id: constraint["xmi:id"], body: constraint["name"], } end end def serialize_member_type(owned_xmi_id, link) return if link.name == 'NoteLink' return generalization_association(owned_xmi_id, link) if link.name == "Generalization" xmi_id = link.attributes["start"].value member_end = lookup_entity_name(xmi_id) member_end_node = if link.name == "Association" link_xmk_id = link["xmi:id"] main_model.xpath(%(//packagedElement[@xmi:id="#{link_xmk_id}"]//type[@xmi:idref="#{xmi_id}"])).first else main_model.xpath(%(//ownedAttribute[@association]/type[@xmi:idref="#{xmi_id}"])).first end if member_end_node assoc = member_end_node.parent member_end_cardinality = { "min" => cardinality_min_value(assoc), "max" => cardinality_max_value(assoc) } member_end_attribute_name = assoc.attributes["name"]&.value end [member_end, "aggregation", member_end_cardinality, member_end_attribute_name] end def generalization_association(owned_xmi_id, link) if link.attributes["start"].value == owned_xmi_id xmi_id = link.attributes["end"].value member_end_type = "inheritance" member_end = lookup_entity_name(xmi_id) || connector_target_name(xmi_id) else xmi_id = link.attributes["start"].value member_end_type = "generalization" member_end = lookup_entity_name(xmi_id) || connector_source_name(xmi_id) end member_end_node = main_model.xpath(%(//ownedAttribute[@association]/type[@xmi:idref="#{xmi_id}"])).first if member_end_node assoc = member_end_node.parent member_end_cardinality = { "min" => cardinality_min_value(assoc), "max" => cardinality_max_value(assoc) } end [member_end, member_end_type, member_end_cardinality, nil] end def class_element_metadata(klass) main_model.xpath(%(//element[@xmi:idref="#{klass['xmi:id']}"])) end def serialize_class_attributes(klass) klass.xpath('.//ownedAttribute[@xmi:type="uml:Property"]').map do |attribute| type = attribute.xpath(".//type").first || {} if attribute.attributes["association"].nil? { # TODO: xmi_id # xmi_id: klass['xmi:id'], name: attribute["name"], type: lookup_entity_name(type["xmi:idref"]) || type["xmi:idref"], is_derived: attribute["isDerived"], cardinality: { "min" => cardinality_min_value(attribute), "max" => cardinality_max_value(attribute) }, definition: lookup_attribute_definition(attribute), } end end.compact end def cardinality_min_value(node) lower_value_node = node.xpath(".//lowerValue").first return unless lower_value_node lower_value = lower_value_node.attributes["value"]&.value LOVER_VALUE_MAPPINGS[lower_value] end def cardinality_max_value(node) upper_value_node = node.xpath(".//upperValue").first return unless upper_value_node upper_value_node.attributes["value"]&.value end def doc_node_attribute_value(node, attr_name) xmi_id = node["xmi:id"] doc_node = main_model.xpath(%(//element[@xmi:idref="#{xmi_id}"]/properties)).first return unless doc_node doc_node.attributes[attr_name]&.value end def lookup_attribute_definition(node) xmi_id = node["xmi:id"] doc_node = main_model.xpath(%(//attribute[@xmi:idref="#{xmi_id}"]/documentation)).first return unless doc_node doc_node.attributes["value"]&.value end def lookup_entity_name(xmi_id) xmi_cache[xmi_id] ||= model_node_name_by_xmi_id(xmi_id) xmi_cache[xmi_id] end def connector_source_name(xmi_id) node = main_model.xpath(%(//source[@xmi:idref="#{xmi_id}"]/model)).first return unless node node.attributes["name"]&.value end def connector_target_name(xmi_id) node = main_model.xpath(%(//target[@xmi:idref="#{xmi_id}"]/model)).first return unless node node.attributes["name"]&.value end def model_node_name_by_xmi_id(xmi_id) node = main_model.xpath(%(//*[@xmi:id="#{xmi_id}"])).first return unless node node.attributes["name"]&.value end end end end end