01-25-2007 11:51 PM
Hello colleagues,
I want to create a recursive function module to lookup through a hierarchy (similar to BOM explosion, but not exactly the same concept -it is actually BOM flattening).
My concern about this approach is about stack overflow. How does ABAP workbench handles stack? is it possible to prevent stack overflow condition?
Let's say my function needs to recursively call itself several times, maybe hundreds of times (because hierarchy has several levels and Func.Mod. would be calling itself once per each level in the hierarchy). In this scenario, a stack overflow might occur is too many levels in the hierarchy...
I appreciate your inputs.
Thank you in advance.
Mario
01-26-2007 1:31 AM
I just wrote a function module that calculates the sum of integer numbers from 1 to N recursively and was able to call it with values upto 60000 without running out of stack space.
With larger values of N it dumps but not because of stack overflow but because the result does not fit in a int4.
So it seems that you would be ok.
However, if you devise an non-recursive equivalent it will most likely perform better and use less memory.
Regards... Lucio
01-26-2007 2:49 AM
Hi Mario,
Please use this recursive FM which i have created for the same requirement
FUNCTION ZBOM_GET_HIERARCHY.
*"----------------------------------------------------------------------
*"*"Global interface:
*" TABLES
*" BOM_RETURN STRUCTURE ZMBOMDATA
*"----------------------------------------------------------------------
CLEAR HIER_TAB.
REFRESH HIER_TAB.
*Put contents of table sent to function(bom_return) into hier_tab
HIER_TAB[] = BOM_RETURN[].
*Clear out table to be returned from function, it will be built in
*hierarchy order
CLEAR BOM_RETURN.
REFRESH BOM_RETURN.
LEVEL = 1.
*Start at the top_level assembly
LOOP AT HIER_TAB
WHERE TOP_LEVEL = 'X'.
*Move fields to table to be returned
MOVE-CORRESPONDING HIER_TAB TO BOM_RETURN.
BOM_RETURN-BOM_LEVEL = LEVEL.
APPEND BOM_RETURN.
*Do not check any other type of item that is not stock
IF HIER_TAB-POSTP NE 'L'.
CONTINUE.
ENDIF.
*Check to see if the component is also a parent part
PERFORM GET_NEXT USING LEVEL
HIER_TAB-IDNRK.
ENDLOOP.
ENDFUNCTION.
*&---------------------------------------------------------------------*
*& Form GET_NEXT
*&---------------------------------------------------------------------*
*Checks to see if the component part is a parent part, if it is move *
*the fields to the table to be returned from the function
*----------------------------------------------------------------------*
FORM GET_NEXT USING VALUE(LOCAL_LEVEL) LIKE LEVEL "BOM level
VALUE(LOCAL_IDNRK) LIKE HIER_TAB-IDNRK. "Component
*Check to see if the component is a parent
READ TABLE HIER_TAB
WITH KEY MATNR = LOCAL_IDNRK.
*If it is, increment the bom level and move the fields to the table to
*be returned from the function
IF SY-SUBRC = 0.
LOCAL_LEVEL = LOCAL_LEVEL + 1.
LOOP AT HIER_TAB
WHERE MATNR = LOCAL_IDNRK.
MOVE-CORRESPONDING HIER_TAB TO BOM_RETURN.
BOM_RETURN-BOM_LEVEL = LOCAL_LEVEL.
APPEND BOM_RETURN.
*Do not check any other type of item than stock
IF HIER_TAB-POSTP NE 'L'.
CONTINUE.
ENDIF.
*Check to see if the component part is a also a parent part
PERFORM GET_NEXT USING LOCAL_LEVEL
BOM_RETURN-IDNRK.
ENDLOOP.
ENDIF.
ENDFORM. " GET_NEXT
This code is working in production without any error.