XMI
Purpose
This Ruby object mapper is a module designed to convert XMI (XML Metadata Interchange) files into Ruby objects. It can also leverage the nokogiri gem for XML parsing and provides methods to dynamically generate Ruby classes and modules based on the XML structure, particularly those related to EA (Enterprise Architect) MDG (Model Driven Generation) extensions.
Installation
Add this line to your application’s Gemfile:
gem "xmi"And then execute:
$ bundle installOr install it yourself as:
$ gem install xmiUsage
Convert XMI file into Ruby objects
To convert XMI file into Ruby objects, run:
xml = "path/to/your/file.xmi"
xml_content = File.read(xml)
xmi_root_model = Xmi::Sparx::SparxRoot.parse_xml(xml_content)This method takes the path to an XMI file and generate the Ruby objects.
Loading Extensions and Generate Ruby Classes and Modules Dynamically
To load an extension from an XML file, use the load_extension method:
Xmi::EaRoot.load_extension("path/to/your/extension.xml")
xml = "path/to/your/file.xmi"
xml_content = File.read(xml)
xmi_root_model = Xmi::Sparx::SparxRoot.parse_xml(xml_content)Xmi::EaRoot.load_extension takes the path to an XML file and generate the
Ruby classes and modules defined in XML file dynamically.
Then, you can generate Ruby objects by Xmi::Sparx::SparxRoot.parse_xml.
Output Classes and Modules Generated from Extension into Ruby Files
You can also generate Ruby files directly from the XMI content:
Xmi::EaRoot.load_extension(
input_xml_path: 'path/to/your/custom_extension.xml',
module_name: 'CustomModule'
)
Xmi::EaRoot.output_rb_file('path/to/output_file.rb')This approach allows you to save the dynamically generated Ruby code to a file for further use.
Create Extension XML File
If you would like to create an extension, which allows to be loaded later, you can create an extension XML file.
For example, you would like to create an extension:
-
A Module named
Mymodule. -
A class named
Klassunder this module. -
The class
Klasshas two attributes:base_apply_attributeandtag.
First, you create an extension XML file mymodule.xml as the following:
<?xml version='1.0' encoding='UTF-8'?>
<MYMODULE version="1.0">
<UMLProfiles>
<UMLProfile profiletype="uml2">
<Documentation name="MYMODULE" version="1.0"
URI="http://www.test.com/profiles/MYMODULE/1.0" />
<Content>
<Stereotypes>
<Stereotype name="klass">
<AppliesTo>
<Apply type="ApplyAttribute" />
</AppliesTo>
<TaggedValues>
<Tag name="tag" type="String" description="" unit="" values="" default=""/>
</TaggedValues>
</Stereotype>
</Stereotypes>
<TaggedValueTypes />
<ViewDefinitions />
<Metamodel />
</Content>
</UMLProfile>
</UMLProfiles>
</MYMODULE>-
The attribute
namein theDocumentationdefines the module name. -
The attribute
URIin theDocumentationdefines the namespace. -
The attribute
versionin theDocumentationdefines the version. -
The attribute
namein theStereotypedefines the class name. -
The attribute
typein theApplydefines the attribute name with prefixbase_. -
The attribute
namein theTagdefines the attribute name.
To load the extension, you can use the following code:
mymodule_xml = "mymodule.xml"
Xmi::EaRoot.load_extension(mymodule_xml)After you load the extension, a class Klass have been generated in the module
Mymodule.
module Xmi
class EaRoot
module Mymodule
class Klass < Lutaml::Model::Serializable
attribute :base_apply_attribute, :string
attribute :tag, :string
xml do
root "import"
map_attribute "base_ApplyAttribute", to: :base_apply_attribute
map_attribute "tag", to: :tag
end
end
end
end
endLimitation
This module is designed to work with XMI files generated by Enterprise Architect. It may not work with other XMI files.
Some XML elements, for example GML:ApplicationSchema, use xmlns as
attributes. As Lutaml::Model::Serializable uses xmlns as an internal
keyword, these attributes will be renamed to altered_xmlns.
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to [rubygems.org](https://rubygems.org).
Contributing
Code of Conduct
Everyone interacting in the Xmi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](USERNAME/xmi/blob/master/CODE_OF_CONDUCT.md).