REST API を使用したルール・セットの呼び出し

クライアント・アプリケーションは、REST API を介して意思決定サービスのルール・セットを呼び出します。 標準HTTP GETPOSTPUTDELETE コマンドを使用します。

始める前に

クライアント・アプリケーションは、クラウド・ポータルの認証資格情報を渡す必要があります ( クライアント・アプリケーションのサービス資格情報を参照)

このタスクについて

クライアント・アプリケーションは、実行サーバーと接続し、REST API を介してルール・セットを呼び出すことによって、意思決定サービスのルール・セットを呼び出します。
サーバーのルール・セットのエンドポイント URI は次のようになります。
https://<vhostname>.bpm.ibmcloud.com/odm/<odm_on_cloud_environment>/DecisionService/rest/v1/<ruleset_path>

以下のリストでは、URI の部分を説明します。

  • <vhostname>: 意思決定サービスのホストの名前となります。
  • <odm_on_cloud_environment>: 意思決定サービスのルール・セットが開発環境、テスト環境、または実稼働環境のいずれにあるかに応じて、devtest、または prod を使用します。
  • <ruleset_path>: 意思決定サービスのルールを実行するためのパスを提供し、以下のいずれかの形式を取る必要があります。
    • ruleAppName/rulesetName
    • ruleAppName/ruleAppVersion/rulesetName
    • ruleAppName/ruleAppVersion/rulesetName/rulesetVersion
以下の例は、MiniloanServiceRuleset の URI を示しています。
https://vhost005.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRuleset
ルールセットの実行は、指定されたエンドポイントURIの HTTP POSTを通じて実行される。 認証のユーザー名とパスワードは、許可ヘッダーとともに渡されます ( REST および SOAP 呼び出しの認証を参照してください)。

以下の例は、サーバー上で MiniloanServiceRuleset を呼び出す Java™ コードを示しています。 この例では、Apache HTTPClient v4.3 を使用しています。
import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DecisionServiceExecution {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		
		// Replace <vhostname> with the name of the host of the cloud portal
		String endpointURI = "https://<vhostname>.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";

		// Replace "loginID" with the functional ID of the service account you are using to authenticate with your tenant in the Cloud
		String userName = "loginID";
		
		// Replace "password" with the password of the service account you are using to authenticate with your tenant in the Cloud
		String password = "password";

		String credentials = userName + ":" + password;
		String encodedValue = Base64.encodeBase64String(credentials.getBytes());
		String authorization = "Basic " + new String(encodedValue);

		String contentType = "application/json";

		// Set the borrower and the loan
		String payload = "{\"borrower\": {" +
				"\"name\": \"John\"," +
				"\"creditScore\": 600," +
				"\"yearlyIncome\": 80000" +
				"}," +
				"\"loan\": {" +
				"\"amount\": 500000," +
				"\"duration\": 240," +
				"\"yearlyInterestRate\": 0.05" +
				"}" +
				"}";
		
		CloseableHttpClient client = HttpClients.createDefault();
		
		try {
			HttpPost httpPost = new HttpPost(endpointURI);
			// Add the basic authentication header
			httpPost.addHeader("Authorization", authorization);
			httpPost.setEntity(new StringEntity(payload));
			httpPost.addHeader("Content-Type", contentType);
			CloseableHttpResponse response = client.execute(httpPost);
			try {
				if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
					System.err.println("Status Code: " + response.getStatusLine().getStatusCode());
					System.err.println("Status Line: " + response.getStatusLine());
					String responseEntity = EntityUtils.toString(response.getEntity());
					System.err.println("Response Entity: " + responseEntity);

					throw new RuntimeException(
							"An error occurred when invoking Decision Service at: "
									+ endpointURI
									+ "\n"
									+ response.getStatusLine() + ": " + responseEntity);
				} else {
					String result = EntityUtils.toString(response.getEntity());
					System.out.println("Result: " + result);
				}
			} finally {
				if (response != null) {
					response.close();
				}
			}
		} finally {
			client.close();
		}
	}
	
}