Skip to Content
Technical Articles
Author's profile photo Naveen Kumar Chikkanna

Using the graph engine in SAP HANA for Master Data Substitution

What is Master Data Substitution?

Master Data Substitution is a process when requested master data entity can be substituted by another one. Master Data substitution is very generic framework which supports different master data entities and provides various features like Exclusion, Grouping, Context Objects, Validities etc.

Example: Product Substitution. In Sales order, if Customer is requesting for Product A, but this product A is not available due to out of stock or for any other reason we will not be fulfilling customer demands. But Product B is available which is having the similar specifications as product A, then Product B can be substituted and fulfill customer demands. Product B can further be substituted with Product C and Product C can be substituted with Product D.

In Master Data Substitution, following default features are supported.

Master%20Data%20Substitution%20Features

Master Data Substitution Features

 

Substitutions and Edges

In the above example we have seen how one product can be substituted with a chain and this makes it necessary to use directed graph with several nodes. The objects/Substitutes are nodes/vertex in the graph, the substitutions (connection between Object and Substitutes) are edges in the graph. We are interested in the substitutions, not in the master data objects itself.

 

SAP Graph Engine

SAP HANA Graph is an integral part of the SAP HANA core functionality. For more information: please check this blog post.

Graph is a set of vertices and a set of edges. Each edge connects two vertices; one vertex is denoted as the source and the other as the target. Edges are always directed and there can be two or more edges connecting the same two vertices.

Directed%20Graph%20with%20Nodes%20and%20Edge

Directed Graph with Nodes and Edge

How to consume SAP Graph Engine Capabilities in ABAP?

It is on similar lines with AMDP. But we are using Language Graph Script and Defining Graph Work Space, Edge Table and Vertex Table is a must.

Class Definition.

CLASS ycl_md_substn_graph_api DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES: if_amdp_marker_hdb.

    TYPES: BEGIN OF ty_output,
             nodes TYPE zmd_substn_object,
           END OF ty_output,
           tt_output TYPE STANDARD TABLE OF ty_output.

    CLASS-METHODS set_graph_work_space FOR DDL OBJECT OPTIONS CDS SESSION CLIENT REQUIRED.

    CLASS-METHODS get_substitutes AMDP OPTIONS CDS SESSION CLIENT current
      IMPORTING
        VALUE(object)      TYPE zmd_substn_object
      EXPORTING
        VALUE(output_data) TYPE tt_output.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

 

Class Implementation

CLASS ycl_md_substn_graph_api IMPLEMENTATION.

  METHOD set_graph_work_space BY DATABASE GRAPH WORKSPACE FOR HDB LANGUAGE SQL USING
  ytest_graph_edges ytest_graph_nodes.

    edge table ytest_graph_edges
    source column mdobject
    target column mdsubstitute
    key column mdsubstnuuid

    vertex table ytest_graph_nodes
    key column node

  endmethod.

  METHOD get_substitutes BY DATABASE PROCEDURE FOR HDB LANGUAGE
  GRAPH OPTIONS READ-ONLY USING ycl_md_substn_graph_api=>set_graph_work_space.

    graph load_graph = graph( "YCL_MD_SUBSTN_GRAPH_API=>SET_GRAPH_WORK_SPACE" );

    Vertex start_node = vertex ( :load_graph, :object );

    BigInt row_counter = 1L;

     Traverse BFS :load_graph from :start_node  on visit edge ( edge current_edge )
     {

      output_data."NODES"[ :row_counter ] = :current_edge.MDSubstitute;
      row_counter = :row_counter + 1L;

     } ;

  ENDMETHOD.

ENDCLASS.

Method for GRAPH WORK SPACE is a must which exposes the data to the Graph Engine. In this method we need to mention the Edge Table and Vertex Table. For Edge Table, we need to mention the Source Column, Target Column and Key Column and for Vertex Table, we need to mention the Key Column.

In this example, method set_graph_work_space is used for defining the Graph Work Space. For Edge table and Vertex table, we are using CDS ytest_graph_edges and ytest_graph_nodes respectively.

Method get_substitutes is used for finding out the substitutes for the input value. We are using BFS(Breadth First Search) for traversing and finding substitutes for the given input value.

This method get_substitutes can be called in ABAP program like any other method in ABAP class.

Report:

REPORT ytest_subs_read_api.

TYPES: BEGIN OF ty_output,
             nodes TYPE zmd_substn_object,
       END OF ty_output,
       tt_output TYPE STANDARD TABLE OF ty_output.

DATA: output_tab type tt_output.

      ycl_md_substn_graph_api=>get_substitutes(
        EXPORTING
          object      = 'A'
        IMPORTING
          output_data = output_tab
      ).

Summary:

  • Master Data substitution is a generic framework and supports 1:1, 1:N substitutions. Every Edge has attributes likes Status, Exit Indicator, Lead Indicator, Sequences in master data substitution.
  • To consume SAP HANA Graph Engine capabilities, Edge Table, Vertex Table is a must. In the class, interface IF_AMDP_MARKER_HDB and Method for Graph Workspace is a must.
  • Breadth-first search(BFS) built-in functionality is used for traversing from the starting node within the graph and find the substitutes.

Reference:

SAP HANA Graph Reference.

Hope you like this blog post, in case of any questions please post them here. Your feedbacks are welcome in the comments section below.

Please visit the SAP S/4HANA Community page for other SAP S/4HANA topics.

Happy coding!

Assigned tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Mathias Kemeter
      Mathias Kemeter

      Nice use case! Many thanks for the write-up!