source: src/main/java/weka/gui/scripting/JythonScript.java @ 20

Last change on this file since 20 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 4.8 KB
RevLine 
[4]1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * JythonScript.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui.scripting;
23
24import weka.core.Utils;
25import weka.core.scripting.Jython;
26import weka.gui.ExtensionFileFilter;
27
28import java.io.File;
29
30import javax.swing.text.Document;
31
32/**
33 * Represents a <a href="http://www.jython.org/" target="_blank">Jython</a> script.
34 *
35 * @author  fracpete (fracpete at waikato dot ac dot nz)
36 * @version $Revision: 5142 $
37 */
38public class JythonScript
39  extends Script {
40 
41  /** for serialization. */
42  private static final long serialVersionUID = 3469648507172973169L;
43
44  /**
45   * Executes a Jython script in a thread.
46   *
47   * @author  fracpete (fracpete at waikato dot ac dot nz)
48   * @version $Revision: 5142 $
49   */
50  public static class JythonThread
51    extends ScriptThread {
52   
53    /**
54     * Initializes the thread.
55     *
56     * @param owner     the owning script
57     * @param args      the commandline arguments
58     */
59    public JythonThread(Script owner, String[] args) {
60      super(owner, args);
61    }
62
63    /**
64     * Performs the actual run.
65     */
66    protected void doRun() {
67      Jython    jython;
68      Class[]   classes;
69      Object[]  params;
70      String    argv;
71      String    arg;
72      int       i;
73     
74      classes = new Class[]{String.class};
75      params  = new Object[]{m_Owner.getFilename().getPath()};
76      argv    = "sys.argv = ['" + Utils.backQuoteChars(m_Owner.getFilename().getPath()) + "'";
77      for (i = 0; i < getArgs().length; i++) {
78        arg   = Utils.backQuoteChars(getArgs()[i]);
79        argv += ", '" + arg + "'";
80      }
81      argv   += "]";
82     
83      jython  = new Jython();
84     
85      // set commandline parameters
86      jython.invoke("exec", new Class[]{String.class}, new Object[]{"import sys"});
87      jython.invoke("exec", new Class[]{String.class}, new Object[]{argv});
88     
89      jython.invoke("execfile", classes, params);
90    }
91  }
92 
93  /**
94   * Initializes the script.
95   */
96  public JythonScript() {
97    super();
98  }
99 
100  /**
101   * Initializes the script.
102   *
103   * @param doc         the document to use as basis
104   */
105  public JythonScript(Document doc) {
106    super(doc);
107  }
108 
109  /**
110   * Initializes the script. Automatically loads the specified file, if not
111   * null.
112   *
113   * @param doc         the document to use as basis
114   * @param file        the file to load (if not null)
115   */
116  public JythonScript(Document doc, File file) {
117    super(doc, file);
118  }
119 
120  /**
121   * Returns the extension filters for this type of script.
122   *
123   * @return            the filters
124   */
125  public ExtensionFileFilter[] getFilters() {
126    ExtensionFileFilter[]       result;
127   
128    result = new ExtensionFileFilter[1];
129    result[0] = new ExtensionFileFilter(getDefaultExtension(), "Jython script (*" + getDefaultExtension() + ")");
130   
131    return result;
132  }
133 
134  /**
135   * Returns the default extension. Gets automatically added to files if
136   * their name doesn't end with this.
137   *
138   * @return            the default extension (incl. the dot)
139   */
140  public String getDefaultExtension() {
141    return ".py";
142  }
143
144  /**
145   * Returns whether scripts can be executed, i.e., Jython is present.
146   *
147   * @return            true if scripts can be executed
148   */
149  protected boolean canExecuteScripts() {
150    return Jython.isPresent();
151  }
152 
153  /**
154   * Performs pre-execution checks.
155   * <p/>
156   * This method checks whether Jython is available (throws an exception if not).
157   *
158   * @param args        optional commandline arguments
159   * @throws Exception  if checks fail
160   */
161  protected void preCheck(String[] args) throws Exception {
162    super.preCheck(args);
163   
164    if (!Jython.isPresent())
165      throw new Exception("Jython classes are not present in CLASSPATH!");
166  }
167
168  /**
169   * Returns a new thread to execute.
170   *
171   * @param args        optional commandline arguments
172   * @return            the new thread object
173   */
174  public ScriptThread newThread(String[] args) {
175    return new JythonThread(this, args);
176  }
177 
178  /**
179   * Runs the script from commandline. Use "-h" to list all options.
180   *
181   * @param args        the commandline arguments
182   * @throws Exception  if execution fails
183   */
184  public static void main(String[] args) throws Exception {
185    runScript(new JythonScript(), args);
186  }
187}
Note: See TracBrowser for help on using the repository browser.