01/14: Ajout LengthLengthParameter et revue de LWParameter (persistence)
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository observe. See https://gitlab.nuiton.org/codelutin/observe.git commit 63eb6c05c52a3fd8f43f7c44b65adad9a8b2da79 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sat Nov 5 15:29:23 2016 +0100 Ajout LengthLengthParameter et revue de LWParameter (persistence) --- .../DataSourceMigrationForVersion_5_1.java | 1 + .../V5_1_18_evol_8404_lengthLengthParameter-H2.sql | 14 + .../V5_1_18_evol_8404_lengthLengthParameter-PG.sql | 14 + .../java/fr/ird/observe/entities/Entities.java | 1 + .../AbstractObserveMigrationCallBack.java | 2 +- .../entities/referentiel/FormulaHelper.java | 169 +++++++++++ .../entities/referentiel/FormulaSupportImpl.java | 88 ++++++ .../referentiel/LengthLengthParameterImpl.java | 55 ++++ .../referentiel/LengthLengthParameters.java | 12 + .../referentiel/LengthWeightParameterImpl.java | 110 +------ .../referentiel/LengthWeightParameters.java | 199 +----------- .../referentiel/LengthWeightParemeterHelper.java | 332 --------------------- entities/src/main/xmi/observe-common.zargo | Bin 42084 -> 45678 bytes entities/src/main/xmi/observe-seine.zargo | Bin 86910 -> 87124 bytes entities/src/main/xmi/observe.ini | 9 +- .../entities/referentiel/FormulaHelperTest.java | 27 ++ .../LengthWeightParemeterHelperTest.java | 48 --- 17 files changed, 414 insertions(+), 667 deletions(-) diff --git a/entities-migration/src/main/java/fr/ird/observe/entities/migration/DataSourceMigrationForVersion_5_1.java b/entities-migration/src/main/java/fr/ird/observe/entities/migration/DataSourceMigrationForVersion_5_1.java index 4b2ce80..683af4d 100644 --- a/entities-migration/src/main/java/fr/ird/observe/entities/migration/DataSourceMigrationForVersion_5_1.java +++ b/entities-migration/src/main/java/fr/ird/observe/entities/migration/DataSourceMigrationForVersion_5_1.java @@ -83,6 +83,7 @@ public class DataSourceMigrationForVersion_5_1 extends AbstractObserveMigrationC addScript("15", "evol_8578_nonTargetLength_add_speciesFate", queries); addScript("16", "evol_8377_speciesFate_add_discard", queries); addScript("17", "evol_8376_vesselActivitySeine_add_allowFad", queries); + addScript("18", "evol_8404_lengthLengthParameter", queries); } private void evol8391(TopiaSqlSupport topiaSqlSupport, List<String> queries) { diff --git a/entities-migration/src/main/resources/db/migration/5.1/V5_1_18_evol_8404_lengthLengthParameter-H2.sql b/entities-migration/src/main/resources/db/migration/5.1/V5_1_18_evol_8404_lengthLengthParameter-H2.sql new file mode 100644 index 0000000..0f7a390 --- /dev/null +++ b/entities-migration/src/main/resources/db/migration/5.1/V5_1_18_evol_8404_lengthLengthParameter-H2.sql @@ -0,0 +1,14 @@ +ALTER TABLE observe_seine.nontargetlength ADD COLUMN sizeMeasureType VARCHAR(255); +ALTER TABLE observe_seine.nontargetlength ADD CONSTRAINT FK_NON_TARGET_LENGTH_SIZE_MEASURE_TYPE FOREIGN KEY(sizeMeasureType) REFERENCES OBSERVE_COMMON.sizemeasuretype(topiaid); + +ALTER TABLE observe_common.lengthweightparameter ADD COLUMN sizeMeasureType VARCHAR(255); +ALTER TABLE observe_common.lengthweightparameter ADD CONSTRAINT FK_LENGTH_WEIGHT_PARAMETER_SIZE_MEASURE_TYPE FOREIGN KEY(sizeMeasureType) REFERENCES OBSERVE_COMMON.sizemeasuretype(topiaid); +UPDATE observe_common.lengthweightparameter SET sizeMeasureType = ( SELECT sizeMeasureType FROM observe_common.species s WHERE s.topiaId = species); + +CREATE TABLE observe_common.lengthlengthparameter ( topiaid VARCHAR(255) PRIMARY KEY NOT NULL, topiaversion BIGINT NOT NULL, inputSizeMeasureType VARCHAR(255) NOT NULL, outputSizeMeasureType VARCHAR(255) NOT NULL, topiacreatedate TIMESTAMP, uri VARCHAR(255), status INTEGER DEFAULT 1, coefficients VARCHAR(255), inputOutputFormula VARCHAR(255), outputInputFormula VARCHAR(255), code VARCHAR(255) DEFAULT 0, needcomment BOOLEAN DEFAULT false, lastupdatedate TIMESTAMP DEFAULT CURRENT_TIMESTAMP [...] +ALTER TABLE observe_common.lengthlengthparameter ADD CONSTRAINT fk_lengthlengthparameter_inputSizeMeasureType FOREIGN KEY (inputSizeMeasureType) REFERENCES observe_common.sizeMeasureType (topiaid); +ALTER TABLE observe_common.lengthlengthparameter ADD CONSTRAINT fk_lengthlengthparameter_outputSizeMeasureType FOREIGN KEY (outputSizeMeasureType) REFERENCES observe_common.sizeMeasureType (topiaid); + +CREATE INDEX index_observe_common_lengthlengthparameter_lastupdatedate ON observe_common.lengthlengthparameter (lastupdatedate); + +INSERT INTO observe_common.LASTUPDATEDATE (topiaId, topiaversion, topiacreatedate, TYPE , LASTUPDATEDATE) values ('fr.ird.observe.entities.referentiel.LastUpdateDate#666#2', 0,CURRENT_DATE, 'fr.ird.observe.entities.referentiel.LengthLengthParameter', CURRENT_TIMESTAMP); diff --git a/entities-migration/src/main/resources/db/migration/5.1/V5_1_18_evol_8404_lengthLengthParameter-PG.sql b/entities-migration/src/main/resources/db/migration/5.1/V5_1_18_evol_8404_lengthLengthParameter-PG.sql new file mode 100644 index 0000000..54f8d18 --- /dev/null +++ b/entities-migration/src/main/resources/db/migration/5.1/V5_1_18_evol_8404_lengthLengthParameter-PG.sql @@ -0,0 +1,14 @@ +ALTER TABLE observe_seine.nontargetlength ADD COLUMN sizeMeasureType character varying(255); +ALTER TABLE observe_seine.nontargetlength ADD CONSTRAINT FK_NON_TARGET_LENGTH_SIZE_MEASURE_TYPE FOREIGN KEY(sizeMeasureType) REFERENCES OBSERVE_COMMON.sizemeasuretype(topiaid); + +ALTER TABLE observe_common.lengthweightparameter ADD COLUMN sizeMeasureType character varying(255); +ALTER TABLE observe_common.lengthweightparameter ADD CONSTRAINT FK_LENGTH_WEIGHT_PARAMETER_SIZE_MEASURE_TYPE FOREIGN KEY(sizeMeasureType) REFERENCES OBSERVE_COMMON.sizemeasuretype(topiaid); +UPDATE observe_common.lengthweightparameter SET sizeMeasureType = ( SELECT sizeMeasureType FROM observe_common.species s WHERE s.topiaId = species); + +CREATE TABLE observe_common.lengthlengthparameter ( topiaid character varying(255) PRIMARY KEY NOT NULL, topiaversion BIGINT NOT NULL, inputSizeMeasureType character varying(255) NOT NULL, outputSizeMeasureType character varying(255) NOT NULL, topiacreatedate TIMESTAMP, uri character varying(255), status INTEGER DEFAULT 1, coefficients character varying(255), inputOutputFormula character varying(255), outputInputFormula character varying(255), code character varying(255) DEFAULT 0, needc [...] +ALTER TABLE observe_common.lengthlengthparameter ADD CONSTRAINT fk_lengthlengthparameter_inputSizeMeasureType FOREIGN KEY (inputSizeMeasureType) REFERENCES observe_common.sizeMeasureType (topiaid); +ALTER TABLE observe_common.lengthlengthparameter ADD CONSTRAINT fk_lengthlengthparameter_outputSizeMeasureType FOREIGN KEY (outputSizeMeasureType) REFERENCES observe_common.sizeMeasureType (topiaid); + +CREATE INDEX index_observe_common_lengthlengthparameter_lastupdatedate ON lengthlengthparameter (lastupdatedate); + +INSERT INTO observe_common.LASTUPDATEDATE (topiaId, topiaversion, topiacreatedate, TYPE , LASTUPDATEDATE) values ('fr.ird.observe.entities.referentiel.LastUpdateDate#666#2', 0,CURRENT_DATE, 'fr.ird.observe.entities.referentiel.LengthLengthParameter', CURRENT_TIMESTAMP); diff --git a/entities/src/main/java/fr/ird/observe/entities/Entities.java b/entities/src/main/java/fr/ird/observe/entities/Entities.java index bf66220..33f3682 100644 --- a/entities/src/main/java/fr/ird/observe/entities/Entities.java +++ b/entities/src/main/java/fr/ird/observe/entities/Entities.java @@ -65,6 +65,7 @@ public class Entities { ObserveEntityEnum.SpeciesList, ObserveEntityEnum.Person, ObserveEntityEnum.Organism, + ObserveEntityEnum.LengthLengthParameter, ObserveEntityEnum.LengthWeightParameter, ObserveEntityEnum.Program, ObserveEntityEnum.GearCaracteristicType, diff --git a/entities/src/main/java/fr/ird/observe/entities/migration/AbstractObserveMigrationCallBack.java b/entities/src/main/java/fr/ird/observe/entities/migration/AbstractObserveMigrationCallBack.java index 7dfdcfa..f5838ab 100644 --- a/entities/src/main/java/fr/ird/observe/entities/migration/AbstractObserveMigrationCallBack.java +++ b/entities/src/main/java/fr/ird/observe/entities/migration/AbstractObserveMigrationCallBack.java @@ -92,7 +92,7 @@ public abstract class AbstractObserveMigrationCallBack extends TopiaMigrationCal String migrationScript = prefix + "-" + scriptSuffix + ".sql"; String scriptPath = "/db/migration/"; if (getClass().getName().contains(".old.")) { - scriptPath += "/old/"; + scriptPath += "old/"; } scriptPath += this.version + "/V" + version.getValidName() + "_" + rank + "_" + migrationScript; diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/FormulaHelper.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/FormulaHelper.java new file mode 100644 index 0000000..10ea378 --- /dev/null +++ b/entities/src/main/java/fr/ird/observe/entities/referentiel/FormulaHelper.java @@ -0,0 +1,169 @@ +package fr.ird.observe.entities.referentiel; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.NumberUtil; + +import javax.script.Bindings; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import java.util.Map; +import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created on 05/11/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 5.1 + */ +public class FormulaHelper { + + /** Logger */ + private static final Log log = LogFactory.getLog(FormulaHelper.class); + + private static final Pattern COEFFICIENTS_PATTERN = Pattern.compile("(.+)=(.+)"); + + /** variable weight à utiliser dans la relation taille */ + public static final String VARIABLE_WEIGHT = "P"; + + /** variable taille à utiliser dans la relation weight */ + public static final String VARIABLE_LENGTH = "L"; + + public static final String VARIABLE_INPUT = "I"; + + public static final String VARIABLE_OUTPUT = "O"; + + public static final String COEFFICIENT_A = "a"; + + public static final String COEFFICIENT_B = "b"; + + /** moteur d'évaluation d'expression */ + protected static ScriptEngine scriptEngine; + + + protected static ScriptEngine getScriptEngine() { + if (scriptEngine == null) { + ScriptEngineManager factory = new ScriptEngineManager(); + + scriptEngine = factory.getEngineByExtension("js"); + } + return scriptEngine; + } + + public static Map<String, Double> getCoefficientValues(FormulaSupport parametrage) { + + Map<String, Double> result = new TreeMap<>(); + String coefficients = parametrage.getCoefficients(); + if (coefficients != null) { + for (String coefficientDef : coefficients.split(":")) { + Matcher matcher = COEFFICIENTS_PATTERN.matcher(coefficientDef.trim()); + if (log.isDebugEnabled()) { + log.debug("constant to test = " + coefficientDef); + } + if (matcher.matches()) { + + String key = matcher.group(1); + String val = matcher.group(2); + try { + Double d = Double.valueOf(val); + result.put(key, d); + if (log.isDebugEnabled()) { + log.debug("detects coefficient " + key + '=' + val); + } + } catch (NumberFormatException e) { + // pas pu recupere le count... + if (log.isWarnEnabled()) { + log.warn("could not parse double " + val + " for coefficient " + key); + } + } + } + } + } + return result; + } + + protected static boolean validateRelation(FormulaSupport parametrage, String relation, String variable) { + boolean result = false; + if (!StringUtils.isEmpty(relation)) { + + Map<String, Double> coeffs = parametrage.getCoefficientValues(); + + ScriptEngine engine = getScriptEngine(); + Bindings bindings = engine.createBindings(); + for (Map.Entry<String, Double> entry : coeffs.entrySet()) { + String key = entry.getKey(); + Double value = entry.getValue(); + bindings.put(key, value); + + if (log.isDebugEnabled()) { + log.debug("add constant " + key + '=' + value); + } + } + bindings.put(variable, 1); + + try { + engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); + Double o = (Double) engine.eval("parseFloat(" + relation + ")"); + if (log.isDebugEnabled()) { + log.debug("evaluation ok : " + relation + " (" + variable + "=1) = " + o); + } + result = true; + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("evalution ko : " + relation + ", reason : " + e.getMessage()); + } + } + } + return result; + } + + public static Float computeValue(FormulaSupport parametrage, String formula, String coefficientName, String variableName, float data) { + if (coefficientName!=null) { + Double b = parametrage.getCoefficientValue(coefficientName); + if (b == 0) { + + // ce cas limite ne permet pas de calculer la taille a partir du weight + return null; + } + } + Float o = computeValue(parametrage, formula, variableName, data); + + if (o != null) { + o = NumberUtil.roundOneDigit(o); + } + return o; + } + + private static Float computeValue(FormulaSupport parametrage, String relation, String variable, float taille) { + Map<String, Double> coeffs = parametrage.getCoefficientValues(); + ScriptEngine engine = getScriptEngine(); + Bindings bindings = engine.createBindings(); + for (Map.Entry<String, Double> entry : coeffs.entrySet()) { + String key = entry.getKey(); + Double value = entry.getValue(); + bindings.put(key, value); + + if (log.isDebugEnabled()) { + log.debug("add constant " + key + '=' + value); + } + } + bindings.put(variable, taille); + engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); + Double o = null; + try { + o = (Double) engine.eval("parseFloat(" + relation + ")"); + } catch (ScriptException e) { + if (log.isErrorEnabled()) { + log.error("Could not compute value from " + relation); + } + } + return o == null ? null : o.floatValue(); + } + + +} diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/FormulaSupportImpl.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/FormulaSupportImpl.java new file mode 100644 index 0000000..e724013 --- /dev/null +++ b/entities/src/main/java/fr/ird/observe/entities/referentiel/FormulaSupportImpl.java @@ -0,0 +1,88 @@ +package fr.ird.observe.entities.referentiel; + +import org.apache.commons.lang3.BooleanUtils; + +import java.util.Map; +import java.util.Set; + +/** + * Created on 05/11/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 5.1 + */ +public abstract class FormulaSupportImpl extends FormulaSupportAbstract { + + private static final long serialVersionUID = 1L; + + private Map<String, Double> coefficientValues; + private Boolean formulaOneValid; + private Boolean formulaTwoValid; + + @Override + public String getCode() { + // pas utilise + return null; + } + + @Override + public void setCode(String code) { + // pas utilise + } + + @Override + public void setCoefficients(String value) { + super.setCoefficients(value); + coefficientValues = null; + revalidateFormulaOne(); + revalidateFormulaTwo(); + } + + @Override + public Set<String> getCoefficientNames() { + return getCoefficientValues().keySet(); + } + + @Override + public Double getCoefficientValue(String coefficientName) { + return getCoefficientValues().get(coefficientName); + } + + @Override + public Map<String, Double> getCoefficientValues() { + if (coefficientValues == null) { + coefficientValues = FormulaHelper.getCoefficientValues(this); + } + return coefficientValues; + } + + + @Override + public boolean isFormulaOneValid() { + if (formulaOneValid == null) { + revalidateFormulaOne(); + } + return BooleanUtils.isTrue(formulaOneValid); + } + + @Override + public boolean isFormulaTwoValid() { + if (formulaTwoValid == null) { + revalidateFormulaTwo(); + } + return BooleanUtils.isTrue(formulaTwoValid); + } + + @Override + public void setFormulaOneValid(boolean formulaOneValid) { + this.formulaOneValid = formulaOneValid; + fireOnPostWrite(PROPERTY_FORMULA_ONE_VALID, null, formulaOneValid); + } + + @Override + public void setFormulaTwoValid(boolean formulaTwoValid) { + this.formulaTwoValid = formulaTwoValid; + fireOnPostWrite(PROPERTY_FORMULA_TWO_VALID, null, formulaTwoValid); + } + +} diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthLengthParameterImpl.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthLengthParameterImpl.java new file mode 100644 index 0000000..4be7a2f --- /dev/null +++ b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthLengthParameterImpl.java @@ -0,0 +1,55 @@ +package fr.ird.observe.entities.referentiel; + +/** + * Created on 05/11/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 5.1 + */ +public class LengthLengthParameterImpl extends LengthLengthParameterAbstract { + + @Override + public void setInputOutputFormula(String inputOutputFormula) { + super.setInputOutputFormula(inputOutputFormula); + revalidateFormulaOne(); + } + + @Override + public void setOutputInputFormula(String outputInputFormula) { + super.setOutputInputFormula(outputInputFormula); + revalidateFormulaTwo(); + } + + @Override + public Float computeFromFomulaOne(float data) { + return FormulaHelper.computeValue(this, inputOutputFormula, null, FormulaHelper.VARIABLE_INPUT, data); + } + + @Override + public Float computeFromFomulaTwo(float data) { + return FormulaHelper.computeValue(this, outputInputFormula, FormulaHelper.COEFFICIENT_B, FormulaHelper.VARIABLE_OUTPUT, data); + } + + @Override + public String getFormulaOne() { + return inputOutputFormula; + } + + @Override + public String getFormulaTwo() { + return outputInputFormula; + } + + @Override + public void revalidateFormulaOne() { + boolean result = FormulaHelper.validateRelation(this, inputOutputFormula, FormulaHelper.VARIABLE_INPUT); + setFormulaOneValid(result); + } + + @Override + public void revalidateFormulaTwo() { + boolean result = FormulaHelper.validateRelation(this, outputInputFormula, FormulaHelper.VARIABLE_OUTPUT); + setFormulaTwoValid(result); + } + +} diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthLengthParameters.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthLengthParameters.java new file mode 100644 index 0000000..d24f086 --- /dev/null +++ b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthLengthParameters.java @@ -0,0 +1,12 @@ +package fr.ird.observe.entities.referentiel; + +/** + * Created on 05/11/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 5.1 + */ +public class LengthLengthParameters { + + +} diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameterImpl.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameterImpl.java index 72ba143..8b1c687 100644 --- a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameterImpl.java +++ b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameterImpl.java @@ -21,127 +21,49 @@ */ package fr.ird.observe.entities.referentiel; -import java.util.Map; -import java.util.Set; - public class LengthWeightParameterImpl extends LengthWeightParameterAbstract { - private static final long serialVersionUID = 1L; - - protected Map<String, Double> coefficientValues; - - protected Boolean lengthWeightFormulaValid; - - protected Boolean weightLengthFormulaValid; - - //FIXME not generated with maven-release ? - public static final String PROPERTY_LENGTH_WEIGHT_FORMULA_VALID = "lengthWeightFormulaValid"; - - //FIXME not generated with maven-release ? - public static final String PROPERTY_WEIGHT_LENGTH_FORMULA_VALID = "weightLengthFormulaValid"; - - @Override - public String getCode() { - // pas utilise - return null; - } - - @Override - public void setCode(String code) { - // pas utilise - } - - @Override - public void setCoefficients(String value) { - super.setCoefficients(value); - coefficientValues = null; - revalidateRelationPoids(); - revalidateRelationTaille(); - } - - @Override - public Set<String> getCoefficientNames() { - return getCoefficientValues().keySet(); - } - - @Override - public Double getCoefficientValue(String coefficientName) { - return getCoefficientValues().get(coefficientName); - } - - @Override - public Map<String, Double> getCoefficientValues() { - if (coefficientValues == null) { - coefficientValues = - LengthWeightParemeterHelper.getCoefficientValues(this); - } - return coefficientValues; - } - @Override public void setLengthWeightFormula(String value) { super.setLengthWeightFormula(value); - revalidateRelationPoids(); + revalidateFormulaOne(); } @Override public void setWeightLengthFormula(String value) { super.setWeightLengthFormula(value); - revalidateRelationTaille(); + revalidateFormulaTwo(); } @Override - public boolean isLengthWeightFormulaValid() { - if (lengthWeightFormulaValid == null) { - revalidateRelationPoids(); - } - return lengthWeightFormulaValid != null && lengthWeightFormulaValid; + public void revalidateFormulaOne() { + boolean result = FormulaHelper.validateRelation(this, getLengthWeightFormula(), FormulaHelper.VARIABLE_LENGTH); + setFormulaOneValid(result); } @Override - public boolean isWeightLengthFormulaValid() { - if (weightLengthFormulaValid == null) { - revalidateRelationTaille(); - } - return weightLengthFormulaValid != null && weightLengthFormulaValid; + public void revalidateFormulaTwo() { + boolean result = FormulaHelper.validateRelation(this, getWeightLengthFormula(), FormulaHelper.VARIABLE_WEIGHT); + setFormulaTwoValid(result); } @Override - public void setLengthWeightFormulaValid(boolean lengthWeightFormulaValid) { - this.lengthWeightFormulaValid = lengthWeightFormulaValid; - fireOnPostWrite(PROPERTY_LENGTH_WEIGHT_FORMULA_VALID, null, lengthWeightFormulaValid); + public String getFormulaOne() { + return lengthWeightFormula; } @Override - public void setWeightLengthFormulaValid(boolean weightLengthFormulaValid) { - this.weightLengthFormulaValid = weightLengthFormulaValid; - fireOnPostWrite(PROPERTY_WEIGHT_LENGTH_FORMULA_VALID, null, weightLengthFormulaValid); + public String getFormulaTwo() { + return weightLengthFormula; } @Override - public Float computeWeight(float length) { - return LengthWeightParemeterHelper.computeWeight(this, length); + public Float computeFromFomulaOne(float data) { + return FormulaHelper.computeValue(this, lengthWeightFormula, null, FormulaHelper.VARIABLE_LENGTH, data); } @Override - public Float computeLength(float weight) { - return LengthWeightParemeterHelper.computeLength(this, weight); - } - - protected void revalidateRelationPoids() { - lengthWeightFormulaValid = null; - - // validate equation - boolean result = LengthWeightParemeterHelper.validateWeightRelation(this); - setLengthWeightFormulaValid(result); + public Float computeFromFomulaTwo(float data) { + return FormulaHelper.computeValue(this, weightLengthFormula, FormulaHelper.COEFFICIENT_B, FormulaHelper.VARIABLE_WEIGHT, data); } - - protected void revalidateRelationTaille() { - weightLengthFormulaValid = null; - - // validate equation - boolean result = LengthWeightParemeterHelper.validateLengthRelation(this); - setWeightLengthFormulaValid(result); - } - } diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameters.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameters.java index 0a83fee..a8c3c76 100644 --- a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameters.java +++ b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParameters.java @@ -24,25 +24,12 @@ package fr.ird.observe.entities.referentiel; import fr.ird.observe.ObserveTopiaDaoSupplier; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.NumberUtil; - -import javax.script.Bindings; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; + import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.TreeMap; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Created on 28/08/15. @@ -51,33 +38,6 @@ import java.util.regex.Pattern; */ public class LengthWeightParameters { - /** Logger. */ - private static final Log log = LogFactory.getLog(LengthWeightParameters.class); - - private static final Pattern COEFFICIENTS_PATTERN = Pattern.compile("(.+)=(.+)"); - - /** moteur d'évaluation d'expression */ - protected static ScriptEngine scriptEngine; - - /** variable weight à utiliser dans la relation taille */ - public static final String VARIABLE_POIDS = "P"; - - /** variable taille à utiliser dans la relation weight */ - public static final String VARIABLE_TAILLE = "L"; - - private static final String COEFFICIENT_A = "a"; - - private static final String COEFFICIENT_B = "b"; - - protected static ScriptEngine getScriptEngine() { - if (scriptEngine == null) { - ScriptEngineManager factory = new ScriptEngineManager(); - - scriptEngine = factory.getEngineByExtension("js"); - } - return scriptEngine; - } - /** * Recherche d'un {@link LengthWeightParameter} à partir des paramètres donnés. * @@ -149,6 +109,7 @@ public class LengthWeightParameters { return list.get(0); } + /** * Recherche de la liste des {@link LengthWeightParameter} à partir des paramètres donnés. * @@ -237,90 +198,7 @@ public class LengthWeightParameters { return daoSupplier.getSexDao().forCodeEquals("0").findUnique(); } - public static Map<String, Double> getCoefficientValues(LengthWeightParameter parametrage) { - - Map<String, Double> result = new TreeMap<>(); - String coefficients = parametrage.getCoefficients(); - if (coefficients != null) { - for (String coefficientDef : coefficients.split(":")) { - Matcher matcher = COEFFICIENTS_PATTERN.matcher(coefficientDef.trim()); - if (log.isDebugEnabled()) { - log.debug("constant to test = " + coefficientDef); - } - if (matcher.matches()) { - - String key = matcher.group(1); - String val = matcher.group(2); - try { - Double d = Double.valueOf(val); - result.put(key, d); - if (log.isDebugEnabled()) { - log.debug("detects coefficient " + key + '=' + val); - } - } catch (NumberFormatException e) { - // pas pu recupere le count... - if (log.isWarnEnabled()) { - log.warn("could not parse dou" + COEFFICIENT_B + "le " + val + " for coefficient " + key); - } - } - } - } - } - return result; - } - - public static boolean validateWeightRelation(LengthWeightParameter parametrage) { - return validateRelation(parametrage, - parametrage.getLengthWeightFormula(), - VARIABLE_TAILLE - ); - } - - public static boolean validateLengthRelation(LengthWeightParameter parametrage) { - return validateRelation(parametrage, - parametrage.getWeightLengthFormula(), - VARIABLE_POIDS - ); - } - - public static Float computeLength(LengthWeightParameter parametrage, - float weight) { - Double b = parametrage.getCoefficientValue(COEFFICIENT_B); - if (b == 0) { - - // ce cas limite ne permet pas de calculer la taille a partir du weight - return null; - } - Float o = computeValue(parametrage, - parametrage.getWeightLengthFormula(), - VARIABLE_POIDS, - weight - ); - - if (o != null) { - o = NumberUtil.roundOneDigit(o); - } - return o; - } - - public static Float computeWeight(LengthWeightParameter parametrage, - float taille) { - - Float o = computeValue(parametrage, - parametrage.getLengthWeightFormula(), - VARIABLE_TAILLE, - taille - ); - - if (o != null) { - o = NumberUtil.roundTwoDigits(o); - } - return o; - } - - @SuppressWarnings({"unchecked"}) - public static List<LengthWeightParameter> findBySpecies(ObserveTopiaDaoSupplier daoSupplier, - Species species) { + public static List<LengthWeightParameter> findBySpecies(ObserveTopiaDaoSupplier daoSupplier, Species species) { LengthWeightParameterTopiaDao dao = daoSupplier.getLengthWeightParameterDao(); @@ -330,13 +208,13 @@ public class LengthWeightParameters { Iterator<LengthWeightParameter> itr = list.iterator(); while (itr.hasNext()) { LengthWeightParameter p = itr.next(); - Double a = p.getCoefficientValue(COEFFICIENT_A); + Double a = p.getCoefficientValue(FormulaHelper.COEFFICIENT_A); if (a == null || a == 0) { itr.remove(); continue; } - Double b = p.getCoefficientValue(COEFFICIENT_B); + Double b = p.getCoefficientValue(FormulaHelper.COEFFICIENT_B); // on autorise d'avoir b à 0 (mais cela ne permet plus de calculer la taille à partir du poids) // if (b == null || b == 0) { if (b == null) { @@ -384,7 +262,7 @@ public class LengthWeightParameters { for (LengthWeightParameter parametrageLengthWeight : list) { if (parametrageLengthWeight.getStartDate().before(startDate) || - parametrageLengthWeight.getStartDate().equals(startDate)) { + parametrageLengthWeight.getStartDate().equals(startDate)) { result.add(parametrageLengthWeight); } } @@ -412,8 +290,8 @@ public class LengthWeightParameters { Date date = parametrageLengthWeight.getEndDate(); if (date == null || - date.after(endDate) || - date.equals(endDate)) { + date.after(endDate) || + date.equals(endDate)) { result.add(parametrageLengthWeight); } } @@ -421,65 +299,4 @@ public class LengthWeightParameters { return result; } - protected static boolean validateRelation(LengthWeightParameter parametrage, String relation, String variable) { - boolean result = false; - if (!StringUtils.isEmpty(relation)) { - - Map<String, Double> coeffs = parametrage.getCoefficientValues(); - - ScriptEngine engine = getScriptEngine(); - Bindings bindings = engine.createBindings(); - for (Map.Entry<String, Double> entry : coeffs.entrySet()) { - String key = entry.getKey(); - Double value = entry.getValue(); - bindings.put(key, value); - - if (log.isDebugEnabled()) { - log.debug("add constant " + key + '=' + value); - } - } - bindings.put(variable, 1); - - try { - engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); - Double o = (Double) engine.eval("parseFloat(" + relation + ")"); - if (log.isDebugEnabled()) { - log.debug("evaluation ok : " + relation + " (" + variable + "=1) = " + o); - } - result = true; - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.debug("evalution ko : " + relation + ", reason : " + e.getMessage()); - } - } - } - return result; - } - - public static Float computeValue(LengthWeightParameter parametrage, String relation, String variable, float taille) { - Map<String, Double> coeffs = parametrage.getCoefficientValues(); - ScriptEngine engine = getScriptEngine(); - Bindings bindings = engine.createBindings(); - for (Map.Entry<String, Double> entry : coeffs.entrySet()) { - String key = entry.getKey(); - Double value = entry.getValue(); - bindings.put(key, value); - - if (log.isDebugEnabled()) { - log.debug("add constant " + key + '=' + value); - } - } - bindings.put(variable, taille); - engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); - Double o = null; - try { - o = (Double) engine.eval("parseFloat(" + relation + ")"); - } catch (ScriptException e) { - if (log.isErrorEnabled()) { - log.error("Could not compute value from " + relation); - } - } - return o == null ? null : o.floatValue(); - } - } diff --git a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelper.java b/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelper.java deleted file mode 100644 index 7460c18..0000000 --- a/entities/src/main/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelper.java +++ /dev/null @@ -1,332 +0,0 @@ -/* - * #%L - * ObServe :: Entities - * %% - * Copyright (C) 2008 - 2016 IRD, Code Lutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ird.observe.entities.referentiel; - -import fr.ird.observe.ObserveTopiaPersistenceContext; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.topia.persistence.TopiaDao; -import org.nuiton.util.NumberUtil; - -import javax.script.Bindings; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import java.util.ArrayList; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Classe utilitaire pour les traitements communs des implantations de - * {@link LengthWeightParameter}. - * - * @author Tony Chemit - chemit@codelutin.com - * @since 1.5 - * @deprecated Utiliser à la place LengthWeightParemeters - */ -@Deprecated -public class LengthWeightParemeterHelper { - - private static final Pattern COEFFICIENTS_PATTERN = - Pattern.compile("(.+)=(.+)"); - - /** Logger */ - private static final Log log = - LogFactory.getLog(LengthWeightParemeterHelper.class); - - /** moteur d'évaluation d'expression */ - protected static ScriptEngine scriptEngine; - - /** variable weight à utiliser dans la relation taille */ - public static final String VARIABLE_POIDS = "P"; - - /** variable taille à utiliser dans la relation weight */ - public static final String VARIABLE_TAILLE = "L"; - - private static final String COEFFICIENT_A = "a"; - - private static final String COEFFICIENT_B = "b"; - - protected static ScriptEngine getScriptEngine() { - if (scriptEngine == null) { - ScriptEngineManager factory = new ScriptEngineManager(); - - scriptEngine = factory.getEngineByExtension("js"); - } - return scriptEngine; - } - - public static Map<String, Double> getCoefficientValues(LengthWeightParameter parametrage) { - - Map<String, Double> result = new TreeMap<>(); - String coefficients = parametrage.getCoefficients(); - if (coefficients != null) { - for (String coefficientDef : coefficients.split(":")) { - Matcher matcher = COEFFICIENTS_PATTERN.matcher(coefficientDef.trim()); - if (log.isDebugEnabled()) { - log.debug("constant to test = " + coefficientDef); - } - if (matcher.matches()) { - - String key = matcher.group(1); - String val = matcher.group(2); - try { - Double d = Double.valueOf(val); - result.put(key, d); - if (log.isDebugEnabled()) { - log.debug("detects coefficient " + key + '=' + val); - } - } catch (NumberFormatException e) { - // pas pu recupere le count... - if (log.isWarnEnabled()) { - log.warn("could not parse dou" + COEFFICIENT_B + "le " + val + " for coefficient " + key); - } - } - } - } - } - return result; - } - - public static boolean validateWeightRelation(LengthWeightParameter parametrage) { - return validateRelation(parametrage, - parametrage.getLengthWeightFormula(), - VARIABLE_TAILLE - ); - } - - public static boolean validateLengthRelation(LengthWeightParameter parametrage) { - return validateRelation(parametrage, - parametrage.getWeightLengthFormula(), - VARIABLE_POIDS - ); - } - - public static Float computeLength(LengthWeightParameter parametrage, - float weight) { - Double b = parametrage.getCoefficientValue(COEFFICIENT_B); - if (b == 0) { - - // ce cas limite ne permet pas de calculer la taille a partir du weight - return null; - } - Float o = computeValue(parametrage, - parametrage.getWeightLengthFormula(), - VARIABLE_POIDS, - weight - ); - - if (o != null) { - o = NumberUtil.roundOneDigit(o); - } - return o; - } - - public static Float computeWeight(LengthWeightParameter parametrage, - float taille) { - - Float o = computeValue(parametrage, - parametrage.getLengthWeightFormula(), - VARIABLE_TAILLE, - taille - ); - - if (o != null) { - o = NumberUtil.roundTwoDigits(o); - } - return o; - } - - @SuppressWarnings({"unchecked"}) - public static <P extends LengthWeightParameter> List<P> findBySpecies(ObserveTopiaPersistenceContext tx, - Species taillePoidsAble) { - - // le type de resultat recherche - Class<P> entityClass = (Class<P>) LengthWeightParameter.class; - - TopiaDao<P> dao = tx.getDao(entityClass); - - List<P> list = dao.forProperties(LengthWeightParameter.PROPERTY_SPECIES, taillePoidsAble).findAll(); - - // on supprime les paramétrages qui ont a=0 ou a=null ou b=0 ou b = null - Iterator<P> itr = list.iterator(); - while (itr.hasNext()) { - P p = itr.next(); - Double a = p.getCoefficientValue(COEFFICIENT_A); - if (a == null || a == 0) { - itr.remove(); - continue; - } - - Double b = p.getCoefficientValue(COEFFICIENT_B); - // on autorise d'avoir b à 0 (mais cela ne permet plus de calculer la taille à partir du poids) -// if (b == null || b == 0) { - if (b == null) { - itr.remove(); - } - } - return list; - - } - - public static <P extends LengthWeightParameter> List<P> filterByOcean(List<P> list, Ocean ocean) { - List<P> result = new ArrayList<>(); - if (ocean == null) { - - // on n'accepte que les parametrage sans ocean - for (P parametrageLengthWeight : list) { - if (parametrageLengthWeight.getOcean() == null) { - result.add(parametrageLengthWeight); - } - } - } else { - for (P parametrageLengthWeight : list) { - if (ocean.equals(parametrageLengthWeight.getOcean())) { - result.add(parametrageLengthWeight); - } - } - } - return result; - } - - public static <P extends LengthWeightParameter> List<P> filterBySexe(List<P> list, Sex sex) { - List<P> result = new ArrayList<>(); - - for (P parametrageLengthWeight : list) { - if (parametrageLengthWeight.getSex() == sex) { - result.add(parametrageLengthWeight); - } - } - return result; - } - - public static <P extends LengthWeightParameter> List<P> filterByDateDebutValidite(List<P> list, Date startDate) { - List<P> result = new ArrayList<>(); - - for (P parametrageLengthWeight : list) { - - if (parametrageLengthWeight.getStartDate().before(startDate) || - parametrageLengthWeight.getStartDate().equals(startDate)) { - result.add(parametrageLengthWeight); - } - } - return result; - } - - public static <P extends LengthWeightParameter> List<P> filterByDateFinValidite(List<P> list, Date endDate) { - List<P> result = new ArrayList<>(); - - if (endDate == null) { - // on n'accepte que les parametrages selon les critères suivants : - // - sans date de fin (i.e en cours de validite) - for (P parametrageLengthWeight : list) { - - Date date = parametrageLengthWeight.getEndDate(); - if (date == null) { - result.add(parametrageLengthWeight); - } - } - } else { - // on n'accepte que les parametrages selon les critères suivants : - // - sans date de fin (i.e en cours de validite) - // - ceux dont la date de fin est avant la date de fin donnée - for (P parametrageLengthWeight : list) { - - Date date = parametrageLengthWeight.getEndDate(); - if (date == null || - date.after(endDate) || - date.equals(endDate)) { - result.add(parametrageLengthWeight); - } - } - } - return result; - } - - protected static boolean validateRelation(LengthWeightParameter parametrage, String relation, String variable) { - boolean result = false; - if (!StringUtils.isEmpty(relation)) { - - Map<String, Double> coeffs = parametrage.getCoefficientValues(); - - ScriptEngine engine = getScriptEngine(); - Bindings bindings = engine.createBindings(); - for (Map.Entry<String, Double> entry : coeffs.entrySet()) { - String key = entry.getKey(); - Double value = entry.getValue(); - bindings.put(key, value); - - if (log.isDebugEnabled()) { - log.debug("add constant " + key + '=' + value); - } - } - bindings.put(variable, 1); - - try { - engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); - Double o = (Double) engine.eval("parseFloat(" + relation + ")"); - if (log.isDebugEnabled()) { - log.debug("evaluation ok : " + relation + " (" + variable + "=1) = " + o); - } - result = true; - } catch (Exception e) { - if (log.isDebugEnabled()) { - log.debug("evalution ko : " + relation + ", reason : " + e.getMessage()); - } - } - } - return result; - } - - public static Float computeValue(LengthWeightParameter parametrage, String relation, String variable, float taille) { - Map<String, Double> coeffs = parametrage.getCoefficientValues(); - ScriptEngine engine = getScriptEngine(); - Bindings bindings = engine.createBindings(); - for (Map.Entry<String, Double> entry : coeffs.entrySet()) { - String key = entry.getKey(); - Double value = entry.getValue(); - bindings.put(key, value); - - if (log.isDebugEnabled()) { - log.debug("add constant " + key + '=' + value); - } - } - bindings.put(variable, taille); - engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); - Double o = null; - try { - o = (Double) engine.eval("parseFloat(" + relation + ")"); - } catch (ScriptException e) { - if (log.isErrorEnabled()) { - log.error("Could not compute value from " + relation); - } - } - return o == null ? null : o.floatValue(); - } -} diff --git a/entities/src/main/xmi/observe-common.zargo b/entities/src/main/xmi/observe-common.zargo index 40415f1..17d14db 100644 Binary files a/entities/src/main/xmi/observe-common.zargo and b/entities/src/main/xmi/observe-common.zargo differ diff --git a/entities/src/main/xmi/observe-seine.zargo b/entities/src/main/xmi/observe-seine.zargo index 7886e18..f87b2aa 100644 Binary files a/entities/src/main/xmi/observe-seine.zargo and b/entities/src/main/xmi/observe-seine.zargo differ diff --git a/entities/src/main/xmi/observe.ini b/entities/src/main/xmi/observe.ini index f1ca6ad..51d0247 100644 --- a/entities/src/main/xmi/observe.ini +++ b/entities/src/main/xmi/observe.ini @@ -152,14 +152,21 @@ gearCaracteristic.lazy = false [class fr.ird.observe.entities.referentiel.GearCaracteristic] gearCaracteristicType.lazy = false +[class fr.ird.observe.entities.referentiel.FormulaSupport] +source.hibernateAttributeType = text + +[class fr.ird.observe.entities.referentiel.LengthLengthParameter] +inputSizeMeasureType.lazy = false +outputSizeMeasureType.lazy = false + [class fr.ird.observe.entities.referentiel.LengthWeightParameter] ocean.lazy = false ocean.notNull = true sex.lazy = false sex.notNull = true -source.hibernateAttributeType = text species.lazy = false species.notNull = true +sizeMeasureType.lazy = false [class fr.ird.observe.entities.referentiel.ObserveReferentialEntity] needComment.notNull = true diff --git a/entities/src/test/java/fr/ird/observe/entities/referentiel/FormulaHelperTest.java b/entities/src/test/java/fr/ird/observe/entities/referentiel/FormulaHelperTest.java new file mode 100644 index 0000000..18e9195 --- /dev/null +++ b/entities/src/test/java/fr/ird/observe/entities/referentiel/FormulaHelperTest.java @@ -0,0 +1,27 @@ +package fr.ird.observe.entities.referentiel; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created on 05/11/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 5.1 + */ +public class FormulaHelperTest { + + @Test + public void testComputeValue() { + + LengthWeightParameterImpl parametrage = new LengthWeightParameterImpl(); + parametrage.setCoefficients("a=3.8e-5:b=2.78 "); + parametrage.setLengthWeightFormula("a * Math.pow(L, b)"); + Assert.assertTrue(parametrage.isFormulaOneValid()); + Float weight = FormulaHelper.computeValue(parametrage, "a * Math.pow(L, b)", null, "L", 84.0f); + Assert.assertNotNull(weight); + + Float excepted = (float) (Math.pow(84.0, 2.78) * 3.8e-5); + Assert.assertEquals(excepted, weight, 2); + } +} diff --git a/entities/src/test/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelperTest.java b/entities/src/test/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelperTest.java deleted file mode 100644 index 9498cef..0000000 --- a/entities/src/test/java/fr/ird/observe/entities/referentiel/LengthWeightParemeterHelperTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * #%L - * ObServe :: Entities - * %% - * Copyright (C) 2008 - 2016 IRD, Code Lutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ird.observe.entities.referentiel; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Test de la classe {@link LengthWeightParemeterHelper}. - * - * @author Tony Chemit - chemit@codelutin.com - * @since 1.8 - */ -public class LengthWeightParemeterHelperTest { - - @Test - public void testComputeValue() { - Float weight; - - LengthWeightParameter parametrage = new LengthWeightParameterImpl(); - parametrage.setCoefficients("a=3.8e-5:b=2.78 "); - parametrage.setLengthWeightFormula("a * Math.pow(L, b)"); - Assert.assertTrue(parametrage.isLengthWeightFormulaValid()); - weight = LengthWeightParemeterHelper.computeWeight(parametrage, 84.0f); - - Double excepted = Math.pow(84.0, 2.78) * 3.8e-5; - Assert.assertEquals(excepted, weight, 2); - } -} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.
participants (1)
-
codelutin.com scm