Invoking a Decision Service from a different Decision Service
You can use an extended operator or service call out to invoke a decision service that has its own vocabulary from within another decision service. The called decision service does not need to be on the same server. It can be executed in-process as shown below.
The following code example is an extended operator that uses in-process execution. You need to construct and pass in an XML SOAP message as the input. You also need to parse the response to extract the values of interest and any messages.
public class Corticon implements ICcStandAloneExtension{
/** * callCorticon makes an in-process call to the specified decision service * The decisionServicePath points to a Ruleflow file * The XML request conforms to the WSDL for the decision service * The return value is the XML response from the decision service * @param decisionServiceName * @param decisionServicePath * @param XMLRequest * @return */
3. Prepare the HTTP POST using Apache HTTP librariess
4. Display the request JSON (for debugging)
5. Invoke Corticon
6. Display the response JSON (for debugging)
7. Extract the results
8. Display debugging information
public class restInvocationTest { public restInvocationTest() { } public static void main(String[] args) { try { executeREST(); } catch (Exception e) { System.out.println(e.getMessage()); } } private static void executeREST() throws Exception { String ccServerURL = "corticon-demo.dyndns.org:8860/.../execute";
// 1. CREATE PAYLOAD: //SQL to get values JSONObject applicantJSON = new JSONObject(); // create a JSON object for the Applicant entity applicantJSON.put("skydiver", "false"); // set its isSkydiver attribute applicantJSON.put("age", "25"); // set its age attribute JSONObject metadataJSON = new JSONObject(); // create a JSON object for metadata for Person metadataJSON.put("#type", "Applicant"); // set its #type attribute to "Applicant" applicantJSON.put("__metadata", metadataJSON); // add the metadata object to the applicant object JSONArray ccObjects = new JSONArray(); // create an array of JSON objects ccObjects.put(applicantJSON); // add the applicant to it JSONObject ccRequestObj = new JSONObject(); // create a JSON request object ccRequestObj.put("Objects", ccObjects); // set its objects attribute to the array of ccObjects
// 3. PREPARE THE HTTP PPOST Using Apache HTTP libs HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(ccServerURL); post.setHeader("dsname", "Skydiver"); // might also set version or effective date post.setEntity(ccRequestEntity);
// 4. DISPLAY THE REQUEST JSON before invoking Corticon (only needed for debugging) // [this is to display the request on the console] BufferedReader rd = new BufferedReader (new InputStreamReader(post.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) {result.append(line);} String postJSONString = result.toString(); System.out.println("\nccRequestObj.toString() :\n"+ccRequestObj.toString()); // Payload
// 6. DISPLAY THE RESPONSE JSON (only needed for debugging) rd = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); result = new StringBuffer(); line = ""; while ((line = rd.readLine()) != null) {result.append(line);} String responseJSONString = result.toString();
// 7. EXTRACT THE RESULTS: riskRating value from Corticon response JSON JSONTokener tokener = new JSONTokener(responseJSONString); JSONObject respJSON = new JSONObject(tokener); JSONArray objects = respJSON.getJSONArray("Objects"); JSONObject messages = respJSON.getJSONObject("Messages");
// Get object values String riskRating = objects.getJSONObject(0).getString("riskRating"); String age = objects.getJSONObject(0).getString("age"); String isSkydiver = objects.getJSONObject(0).getString("skydiver");
// Display results System.out.println("\nRESULTS -----------------------------"); System.out.println("\nRisk: " + riskRating + " for age = " + age + " and skydiver = " + isSkydiver); System.out.println("Message: " + message); System.out.println("Severity: " + severity); System.out.println("Entity Reference: " + entityRef);
// 8. DEBUGGING (only needed for debugging) System.out.println("\nDEBUGGING ---------------------------"); System.out.println("\nccRequestObj.toString():\n"+ccRequestObj.toString()); // Payload System.out.println("\nccRequestEntity.toString():\n"+ ccRequestEntity.toString()); // Request entity System.out.println("\nRequestJSONString:\n" + postJSONString); // Request JSON System.out.println("\nResponse:\n" + response.toString()); // Shows just a response summary NOT the response JSON System.out.println("\nResponseJSONString:\n" + responseJSONString); // Response
You could pass multiple Person objects in a single payload but then you need to use the entityReference to ensure that the correct message is matched up with the corresponding Person. Also if the rules return multiple messages for a single Person then you will need to iterate over the messages to extract all of them.