r3959 - in branches/4.0.1/src/main: java/fr/ifremer/isisfish/entities java/fr/ifremer/isisfish/util xmi
Author: echatellier Date: 2014-04-15 18:08:05 +0200 (Tue, 15 Apr 2014) New Revision: 3959 Url: http://forge.codelutin.com/projects/isis-fish/repository/revisions/3959 Log: Remove args name management with Map (only use ordered args value as array) Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/entities/EquationImpl.java branches/4.0.1/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java branches/4.0.1/src/main/xmi/isis-fish.zargo Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/entities/EquationImpl.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/entities/EquationImpl.java 2014-04-15 15:08:38 UTC (rev 3958) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/entities/EquationImpl.java 2014-04-15 16:08:05 UTC (rev 3959) @@ -27,9 +27,6 @@ import static org.nuiton.i18n.I18n.t; -import java.util.HashMap; -import java.util.Map; - import fr.ifremer.isisfish.IsisFishRuntimeException; import fr.ifremer.isisfish.datastore.FormuleStorage; import fr.ifremer.isisfish.equation.Language; @@ -55,22 +52,29 @@ /** * Evalue l'equation et retourne le résultat. * - * @param param equation args + * @param args equation args (arg name and arg value) * @return equation evaluation */ - public double evaluate(Map<String, Object> param) { + public double evaluate(Object... args) { String eq = getContent(); try { - // add Simulation context in map - param.put("context", SimulationContext.get()); // default is BSH if (Language.JAVA.equals(getLanguage()) || getJavaInterface() != null) { + + // in Java, we don't need args names, only args value ordered + // build a new array with SimulationContext in first position + Object[] params = new Object[args.length / 2 + 1]; + params[0] = SimulationContext.get(); + for (int i = 0; i < args.length / 2; i++) { + params[i + 1] = args[i*2 + 1]; + } + // default Java if there are javaInterface Object val = EvaluatorHelper.evaluate( FormuleStorage.FORMULE_PATH, getTopiaId(), - getJavaInterface(), getContent(), param); + getJavaInterface(), getContent(), params); if (val instanceof Number) { double result = ((Number) val).doubleValue(); return result; @@ -85,21 +89,8 @@ } catch (Exception eee) { throw new IsisFishRuntimeException(t("isisfish.error.evaluate.equation", eq), eee); } - } - public double evaluate(String name, Object value, Object... others) { - Map<String, Object> param = new HashMap<String, Object>(); - param.put(name, value); - for (int i = 0; i < others.length;) { - name = (String) others[i++]; - value = others[i++]; - param.put(name, value); - } - double result = evaluate(param); - return result; - } - /* * @see fr.ifremer.isisfish.entities.EquationAbstract#toString() */ Modified: branches/4.0.1/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java =================================================================== --- branches/4.0.1/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java 2014-04-15 15:08:38 UTC (rev 3958) +++ branches/4.0.1/src/main/java/fr/ifremer/isisfish/util/EvaluatorHelper.java 2014-04-15 16:08:05 UTC (rev 3959) @@ -42,10 +42,14 @@ import fr.ifremer.isisfish.IsisFish; import fr.ifremer.isisfish.IsisFishRuntimeException; + import java.util.regex.Matcher; import java.util.regex.Pattern; + import fr.ifremer.isisfish.simulator.SimulationContext; + import java.util.HashMap; + import org.apache.commons.lang3.StringUtils; /** @@ -181,7 +185,7 @@ * @return la valeur retourné par la methode */ public static Object evaluate(String packageName, String className, - Class javaInterface, String script, Map<String, Object> args) { + Class javaInterface, String script, Object... args) { className = normalizeClassName(className); Object result = null; @@ -206,8 +210,8 @@ // if Java file's checkSum is not equals to script's checkSum // generate new Java file if (!checkSumEquals) { + String content = generateContent(packageName, className, interfaceMethod, script); - try { // force writing to UTF-8 // fix compilation issue : unmappable characters @@ -217,18 +221,7 @@ throw new IsisFishRuntimeException(t("isisfish.error.save.script.compilation", fileSrc), zzz); } - try { - List<File> classpath = new ArrayList<File>(); - classpath.add(fileRootSrc.getAbsoluteFile()); - classpath.add(IsisFish.config.getDatabaseDirectory().getAbsoluteFile()); - int compileResult = CompileHelper.compile(classpath, Collections.singletonList(fileSrc), fileRootSrc, null); - - if (compileResult != 0) { - throw new IsisFishRuntimeException(t("isisfish.error.compile.script", compileResult, fileSrc)); - } - } catch (Exception zzz) { - throw new IsisFishRuntimeException(t("isisfish.error.compile.script", fileSrc), zzz); - } + compile(fileRootSrc, fileSrc); } // try to load class @@ -239,16 +232,9 @@ // force to compile it again; may happen if hashcode // is correct, but .class file can't be loaded - try { - List<File> classpath = new ArrayList<File>(); - classpath.add(fileRootSrc.getAbsoluteFile()); - classpath.add(IsisFish.config.getDatabaseDirectory().getAbsoluteFile()); - int compileResult = CompileHelper.compile(classpath, Collections.singletonList(fileSrc), fileRootSrc, null); + compile(fileRootSrc, fileSrc); - if (compileResult != 0) { - throw new IsisFishRuntimeException(t("isisfish.error.compile.script", compileResult, fileSrc)); - } - + try { // second load ClassLoader cl = IsisFish.config.getScriptClassLoader(); clazz = cl.loadClass(classname); @@ -262,6 +248,21 @@ return result; } + protected static void compile(File fileRootSrc, File fileSrc) { + try { + List<File> classpath = new ArrayList<File>(); + classpath.add(fileRootSrc.getAbsoluteFile()); + classpath.add(IsisFish.config.getDatabaseDirectory().getAbsoluteFile()); + int compileResult = CompileHelper.compile(classpath, Collections.singletonList(fileSrc), fileRootSrc, null); + + if (compileResult != 0) { + throw new IsisFishRuntimeException(t("isisfish.error.compile.script", compileResult, fileSrc)); + } + } catch (Exception zzz) { + throw new IsisFishRuntimeException(t("isisfish.error.compile.script", fileSrc), zzz); + } + } + /** * Generate script content. * @@ -351,23 +352,20 @@ others.append(code.substring(pos)); } - protected static Object invoke(Class clazz, Method interfaceMethod, Map<String, Object> args) { + protected static Object invoke(Class clazz, Method interfaceMethod, Object... args) { + + Object result; try { Method method = clazz.getDeclaredMethod(interfaceMethod.getName(), interfaceMethod.getParameterTypes()); - - List<Object> params = new ArrayList<Object>(); - for (String name : interfaceMethod.getAnnotation(Args.class).value()) { - Object arg = args.get(name); - params.add(arg); - } + Object eq = clazz.newInstance(); - Object result; - result = method.invoke(eq, params.toArray()); - return result; + result = method.invoke(eq, args); + } catch (Exception eee) { throw new IsisFishRuntimeException(t("isisfish.error.invoke.method", interfaceMethod, clazz.getName()), eee); } + return result; } } Modified: branches/4.0.1/src/main/xmi/isis-fish.zargo =================================================================== (Binary files differ)
participants (1)
-
echatellier@users.forge.codelutin.com