/** * Validate the file format. After this bean is deserialized, classes for XML * serialization may not be in the classpath any more. * * @param ff the current file format to validate */ public Tag validateFileFormat(Tag ff) { Tag r = ff; if (ff.getID() == BINARY) { return ff; } if (ff.getID() == KOMLV && !KOML.isPresent()) { r = null; } if (ff.getID() == XSTREAM && !XStream.isPresent()) { r = null; } return r; }
/** * Validate the file format. After this bean is deserialized, classes for * XML serialization may not be in the classpath any more. * * @param ff the current file format to validate */ public Tag validateFileFormat(Tag ff) { Tag r = ff; if (ff.getID() == BINARY) { return ff; } if (ff.getID() == KOMLV && !KOML.isPresent()) { r = null; } if (ff.getID() == XSTREAM && !XStream.isPresent()) { r = null; } return r; }
public String serializeClassifierAsJson() throws IOException { try { return XStream.serialize(trainedClassifier); } catch (Exception e) { if(e instanceof IOException) throw (IOException)e; else throw new OperationFailedUnexpectedly(e); } }
public Classifier deserializeClassifierFromJson(String xmlString) throws IOException { try { if (!StringUtils.isEmpty(classifierImpl)) { Class.forName(classifierImpl); trainedClassifier = (Classifier) XStream.deSerialize(xmlString); } } catch (Exception e) { throw new IOException(e); } return trainedClassifier; }
protected void setupFileChooser() { if (m_fileChooser == null) { m_fileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); } m_fileChooser.addChoosableFileFilter(m_binaryFilter); if (KOML.isPresent()) { m_fileChooser.addChoosableFileFilter(m_KOMLFilter); } if (XStream.isPresent()) { m_fileChooser.addChoosableFileFilter(m_XStreamFilter); } m_fileChooser.setFileFilter(m_binaryFilter); }
/** * Save a model in XStream deep object serialized XML form. * * @param saveTo the file name to save to * @param model the model to save * @param header the header of the data that was used to train the model * (optional) */ public static void saveXStream(File saveTo, Object model, Instances header) throws Exception { Vector<Object> v = new Vector<Object>(); v.add(model); if (header != null) { v.add(header); } v.trimToSize(); XStream.write(saveTo.getAbsolutePath(), v); }
/** * Helper routine to actually save the models. */ private void saveModel(String fileName, Instances trainHeader, Object model) { m_fileFormat = validateFileFormat(m_fileFormat); if (m_fileFormat == null) { // default to binary if validation fails m_fileFormat = s_fileFormatsAvailable.get(0); } try { switch (m_fileFormat.getID()) { case KOMLV: fileName = fileName + KOML.FILE_EXTENSION + FILE_EXTENSION; saveKOML(new File(fileName), model, trainHeader); break; case XSTREAM: fileName = fileName + XStream.FILE_EXTENSION + FILE_EXTENSION; saveXStream(new File(fileName), model, trainHeader); break; default: fileName = fileName + "." + FILE_EXTENSION; saveBinary(new File(fileName), model, trainHeader); break; } } catch (Exception ex) { stop(); // stop all processing System.err.println("[SerializedModelSaver] Problem saving model"); if (m_logger != null) { m_logger.logMessage("[SerializedModelSaver] " + statusMessagePrefix() + " Problem saving model"); m_logger.statusMessage(statusMessagePrefix() + "ERROR (See log for details)"); } } }
/** * Save a model in XStream deep object serialized XML form. * * @param saveTo the file name to save to * @param model the model to save * @param header the header of the data that was used to train the model (optional) */ public static void saveXStream(File saveTo, Object model, Instances header) throws Exception { Vector v = new Vector(); v.add(model); if (header != null) { v.add(header); } v.trimToSize(); XStream.write(saveTo.getAbsolutePath(), v); }
/** * Helper routine to actually save the models. */ private void saveModel(String fileName, Instances trainHeader, Object model) { m_fileFormat = validateFileFormat(m_fileFormat); if (m_fileFormat == null) { // default to binary if validation fails m_fileFormat = s_fileFormatsAvailable.get(0); } try { switch (m_fileFormat.getID()) { case KOMLV: fileName = fileName + KOML.FILE_EXTENSION + FILE_EXTENSION; saveKOML(new File(fileName), model, trainHeader); break; case XSTREAM: fileName = fileName + XStream.FILE_EXTENSION + FILE_EXTENSION; saveXStream(new File(fileName), model, trainHeader); break; default: fileName = fileName + "." + FILE_EXTENSION; saveBinary(new File(fileName), model, trainHeader); break; } } catch (Exception ex) { System.err.println(Messages.getInstance().getString( "SerializedModelSaver_SaveModel_Error_Text")); if (m_logger != null) { m_logger.logMessage(Messages.getInstance().getString( "SerializedModelSaver_SaveModel_LogMessage_Text_First") + statusMessagePrefix() + Messages.getInstance().getString( "SerializedModelSaver_SaveModel_LogMessage_Text_Second") + " : " + ex.getMessage()); m_logger.statusMessage(statusMessagePrefix() + Messages.getInstance().getString( "SerializedModelSaver_SaveModel_StatusMessage_Text")); } } }
/** * Save a model in XStream deep object serialized XML form. * * @param saveTo the file name to save to * @param model the model to save * @param header the header of the data that was used to train the model * (optional) */ public static void saveXStream(File saveTo, Object model, Instances header) throws Exception { Vector v = new Vector(); v.add(model); if (header != null) { v.add(header); } v.trimToSize(); XStream.write(saveTo.getAbsolutePath(), v); }
public void loadModel() { try { if (m_fileChooser == null) { // i.e. after de-serialization setupFileChooser(); } int returnVal = m_fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File loadFrom = m_fileChooser.getSelectedFile(); // add extension if necessary if (m_fileChooser.getFileFilter() == m_binaryFilter) { if (!loadFrom.getName().toLowerCase().endsWith("." + FILE_EXTENSION)) { loadFrom = new File(loadFrom.getParent(), loadFrom.getName() + "." + FILE_EXTENSION); } } else if (m_fileChooser.getFileFilter() == m_KOMLFilter) { if (!loadFrom.getName().toLowerCase() .endsWith(KOML.FILE_EXTENSION + FILE_EXTENSION)) { loadFrom = new File(loadFrom.getParent(), loadFrom.getName() + KOML.FILE_EXTENSION + FILE_EXTENSION); } } else if (m_fileChooser.getFileFilter() == m_XStreamFilter) { if (!loadFrom.getName().toLowerCase() .endsWith(XStream.FILE_EXTENSION + FILE_EXTENSION)) { loadFrom = new File(loadFrom.getParent(), loadFrom.getName() + XStream.FILE_EXTENSION + FILE_EXTENSION); } } loadFromFile(loadFrom); } } catch (Exception ex) { JOptionPane.showMessageDialog(Classifier.this, "Problem loading classifier.\n" + ex.getMessage(), "Load Model", JOptionPane.ERROR_MESSAGE); if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "ERROR: unable to load " + "model (see log)."); m_log.logMessage("[Classifier] " + statusMessagePrefix() + "Problem loading classifier. " + ex.getMessage()); } } }
public void saveModel() { try { if (m_fileChooser == null) { // i.e. after de-serialization setupFileChooser(); } int returnVal = m_fileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File saveTo = m_fileChooser.getSelectedFile(); String fn = saveTo.getAbsolutePath(); if (m_fileChooser.getFileFilter() == m_binaryFilter) { if (!fn.toLowerCase().endsWith("." + FILE_EXTENSION)) { fn += "." + FILE_EXTENSION; } } else if (m_fileChooser.getFileFilter() == m_KOMLFilter) { if (!fn.toLowerCase().endsWith(KOML.FILE_EXTENSION + FILE_EXTENSION)) { fn += KOML.FILE_EXTENSION + FILE_EXTENSION; } } else if (m_fileChooser.getFileFilter() == m_XStreamFilter) { if (!fn.toLowerCase().endsWith( XStream.FILE_EXTENSION + FILE_EXTENSION)) { fn += XStream.FILE_EXTENSION + FILE_EXTENSION; } } saveTo = new File(fn); // now serialize model // KOML? if ((KOML.isPresent()) && saveTo.getAbsolutePath().toLowerCase() .endsWith(KOML.FILE_EXTENSION + FILE_EXTENSION)) { SerializedModelSaver.saveKOML(saveTo, m_Classifier, (m_trainingSet != null) ? new Instances(m_trainingSet, 0) : null); /* * Vector v = new Vector(); v.add(m_Classifier); if (m_trainingSet != * null) { v.add(new Instances(m_trainingSet, 0)); } v.trimToSize(); * KOML.write(saveTo.getAbsolutePath(), v); */ } /* XStream */else if ((XStream.isPresent()) && saveTo.getAbsolutePath().toLowerCase() .endsWith(XStream.FILE_EXTENSION + FILE_EXTENSION)) { SerializedModelSaver.saveXStream(saveTo, m_Classifier, (m_trainingSet != null) ? new Instances(m_trainingSet, 0) : null); /* * Vector v = new Vector(); v.add(m_Classifier); if (m_trainingSet != * null) { v.add(new Instances(m_trainingSet, 0)); } v.trimToSize(); * XStream.write(saveTo.getAbsolutePath(), v); */ } else /* binary */{ ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream( new FileOutputStream(saveTo))); os.writeObject(m_Classifier); if (m_trainingSet != null) { Instances header = new Instances(m_trainingSet, 0); os.writeObject(header); } os.close(); } if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "Model saved."); m_log.logMessage("[Classifier] " + statusMessagePrefix() + " Saved classifier " + getCustomName()); } } } catch (Exception ex) { JOptionPane .showMessageDialog(Classifier.this, "Problem saving classifier.\n", "Save Model", JOptionPane.ERROR_MESSAGE); if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "ERROR: unable to" + " save model (see log)."); m_log.logMessage("[Classifier] " + statusMessagePrefix() + " Problem saving classifier " + getCustomName() + ex.getMessage()); } } }
/** * Load a pre-saved layout */ private void loadLayout() { m_loadB.setEnabled(false); m_saveB.setEnabled(false); m_playB.setEnabled(false); m_playBB.setEnabled(false); int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { // stopFlow(); // determine filename File oFile = m_FileChooser.getSelectedFile(); // set internal flow directory environment variable // add extension if necessary if (m_FileChooser.getFileFilter() == m_KfFilter) { if (!oFile.getName().toLowerCase().endsWith(FILE_EXTENSION)) { oFile = new File(oFile.getParent(), oFile.getName() + FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!oFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION + "kf")) { oFile = new File(oFile.getParent(), oFile.getName() + KOML.FILE_EXTENSION + "kf"); } } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!oFile.getName().toLowerCase().endsWith(FILE_EXTENSION_XML)) { oFile = new File(oFile.getParent(), oFile.getName() + FILE_EXTENSION_XML); } } else if (m_FileChooser.getFileFilter() == m_XStreamFilter) { if (!oFile.getName().toLowerCase() .endsWith(XStream.FILE_EXTENSION + "kf")) { oFile = new File(oFile.getParent(), oFile.getName() + XStream.FILE_EXTENSION + "kf"); } } String flowName = oFile.getName(); if (flowName.lastIndexOf('.') > 0) { flowName = flowName.substring(0, flowName.lastIndexOf('.')); } loadLayout(oFile, getAllowMultipleTabs()); } m_loadB.setEnabled(true); m_playB.setEnabled(true); m_playBB.setEnabled(true); m_saveB.setEnabled(true); }
/** * Serialize the layout to a file */ private void saveLayout(int tabIndex, boolean showDialog) { getBackground(); File sFile = m_mainKFPerspective.getFlowFile(tabIndex); int returnVal = JFileChooser.APPROVE_OPTION; boolean shownDialog = false; if (showDialog || sFile.getName().equals("-NONE-")) { returnVal = m_FileChooser.showSaveDialog(this); shownDialog = true; } if (returnVal == JFileChooser.APPROVE_OPTION) { // temporarily remove this panel as a property changle listener from // each bean Vector<Object> beans = BeanInstance.getBeanInstances(tabIndex); detachFromLayout(beans); // determine filename (if necessary) if (shownDialog) { sFile = m_FileChooser.getSelectedFile(); } // add extension if necessary if (m_FileChooser.getFileFilter() == m_KfFilter) { if (!sFile.getName().toLowerCase().endsWith(FILE_EXTENSION)) { sFile = new File(sFile.getParent(), sFile.getName() + FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!sFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION + "kf")) { sFile = new File(sFile.getParent(), sFile.getName() + KOML.FILE_EXTENSION + "kf"); } } else if (m_FileChooser.getFileFilter() == m_XStreamFilter) { if (!sFile.getName().toLowerCase() .endsWith(XStream.FILE_EXTENSION + "kf")) { sFile = new File(sFile.getParent(), sFile.getName() + XStream.FILE_EXTENSION + "kf"); } } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!sFile.getName().toLowerCase().endsWith(FILE_EXTENSION_XML)) { sFile = new File(sFile.getParent(), sFile.getName() + FILE_EXTENSION_XML); } } saveLayout(sFile, m_mainKFPerspective.getCurrentTabIndex(), false); m_mainKFPerspective.setFlowFile(tabIndex, sFile); } }
public void loadModel() { try { if (m_fileChooser == null) { // i.e. after de-serialization setupFileChooser(); } int returnVal = m_fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File loadFrom = m_fileChooser.getSelectedFile(); // add extension if necessary if (m_fileChooser.getFileFilter() == m_binaryFilter) { if (!loadFrom.getName().toLowerCase().endsWith("." + FILE_EXTENSION)) { loadFrom = new File(loadFrom.getParent(), loadFrom.getName() + "." + FILE_EXTENSION); } } else if (m_fileChooser.getFileFilter() == m_KOMLFilter) { if (!loadFrom.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION + FILE_EXTENSION)) { loadFrom = new File(loadFrom.getParent(), loadFrom.getName() + KOML.FILE_EXTENSION + FILE_EXTENSION); } } else if (m_fileChooser.getFileFilter() == m_XStreamFilter) { if (!loadFrom.getName().toLowerCase().endsWith(XStream.FILE_EXTENSION + FILE_EXTENSION)) { loadFrom = new File(loadFrom.getParent(), loadFrom.getName() + XStream.FILE_EXTENSION + FILE_EXTENSION); } } loadFromFile(loadFrom); } } catch (Exception ex) { JOptionPane.showMessageDialog(Classifier.this, "Problem loading classifier.\n" + ex.getMessage(), "Load Model", JOptionPane.ERROR_MESSAGE); if (m_log != null) { m_log.statusMessage(statusMessagePrefix() + "ERROR: unable to load " + "model (see log)."); m_log.logMessage("[Classifier] " + statusMessagePrefix() + "Problem loading classifier. " + ex.getMessage()); } } }
/** * Load a pre-saved layout */ private void loadLayout() { m_loadB.setEnabled(false); m_saveB.setEnabled(false); m_playB.setEnabled(false); m_playBB.setEnabled(false); int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { stopFlow(); // determine filename File oFile = m_FileChooser.getSelectedFile(); // set internal flow directory environment variable // add extension if necessary if (m_FileChooser.getFileFilter() == m_KfFilter) { if (!oFile.getName().toLowerCase().endsWith(FILE_EXTENSION)) { oFile = new File(oFile.getParent(), oFile.getName() + FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!oFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION + "kf")) { oFile = new File(oFile.getParent(), oFile.getName() + KOML.FILE_EXTENSION + "kf"); } } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!oFile.getName().toLowerCase().endsWith(FILE_EXTENSION_XML)) { oFile = new File(oFile.getParent(), oFile.getName() + FILE_EXTENSION_XML); } } else if (m_FileChooser.getFileFilter() == m_XStreamFilter) { if (!oFile.getName().toLowerCase().endsWith(XStream.FILE_EXTENSION +"kf")) { oFile = new File(oFile.getParent(), oFile.getName() + XStream.FILE_EXTENSION + "kf"); } } String flowName = oFile.getName(); if (flowName.lastIndexOf('.') > 0) { flowName = flowName.substring(0, flowName.lastIndexOf('.')); } loadLayout(oFile, getAllowMultipleTabs()); } m_loadB.setEnabled(true); m_playB.setEnabled(true); m_playBB.setEnabled(true); m_saveB.setEnabled(true); }
/** * Serialize the layout to a file */ private void saveLayout(int tabIndex, boolean showDialog) { // m_loadB.setEnabled(false); // m_saveB.setEnabled(false); java.awt.Color bckC = getBackground(); File sFile = m_mainKFPerspective.getFlowFile(tabIndex); int returnVal = JFileChooser.APPROVE_OPTION; if (showDialog || sFile.getName().equals("-NONE-")) { returnVal = m_FileChooser.showSaveDialog(this); } if (returnVal == JFileChooser.APPROVE_OPTION) { // temporarily remove this panel as a property changle listener from // each bean Vector beans = BeanInstance.getBeanInstances(tabIndex); detachFromLayout(beans); // determine filename sFile = m_FileChooser.getSelectedFile(); // add extension if necessary if (m_FileChooser.getFileFilter() == m_KfFilter) { if (!sFile.getName().toLowerCase().endsWith(FILE_EXTENSION)) { sFile = new File(sFile.getParent(), sFile.getName() + FILE_EXTENSION); } } else if (m_FileChooser.getFileFilter() == m_KOMLFilter) { if (!sFile.getName().toLowerCase().endsWith(KOML.FILE_EXTENSION + "kf")) { sFile = new File(sFile.getParent(), sFile.getName() + KOML.FILE_EXTENSION + "kf"); } } else if (m_FileChooser.getFileFilter() == m_XStreamFilter) { if (!sFile.getName().toLowerCase().endsWith(XStream.FILE_EXTENSION + "kf")) { sFile = new File(sFile.getParent(), sFile.getName() + XStream.FILE_EXTENSION + "kf"); } } else if (m_FileChooser.getFileFilter() == m_XMLFilter) { if (!sFile.getName().toLowerCase().endsWith(FILE_EXTENSION_XML)) { sFile = new File(sFile.getParent(), sFile.getName() + FILE_EXTENSION_XML); } } saveLayout(sFile, m_mainKFPerspective.getCurrentTabIndex(), false); m_mainKFPerspective.setFlowFile(tabIndex, sFile); } }