Download Deploying a Progress Corticon Decision Service in Process for Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Tutorial: Deploying a Progress Corticon Decision Service
in Process for Java
Product Version: Corticon 5.5.2
Introduction .................................................................................................................................... 3
Setting up the tutorial ..................................................................................................................... 4
Step 1: Installing Corticon Server .............................................................................................................. 4
Step 2 : Importing a Sample Project ......................................................................................................... 4
Creating Java classes for the Vocabulary entities ........................................................................... 7
Switch to Java Perspective in Corticon Studio .......................................................................................... 7
Creating Java ‘get’ and ‘set’ methods ....................................................................................................... 8
Importing the JAR file into the Vocabulary ................................................................................... 11
Verifying that the Java classes are imported and mapped ..................................................................... 14
Packaging the ruleflow.................................................................................................................. 15
Packaging the Ruleflow into an EDS file.................................................................................................. 15
Writing Java Client code to deploy and access the Decision Service ........................................... 19
Writing Java Client code.......................................................................................................................... 20
Testing the Decision Service ................................................................................................................... 26
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
2
Introduction
Once the rules in a Ruleflow are modeled, analyzed and tested, they are ready to be deployed to
Progress Corticon Server as a Decision Service.
There are a number of ways you can deploy a Decision Service. You can deploy a Decision Service inprocess or as a Web Service for either Java or .NET. In this tutorial, you will learn how to deploy and
access a Decision Service in-process from a Java client.
To deploy and access a Decision Service from a Java client application, you need to perform the
following tasks:
1.
2.
3.
4.
Create Java classes for the Vocabulary entities
Import the Java files into the Corticon Studio
Package the Ruleflow in Corticon Studio
Write Java client code to deploy and access the Decision Service
In this tutorial, you will learn how to perform these tasks.
In this tutorial we will deploy the sample “Cargo” decision service bundled with Corticon Studio in
process. The steps described will be applicable to your own projects.
Note: Before you can perform these tasks, you need to ensure that Corticon Studio and Corticon Server
are installed.
You will use Corticon Studio for Java programming. You will need a JDK to test Java code. The JDK used
for the examples shown in this tutorial is Java SE 1.8.
This tutorial is designed for hands-on use. We recommend that you follow along in your Java editor,
Corticon Studio and Corticon Server, using the instructions and illustrations that are provided.
If you haven’t installed Corticon Studio yet, install it now. Click here for instructions on installing
Corticon Studio.
You will install Corticon Server next as part of setting up the environment for this tutorial.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
3
Setting up the tutorial
Before you work on deploying a Decision Service, you need to set-up your environment for this tutorial.
You must:
1. Install Corticon Server
2. Import a Sample Project
Step 1: Installing Corticon Server
You will need to first install Corticon Server 5.5 and then a service pack for Corticon Server 5.5.2 or later.
When you install Corticon Server 5.5, make sure that you install Corticon Server for Java. You may
choose to also install the Corticon Web Console, which is a tool used for administering and monitoring
your Decision Services.
Step 2 : Importing a Sample Project
Corticon Studio comes with built-in sample rule projects. We will use one of the sample projects for this
tutorial. To import the sample:
1. Launch Corticon Studio.
2. Go to Help > Samples.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
4
3. On the Samples page, select Tutorial and click Open.
4. In the Import Projects window that opens, click OK.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
5
5. The sample rule project should appear in Corticon Studio.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
6
Creating Java classes for the Vocabulary entities
The data payloads of the request and response messages that are sent or received by the Java client are
in the form of a collection of Java objects.
We will need to create a class for each Vocabulary entity that is used by the rules you want to call. We
will create get and set methods within each class file. Then we will export the code to a JAR file, and
place it in a location where it can be accessed by Corticon Server during run-time.
Switch to Java Perspective in Corticon Studio
1. Click on the Java option in the top right of your Corticon Studio screen.
2. Click File>New>Java Project
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
7
Creating Java ‘get’ and ‘set’ methods
To create getters and setters in Java:
1. Define a package and create a class for each Vocabulary entity that is used by the rules you want
to call. In this tutorial, we will create a class for one entity – Cargo.
Remember that the class name and the entity name must match exactly; the names are casesensitive.
2. Within the entity class, declare the entity’s attributes as variables with the appropriate data
type. In this example, declare the container and manifestNumber as Strings, needsRefrigeration
as a Boolean and volume and weight as long data types, as shown.
Note that just like the entity, the variable and the attribute names must match exactly; the
names are case-sensitive.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
8
3. Now, in each entity class, define a get and set method for each attribute.
The final code should look like this.
package cargoLibrary;
public class Cargo {
public
public
public
public
public
String container;
String manifestNumber;
Boolean needsRefrigeration;
long volume;
long weight;
public String getContainer() {
return container;
}
public void setContainer(String container) {
this.container = container;
}
public String getManifestNumber() {
return manifestNumber;
}
public void setManifestNumber(String manifestNumber) {
this.manifestNumber = manifestNumber;
}
public Boolean getNeedsRefrigeration() {
return needsRefrigeration;
}
public void setNeedsRefrigeration(Boolean needsRefrigeration) {
this.needsRefrigeration = needsRefrigeration;
}
public long getVolume() {
return volume;
}
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
9
public void setVolume(long volume) {
this.volume = volume;
}
public long getWeight() {
return weight;
}
public void setWeight(long weight) {
this.weight = weight;
}
}
4. After you write a class with get and set methods for all the entities and attributes used by the
rules you want to call, export the classes to a JAR file. In this example, export the Cargo class to
a JAR file.
Note: At runtime, Corticon Server will need to access the folder that contains the generated Java code.
To ensure that Corticon Server can access these files, it is a good practice to copy the JAR file to the
Server install directory, under the lib folder.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
10
Importing the JAR file into the Vocabulary
The next step is to map the Java classes and variables you just created to the Vocabulary entities in
Corticon Studio. You need to import the JAR file that you just created.
You need to switch back to Corticon Designer Perspective. Click on Corticon Designer button on the top
right of your screen.
To import the JAR file:
1. Open the Vocabulary file Cargo.ecore.
2. Select Vocabulary > Java Object Messaging > Import Java Class Metadata.
3. In the Import Java Class Metadata window:
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
11
a. Use the Browse button to find the JAR file you created earlier.
b. In the Java Package Selection page, ensure that the package in which you defined the
entity classes is selected and deselect other packages. In this case, the Package is
CargoLibrary.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
12
c. Click Finish.
4. When the import succeeds, you see the following message:
5. Save the Vocabulary file.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
13
Verifying that the Java classes are imported and mapped
Next, we will verify that the Java classes and variables have been imported and mapped in the
Vocabulary. You do this by selecting an entity or attribute in the Vocabulary tree and verifying that the
Java properties on the right are populated.
Click on the Cargo entity. You can see the Java properties—Java Package and Java Class Name. The Java
Package name matches the package we defined earlier. The Java Class Name is the same as the entity
name.
Click on the weight attribute. Note that each attribute has the following Java properties—Java Object
Get Method, Java Object Set Method, and Java Object Field Name. The get and set methods should be
automatically populated after importing the Java class metadata.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
14
Packaging the ruleflow
Now that you have mapped the Java classes with the Vocabulary, you need to package the ruleflow to
make it available to the Java client application.
There are different ways to package a Ruleflow. In this tutorial, you will package the Ruleflow in a precompiled EDS file.
Packaging the Ruleflow into an EDS file
Let’s start by packaging the Ruleflow in Corticon Studio.
1. Open the tutorial_example.erf file.
The Ruleflow has just one Rulesheet—Cargo.ers – which is renamed to tutorial_example in the
Ruleflow. When you create an EDS file, the Ruleflow, the Rulesheet that it contains, and the
Vocabulary all get compiled into a package that can later be deployed on Corticon Server.
2. To package the Ruleflow, select Project > Package and Deploy Decision Services.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
15
3. In the Deployment Target window, choose Package and save for later deployment and click
Next.
4. In the Package Ruleflows screen:
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
16
a. Select the tutorial_example Ruleflow.
b. Keep the default directory or specify a different location to save the EDS file by
clicking the Browse button.
c. Click Next.
5. In the Additional Jar Files screen, browse and locate the CargoLibrary.jar file that you
created earlier. Then click Finish.
You should see a message indicating that the Ruleflow was successfully compiled into an EDS file
and saved in the location you specified.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
17
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
18
Writing 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. Switch to Corticon Studio Java perspective.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
19
2. Create a new Java Project.
3. Before you write Java code to deploy the Decision Service, you need to copy the following
Corticon Server JAR files from the Corticon Server Installation directory and place them in a
location accessible by the surrounding Java container:
 CcServer.jar
 CcConfig.jar
 CcLicense.jar
 CcThirdPartyJars.jar
 CcI18nBundles.jar
 ant_launcher.jar
 CcExtensions.jar
4. Import the Cargolibrary.jar file containing the get and set methods to your Java Project.
5. Configure the Java classpath to include the JAR files listed above.
Writing Java Client code
Let’s begin writing the Java code:
1. In your client program, begin by importing the required packages and classes as shown.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
20
package program;
import
import
import
import
import
import
import
2.
com.corticon.eclipse.server.core.CcServerFactory;
com.corticon.eclipse.server.core.ICcServer;
com.corticon.service.ccserver.*;
cargoLibrary.*;
java.util.ArrayList;
java.util.List;
java.util.Iterator;
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(){
}
}
3. 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);
4. 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.
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.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
21
List<Cargo> cargoList = new ArrayList<>();
cargoList.add(cargo);
5. 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();
6. 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.
if (server.isDecisionServiceDeployed("decisionServiceName")==false){
server.addDecisionService(decisionServiceName,
"C:\\Users\\fmohiudd\\Desktop\\Cargo.eds", false);
}
7. 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);
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
22
8. Next, create a list that will receive the rule messages from the ICcRuleMessages object.
List<ICcRuleMessage> msgList = msgs.getMessages();
9. Write code to iterate through each rule message.
Iterator<ICcRuleMessage> itr = msgList.iterator();
while(itr.hasNext()) {
ICcRuleMessage m = itr.next();
10. 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;
11. Write print statements to display the output in the console.
System.out.println("----------");
System.out.println("Cargo volume is "+x.getVolume());
System.out.println("Cargo weight is "+x.getWeight());
System.out.println("Cargo container value is "+x.getContainer());
System.out.println(m.getSeverity());
System.out.println(m.getText());
12. Add a catch block for the try block we have used in the code.
13. 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();
}
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
23
}
You have now finished writing the Java client code. Make sure you 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
import
import
import
import
import
import
com.corticon.eclipse.server.core.CcServerFactory;
com.corticon.eclipse.server.core.ICcServer;
com.corticon.service.ccserver.*;
cargoLibrary.*;
java.util.ArrayList;
java.util.List;
java.util.Iterator;
public class InvokeDS {
private static String decisionServiceName = "Cargo";
public void callDS(){
try {
//Create request objects
Cargo cargo = new Cargo();
cargo.setWeight(3000);
cargo.setVolume(60);
//Cargo cargo1 = new Cargo();
//cargo1.setWeight(1000);
//cargo1.setVolume(60);
//Add request objects to a collection (When creating array
lists it is considered best practice to assign it to a List variable)
List<Cargo> cargoList = new ArrayList<>();
cargoList.add(cargo);
//c.add(cargo1);
//Instantiate Corticon Server
ICcServer server = CcServerFactory.getCcServer();
//Check if Decision Service is already deployed to Corticon
Server
if (server.isDecisionServiceDeployed("Cargo")==false){
//If not, then deploy Decision Service to Corticon
Server
server.addDecisionService(decisionServiceName,
"C:\\Users\\fmohiudd\\Desktop\\Cargo.eds", false);
}
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
24
//Send collection containing request objects to Decision
Service using server.execute and add the response to an ICcRuleMessages object
ICcRuleMessages msgs = server.execute("Cargo", cargoList);
//Get rule messages from the ICcRuleMessages object,
(declares a variable and assigns a value to it)
List<ICcRuleMessage> msgList = msgs.getMessages();
//Iterate through each rule message
Iterator<ICcRuleMessage> itr = msgList.iterator();
while(itr.hasNext()) {
ICcRuleMessage m = itr.next();
//Get the rule message's associated object, which is
a generic java.lang.Object
Object obj = m.getEntityReference();
//Convert the generic object to a Cargo object
Cargo x = (Cargo) obj;
//Print statements
System.out.println("----------");
System.out.println("Cargo volume is "+x.getVolume());
System.out.println("Cargo weight is "+x.getWeight());
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();
}
}
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
25
Testing the Decision Service
Now that you have written the Java client code, you can test it and confirm whether the code accesses
the Decision Service.
1. In your eclipse project, run the Java client code.
The Decision Service updates the container variable based on the values of weight and volume
provided in the request object message. Based on the rules defined for this project in Corticon
Studio, for an input volume of 10 and weight of 1000, you should see the response ‘standard’.
This indicates that the decision service is responding to the Java client code.
Now, let’s change the values of weight and volume defined while writing java client code. To do this,
refer to step 3 under Writing Java Client code to deploy and access the Decision Service.
2. Enter values 3000 for weight and 60 for volume.
try {
//Create request objects (Cargo)
Cargo cargo = new Cargo();
cargo.setWeight(3000);
cargo.setVolume(60);
List<Cargo> cargoList = new ArrayList<>();
cargoList.add(cargo);
3. Now, run the Java client code again. Based on the rules defined for this project in Corticon
Studio, for an input volume of 60 and weight of 1000, you should see the response ‘oversize’.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
26
This indicates that the decision service is responding to the modified client code.
--
Congratulations! You have completed this tutorial.
You have installed Corticon Server, created get and set methods in Java, prepared the Vocabulary for
deployment, packaged the ruleflow as an EDS file, and created Java client code to deploy and access the
Decision Services. Finally, you tested the Java client code to confirm that it accesses the Decision
Service.
Tutorial: Deploying a Progress Corticon Decision Service in Process for Java
© 2016 Progress Software Corporation. All rights reserved.
27