1
2
3
4
5
6
7
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
23
24
25
26
27
28
29 public class ScriptEngine {
30
31
32 private static final Logger LOGGER =
33 Logger.getLogger(ScriptEngine.class.getName());
34
35
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
53
54
55
56
57
58
59
60
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
109
110
111
112
113
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
136
137
138
139 public final String getScriptEnginName() {
140 return this.scriptEnginName;
141 }
142
143
144
145
146
147 public final String getScriptPath() {
148 return this.scriptPath;
149 }
150
151
152
153
154
155
156 public final String getScriptFile() {
157 return this.scriptFile;
158 }
159 }