REST API sample details

This sample demonstrates the usage of the REST API in Java™ in various scenarios. To run the sample, you use the Samples Console.

Running this sample

First, open the Samples Console as explained in Setting up the REST API sample.
Important: You must run the scenarios in this order, otherwise they do not execute.
  1. The RuleApp scenario lists, adds, and modifies RuleApps.
  2. The XOM scenario lists, adds, and modifies execution object models (XOM).
  3. The ruleset scenario lists, adds, and modifies rulesets.
  4. The execution scenario takes the parameters of a ruleset in XML format and executes the ruleset.
  5. Execution with the JSON request scenario takes the parameters of a ruleset in the JSON object format and executes the ruleset.
  6. The clean scenario removes all the new artifacts.
The output trace is formatted as follows:
  1. ** Announces the action.
  2. >> URL: Reports the requested URL.
  3. << RESPONSE - Status code: Reports the returned status.
  4. The JSON response

RuleApp scenario

This scenario lists, adds, and modifies RuleApps.
  1. In the Samples Commands view, double-click the add.ruleapp command.
    The command compiles the code and processes as follows:
    1. Checks whether the scenario can run.
    2. Lists all the RuleApps.
    3. Adds a new RuleApp.
    4. Lists all the RuleApps again to show the new one.
    5. Modifies the RuleApp.
    6. Shows the modified RuleApp.

    You can see the results in the Rule Execution Server console.

  2. Open a web browser and type the following URL to start the Rule Execution Server console: http://localhost:<PORT>/res
  3. Logon to the Rule Execution Server console.
    • User name: resAdmin
    • Password: resAdmin
  4. Click the Explorer tab. In the RuleApps View, you can see the added RuleApp myRuleApp.
  5. Click the new RuleApp to see its description: My REST modified RuleApp. It contains no ruleset.

XOM scenario

This scenario lists, adds, and modifies execution object models (XOM).
  1. In the Samples Commands view, double-click the add.xom command.
    The command compiles the code and processes as follows:
    • Checks whether the scenario can run.
    • Lists all the execution object models (XOM).
    • Adds a new XOM.
    • Lists all the XOMs again to show the new one.

    You can see the results in the Rule Execution Server console.

  2. Open a web browser and type the following URL to start the Rule Execution Server console: http://localhost:<PORT>/res
  3. Logon to the Rule Execution Server console.
    • User name: resAdmin
    • Password: resAdmin
  4. Click the Explorer tab. In the Resources View, you see the added resource myxom.zip.
  5. Click the new resource to see its properties. You can download this resource. No ruleset references it.

Ruleset scenario

This scenario lists, adds, and modifies rulesets.
  1. In the Samples Commands view, double-click the add.ruleset command.
    The command compiles the code and processes as follows:
    • Checks whether the scenario can run.
    • Adds a new ruleset to the previously added RuleApp.
    • Shows the created ruleset.
    • Modifies the ruleset description.
    • Adds a property to the XOM URI.
    • Shows the modified ruleset.

    You can see the results in the Rule Execution Server console.

  2. Open a web browser and type the following URL to start the Rule Execution Server console: http://localhost:<PORT>/res
  3. Logon to the Rule Execution Server console.
    • User name: resAdmin
    • Password: resAdmin
  4. Click the Explorer tab. In the RuleApps View, you can see that myRuleApp now contains one ruleset.
  5. Click myRuleApp and then click myRuleset to open its Ruleset View. You can see its description: My REST ruleset
  6. Scroll down to the Ruleset Parameters part of the window. Next to Show Monitoring Options, you can see that execution tracing is enabled in Decision Warehouse.
  7. Optionally, click Show Monitoring Options to view the tracing options.
  8. Open the Managed URIs section. You can see one managed URI.
  9. Click the URI. You can see that the myxom.zip resource is now referenced by one ruleset.

Execution scenario

This scenario executes rulesets by using the provided parameters.
  1. In the Samples Commands view, double-click the execute command.
    The command compiles the code and processes as follows:
    1. Checks whether the scenario can run.
    2. Passes the ruleset parameters in XML format and executes the ruleset to apply for a loan for an amount of 5000.
    3. Shows the XML response: in the output parameter loan, the approved field is set to true.
  2. In the Samples Commands view, double-click the executeNotApproved command to execute the ruleset. The amount of the requested loan is 500000. This time, the loan is not approved.

    You can see the execution trace in the Rule Execution Server console because the execution trace is enabled on the ruleset, as indicated in Ruleset scenario.

  3. Open a web browser and type the following URL to start the Rule Execution Server console: http://localhost:<PORT>/res
  4. Logon to the Rule Execution Server console.
    • User name: resAdmin
    • Password: resAdmin
  5. Click the Decision Warehouse tab, and then click Search Decisions to search for the latest decisions. You can see two decisions with idApproved and idNotApproved. Decision idNotApproved rejects the loan as the result of the execution of one rule.

Execution with the JSON request scenario

This scenario executes rulesets by using the JSON object to send the request parameters.

In the Samples Commands view, double-click the execute.json command. The command compiles the code and processes it as follows:
  • Checks whether the scenario can run.
  • Passes the ruleset parameters in JSONObject format and executes the ruleset to apply for a loan for an amount of 5,000.
  • Shows the JSON response. In the output parameter loan, the approved field is set to true.
In the Samples Commands view, double-click the executeNotApproved.json command to execute the ruleset. The amount of the requested loan is 500,000. This time, the loan is not approved. The reject message is also displayed.

You can check the execution trace in the Rule Execution Server console. The steps are similar to the steps in the Execution scenario.

Clean scenario

This scenario removes all the new artifacts.
  1. In the Samples Commands view, double-click the clean command.
    The command compiles the code and processes as follows:
    • Checks whether the scenario can run.
    • Removes the myRuleApp application.
    • Lists all the RuleApps to show that myRuleApp does not exist any more.
    • Lists all the resources to show that myxom.zip is not referenced any more.
    • Removes the myxomp.zip resource.

    You can see the results in the Rule Execution Server console.

  2. Open a web browser and type the following URL to start the Rule Execution Server console: http://localhost:<PORT>/res
  3. Logon to the Rule Execution Server console.
    • User name: resAdmin
    • Password: resAdmin
  4. Click the Explorer tab. The RuleApps View does not list myRuleApp and the Resources View does not list myxom.zip any more.

Rebuilding this sample

To rebuild this sample:
  1. Make sure that you are in the Samples Console perspective.
  2. In the Samples Commands view, double-click the build command to build the classes.

Source files

You can find this sample in <InstallDir>/executionserver/samples/restapi. The JavaClient.java file is the class that calls the REST API.
  • The constructor creates the HttpClient instance and sets the preemptive authentication. Thus, all further requests use the apiauth URL where the credentials are already given.
  • The class defines one runXXScenario method for each scenario. That method first calls a canRunXXScenario method to check whether the scenario executes.
  • The canRunXXScenario method executes a GET request that returns a JSON result to check whether the artifact to be added is already there.
  • The runXXscenario method makes several requests of type GET, POST, UPDATE, or DELETE according to the scenario.
  • The getXOMURI method makes a GET request that returns a JSON result to access the URI from the server.

You can also view and modify the sample source files in Rule Designer. To import the sample into your workspace, click Import projects in the Samples and Tutorials view and switch to the Java perspective.