Listing 1

       //Get Connection factory object from the JNDI server

javax.datamining.resource.ConnectionFactory jdmCFactory =

(ConnectionFactory) jndiContext.lookup("java:comp/env/jdm/MyServer");

//Create a data mining server connection specification

ConnectionSpec svrConnSpec = (javax.datamining.resource.ConnectionSpec)


svrConnSpec.setName( žuserÓ );

svrConnSpec.setPassword( žpasswordÓ );

svrConnSpec.setURI( žserverURIÓ );

//Create a Data Mining Engine (DME) Connection

javax.datamining.resource.Connection dmeConn = 

(javax.datamining.resource.Connection)jdmCFactory.getConnection( svrConnSpec );

Listing 2

       //Get PhysicalDataSetFactory

PhysicalDataSetFactory pdsFactory =  



//Create PhysicalDataSet by settings import flag set to true 

//to import the physical attributes details derived from 

//the meta-data of the table. 

PhysicalDataSet pdsBuild = pdsFactory.create(


//Set the role of the CUSTOMER_ID column as caseId

PhysicalAttribute caseId = pdsBuild.getAttribute(žCUSTOMER_IDÓ);

//Save the PhysicalDataSet in the DME

dmeConn.saveObject(pdsBuild, žCHURN_BUILD_PDSÓ);

Listing 3

//Get LogicalDataFactory

LogicalDataFactory ldFactory =



//Create LogicalData by specifying BUILD_PDS as input

LogicalData ldBuild = ldFactory.create(žBUILD_PDSÓ);

//Set the attribute types of all the logical attributes

LogicalAttribute ageAttr = ldBuild.getAttribute(žAGEÓ);


LogicalAttribute workClassAttr = ldBuild.getAttribute(žWORKCLASSÓ);


. . . .
//Save LogicalData in the DME

dmeConn.saveObject(ldBuild, žCHURN_LDÓ);

Listing 4

//Create a classification settings factory

ClassificationSettingsFactory clasFactory = 


//Create a ClassificationSettings object

ClassificationSettings clas = clasFactory.create();

//Set Logical data


//Set target attribute name


//Create a TreeSettingsFactory

TreeSettingsFactory treeFactory =



//Create TreeSettings instance

TreeSettings treeAlgo = treeFactory.create();



treeAlgo.setMinNodeSize( 10, SizeUnit.count );

//Set algorithm settings in the classification settings


//Save the build settings object in the database

dmeConn.saveObject(žCHURN_BUILD_SETTINGSÓ, clas, true);

Listing 5

//Create BuildTaskFactory

BuildTaskFactory buildTaskFactory = BuildTaskFactory)dmeConn.getFactory(


//Create BuildTask object

BuildTask buildTask = buildTaskFactory.create( 


//Save BuildTask object

dmeConn.saveObject(žCHURN_BUILD_TASKÓ, buildTask, true);

//Execute build task asynchronously in the database

ExecutionHandle execHandle = dmeConn.execute(žCHURN_BUILD_TASKÓ);

//Wait for completion of the task

ExecutionStatus execStatus = 


Listing 6

//Create & save PhysicalDataSpecification

PhysicalDataSet testData = pdsFactory.create(

m_dmeConn.saveObject( žCHURN_TEST_PDSÓ, testData, true );

//Create ClassificationTestTaskFactory
ClassificationTestTaskFactory testTaskFactory =  
žjavax.datamining.supervised.classification.ClassificationTestTaskÓ );

//Create, store & execute Test Task
ClassificationTestTask testTask = testTaskFactory.create( 

//Set lift metrics


testTask.setPositiveTargetValue(new Integer(1));

//Save TestTask object

dmeConn.saveObject(žCHURN_TEST_TASKÓ, testTask, true);

//Execute test task asynchronously in the database

ExecutionHandle execHandle = dmeConn.execute(žCHURN_TEST_TASKÓ);

//Wait for completion of the task

ExecutionStatus execStatus = execHandle.waitForCompletion(Integer.MAX_VALUE);

//Explore the test metrics after successful completion of the task

if(ExecutionState.success.equals(execStatus.getState())) {
  //Retrieve the test metrics object
  ClassificationTestMetrics testMetrics =  
  //Retrieve confusion matrix and accuracy
  Double accuracy = testMetrics.getAccuracy();
  ConfusionMatrix cfm = testMetrics.getConfusionMatrix();
  //Retrieve lift 
  Lift lift = testMetrics.getLift();

Listing 7

//Create & save PhysicalDataSpecification

PhysicalDataSet applyData = pdsFactory.create( 
"CHURN_APPLY_TABLE ", false );

applyData.addAttribute( caseId );

m_dmeConn.saveObject( "CHURN_APPLY_PDS", applyData, true );

//Create ClassificationApplySettingsFactory
ClassificationApplySettingsFactory applySettingsFactory =  

//Create & save ClassificationApplySettings

ClassificationApplySettings clasAS = applySettingsFactory.create();

HashMap srcDestMap = new HashMap();



m_dmeConn.saveObject( "CHURN_APPLY_SETTINGS", clasAS, true);

//Create DataSetApplyTaskFactory

DataSetApplyTaskFactory applyTaskFactory =  

//Create, store & execute apply Task

DataSetApplyTask applyTask = m_dsApplyFactory.create(

//Save ApplyTask object

dmeConn.saveObject(žCHURN_APPLY_TASKÓ, applyTask, true);

//Execute test task asynchronously in the database

ExecutionHandle execHandle = dmeConn.execute(žCHURN_APPLY_TASKÓ);

//Wait for completion of the task
ExecutionStatus execStatus = 

Listing 8

import java.util.*;

import javax.datamining.*;
import javax.datamining.association.*;
import javax.datamining.resource.*;
import javax.datamining.task.*;

import javax.naming.*;
// JDM API Imports

 * This class demonstrates the market basket analysis using JDM API. 
 * Execute of this class requires a JDM compatible Data Mining Engine (DME)
 * that supports association rules.
 * Before executing this follow these steps:
 * Step-1: Connection Setup
 *   Ensure that DME ConnectionFactory is registered with the JNDI server. 
 *   Change the connection details in the example accordingly. JDM vendors 
 *   could provide the vendor specific ConnectionFactory class that can 
 *   be used to simplify the execution of this example code.
 * Step-2: PRODUCT_TRANSACTIONS table setup
 *   This example cannot be run without the data. For convenience, a sample data
 *   is printed here in comma separated format that can be imported as a table
 *   in the database or vendor specific data format.
          1,Pepperoni Pizza,1
          1,Diet Coke,1
          1,Buffalo wings,1
          2,Buffalo wings,1
          2,Diet Coke,1
          3,Pepperoni Pizza,1
          3,Diet Coke,1
          4,Diet Coke,1
          4,French Fries,1
          5,Diet Coke,1
          5,Buffalo wings,1
 * Step-3: Classpath setup
 *   Ensure that to run this example you have all the jar files inclusing the
 *   jdm.jar file in your classpath. Refer JDM Vendor product documentation
 *   to find the appropriate dependent libraries to execute this program.
 * Step-4: Execute the class
 *  Using 
 *   java MarketBasket
public class MarketBasket 
  //Connection related data members
  private Connection m_dmeConn = null;
  private ConnectionFactory m_dmeConnFactory = null;
  //Object factories used in this demo program
  private PhysicalDataSetFactory m_pdsFactory = null;
  private PhysicalAttributeFactory m_paFactory = null;  
  private AssociationSettingsFactory m_assoFactory = null;
  private RulesFilterFactory m_filterFactory = null;
  private BuildTaskFactory m_buildFactory = null;  
  public static void main( String args[] )
      MarketBasket marketBasket = new MarketBasket();
      //Login to DME  
      //Initialize object factories
      //Build Market Basket Model & explore the model rules
    catch(Exception anyExp) {
    finally {
      try {
        //ogout from the Data Mining Engine
      } catch(Exception anyExp1) { }//Ignore

   * Constructor initializes all the object factories
   * Get all the factory objects from the connection to create the JDM objects.
  public void initFactories() throws JDMException
    m_pdsFactory = (PhysicalDataSetFactory)m_dmeConn.getFactory(
    m_paFactory = (PhysicalAttributeFactory)m_dmeConn.getFactory(
    m_assoFactory = (AssociationSettingsFactory)m_dmeConn.getFactory(
    m_buildFactory = (BuildTaskFactory)m_dmeConn.getFactory(
    m_filterFactory = (RulesFilterFactory)m_dmeConn.getFactory(
  public void login() throws JDMException, NamingException
    //1. Create or Get DME ConnectionFactory. 
    //   Here one could create vendor specific connection factory to 
    //   simplify the execution of the demo or use the JNDI to get
    //   the connection factory in vendor neutral approach
    //Create JNDI initial context
      Hashtable jndiEnv = new Hashtable ();
      //Set JNDI server specific environment variables 
      //and authentication details
      jndiEnv.put( Context.INITIAL_CONTEXT_FACTORY,
        "com.myCompany.jndi.InitialContextFactoryImpl" );
      jndiEnv.put( Context.PROVIDER_URL, "http://myHost:myPort/myService" );
      jndiEnv.put( Context.SECURITY_PRINCIPAL, "user" );
      jndiEnv.put( Context.SECURITY_CREDENTIALS, "password" );
      InitialContext jndiContext = new InitialContext(jndiEnv); 
    //Perform JNDI lookup to obtain the connection factory
      javax.datamining.resource.ConnectionFactory jdmCFactory =
      (ConnectionFactory) jndiContext.lookup("java:comp/env/jdm/MyServer");
    //Create a data mining server connection
      ConnectionSpec svrConnSpec = (javax.datamining.resource.ConnectionSpec)
      //If the DME authentication details are note registered specify the DME 
      //authentication details. Refer to JDM vendor product documentation for
      //DME authentication details
      svrConnSpec.setName( "user" );
      svrConnSpec.setPassword( "password" );
      svrConnSpec.setURI( "serverURI" );
      m_dmeConn = jdmCFactory.getConnection( svrConnSpec );

   * This method builds a market basket model using PRODUCT_TRANSACTIONS table.
   * Refer to JDM Vendor product documentation to see the data URI representation.
  public void buildModel() throws JDMException 
    //1. Create & save PhysicalDataSet
      PhysicalDataSet buildData = m_pdsFactory.create( 
        "PRODUCT_TRANSACTIONS", false );//Import meta-data is set to false
      //Specify the transactional format data attribute roles
      PhysicalAttribute transId = m_paFactory.create(
          "Transaction_Id", AttributeDataType.integerType, 
          PhysicalAttributeRole.caseId );
      PhysicalAttribute productName = m_paFactory.create(
          "Product_name", AttributeDataType.stringType, 
          PhysicalAttributeRole.attributeName );
      PhysicalAttribute purchasedFlag = m_paFactory.create(
          "Purchased", AttributeDataType.integerType, 
          PhysicalAttributeRole.attributeValue );
      m_dmeConn.saveObject( "MarketBasket_DATA", buildData, false );

    //2. Create & save Mining Function Settings
      //Create ClassificationSettings
      AssociationSettings buildSettings = m_assoFactory.create();
      buildSettings.setMinSupport( 0.1 );
      buildSettings.setMinConfidence( 0.51 );
      m_dmeConn.saveObject( "MarketBasket_SETTINGS", buildSettings, true );

    //3. Create, save & execute Build Task
      BuildTask buildTask = m_buildFactory.create(
                     "MarketBasket_DATA",      //Build data 
                     "MarketBasket_SETTINGS",  //Build settings name
                     "MarketBasket_MODEL"      //Mining model name
      m_dmeConn.saveObject("MarketBasket_TASK", buildTask, true);
      ExecutionHandle execHandle = m_dmeConn.execute("MarketBasket_TASK");
      System.out.print("MarketBasket_TASK is started, please wait. ");
      //Wait for completion of the task
      ExecutionStatus status = execHandle.waitForCompletion(Integer.MAX_VALUE);
      //Check the status of the task after completion
      boolean isTaskSuccess = status.getState().equals(ExecutionState.success);
      if( isTaskSuccess ) {
        //Task completed successfully
        System.out.println("MarketBasket_TASK is successful.");
      } else {//Task failed
        System.out.println("MarketBasket_TASK is failed.\nFailure Description: " + 
          status.getDescription() );
    //4. Restore the model from the DME and explore the details of the model
      AssociationModel model = 
              "MarketBasket_MODEL", NamedObject.model);
    //5. Display the rules
    displayAssociationRules( model );

   * This method displays rules from association model.
   * @param model An association model
  private void displayAssociationRules(AssociationModel model)
    throws JDMException
    // specify the rule selection criteria
    // retrieve rules with selection criteria
    RulesFilter filter = (RulesFilter) m_filterFactory.create();
        new RuleProperty[] {, RuleProperty.confidence },
        new SortOrder[] { SortOrder.descending, SortOrder.descending }
    filter.setRange(, 0.15, 1.0 );
    // filter.setRange( RuleProperty.confidence, 0.55, 1.0 );
    // retrieve rules
    Collection rules = model.getRules(filter);
    Iterator iRules = rules.iterator();
    int ruleNumber = 1;
    while( iRules.hasNext() )
      AssociationRule rule = (AssociationRule);
      System.out.println("Rule-" + ruleNumber++);
      Itemset antecedent = rule.getAntecedent();
      Object[] ante_items = antecedent.getItems();
      // sort the items in antecedent to produce deterministic order of items
      TreeSet sortedSet = new TreeSet();
      for( int i=0; i<ante_items.length; i++ )
        sortedSet.add( ante_items[i] );
      Iterator sortedI = sortedSet.iterator();
      while( sortedI.hasNext() )
        System.out.print( + " " );
      Itemset consequent = rule.getConsequent();
      Object[] cons_items = consequent.getItems();
      System.out.println( "==> " + cons_items[0] + " (support=" +
          rule.getSupport() + ", confidence=" + rule.getConfidence() + ")" );