// Copyright (c) 2001, 2004, Oracle Corporation.  All rights reserved.  
// File: PriorsDemo.java

/**
 * This sample program describes how to use the ODM Java API to provide a prior 
 * probability to build a classification model using the NaiveBayes(NB) algorithm.
 * This program uses the "MINING_DATA_BUILD_V" dataset for building a
 * model to predict which customer should receive an "AFFINITY_CARD".
 * It then applies the built model on "MINING_DATA_APPLY_V" dataset to predict 
 * which customers must receive "AFFINITY_CARD".  We will compare apply results
 * on a built model with the prior probability and a built model without the  
 * prior probability.
 * 
 * NOTE:
 *   Before executing this program you need to provide DB URL, username 
 *   and password in login method and a valid data schema name
 *   by changing the DATA_SCHEMA_NAME constant value in the program.
 */

// Generic Java Imports
import java.io.*;
import java.util.*;
import java.sql.*;

// ODM Imports
import oracle.dmt.odm.*;
import oracle.dmt.odm.data.*;
import oracle.dmt.odm.settings.function.*;
import oracle.dmt.odm.settings.algorithm.*;
import oracle.dmt.odm.task.*;
import oracle.dmt.odm.model.*;
import oracle.dmt.odm.result.*;

public class PriorsDemo extends Object {

  private static DataMiningServer m_dms = null;
  private static oracle.dmt.odm.Connection m_dmsConn = null;
  //provide schema name that has the data tables/views
  private static final String DATA_SCHEMA_NAME = "schema name";
  public static void main( String args[] ) { 
    try {      
      //1. Login to the Data Mining Server
          //Create a DMS object
          m_dms = new DataMiningServer(
            "put DB URL here", //JDBC URL jdbc:oracle:thin:@<Host name>:<Port>:<SID>
            "user name", //User Name
            "password" //Password 
          );
          //Login to the DMS and create a DMS Connection
          m_dmsConn = m_dms.login();
          
          //Remove the objects with prior probability to avoid name conflicts
          cleanupWithPriorProbability();
      //2. Build a model with prior probability
      buildModelWithPriorProbability();
      //3. Apply the model with prior probability
      applyModelWithPriorProbability();
      
          //Remove the objects without prior probability to avoid name conflicts
          cleanup();
      //4. Build a model without prior probability
      buildModel();
      //5. Apply the model without prior probability
      applyModel();
      
    } catch(Exception anyExp) {
      anyExp.printStackTrace(System.out);
    } finally {
      try {
      //6. Logout from the Data Mining Server
      m_dms.logout(m_dmsConn);
      } catch(Exception anyExp1) { }//Ignore
    }
  }

  /**
   * To build a mining model with prior probability, the build dataset location 
   * details and the mining function settings are required. 
   * In this example, we will build a classification model with prior probability 
   * using the build dataset "MINING_DATA_BUILD_V" in the user schema, with the 
   * NaiveBayes algorithm. This model can be used to predict which customer 
   * should receive an "AFFINITY_CARD". After completing the build task, the 
   * model named "PriorsDemo_Model" will be created in the DMS.
   */
  public static void buildModelWithPriorProbability() throws ODMException, SQLException, IOException 
  {
    //1. Create PhysicalDataSpecification
        LocationAccessData lad = new LocationAccessData( 
                                       "MINING_DATA_BUILD_V", //Build Table Name
                                       DATA_SCHEMA_NAME //Schema Name
                                     );
        PhysicalDataSpecification pds = 
          new NonTransactionalDataSpecification( lad );
    //2. Create & Store Mining Function Settings
        NaiveBayesSettings nbAlgo = new NaiveBayesSettings(0.01f, 0.01f);
        ClassificationFunctionSettings mfs =
          ClassificationFunctionSettings.create(
                   m_dmsConn,       //DMS Connection
                   nbAlgo,          //NB algorithm settings
                   pds,             //Build data specification
                   "AFFINITY_CARD", //Target column
                   AttributeType.categorical, //Default attribute type
                   DataPreparationStatus.unprepared //Default preparation status
                   );
        //Set inactive attributes by changing the default attribute usage
        mfs.adjustAttributeUsage(new String[]{"CUST_ID"},AttributeUsage.inactive);
        // Set Prior Probability
        // Prior Probability is used in a classification problem. It enables the 
        // user to let algorithm knows what relative values of various classes 
        // are to the user.  For example, suppose a problem is to predict whether 
        // a user will respond to a promotional mailing, "AFFINITY_CARD".  The 
        // target has two classes: YES (1) and NO (0). Suppose a predicts of a 
        // probability of YES is 0.05, and  predicts of a probability of NO is 
        // 1 - 0.05 = 0.95.
        //
        // For example to represent the above prior probability
        //
        // target   probability
        // 0      | 0.95   
        // 1      | 0.05
        //
        // In the above case, there are very few positive (response) records, 
        // where Prior Probability will be 0.05 for 5-95 case.

        // Define a list of categories
        Category negativeCat = new Category("negativeResponse", "0", DataType.intType);
        Category positiveCat = new Category("positiveResponse", "1", DataType.intType);  
        // Define a Prior Probability 
        // AddEntry( Target Category, Probability Value)
        PriorProbabilities priorProbability = new PriorProbabilities();
        // Row 1
        priorProbability.addEntry(negativeCat, new Float("0.95"));
        // Row 2
        priorProbability.addEntry(positiveCat, new Float("0.05"));       
        // Add Prior Probability 
        if (priorProbability != null) {
          mfs.setPriors(priorProbability);
        }
        mfs.store( m_dmsConn, "PriorsDemo_MFS");
    //3. Create, store & execute Build Task
        MiningBuildTask buildTask = new MiningBuildTask(
                     pds, //Build data specification
                     "PriorsDemo_MFS", //Mining function settings name
                     "PriorsDemo_Model" //Mining model name
                     );
        executeTask(buildTask, "PriorsDemo_BuildTask" );
  }

  /**
   * To build a mining model without prior probability, the build dataset 
   * location details and the mining function settings are required. 
   * In this example, we will build a classification model using the 
   * build dataset "MINING_DATA_BUILD_V" in the user schema, with the NaiveBayes 
   * algorithm. This model can be used to predict which customer should 
   * receive an "AFFINITY_CARD". After completing the build task, the model 
   * named "PriorsDemo_Model" will be created in the DMS.
   */
  public static void buildModel() throws ODMException, SQLException, IOException 
  {
    //1. Create PhysicalDataSpecification
        LocationAccessData lad = new LocationAccessData( 
                                       "MINING_DATA_BUILD_V", //Build Table Name
                                       DATA_SCHEMA_NAME //Schema Name
                                     );
        PhysicalDataSpecification pds = 
          new NonTransactionalDataSpecification( lad );
    //2. Create & Store Mining Function Settings
        NaiveBayesSettings nbAlgo = new NaiveBayesSettings(0.01f, 0.01f);
        ClassificationFunctionSettings mfs =
          ClassificationFunctionSettings.create(
                   m_dmsConn,       //DMS Connection
                   nbAlgo,          //NB algorithm settings
                   pds,             //Build data specification
                   "AFFINITY_CARD", //Target column
                   AttributeType.categorical, //Default attribute type
                   DataPreparationStatus.unprepared //Default preparation status
                   );
        //Set inactive attributes by changing the default attribute usage
        mfs.adjustAttributeUsage(new String[]{"CUST_ID"},AttributeUsage.inactive);
        mfs.store( m_dmsConn, "NoPriorsDemo_MFS");
    //3. Create, store & execute Build Task
        MiningBuildTask buildTask = new MiningBuildTask(
                     pds, //Build data specification
                     "NoPriorsDemo_MFS", //Mining function settings name
                     "NoPriorsDemo_Model" //Mining model name
                     );
        executeTask(buildTask, "NoPriorsDemo_BuildTask" );
  }
  
  /**
   * This method stores the given task with the specified name in the DMS 
   * and submits the task for asynchronous execution in the DMS. After 
   * completing the task successfully it returns true. If there is a task 
   * failure, then it prints error description and returns false.
   * 
   * @param taskObj task object
   * @param taskName name of the task
   * 
   * @return boolean returns true when the task is successful
   */
  public static boolean executeTask(MiningTask taskObj, String taskName)
    throws ODMException, SQLException {
    boolean isTaskSuccess = false;
    taskObj.store(m_dmsConn, taskName); //Store the task
    taskObj.execute(m_dmsConn); //Submit the task for asynchronous execution
    System.out.print(taskName + " is started, please wait. ");
    //Wait for completion of the task
    MiningTaskStatus taskStatus = taskObj.waitForCompletion(m_dmsConn);
    //Check the status of the task after completion
    isTaskSuccess = taskStatus.getTaskState().equals(MiningTaskState.success);
    if( isTaskSuccess ) {
      //Task completed successfully
      System.out.println("It is successful.");
    } else {//Task failed
      throw new ODMException("It is failed.\nFailure Description: " + 
        taskStatus.getStateDescription() );
    }
    return isTaskSuccess;
  }
  /**
   * To apply a mining model with prior probability to a data, apply dataset 
   * location details, input model and the output table location details are 
   * required. 
   * In this example, we apply the PriorsDemo_Model to the "MINING_DATA_APPLY_V"  
   * dataset to predict which customers receive an "AFFINITY_CARD".
   * After completing the apply task, PriorsDemo_ApplyResults object will be  
   * created in the DMS and an apply output table will be created at the  
   * specfied location.
   */
  public static void applyModelWithPriorProbability() 
    throws ODMException, SQLException, IOException 
  {
    //1. Create PhysicalDataSpecification
        LocationAccessData lad = new LocationAccessData( 
                                 "MINING_DATA_APPLY_V", //Apply table/view Name
                                 DATA_SCHEMA_NAME //Schema Name
                               );
        PhysicalDataSpecification pds = 
          new NonTransactionalDataSpecification( lad );
    //2. Create MiningApplyOutput & apply output table/view details
        // Create MiningApplyOutput object
        MiningApplyOutput mao = MiningApplyOutput.createDefault();
        // Add all the source attributes to be returned with the scored result.
        // In this example, attribute "CUST_ID" from the source table will be 
        // returned with the result aliased as "CUST_ID"
        MiningAttribute sourceAttribute =
                  new MiningAttribute("CUST_ID", DataType.intType,
                                      AttributeType.notApplicable);
        Attribute destinationAttribute = new Attribute( "CUST_ID", 
                                                        DataType.intType);

        ApplySourceAttributeItem m_ApplySourceAttributeItem =
           new ApplySourceAttributeItem(sourceAttribute,destinationAttribute);
        // Add the ApplySourceAttributeItem object to the MiningApplyOutput
        mao.addItem(m_ApplySourceAttributeItem);
        //Output table location details
        LocationAccessData outputTable =
                 new LocationAccessData("PriorsDemo_Apply_Output_Table", 
                                        DATA_SCHEMA_NAME);      
    //3. Create, store & execute apply Task
        MiningApplyTask applyTask = new MiningApplyTask(
                     pds, //test data specification
                     "PriorsDemo_Model", //Input model name
                     mao, //MiningApplyOutput object
                     outputTable, //Apply output table location details
                     "PriorsDemo_ApplyResults" //Apply results name
                     );
        //Store & execute the task
        boolean isTaskSuccess = executeTask(applyTask, "PriorsDemo_ApplyTask"); 
        if( isTaskSuccess ) {
          //Restore & display apply results
          MiningApplyResult applyResult = 
            MiningApplyResult.restore(m_dmsConn, "PriorsDemo_ApplyResults");
          System.out.println(applyResult.toString());
          executeQuery(m_dmsConn.getDBConnection(),
            "SELECT CUST_ID, MAO_PREDICTION, MAO_PROBABILITY " +
            "FROM PriorsDemo_Apply_Output_Table " +
            "WHERE CUST_ID < 100010 ORDER BY CUST_ID");
        }
  }
  
  /**
   * To apply a mining model without prior probability to a data, apply dataset 
   * location details, input model and the output table location details are 
   * required. 
   * In this example, we apply the PriorsDemo_Model to the "MINING_DATA_APPLY_V"  
   * dataset to predict which customers receive an "AFFINITY_CARD".
   * After completing the apply task, PriorsDemo_ApplyResults object will be  
   * created in the DMS and an apply output table will be created at the  
   * specfied location.
   */
  public static void applyModel() throws ODMException, SQLException, IOException 
  {
    //1. Create PhysicalDataSpecification
        LocationAccessData lad = new LocationAccessData( 
                                 "MINING_DATA_APPLY_V", //Apply table/view Name
                                 DATA_SCHEMA_NAME //Schema Name
                               );
        PhysicalDataSpecification pds = 
          new NonTransactionalDataSpecification( lad );
    //2. Create MiningApplyOutput & apply output table/view details
        // Create MiningApplyOutput object
        MiningApplyOutput mao = MiningApplyOutput.createDefault();
        // Add all the source attributes to be returned with the scored result.
        // In this example, attribute "CUST_ID" from the source table will be 
        // returned with the result aliased as "CUST_ID"
        MiningAttribute sourceAttribute =
                  new MiningAttribute("CUST_ID", DataType.intType,
                                      AttributeType.notApplicable);
        Attribute destinationAttribute = new Attribute("CUST_ID", DataType.intType);

        ApplySourceAttributeItem m_ApplySourceAttributeItem =
           new ApplySourceAttributeItem(sourceAttribute,destinationAttribute);
        // Add the ApplySourceAttributeItem object to the MiningApplyOutput
        mao.addItem(m_ApplySourceAttributeItem);
        //Output table location details
        LocationAccessData outputTable =
                 new LocationAccessData("NoPriorsDemo_ApplyOutput_Table", DATA_SCHEMA_NAME);      
    //3. Create, store & execute apply Task
        MiningApplyTask applyTask = new MiningApplyTask(
                     pds, //test data specification
                     "NoPriorsDemo_Model", //Input model name
                     mao, //MiningApplyOutput object
                     outputTable, //Apply output table location details
                     "NoPriorsDemo_ApplyResults" //Apply results name
                     );
        //Store & execute the task
        boolean isTaskSuccess = executeTask(applyTask, "NoPriorsDemo_ApplyTask"); 
        if( isTaskSuccess ) {
          //Restore & display apply results
          MiningApplyResult applyResult = 
            MiningApplyResult.restore(m_dmsConn, "NoPriorsDemo_ApplyResults");
          System.out.println(applyResult.toString());
          executeQuery(m_dmsConn.getDBConnection(),
            "SELECT CUST_ID, MAO_PREDICTION, MAO_PROBABILITY " +
            "FROM NoPriorsDemo_ApplyOutput_Table " + 
            "WHERE CUST_ID < 100010 ORDER BY CUST_ID");
        }
  }

  /**
   * Cleanup the previously created objects with prior probability if any. 
   */
  public static void cleanupWithPriorProbability () 
    throws ODMException, SQLException
  {
    //Remove the MiningFunctionSettings
    try {
      ClassificationFunctionSettings.remove(m_dmsConn, "PriorsDemo_MFS");
    }catch(Exception anyExp1) { }//Ignore
    //Remove the MiningBuildTask
    try {
      MiningBuildTask.remove(m_dmsConn, "PriorsDemo_BuildTask");
    }catch(Exception anyExp2) { }//Ignore
    //Remove the MiningModel
    try {
      MiningModel.remove(m_dmsConn, "PriorsDemo_Model");
    }catch(Exception anyExp3) { }//Ignore
    //Remove the MiningApplyTask
    try {
      MiningApplyTask.remove(m_dmsConn, "PriorsDemo_ApplyTask");
    }catch(Exception anyExp4) { }//Ignore
    //Remove the MiningApplyResult and apply output table
    try {
      MiningApplyResult.remove(m_dmsConn, "PriorsDemo_ApplyResults", true);
    }catch(Exception anyExp5) { }//Ignore    
  }
  
  /**
   * Cleanup the previously created objects without prior probability if any. 
   */
  public static void cleanup() throws ODMException, SQLException
  {
    //Remove the MiningFunctionSettings
    try {
      ClassificationFunctionSettings.remove(m_dmsConn, "NoPriorsDemo_MFS");
    }catch(Exception anyExp1) { }//Ignore
    //Remove the MiningBuildTask
    try {
      MiningBuildTask.remove(m_dmsConn, "NoPriorsDemo_BuildTask");
    }catch(Exception anyExp2) { }//Ignore
    //Remove the MiningModel
    try {
      MiningModel.remove(m_dmsConn, "NoPriorsDemo_Model");
    }catch(Exception anyExp3) { }//Ignore
    //Remove the MiningApplyTask
    try {
      MiningApplyTask.remove(m_dmsConn, "NoPriorsDemo_ApplyTask");
    }catch(Exception anyExp4) { }//Ignore
    //Remove the MiningApplyResult and apply output table
    try {
      MiningApplyResult.remove(m_dmsConn, "NoPriorsDemo_ApplyResults", true);
    }catch(Exception anyExp5) { }//Ignore    
  }

  public static void executeQuery (java.sql.Connection conn, String query) throws SQLException
  {
    Statement stmt = conn.createStatement ();
    ResultSet rset = stmt.executeQuery (query);
    try
    {
      // Iterate through the result and print the employee names
      System.out.println("Scoring result:");
      System.out.println("CUST_ID | MAO_PREDICTION | MAO_PROPABILITY");
      while (rset.next ())
        System.out.println (rset.getString (1) + " | " +
                            rset.getString (2) + " | " +
                            rset.getString (3));
    }
    finally
    {
      // Close the RseultSet
      rset.close();
      // Close the Statement
      stmt.close();
    }
  }
}
