Using DO for WML from Java

AlainChabrier
3 min readMar 6, 2020

--

Decision Optimization models can be deployed and solved in Watson Machine Learning from any programming language, including Java, through REST APIs.

The availability of Decision Optimization (DO) in Watson Machine Learning (WML) has already been commented in several previous posts, see for example this post on scaling. These posts were focusing on the generic characteristics of DO for WML and were using basic REST API or Python examples. Even if REST APIs can be used from any programming language, I got many requests to provide working examples in some of these programming languages.

In this post, I provide some guidelines and recommendations around some sample Java code to deploy and solve DO models using WML REST APIs.

You can find the Java code in the following github repository: https://github.com/IBMDecisionOptimization/DOforWMLwithJava

This code has been moved to a new repository with more robust and polished code, hence some of the links below are broken. See this post for up-to-date links.

It is built around 3 classes.

A generic connector to call REST APIs.

All the example is base on some generic ConnectorImpl class with a method to call REST APIs using the HttpURLConnection class.

This doCall method can be used to do GET, POST, DELETE or PUT REST calls. The method takes a map of HTTP headers, which will be used to pass the instance_id or the bearer token obtained from authentication. The method also takes (for PUT and POST calls) a body, which can be a JSON structure or some content.

You can see the implementation of this method in github.

A WMLconnector class

This base ConnectorImpl class implementing the REST mechanism is extended with a WMLConnectorImpl class implementing the WMLConnector interface which provides methods implementing the basic actions.

The constructor of this class will get a bearer token using IAM authentication. In a production application, the validity of the token has to be checked as there is a timeout.

public interface WMLConnector {
public void lookupBearerToken();
public String getBearerToken();
public String createNewModel(String modelName, String type, String modelAssetFilePath );
public String getModelHref(String modelId, boolean displayModel);
public String deployModel(String deployName, String modelHref, String size, int nodes);
public WMLJob createJob(String deployment_id, JSONArray input_data);
public void deleteDeployment(String deployment_id);
}

An Sample class

The repository finally includes a Sample class as an example showing how to use all these methods to run the complete lifecycle of a model: create the model, upload its formulation, deploy it, create job and monitor them, and delete the deployment.

You can see all this in this class: https://github.com/IBMDecisionOptimization/DOforWMLwithJava/blob/master/src/com/ibm/Sample.java

In order to use it, you should set the right static credentials in Sample

private static final String WML_URL = "https://us-south.ml.cloud.ibm.com";
private static final String WML_APIKEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
private static final String WML_INSTANCE_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";

Note that in this sample we also show how to use Cloud Object Storage to handle log files through external data references.

Then you also need to set the following credentials:

private static final String COS_APIKEY  = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
private static final String COS_ACCESS_KEY_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
private static final String COS_SECRET_ACCESS_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";


private static final String COS_ENDPOINT = "https://s3.eu-gb.cloud-object-storage.appdomain.cloud";
private static final String COS_BUCKET = "test-lp";

This code is provided as a contribution and example on how DO for WML can be used from Java. The code probably needs to be robustified and adapted to particular situations.

The github project is prepared for IntelliJ IDE.

The project in InteliJ

PS: I extended the sample in the github project to also show how to run LP models from Java using Cloud Object Storage (COS).

Alain.chabrier@ibm.com

@AlainChabrier

https://www.linkedin.com/in/alain-chabrier-5430656/

--

--

AlainChabrier

Former Decision Optimization Senior Technical Staff Member at IBM Opinions are my own and I do not work for any company anymore.