Class: DecisionAgent::Dmn::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/dmn/exporter.rb

Overview

Exports DecisionAgent rules to DMN XML format

Instance Method Summary collapse

Constructor Details

#initialize(version_manager: nil) ⇒ Exporter

Returns a new instance of Exporter.



12
13
14
# File 'lib/decision_agent/dmn/exporter.rb', line 12

def initialize(version_manager: nil)
  @version_manager = version_manager || Versioning::VersionManager.new
end

Instance Method Details

#export(rule_id, output_path: nil) ⇒ String

Export ruleset to DMN XML

Parameters:

  • rule_id (String)

    Rule ID to export

  • output_path (String, nil) (defaults to: nil)

    Optional file path to write

Returns:

  • (String)

    DMN XML content

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/decision_agent/dmn/exporter.rb', line 41

def export(rule_id, output_path: nil)
  # Get active version
  version = @version_manager.get_active_version(rule_id: rule_id)
  raise InvalidDmnModelError, "No active version found for '#{rule_id}'" unless version

  # Convert JSON rules to DMN
  dmn_xml = convert_to_dmn(version[:content], rule_id)

  # Write to file if path provided
  File.write(output_path, dmn_xml) if output_path

  dmn_xml
end

#serialize_model(model) ⇒ String

Serialize an in-memory DMN Model object to DMN XML. Unlike #export, this does NOT look up any stored version — it converts the live model directly. Use this when saving a new version.

Parameters:

Returns:

  • (String)

    DMN XML



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/decision_agent/dmn/exporter.rb', line 21

def serialize_model(model)
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.definitions(
      "xmlns" => "https://www.omg.org/spec/DMN/20191111/MODEL/",
      "xmlns:dmndi" => "https://www.omg.org/spec/DMN/20191111/DMNDI/",
      "xmlns:dc" => "http://www.omg.org/spec/DMN/20180521/DC/",
      "id" => "definitions_#{model.id}",
      "name" => model.name,
      "namespace" => model.namespace
    ) do
      model.decisions.each { |d| serialize_decision_node(xml, d) }
    end
  end
  builder.to_xml
end