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

Added exception handling

parent 5771268c
No related branches found
No related tags found
No related merge requests found
......@@ -7,12 +7,16 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import no.bioforsk.vips.i18n.I18nImpl;
import no.bioforsk.vips.entity.ModelConfiguration;
import no.bioforsk.vips.entity.Observation;
import no.bioforsk.vips.entity.Result;
import no.bioforsk.vips.entity.ResultImpl;
import no.bioforsk.vips.model.ConfigValidationException;
import no.bioforsk.vips.model.Model;
import no.bioforsk.vips.model.ModelExcecutionException;
import no.bioforsk.vips.model.ModelId;
import no.bioforsk.vips.util.WeatherElements;
import no.bioforsk.vips.util.WeatherUtil;
......@@ -50,46 +54,59 @@ public class NaerstadModel extends I18nImpl implements Model{
}
@Override
public List<Result> getResult() {
this.calculateNaerstadModellBakgroundData();
this.backgroundData.toString();
List<Result> results = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTime(this.calculationStart);
/*
* Results can only be inferred from 1 day after calculation start.
* Therefore we add one day before we begin iteration
*/
cal.add(Calendar.DATE, 1);
DecimalFormat dFormat = new DecimalFormat("###.##");
for(int j=0;j<4;j++)
public List<Result> getResult() throws ModelExcecutionException
{
try
{
Date dateStart = cal.getTime();
this.calculateNaerstadModellBakgroundData();
this.backgroundData.toString();
List<Result> results = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTime(this.calculationStart);
/*
* Results can only be inferred from 1 day after calculation start.
* Therefore we add one day before we begin iteration
*/
cal.add(Calendar.DATE, 1);
Date dateEnd = cal.getTime();
Date thePresent = dateStart;
while(thePresent.before(dateEnd))
DecimalFormat dFormat = new DecimalFormat("###.##");
for(int j=0;j<4;j++)
{
Result result = new ResultImpl();
result.setResultValidTime(thePresent);
Double RISK = this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.RISK);
// TODO: Find better way of terminating?
if(RISK == null)
Date dateStart = cal.getTime();
cal.add(Calendar.DATE, 1);
Date dateEnd = cal.getTime();
Date thePresent = dateStart;
while(thePresent.before(dateEnd))
{
break;
Result result = new ResultImpl();
result.setResultValidTime(thePresent);
Double RISK = this.backgroundData.getParamDoubleValueForDate(thePresent, NaerstadModelBackgroundDataMatrix.RISK);
// TODO: Find better way of terminating?
if(RISK == null)
{
break;
}
if(this.DEBUG)
System.out.println("RISK(" + thePresent + ")=" + RISK);
result.setValue("RISK", dFormat.format(RISK));
results.add(result);
// Moving on
cal.setTime(thePresent);
cal.add(Calendar.HOUR_OF_DAY, 1);
thePresent = cal.getTime();
}
if(this.DEBUG)
System.out.println("RISK(" + thePresent + ")=" + RISK);
result.setValue("RISK", dFormat.format(RISK));
results.add(result);
// Moving on
cal.setTime(thePresent);
cal.add(Calendar.HOUR_OF_DAY, 1);
thePresent = cal.getTime();
}
return results;
} catch(Exception e) {
if(e instanceof ModelExcecutionException)
throw e;
// Log the error
Logger.getLogger(NaerstadModel.class.getName()).log(Level.SEVERE, null, e);
throw new ModelExcecutionException("An exception occurred. Message is " + e.getMessage() + " See logs for details");
}
return results;
}
@Override
......@@ -176,7 +193,8 @@ public class NaerstadModel extends I18nImpl implements Model{
* @param config
*/
@Override
public void setConfiguration(ModelConfiguration config) {
public void setConfiguration(ModelConfiguration config) throws ConfigValidationException
{
initCollections();
ObjectMapper mapper = new ObjectMapper();
......@@ -204,16 +222,20 @@ public class NaerstadModel extends I18nImpl implements Model{
}
}
// TODO: Validate all input!!!!
/*for(Observation o:observations)
if( this.RR.size() != this.TM.size()
|| this.Q0.size() != this.TM.size()
|| this.UM.size() != this.TM.size()
|| this.BT.size() != this.BT.size())
{
System.out.println("o time Measured = " + o.getTimeMeasured());
throw new ConfigValidationException("Incorrect number of weather data. TM.size() = " + this.TM.size() + ", BT.size()=" + this.BT.size() + ", UM.size()=" + this.UM.size() + ", RR.size()=" + this.RR.size() + ", Q0.size=" + this.Q0.size());
}
catch(ArrayIndexOutOfBoundsException aioue)
{
throw new Exception("Feil med antall måledata. TMliste.length = " + TMliste.length + ", BTliste.length=" + BTliste.length + ", UMListe.length=" + UMliste.length + ", RRliste.length=" + RRliste.length + ", Q0liste.length=" + Q0liste.length);
}*/
int minimumNumberOfWeatherData = 72;
if((this.TM.size() + this.RR.size() + this.Q0.size() + this.UM.size() + this.BT.size()) / 5 < minimumNumberOfWeatherData)
{
throw new ConfigValidationException("Minimum number of weather data = " + minimumNumberOfWeatherData);
}
}
......
......@@ -5,24 +5,22 @@
package no.bioforsk.vips.model.naerstadmodel;
import java.io.BufferedInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static junit.framework.Assert.fail;
import junit.framework.TestCase;
import no.bioforsk.vips.entity.ModelConfiguration;
import no.bioforsk.vips.entity.Observation;
import no.bioforsk.vips.model.ModelId;
import no.bioforsk.vips.model.ConfigValidationException;
import no.bioforsk.vips.model.ModelExcecutionException;
import no.bioforsk.vips.util.JSONUtil;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
......@@ -51,12 +49,18 @@ public class NaerstadModelTest extends TestCase {
* Test of getResult method, of class NaerstadModel.
*/
public void testGetResult() {
System.out.println("getResult");
NaerstadModel instance = new NaerstadModel();
instance.setConfiguration(this.getConfiguration());
List result = instance.getResult();
//System.out.println("Result size=" + result.size());
assertTrue(result.size() == 96);
try {
System.out.println("getResult");
NaerstadModel instance = new NaerstadModel();
instance.setConfiguration(this.getConfiguration());
List result;
result = instance.getResult();
//System.out.println("Result size=" + result.size());
assertTrue(result.size() == 96);
} catch (ConfigValidationException | ModelExcecutionException ex) {
fail("Exception: " + ex.getMessage());
}
}
......@@ -134,11 +138,14 @@ public class NaerstadModelTest extends TestCase {
* Test of setConfiguration method, of class NaerstadModel.
*/
public void testSetConfiguration() {
try {
System.out.println("setConfiguration");
NaerstadModel instance = new NaerstadModel();
instance.setConfiguration(this.getConfiguration());
} catch (ConfigValidationException ex) {
fail("Error in configuration:" + ex.getMessage());
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment