How to Populate a Decision Table from a Data Source
|
PREVIOUS
NEXT
|
This sample shows how to fill an empty, structured decision table using Rule Studio APIs. The values used to populate the decision table model come from a customized data provider object providing the decision table content. This sample comes with three providers:
-
File provider: Data are stored in a file
-
Excel provider: Data are stored in an excel sheet
-
Database provider: Data are stored in a database
This sample uses the Apache Derby engine and the Jxl class library provided with ILOG JRules.
Key Features
Key features include:
-
Development of an Eclipse plug-in using the exposed Rule Studio API
-
Ability to automate Rule Studio tasks through ant tasks
-
Retrieve project, and a rule in the workspace from a name
-
Check if a project or a rule are defined in the workspace
-
Saving a resource in a workspace after modification
-
Use of the repository API to access the definition object of the decision table
-
Definition of a custom data provider class implementing
IlrDTDataProvider
Installing this Sample
This sample extends the Rule Studio API, and the required sample plug-ins are delivered as pre-deployed. If you have not already done so, you must install the samples extension site in Eclipse before running this sample.
To install the plug-in samples extension site:
-
Click Help > Software Updates > Manage Configuration.
-
In the Product Configuration dialog, click the Add an Extension Location link.
-
In the Browse dialog, select the folder
<InstallDir>/studio/samples/eclipse.
-
If prompted to restart the Eclipse Workbench, click Yes.
Eclipse shuts down and restarts.
When Eclipse restarts, you need to check that the Decision Table Loading Sample feature is enabled.
To check that the Decision Table Loading Sample feature is enabled:
-
Click Help > Software Updates > Manage Configuration.
-
In the left pane of the Product Configuration page, expand
C:\ILOG\JRules\studio\samples\eclipse and check that Decision Table Loading Sample 1.0.0. is listed.
You can now import the sample project files.
To import the dtpopulate sample:
-
Click File > Import.
-
In the Import Wizard, select
ILOG Rule Studio Samples and Tutorials.
-
Click Next.
-
On the Sample or Tutorial page, select the sample
authoring > dtpopulate.
-
Click Finish.
-
If one or more files already exist in your workspace, a warning message is displayed indicating that the file(s) were not re-imported. Close the message window.
The imported sample project files are displayed in the Rule Explorer. You should see three new projects:
-
dtpopulate-rule. The target sample rule project. This project contains the decision table that will be populated.
-
dtpopulate-xom. A Java project containing the execution model of
dtpopulate-rule.
-
dtpopulate-application. A Java project including an Ant build file (
build.xml) which contains different Ant tasks that will be used in order to demonstrate different execution scenarios.
| Note |
The target init.runtime was added in the build.xml file to help Eclipse set the runtime properties: os (operating system), ws (window system) and arch (architecture) before running the sample. This should work for most common configurations. If it does not match your runtime environment, you can modify the Ant target to force the values of these properties.
|
Running this Sample
The sample is run in several stages.
To run the File Provider:
-
If necessary, switch to the Rule perspective:
Click
Window >
Open Perspective > Other. Select Rule Perspective, then click
OK.
-
Click Run > External Tools > External Tools.
The External Tools window is displayed.
-
In the Configurations list, click
Ant Build > dtpopulate-application.
-
Click the Targets tab.
-
Clear the run.xl and run.db target check boxes and check the run.file target checkbox.
-
Click Run.
Output is displayed in the Console view:
[java] Starting application:
[java] Received arguments:
[java] targetproject='dtpopulate-rule'
[java] url='file://./data/script/data.txt'
[java] dtsample successfully loaded
-
In the Rule Explorer, right-click the decision table
dtpopulate-rule > rules > dtsample and click Refresh.
-
Double-click the decision table
dtsample to open it and load the associated data.
-
Close the
dtsample decision table.
-
Right-click the
dtsample decision table and click Refresh.
To run the Excel Provider:
-
In the Rule perspective, on the Run menu, click External Tools > External Tools.
The External Tools window is displayed.
-
In the Configurations list, click
Ant Build > dtpopulate-application.
-
Click the Targets tab.
-
Clear the run.file and run.db target checkbox and check the run.xl target checkbox.
-
Click Run.
Output is displayed in the Console view:
[java] Starting application:
[java] Received arguments:
[java] targetproject='dtpopulate-rule'
[java] url='xl://./data/script/data.xls'
[java] dtsample successfully loaded
-
In the Rule Explorer, right-click the decision table
dtpopulate-rule > rules > dtsample and click Refresh to refresh the decision table.
-
Double-click the decision table
dtsample to open it and load the associated data.
-
Close the
dtsample decision table.
-
Right-click the
dtsample decision table and click Refresh to refresh the decision table.
To run the Database Provider:
-
In the Rule perspective, on the Run menu, click External Tools > External Tools.
The External Tools window is displayed.
-
In the Configurations list, click
Ant Build > dtpopulate-application.
-
Click the Targets tab.
-
Clear the run.file and run.xl target checkbox and check the run.db target checkbox.
-
Click Run.
The run.db target first creates and loads a simple Derby database schema. Output is displayed in the Console view:
[java] Received arguments:
[java] targetproject='dtpopulate-rule'
[java] driver='org.apache.derby.jdbc.ClientDriver'
[java] url='jdbc:derby://localhost:1527/../DTPOPULATION'
[java] user='DTPOPULATIONSAMPLE'
[java] password='DTPOPULATIONSAMPLE'
[java] data='SELECT * FROM DATATABLE'
[java] dtsample successfully loaded
-
In the Rule Explorer, right-click the
dtpopulate-rule > rules > dtsample decision table and click Refresh to refresh the decision table.
-
Double-click the
dtsample to open it and load the associated data.
-
Close the
dtsample decision table.
-
Right-click the
dtsample decision table and click Refresh to refresh the decision table.
Source Files
To display the source code of the dtpopulate Eclipse plug-in:
-
Click File > Import.
-
In the Import Wizard, select
External Plug-ins and Fragments and click Next.
-
In the Import As section, select the Projects with source folders option and click Next.
-
In the Plug-ins and Fragments Found list, scroll down and select the
ilog.rules.studio.samples.dtpopulate plug-in (be careful not to select ilog.rules.studio.samples.dtpopulate.source), then click Add.
-
Click Finish.
The source code of the plug-in is imported and appears in the Rule Explorer.
-
Switch to the Plug-in Development perspective.
Click
Window >
Open Perspective > Other. Select the Plug-in Development perspective then click
OK.
-
In the Package Explorer, navigate to the Java source files in the
ilog.rules.studio.samples.dtpopulate > src-dtpopulate folder.
The following source files are of interest and are described below in the Highlights section of this sample:
ilog.rules.studio.samples.dtpopulate.DTFeeder
ilog.rules.studio.samples.dtpopulate.helper.Argument
ilog.rules.studio.samples.dtpopulate.helper.FileLoader
ilog.rules.studio.samples.dtpopulate.helper.JdbcLoader
ilog.rules.studio.samples.dtpopulate.helper.ExcelLoader
ilog.rules.studio.samples.dtpopulate.helper.DataProvider
Highlights
Highlights of this sample include:
-
ilog.rules.studio.samples.dtpopulate.DTFeeder:
This class is called when the application is launched.
It parses arguments passed through from the
build.xml file.
It retrieves the project and the decision table.
It creates the data provider and the controller.
Controller is requested to load the data (with its provider).
Any modifications made are committed over the decision table.
The decision table is persisted in the workspace.
-
ilog.rules.studio.samples.dtpopulate.helper.Argument:
This class parses the arguments passed and creates the data loader depending on input arguments.
Arguments required when using the database provider:
-
workspace: The eclipse workspace directory
-
targetproject: The target project name (dtpopulate-rule)
-
dtname: The decision table name (dtsample)
-
driver: The jdbc driver to use (org.apache.derby.jdbc.ClientDriver)
-
url: The database url (jdbc:derby:localhost:1527/DTPOPULATION;create=true)
-
user: User to connect to the database (DTPOPULATIONSAMPLE)
-
pwd: Password to connect to the database (DTPOPULATIONSAMPLE)
-
info: A specific argument. For the database provider it is the query to run in order to get the data; (SELECT * FROM DATATABLE)
Arguments required when using the file provider:
-
workspace: The eclipse workspace directory
-
targetproject: The target project name (dtpopulate-rule)
-
dtname: The decision table name (dtsample)
-
driver: Not used
-
url: The file url (csv://./data/script/data.txt)
-
user: Not used
-
pwd: Not used
-
info: A specific argument. For the file provider it is the separator used to tokenize data read from the file.
Arguments required when using the Excel provider:
-
workspace: The eclipse workspace directory
-
targetproject: The target project name (dtpopulate-rule)
-
dtname: The decision table name (dtsample)
-
driver: Not used
-
url: The xls file url (xl://./data/script/data.xls)
-
user: Not used
-
pwd: Not used
-
info: A specific argument. For the Excel provider it is the sheet name containing the data.
The data loader is a delegated object used by our data provider. The data loader to use is specified by the
url input argument:
-
When the
url argument starts with jdbc: the database loader is used
-
When the
url argument starts with csv: the file loader is used
-
When the
url argument starts with xl: the Excel loader is used
-
ilog.rules.studio.samples.dtpopulate.helper.FileLoader:
This class is called when the application uses a file loader. It reads data from a file.
This class implements the
ilog.rules.studio.samples.dtpopulate.helper.DataLoader interface. The main processing is done in the
public void load() method.
-
The file is opened.
-
The file is read line by line, and each line is tokenized with the separator argument passed.
-
Once tokenized, the data is stored in a tabular list. The provider uses this list to populate the decision table.
-
ilog.rules.studio.samples.dtpopulate.helper.JdbcLoader
This class is called when the application use a jdbc loader. It reads data from a database.
This class implements the
ilog.rules.studio.samples.dtpopulate.helper.DataLoader interface. The main processing is done in the
public void load() method.
-
A connection is made to the database using the
driver, url, user, and password arguments.
-
The query given as an argument is executed.
-
Data are read from the result set returned by the query and stored in a tabular list. The provider uses this list to populate the decision table.
-
ilog.rules.studio.samples.dtpopulate.helper.ExcelLoader
This class is called when the application use an Excel loader. It reads data from an Excel spreadsheet.
This class implements the
ilog.rules.studio.samples.dtpopulate.helper.DataLoader interface. The main processing is done in the
public void load() method. Note that the Excel spreadsheet contains numbered columns. When read, these columns return their value as a
double.
| Notes |
| The sample decision table has columns in Integer format, therefore the corresponding cells in the Excel spreadsheet must also be formatted as Integer.
|
-
The file is opened using the Jxl API.
-
The sheet is read row by row, columns by column. The data are stored in a tabular list. The provider uses this list to populate the decision table.
-
ilog.rules.studio.samples.dtpopulate.helper.DataProvider
This class extends the
ilog.rules.dt.model.provider.IlrDTAbstractDataProviderTableModel. It is the provider. It is contracted with a
ilog.rules.studio.samples.dtpopulate.helper.DataLoader implementing class and delegates all its works to it. Three factory methods help to create the data loader:
public static IlrDTDataProvider createFileDataProvider (String filepath, String separator) which creates the file provider.
public static IlrDTDataProvider createExcelDataProvider (String filepath, String sheet) which creates the Excel provider.
public static IlrDTDataProvider createJDBCDataProvider (String driver, String url, String user, String password, String query) which creates the database provider.
Related Concepts
Related Tasks
Related Samples and Tutorials