<commit_before>module DTK
  class ServiceAddOn
   private
    class Import 
      def initialize(library_idh,module_name,dsl_file,hash_content,ports,aug_assembly_nodes)
        @library_idh = library_idh
        @module_name = module_name
        @dsl_file = dsl_file
        @hash_content = hash_content
        @ports = ports
        augmnent_with_parsed_nanems_and_assembly_ids!(@ports,aug_assembly_nodes)
        @aug_assembly_nodes = aug_assembly_nodes
        @assemblies = find_assemblies(aug_assembly_nodes)
      end
      def import()
        type = (dsl_file =~ DslRegExp;$1)
        assembly,assembly_ref = ret_assembly_info(:assembly)
        sub_assembly,sa_ref,sub_assembly_id = ret_assembly_info(:add_on_sub_assembly)
        ao_input_hash = {
          :display_name => type,
          :description => hash_content["description"],
          :type => type,
          :sub_assembly_id => sub_assembly_id
        }
        port_links = import_add_on_port_links(ports,hash_content["port_links"],assembly,sub_assembly)
        unless port_links.empty?
          ao_input_hash.merge!(:port_link => port_links)
        end
        
        node_bindings = ServiceNodeBinding.import_add_on_node_bindings(@aug_assembly_nodes,hash_content["node_bindings"])
        unless node_bindings.empty?
          ao_input_hash.merge!(:service_node_binding => node_bindings)
        end

        input_hash = {assembly_ref => {:service_add_on => {type => ao_input_hash}}}
        Model.import_objects_from_hash(library_idh,"component" =>  input_hash)
      end

      def self.dsl_filename_path_info()
        {
          :regexp => DslRegExp,
          :path_depth => 4
        }
      end

     private
      include AssemblyImportExportCommon
      def import_add_on_port_links(ports,add_on_port_links,assembly,sub_assembly)
        ret = Hash.new
        return ret if (add_on_port_links||[]).empty?
        assembly_list = [assembly,sub_assembly]
        add_on_port_links.each do |ao_pl_ref,ao_pl|
          link = ao_pl["link"]
          input_assembly,input_port = AssemblyImportPortRef::AddOn.parse(link.values.first,assembly_list)
          output_assembly,output_port = AssemblyImportPortRef::AddOn.parse(link.keys.first,assembly_list)
          input_id = input_port.matching_id(ports)
          output_id = output_port.matching_id(ports)
          output_is_local = (output_assembly == assembly_name) 
          pl_hash = {"input_id" => input_id,"output_id" => output_id, "output_is_local" => output_is_local, "required" => ao_pl["required"]}
          ret.merge!(ao_pl_ref => pl_hash)
        end
        ret
      end
      
      def augment_with_assembly_ids!(ports)
        nil
      end

      DslRegExp = Regexp.new("add-ons/([^/]+)\.json$")    
      attr_reader :library_idh, :module_name, :dsl_file, :hash_content, :ports

      def import_port_link(port_link_info)
      end

      def find_assemblies(aug_assembly_nodes)
        ndx_ret = Hash.new
        aug_assembly_nodes.each do |n|
          assembly = n[:assembly]
          ndx_ret[assembly[:id]] ||= assembly
        end
        ndx_ret.values
      end

      def augmnent_with_parsed_nanems_and_assembly_ids!(ports,aug_assembly_nodes)
        ServiceModule::AssemblyImport.augment_with_parsed_port_names!(ports)
        ndx_node_assembly = aug_assembly_nodes.inject(Hash.new){|h,n|h.merge(n[:id] => n[:assembly][:id])}
        ports.each do |p|
          p[:assembly_id] ||= ndx_node_assembly[:node_node_id]
        end
      end

      #returns [assembly,assembly_ref]
      def ret_assembly_info(field)
        unless name = hash_content[field.to_s]
          raise ErrorUsage("Field (#{field}) not given in the service add-on file #{dsl_file}")
        end
        unless assembly = @assemblies.find{|a|a[:display_name] == name}
          Log.error("Field (#{field}) has value (#{name}) which is not a valid assembly reference")
        end
        [assembly,ServiceModule.assembly_ref(module_name,name)]
      end
    end
  end
end<commit_msg>feature_service_addons: working on bug where service add port links not translating to right port ids<commit_after>module DTK
  class ServiceAddOn
   private
    class Import 
      def initialize(library_idh,module_name,dsl_file,hash_content,ports,aug_assembly_nodes)
        @library_idh = library_idh
        @module_name = module_name
        @dsl_file = dsl_file
        @hash_content = hash_content
        @ports = ports
        augmnent_with_parsed_nanems_and_assembly_ids!(@ports,aug_assembly_nodes)
        @aug_assembly_nodes = aug_assembly_nodes
        @assemblies = find_assemblies(aug_assembly_nodes)
      end
      def import()
        type = (dsl_file =~ DslRegExp;$1)
        assembly,assembly_ref = ret_assembly_info(:assembly)
        sub_assembly,sa_ref,sub_assembly_id = ret_assembly_info(:add_on_sub_assembly)
        ao_input_hash = {
          :display_name => type,
          :description => hash_content["description"],
          :type => type,
          :sub_assembly_id => sub_assembly_id
        }
        port_links = import_add_on_port_links(ports,hash_content["port_links"],assembly,sub_assembly)
        unless port_links.empty?
          ao_input_hash.merge!(:port_link => port_links)
        end
        
        node_bindings = ServiceNodeBinding.import_add_on_node_bindings(@aug_assembly_nodes,hash_content["node_bindings"])
        unless node_bindings.empty?
          ao_input_hash.merge!(:service_node_binding => node_bindings)
        end

        input_hash = {assembly_ref => {:service_add_on => {type => ao_input_hash}}}
        Model.import_objects_from_hash(library_idh,"component" =>  input_hash)
      end

      def self.dsl_filename_path_info()
        {
          :regexp => DslRegExp,
          :path_depth => 4
        }
      end

     private
      include AssemblyImportExportCommon
      def import_add_on_port_links(ports,add_on_port_links,assembly,sub_assembly)
        ret = Hash.new
        return ret if (add_on_port_links||[]).empty?
        assembly_list = [assembly,sub_assembly]
        add_on_port_links.each do |ao_pl_ref,ao_pl|
          link = ao_pl["link"]
          input_assembly,input_port = AssemblyImportPortRef::AddOn.parse(link.values.first,assembly_list)
          output_assembly,output_port = AssemblyImportPortRef::AddOn.parse(link.keys.first,assembly_list)
          input_id = input_port.matching_id(ports)
          output_id = output_port.matching_id(ports)
          output_is_local = (output_assembly == assembly[:display_name]) 
          pl_hash = {"input_id" => input_id,"output_id" => output_id, "output_is_local" => output_is_local, "required" => ao_pl["required"]}
          ret.merge!(ao_pl_ref => pl_hash)
        end
        ret
      end
      
      def augment_with_assembly_ids!(ports)
        nil
      end

      DslRegExp = Regexp.new("add-ons/([^/]+)\.json$")    
      attr_reader :library_idh, :module_name, :dsl_file, :hash_content, :ports

      def import_port_link(port_link_info)
      end

      def find_assemblies(aug_assembly_nodes)
        ndx_ret = Hash.new
        aug_assembly_nodes.each do |n|
          assembly = n[:assembly]
          ndx_ret[assembly[:id]] ||= assembly
        end
        ndx_ret.values
      end

      def augmnent_with_parsed_nanems_and_assembly_ids!(ports,aug_assembly_nodes)
        ServiceModule::AssemblyImport.augment_with_parsed_port_names!(ports)
        ndx_node_assembly = aug_assembly_nodes.inject(Hash.new){|h,n|h.merge(n[:id] => n[:assembly][:id])}
        ports.each do |p|
          p[:assembly_id] ||= ndx_node_assembly[p[:node_node_id]]
        end
      end

      #returns [assembly,assembly_ref]
      def ret_assembly_info(field)
        unless name = hash_content[field.to_s]
          raise ErrorUsage("Field (#{field}) not given in the service add-on file #{dsl_file}")
        end
        unless assembly = @assemblies.find{|a|a[:display_name] == name}
          Log.error("Field (#{field}) has value (#{name}) which is not a valid assembly reference")
        end
        [assembly,ServiceModule.assembly_ref(module_name,name)]
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09709452004413387, 'fraction_numerical': 0.000490376363859262, 'mean_word_length': 2.94296761720638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6199482', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2560, 'n_words': 407}