Executing a decision operation

You execute a decision operation that is packaged in a decision service archive by using the execution Java™™ API.

For more information about the API, see the Execution Java API reference manual in Reference.

  1. Getting a DecisionRunnerProvider instance

    Get a DecisionRunnerProvider instance that corresponds to the way the decision service archives are accessed.

  2. Getting a DecisionRunner instance

    Get a DecisionRunner instance that corresponds to the decision operation from the DecisionRunnerProvider instance.

  3. Executing a decision operation by using Java objects

    Execute the decision operation by using DecisionRunner.

Getting a DecisionRunnerProvider instance

Your Java application must have access to decision service archives for execution.

If the decision service archives are accessible at give URLs, you can use URLDecisionRunnerProvider:
URL url = ...;
DecisionRunnerProvider provider = new URLDecisionRunnerProvider
   .Builder()
   .addURL(urlToDSA1)
   .addURL(urlToDSA2)
   .build();
If the decision service archives are added to the class path of the application, you can use ClassLoaderRunnerProvider:
DecisionRunnerProvider provider = new ClassLoaderDecisionRunnerProvider
   .Builder()
   .build();
The lookup strategy for the decision service archive is to first search in the thread context class loader, then in the class loader that loads the classes of the API.
You can also specify the class loader that contains the decision service archive:
ClassLoader cl = ..; // classloader containig the DSA
DecisionRunnerProvider provider = new ClassLoaderDecisionRunnerProvider
   .Builder()
   .withClassLoader(cl)
   .build();
For an instance of DecisionRunnerProvider, the engine runtime version for all decision service archives must be the same.

Getting a DecisionRunner instance

A DecisionRunner instance is dedicated to a particular decision operation. It is the main class for executing a decision operation.

You use the DecisionRunnerProvider instance to get the DecisionRunner instance that corresponds to a given operation from the accessible decision service archives.

To obtain a DecisionRunner instance for the decision operation operationName:
String operationName = ..; // the name of an operation
DecisionRunner runner = provider.getDecisionRunner(operationName);

Executing a decision operation by using Java objects

To execute a decision operation named LoanValidation by using Java objects as input and output:
DecisionRunnerProvider provider = ...;
DecisionRunner runner = provider.getDecisionRunner("LoanValidation");
Object in = ...;
Object out = runner.execute(in);
Here is a complete example for executing a decision operation of the decision service Loan Approval in the Banking sample decision automation. Add the decision service archive loanApproval-<version>.jar to the application class path. This decision service archive is in the archives folder in the GitHub repository for samples and tutorials. For more information, see Samples and tutorials in GitHub.
import com.ibm.decision.run.DecisionRunner;
import com.ibm.decision.run.provider.ClassLoaderDecisionRunnerProvider;
import com.ibm.decision.run.provider.DecisionRunnerProvider;
import decisions.techpreview_samples.loan_validation.loanvalidationdata.Borrower;
import decisions.techpreview_samples.loan_validation.loanvalidationdata.Loan;
import decisions.techpreview_samples.loan_validation.loanvalidationdecisionmodel.Approval_decision_modelOutput;
import decisions.techpreview_samples.loan_validation.loanvalidationdecisionmodel.approval_decision_model.Input;

[..]

DecisionRunnerProvider provider = new ClassLoaderDecisionRunnerProvider.Builder().build();

DecisionRunner runner = provider.getDecisionRunner("approval");

Input in = new Input();

Borrower borrower = new Borrower();
borrower.setZipCode("12345");
borrower.setCreditScore(700L);
borrower.setFirstName("John");
borrower.setLastName("Doe");
borrower.setYearlyIncome(100000L);
in.borrower = borrower;

Loan loan = new Loan();
loan.setAmount(185000L);
loan.setNumberOfMonthlyPayments(72L);
in.loan = loan;

Approval_decision_modelOutput out = (Approval_decision_modelOutput)runner.execute(in);
Alternatively, the input can be created from a map of objects instead of a specific input Java class:
import com.ibm.decision.run.DecisionRunner;
import com.ibm.decision.run.provider.ClassLoaderDecisionRunnerProvider;
import com.ibm.decision.run.provider.DecisionRunnerProvider;
import decisions.techpreview_samples.loan_validation.loanvalidationdata.Borrower;
import decisions.techpreview_samples.loan_validation.loanvalidationdata.Loan;
import decisions.techpreview_samples.loan_validation.loanvalidationdecisionmodel.Approval_decision_modelOutput;

[..]

DecisionRunnerProvider provider = new ClassLoaderDecisionRunnerProvider.Builder().build();

DecisionRunner runner = provider.getDecisionRunner("approval");

Map<String, Object> inParams = new HashMap<String, Object>();

Borrower borrower = new Borrower();
borrower.setZipCode("12345");
borrower.setCreditScore(700L);
borrower.setFirstName("John");
borrower.setLastName("Doe");
borrower.setYearlyIncome(100000L);
inParams.put("borrower", borrower);

Loan loan = new Loan();
loan.setAmount(185000L);
loan.setNumberOfMonthlyPayments(72L);
inParams.put("loan", loan);

Approval_decision_modelOutput out = (Approval_decision_modelOutput) runner.execute(runner.createInput(inParams));

Sample

The sample Executing a decision service in Java is available in GitHub. Click Automation Decision Services samples External link opens a new window or tab to be redirected to the GitHub repository.