diff --git a/nb-configuration.xml b/nb-configuration.xml index 6995d35469afb453779bdafd31d4626163522c32..8a89e6deb1612912550a17f804b7132c7c3a2b6c 100644 --- a/nb-configuration.xml +++ b/nb-configuration.xml @@ -15,6 +15,6 @@ Any value defined here will override the pom.xml file value but is only applicab --> <netbeans.hint.license>default_1</netbeans.hint.license> <org-netbeans-modules-javascript2-editor.jsversion>ECMA5</org-netbeans-modules-javascript2-editor.jsversion> - <netbeans.hint.jdkPlatform>JDK_1.8_SUN</netbeans.hint.jdkPlatform> + <netbeans.hint.jdkPlatform>JDK_1.8__GraalVM_1.0_</netbeans.hint.jdkPlatform> </properties> </project-shared-configuration> diff --git a/pom.xml b/pom.xml index 9c0c93d3c4b10023b6c32d3b592d9943e15750c3..aa2736771087603066e0a6ce29c474e6ba584bf1 100644 --- a/pom.xml +++ b/pom.xml @@ -29,24 +29,19 @@ <version>5.3.1</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.seleniumhq.selenium</groupId> - <artifactId>selenium-java</artifactId> - <scope>test</scope> - <version>2.44.0</version> - </dependency> - <dependency> - <groupId>com.opera</groupId> - <artifactId>operadriver</artifactId> - <scope>test</scope> - <version>1.5</version> - <exclusions> - <exclusion> - <groupId>org.seleniumhq.selenium</groupId> - <artifactId>selenium-remote-driver</artifactId> - </exclusion> - </exclusions> - </dependency> + + <dependency> + <groupId>org.graalvm.sdk</groupId> + <artifactId>graal-sdk</artifactId> + <version>1.0.0-rc12</version> + </dependency> + <dependency> + <groupId>com.eclipsesource.j2v8</groupId> + <artifactId>j2v8_linux_x86_64</artifactId> + <version>4.8.0</version> + </dependency> + + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> diff --git a/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModel.java b/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModel.java index 2999eaf462e243494a36bc845edfde562aa85c1f..d1fe7368b9a5d8daf6ff8538b35e37ef34ed65bc 100644 --- a/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModel.java +++ b/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModel.java @@ -51,9 +51,9 @@ public class FallArmywormModel extends I18nImpl implements Model{ private Date plantingDate; private String intercroppingType; private final Map<String, Double> INTERCROPPING_RISK_REDUCTION; - Integer RISK_THRESHOLD_HIGH = 100; + Integer RISK_THRESHOLD_HIGH = 50; Double OBSERVATIONS_10KM_ABSCENCE_RISK_REDUCTION = 10d/100; // 10% risk reduction if no observations within 10km - Double OBSERVATIONS_25KM_ABSCENCE_RISK_REDUCTION = 60d/100; // 60% risk reduction if no observations within 25km + Double OBSERVATIONS_25KM_ABSCENCE_RISK_REDUCTION = 70d/100; // 60% risk reduction if no observations within 25km public final static ModelId MODEL_ID = new ModelId("FAWMODEL01"); private final ModelUtil modelUtil; @@ -154,7 +154,7 @@ public class FallArmywormModel extends I18nImpl implements Model{ @Override public String getLicense() { return - " Copyright (c) 2019 FAO <http://www.fao.org/>. \n" + + " Copyright (c) 2019 NIBIO <http://www.nibio.no/>. \n" + "\n" + " This program is free software: you can redistribute it and/or modify\n" + " it under the terms of the GNU Affero General Public License as\n" + @@ -174,7 +174,7 @@ public class FallArmywormModel extends I18nImpl implements Model{ @Override public String getCopyright() { - return "(c) 2019 FAO (http://www.fao.org/)."; + return "(c) 2019 NIBIO (http://www.nibio.no/)."; } diff --git a/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJSAdapter.java b/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelGraalVMAdapter.java similarity index 79% rename from src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJSAdapter.java rename to src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelGraalVMAdapter.java index c88b05750d1aef41be3e885efcda454fd8ca8120..e242a4e4192ff688dea7feecef2186baec320548 100644 --- a/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJSAdapter.java +++ b/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelGraalVMAdapter.java @@ -23,61 +23,63 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.io.InputStreamReader; -import java.util.Date; +import java.io.Reader; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; -import javax.script.Invocable; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; import javax.script.ScriptException; -import jdk.nashorn.api.scripting.ScriptObjectMirror; import no.nibio.vips.entity.ModelConfiguration; import no.nibio.vips.entity.Result; -import no.nibio.vips.entity.ResultImpl; import no.nibio.vips.i18n.I18nImpl; import no.nibio.vips.model.ConfigValidationException; import no.nibio.vips.model.Model; import no.nibio.vips.model.ModelExcecutionException; import no.nibio.vips.model.ModelId; import no.nibio.vips.util.ModelUtil; +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Source; +import org.graalvm.polyglot.Value; /** * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ -public class FallArmywormModelJSAdapter extends I18nImpl { +public class FallArmywormModelGraalVMAdapter extends I18nImpl implements Model{ - protected ScriptEngine engine; + protected Context jsContext; + protected Value jsBindings; - public final static ModelId MODEL_ID = new ModelId("FAWMODEL01"); + public final static ModelId MODEL_ID = new ModelId("FAWMODEL02"); private final ModelUtil modelUtil; - public FallArmywormModelJSAdapter() { + public FallArmywormModelGraalVMAdapter() { super("no.nibio.vips.model.fallarmywormmodel.texts"); - ScriptEngineManager sem = new ScriptEngineManager(); - engine = sem.getEngineByName("nashorn"); - modelUtil = new ModelUtil(); - try { - - this.addJavaScript("/js/moment.min.js"); - this.addJavaScript("/js/moment-timezone-with-data.min.js"); - this.addJavaScript("/js/FallArmywormModelImpl.js"); - } catch (InstantiationException ex) { - Logger.getLogger(FallArmywormModelJSAdapter.class.getName()).log(Level.SEVERE, null, ex); - } + modelUtil = new ModelUtil(); + try { + jsContext = Context.create("js"); + jsBindings = jsContext.getBindings("js"); + this.addJavaScript("/js/moment.min.js"); + this.addJavaScript("/js/moment-timezone-with-data.min.js"); + this.addJavaScript("/js/FallArmywormModelImpl.js"); + } catch (IOException ex) { + Logger.getLogger(FallArmywormModelGraalVMAdapter.class.getName()).log(Level.SEVERE, null, ex); + } } + + public List<Result> getResult() throws ModelExcecutionException { - try - { + //try + //{ ObjectMapper mapper = new ObjectMapper(); - ScriptObjectMirror jsObject = (ScriptObjectMirror) this.getRunnableJSWrapper().invokeFunction("getResult"); - List<Result> retVal = (List<Result>) jsObject.keySet().stream().map(key->{ + //ScriptObjectMirror jsObject = (ScriptObjectMirror) this.getRunnableJSWrapper().invokeFunction("getResult"); + List<Map> jsResultList = jsBindings.getMember("getResult").execute().as(List.class); + System.out.println("jsResultList size is " + jsResultList.size()); + /*List<Result> retVal = (List<Result>) jsObject.keySet().stream().map(key->{ Result r = new ResultImpl(); ScriptObjectMirror resultObject = (ScriptObjectMirror) jsObject.get(key); r.setWarningStatus((Integer)resultObject.get("warningStatus")); @@ -87,13 +89,14 @@ public class FallArmywormModelJSAdapter extends I18nImpl { ScriptObjectMirror allValues = (ScriptObjectMirror) resultObject.get("allValues"); r.setAllValues(allValues.to(Map.class)); return r; - }).collect(Collectors.toList()); - return retVal; - } - catch(ScriptException | NoSuchMethodException ex) + }).collect(Collectors.toList());*/ + //return retVal; + return new ArrayList<>(); + //} + /*catch(ScriptException | NoSuchMethodException ex) { throw new ModelExcecutionException("ERROR: There was a problem running the JavaScript code. Message is: " + ex.getMessage()); - } + }*/ } @@ -114,7 +117,7 @@ public class FallArmywormModelJSAdapter extends I18nImpl { public String getLicense() { return - " Copyright (c) 2019 FAO <http://www.fao.org/>. \n" + + " Copyright (c) 2019 NIBIO <http://www.nibio.no/>. \n" + "\n" + " This program is free software: you can redistribute it and/or modify\n" + " it under the terms of the GNU Affero General Public License as\n" + @@ -133,7 +136,7 @@ public class FallArmywormModelJSAdapter extends I18nImpl { public String getCopyright() { - return "(c) 2019 FAO (http://www.fao.org/)."; + return "(c) 2019 NIBIO (http://www.nibio.no/)."; } @@ -207,10 +210,11 @@ public class FallArmywormModelJSAdapter extends I18nImpl { } ObjectMapper mapper = new ObjectMapper(); String jsonInput = mapper.writeValueAsString(config); + jsBindings.getMember("setConfiguration").execute(jsonInput); //System.out.println(jsonInput); - this.getRunnableJSWrapper().invokeFunction("setConfiguration", jsonInput); + } - catch(ScriptException | NoSuchMethodException | JsonProcessingException ex) + catch(JsonProcessingException ex) { throw new ConfigValidationException("ERROR: There was a problem with running the JavaScript code. Message is: " + ex.getMessage()); } @@ -237,22 +241,11 @@ public class FallArmywormModelJSAdapter extends I18nImpl { * @param scriptPath * @throws ScriptException */ - protected final void addJavaScript(String scriptPath) throws InstantiationException + protected final void addJavaScript(String scriptPath) throws IOException { - try - { - InputStreamReader fr = new java.io.InputStreamReader(this.getClass().getResourceAsStream(scriptPath)); - engine.eval(fr); - }catch(ScriptException ex) - { - throw new InstantiationException(ex.getMessage()); - } + Reader stream = new InputStreamReader(this.getClass().getResourceAsStream(scriptPath)); + Source source = Source.newBuilder("js", stream, "JavaScript").build(); + jsContext.eval(source); } - protected Invocable getRunnableJSWrapper() - { - return (Invocable) engine; - } - - } diff --git a/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJ2V8Adapter.java b/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJ2V8Adapter.java new file mode 100644 index 0000000000000000000000000000000000000000..73acb405932598d9e76ebe81a9d9773c996b009f --- /dev/null +++ b/src/main/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJ2V8Adapter.java @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. + * + * This file is part of FallArmywormModel. + * FallArmywormModel is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FallArmywormModel 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with FallArmywormModel. If not, see <http://www.gnu.org/licenses/>. + * + */ + +package no.nibio.vips.model.fallarmywormmodel; + +import com.eclipsesource.v8.V8; +import com.eclipsesource.v8.V8Array; +import com.eclipsesource.v8.V8Object; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import javax.script.ScriptException; +import no.nibio.vips.entity.ModelConfiguration; +import no.nibio.vips.entity.Result; +import no.nibio.vips.entity.ResultImpl; +import no.nibio.vips.i18n.I18nImpl; +import no.nibio.vips.model.ConfigValidationException; +import no.nibio.vips.model.Model; +import no.nibio.vips.model.ModelExcecutionException; +import no.nibio.vips.model.ModelId; +import no.nibio.vips.util.ModelUtil; + + +/** + * Adapter for the Android compatible J2V8 JavaScript adapter: https://github.com/eclipsesource/j2v8 + * + * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class FallArmywormModelJ2V8Adapter extends I18nImpl implements Model{ + + V8 runtime; + + public final static ModelId MODEL_ID = new ModelId("FAWMODEL03"); + private final ModelUtil modelUtil; + + public FallArmywormModelJ2V8Adapter() { + super("no.nibio.vips.model.fallarmywormmodel.texts"); + modelUtil = new ModelUtil(); + try { + this.runtime = V8.createV8Runtime(); + //jsBindings = jsContext.getBindings("js"); + this.addJavaScript("/js/moment.min.js"); + this.addJavaScript("/js/moment-timezone-with-data.min.js"); + this.addJavaScript("/js/FallArmywormModelImpl.js"); + + } catch (IOException ex) { + Logger.getLogger(FallArmywormModelJ2V8Adapter.class.getName()).log(Level.SEVERE, null, ex); + } + } + + + + public List<Result> getResult() throws ModelExcecutionException { + //try + //{ + List<Result> retVal = new ArrayList<>(); + ObjectMapper mapper = new ObjectMapper(); + V8Array resultArray = this.runtime.executeArrayFunction("getResult",null); + //System.out.println("Got " + resultArray.length() + " result items back"); + for(Integer i=0;i<resultArray.length();i++) + { + V8Object resultObject = resultArray.getObject(i); + Result r = new ResultImpl(); + r.setWarningStatus((Integer)resultObject.get("warningStatus")); + + r.setValidTimeStart(mapper.convertValue((String)resultObject.getString("validTimeStart"), Date.class)); + r.setValidTimeEnd(mapper.convertValue((String)resultObject.getString("validTimeEnd"), Date.class)); + V8Object allValues = resultObject.getObject("allValues"); + for(String key: allValues.getKeys()) + { System.out.println("key=" + key); + + String keyWithoutNS = key.split("\\.")[key.split("\\.").length - 1]; + String nameSpace = key.split("\\.").length == 1 ? FallArmywormModelJ2V8Adapter.MODEL_ID.toString() : key.split("\\.")[0]; + r.setValue(nameSpace, keyWithoutNS, String.valueOf(allValues.get(key))); + } + retVal.add(r); + allValues.release(); + resultObject.release(); + + } + resultArray.release(); + String[] keysLeft = this.runtime.getKeys(); + System.out.println("Still keeping " + keysLeft[0]); + //this.releaseAll(); + this.runtime.release(); + + return retVal; + } + + private void releaseAll() + { + for(String key: this.runtime.getKeys()) + { + System.out.println("Trying to release " + key +", which is a " + V8.getStringRepresentation(this.runtime.getType(key))); + ((V8Object)this.runtime.get(key)).release(); + } + } + + + public ModelId getModelId() { + return MODEL_ID; + } + + + public String getModelName() { + return this.getModelName(Model.DEFAULT_LANGUAGE); + } + + + public String getModelName(String language) { + return this.getText("name", language); + } + + + public String getLicense() { + return + " Copyright (c) 2019 NIBIO <http://www.nibio.no/>. \n" + + "\n" + + " This program is free software: you can redistribute it and/or modify\n" + + " it under the terms of the GNU Affero General Public License as\n" + + " published by the Free Software Foundation, either version 3 of the\n" + + " License, or (at your option) any later version.\n" + + "\n" + + " This program is distributed in the hope that it will be useful,\n" + + " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + + " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + + " GNU Affero General Public License for more details.\n" + + "\n" + + " You should have received a copy of the GNU Affero General Public License\n" + + " along with this program. If not, see <https://www.gnu.org/licenses/>."; + + } + + + public String getCopyright() { + return "(c) 2019 NIBIO (http://www.nibio.no/)."; + } + + + public String getModelDescription() { + return this.getModelName(Model.DEFAULT_LANGUAGE); + } + + + public String getModelDescription(String language) { + try + { + return this.modelUtil.getTextWithBase64EncodedImages(this.getText("description", language), this.getClass()); + } + catch(IOException ex) + { + return this.getText("description", language); + } + } + + + public String getWarningStatusInterpretation() { + return this.getWarningStatusInterpretation(Model.DEFAULT_LANGUAGE); + } + + + public String getWarningStatusInterpretation(String language) { + return this.getText("statusInterpretation", language); + } + + + public String getModelUsage() { + return this.getModelUsage(Model.DEFAULT_LANGUAGE); + } + + + public String getModelUsage(String language) { + return this.getText("usage", language); + } + + + public String getSampleConfig() { + return "{\n" + + " \"modelId\": \"FAWMODEL01\",\n" + + " \"configParameters\": {\n" + + " \"longitude\": \"12.4885772\",\n" + + " \"latitude\": \"41.8822185\",\n" + + " \"plantingDate\": \"2019-02-02\",\n" + + " \"intercroppingType\": \"BEANS/DESMODIUM\",\n" + + " \"timeZone\": \"Europe/Rome\",\n" + + " \"observations10kmLast3Months\": 0,\n" + + " \"observations25kmLast3Months\": 0\n" + + " }\n" + + "}"; + } + + + public void setConfiguration(ModelConfiguration config) throws ConfigValidationException { + try + { + if(config.getConfigParameter("observations10kmLast3Months") == null || config.getConfigParameter("observations25kmLast3Months") == null) + { + Map<Integer, Integer> observations = this.getFAWObservations(Double.valueOf((String) config.getConfigParameter("longitude")), Double.valueOf((String)config.getConfigParameter("longitude"))); + if(config.getConfigParameter("observations10kmLast3Months") == null) + { + config.setConfigParameter("observations10kmLast3Months", observations.get(10)); + } + if(config.getConfigParameter("observations25kmLast3Months") == null) + { + config.setConfigParameter("observations25kmLast3Months", observations.get(25)); + } + } + ObjectMapper mapper = new ObjectMapper(); + String jsonInput = mapper.writeValueAsString(config); + V8Array params = new V8Array(runtime).push(jsonInput); + this.runtime.executeVoidFunction("setConfiguration", params); + params.release(); + //jsBindings.getMember("setConfiguration").execute(jsonInput); + //System.out.println(jsonInput); + + } + catch(JsonProcessingException ex) + { + throw new ConfigValidationException("ERROR: There was a problem with running the JavaScript code. Message is: " + ex.getMessage()); + } + } + + /** + * TODO: Actually pull the data from the FAMEWS server + * Need some help from Fabio Lana + * @param longitude + * @param latitude + * @return + */ + private Map<Integer, Integer> getFAWObservations(Double longitude, Double latitude) + { + + Map<Integer, Integer> retVal = new HashMap<>(); + retVal.put(10, 0); + retVal.put(25,40); + return retVal; + } + + /** + * Add JavaScript + * @param scriptPath + * @throws ScriptException + */ + protected final void addJavaScript(String scriptPath) throws IOException + { + String js = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(scriptPath))).lines().collect(Collectors.joining("\n")); + this.runtime.executeVoidScript(js); + } + +} diff --git a/src/main/resources/js/FallArmywormModelImpl.js b/src/main/resources/js/FallArmywormModelImpl.js index 83281d03a0ed8e554698249fb2c92d6219a6baee..2c625dec1137484deeaf49a88af9dd20728915fb 100644 --- a/src/main/resources/js/FallArmywormModelImpl.js +++ b/src/main/resources/js/FallArmywormModelImpl.js @@ -18,7 +18,7 @@ /** * The JavaScript implementation of the Fall Armyworm model - * @copyright 2019 FAO.org + * @copyright 2019 NIBIO.no * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ @@ -197,6 +197,8 @@ var setConfiguration = function (modelConfigurationUnknownType) TM = getDailyObservations(TMHourly, timeZone, 4); } */ + // TEST + }; /** diff --git a/src/test/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJSAdapterTest.java b/src/test/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJSAdapterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a5890344e4f316a3465d0ff0028aee53f5de3a2e --- /dev/null +++ b/src/test/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJSAdapterTest.java @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. + * + * This file is part of FallArmywormModel. + * FallArmywormModel is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FallArmywormModel 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with FallArmywormModel. If not, see <http://www.gnu.org/licenses/>. + * + */ +package no.nibio.vips.model.fallarmywormmodel; + +import no.nibio.vips.model.fallarmywormmodel.FallArmywormModel; +import java.util.List; +import no.nibio.vips.entity.ModelConfiguration; +import no.nibio.vips.entity.Result; +import no.nibio.vips.model.ModelId; +import no.nibio.vips.util.test.WeatherDataFileReader; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author treinar + */ +public class FallArmywormModelJSAdapterTest { + + public FallArmywormModelJSAdapterTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getResult method, of class FallArmywormModel. + */ + @Test + public void testGetResult() throws Exception { + System.out.println("getResult"); + WeatherDataFileReader fr = new WeatherDataFileReader(); + ModelConfiguration config = fr.getModelConfigurationWithWeatherData("/romeweather.json", "FAWMODEL01"); + config.setConfigParameter("observations", null); + config.setConfigParameter("longitude", "12.4885772"); + config.setConfigParameter("latitude", "41.8822185"); + config.setConfigParameter("plantingDate", "2019-02-02"); + config.setConfigParameter("timeZone", "Europe/Rome"); + config.setConfigParameter("intercroppingType", "BEANS/DESMODIUM"); + config.setConfigParameter("observations10kmLast3Months", 0); + config.setConfigParameter("observations25kmLast3Months", 0); + FallArmywormModel instance = new FallArmywormModel(); + instance.setConfiguration(config); + List<Result> result = instance.getResult(); + assertNotNull(result); + result.forEach(r->{System.out.println(r.getValidTimeStart() + " Accumulated RISK = " + String.valueOf(r.getValue(FallArmywormModel.MODEL_ID.toString(), "accumulatedRisk")) + ", warning status = " + r.getWarningStatus());}); + + } + + /** + * Test of getModelId method, of class FallArmywormModel. + */ + @Test + public void testGetModelId() { + + System.out.println("getModelId"); + FallArmywormModel instance = new FallArmywormModel(); + String expResult = "FAWMODEL01"; + ModelId result = instance.getModelId(); + assertEquals(expResult, result.toString()); + + + } + + /** + * Test of getModelName method, of class FallArmywormModel. + */ + @Test + public void testGetModelName_0args() { + + System.out.println("getModelName"); + FallArmywormModel instance = new FallArmywormModel(); + String expResult = "Fall Armyworm Model"; + String result = instance.getModelName(); + assertEquals(expResult, result); + + } + + /** + * Test of getModelName method, of class FallArmywormModel. + */ + @Test + public void testGetModelName_String() { + + System.out.println("getModelName"); + String language = "nb"; + FallArmywormModel instance = new FallArmywormModel(); + String expResult = "Fall Armyworm-modell"; + String result = instance.getModelName(language); + assertEquals(expResult, result); + + } + + /** + * Test of getLicense method, of class FallArmywormModel. + */ + @Test + public void testGetLicense() { + + System.out.println("getLicense"); + FallArmywormModel instance = new FallArmywormModel(); + String expResult = ""; + String result = instance.getLicense(); + assertNotNull(result); + + + } + + /** + * Test of getCopyright method, of class FallArmywormModel. + */ + @Test + public void testGetCopyright() { + + System.out.println("getCopyright"); + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getCopyright(); + assertNotNull(result); + + + } + + /** + * Test of getModelDescription method, of class FallArmywormModel. + */ + @Test + public void testGetModelDescription_0args() { + + System.out.println("getModelDescription"); + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getModelDescription(); + assertNotNull(result); + + } + + /** + * Test of getModelDescription method, of class FallArmywormModel. + */ + @Test + public void testGetModelDescription_String() { + + System.out.println("getModelDescription"); + String language = "nb"; + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getModelDescription(language); + assertNotNull(result); + + + } + + /** + * Test of getWarningStatusInterpretation method, of class FallArmywormModel. + */ + @Test + public void testGetWarningStatusInterpretation_0args() { + + System.out.println("getWarningStatusInterpretation"); + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getWarningStatusInterpretation(); + assertNotNull(result); + + } + + /** + * Test of getWarningStatusInterpretation method, of class FallArmywormModel. + */ + @Test + public void testGetWarningStatusInterpretation_String() { + + System.out.println("getWarningStatusInterpretation"); + String language = "nb"; + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getWarningStatusInterpretation(language); + assertNotNull(result); + + + } + + /** + * Test of getModelUsage method, of class FallArmywormModel. + */ + @Test + public void testGetModelUsage_0args() { + + System.out.println("getModelUsage"); + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getModelUsage(); + assertNotNull(result); + + + } + + /** + * Test of getModelUsage method, of class FallArmywormModel. + */ + @Test + public void testGetModelUsage_String() { + + System.out.println("getModelUsage"); + String language = "nb"; + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getModelUsage(language); + assertNotNull(result); + + } + + /** + * Test of getSampleConfig method, of class FallArmywormModel. + */ + @Test + public void testGetSampleConfig() { + + System.out.println("getSampleConfig"); + FallArmywormModel instance = new FallArmywormModel(); + String result = instance.getSampleConfig(); + assertNotNull(result); + + + } + + /** + * Test of setConfiguration method, of class FallArmywormModel. + */ + //@Test + public void testSetConfiguration() throws Exception { + System.out.println("setConfiguration"); + WeatherDataFileReader fr = new WeatherDataFileReader(); + ModelConfiguration config = fr.getModelConfigurationWithWeatherData("/romeweather.json", "FAWMODEL01"); + config.setConfigParameter("plantingDate", "2019-01-22+01"); + config.setConfigParameter("timeZone", "Europe/Rome"); + FallArmywormModel instance = new FallArmywormModel(); + instance.setConfiguration(config); + + } + +} diff --git a/src/test/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJV8AdapterTest.java b/src/test/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJV8AdapterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a134f7f1feb98036588bd3e8ec5d317d6f840857 --- /dev/null +++ b/src/test/java/no/nibio/vips/model/fallarmywormmodel/FallArmywormModelJV8AdapterTest.java @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. + * + * This file is part of FallArmywormModel. + * FallArmywormModel is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * FallArmywormModel 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with FallArmywormModel. If not, see <http://www.gnu.org/licenses/>. + * + */ +package no.nibio.vips.model.fallarmywormmodel; + + +import java.util.List; +import no.nibio.vips.entity.ModelConfiguration; +import no.nibio.vips.entity.Result; +import no.nibio.vips.model.ModelId; +import no.nibio.vips.util.test.WeatherDataFileReader; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author treinar + */ +public class FallArmywormModelJV8AdapterTest { + + public FallArmywormModelJV8AdapterTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getResult method, of class FallArmywormModel. + */ + @Test + public void testGetResult() throws Exception { + System.out.println("getResult"); + WeatherDataFileReader fr = new WeatherDataFileReader(); + ModelConfiguration config = fr.getModelConfigurationWithWeatherData("/romeweather.json", "FAWMODEL01"); + config.setConfigParameter("observations", null); + config.setConfigParameter("longitude", "12.4885772"); + config.setConfigParameter("latitude", "41.8822185"); + config.setConfigParameter("plantingDate", "2019-02-02"); + config.setConfigParameter("timeZone", "Europe/Rome"); + config.setConfigParameter("intercroppingType", "BEANS/DESMODIUM"); + config.setConfigParameter("observations10kmLast3Months", 0); + config.setConfigParameter("observations25kmLast3Months", 0); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + instance.setConfiguration(config); + List<Result> result = instance.getResult(); + assertNotNull(result); + result.forEach(r->{System.out.println(r.getValidTimeStart() + " Accumulated RISK = " + String.valueOf(r.getValue(FallArmywormModelJ2V8Adapter.MODEL_ID.toString(), "accumulatedRisk")) + ", warning status = " + r.getWarningStatus());}); + + } + + /** + * Test of getModelId method, of class FallArmywormModel. + */ + @Test + public void testGetModelId() { + + System.out.println("getModelId"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String expResult = "FAWMODEL03"; + ModelId result = instance.getModelId(); + assertEquals(expResult, result.toString()); + + + } + + /** + * Test of getModelName method, of class FallArmywormModel. + */ + @Test + public void testGetModelName_0args() { + + System.out.println("getModelName"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String expResult = "Fall Armyworm Model"; + String result = instance.getModelName(); + assertEquals(expResult, result); + + } + + /** + * Test of getModelName method, of class FallArmywormModel. + */ + @Test + public void testGetModelName_String() { + + System.out.println("getModelName"); + String language = "nb"; + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String expResult = "Fall Armyworm-modell"; + String result = instance.getModelName(language); + assertEquals(expResult, result); + + } + + /** + * Test of getLicense method, of class FallArmywormModel. + */ + @Test + public void testGetLicense() { + + System.out.println("getLicense"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String expResult = ""; + String result = instance.getLicense(); + assertNotNull(result); + + + } + + /** + * Test of getCopyright method, of class FallArmywormModel. + */ + @Test + public void testGetCopyright() { + + System.out.println("getCopyright"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getCopyright(); + assertNotNull(result); + + + } + + /** + * Test of getModelDescription method, of class FallArmywormModel. + */ + @Test + public void testGetModelDescription_0args() { + + System.out.println("getModelDescription"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getModelDescription(); + assertNotNull(result); + + } + + /** + * Test of getModelDescription method, of class FallArmywormModel. + */ + @Test + public void testGetModelDescription_String() { + + System.out.println("getModelDescription"); + String language = "nb"; + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getModelDescription(language); + assertNotNull(result); + + + } + + /** + * Test of getWarningStatusInterpretation method, of class FallArmywormModel. + */ + @Test + public void testGetWarningStatusInterpretation_0args() { + + System.out.println("getWarningStatusInterpretation"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getWarningStatusInterpretation(); + assertNotNull(result); + + } + + /** + * Test of getWarningStatusInterpretation method, of class FallArmywormModel. + */ + @Test + public void testGetWarningStatusInterpretation_String() { + + System.out.println("getWarningStatusInterpretation"); + String language = "nb"; + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getWarningStatusInterpretation(language); + assertNotNull(result); + + + } + + /** + * Test of getModelUsage method, of class FallArmywormModel. + */ + @Test + public void testGetModelUsage_0args() { + + System.out.println("getModelUsage"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getModelUsage(); + assertNotNull(result); + + + } + + /** + * Test of getModelUsage method, of class FallArmywormModel. + */ + @Test + public void testGetModelUsage_String() { + + System.out.println("getModelUsage"); + String language = "nb"; + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getModelUsage(language); + assertNotNull(result); + + } + + /** + * Test of getSampleConfig method, of class FallArmywormModel. + */ + @Test + public void testGetSampleConfig() { + + System.out.println("getSampleConfig"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + String result = instance.getSampleConfig(); + assertNotNull(result); + + + } + + /** + * Test of setConfiguration method, of class FallArmywormModel. + */ + //@Test + public void testSetConfiguration() throws Exception { + System.out.println("setConfiguration"); + WeatherDataFileReader fr = new WeatherDataFileReader(); + ModelConfiguration config = fr.getModelConfigurationWithWeatherData("/romeweather.json", "FAWMODEL01"); + config.setConfigParameter("plantingDate", "2019-01-22+01"); + config.setConfigParameter("timeZone", "Europe/Rome"); + FallArmywormModelJ2V8Adapter instance = new FallArmywormModelJ2V8Adapter(); + instance.setConfiguration(config); + + } + +}