是否创建动态父子层次结构



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

示例数据:

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

我想基于以下行生成一个父子关系:生产者权重飞机和忽略颜色。然后我会改变它并使用:色重飞机和忽略生产者

最后,我需要一个看起来像";生产商重量的飞机;

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
.....

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

这里有一个简单的树构建算法,可以满足您的需要。

请注意,该算法在性能方面没有进行优化。如果您管理大量数据,您可能需要修改某些方面,例如使用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.

最新更新