‎2013 Aug 09 11:34 AM
Hello Experts,
I am trying to learn RTTS. I am understanding certain concepts but still there are areas which I need more clarification. The below given link has been advised by many experts to learn RTTS. http://scn.sap.com/thread/1725739. Can anyone explain the correct answer mentioned in this thread.
Regards,
Marina.
‎2013 Aug 09 11:59 AM
Hi Marina,
Please see this links for better understanding about RTTS
http://wiki.sdn.sap.com/wiki/display/ABAP/Runtime+Type+Services+%28RTTS%29
‎2013 Aug 09 2:47 PM
Hi Marina,
RTTC (run-time type creation) definitely is not very handy in ABAP.
I have a commented example for you that shall illustrate the single steps necessary to create and use a fully dynamic structure.
So here we go:
" this is a fully dynamic (RTTC) version to create the following code:
" DATA:
" BEGIN OF ls_dynamic_struct,
" material TYPE matnr,
" mattext TYPE maktx,
" END OF ls_dynamic_struct.
"
" ls_dynamic_struct-material = '4711-0815'.
" ls_dynamic_struct-mattext = 'Dynamic Structure Demonstration Material'.
"
" WRITE: ls_dynamic_struct.
DATA:
gs_component TYPE cl_abap_structdescr=>component, " type inside class CL_ABAP_STRUCTDESCR
gt_compontents TYPE cl_abap_structdescr=>component_table, " type inside class CL_ABAP_STRUCTDESCR
lo_dynamic_struct TYPE REF TO cl_abap_structdescr, " the structure's type definition
ls_dynamic_struct TYPE REF TO data. " the structure (created via CREATE DATA)
FIELD-SYMBOLS:
<dynamic_struct> TYPE any, " associated with the dynamically created structure
<struct_material> TYPE any, " associated with the structure's MATERIAL component
<struct_mattext> TYPE any. " associated with the structure's MATTEXT component
" define the structure's first component MATERIAL
gs_component-name = 'MATERIAL'.
" create a type description for ABAP dictionary type MATNR (material number) - casting required
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).
APPEND gs_component TO gt_compontents.
" define the structure's second component MATTEXT
gs_component-name = 'MATTEXT'.
" create a type description for ABAP dictionary type MAKTX (material short text) - casting required
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).
APPEND gs_component TO gt_compontents.
" create structure's type definition from the component table
lo_dynamic_struct = cl_abap_structdescr=>create( gt_compontents ).
" now create memory structure to actually store the data
CREATE DATA:
ls_dynamic_struct TYPE HANDLE lo_dynamic_struct.
" assign field symbols to structure and all of its components
" this is necessary to be able to access the structure and its components
" since the classical struct_var-component = value doesn't work for RTTC
ASSIGN ls_dynamic_struct->* TO <dynamic_struct>.
ASSIGN COMPONENT 'MATERIAL' OF STRUCTURE <dynamic_struct> TO <struct_material>.
ASSIGN COMPONENT 'MATTEXT' OF STRUCTURE <dynamic_struct> TO <struct_mattext>.
" assign values to both components
<struct_material> = '4711-0815'.
<struct_mattext> = 'Dynamic Structure Demonstration Material'.
" write structure
WRITE: <dynamic_struct>.
Though this seems pretty weird it definitely works.
Regards,
Chris
‎2013 Aug 09 3:22 PM
Hi,
I am quite new to ABAP (since 2006) but not to data processing (since 1973).
All my professional career was within manufacturing (Apparel , Chemicals) .
So my questions:
Are you using it a lot ?
What are the business circumstances where RTTC (RunTime Type Creation) is the most cost effective solution ?
Thanks for any hint.
Regards.
‎2013 Aug 10 9:34 AM
Hi Eitan,
as you have seen for yourself RTTC is not very handy in ABAP. The code becomes quite confusing for more complex use cases - just imagine to create an internal table that stores a more complex structure (> 10 elements) and everything has to be created via RTTC ...
I'm not using RTTC very often but there is however one use case where I find it particularly useful and beneficially to employ:
Imagine getting a structured record description in a text file like
01 CUSTOMER_ID CHAR 10
02 CUST_NAME CHAR 40
03 CUST_ADDR CHAR 80
04 CUST_ACTIVE NUM
05 DATA_CREATE DAT
06 DATA_CHANGE DAT
along with the real data - in that case you can parse the record description and create a suitable structure via RTTC to process the actual data for instance from a CSV file.
And the good news is that the code will work for every CSV file that comes with such a record description - extremely flexible!
But again - there are only a few circumstances where the (considerable) effort is justified and even then it makes sense to encapsulate some of the steps to create tables and structures via RTTC in a class hierarchy to make the code more expressive and easier to maintain.
Example:
DATA:
lo_container TYPE REF TO zcl_abap_rttc_container,
lo_structure TYPE REF TO zcl_abap_struct_component.
lo_container = zcl_abap_rttc_container_factory=>create( ).
lo_structure = lo_container->create_structure( 'LINE_STRUCT' ).
lo_structure->add_element( 'MATERIAL', 'MATNR', 'material number' ).
lo_structure->add_element( 'MATTEXT', 'MAKTX', 'material short text' ).
Regards,
Chris
PS: If you consider yourself new to ABAP because you're using it since 2006 then I'm an absolute rookie (using ABAP since 2010)
‎2013 Aug 10 2:23 PM
Hi Chris,
Thank you very much for your time and the gracious explanation .
regards .
‎2013 Aug 10 11:07 PM
" define the structure's first component MATERIAL
gs_component-name = 'MATERIAL'.
" create a type description for ABAP dictionary type MATNR (material number) - casting required
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).
APPEND gs_component TO gt_compontents.
How can we write component name as Material and component type as MATNR when we do not the component name and type before hand.
" define the structure's second component MATTEXT
gs_component-name = 'MATTEXT'.
" create a type description for ABAP dictionary type MAKTX (material short text) - casting required
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).
APPEND gs_component TO gt_compontents.
" create structure's type definition from the component table
How can we write component name as MATTEXT and component type as MAKTX when we do not the component name and type before hand.
Regards,
Marina.
‎2013 Aug 11 4:11 AM
Hi Christian,
Thanks for your reply. But I have some doubts as well. Below I have highlighted them.
" define the structure's first component MATERIAL
gs_component-name = 'MATERIAL'.
" create a type description for ABAP dictionary type MATNR (material number) - casting required
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).
APPEND gs_component TO gt_compontents.
How can we write component name as 'MATERIAL' and component type as MATNR when do not know component name and type. Same applies to the below component type as well. We will come to know the components at run time.
gs_component-name = 'MATTEXT'.
" create a type description for ABAP dictionary type MAKTX (material short text) - casting required
gs_component-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).
APPEND gs_component TO gt_compo, ntents.
" create structure's type definition from the component table
lo_dynamic_struct = cl_abap_structdescr=>create( gt_compontents ).
" now create memory structure to actually store the data
‎2013 Aug 11 4:18 AM
Hi Marina,
at some point in time you have to know something about the data that you actually want to store through RTTC objects.
For the element's names you can go with generic names like COLUMN1 ... COLUMNn although this somewhat contradicts the concept behind RTTC because if the data is that unspecific you could probably store everything in a STANDARD TABLE OF string or something similar.
If you want to have more general types you can simply achieve this by using the following snippet:
DATA:
gv_field_type TYPE char40,
...
gs_component-type ?= cl_abap_elemdescr=>describe_by_data( gv_field_type ).
In that instance the type is derived via RTTI (runtime type identification) from the pre-existing type you provided.
If you rather want it to be maximum generic (like you're getting a formal description of the structure that you want to build up - comparable to an XML schema) you can use this style:
gs_component-type ?= cl_abap_elemdescr=>get_c( 40 ).
This example creates a C(40) type - check out the other static methods the class cl_abap_elemdescr provides to see what's in there (get_i(), get_p(), get_string() etc.).
Regards,
Chris
‎2013 Aug 11 4:51 AM
Hi Marina,
basically there are two prerequisites that have to be met once it comes to object creation via RTTC:
If you cannot devise a name and/or a suitable type (that fits into one of the three categories above) then RTTC probably is not the way to go.
Regards,
Chris