Data Adaptor - Sample Code (DerbyDataAdaptor)

The sample DataAdaptor in this example does the following:

 

The sample code demonstrates:

 

The sample code is for requirements such as:

Setup

This sample code needs the following to run:

About the Parents and Children Rulebase

The rule is not relevant for this sample, it is the entities, attributes, and relationships that we are interested about (since the Data Adaptor needs to save and load them)

The Rule entities it uses are: Global, child, person.

The relationships are:

 

source relationship type target source text target text
person m-m child personschild childsparents
The attributes relevant to this example:

Global

 

Child

 

Person

About the DERBY Database

The Database tables are:

 

WD_CASE (represents the Global entity)
WD_CASE_ID (VARCHAR(255), PK)
SUN_SHINING(INT)

 

DERBY CREATE TABLE COMMAND:

CREATE TABLE WD_CASE (WD_CASE_ID VARCHAR(255) PRIMARY KEY, SUN_SHINING INT);

 

CHILD
CHILD_ID (INT, PK-IDENTITY)
WD_CASE_ID (VARCHAR(255),FK)
CHILD_NAME (VARCHAR(255))
EATING_ICECREAM(INT)

 

DERBY CREATE TABLE COMMAND:

CREATE TABLE CHILD (CHILD_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY, WD_CASE_ID VARCHAR(255), CHILD_NAME VARCHAR(255), EATING_ICECREAM INT, PRIMARY KEY(CHILD_ID));

 

PERSON
PERSON_ID (INT, PK – IDENTITY)
PERSON_NAME(VARCHAR(255))

 

DERBY CREATE TABLE COMMAND:

CREATE TABLE PERSON (PERSON_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY, WD_CASE_ID VARCHAR(255), PERSON_NAME VARCHAR(255), PRIMARY_KEY(PERSON_ID));

 

CHILD_PARENT
(represents the m:m relationship 'childsparents')
CHILD_ID (INT, PK and FK)
PARENT_ID (INT, PK and FK)

 

DERBY CREATE TABLE COMMAND:

CREATE TABLE CHILD_PARENT (CHILD_ID INT, PARENT_ID INT, PRIMARY KEY(CHILD_ID, PARENT_ID));

 

About the Derby Data Adaptor plugin

The sample code below can do the following:

 

All the main methods of the DerbyDataAdaptor (listCases, load, save) use the methods for connecting and disconnecting to the DERBY database (connectDBObjects, closeDBObjects). Because those methods connect to the database, they must put the method logic in a try-catch block.

To setup this scenario:

  1. Create an SQL Database ‘derbydataadaptor’; it doesn't have to be DERBY, but that is what the source code is set up for.
  2. Create the tables in the ‘derbydataadaptor’ database.
  3. Extract and open the “Parents and Children” rulebase located in C:\Program Files\Oracle\Policy Modeling\examples\Parents and Children \Parents and Children.zip
  4. Build the project
  5. Copy the Parents and Children .zip file from the project output directory (for example, C:\projects\Parents and Children\Development\output) to the rulebase folder in Web Determinations (for example, <webroot>\WEB-INF\classes\rulebase)
  6. Copy the code for the DerbyDataAdaptor plugin (you may need to modify the database connection details)
  7. Compile and JAR the DerbyDataAdaptor (you may need class files for your specific database implementation)
  8. Install the JAR file on the Web Determinations. More info in article: Create a Plugin
  9. Copy the DERBY libraries to library folder in Web Determinations (for example, <webroot>\WEB-INF\lib)
  10. Make sure DERBY is run in network server mode
  11. Run a Web Determinations Interview

How the DerbyDataAdaptor works

 

The DerbyDataAdaptor is triggered by the following actions (detailed in the Data Adaptor Plugin page), that is:

listCases()

The listCases() method is straightforward. It needs to retrieve all the available cases that can be 'loaded'. These cases are stored in the WD_CASE table, so this method simply queries the WD_CASE table, retrieves all the ID's, and returns them to be displayed in the 'Load' screen.

load()

The load() method builds the InterviewUserData to be returned with the following steps:

  1. The user is authenticated
  2. The method connects to the database
  3. Creates a new interview user data
  4. Retrieves the global record in WD_CASE table by using the case ID
  5. Gets the global interview entity instance in the created interview user data
  6. Sets the attribute ‘sun_shining’ in the global instance
  7. Retrieves all children from the CHILD table by using the case ID
  8. For each child retrieved in step 7;
    1. An interview entity instance is created for the child
    2. The ‘child_name’ and ‘eating_icecream’ are set in the child’s interview entity instance.
    3. The child’s interview entity instance containment parent is set to the global interview entity instance.
  9. The global interview entity instance containment for child entity is set to complete.
  10. Retrieves all people from the CHILD table by using the case ID
  11. For each person retrieved in step 10;
    1. An interview entity instance is created for the person
    2. The ‘person_name’ attribute is set in the person’s interview entity instance.
    3. The person’s entity instance containment parent is set to global interview entity instance.
  12. The global interview entity instance containment for person entity is set to complete.
  13. For each person interview entity instance created in step 11, the relationship ‘personschildren’ is set by adding the appropriate child interview entity instance/s using the table CHILD_PARENT.
  14. The database objects are closed and connection is terminated.

save()

The save() method accesses the Rulebase model data (Rulebase object) and the Session instance data (Session object) to save the current Interview user data. For this sample code, the global, child and person entities’ instances are saved.

Only base attributes for all entities are saved.

The save process is as follows:

  1. The user is authenticated
  2. The method connects to the database
  3. The global entity instance from the session is written in the WD_CASE table with fields WD_CASE_ID and SUN_SHINING .
  4. For each contained child entity instance in the global entity instance, the child entity instance is written in the CHILD table with fields WD_CASE_ID, CHILD_NAME and EATING_ICECREAM.
    Note: When a record is written in the CHILD table, the CHILD_ID is automatically generated.
  5. For each contained person entity instance in the global entity instance, the person entity instance is written in the PERSON table with fields WD_CASE_ID and PERSON_NAME
    Note: When a record is written in the PERSON table, the PERSON_ID is automatically generated.
  6. The m:m relationship ‘parentschildren’ is recorded in the table CHILD_PARENT by using the generated CHILD_ID field values in table CHILD and PERSON_ID field values in table PERSON.
  7. The database objects are closed and connection is terminated.

 

DerbyDataAdaptor.java