Wednesday, 30 June 2021

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.

SAP HANA Exam Prep, SAP HANA Preparation, SAP HANA Tutorial and Materials, SAP HANA Career, SAP HANA Guides
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.

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.

SAP HANA Exam Prep, SAP HANA Preparation, SAP HANA Tutorial and Materials, SAP HANA Career, SAP HANA Guides
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
      ).

No comments:

Post a Comment