创建动态父子层次结构?

Create dynamic parent-child hierarchy?

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

示例数据:

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

我想根据以下行生成子父关系:生产者重量飞机并忽略颜色。然后我将更改它并使用:Color weight Airplane 并忽略 producer

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

       CompanyA               CompanyB
    330       222         222
A350             A320     B450
H450             

在内部table最后应该是这样

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

有没有人有好的高效想法? 非常感谢。

这是一个简单的 tree-building 算法,可以满足您的需求。

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

该算法也没有重构以获得最佳代码风格。例如,干净的代码表明我们可能想要提取几个方法来提高可读性。

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.