Write Java Client code to deploy and access the Decision Service

To deploy and access a Decision Service from a Java client, you write Java code that uses Corticon APIs.

  1. In the App project, choose New > Package. Name the package program.
  2. Click on the program package, and then choose New > Class. Name the class InvokeDS.
  3. The editor opens for InvokeDS.java.
  4. Let’s begin writing the Java code:
    In your client program, begin by importing the required packages and classes as shown.
    package program;
    
    import com.corticon.eclipse.server.core.CcServerFactory;
    import com.corticon.eclipse.server.core.ICcServer;
    import com.corticon.service.ccserver.*;
    import cargoLibrary.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Iterator;
    import java.util.Properties;
  5. Next, create a String variable to hold the Decision Service name. Note that the Decision Service name you provide overrides the name you may have specified earlier, while compiling the Ruleflow.
    public class InvokeDS {
    
      private static String decisionServiceName = "Cargo";
    
      public void callDS(){
      }
    }
  6. Now, write code to create an object of the entity class. In this step, you create an object of the entity class and populate it with data that you want to test against the Decision Service. In our example, we will set an input volume of 10 and weight of 1000. You can try various other input combinations, based on the attributes defined in the rules.
    try {
         Cargo cargo = new Cargo();
         cargo.setWeight(1000);
         cargo.setVolume(10);
  7. Write code to create an arraylist and add the object to the arraylist.The method to send a request message requires a collection as one of the parameters. So, you must create a collection, such as an arraylist, and add the entity object to it.
  8. In this example, the weight and volume values for Cargo , defined in the Cargo request object, will be processed and the Cargo’s container variable will be determined by the Decision Service.
    List<Cargo> cargoList = new ArrayList<>();
    cargoList.add(cargo);
  9. Write code to initialize Corticon Server.To do this, you use the ICcServer interface. ICcServer is implemented by the CcServerFactory class.To instantiate Corticon Server you use a method in CcServerFactory named getCcServer() to return an object of the type ICcServer as shown in the example. In the example, the object is assigned to the variable ccServer.
    ICcServer server = CcServerFactory.getCcServer();
  10. Once you have obtained an object of the type ICcServer, you use it to call the addDecisionService() method to deploy the Ruleflow.In our example, we will deploy the Cargo.eds file that we packaged earlier in Corticon Studio.The addDecisionService() method is an overloaded method. However, at minimum, you must pass three parameters—the Decision Service name, the path to the compiled EDS file, and a Boolean parameter that specifies whether dynamic reload should be set to true or false. Before you deploy the decision service, check if the Decision Service is already loaded using an ‘if’ statement.
    Properties p = new Properties();
    p.put(ICcServer.PROPERTY_AUTO_RELOAD, false);
    if (!server.isDecisionServiceDeployed("Cargo")){
       server.addDecisionService(decisionServiceName,"Cargo_v0_16.eds", p); 
    }
  11. Write code to execute the Decision Service.The execute() method accepts two parameters—the Decision Service name, and the collection that contains the request object.This sends the request object message to Corticon Server. This message is then processed by the Decision Service that is deployed on the server. The execute method then updates the Cargo request object in memory and returns rule messages. You can access the rule messages by creating an object of the type ICcRuleMessages.
    ICcRuleMessages msgs = server.execute("Cargo", cargoList);
  12. Next, create a list that will receive the rule messages from the ICcRuleMessages object.
    List<ICcRuleMessage> msgList = msgs.getMessages();
  13. Write code to iterate through each rule message.
    Iterator<ICcRuleMessage> itr = msgList.iterator();
    while(itr.hasNext()) {
      ICcRuleMessage m = itr.next();
    }
  14. Now write code to get the rule message’s associated object, and convert the generic object to a Cargo object.
    Object obj = m.getEntityReference();
    Cargo x = (Cargo) obj;
  15. Write print statements to display the output in the console.
    System.out.println("----------");
    System.out.println("Cargo weight is "+x.getWeight());
    System.out.println("Cargo volume is "+x.getVolume());
    System.out.println("Cargo container value is "+x.getContainer());
    System.out.println(m.getSeverity());
    System.out.println(m.getText());  
  16. Add a catch block for the try block we have used in the code.And finally, write the Java main method.
    } catch(Exception e){
            System.out.println(e);      
      }
    }
    
      public static void main(String[] args) {
        InvokeDS d = new InvokeDS();
        d.callDS();
      }
    
    }
You have now finished writing the Java client code. Save the Java code under an appropriate package and project name. Here is the complete code used to deploy and access the Decision Service.
package program;
import com.corticon.eclipse.server.core.CcServerFactory;
import com.corticon.eclipse.server.core.ICcServer;
import com.corticon.service.ccserver.*;
import cargoLibrary.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Properties;

public class InvokeDS {

   private static String decisionServiceName = "Cargo";

   public void callDS(){ 
     try {
        Cargo cargo = new Cargo();
        cargo.setWeight(1000);
        cargo.setVolume(10);
        List<Cargo> cargoList = new ArrayList<>();
        cargoList.add(cargo);
        
        ICcServer server = CcServerFactory.getCcServer();
        Properties p = new Properties();
        p.put(ICcServer.PROPERTY_AUTO_RELOAD, false);
        if (!server.isDecisionServiceDeployed("Cargo")){
         server.addDecisionService(decisionServiceName,"Cargo_v0_16.eds", p); }
         ICcRuleMessages msgs = server.execute("Cargo", cargoList);
         List<ICcRuleMessage> msgList = msgs.getMessages();
         Iterator<ICcRuleMessage>
         itr = msgList.iterator();
        
         while(itr.hasNext()) {
             ICcRuleMessage m = itr.next();
             Object obj = m.getEntityReference();
             Cargo x = (Cargo) obj;
             System.out.println("----------");
             System.out.println("Cargo weight is "+x.getWeight());
             System.out.println("Cargo volume is "+x.getVolume());
             System.out.println("Cargo container value is " +x.getContainer());
             System.out.println(m.getSeverity());
             System.out.println(m.getText());
     } catch(Exception e){
        System.out.println(e);
     }

  public static void main(String[] args) {
    InvokeDS d = new InvokeDS();
    d.callDS();
  }
}