View Javadoc
1   /**
2    * @(#)ScriptEngineDriver.java
3    * Copyright (C) 2008-2011 delhezi.com
4    *
5    * This class is released under the:
6    * GNU Lesser General Public License (LGPL) version 3 or later.
7    * http://www.gnu.org/copyleft/lesser.html
8    */
9   package com.delhezi.ga.utility;
10  
11  import com.delhezi.ga.exception.GeneticAlgorithmException;
12  
13  import java.io.FileNotFoundException;
14  import java.io.IOException;
15  import java.util.logging.Level;
16  import java.util.logging.Logger;
17  import java.io.FileReader;
18  import javax.script.Invocable;
19  import javax.script.ScriptEngineManager;
20  import javax.script.ScriptException;
21  
22  //http://www.java2s.com/Code/Java/JDK-6/CatchScriptException.htm
23  /**
24   * <code>FitnessFunction</code>: Silnik skryptów.
25   * @version 1.0 2009-12-10
26   * @author <a href="mailto:wojciech.wolszczak@delhezi.com">
27   * Wojciech Wolszczak</a>
28   */
29  public class ScriptEngine {
30  
31      /** Logger object. */
32      private static final Logger LOGGER =
33          Logger.getLogger(ScriptEngine.class.getName());
34  
35      /** Delhezi Error Code. */
36      private static final String DERC = "1-9-1-";
37  
38      /** */
39      private Invocable invokeEngine;
40  
41      /** */
42      private String scriptEnginName;
43  
44      /** */
45      private String scriptPath;
46  
47      /** */
48      private String scriptFile;
49  
50  
51      /**
52       * Konstruktor.
53       * @param scriptEnginName Nazwa silnika skryptów. Przykładowe wartości:
54       *                        [js, rhino, JavaScript, javascript,
55       *                        ECMAScript, ecmascript].
56       * @param scriptPath  Bezwzględna ścieżka zakończona "/" do katalogu w
57       * którym składowane są skrytpy.
58       * @param scriptFile  Naza pliku ze skrytem.
59       * @throws GeneticAlgorithmException DERC-1-9-1-1, DERC-1-9-1-2, DERC-1-9-1-3
60       * @since 1.0
61       */
62      public ScriptEngine(final String scriptEnginName,
63                          final String scriptPath,
64                          final String scriptFile)
65      throws GeneticAlgorithmException {
66          this.scriptEnginName = scriptEnginName;
67          this.scriptPath = scriptPath;
68          this.scriptFile = scriptFile;
69  
70  
71          ScriptEngineManager manager = new ScriptEngineManager();
72          javax.script.ScriptEngine engine =
73              manager.getEngineByName(scriptEnginName);
74  
75          FileReader reader;
76          try {
77              reader = new FileReader(scriptPath + scriptFile);
78          } catch (FileNotFoundException e) {
79              String errMessage =
80                  "DERC-" + DERC + "1: The script " + scriptPath
81                  + scriptFile
82                  + " does not exist, is a directory rather than a regular file,"
83                  + " or for some other reason cannot be opened for reading.";
84              LOGGER.log(Level.WARNING, errMessage, e);
85              throw new GeneticAlgorithmException(errMessage);
86          }
87          try {
88              engine.eval(reader);
89          } catch (ScriptException e) {
90              String errMessage =
91                  "DERC-" + DERC + "2: Error occurrs in script "
92                  + scriptPath + scriptFile + ".";
93              LOGGER.log(Level.WARNING, errMessage, e);
94              throw new GeneticAlgorithmException(errMessage);
95          }
96          try {
97              reader.close();
98          } catch (IOException e) {
99              String errMessage =
100                 "DERC-" + DERC + "3: Nie udało się zamknąć uchwytu do pliku.";
101             LOGGER.log(Level.WARNING, errMessage, e);
102             throw new GeneticAlgorithmException(errMessage);
103         }
104         invokeEngine = (Invocable) engine;
105     }
106 
107     /**
108      * Wywołuje funkcję zdefiniowaną w skrypcie.
109      * @param functionName Nazwa funkcji zdefiniowanej w skrypcie.
110      * @param args Argumenty do przekazania do wywoływanej funkcji.
111      * @return Wartość zwrócona przez wywoływaną funkcję.
112      * @throws GeneticAlgorithmException DERC-1-9-1-4, DERC-1-9-1-5
113      * @since 1.0
114      */
115     public Object invoke(final String functionName,
116                          final Object... args) throws GeneticAlgorithmException {
117         Object o = null;
118         try {
119             o = invokeEngine.invokeFunction(functionName, args);
120         } catch (ScriptException e) {
121             String errMessage =
122                 "DERC-" + DERC + "4: Error occurrs during invocation of the method.";
123             LOGGER.log(Level.WARNING, errMessage, e);
124             throw new GeneticAlgorithmException(errMessage);
125         } catch (NoSuchMethodException e) {
126             String errMessage =
127                 "DERC-" + DERC + "5: Method with given name or matching argument types cannot be found.";
128             LOGGER.log(Level.WARNING, errMessage, e);
129             throw new GeneticAlgorithmException(errMessage);
130         }
131         return o;
132     }
133 
134     /**
135      * Zwraca nazwę silnika skryptów.
136      * @return Nazwa silnika skryptów.
137      * @since 1.0
138      */
139     public final String getScriptEnginName() {
140         return this.scriptEnginName;
141     }
142 
143     /**
144      * Zwraca bezwzględną ścieżkę do katalogu w którym składowane są skrytpy.
145      * @return Ścieżka do katalogu.
146      */
147     public final String getScriptPath() {
148         return this.scriptPath;
149     }
150 
151     /**
152      * Zwraca nazwę pliku z wykonywanym skrytem.
153      * @return Nazwa pliku z wykonywanym skrytem.
154      * @since 1.0
155      */
156     public final String getScriptFile() {
157         return this.scriptFile;
158     }
159 }