All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.gui.scripting.Script Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This is the stable version. Apart from bugfixes, this version does not receive any other updates.

There is a newer version: 3.8.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 * Script.java
 * Copyright (C) 2009-2012 University of Waikato, Hamilton, New Zealand
 */

package weka.gui.scripting;

import java.io.File;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.SerializedObject;
import weka.core.Utils;
import weka.core.WekaException;
import weka.gui.ExtensionFileFilter;
import weka.gui.scripting.event.ScriptExecutionEvent;
import weka.gui.scripting.event.ScriptExecutionEvent.Type;
import weka.gui.scripting.event.ScriptExecutionListener;

/**
 * A simple helper class for loading, saving scripts.
 * 
 * @author fracpete (fracpete at waikato dot ac dot nz)
 * @version $Revision: 10222 $
 */
public abstract class Script implements OptionHandler, Serializable {

  /** for serialization. */
  private static final long serialVersionUID = 5053328052680586401L;

  /**
   * The Thread for running a script.
   * 
   * @author fracpete (fracpete at waikato dot ac dot nz)
   * @version $Revision: 10222 $
   */
  public abstract static class ScriptThread extends Thread {

    /** the owning script. */
    protected Script m_Owner;

    /** commandline arguments. */
    protected String[] m_Args;

    /** whether the thread was stopped. */
    protected boolean m_Stopped;

    /**
     * Initializes the thread.
     * 
     * @param owner the owning script
     * @param args the commandline arguments
     */
    public ScriptThread(Script owner, String[] args) {
      super();

      m_Owner = owner;
      m_Args = args.clone();
    }

    /**
     * Returns the owner.
     * 
     * @return the owning script
     */
    public Script getOwner() {
      return m_Owner;
    }

    /**
     * Returns the commandline args.
     * 
     * @return the arguments
     */
    public String[] getArgs() {
      return m_Args;
    }

    /**
     * Performs the actual run.
     */
    protected abstract void doRun();

    /**
     * Executes the script.
     */
    @Override
    public void run() {
      m_Stopped = false;

      getOwner().notifyScriptFinishedListeners(
        new ScriptExecutionEvent(m_Owner, Type.STARTED));
      try {
        doRun();
        if (!m_Stopped) {
          getOwner().notifyScriptFinishedListeners(
            new ScriptExecutionEvent(m_Owner, Type.FINISHED));
        }
      } catch (Exception e) {
        e.printStackTrace();
        getOwner().notifyScriptFinishedListeners(
          new ScriptExecutionEvent(m_Owner, Type.ERROR, e));
      }
      getOwner().m_ScriptThread = null;
    }

    /**
     * Stops the script execution.
     */
    @SuppressWarnings("deprecation")
    public void stopScript() {
      if (isAlive()) {
        m_Stopped = true;
        try {
          stop();
        } catch (Exception e) {
          // ignored
        }
      }
    }
  }

  /** the backup extension. */
  public final static String BACKUP_EXTENSION = ".bak";

  /** the document this script is a wrapper around. */
  protected Document m_Document;

  /** the filename of the script. */
  protected File m_Filename;

  /** the newline used on this platform. */
  protected String m_NewLine;

  /** whether the script is modified. */
  protected boolean m_Modified;

  /** the current script thread. */
  protected transient ScriptThread m_ScriptThread;

  /** optional listeners when the script finishes. */
  protected HashSet m_FinishedListeners;

  /**
   * Initializes the script.
   */
  public Script() {
    this(null);
  }

  /**
   * Initializes the script.
   * 
   * @param doc the document to use as basis
   */
  public Script(Document doc) {
    this(doc, null);
  }

  /**
   * Initializes the script. Automatically loads the specified file, if not
   * null.
   * 
   * @param doc the document to use as basis
   * @param file the file to load (if not null)
   */
  public Script(Document doc, File file) {
    initialize();

    m_Document = doc;

    if (m_Document != null) {
      m_Document.addDocumentListener(new DocumentListener() {
        @Override
        public void changedUpdate(DocumentEvent e) {
          m_Modified = true;
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
          m_Modified = true;
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
          m_Modified = true;
        }
      });
    }

    if (file != null) {
      open(file);
    }
  }

  /**
   * Initializes the script.
   */
  protected void initialize() {
    m_Filename = null;
    m_NewLine = System.getProperty("line.separator");
    m_Modified = false;
    m_ScriptThread = null;
    m_FinishedListeners = new HashSet();
  }

  /**
   * Returns an enumeration describing the available options.
   * 
   * @return an enumeration of all the available options
   */
  @Override
  public Enumeration




© 2015 - 2024 Weber Informatics LLC | Privacy Policy