Skip to content

Instantly share code, notes, and snippets.

@weiserman
Created November 25, 2026 11:03
Show Gist options
  • Select an option

  • Save weiserman/4e4b45316599b4fbd1cf392eb6a35ed2 to your computer and use it in GitHub Desktop.

Select an option

Save weiserman/4e4b45316599b4fbd1cf392eb6a35ed2 to your computer and use it in GitHub Desktop.
zcl_mdg_brf_demo1.abap
CLASS zcl_mdg_brf_demo DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
*----------------------------------------------------------------------*
* ZCL_MDG_BRF_DEMO - Demo: MDG derivation rule in BRF+ -> CDS view
*
* Purpose
* End-to-end demo of the BRF+ to CDS converter. Run it with F9 in ADT
* (interface IF_OO_ADT_CLASSRUN). It does three things:
*
* 1. Creates a small BRF+ application with one MDG-style derivation rule:
* SCOPE : material type in (FERT, HALB) AND plant in (1010, 1020)
* DERIVATION : decision table (material type, plant) -> profit center
* 2. Reads the rule back with ZCL_BRF2CDS_GENERATOR and prints the CDS
* source it would generate (preview, no write).
* 3. Only if GC_GENERATE = abap_true: creates and activates the CDS view
* entity GC_CDS_NAME through XCO.
*
* What gets written to the system
* Step 1 creates BRF+ objects in a local application (package $TMP).
* Step 3 creates the DDLS in GC_PACKAGE on GC_TRANSPORT.
*
* Running it twice
* BRF+ application names are unique. For a second run, change
* GC_APPLICATION or call CREATE_SAMPLE_RULE with a new name.
*----------------------------------------------------------------------*
PUBLIC SECTION.
" Makes the class runnable with F9 in ADT (output goes to the console)
INTERFACES if_oo_adt_classrun.
CONSTANTS:
" Name of the BRF+ application that holds all demo objects
gc_application TYPE if_fdt_types=>name VALUE 'ZMDG_DEMO_DERIVATION',
" abap_false = preview only; abap_true = also create the CDS view
gc_generate TYPE abap_bool VALUE abap_false,
" Name of the CDS view entity the generator creates
gc_cds_name TYPE sxco_cds_object_name VALUE 'ZI_MDG_DERIVEPROFITCTR',
" Package and transport for the generated DDLS ($TMP needs no transport)
gc_package TYPE sxco_package VALUE '$TMP',
gc_transport TYPE sxco_transport VALUE ''.
" Builds the sample rule in BRF+ (elements, Boolean scope, decision
" table, rule and function), activates and saves it.
" IV_APPLICATION: name of the new BRF+ application (must not exist yet)
" Returns the ID of the rule, which is the input for the generator.
METHODS create_sample_rule
IMPORTING iv_application TYPE if_fdt_types=>name DEFAULT gc_application
RETURNING VALUE(rv_rule_id) TYPE if_fdt_types=>id
RAISING cx_fdt.
" Returns the generator, configured for this rule: which CDS view to
" select from, its key fields, and how each BRF+ element maps to a
" field of that view.
METHODS get_generator
RETURNING VALUE(ro_generator) TYPE REF TO zcl_brf2cds_generator.
PRIVATE SECTION.
" ID of the BRF+ application created by CREATE_APPLICATION
DATA mv_application_id TYPE if_fdt_types=>id.
" Creates, activates and saves an empty local BRF+ application
METHODS create_application
IMPORTING iv_application TYPE if_fdt_types=>name
RAISING cx_fdt.
" Creates a BRF+ text element bound to a DDIC data element.
" The binding gives the element the DDIC type, length and texts,
" and lets the generator CAST the derived value to that type.
METHODS create_element
IMPORTING iv_name TYPE if_fdt_types=>name
iv_ddic_type TYPE csequence
RETURNING VALUE(rv_id) TYPE if_fdt_types=>id
RAISING cx_fdt_input.
" Builds one BRF+ range line "= value" (sign I, option EQ)
METHODS eq_range
IMPORTING ia_value TYPE any
RETURNING VALUE(rs_range) TYPE if_fdt_range=>s_range.
" Copies a value into a new data reference of the same type.
" BRF+ stores range and cell values as REF TO data.
METHODS value_ref
IMPORTING ia_value TYPE any
RETURNING VALUE(rr_ref) TYPE REF TO data.
ENDCLASS.
CLASS zcl_mdg_brf_demo IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TRY.
" Step 1: create the BRF+ rule and remember its ID
DATA(lv_rule_id) = create_sample_rule( ).
out->write( |BRF+ rule created: { lv_rule_id }| ).
" Step 2: read the rule into the generator's neutral model
" (scope conditions + decision table rows) and print the CDS source
DATA(lo_generator) = get_generator( ).
DATA(ls_model) = lo_generator->read_rule( lv_rule_id ).
out->write( |--- CDS source for rule { ls_model-rule_name } ---| ).
out->write( lo_generator->preview( ls_model ) ).
" Step 3: write the view only when explicitly switched on
IF gc_generate = abap_true.
DATA(lt_findings) = lo_generator->generate( ls_model ).
out->write( |{ gc_cds_name } generated. Findings:| ).
out->write( lt_findings ).
ELSE.
out->write( |Preview only. Set GC_GENERATE = abap_true to create { gc_cds_name }.| ).
ENDIF.
CATCH cx_fdt INTO DATA(lx_fdt).
" BRF+ errors carry their details in MT_MESSAGE, not in the text
out->write( |BRF+ error: { lx_fdt->get_text( ) }| ).
LOOP AT lx_fdt->mt_message INTO DATA(ls_message).
out->write( ls_message-text ).
ENDLOOP.
CATCH zcx_brf2cds INTO DATA(lx_conv).
" The rule uses a construct the generator cannot translate yet
out->write( |Conversion error: { lx_conv->get_text( ) }| ).
ENDTRY.
ENDMETHOD.
METHOD get_generator.
" Field map: tells the generator which CDS field stands for which
" BRF+ element.
" ZMDG_MTART -> _Product.ProductType (via association, shown as ProductType)
" ZMDG_WERKS -> Plant (already a key field, so no extra alias)
" ZMDG_PRCTR -> no source field; it is the result column of the
" decision table and becomes DerivedProfitCenter
" Extra field: the stored profit center, so callers can compare it
" with the derived one.
ro_generator = NEW zcl_brf2cds_generator( VALUE #(
cds_name = gc_cds_name
description = 'MDG derivation: profit center (BRF+)'
package = gc_package
transport = gc_transport
data_source = 'I_PRODUCTPLANTBASIC'
key_fields = VALUE #( ( `Product` ) ( `Plant` ) )
field_map = VALUE #(
( element = 'ZMDG_MTART' cds_path = `_Product.ProductType` alias = 'ProductType' )
( element = 'ZMDG_WERKS' cds_path = `Plant` alias = '' )
( element = 'ZMDG_PRCTR' cds_path = `` alias = 'DerivedProfitCenter' ) )
extra_fields = VALUE #(
( cds_path = `ProfitCenter` alias = 'CurrentProfitCenter' ) ) ) ).
ENDMETHOD.
METHOD create_sample_rule.
" All objects below are created inside this application
create_application( iv_application ).
DATA(lo_factory) = cl_fdt_factory=>if_fdt_factory~get_instance( mv_application_id ).
"--------------------------------------------------------------------
" Context elements
" The three values the rule works with. They are bound to the same
" DDIC types MDG uses for material type, plant and profit center.
"--------------------------------------------------------------------
DATA(lv_mtart_id) = create_element( iv_name = 'ZMDG_MTART' iv_ddic_type = 'MTART' ).
DATA(lv_werks_id) = create_element( iv_name = 'ZMDG_WERKS' iv_ddic_type = 'WERKS_D' ).
DATA(lv_prctr_id) = create_element( iv_name = 'ZMDG_PRCTR' iv_ddic_type = 'PRCTR' ).
"--------------------------------------------------------------------
" Scope (Boolean expression)
" material type in (FERT, HALB) AND plant in (1010, 1020)
" Each operand is a range test on one element ("simplified" Boolean,
" as in SAP's FDT_DEMO_REPORT_BOOLEAN). The generator turns this
" into the WHERE clause of the view.
"--------------------------------------------------------------------
DATA lo_scope TYPE REF TO if_fdt_boolean.
lo_scope ?= lo_factory->get_expression( iv_expression_type_id = if_fdt_constants=>gc_exty_boolean ).
lo_scope->if_fdt_transaction~enqueue( ).
lo_scope->if_fdt_admin_data~set_name( 'ZMDG_SCOPE_PRCTR' ).
" Short texts are CHAR 20 in BRF+
lo_scope->if_fdt_admin_data~set_texts( iv_short_text = CONV #( 'Scope FERT/HALB' ) ).
" Several range lines on one element are combined with OR.
" POSITION orders the lines and must be unique within the range table.
DATA(ls_scope_mtart) = VALUE if_fdt_boolean=>s_param_range(
parameter_id = lv_mtart_id
ts_range = VALUE #( ( eq_range( CONV mtart( 'FERT' ) ) )
( VALUE #( BASE eq_range( CONV mtart( 'HALB' ) ) position = 2 ) ) ) ).
DATA(ls_scope_werks) = VALUE if_fdt_boolean=>s_param_range(
parameter_id = lv_werks_id
ts_range = VALUE #( ( eq_range( CONV werks_d( '1010' ) ) )
( VALUE #( BASE eq_range( CONV werks_d( '1020' ) ) position = 2 ) ) ) ).
lo_scope->set_operands( is_first_operand_range = ls_scope_mtart
is_second_operand_range = ls_scope_werks ).
lo_scope->set_operation( if_fdt_boolean=>gc_operation_a_and_b ).
"--------------------------------------------------------------------
" Derivation (decision table)
" Row | Material type | Plant | -> Profit center
" 1 | FERT | 1010 | -> YB101
" 2 | FERT | 1020 | -> YB102
" 3 | HALB | any | -> YB900
" First match wins. No match is allowed and returns no value.
" The generator turns the rows into CASE WHEN ... in the same order.
"--------------------------------------------------------------------
DATA lo_dt TYPE REF TO if_fdt_decision_table.
lo_dt ?= lo_factory->get_expression( iv_expression_type_id = if_fdt_constants=>gc_exty_decision_table ).
lo_dt->if_fdt_transaction~enqueue( ).
lo_dt->if_fdt_admin_data~set_name( 'ZMDG_DT_PRCTR' ).
lo_dt->if_fdt_admin_data~set_texts( iv_short_text = CONV #( 'Derive profit center' ) ).
" Two condition columns and one result column
lo_dt->set_columns( VALUE #( ( col_no = 1 object_id = lv_mtart_id is_result = abap_false )
( col_no = 2 object_id = lv_werks_id is_result = abap_false )
( col_no = 3 object_id = lv_prctr_id is_result = abap_true ) ) ).
lo_dt->set_table_property( iv_multiple_match = abap_false
iv_allow_no_match = abap_true ).
" The table content as plain ABAP data, converted to BRF+ cells below
TYPES: BEGIN OF ty_line,
mtart TYPE mtart,
werks TYPE werks_d,
prctr TYPE prctr,
END OF ty_line.
DATA lt_lines TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY.
lt_lines = VALUE #( ( mtart = 'FERT' werks = '1010' prctr = 'YB101' )
( mtart = 'FERT' werks = '1020' prctr = 'YB102' )
( mtart = 'HALB' werks = '' prctr = 'YB900' ) ).
" One cell per row and column:
" condition cells hold a range table (TS_RANGE)
" the result cell holds the value (R_VALUE)
DATA lts_data TYPE if_fdt_decision_table=>ts_table_data.
LOOP AT lt_lines INTO DATA(ls_line).
DATA(lv_row) = sy-tabix.
INSERT VALUE #( row_no = lv_row col_no = 1
ts_range = VALUE #( ( eq_range( ls_line-mtart ) ) ) ) INTO TABLE lts_data.
" An empty plant leaves the cell without a range, which matches any plant
INSERT VALUE #( row_no = lv_row col_no = 2
ts_range = COND #( WHEN ls_line-werks IS NOT INITIAL
THEN VALUE #( ( eq_range( ls_line-werks ) ) ) ) ) INTO TABLE lts_data.
INSERT VALUE #( row_no = lv_row col_no = 3
r_value = value_ref( ls_line-prctr ) ) INTO TABLE lts_data.
ENDLOOP.
lo_dt->set_table_data( lts_data ).
"--------------------------------------------------------------------
" Rule
" IF scope is true THEN profit center = result of the decision table
" This is the object the generator reads: its condition is the scope,
" its single expression assignment is the derivation.
"--------------------------------------------------------------------
DATA lo_rule TYPE REF TO if_fdt_rule.
lo_rule ?= lo_factory->get_expression( iv_expression_type_id = if_fdt_constants=>gc_exty_rule ).
lo_rule->if_fdt_transaction~enqueue( ).
lo_rule->if_fdt_admin_data~set_name( 'ZMDG_RULE_DERIVE_PRCTR' ).
lo_rule->if_fdt_admin_data~set_texts( iv_short_text = CONV #( 'Derive plant PrCtr' ) ).
lo_rule->set_condition( lo_scope->mv_id ).
lo_rule->set_true_action_extended(
it_expression_assignment = VALUE #( ( position = 1
expression = lo_dt->mv_id
target_data_object = lv_prctr_id ) ) ).
"--------------------------------------------------------------------
" Function
" Makes the rule callable in BRF+ (context = the three elements,
" result = profit center). Activating it with IV_DEEP = abap_true
" also activates every object it uses, so one call covers all.
"--------------------------------------------------------------------
DATA lo_function TYPE REF TO if_fdt_function.
lo_function ?= lo_factory->get_function( ).
lo_function->if_fdt_transaction~enqueue( ).
lo_function->if_fdt_admin_data~set_name( 'ZMDG_FN_DERIVE_PRCTR' ).
lo_function->set_context_data_objects( VALUE #( ( lv_mtart_id ) ( lv_werks_id ) ( lv_prctr_id ) ) ).
lo_function->set_expression( lo_rule->if_fdt_admin_data~mv_id ).
lo_function->set_result_data_object( lv_prctr_id ).
lo_function->if_fdt_transaction~activate( EXPORTING iv_deep = abap_true
IMPORTING et_message = DATA(lt_message)
ev_activation_failed = DATA(lv_failed) ).
IF lv_failed = abap_true.
" Release the locks and hand the BRF+ messages to the caller
lo_function->if_fdt_transaction~dequeue( iv_deep = abap_true ).
RAISE EXCEPTION TYPE cx_fdt_input EXPORTING mt_message = lt_message.
ENDIF.
lo_function->if_fdt_transaction~save( iv_deep = abap_true ).
lo_function->if_fdt_transaction~dequeue( iv_deep = abap_true ).
rv_rule_id = lo_rule->if_fdt_admin_data~mv_id.
ENDMETHOD.
METHOD create_application.
" A local application lives in $TMP and needs no transport
DATA(lo_factory) = cl_fdt_factory=>if_fdt_factory~get_instance( ).
DATA(lo_application) = lo_factory->get_application( ).
lo_application->if_fdt_transaction~enqueue( ).
lo_application->set_development_package( '$TMP' ).
lo_application->if_fdt_admin_data~set_name( iv_application ).
lo_application->if_fdt_admin_data~set_texts( iv_short_text = CONV #( 'Demo BRF+ to CDS' ) ).
lo_application->create_local_application( ).
lo_application->if_fdt_transaction~activate( IMPORTING et_message = DATA(lt_message)
ev_activation_failed = DATA(lv_failed) ).
IF lv_failed = abap_true.
lo_application->if_fdt_transaction~dequeue( ).
RAISE EXCEPTION TYPE cx_fdt_input EXPORTING mt_message = lt_message.
ENDIF.
lo_application->if_fdt_transaction~save( ).
lo_application->if_fdt_transaction~dequeue( ).
" Later objects are created inside this application
mv_application_id = lo_application->mv_id.
ENDMETHOD.
METHOD create_element.
" The element is left inactive here; the deep activation of the
" function in CREATE_SAMPLE_RULE activates it together with the rest.
cl_fdt_convenience=>create_element(
EXPORTING iv_name = iv_name
iv_application_id = mv_application_id
iv_element_type = if_fdt_constants=>gc_element_type_text
iv_activate = abap_false
IMPORTING ev_element_id = rv_id
eo_element = DATA(lo_element) ).
" Bind to the DDIC data element, so the generator can CAST the result
lo_element->if_fdt_data_object~set_ddic_binding( iv_ddic_type ).
ENDMETHOD.
METHOD eq_range.
" Position 1 by default; the caller overrides it for further lines
rs_range = VALUE #( position = 1
sign = if_fdt_range=>gc_sign_include
option = if_fdt_range=>gc_option_equal
r_low_value = value_ref( ia_value ) ).
ENDMETHOD.
METHOD value_ref.
" CREATE DATA ... LIKE keeps the DDIC type of the value (e.g. MTART),
" which BRF+ checks against the element type
CREATE DATA rr_ref LIKE ia_value.
ASSIGN rr_ref->* TO FIELD-SYMBOL(<lv_value>).
<lv_value> = ia_value.
ENDMETHOD.
ENDCLASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment