Certainly! Let's continue building your ABAP program by processing the UML data to generate the PlantUML diagram. We'll introduce configuration parameters to control which parts of the classes (like data types, method parameters, attributes, methods) should be included in the diagram. We'll ensure that the ABAP syntax and types are correctly used.
First, we'll define configuration parameters on the selection screen:
" Configuration parameters
PARAMETERS:
p_inc_dtypes AS CHECKBOX DEFAULT abap_true USER-COMMAND config, " Include data types
p_inc_mparams AS CHECKBOX DEFAULT abap_true USER-COMMAND config, " Include method parameters
p_inc_attrs AS CHECKBOX DEFAULT abap_true USER-COMMAND config, " Include attributes
p_inc_methods AS CHECKBOX DEFAULT abap_true USER-COMMAND config. " Include methods
Next, we'll define a configuration structure to hold these parameters:
" Define a configuration structure
TYPES: BEGIN OF ty_config,
include_data_types TYPE abap_bool,
include_method_parameters TYPE abap_bool,
include_attributes TYPE abap_bool,
include_methods TYPE abap_bool,
END OF ty_config.
" Data declaration for configuration
DATA: ls_config TYPE ty_config.
" Assign configuration parameters
ls_config-include_data_types = p_inc_dtypes.
ls_config-include_method_parameters = p_inc_mparams.
ls_config-include_attributes = p_inc_attrs.
ls_config-include_methods = p_inc_methods.
Now, let's continue from where you left off. We will process the UML table and generate the PlantUML text accordingly:
" Process classes, interfaces, and collect relations
LOOP AT <lt_uml_tab> ASSIGNING FIELD-SYMBOL(<ls_uml_line>).
CASE <ls_uml_line>-type. " Adjust the field name based on actual structure
WHEN 'class' OR 'interface'.
" Start class or interface declaration
lv_line = <ls_uml_line>-definition. " Adjust field name accordingly
APPEND lv_line TO lt_output.
" Open class/interface block
APPEND '{' TO lt_output.
" Process attributes if included in configuration
IF ls_config-include_attributes = abap_true AND <ls_uml_line>-attributes IS NOT INITIAL.
LOOP AT <ls_uml_line>-attributes ASSIGNING FIELD-SYMBOL(<ls_attribute>).
lv_line = ' '.
lv_line = lv_line && <ls_attribute>-visibility && ' '.
lv_line = lv_line && <ls_attribute>-name.
" Include data types if configured
IF ls_config-include_data_types = abap_true AND <ls_attribute>-datatype IS NOT INITIAL.
lv_line = lv_line && ' : ' && <ls_attribute>-datatype.
ENDIF.
APPEND lv_line TO lt_output.
ENDLOOP.
ENDIF.
" Process methods if included in configuration
IF ls_config-include_methods = abap_true AND <ls_uml_line>-methods IS NOT INITIAL.
LOOP AT <ls_uml_line>-methods ASSIGNING FIELD-SYMBOL(<ls_method>).
lv_line = ' '.
lv_line = lv_line && <ls_method>-visibility && ' '.
lv_line = lv_line && <ls_method>-name.
" Include method parameters if configured
IF ls_config-include_method_parameters = abap_true.
lv_line = lv_line && '('.
" Process method parameters
IF <ls_method>-parameters IS NOT INITIAL.
DATA(lv_param_list) = ''.
LOOP AT <ls_method>-parameters ASSIGNING FIELD-SYMBOL(<ls_param>).
DATA(lv_param) = <ls_param>-name.
" Include data types if configured
IF ls_config-include_data_types = abap_true AND <ls_param>-datatype IS NOT INITIAL.
lv_param = lv_param && ' : ' && <ls_param>-datatype.
ENDIF.
" Append parameter to parameter list
IF lv_param_list IS INITIAL.
lv_param_list = lv_param.
ELSE.
lv_param_list = lv_param_list && ', ' && lv_param.
ENDIF.
ENDLOOP.
lv_line = lv_line && lv_param_list.
ENDIF.
lv_line = lv_line && ')'.
ELSE.
lv_line = lv_line && '()'.
ENDIF.
" Include return type if configured and available
IF ls_config-include_data_types = abap_true AND <ls_method>-return_type IS NOT INITIAL.
lv_line = lv_line && ' : ' && <ls_method>-return_type.
ENDIF.
APPEND lv_line TO lt_output.
ENDLOOP.
ENDIF.
" Close class/interface block
APPEND '}' TO lt_output.
WHEN 'relation'.
" Collect relations separately
APPEND <ls_uml_line> TO lt_relations.
WHEN OTHERS.
" Handle other kinds if necessary
ENDCASE.
ENDLOOP.
" Process relations
LOOP AT lt_relations ASSIGNING FIELD-SYMBOL(<ls_relation>).
lv_line = <ls_relation>-definition. " Adjust field name accordingly
APPEND lv_line TO lt_output.
ENDLOOP.
" Finish the PlantUML diagram
APPEND '@enduml' TO lt_output.
" Output the PlantUML text
LOOP AT lt_output INTO lv_line.
WRITE / lv_line.
ENDLOOP.
Notes:
- Field Adjustments: Ensure that you adjust the field names (
<ls_uml_line>-type, <ls_uml_line>-definition, <ls_uml_line>-attributes, <ls_attribute>-visibility, etc.) to match the actual structure provided by cl_uml_class_scanner.
- Data Types: The
ls_config structure holds boolean flags to include or exclude various components in the diagram.
- Visibility Symbols: The
<ls_attribute>-visibility and <ls_method>-visibility should contain symbols like + (public), - (private), # (protected). Ensure these are properly set in your data.
Example Structure Adjustments:
" Assuming the structure of uml_line
TYPES: BEGIN OF ty_uml_line,
type TYPE string, " 'class', 'interface', 'relation', etc.
definition TYPE string, " Definition line for class/interface/relation
attributes TYPE STANDARD TABLE OF ty_attribute WITH EMPTY KEY,
methods TYPE STANDARD TABLE OF ty_method WITH EMPTY KEY,
END OF ty_uml_line.
" Attribute structure
TYPES: BEGIN OF ty_attribute,
visibility TYPE string, " '+', '-', '#', etc.
name TYPE string, " Attribute name
datatype TYPE string, " Data type
END OF ty_attribute.
" Method structure
TYPES: BEGIN OF ty_method,
visibility TYPE string, " '+', '-', '#', etc.
name TYPE string, " Method name
parameters TYPE STANDARD TABLE OF ty_parameter WITH EMPTY KEY,
return_type TYPE string, " Return data type
END OF ty_method.
" Parameter structure
TYPES: BEGIN OF ty_parameter,
name TYPE string, " Parameter name
datatype TYPE string, " Data type
END OF ty_parameter.
Important: The above types are hypothetical and need to be aligned with the actual types provided by cl_uml_class_scanner.
Explanation:
- We start by processing each UML line in
<lt_uml_tab>.
- For each class or interface, we write the definition, open a block (
{), and then add attributes and methods based on the configuration.
- Attributes and methods are indented with two spaces.
- For methods, we include parameters and return types if configured.
- Relations are collected separately and appended after processing classes and interfaces.
- Finally, we close the PlantUML diagram with
@enduml and output the entire content.
ABAP Syntax and Types:
- We use field symbols to dynamically access the contents of the tables.
- We ensure that all data types and structures match the expected types in the code.
- We handle optional fields and configurations carefully to avoid runtime errors.
Complete Program:
Here's the complete program with the additions:
REPORT zuml_generator.
PARAMETERS:
p_obj TYPE seoclsname, " Object name (Class/Interface)
p_pkg TYPE devclass. " Package name
" Configuration parameters
PARAMETERS:
p_inc_dtypes AS CHECKBOX DEFAULT abap_true USER-COMMAND config, " Include data types
p_inc_mparams AS CHECKBOX DEFAULT abap_true USER-COMMAND config, " Include method parameters
p_inc_attrs AS CHECKBOX DEFAULT abap_true USER-COMMAND config, " Include attributes
p_inc_methods AS CHECKBOX DEFAULT abap_true USER-COMMAND config. " Include methods
" Ensure at least one parameter is provided
AT SELECTION-SCREEN.
IF p_obj IS INITIAL AND p_pkg IS INITIAL.
MESSAGE 'Please specify either an object name or a package name.' TYPE 'E'.
ENDIF.
" Data declarations
DATA: lo_uml_scanner TYPE REF TO cl_uml_class_scanner,
lv_puml_text TYPE string.
" Create an instance of the UML Class Scanner
CREATE OBJECT lo_uml_scanner.
" Set up scanner configuration
lo_uml_scanner->set_scanner_configuration(
EXPORTING
scan_local_types = abap_true
add_structures = abap_true
scan_used_types = abap_true
scan_function_groups = abap_false
scan_programs = abap_false
scan_deep = abap_false
scan_business_objects = abap_false
max_scan_depth = 10
).
" Define which types to scan (type range)
DATA: lt_scan_types TYPE cl_uml_class_scanner=>uml_range_types,
ls_scan_type LIKE LINE OF lt_scan_types.
IF p_obj IS NOT INITIAL.
ls_scan_type-sign = 'I'.
ls_scan_type-option = 'EQ'.
ls_scan_type-low = p_obj.
APPEND ls_scan_type TO lt_scan_types.
ENDIF.
" Define which packages to scan (package range)
DATA: lt_scan_packages TYPE cl_uml_class_scanner=>uml_range_packages,
ls_scan_package LIKE LINE OF lt_scan_packages.
IF p_pkg IS NOT INITIAL.
ls_scan_package-sign = 'I'.
ls_scan_package-option = 'EQ'.
ls_scan_package-low = p_pkg.
APPEND ls_scan_package TO lt_scan_packages.
ENDIF.
" Execute the scanner
TRY.
lo_uml_scanner->execute(
EXPORTING
scan_packages = lt_scan_packages
scan_types = lt_scan_types
).
CATCH cx_root INTO DATA(lx_root).
MESSAGE lx_root->get_text( ) TYPE 'E'.
EXIT.
ENDTRY.
" Get the diagram data
DATA: lv_kind TYPE c,
lr_data TYPE REF TO data.
lo_uml_scanner->get_diagram(
IMPORTING
e_kind = lv_kind
CHANGING
c_data = lr_data
).
" Check if the returned kind is the expected UML table
IF lv_kind <> lo_uml_scanner->c_kind_uml_tab.
MESSAGE 'Unexpected diagram kind' TYPE 'E'.
EXIT.
ENDIF.
" Assign the diagram data to a field symbol
FIELD-SYMBOLS: <lt_uml_tab> TYPE cl_uml_class_scanner=>uml_tab.
ASSIGN lr_data->* TO <lt_uml_tab>.
IF sy-subrc <> 0 OR <lt_uml_tab> IS INITIAL.
MESSAGE 'Failed to retrieve diagram data' TYPE 'E'.
EXIT.
ENDIF.
" Define a configuration structure
TYPES: BEGIN OF ty_config,
include_data_types TYPE abap_bool,
include_method_parameters TYPE abap_bool,
include_attributes TYPE abap_bool,
include_methods TYPE abap_bool,
END OF ty_config.
" Data declaration for configuration
DATA: ls_config TYPE ty_config.
" Assign configuration parameters
ls_config-include_data_types = p_inc_dtypes.
ls_config-include_method_parameters = p_inc_mparams.
ls_config-include_attributes = p_inc_attrs.
ls_config-include_methods = p_inc_methods.
" Generate PlantUML text from the diagram data
DATA: lt_output TYPE TABLE OF string,
lt_relations TYPE STANDARD TABLE OF cl_uml_class_scanner=>uml_line,
lv_line TYPE string.
APPEND '@startuml' TO lt_output.
" Process classes, interfaces, and collect relations
LOOP AT <lt_uml_tab> ASSIGNING FIELD-SYMBOL(<ls_uml_line>).
CASE <ls_uml_line>-type.
WHEN 'class' OR 'interface'.
" Start class or interface declaration
lv_line = <ls_uml_line>-definition.
APPEND lv_line TO lt_output.
" Open class/interface block
APPEND '{' TO lt_output.
" Process attributes
IF ls_config-include_attributes = abap_true.
LOOP AT <ls_uml_line>-attributes ASSIGNING FIELD-SYMBOL(<ls_attribute>).
lv_line = ' '.
lv_line = lv_line && <ls_attribute>-visibility && ' '.
lv_line = lv_line && <ls_attribute>-name.
IF ls_config-include_data_types = abap_true AND <ls_attribute>-datatype IS NOT INITIAL.
lv_line = lv_line && ' : ' && <ls_attribute>-datatype.
ENDIF.
APPEND lv_line TO lt_output.
ENDLOOP.
ENDIF.
" Process methods
IF ls_config-include_methods = abap_true.
LOOP AT <ls_uml_line>-methods ASSIGNING FIELD-SYMBOL(<ls_method>).
lv_line = ' '.
lv_line = lv_line && <ls_method>-visibility && ' '.
lv_line = lv_line && <ls_method>-name.
IF ls_config-include_method_parameters = abap_true.
lv_line = lv_line && '('.
DATA(lv_param_list) = ''.
LOOP AT <ls_method>-parameters ASSIGNING FIELD-SYMBOL(<ls_param>).
DATA(lv_param) = <ls_param>-name.
IF ls_config-include_data_types = abap_true AND <ls_param>-datatype IS NOT INITIAL.
lv_param = lv_param && ' : ' && <ls_param>-datatype.
ENDIF.
IF lv_param_list IS INITIAL.
lv_param_list = lv_param.
ELSE.
lv_param_list = lv_param_list && ', ' && lv_param.
ENDIF.
ENDLOOP.
lv_line = lv_line && lv_param_list && ')'.
ELSE.
lv_line = lv_line && '()'.
ENDIF.
" Include return type if available
IF ls_config-include_data_types = abap_true AND <ls_method>-return_type IS NOT INITIAL.
lv_line = lv_line && ' : ' && <ls_method>-return_type.
ENDIF.
APPEND lv_line TO lt_output.
ENDLOOP.
ENDIF.
" Close class/interface block
APPEND '}' TO lt_output.
WHEN 'relation'.
" Collect relations
APPEND <ls_uml_line> TO lt_relations.
WHEN OTHERS.
" Handle other kinds if necessary
ENDCASE.
ENDLOOP.
" Process relations
LOOP AT lt_relations ASSIGNING FIELD-SYMBOL(<ls_relation>).
lv_line = <ls_relation>-definition.
APPEND lv_line TO lt_output.
ENDLOOP.
" Finish the PlantUML diagram
APPEND '@enduml' TO lt_output.
" Output the PlantUML text
LOOP AT lt_output INTO lv_line.
WRITE / lv_line.
ENDLOOP.
Final Steps:
- Replace field names in the code with the actual field names from
cl_uml_class_scanner=>uml_line.
- Ensure that any optional fields are checked for
IS NOT INITIAL before usage to prevent runtime errors.
- Test the program with different configurations to verify that it includes or excludes the desired parts in the UML diagram.
Conclusion:
We've successfully continued your program by adding code to process the UML data and generate the PlantUML diagram according to the specified configuration. The code carefully considers ABAP syntax and types, and uses configuration parameters to control which parts of the classes are displayed in the diagram.