创建动态父子层次结构?

时间:2020-10-20 06:46:06

标签: parent-child abap hierarchy internal-tables

您知道一种从没有父节点ID的表生成父子层次结构的有效方法吗?父子关系应该是可切换的。所有这些都应在ABAP中完成。

示例数据:

Color   producer    weight  Airplane(key)
green   CompanyA    330     A350
green   CompanyA    222     A320
green   CompanyB    222     B450
yellow  CompanyA    330     H450

我要基于以下行生成子级父级关系:生产者权重Airplane并忽略颜色。然后,我将其更改并使用:调整飞机的颜色重量并忽略生产者

最后,我需要一个类似于“生产者体重飞机”的结果

       CompanyA               CompanyB
    330       222         222
A350             A320     B450
H450             

在内部表中,最后应该是这样

ID  attribute   value       H_level parentID
1   producer    CompanyA    1       
2   weight      330         2       1
3   airplane    A350        3       2
.....

有人有一个有效的好主意吗? 非常感谢。

1 个答案:

答案 0 :(得分:1)

这是一种简单的树构建算法,可以满足您的需求。

请注意,此算法并未在性能方面进行优化。如果您管理大量数据,则可能需要修改某些方面,例如,使用SORTED或HASHED而不是STANDARD表来改进查找READ TABLE。

该算法也未针对最佳代码样式进行重构。例如,干净的代码表明我们可能想提取两种方法来提高可读性。

CLASS hierarchy_builder DEFINITION
    PUBLIC FINAL CREATE PUBLIC.

  PUBLIC SECTION.

    TYPES:
      BEGIN OF ts_input,
        color    TYPE string,
        producer TYPE string,
        weight   TYPE string,
        airplane TYPE string,
      END OF ts_input.

    TYPES tt_input TYPE
      STANDARD TABLE OF ts_input
        WITH EMPTY KEY.

    TYPES tt_attributes TYPE string_table.

    TYPES:
      BEGIN OF ts_output,
        id        TYPE string,
        attribute TYPE string,
        value     TYPE string,
        level     TYPE i,
        parent_id TYPE string,
      END OF ts_output.

    TYPES tt_output
      TYPE STANDARD TABLE OF ts_output
        WITH EMPTY KEY.

    CLASS-METHODS build_hierarchy
      IMPORTING
        it_data          TYPE tt_input
        it_hierarchy     TYPE tt_attributes
      RETURNING
        VALUE(rt_result) TYPE tt_output.

ENDCLASS.

CLASS hierarchy_builder IMPLEMENTATION.

  METHOD build_hierarchy.

    DATA(lv_parent_attribute) = ``.
    DATA(lv_next_id) = 1.

    LOOP AT it_hierarchy INTO DATA(lv_child_attribute).

      DATA(lv_level) = sy-tabix.

      LOOP AT it_data INTO DATA(ls_data).

        DATA(lv_parent_id) = ``.

        ASSIGN COMPONENT lv_child_attribute
          OF STRUCTURE ls_data
          TO FIELD-SYMBOL(<lv_child_value>).

        IF lv_parent_attribute IS NOT INITIAL.

          ASSIGN COMPONENT lv_parent_attribute
            OF STRUCTURE ls_data
            TO FIELD-SYMBOL(<lv_parent_value>).

          READ TABLE rt_result
            INTO DATA(ls_parent)
            WITH KEY
              attribute = lv_parent_attribute
              value = <lv_parent_value>.

          lv_parent_id = ls_parent-id.

        ENDIF.

        READ TABLE rt_result
          TRANSPORTING NO FIELDS
          WITH KEY
            attribute = lv_child_attribute
            value = <lv_child_value>
            parent_id = lv_parent_id.

        IF sy-subrc <> 0.

          INSERT VALUE #(
              id = |{ lv_next_id }|
              attribute = lv_child_attribute
              value = <lv_child_value>
              level = lv_level
              parent_id = lv_parent_id )
            INTO TABLE rt_result.

          lv_next_id += 1.

        ENDIF.

      ENDLOOP.

      lv_parent_attribute = lv_child_attribute.

    ENDLOOP.

  ENDMETHOD.

ENDCLASS.

已通过以下单元测试验证。它代表了您给出的示例:

CLASS ltc_unit_tests DEFINITION
    FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.

  PRIVATE SECTION.

    METHODS builds_example FOR TESTING.

ENDCLASS.

CLASS ltc_unit_tests IMPLEMENTATION.

  METHOD builds_example.

    DATA(lt_data) =
      VALUE hierarchy_builder=>tt_input(
        ( color = 'green' producer = 'CompanyA' weight = '330' airplane = 'A350' )
        ( color = 'green' producer = 'CompanyA' weight = '222' airplane = 'A320' )
        ( color = 'green' producer = 'CompanyB' weight = '222' airplane = 'B450' )
        ( color = 'yellow' producer = 'CompanyA' weight = '330' airplane = 'H450' ) ).

    DATA(lt_hierarchy) =
      VALUE hierarchy_builder=>tt_attributes(
        ( `PRODUCER` )
        ( `WEIGHT` )
        ( `AIRPLANE` ) ).

    DATA(lt_result) =
      hierarchy_builder=>build_hierarchy(
        it_data = lt_data
        it_hierarchy = lt_hierarchy ).

    DATA(lt_expected) =
      VALUE hierarchy_builder=>tt_output(
        ( id = '1' attribute = 'PRODUCER' value = 'CompanyA' level = 1 parent_id = '' )
        ( id = '2' attribute = 'PRODUCER' value = 'CompanyB' level = 1 parent_id = '' )
        ( id = '3' attribute = 'WEIGHT' value = '330' level = 2 parent_id = '1' )
        ( id = '4' attribute = 'WEIGHT' value = '222' level = 2 parent_id = '1' )
        ( id = '5' attribute = 'WEIGHT' value = '222' level = 2 parent_id = '2' )
        ( id = '6' attribute = 'AIRPLANE' value = 'A350' level = 3 parent_id = '3' )
        ( id = '7' attribute = 'AIRPLANE' value = 'A320' level = 3 parent_id = '4' )
        ( id = '8' attribute = 'AIRPLANE' value = 'B450' level = 3 parent_id = '4' )
        ( id = '9' attribute = 'AIRPLANE' value = 'H450' level = 3 parent_id = '3' ) ).

    cl_abap_unit_assert=>assert_equals(
        act = lt_result
        exp = lt_expected ).

  ENDMETHOD.

ENDCLASS.