⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistableobject.java

📁 JGAP是一种遗传算法和遗传规划的组成部分提供了一个Java框架。它提供了基本的遗传机制
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licensing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.util;

import java.io.*;
import org.apache.log4j.*;
import org.jgap.distr.grid.gp.*;
import org.jgap.gp.*;
import org.jgap.gp.impl.*;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.*;

/**
 * A wrapper that allows an object to be written to and read from a file.
 *
 * @author Klaus Meffert
 * @since 3.3.3
 */
public class PersistableObject {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.6 $";

  private transient Logger log = Logger.getLogger(getClass());

  private Object m_object;

  private File m_file;

  public PersistableObject(File a_file) {
    m_file = a_file;
  }

  public PersistableObject(String a_filename) {
    this(new File(a_filename));
  }

  public void setObject(Object a_object) {
    m_object = a_object;
  }

  public void save()
      throws Exception {
    save(false);
  }

  public void save(boolean a_omitConfig)
      throws Exception {
    save(a_omitConfig, null);
  }
    public void save(boolean a_omitConfig, Object[][] a_omitFields)
        throws Exception {
    JGAPGPXStream xstream = new JGAPGPXStream();
    init(xstream);
    FileOutputStream fos = new FileOutputStream(m_file);
    if (a_omitConfig) {
      xstream.omitField(GPPopulation.class, "m_config");
      xstream.omitField(GPProgram.class, "m_conf");
      xstream.omitField(GPProgramBase.class, "m_conf");
      xstream.omitField(JGAPRequestGP.class, "m_config");
      xstream.omitField(CommandGene.class, "m_configuration");
    }
    if (a_omitFields != null) {
      for (int i = 0; i < a_omitFields.length; i++) {
        Class clazz = (Class)a_omitFields[i][0];
        String fieldName = (String)a_omitFields[i][1];
        xstream.omitField(clazz, fieldName);
      }
    }
    FileWriter fw = new FileWriter(m_file);
    CompactWriter compact = new CompactWriter(fw);
    xstream.marshal(m_object, compact);
    fos.close();
  }

  public Object load()
      throws Exception {
    return load(m_file);
  }

  public Object load(File a_file) {
    log.info("Loading database for file "+a_file.getName());
    JGAPGPXStream xstream = new JGAPGPXStream();
    init(xstream);
    try {
      FileInputStream fis = new FileInputStream(a_file);
      m_object = (Object) xstream.fromXML(fis);
      fis.close();
      return m_object;
    } catch (Exception ex) {
      return null;
    }
  }

  public Object getObject() {
    return m_object;
  }

  protected void init(XStream a_xstream) {
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -