Call Corticon DS from another DS

Posted by archana.gupta on 20-Jun-2016 09:26

Hi,

Please suggest if there is any way to call one decision service from another decision service deployed on same corticon server.

Thanks,

Archana 

Posted by mparish on 20-Jun-2016 11:20

Including a rule flow in another one only works if they both use the same vocabulary.
If you want to invoke one decision service that has a different vocabulary from within another decision service then an extended operator or service call out is the way to go.
The called decision service does not need to be on the same server. It can be executed in-process as shown below.
 
Here’s a simple generic example as an extended operator that uses in-process execution.
You will need to construct and pass in an XML SOAP message as the input.
You will also need to parse the response to extract the values of interest and any messages
Using JSON and REST would probably be simpler
public class Corticon implements ICcStandAloneExtension{
/**
* callCorticon makes an in-process call to the specified decision service
* The decisionServicePath should point to an erf file
* The XML request should conform 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
*/
    public static String callCorticon(String decisionServiceName, String decisionServicePath, String XMLRequest, Boolean enableUpdate)
          {int minPoolSize = 1;
              int maxPoolSize = 1;
//            boolean enableUpdate = true;
              String msgStyle = ICcServer.XML_HIER_STYLE;
           try {    
                   ICcServer iServer = CcServerFactory.getCcServer();
                   if(!iServer.isDecisionServiceDeployed(decisionServiceName)){
                                  iServer.addDecisionService(decisionServiceName, decisionServicePath, enableUpdate, minPoolSize, maxPoolSize, msgStyle);
                           }
                   return ( "Corticon Result = " + iServer.execute(XMLRequest));
                   }
           catch (Exception e) {return "Corticon call to " + decisionServiceName + " failed" + e;} //End of try call corticon
         } // End of method callCorticon
 
 
In order to make a REST call to a decision service that is already deployed on a server you could wrap the following in an extended operator or service call out.
You will still need to pass the input values to the decision service and you will still need to get the results and messages back.
 
/* Summary of steps
* 1. CREATE PAYLOAD:
* 2. CREATE HTTP REQUEST
* 3. PREPARE THE HTTP PPOST Using Apache HTTP libs
* 4. DISPLAY THE REQUEST JSON (only needed for debugging)
* 5. INVOKE CORTICON
* 6. DISPLAY THE RESPONSE JSON  (only needed for debugging)
* 7. EXTRACT THE RESULTS
* 8. DISPLAY DEBUGGING INFO  (only needed for debugging)
*/
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
             
 
           // 2. CREATE HTTP REQUEST
              HttpEntity ccRequestEntity = EntityBuilder.create().setContentType(ContentType.APPLICATION_JSON).setText(ccRequestObj.toString()).build();
 
              // 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
             
              // 5. INVOKE CORTICON
              HttpResponse response = client.execute(post);
             
              // 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 the 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");
              // Get message values
              String message             = messages.getJSONArray("Message").getJSONObject(0).getString("text");
              String severity      = messages.getJSONArray("Message").getJSONObject(0).getString("severity");
              String entityRef     = messages.getJSONArray("Message").getJSONObject(0).getString("entityReference");
              // 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
             
              //9. NOTES
              /*
              You can pass multiple Person objects in a single payload but then you will then 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
              */
             
             
       }
}

All Replies

Posted by James Arsenault on 20-Jun-2016 09:43

Archana,

Technically it's possible with a custom service call out as you would for calling any other REST service but it's not typically done. A better approach might be to just include one ruleflow in another. In 5.5 we introduced the ability to include one ruleflow in another.

Posted by mparish on 20-Jun-2016 11:20

Including a rule flow in another one only works if they both use the same vocabulary.
If you want to invoke one decision service that has a different vocabulary from within another decision service then an extended operator or service call out is the way to go.
The called decision service does not need to be on the same server. It can be executed in-process as shown below.
 
Here’s a simple generic example as an extended operator that uses in-process execution.
You will need to construct and pass in an XML SOAP message as the input.
You will also need to parse the response to extract the values of interest and any messages
Using JSON and REST would probably be simpler
public class Corticon implements ICcStandAloneExtension{
/**
* callCorticon makes an in-process call to the specified decision service
* The decisionServicePath should point to an erf file
* The XML request should conform 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
*/
    public static String callCorticon(String decisionServiceName, String decisionServicePath, String XMLRequest, Boolean enableUpdate)
          {int minPoolSize = 1;
              int maxPoolSize = 1;
//            boolean enableUpdate = true;
              String msgStyle = ICcServer.XML_HIER_STYLE;
           try {    
                   ICcServer iServer = CcServerFactory.getCcServer();
                   if(!iServer.isDecisionServiceDeployed(decisionServiceName)){
                                  iServer.addDecisionService(decisionServiceName, decisionServicePath, enableUpdate, minPoolSize, maxPoolSize, msgStyle);
                           }
                   return ( "Corticon Result = " + iServer.execute(XMLRequest));
                   }
           catch (Exception e) {return "Corticon call to " + decisionServiceName + " failed" + e;} //End of try call corticon
         } // End of method callCorticon
 
 
In order to make a REST call to a decision service that is already deployed on a server you could wrap the following in an extended operator or service call out.
You will still need to pass the input values to the decision service and you will still need to get the results and messages back.
 
/* Summary of steps
* 1. CREATE PAYLOAD:
* 2. CREATE HTTP REQUEST
* 3. PREPARE THE HTTP PPOST Using Apache HTTP libs
* 4. DISPLAY THE REQUEST JSON (only needed for debugging)
* 5. INVOKE CORTICON
* 6. DISPLAY THE RESPONSE JSON  (only needed for debugging)
* 7. EXTRACT THE RESULTS
* 8. DISPLAY DEBUGGING INFO  (only needed for debugging)
*/
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
             
 
           // 2. CREATE HTTP REQUEST
              HttpEntity ccRequestEntity = EntityBuilder.create().setContentType(ContentType.APPLICATION_JSON).setText(ccRequestObj.toString()).build();
 
              // 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
             
              // 5. INVOKE CORTICON
              HttpResponse response = client.execute(post);
             
              // 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 the 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");
              // Get message values
              String message             = messages.getJSONArray("Message").getJSONObject(0).getString("text");
              String severity      = messages.getJSONArray("Message").getJSONObject(0).getString("severity");
              String entityRef     = messages.getJSONArray("Message").getJSONObject(0).getString("entityReference");
              // 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
             
              //9. NOTES
              /*
              You can pass multiple Person objects in a single payload but then you will then 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
              */
             
             
       }
}

Posted by archana.gupta on 27-Jun-2016 06:05

Using this help I am now able to call one decision service from another DS using SOAP method. Thank you mparish....

Thanks,

Archana

This thread is closed