Tony CHEMIT pushed to branch develop-7.x at ultreiaio / ird-observe
Commits:
-
071ef71a
by Tony Chemit at 2022-07-29T13:27:40+02:00
8 changed files:
- client-core/src/main/java/fr/ird/observe/client/ObserveRunner.java
- common-dto/pom.xml
- common-dto/src/main/java/fr/ird/observe/dto/ObserveUtil.java
- common-dto/src/main/java/fr/ird/observe/dto/referential/FormulaHelper.java
- common-dto/src/test/java/fr/ird/observe/dto/ScriptTest.java
- dto/src/main/java/fr/ird/observe/dto/referential/seine/ObjectMaterialDto.java
- pom.xml
- services-local/src/test/java/fr/ird/observe/services/local/service/actions/validate/ValidateServiceLocalTest.java
Changes:
| ... | ... | @@ -48,7 +48,6 @@ import org.nuiton.jaxx.runtime.swing.application.ApplicationRunner; |
| 48 | 48 | import org.nuiton.util.StringUtil;
|
| 49 | 49 | |
| 50 | 50 | import javax.script.ScriptEngine;
|
| 51 | -import javax.script.ScriptEngineManager;
|
|
| 52 | 51 | import javax.script.ScriptException;
|
| 53 | 52 | import javax.swing.UIManager;
|
| 54 | 53 | import java.io.File;
|
| ... | ... | @@ -88,7 +87,9 @@ public abstract class ObserveRunner extends ApplicationRunner { |
| 88 | 87 | java.util.logging.LogManager.getLogManager().getLogger("").setLevel(Level.SEVERE);
|
| 89 | 88 | }
|
| 90 | 89 | |
| 91 | - /** Logger */
|
|
| 90 | + /**
|
|
| 91 | + * Logger
|
|
| 92 | + */
|
|
| 92 | 93 | private static Logger log = LogManager.getLogger(ObserveRunner.class);
|
| 93 | 94 | |
| 94 | 95 | private static ObserveResourceManager resourceManager;
|
| ... | ... | @@ -136,7 +137,7 @@ public abstract class ObserveRunner extends ApplicationRunner { |
| 136 | 137 | .map(Path::toFile)
|
| 137 | 138 | .forEach(File::delete);
|
| 138 | 139 | } catch (IOException e) {
|
| 139 | - throw new IllegalStateException("Can't delete i18n directory: "+i18nDirectory, e);
|
|
| 140 | + throw new IllegalStateException("Can't delete i18n directory: " + i18nDirectory, e);
|
|
| 140 | 141 | }
|
| 141 | 142 | |
| 142 | 143 | }
|
| ... | ... | @@ -147,8 +148,8 @@ public abstract class ObserveRunner extends ApplicationRunner { |
| 147 | 148 | |
| 148 | 149 | I18n.init(i18nInitializer, locale);
|
| 149 | 150 | |
| 150 | - log.debug("i18n language : " + locale);
|
|
| 151 | - log.debug("i18n loading time : " + StringUtil.convertTime(t00, System.nanoTime()));
|
|
| 151 | + log.debug("i18n language : " + locale);
|
|
| 152 | + log.debug("i18n loading time : " + StringUtil.convertTime(t00, System.nanoTime()));
|
|
| 152 | 153 | }
|
| 153 | 154 | |
| 154 | 155 | public abstract String getRunnerName();
|
| ... | ... | @@ -559,9 +560,7 @@ public abstract class ObserveRunner extends ApplicationRunner { |
| 559 | 560 | */
|
| 560 | 561 | private void checkScriptEngineFound() {
|
| 561 | 562 | |
| 562 | - ScriptEngineManager factory = new ScriptEngineManager();
|
|
| 563 | - |
|
| 564 | - ScriptEngine scriptEngine = factory.getEngineByExtension("js");
|
|
| 563 | + ScriptEngine scriptEngine = ObserveUtil.getScriptEngine();
|
|
| 565 | 564 | |
| 566 | 565 | if (scriptEngine == null) {
|
| 567 | 566 |
| ... | ... | @@ -80,6 +80,11 @@ |
| 80 | 80 | <groupId>com.google.auto.service</groupId>
|
| 81 | 81 | <artifactId>auto-service-annotations</artifactId>
|
| 82 | 82 | </dependency>
|
| 83 | + <dependency>
|
|
| 84 | + <groupId>org.mozilla</groupId>
|
|
| 85 | + <artifactId>rhino-engine</artifactId>
|
|
| 86 | + <scope>runtime</scope>
|
|
| 87 | + </dependency>
|
|
| 83 | 88 | |
| 84 | 89 | <!-- test -->
|
| 85 | 90 | <dependency>
|
| ... | ... | @@ -25,6 +25,8 @@ package fr.ird.observe.dto; |
| 25 | 25 | import io.ultreia.java4all.config.ApplicationConfig;
|
| 26 | 26 | import org.apache.logging.log4j.core.config.Configurator;
|
| 27 | 27 | |
| 28 | +import javax.script.ScriptEngine;
|
|
| 29 | +import javax.script.ScriptEngineManager;
|
|
| 28 | 30 | import java.io.BufferedReader;
|
| 29 | 31 | import java.io.IOException;
|
| 30 | 32 | import java.io.InputStreamReader;
|
| ... | ... | @@ -54,6 +56,8 @@ import java.util.function.Supplier; |
| 54 | 56 | */
|
| 55 | 57 | public class ObserveUtil {
|
| 56 | 58 | |
| 59 | + public static final String JS_ENGINE_NAME = "rhino";
|
|
| 60 | + |
|
| 57 | 61 | public static <C extends Class<?>> List<C> sortTypes(Collection<C> types, Function<Class, String> function, Locale locale) {
|
| 58 | 62 | |
| 59 | 63 | List<C> list = new ArrayList<>(types);
|
| ... | ... | @@ -109,4 +113,10 @@ public class ObserveUtil { |
| 109 | 113 | }
|
| 110 | 114 | }
|
| 111 | 115 | |
| 116 | + |
|
| 117 | + public static ScriptEngine getScriptEngine() {
|
|
| 118 | + ScriptEngineManager factory = new ScriptEngineManager();
|
|
| 119 | + return factory.getEngineByName(JS_ENGINE_NAME);
|
|
| 120 | + }
|
|
| 121 | + |
|
| 112 | 122 | } |
| ... | ... | @@ -22,19 +22,20 @@ package fr.ird.observe.dto.referential; |
| 22 | 22 | * #L%
|
| 23 | 23 | */
|
| 24 | 24 | |
| 25 | -import java.util.Map;
|
|
| 26 | -import java.util.TreeMap;
|
|
| 27 | -import java.util.regex.Matcher;
|
|
| 28 | -import java.util.regex.Pattern;
|
|
| 25 | +import fr.ird.observe.dto.ObserveUtil;
|
|
| 26 | +import org.apache.commons.lang3.StringUtils;
|
|
| 27 | +import org.apache.logging.log4j.LogManager;
|
|
| 28 | +import org.apache.logging.log4j.Logger;
|
|
| 29 | +import org.nuiton.util.NumberUtil;
|
|
| 30 | + |
|
| 29 | 31 | import javax.script.Bindings;
|
| 30 | 32 | import javax.script.ScriptContext;
|
| 31 | 33 | import javax.script.ScriptEngine;
|
| 32 | -import javax.script.ScriptEngineManager;
|
|
| 33 | 34 | import javax.script.ScriptException;
|
| 34 | -import org.apache.commons.lang3.StringUtils;
|
|
| 35 | -import org.apache.logging.log4j.Logger;
|
|
| 36 | -import org.apache.logging.log4j.LogManager;
|
|
| 37 | -import org.nuiton.util.NumberUtil;
|
|
| 35 | +import java.util.Map;
|
|
| 36 | +import java.util.TreeMap;
|
|
| 37 | +import java.util.regex.Matcher;
|
|
| 38 | +import java.util.regex.Pattern;
|
|
| 38 | 39 | |
| 39 | 40 | /**
|
| 40 | 41 | * Created on 05/11/16.
|
| ... | ... | @@ -44,15 +45,18 @@ import org.nuiton.util.NumberUtil; |
| 44 | 45 | */
|
| 45 | 46 | public class FormulaHelper {
|
| 46 | 47 | |
| 47 | - /** Logger */
|
|
| 48 | 48 | private static final Logger log = LogManager.getLogger(FormulaHelper.class);
|
| 49 | 49 | |
| 50 | 50 | private static final Pattern COEFFICIENTS_PATTERN = Pattern.compile("(.+)=(.+)");
|
| 51 | 51 | |
| 52 | - /** variable weight à utiliser dans la relation taille */
|
|
| 52 | + /**
|
|
| 53 | + * weight variable to use ina length relation.
|
|
| 54 | + */
|
|
| 53 | 55 | public static final String VARIABLE_WEIGHT = "P";
|
| 54 | 56 | |
| 55 | - /** variable taille à utiliser dans la relation weight */
|
|
| 57 | + /**
|
|
| 58 | + * length variable to use in a weight relation.
|
|
| 59 | + */
|
|
| 56 | 60 | public static final String VARIABLE_LENGTH = "L";
|
| 57 | 61 | |
| 58 | 62 | private static final String VARIABLE_X = "x";
|
| ... | ... | @@ -64,43 +68,34 @@ public class FormulaHelper { |
| 64 | 68 | |
| 65 | 69 | public static final String COEFFICIENT_B = "b";
|
| 66 | 70 | |
| 67 | - /** moteur d'évaluation d'expression */
|
|
| 68 | 71 | private static ScriptEngine scriptEngine;
|
| 69 | 72 | |
| 70 | - |
|
| 71 | 73 | private static ScriptEngine getScriptEngine() {
|
| 72 | 74 | if (scriptEngine == null) {
|
| 73 | - ScriptEngineManager factory = new ScriptEngineManager();
|
|
| 74 | - |
|
| 75 | - scriptEngine = factory.getEngineByExtension("js");
|
|
| 75 | + scriptEngine = ObserveUtil.getScriptEngine();
|
|
| 76 | 76 | }
|
| 77 | 77 | return scriptEngine;
|
| 78 | 78 | }
|
| 79 | 79 | |
| 80 | - public static Map<String, Double> getCoefficientValues(WithFormula parametrage) {
|
|
| 80 | + public static Map<String, Double> getCoefficientValues(WithFormula formula) {
|
|
| 81 | 81 | |
| 82 | 82 | Map<String, Double> result = new TreeMap<>();
|
| 83 | - String coefficients = parametrage.getCoefficients();
|
|
| 83 | + String coefficients = formula.getCoefficients();
|
|
| 84 | 84 | if (coefficients != null) {
|
| 85 | 85 | for (String coefficientDef : coefficients.split(":")) {
|
| 86 | 86 | Matcher matcher = COEFFICIENTS_PATTERN.matcher(coefficientDef.trim());
|
| 87 | - if (log.isDebugEnabled()) {
|
|
| 88 | - log.debug("constant to test = " + coefficientDef);
|
|
| 89 | - }
|
|
| 87 | + log.debug("constant to test = " + coefficientDef);
|
|
| 90 | 88 | if (matcher.matches()) {
|
| 91 | - |
|
| 92 | 89 | String key = matcher.group(1);
|
| 93 | 90 | String val = matcher.group(2);
|
| 94 | 91 | try {
|
| 95 | 92 | Double d = Double.valueOf(val);
|
| 96 | 93 | result.put(key, d);
|
| 97 | - if (log.isDebugEnabled()) {
|
|
| 98 | - log.debug("detects coefficient " + key + '=' + val);
|
|
| 99 | - }
|
|
| 94 | + log.debug("detects coefficient " + key + '=' + val);
|
|
| 100 | 95 | } catch (NumberFormatException e) {
|
| 101 | - // pas pu recupere le count...
|
|
| 96 | + // could not get count
|
|
| 97 | + log.warn("could not parse double " + val + " for coefficient " + key);
|
|
| 102 | 98 | if (log.isWarnEnabled()) {
|
| 103 | - log.warn("could not parse double " + val + " for coefficient " + key);
|
|
| 104 | 99 | }
|
| 105 | 100 | }
|
| 106 | 101 | }
|
| ... | ... | @@ -109,66 +104,51 @@ public class FormulaHelper { |
| 109 | 104 | return result;
|
| 110 | 105 | }
|
| 111 | 106 | |
| 112 | - public static boolean validateRelation(WithFormula parametrage, String relation, String variable) {
|
|
| 113 | - boolean result = false;
|
|
| 107 | + public static boolean validateRelation(WithFormula formula, String relation, String variable) {
|
|
| 114 | 108 | if (!StringUtils.isEmpty(relation)) {
|
| 115 | - |
|
| 116 | - Map<String, Double> coeffs = parametrage.getCoefficientValues();
|
|
| 117 | - |
|
| 109 | + Map<String, Double> coefficientValues = formula.getCoefficientValues();
|
|
| 118 | 110 | ScriptEngine engine = getScriptEngine();
|
| 119 | 111 | Bindings bindings = engine.createBindings();
|
| 120 | - addBindings(coeffs, bindings, variable, 1);
|
|
| 121 | - |
|
| 112 | + addBindings(coefficientValues, bindings, variable, 1);
|
|
| 122 | 113 | try {
|
| 123 | 114 | engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
|
| 124 | - Double o = (Double) engine.eval("parseFloat(" + relation + ")");
|
|
| 125 | - if (log.isDebugEnabled()) {
|
|
| 126 | - log.debug("evaluation ok : " + relation + " (" + variable + "=1) = " + o);
|
|
| 127 | - }
|
|
| 128 | - result = true;
|
|
| 115 | + Number o = (Number) engine.eval("parseFloat(" + relation + ")");
|
|
| 116 | + log.debug("evaluation ok : " + relation + " (" + variable + "=1) = " + o);
|
|
| 117 | + return true;
|
|
| 129 | 118 | } catch (Exception e) {
|
| 130 | - if (log.isDebugEnabled()) {
|
|
| 131 | - log.debug("evaluation ko : " + relation + ", reason : " + e.getMessage());
|
|
| 132 | - }
|
|
| 119 | + log.debug("evaluation ko : " + relation + ", reason : " + e.getMessage());
|
|
| 133 | 120 | }
|
| 134 | 121 | }
|
| 135 | - return result;
|
|
| 122 | + return false;
|
|
| 136 | 123 | }
|
| 137 | 124 | |
| 138 | 125 | public static boolean validateObjectMaterialValidation(String relation, Object value) {
|
| 139 | 126 | boolean result = false;
|
| 140 | 127 | if (!StringUtils.isEmpty(relation)) {
|
| 141 | - |
|
| 142 | 128 | ScriptEngine engine = getScriptEngine();
|
| 143 | 129 | Bindings bindings = engine.createBindings();
|
| 144 | 130 | bindings.put(FormulaHelper.VARIABLE_X, value);
|
| 145 | - |
|
| 146 | 131 | try {
|
| 147 | 132 | engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
|
| 148 | 133 | Boolean o = (Boolean) engine.eval(relation);
|
| 149 | - if (log.isDebugEnabled()) {
|
|
| 150 | - log.debug(String.format("evaluation ok : %s (%s=%s) = %s", relation, FormulaHelper.VARIABLE_X, value, o));
|
|
| 151 | - }
|
|
| 134 | + log.debug(String.format("evaluation ok : %s (%s=%s) = %s", relation, FormulaHelper.VARIABLE_X, value, o));
|
|
| 152 | 135 | result = o != null && o;
|
| 153 | 136 | } catch (Exception e) {
|
| 154 | - if (log.isDebugEnabled()) {
|
|
| 155 | - log.debug("evaluation ko : " + relation + ", reason : " + e.getMessage());
|
|
| 156 | - }
|
|
| 137 | + log.debug("evaluation ko : " + relation + ", reason : " + e.getMessage());
|
|
| 157 | 138 | }
|
| 158 | 139 | }
|
| 159 | 140 | return result;
|
| 160 | 141 | }
|
| 161 | 142 | |
| 162 | - public static Float computeValue(WithFormula parametrage, String formula, String coefficientName, String variableName, float data) {
|
|
| 143 | + public static Float computeValue(WithFormula formula, String relation, String coefficientName, String variableName, float data) {
|
|
| 163 | 144 | if (coefficientName != null) {
|
| 164 | - Double b = parametrage.getCoefficientValue(coefficientName);
|
|
| 145 | + Double b = formula.getCoefficientValue(coefficientName);
|
|
| 165 | 146 | if (b == 0) {
|
| 166 | - |
|
| 167 | - // ce cas limite ne permet pas de calculer la taille a partir du weight
|
|
| 147 | + // limit case, can't compute length from weight
|
|
| 168 | 148 | return null;
|
| 169 | 149 | }
|
| 170 | 150 | }
|
| 171 | - Float o = computeValue(parametrage, formula, variableName, data);
|
|
| 151 | + Float o = computeValue(formula, relation, variableName, data);
|
|
| 172 | 152 | |
| 173 | 153 | if (o != null) {
|
| 174 | 154 | o = NumberUtil.roundOneDigit(o);
|
| ... | ... | @@ -176,35 +156,30 @@ public class FormulaHelper { |
| 176 | 156 | return o;
|
| 177 | 157 | }
|
| 178 | 158 | |
| 179 | - private static Float computeValue(WithFormula parametrage, String relation, String variable, float taille) {
|
|
| 180 | - Map<String, Double> coeffs = parametrage.getCoefficientValues();
|
|
| 159 | + private static Float computeValue(WithFormula formula, String relation, String variable, float data) {
|
|
| 160 | + Map<String, Double> coefficientValues = formula.getCoefficientValues();
|
|
| 181 | 161 | ScriptEngine engine = getScriptEngine();
|
| 182 | 162 | Bindings bindings = engine.createBindings();
|
| 183 | - addBindings(coeffs, bindings, variable, taille);
|
|
| 184 | - |
|
| 163 | + addBindings(coefficientValues, bindings, variable, data);
|
|
| 185 | 164 | engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
|
| 186 | - Double o = null;
|
|
| 165 | + Number result;
|
|
| 187 | 166 | try {
|
| 188 | - o = (Double) engine.eval("parseFloat(" + relation + ")");
|
|
| 167 | + result = ((Number) engine.eval("parseFloat(" + relation + ")"));
|
|
| 189 | 168 | } catch (ScriptException e) {
|
| 190 | - if (log.isErrorEnabled()) {
|
|
| 191 | - log.error("Could not compute value from " + relation);
|
|
| 192 | - }
|
|
| 169 | + log.error("Could not compute value from " + relation);
|
|
| 170 | + result = null;
|
|
| 193 | 171 | }
|
| 194 | - return o == null ? null : o.floatValue();
|
|
| 172 | + return result == null ? null : result.floatValue();
|
|
| 195 | 173 | }
|
| 196 | 174 | |
| 197 | - private static void addBindings(Map<String, Double> coeffs, Bindings bindings, String variable, float taille) {
|
|
| 198 | - for (Map.Entry<String, Double> entry : coeffs.entrySet()) {
|
|
| 175 | + private static void addBindings(Map<String, Double> coefficientValues, Bindings bindings, String variable, float data) {
|
|
| 176 | + for (Map.Entry<String, Double> entry : coefficientValues.entrySet()) {
|
|
| 199 | 177 | String key = entry.getKey();
|
| 200 | 178 | Double value = entry.getValue();
|
| 201 | 179 | bindings.put(key, value);
|
| 202 | - |
|
| 203 | - if (log.isDebugEnabled()) {
|
|
| 204 | - log.debug("add constant " + key + '=' + value);
|
|
| 205 | - }
|
|
| 180 | + log.debug("add constant " + key + '=' + value);
|
|
| 206 | 181 | }
|
| 207 | - bindings.put(variable, taille);
|
|
| 182 | + bindings.put(variable, data);
|
|
| 208 | 183 | }
|
| 209 | 184 | |
| 210 | 185 |
| ... | ... | @@ -21,19 +21,20 @@ |
| 21 | 21 | */
|
| 22 | 22 | package fr.ird.observe.dto;
|
| 23 | 23 | |
| 24 | -import java.util.List;
|
|
| 25 | -import javax.script.ScriptContext;
|
|
| 26 | -import javax.script.ScriptEngine;
|
|
| 27 | -import javax.script.ScriptEngineFactory;
|
|
| 28 | -import javax.script.ScriptEngineManager;
|
|
| 29 | -import javax.script.ScriptException;
|
|
| 30 | -import org.apache.logging.log4j.Logger;
|
|
| 31 | 24 | import org.apache.logging.log4j.LogManager;
|
| 25 | +import org.apache.logging.log4j.Logger;
|
|
| 32 | 26 | import org.junit.Assert;
|
| 33 | 27 | import org.junit.Before;
|
| 34 | 28 | import org.junit.Test;
|
| 35 | 29 | import org.nuiton.util.StringUtil;
|
| 36 | 30 | |
| 31 | +import javax.script.ScriptContext;
|
|
| 32 | +import javax.script.ScriptEngine;
|
|
| 33 | +import javax.script.ScriptEngineFactory;
|
|
| 34 | +import javax.script.ScriptEngineManager;
|
|
| 35 | +import javax.script.ScriptException;
|
|
| 36 | +import java.util.List;
|
|
| 37 | + |
|
| 37 | 38 | /**
|
| 38 | 39 | * @author Tony Chemit - dev@tchemit.fr
|
| 39 | 40 | * @since 1.2
|
| ... | ... | @@ -49,7 +50,7 @@ public class ScriptTest { |
| 49 | 50 | |
| 50 | 51 | final double x = 10f;
|
| 51 | 52 | |
| 52 | - protected static final String EXPRESSION = "a*(x^b)";
|
|
| 53 | + protected static final String EXPRESSION = "parseFloat(a*(Math.pow(x, b)))";
|
|
| 53 | 54 | |
| 54 | 55 | protected static final String EXPRESSION_RESULT = "[%1$-10s] '%2$s' nb eval %3$-5s in %4$-10s (~ %5$s)";
|
| 55 | 56 | |
| ... | ... | @@ -60,34 +61,32 @@ public class ScriptTest { |
| 60 | 61 | factory = new ScriptEngineManager();
|
| 61 | 62 | List<ScriptEngineFactory> scriptEngineFactoryList = factory.getEngineFactories();
|
| 62 | 63 | for (ScriptEngineFactory factory : scriptEngineFactoryList) {
|
| 63 | - if (log.isInfoEnabled()) {
|
|
| 64 | - log.info("available factory [engine: " +
|
|
| 65 | - factory.getEngineName() + " v." +
|
|
| 66 | - factory.getEngineVersion() + "] [language: " +
|
|
| 67 | - factory.getLanguageName() + ", version:" +
|
|
| 68 | - factory.getLanguageVersion() + "], mime-types : " +
|
|
| 69 | - "?" + factory.getMimeTypes() +
|
|
| 70 | - "?" + factory.getExtensions());
|
|
| 71 | - }
|
|
| 64 | + log.info("available factory [engine: " +
|
|
| 65 | + factory.getEngineName() + " v." +
|
|
| 66 | + factory.getEngineVersion() + "] [language: " +
|
|
| 67 | + factory.getLanguageName() + ", version:" +
|
|
| 68 | + factory.getLanguageVersion() + "], mime-types : " +
|
|
| 69 | + "?" + factory.getMimeTypes() +
|
|
| 70 | + "?" + factory.getExtensions());
|
|
| 72 | 71 | }
|
| 73 | 72 | }
|
| 74 | 73 | |
| 75 | 74 | @Test
|
| 76 | 75 | public void testScripts() throws Exception {
|
| 77 | 76 | |
| 78 | - doEvaluate(EXPRESSION, 1);
|
|
| 79 | - doEvaluate(EXPRESSION, 10);
|
|
| 80 | - doEvaluate(EXPRESSION, 100);
|
|
| 81 | - doEvaluate(EXPRESSION, 1000);
|
|
| 77 | + doEvaluate(1);
|
|
| 78 | + doEvaluate(10);
|
|
| 79 | + doEvaluate(100);
|
|
| 80 | + doEvaluate(1000);
|
|
| 82 | 81 | |
| 83 | - doEvaluate(EXPRESSION, 1000);
|
|
| 84 | - doEvaluate(EXPRESSION, 100);
|
|
| 85 | - doEvaluate(EXPRESSION, 10);
|
|
| 86 | - doEvaluate(EXPRESSION, 1);
|
|
| 82 | + doEvaluate(1000);
|
|
| 83 | + doEvaluate(100);
|
|
| 84 | + doEvaluate(10);
|
|
| 85 | + doEvaluate(1);
|
|
| 87 | 86 | }
|
| 88 | 87 | |
| 89 | - protected void doEvaluate(String expression, int nb) throws ScriptException {
|
|
| 90 | - ScriptEngine engine = factory.getEngineByExtension("js");
|
|
| 88 | + protected void doEvaluate(int nb) throws ScriptException {
|
|
| 89 | + ScriptEngine engine = ObserveUtil.getScriptEngine();
|
|
| 91 | 90 | |
| 92 | 91 | long t0 = System.nanoTime();
|
| 93 | 92 | for (int i = 0; i < nb; i++) {
|
| ... | ... | @@ -96,13 +95,13 @@ public class ScriptTest { |
| 96 | 95 | c.setAttribute("a", a, ScriptContext.GLOBAL_SCOPE);
|
| 97 | 96 | c.setAttribute("x", x, ScriptContext.GLOBAL_SCOPE);
|
| 98 | 97 | c.setAttribute("b", b, ScriptContext.GLOBAL_SCOPE);
|
| 99 | - Double o = (Double) engine.eval("parseFloat(a*x^b)");
|
|
| 98 | + Double o = (Double) engine.eval(EXPRESSION);
|
|
| 100 | 99 | Assert.assertEquals(a * Math.pow(x, b), o, 5);
|
| 101 | 100 | }
|
| 102 | 101 | long t1 = System.nanoTime();
|
| 103 | 102 | String result = String.format(EXPRESSION_RESULT,
|
| 104 | 103 | "js",
|
| 105 | - expression,
|
|
| 104 | + ScriptTest.EXPRESSION,
|
|
| 106 | 105 | nb,
|
| 107 | 106 | StringUtil.convertTime(t1 - t0),
|
| 108 | 107 | StringUtil.convertTime((t1 - t0) / nb));
|
| ... | ... | @@ -22,12 +22,12 @@ package fr.ird.observe.dto.referential.seine; |
| 22 | 22 | * #L%
|
| 23 | 23 | */
|
| 24 | 24 | |
| 25 | -import org.apache.commons.lang3.StringUtils;
|
|
| 25 | +import fr.ird.observe.dto.ObserveUtil;
|
|
| 26 | +import org.apache.logging.log4j.util.Strings;
|
|
| 26 | 27 | |
| 27 | 28 | import javax.script.Bindings;
|
| 28 | 29 | import javax.script.ScriptContext;
|
| 29 | 30 | import javax.script.ScriptEngine;
|
| 30 | -import javax.script.ScriptEngineManager;
|
|
| 31 | 31 | |
| 32 | 32 | public class ObjectMaterialDto extends GeneratedObjectMaterialDto {
|
| 33 | 33 | |
| ... | ... | @@ -57,7 +57,7 @@ public class ObjectMaterialDto extends GeneratedObjectMaterialDto { |
| 57 | 57 | value = Boolean.TRUE;
|
| 58 | 58 | } else if (isInteger()) {
|
| 59 | 59 | value = 10;
|
| 60 | - }else if (isFloat()) {
|
|
| 60 | + } else if (isFloat()) {
|
|
| 61 | 61 | value = 10f;
|
| 62 | 62 | }
|
| 63 | 63 | boolean result = validateObjectMaterialValidation(validation, value);
|
| ... | ... | @@ -65,7 +65,7 @@ public class ObjectMaterialDto extends GeneratedObjectMaterialDto { |
| 65 | 65 | }
|
| 66 | 66 | |
| 67 | 67 | public boolean withValidation() {
|
| 68 | - return StringUtils.isNoneEmpty(validation);
|
|
| 68 | + return Strings.isNotEmpty(validation);
|
|
| 69 | 69 | }
|
| 70 | 70 | |
| 71 | 71 | /**
|
| ... | ... | @@ -79,18 +79,15 @@ public class ObjectMaterialDto extends GeneratedObjectMaterialDto { |
| 79 | 79 | /** moteur d'évaluation d'expression */
|
| 80 | 80 | private static ScriptEngine scriptEngine;
|
| 81 | 81 | |
| 82 | - |
|
| 83 | 82 | private static ScriptEngine getScriptEngine() {
|
| 84 | 83 | if (scriptEngine == null) {
|
| 85 | - ScriptEngineManager factory = new ScriptEngineManager();
|
|
| 86 | - |
|
| 87 | - scriptEngine = factory.getEngineByExtension("js");
|
|
| 84 | + scriptEngine = ObserveUtil.getScriptEngine();
|
|
| 88 | 85 | }
|
| 89 | 86 | return scriptEngine;
|
| 90 | 87 | }
|
| 91 | 88 | |
| 92 | 89 | public static boolean validateObjectMaterialValidation(String relation, Object value) {
|
| 93 | - if (!StringUtils.isEmpty(relation)) {
|
|
| 90 | + if (Strings.isNotEmpty(relation)) {
|
|
| 94 | 91 | |
| 95 | 92 | ScriptEngine engine = getScriptEngine();
|
| 96 | 93 | Bindings bindings = engine.createBindings();
|
| ... | ... | @@ -98,7 +95,9 @@ public class ObjectMaterialDto extends GeneratedObjectMaterialDto { |
| 98 | 95 | |
| 99 | 96 | try {
|
| 100 | 97 | engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
|
| 101 | - Boolean o = (Boolean) engine.eval(relation);
|
|
| 98 | + // See https://gitlab.com/ultreiaio/ird-observe/-/issues/2413#note_1044640610
|
|
| 99 | + String fixedRelation = relation.replaceAll("length\\(\\)", "length");
|
|
| 100 | + Boolean o = (Boolean) engine.eval(fixedRelation);
|
|
| 102 | 101 | return true;
|
| 103 | 102 | } catch (Exception e) {
|
| 104 | 103 | return false;
|
| ... | ... | @@ -174,6 +174,7 @@ |
| 174 | 174 | <lib.version.hikariCP>4.0.3</lib.version.hikariCP>
|
| 175 | 175 | <!-- <lib.version.hikariCP>5.0.0</lib.version.hikariCP>-->
|
| 176 | 176 | <lib.version.bytebuddy>1.12.9</lib.version.bytebuddy>
|
| 177 | + <lib.version.rhino>1.7.14</lib.version.rhino>
|
|
| 177 | 178 | <lib.version.nuiton.topia>3.8.1</lib.version.nuiton.topia>
|
| 178 | 179 | <lib.version.nuiton.version>1.0</lib.version.nuiton.version>
|
| 179 | 180 | <lib.version.log4j2>2.17.2</lib.version.log4j2>
|
| ... | ... | @@ -290,7 +291,16 @@ |
| 290 | 291 | <artifactId>jaxx-widgets-combobox</artifactId>
|
| 291 | 292 | <version>${lib.version.java4all.jaxx}</version>
|
| 292 | 293 | </dependency>
|
| 293 | - |
|
| 294 | + <dependency>
|
|
| 295 | + <groupId>org.mozilla</groupId>
|
|
| 296 | + <artifactId>rhino</artifactId>
|
|
| 297 | + <version>${lib.version.rhino}</version>
|
|
| 298 | + </dependency>
|
|
| 299 | + <dependency>
|
|
| 300 | + <groupId>org.mozilla</groupId>
|
|
| 301 | + <artifactId>rhino-engine</artifactId>
|
|
| 302 | + <version>${lib.version.rhino}</version>
|
|
| 303 | + </dependency>
|
|
| 294 | 304 | <dependency>
|
| 295 | 305 | <groupId>org.apache.struts.xwork</groupId>
|
| 296 | 306 | <artifactId>xwork-core</artifactId>
|
| ... | ... | @@ -144,13 +144,13 @@ public class ValidateServiceLocalTest extends ServiceLocalTestSupport { |
| 144 | 144 | ValidationResult<ReferentialValidationRequest> result = service.validateReferential(request);
|
| 145 | 145 | Assert.assertNotNull(result);
|
| 146 | 146 | |
| 147 | - ImmutableSet<Class> types = result.getTypes();
|
|
| 147 | + @SuppressWarnings("rawtypes") ImmutableSet<Class> types = result.getTypes();
|
|
| 148 | 148 | |
| 149 | - for (Class type : types) {
|
|
| 149 | + for (Class<?> type : types) {
|
|
| 150 | 150 | ImmutableSet<ValidationResultDtoMessage> messages = result.getMessagesForType(type);
|
| 151 | 151 | System.out.println("assertValidateResult(result, " + type.getSimpleName() + ".class, " + messages.size() + ");");
|
| 152 | 152 | }
|
| 153 | - // no data of LengthLegnthParameter
|
|
| 153 | + // no data of LengthLengthParameter
|
|
| 154 | 154 | Assert.assertEquals(DtoModelClasses.REFERENTIAL_TYPES.size() - 1, types.size());
|
| 155 | 155 | |
| 156 | 156 | assertValidateResult(result, SpeciesGroupReleaseModeDto.class, 1);
|