Skip to content
Snippets Groups Projects
Commit ee8c7435 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Added GraalVM conceptual test and a working

implementation using J2V8 https://github.com/eclipsesource/j2v8
parent 198cee17
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,6 @@ Any value defined here will override the pom.xml file value but is only applicab ...@@ -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> <netbeans.hint.license>default_1</netbeans.hint.license>
<org-netbeans-modules-javascript2-editor.jsversion>ECMA5</org-netbeans-modules-javascript2-editor.jsversion> <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> </properties>
</project-shared-configuration> </project-shared-configuration>
...@@ -29,24 +29,19 @@ ...@@ -29,24 +29,19 @@
<version>5.3.1</version> <version>5.3.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId> <dependency>
<artifactId>selenium-java</artifactId> <groupId>org.graalvm.sdk</groupId>
<scope>test</scope> <artifactId>graal-sdk</artifactId>
<version>2.44.0</version> <version>1.0.0-rc12</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.opera</groupId> <groupId>com.eclipsesource.j2v8</groupId>
<artifactId>operadriver</artifactId> <artifactId>j2v8_linux_x86_64</artifactId>
<scope>test</scope> <version>4.8.0</version>
<version>1.5</version> </dependency>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
......
...@@ -51,9 +51,9 @@ public class FallArmywormModel extends I18nImpl implements Model{ ...@@ -51,9 +51,9 @@ public class FallArmywormModel extends I18nImpl implements Model{
private Date plantingDate; private Date plantingDate;
private String intercroppingType; private String intercroppingType;
private final Map<String, Double> INTERCROPPING_RISK_REDUCTION; 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_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"); public final static ModelId MODEL_ID = new ModelId("FAWMODEL01");
private final ModelUtil modelUtil; private final ModelUtil modelUtil;
...@@ -154,7 +154,7 @@ public class FallArmywormModel extends I18nImpl implements Model{ ...@@ -154,7 +154,7 @@ public class FallArmywormModel extends I18nImpl implements Model{
@Override @Override
public String getLicense() { public String getLicense() {
return return
" Copyright (c) 2019 FAO <http://www.fao.org/>. \n" + " Copyright (c) 2019 NIBIO <http://www.nibio.no/>. \n" +
"\n" + "\n" +
" This program is free software: you can redistribute it and/or modify\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" + " it under the terms of the GNU Affero General Public License as\n" +
...@@ -174,7 +174,7 @@ public class FallArmywormModel extends I18nImpl implements Model{ ...@@ -174,7 +174,7 @@ public class FallArmywormModel extends I18nImpl implements Model{
@Override @Override
public String getCopyright() { public String getCopyright() {
return "(c) 2019 FAO (http://www.fao.org/)."; return "(c) 2019 NIBIO (http://www.nibio.no/).";
} }
......
...@@ -23,61 +23,63 @@ import com.fasterxml.jackson.core.JsonProcessingException; ...@@ -23,61 +23,63 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Date; import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; 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 javax.script.ScriptException;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import no.nibio.vips.entity.ModelConfiguration; import no.nibio.vips.entity.ModelConfiguration;
import no.nibio.vips.entity.Result; import no.nibio.vips.entity.Result;
import no.nibio.vips.entity.ResultImpl;
import no.nibio.vips.i18n.I18nImpl; import no.nibio.vips.i18n.I18nImpl;
import no.nibio.vips.model.ConfigValidationException; import no.nibio.vips.model.ConfigValidationException;
import no.nibio.vips.model.Model; import no.nibio.vips.model.Model;
import no.nibio.vips.model.ModelExcecutionException; import no.nibio.vips.model.ModelExcecutionException;
import no.nibio.vips.model.ModelId; import no.nibio.vips.model.ModelId;
import no.nibio.vips.util.ModelUtil; 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> * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no> * @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; private final ModelUtil modelUtil;
public FallArmywormModelJSAdapter() { public FallArmywormModelGraalVMAdapter() {
super("no.nibio.vips.model.fallarmywormmodel.texts"); super("no.nibio.vips.model.fallarmywormmodel.texts");
ScriptEngineManager sem = new ScriptEngineManager(); modelUtil = new ModelUtil();
engine = sem.getEngineByName("nashorn"); try {
modelUtil = new ModelUtil(); jsContext = Context.create("js");
try { jsBindings = jsContext.getBindings("js");
this.addJavaScript("/js/moment.min.js");
this.addJavaScript("/js/moment.min.js"); this.addJavaScript("/js/moment-timezone-with-data.min.js");
this.addJavaScript("/js/moment-timezone-with-data.min.js"); this.addJavaScript("/js/FallArmywormModelImpl.js");
this.addJavaScript("/js/FallArmywormModelImpl.js");
} catch (InstantiationException ex) {
Logger.getLogger(FallArmywormModelJSAdapter.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(FallArmywormModelGraalVMAdapter.class.getName()).log(Level.SEVERE, null, ex);
}
} }
public List<Result> getResult() throws ModelExcecutionException { public List<Result> getResult() throws ModelExcecutionException {
try //try
{ //{
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
ScriptObjectMirror jsObject = (ScriptObjectMirror) this.getRunnableJSWrapper().invokeFunction("getResult"); //ScriptObjectMirror jsObject = (ScriptObjectMirror) this.getRunnableJSWrapper().invokeFunction("getResult");
List<Result> retVal = (List<Result>) jsObject.keySet().stream().map(key->{ 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(); Result r = new ResultImpl();
ScriptObjectMirror resultObject = (ScriptObjectMirror) jsObject.get(key); ScriptObjectMirror resultObject = (ScriptObjectMirror) jsObject.get(key);
r.setWarningStatus((Integer)resultObject.get("warningStatus")); r.setWarningStatus((Integer)resultObject.get("warningStatus"));
...@@ -87,13 +89,14 @@ public class FallArmywormModelJSAdapter extends I18nImpl { ...@@ -87,13 +89,14 @@ public class FallArmywormModelJSAdapter extends I18nImpl {
ScriptObjectMirror allValues = (ScriptObjectMirror) resultObject.get("allValues"); ScriptObjectMirror allValues = (ScriptObjectMirror) resultObject.get("allValues");
r.setAllValues(allValues.to(Map.class)); r.setAllValues(allValues.to(Map.class));
return r; return r;
}).collect(Collectors.toList()); }).collect(Collectors.toList());*/
return retVal; //return retVal;
} return new ArrayList<>();
catch(ScriptException | NoSuchMethodException ex) //}
/*catch(ScriptException | NoSuchMethodException ex)
{ {
throw new ModelExcecutionException("ERROR: There was a problem running the JavaScript code. Message is: " + ex.getMessage()); 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 { ...@@ -114,7 +117,7 @@ public class FallArmywormModelJSAdapter extends I18nImpl {
public String getLicense() { public String getLicense() {
return return
" Copyright (c) 2019 FAO <http://www.fao.org/>. \n" + " Copyright (c) 2019 NIBIO <http://www.nibio.no/>. \n" +
"\n" + "\n" +
" This program is free software: you can redistribute it and/or modify\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" + " it under the terms of the GNU Affero General Public License as\n" +
...@@ -133,7 +136,7 @@ public class FallArmywormModelJSAdapter extends I18nImpl { ...@@ -133,7 +136,7 @@ public class FallArmywormModelJSAdapter extends I18nImpl {
public String getCopyright() { 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 { ...@@ -207,10 +210,11 @@ public class FallArmywormModelJSAdapter extends I18nImpl {
} }
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String jsonInput = mapper.writeValueAsString(config); String jsonInput = mapper.writeValueAsString(config);
jsBindings.getMember("setConfiguration").execute(jsonInput);
//System.out.println(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()); 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 { ...@@ -237,22 +241,11 @@ public class FallArmywormModelJSAdapter extends I18nImpl {
* @param scriptPath * @param scriptPath
* @throws ScriptException * @throws ScriptException
*/ */
protected final void addJavaScript(String scriptPath) throws InstantiationException protected final void addJavaScript(String scriptPath) throws IOException
{ {
try Reader stream = new InputStreamReader(this.getClass().getResourceAsStream(scriptPath));
{ Source source = Source.newBuilder("js", stream, "JavaScript").build();
InputStreamReader fr = new java.io.InputStreamReader(this.getClass().getResourceAsStream(scriptPath)); jsContext.eval(source);
engine.eval(fr);
}catch(ScriptException ex)
{
throw new InstantiationException(ex.getMessage());
}
} }
protected Invocable getRunnableJSWrapper()
{
return (Invocable) engine;
}
} }
/*
* 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);
}
}
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
/** /**
* The JavaScript implementation of the Fall Armyworm model * 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> * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/ */
...@@ -197,6 +197,8 @@ var setConfiguration = function (modelConfigurationUnknownType) ...@@ -197,6 +197,8 @@ var setConfiguration = function (modelConfigurationUnknownType)
TM = getDailyObservations(TMHourly, timeZone, 4); TM = getDailyObservations(TMHourly, timeZone, 4);
} }
*/ */
// TEST
}; };
/** /**
......
/*
* 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);
}
}
/*
* 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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment