Data sources for decision tables sample details

You use Rule Designer to run this sample.

Running this sample

You can run this sample in several stages.

To run the File Provider:

  1. If necessary, switch to the Rule perspective:

    In Rule Designer, click Window  > Perspective > Open Perspective  > Other. Select Rule , and then click Open.

  2. Click Run  > External Tools  > External Tools Configurations.

    The External Tools Configurations window opens.

  3. In the Configurations list, click Ant Build  > dtpopulate-application.
  4. Click the Targets tab.
  5. Select the run.file target check box.
  6. Click Run.

    The Console view displays the output:

    ...
    [java] Received arguments:
    [java] targetproject='dtpopulate-rule'
    [java] dtname='dtsample'
    [java] driver=' '
    [java] url='csv://./data/script/data.txt'
    [java] user=''
    [java] password=''
    [java] data=';'
    [java] dtsample successfully loaded 
  7. In the Rule Explorer, right-click the decision table dtpopulate-rule  > rules  > dtsample and click Refresh.
  8. Double-click the decision table dtsample to open it and load the associated data.
  9. Close the dtsample decision table.

To run the Excel Provider:

  1. In the Rule perspective, on the Run menu, click External Tools  > External Tools Configurations.

    The External Tools Configurations window opens.

  2. In the Configurations list, click Ant Build  > dtpopulate-application.
  3. Click the Targets tab.
  4. Clear run.file and select the run.xl target check box.
  5. Click Run.

    The Console view displays the output:

    ...
    [java] Received arguments:
    [java] targetproject='dtpopulate-rule'
    [java] dtname='dtsample'
    [java] driver=' '
    [java] url='xl://./data/script/data.xls'
    [java] user=''
    [java] password=''
    [java] data='sheet'
    [java] dtsample successfully loaded 
  6. In the Rule Explorer, right-click the decision table dtpopulate-rule  > rules  > dtsample and click Refresh to refresh the decision table.
  7. Double-click the decision table dtsample to open it and load the associated data.
  8. Close the dtsample decision table.

To run the Database Provider:

  1. In the Rule perspective, on the Run menu, click External Tools  > External Tools Configurations.

    The External Tools Configurations window opens.

  2. In the Configurations list, click Ant Build  > dtpopulate-application.
  3. Click the Targets tab.
  4. Clear the run.xl target check box and select the run.db target check box.
  5. Click Run.

    The run.db target first creates and loads a simple H2 database schema. The Console view displays the output:

    ...
    [java] Received arguments:
    [java] targetproject='dtpopulate-rule'
    [java] dtname='dtsample'
    [java] driver='org.apache.h2.jdbc.ClientDriver'
    [java] url='jdbc:h2://localhost:1527/DTPOPULATION'
    [java] user='DTPOPULATIONSAMPLE'
    [java] password='DTPOPULATIONSAMPLE'
    [java] data='SELECT * FROM DATATABLE'
    [java] dtsample successfully loaded
    ...
  6. In the Rule Explorer, right-click the dtpopulate-rule  > rules  > dtsample decision table and click Refresh to refresh the decision table.
  7. Double-click the dtsample to open it and load the associated data.
  8. Close the dtsample decision table.

How this sample works

The classes and arguments that are required in this sample:

  • ilog.rules.studio.samples.dtpopulate.DTFeeder:

    When the application starts, it calls this class. The class parses arguments that are passed through from the build.xml file and retrieves the project and the decision table.

    It creates the data provider and the controller. You use Controller to load the data (with its provider). Any modifications that are made are committed over the decision table, which is persisted in the workspace.

  • ilog.rules.studio.samples.dtpopulate.helper.Argument:

    This class parses the arguments that are passed, and creates the data loader that is based on input arguments.

    You use the following arguments when you use 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.h2.jdbc.ClientDriver).
    • url: The database URL (jdbc:h2:localhost:1527/DTPOPULATION;create=true).
    • user: The user to connect to the database (DTPOPULATIONSAMPLE).
    • pwd: The password to connect to the database (DTPOPULATIONSAMPLE).
    • info: A specific argument. For the database provider, it is the query that gets the data; (SELECT * FROM DATATABLE).

    You use the following arguments when you use 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 that is used to tokenize data read from the file.

    You use the following arguments when you use 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 that contains the data.

    The data loader is a delegated object that is used by the data provider. The url input argument specifies the data loader to use:

    • When the url argument starts with jdbc:, use the database loader.
    • When the url argument starts with csv: use the file loader.
    • When the url argument starts with xl: use the Excel loader.
  • ilog.rules.studio.samples.dtpopulate.helper.FileLoader:

    The application calls this class when it uses a file loader. The class 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 class opens the file.
    • The class reads the file line by line and each line is tokenized with the separator argument passed.
    • After each line is tokenized, the class stores the data in a tabular list. The provider uses this list to populate the decision table.
  • ilog.rules.studio.samples.dtpopulate.helper.JdbcLoader

    When the application uses a jdbc loader, it calls this class. The class 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.

    • The class triggers a connection to the database by using the driver, url, user, and password arguments.
    • The class runs the query that is given as an argument.
    • The class reads the data from the result set returned by the query and stores it 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 uses 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. The Excel spreadsheet contains numbered columns. When read, these columns return their value as a double.

    • The class opens the file by using the Jxl API.
    • The class reads the sheet row by row, column by column and stores the data in a tabular list. The provider uses this list to populate the decision table.
    Note: The sample decision table has columns in Integer format. The corresponding cells in the Excel spreadsheet must also be formatted to use Integer format.
  • 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 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.

Source files

To display the source code of the dtpopulate Eclipse plug-in:

  1. Click File  > Import.
  2. In the Import wizard, select Plug-in Development > Plug-ins and Fragments and click Next.
  3. In the Import As section, select the Projects with source folders option and click Next.
  4. In the Plug-ins and Fragments Found list, scroll down and select the ilog.rules.studio.samples.dtpopulate plug-in, and then click Add.
  5. Click Finish.

    Rule Explorer displays the source code of the plug-in.

  6. Switch to the Plug-in Development perspective.

    Click Window  > Open Perspective  > Other. Select the Plug-in Development perspective, and then click OK.

  7. In the Package Explorer, navigate to the Java™ source files in the ilog.rules.studio.samples.dtpopulate  > dtpopulatesrc folder.

    The sample uses the following classes, which are described in How this sample works:

    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