0

I have created a MySQL database with entries similar to nurse roster, Now i need to send this data to optaplanner deployed on my server. To which file do i need to send it in the optaplanner folder deployed on server to get the results displayed on my webpage.

I'm using Xstream to generate XML file.

Can any one please give me brief on how to make this functionality work and get me the desired results.

Angular zeus
  • 23
  • 1
  • 9

4 Answers4

1

The whole dataset serialization from and to XML is part of optaplanner-examples: OptaPlanner itself doesn't provide or require any serialization format. That being said, optaplanner-examples includes the following serialization formats:

Every example: XStream XML format in data directories unsolved and solved. The format is defined by the XStream annotations (@XStreamAlias etc) on the domain classes. In some cases the XML format is too verbose, causing OutOfMemoryError, for example for the big MachineReassignment B datasets. Most examples: Competition specific TXT format in data directories import and export. The format is defined by the competition (see docs). In the examples GUI, click on button Import to load them.

Kshitij Kulshrestha
  • 2,032
  • 1
  • 20
  • 27
  • Iam actually passing the data from my web page which is connected to MySQL database and optaplanner is deployed on a application server. so from my webpage where exactly is the location i have to pass my input file in the application deployed. Please help me out with ur valuable suggestions – Angular zeus Nov 05 '14 at 06:16
  • 1
    This answer is a direct copy from an answer at http://stackoverflow.com/questions/25636540/loading-real-world-xml-problems-in-optaplanner-6-1-0 – BradHards Jan 03 '17 at 22:37
0

I suggested you to read the final chapter in optaplanner manual / documentation :

Chapter 15. Integration

If your data source is a database, you can annotate your domain POJO's with JPA annotations. I think it will be a waste if you still store the data from database to xml file then feed the xml file to optaplanner, it will be more wise to feed your POJO objects to optaplanner directly. I don't know what your web application technology, but the general algorithm will be like this :

  1. Get POJO object data from your database (you can use JPA etc.)
  2. Construct the solution class object
  3. Feed the solution object to optaplanner solver
  4. Get the best solution from optaplanner solver and present it to your user in your user desire.

Take a look at CloudBalancingHelloWorld.java class to get the basic idea. Hope this can help you.

the.wizard
  • 1,079
  • 1
  • 9
  • 25
  • Thanks for your response, i would really appreciate if u can provide me some example for step one and step two. Am just a beginner. – Angular zeus Nov 06 '14 at 08:56
  • What is the web technology you're using right now? Can you post your simple code to load some data from database to your web page? Maybe after that I can help you to put the optaplanner part correctly. – the.wizard Nov 06 '14 at 09:13
  • above i have posted code, im using jsp for web pages and on my web page when a admin clicks on schedule employees button it has to pass the data from MySQL database to optaplanner and get the results and display it on the webpage – Angular zeus Nov 06 '14 at 23:04
0

package com.jdbcxml;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.w3c.dom.Document;



class EmployeeDAO
{
    private Connection conn = null;
    
    static
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public EmployeeDAO()
    {
        String url = "jdbc:mysql://50.62.23.184:3306/gtuser";
        String userId = "gtuser1";
        String passWord = "";
        try
        {
            conn = DriverManager.getConnection(url, userId, passWord);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public void finalize()
    {
        try
        {
            conn.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public Document getCustomerList()
    {
        Document doc = null;

        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * from t7_users");

            doc = JDBCUtil.toDocument(rs);   

            rs.close();
            stmt.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return doc;
    }
    
 public String getCustomerListAsString()
 {
  String xml = null;

  try
  {
   Statement stmt = conn.createStatement();
   ResultSet rs = stmt.executeQuery("SELECT * from t7_users");

            xml = JDBCUtil.toXML(rs);

   rs.close();
   stmt.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }

  return xml;
 }  
   
    public static void main(String argv[]) throws Exception
    { 
        EmployeeDAO dao = new EmployeeDAO(); 
        
  String xml = dao.getCustomerListAsString();
  System.out.println(xml);
  
        Document doc = dao.getCustomerList();
        System.out.println(doc);
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //out.write(doc);;
        //out.close();
        
        
    }
}
Angular zeus
  • 23
  • 1
  • 9
0

Here the pseudo code (I never actually use JSP, I currently using GWT) to give you the basic idea, but please do remember these notes :

  1. I think it will be a waste to save your POJO objects to xml then use XStream library to extract it again to POJO objects. In optaplanner example, they use it because it only need a static data and for the sake of example.
  2. I assume that you already create your approriate domain class model that fit your planning problem domain. Because this is one of the core concept of optaplanner.
  3. In method generateCustomerRoster, you should put your own logic to convert your customer POJO objects to planning solution object.

Hope this can help you and lead you to finish your job. Thanks & Regards.

package com.jdbcxml;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.w3c.dom.Document;

public class EmployeeDAO
{
    private Connection conn = null;
    
    static
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public EmployeeDAO()
    {
        String url = "jdbc:mysql://50.62.23.184:3306/gtuser";
        String userId = "gtuser1";
        String passWord = "";
        try
        {
            conn = DriverManager.getConnection(url, userId, passWord);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public void finalize()
    {
        try
        {
            conn.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public List<Customer> getCustomerList()
    {
        Document doc = null;

        try
        {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * from t7_users");

            doc = JDBCUtil.toDocument(rs);   

            rs.close();
            stmt.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return doc;
    }

    public CustomerRoster generateCustomerRoster(List<Customer> rawData) {
        CustomerRoster result = new CustomerRoster();
        
        // here you should write your logic to generate Customer Roster data from your Raw Data (Customer) 
        
        return result;
    }
   
    public static void main(String argv[]) throws Exception
    { 
        // Build the Solver
        SolverFactory solverFactory = SolverFactory.createFromXmlResource("yourSolverConfig.xml");
        Solver solver = solverFactory.buildSolver();

        // Load your problem 
        EmployeeDAO dao = new EmployeeDAO(); 
        List<Customer> listCustomer = dao.getCustomerList();
        CustomerRoster unsolvedCustomerRoster = generateCustomerRoster(listCustomer); 

        // Solve the problem
        solver.solve(unsolvedCustomerRoster);
        CustomerRoster solvedCustomerRoster = (CustomerRoster) solver.getBestSolution();

        // Display the result
        DataGrid grid = new DataGrid(solvedCustomerRoster); // Just change this line code to display to any of your view component
    }
}
the.wizard
  • 1,079
  • 1
  • 9
  • 25
  • Thank you so much for using your valuable time to help me...... will implement it and get back if i face any issues. – Angular zeus Nov 07 '14 at 03:03
  • I'm not able to pass the data in doc variable it says " The method readSolution(File) in the type AbstractXmlSolutionImporter is not applicable for the arguments (URL)" do i need to use Rest service if i need to pass data which is stored in MySQL database .... any suggestions will be great...... spent all day trying to pass data(MySQL data) to nurse rostering in optaplanner-examples from my optaplanner-webexamples folder – Angular zeus Nov 07 '14 at 07:35
  • I think you still don't get the idea how to use optaplanner correctly, now let me state it one more time : You don't need to pass the data to xml file, the optaplanner examples does it because it just need a static data source, which is store in xml file, and they use Xstream library to convert from xml to object and vice versa. To put in one word, in optaplanner examples, the datasource is xml file, while in your case, is mysql database. Just skip the part of xml things, and focus on creating your problem domain objects and pass it to optaplanner solver object. – the.wizard Nov 07 '14 at 08:05
  • I'm getting what u are saying, I'm developing similar webapp like vrp in optaplanner- webexamples..,, my loader and solve Java classes are in webapp I need to now send this static data to solver in optaplanner rite is that what u think – Angular zeus Nov 07 '14 at 09:08
  • OK, you should have told me from the beginning that you want to create something like in webexamples. I actually haven't touch the web examples, I just run the desktop examples. Give me some time to learn it, I will get back to you when I have the answer. – the.wizard Nov 07 '14 at 09:32