package openminer; import java.io.*; import java.sql.*; import java.util.*; import openminer.util.db.*; import openminer.util.db.sql.*; public class MinerTask { protected int m_Task; protected DataInputStream m_DataInputStream; protected DataOutputStream m_DataOutputStream; protected Connection m_Connection; protected String m_ModelTableSchema; protected String m_ModelTableName; protected SqlStatementBuilder m_SqlBuilder; protected final static String[] COLUMNNAMES = { "modelname", "modeltype", "modelmethod","modeldefine","resultdata" }; protected class ModelDigest { public String m_Name; public String m_Type; public String m_Method; public String m_IsEmpty; } public MinerTask() { // 创建SQL语句构造 m_SqlBuilder = DatabaseProperty.getSqlStmtBuilder(); } public void connect(Connection conn) throws Exception { m_Connection = conn; // 分离Model表名字 getModelTableSchemaAndName(); // 检测Model表 checkModelTable(); } public void setIOStream(InputStream is,OutputStream os) { m_DataInputStream = new DataInputStream(is); m_DataOutputStream= new DataOutputStream(os); } /** * 执行客户请求的任务 * @throws Exception */ public void execute() throws Exception { // 读取任务ID DataInputStream dis = m_DataInputStream; while (true) { m_Task = dis.readInt(); if(m_Task == OpenMinerCommand.TASK_EXIT) break; switch (m_Task) { case OpenMinerCommand.TASK_MODEL_ADD: addModel(); break; case OpenMinerCommand.TASK_MODEL_TRAIN: trainModel(); break; case OpenMinerCommand.TASK_MODEL_USE: userModel(); break; case OpenMinerCommand.TASK_MODEL_LOOKUP: lookupModel(); break; case OpenMinerCommand.TASK_MODEL_REMOVE: removeModel(); break; case OpenMinerCommand.TASK_MODEL_LIST: listModels(); default: break; } } } /** * 向数据库中增加一个模型 * @throws Exception */ public void addModel() throws Exception { DataInputStream dis = m_DataInputStream; DataOutputStream dos = m_DataOutputStream; String[] values ={"?","?","?","?","?"}; try { String modelXML = dis.readUTF(); // 创建模型 Model newModel = new Model(); newModel.createModel(modelXML); // 构造Insert语句 String sql = m_SqlBuilder.buildInsertValueStmt( m_ModelTableSchema, m_ModelTableName, COLUMNNAMES, values); byte[] emptydata = new byte[] { 0 }; PreparedStatement pstmt = m_Connection.prepareStatement(sql); pstmt.setString(1, newModel.getModelName()); pstmt.setString(2, newModel.getModelType()); pstmt.setString(3, newModel.getModelMethod()); pstmt.setBytes(4, newModel.getModelXMLDefine().getBytes("UTF-8")); pstmt.setBytes(5, emptydata); // 执行插入语句 pstmt.execute(); // 关闭sql语句 pstmt.close(); // 返回成功的指令 dos.writeInt(OpenMinerCommand.OK); } catch (Exception e) { e.printStackTrace(); // 返回失败的指令 dos.writeInt(OpenMinerCommand.ERROR); } } public void trainModel()throws Exception { DataInputStream dis = m_DataInputStream; DataOutputStream dos = m_DataOutputStream; try { String trainModelName = dis.readUTF(); String trainXML = dis.readUTF(); Model model = lookUpModel(trainModelName); if(model == null) { dos.writeInt(OpenMinerCommand.ERROR_MODEL_NOTFOUND); return; } else { Miner miner = Miner.buildMiner(model.getMinerClassName()); miner.trainModel(m_Connection, model, trainXML); updateModelResultData(model); dos.writeInt(OpenMinerCommand.OK); } }catch(Exception e) { e.printStackTrace(); dos.writeInt(OpenMinerCommand.ERROR); } } public void userModel() throws Exception { DataInputStream dis = m_DataInputStream; DataOutputStream dos = m_DataOutputStream; try { String modelName = dis.readUTF(); String useXML = dis.readUTF(); Model model = lookUpModel(modelName); String useRet = null; if(model == null) { useRet = ""; dos.writeUTF(useRet); dos.writeInt(OpenMinerCommand.ERROR_MODEL_NOTFOUND); return; } else { Miner miner = Miner.buildMiner(model.getMinerClassName()); byte[] retBytes = miner.useModel(m_Connection,model,useXML); useRet = new String(retBytes); dos.writeUTF(useRet); dos.writeInt(OpenMinerCommand.OK); } }catch(Exception e) { e.printStackTrace(); dos.writeInt(OpenMinerCommand.ERROR); } } public void lookupModel() throws Exception{ DataInputStream dis = m_DataInputStream; DataOutputStream dos = m_DataOutputStream; try { String modelName = dis.readUTF(); Model model = lookUpModel(modelName); if(model == null) { dos.writeInt(OpenMinerCommand.ERROR_MODEL_NOTFOUND); return; } byte[] resultdata = model.getModelResultData(); int resultdataLen; if(resultdata == null) resultdataLen = 0; else resultdataLen = resultdata.length; dos.writeInt(OpenMinerCommand.OK); dos.writeUTF(model.getModelXMLDefine()); dos.writeInt(resultdataLen); dos.write(resultdata); }catch(Exception e) { e.printStackTrace(); dos.writeInt(OpenMinerCommand.ERROR); } } public void removeModel() throws Exception { DataInputStream dis = m_DataInputStream; DataOutputStream dos = m_DataOutputStream; try { String modelName = dis.readUTF(); int ret = removeModel(modelName); if(ret == 0) { dos.writeInt(OpenMinerCommand.ERROR_MODEL_NOTFOUND); } else { dos.writeInt(OpenMinerCommand.OK); } }catch(Exception e) { e.printStackTrace(); dos.writeInt(OpenMinerCommand.ERROR); } } public void listModels() throws Exception { DataOutputStream dos = m_DataOutputStream; try { List list = listAllModels(); dos.writeInt(list.size()); for(int i=0;i