Machine Learning in Digital Process Automation — Part III

Ralf Mueller
Oracle Developers
Published in
7 min readApr 7, 2020

A lot has happened since the publishing of Part II of this article series. Autonomous Data Warehouse (ADW) has advanced and on the process side we’re working on a truly multi-tenant, OCI native offering. As part of this, we’re re-architecting parts of the Machine Learning capabilities for Digital Process Automation and we will talk about this in a later article. In this article though, we’d like to demonstrate how ADW and Oracle REST Data Services can be used today for the consumption of Machine Learning models in Business Processes or any other component that supports RESTful Services.

Trees. (Image © Ralf Mueller)

Use Case

For this article we’re considering a Rental Car Use Case. We pick this use case for various reasons:

  • Renting a car is well understood by many people, almost all of us went through some great or miserable experience while renting a car.
  • We have a great formal description of the case in the form of EU Rent, which was originally developed as a challenge for Business Rules implementations.
  • Quite incidentally, there are two public data sets available that we could use for Machine Learning
    - A Car Evaluation data set for the prediction of the car safety. This can be used to build classification models for the prediction of the safety of a car.
    - A Car Mileage per Gallon (MPG) data set for the prediction of the mileage per gallon of a car. This data set can be used to build regression models.
  • We can build on the use case for future articles on either Digital Process Automation or Machine Learning.

We’re not going into the details of how to build the ML models in ADW and instead refer to Part I of this series. The major steps involved to build the ML Models for this use case are

  • Upload the two data sets (they come in the form of CSV files) to OCI Object Storage.
  • Create a ML Notebook in ADW.
  • Create two tables in ADW that reflect the structure of the corresponding data sets
  • Import the data sets from Object Storage into database tables in ADW using DBMS_CLOUD.COPY_DATA procedure.
  • Build a ML classification model for the Car Evaluation and a regression model for the Car Mileage per Gallon. The concrete algorithm to use plays a minor role here but feel free to experiment with the different ML algorithms in ADW.

Exposing ML Models as RESTful Services in ADW

The interesting part of this article is how we expose the ML Models created in ADW as RESTful Services, so that we can consume those for scoring in Business Processes or other components like Integrations in Oracle Integration Cloud.

Wrapper PL/SQL procedure for ML model scoring

In order to expose the Machine Learning Models created in ADW as RESTful Services, we first have to create some wrapper PL/SQL code that takes the required input parameters, scores against the ML model with the given input and returns the result as output of the PL/SQL procedure. Below PL/SQL procedure does that for the Car Evaluation ML model. One can add a %script paragraph to create this procedure from within the ML notebook itself.

score_car_evaluation PL/SQL procedure

In this example, the ML model for the Car Evaluation is named CAR_EVALUATION_CLASSIFICATION and we’re using SQL functions PREDICTION and PREDICTION_PROBABILITY to score against the ML classification model and return the classification and its probability as results of the function.

REST enablement

The next step is to REST enable the function score_car_evaluation in Autonomous Data Warehouse. For this we login to ADW and open the Service Console.

Autonomous Data Warehouse Instance

From the Service Console, choose Development and then Oracle APEX.

Autonomous Data Warehouse Service Console

Login to Oracle APEX workspace

Oracle Application Express Login screen

Then choose SQL Workshop and open RESTful Services. Oracle APEX provides a rich set of functionality for the development of entire Applications. See Oracle Applications Express Release 19.2 for detailed information. In this article we’re focusing on the ORDS RESTful Services part. See Developing Oracle REST Data Services Applications for the details about ORDS.

Oracle APEX
SQL Workshop RESTful Services in Oracle APEX

Expand Modules and create a new Module for the ML Services. In below example, a module “car” with URI prefix /car/ is created.

ORDS RESTful Modules
ORDS Module Definition

In the next step we’re going to create a Template Definition for the Car Evaluation together with a handler for the REST operations

ORDS Template Definition
ORDS Handler Definition

Please note the Source of the POST Handler definition. Here we’re using our earlier created PL/SQL procedure score_car_evaluation. The input parameters are specified as binding parameters and the output of the PL/SQL procedure must be mapped in the Parameters section of the handler. In this case, we need to map two output parameters

  • prediction : STRING, the prediction as a result of scoring against ML model CAR_CLASSIFICATION_MODEL
  • probability : DOUBLE, the probability of the prediction

This completes the creation of a RESTful Service for the Machine Learning model CAR_EVALUATION_CLASSIFICATION. It is an easy exercise to do this for the regression model as well.

RESTful Service Security

Before we make this newly created REST Service public for consumption, we probably want to add some security for it. Oracle ORDS provides a rich set of security features, see Configuring Secure Access to RESTful Services.

In our example we’re going to add Basic Auth security. For this, open the Privileges menu from the left side

RESTful Services Privileges

and create a new Privilege for our Module as following

ORDS Privileges Definition

Make sure to specify the correct Pattern to secure, in our example this is /oml/*. For Roles, we selected SQL Developer from the list of Roles, see Oracle REST Data Service User Roles for other options.

Bonus Track

In the last section we demonstrated how Oracle APEX and the Oracle REST Data Services can be used to REST enable an ADW Machine Learning Model. While Oracle APEX offers a very nice User Interface and IDE for the development of Applications, as a Data Scientist, we really don’t want to leave the ML notebook experience. So the question is if we can achieve the same result, namely the exposure of the ML models as REST Services, from within the ML notebook itself, where we created the ML models in the first place.

Well, the answer is YES!

ORDS offers a rich set of PL/SQL API’s that allow the same result in a programmatic way. See Oracle REST Data Services PL/SQL Package Reference and Oracle REST Data Services Administration PL/SQL Package Reference for detailed information about the available ORDS PL/SQL packages.

So all the work we did in the last section can actually be achieved by the following PL/SQL code

Enable ML model as REST Service using ORDS PL/SQL packages

which can then be conveniently added to your ML notebook in ADW as a script

Script paragraph in ML notebook

Swagger Document Generation

A variety of development tools support the Swagger standard today. For those, a corresponding Swagger document can be created from the ORDS Module Definition page (see above). Simply click on “Generate Swagger Doc” and a Swagger JSON will be created that can be saved to a file and used in your development tool of choice. Below Swagger document represents the ORDS REST Service we created in this article

Swagger Document for car Module

Rental Car Digital Process Automation

Now that we have the ML Models exposed as REST Services, it is an easy exercise to consume those for Digital Process Automation. Below Dynamic Process diagram shows a very simplified version of the Rental Car use case

Rental Car Process

The activities Car Acceptance classification and Car Mileage per Gallon prediction use REST Connectors to call into the ORDS REST Services for the corresponding ML Models in ADW. For more information on how to create REST connectivity for consumption in Processes see Creating REST and Web Service Connectors.

Conclusion

We have demonstrated on how to REST enable Machine Learning Models build in ML notebooks of ADW using Oracle REST Data Services. We further showed how the REST enablement can be achieved programmatically using ORDS PL/SQL packages. This is especially convenient for the Data Scientist since she/he doesn’t need to leave the ML notebook environment and can simply add a paragraph in the notebook that takes care of the REST enablement.

We’d like to thank Kris Rice (krisrice 🏒🍺🚴🏻) from the Oracle Database Tools Development Team for valuable input regarding the programmatic creation of the RESTful Services using ORDS PL/SQL packages.

--

--

Ralf Mueller
Oracle Developers

Software guy, photography enthusiast. I work for Oracle Corporation, opinions expressed here are my own.