source: branches/MetisMQI/src/main/java/weka/gui/scripting/GroovyScript.java @ 37

Last change on this file since 37 was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.0 KB
Line 
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 * GroovyScript.java
19 * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.gui.scripting;
23
24import weka.core.scripting.Groovy;
25import weka.gui.ExtensionFileFilter;
26
27import java.io.File;
28
29import javax.swing.text.Document;
30
31/**
32 * Represents a <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a> script.
33 *
34 * @author  fracpete (fracpete at waikato dot ac dot nz)
35 * @version $Revision: 5142 $
36 */
37public class GroovyScript
38  extends Script {
39 
40  /** for serialization. */
41  private static final long serialVersionUID = -3708517162415549420L;
42
43  /**
44   * Executes a Groovy script in a thread.
45   *
46   * @author  fracpete (fracpete at waikato dot ac dot nz)
47   * @version $Revision: 5142 $
48   */
49  public static class GroovyThread
50    extends ScriptThread {
51   
52    /**
53     * Initializes the thread.
54     *
55     * @param owner     the owning script
56     * @param args      the commandline arguments
57     */
58    public GroovyThread(Script owner, String[] args) {
59      super(owner, args);
60    }
61
62    /**
63     * Tests whether the groovy object has a certain method.
64     *
65     * @param groovy    the Groovy object to inspect
66     * @param name      the method to look for
67     * @return          true if the object has the given method
68     */
69    protected boolean hasMethod(Object groovy, String name) {
70      boolean   result;
71      try {
72        groovy.getClass().getMethod(name, new Class[]{String[].class});
73        result = true;
74      }
75      catch (Exception e) {
76        result = false;
77      }
78     
79      return result;
80    }
81   
82    /**
83     * Performs the actual run.
84     */
85    protected void doRun() {
86      Object    groovy;
87     
88      groovy = Groovy.newInstance(m_Owner.getFilename(), Object.class);
89      if (hasMethod(groovy, "run"))
90        Groovy.invoke(groovy, "run", new Class[]{String[].class}, new Object[]{getArgs()});
91      else if (hasMethod(groovy, "main"))
92        Groovy.invoke(groovy, "main", new Class[]{String[].class}, new Object[]{getArgs()});
93      else
94        throw new IllegalStateException("Neither 'run' nor 'main' method found!");
95    }
96  }
97 
98  /**
99   * Initializes the script.
100   */
101  public GroovyScript() {
102    super();
103  }
104 
105  /**
106   * Initializes the script.
107   *
108   * @param doc         the document to use as basis
109   */
110  public GroovyScript(Document doc) {
111    super(doc);
112  }
113 
114  /**
115   * Initializes the script. Automatically loads the specified file, if not
116   * null.
117   *
118   * @param doc         the document to use as basis
119   * @param file        the file to load (if not null)
120   */
121  public GroovyScript(Document doc, File file) {
122    super(doc, file);
123  }
124 
125  /**
126   * Returns the extension filters for this type of script.
127   *
128   * @return            the filters
129   */
130  public ExtensionFileFilter[] getFilters() {
131    ExtensionFileFilter[]       result;
132   
133    result = new ExtensionFileFilter[1];
134    result[0] = new ExtensionFileFilter(getDefaultExtension(), "Groovy script (*" + getDefaultExtension() + ")");
135   
136    return result;
137  }
138 
139  /**
140   * Returns the default extension. Gets automatically added to files if
141   * their name doesn't end with this.
142   *
143   * @return            the default extension (incl. the dot)
144   */
145  public String getDefaultExtension() {
146    return ".groovy";
147  }
148
149  /**
150   * Returns whether scripts can be executed, i.e., Groovy is present.
151   *
152   * @return            true if scripts can be executed
153   */
154  protected boolean canExecuteScripts() {
155    return Groovy.isPresent();
156  }
157 
158  /**
159   * Performs pre-execution checks.
160   * <p/>
161   * This method checks whether Groovy is available (throws an exception if not).
162   *
163   * @param args        optional commandline arguments
164   * @throws Exception  if checks fail
165   */
166  protected void preCheck(String[] args) throws Exception {
167    super.preCheck(args);
168   
169    if (!Groovy.isPresent())
170      throw new Exception("Groovy classes are not present in CLASSPATH!");
171  }
172
173  /**
174   * Returns a new thread to execute.
175   *
176   * @param args        optional commandline arguments
177   * @return            the new thread object
178   */
179  public ScriptThread newThread(String[] args) {
180    return new GroovyThread(this, args);
181  }
182 
183  /**
184   * Runs the script from commandline. Use "-h" to list all options.
185   *
186   * @param args        the commandline arguments
187   * @throws Exception  if execution fails
188   */
189  public static void main(String[] args) throws Exception {
190    runScript(new GroovyScript(), args);
191  }
192}
Note: See TracBrowser for help on using the repository browser.