Today let’s have a look at an example project which explains how to implement batch processor in OPA. We will be using Oracle Database as the data source for our example. For the implementation you need to have the Oracle Database 11g installed in your system along with the Oracle SQL Developer. You also required to download the Oracle Policy Automation server components which helps you with the batch processor execution. You can refer my previous posts for getting started. Please follow the links given below;
- How To Install Oracle Database 11g and Configure SQL Developer
- Starting With Batch Processor in Oracle Policy Automation
Important: You have to download the latest OPA server components if you have updated the OPM tool. Else you will get compatibility issues during execution.
I hope you have gone through the above mentioned posts and you are ready to go. This project implementation and end-to-end execution involves 3 steps. In fact almost every such implementation will have these stages in common;
- Rule Authoring using Oracle Policing Modeling tool.
- Creating Table, Inserting Data into Oracle Database using SQL Developer.
- Creating mapping xml file and executing the batch processor.
Rule Authoring Using Oracle Policy Modeling Tool
Let’s create a example rulebase using OPM. I am going to create a project which will tell the user whether he/she is eligible for this particular Government compensation scheme. The conditions are;
- The person applying for this compensation should be of age >= 18
- The person monthly income should be greater than equal to 15000
- The person household size should be greater than equal to 4 and
- Finally, the person should be a citizen of India.
Open up the Policy Modeling tool. Click on Project Tab and select New Project.
Click on Rules tab and create a new Word Document for writing the rules. Once you have done that you can write the following rule in the word document.
Click on validate and compile the rules. Now if you go back to the tool you can view the attributes used under the Data tab. If you double click the attribute you will get a window like this;
Give the name for the attribute in the Name field. This name is used for mapping a particular attribute to the corresponding database column. This is how it will look like once you complete naming all the attributes.
Once you have completed naming the attributes click on the Debug button on the top of the OPM tool window.
This will open up the interview screen and at the same time OPM has compiled and created the rulebase zip file. This will be placed inside the output folder where the project is saved. To view this go to Project tab. Click on Summary option in the left navigation bar and click on Open Project Folder.
Now this window will be popped up.
Open the output folder.
This is a compiled rulebase and this will be used for executing the batch processor. Now let’s set up the database part and will come back to this later.
Creating Table, Inserting Data into Oracle Database using SQL Developer.
We are using SQL Developer for managing the database connection and the tables. For this particular rulebase I am required to get information of people in a, let’s say, state.
I am going to create table named T_PEOPLE to store this information. It will have following columns;
- PERSON_ID – Primary Key
- ELIGIBLE – Output column.
Open SQL developer. Select the database and enter the login credentials. Here xe_sreeragnk is the connection that I have added which connects to the database. Double click on any connection and you will be asked to enter the password. Once entered click OK.
Now you will be able to access the database which has all the tables, views and more. Right click on Tables and select New Table.
Create the table T_PEOPLE with the above mentioned columns;
Once the table is created you need to enter some dummy date for execution. You can add data directly from the SQL Developer tool by clicking on the Data tab for a particular table.
Click on the icon marked ‘1’ to add new rows and ‘2’ is used to commit (save) the changes made to the database. I have made the ELIGIBLE row null since it is the output field and will be automatically processed after the execution.
Creating mapping xml file and executing the batch processor.
Now the database part is over. Lets do the third and final step which is creating XML mapping file. For this create a folder anywhere in the system with following structure.
All the files mentioned above are there in the server components folder you have downloaded. You can just copy and paste it here in this folder.
XML Mapping File
Just open a new notepad++ or notepad file and write the following mappings. Save it as .xml format with name config. Save this under the newly created folder.
<output type=”db” />
<mapping entity=”global” table=”t_people” primary-key=”person_id”>
<attribute name=”household_size” field=”household_size”/>
<attribute name=”monthly_income” field=”monthly_income”/>
<attribute name=”age” field=”age”/>
<attribute name=”citizen” field=”citizen”/>
<attribute name=”eligible” field=”eligible” output=”true”/>
The <options> tag contain the connectivity related information.
- <base> – the name of the base table used. Here it is T_PEOPLE.
- <rulebase> – provide the system path of the compiled rulebase. Here it is inside the rulebase folder.
- <database> – provide the database connectivity related information here.
- <url> – provide the database name, hostname, username and password. In our case it is the localhost and it is pretty much the same except change ‘sreeragnk’ with your username and ‘root’ with your password.
- <driver> – use the same.
- <driversrc> – use the same. Our jar file is inside the lib folder.
The <mappings> section has the mapping details. Here the global entity is mapped with the T_PEOPLE table. The primary key is provided. List out the input mappings first. Attribute name is the name of the attribute we have given in the tool. Field is the name of the table column name.
Finally eligible is our output field. So we have to add an extra text output=”true” which will make the system write back the result back to the database.
Executing Batch Processor
Open the command prompt window in the current folder location. You can do this by right clicking on empty space by pressing and holding Ctrl + Shift . Type the following command.
Java -Jar Deteminations-batch.jar and hit enter.
Now if you go back to the database you can see the eligible column is filled with the result.
This is a very simple example. You can extend the batch processor capability even with the complex rulebases which has entities involved. For that you need to create extra mappings in the XML file. I will cover this in coming posts. If you liked this post please do subscribe for free email updates and Like us on Facebook. Thank you for reading.