c7ca8670 by Nathan Lighthart

Add reading of additional parameters

- Add dynamic handling of unknown parameters
1 parent 67377dbc
package ages;
import io.DataIO;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import lib.TypeFinder;
import oms3.annotations.Description;
import oms3.annotations.Execute;
import oms3.annotations.In;
......@@ -73,8 +77,11 @@ public class AgES {
private AgESParameters createParameters() {
AgESParameters.Builder builder = new AgESParameters.Builder();
Map<String, Object> parameterMap = generateParameterMap();
Map<String, Class<?>> typeMap = generateTypeMap();
// override output files as the full path is generated by OMS
AgESParameters parameters = builder.parameters(params)
AgESParameters parameters = builder.parameters(parameterMap, typeMap)
.outFile_hru(outFile_hru.toPath())
.outFile_hru_crop(outFile_hru_crop.toPath())
.outFile_hru_layer(outFile_hru_layer.toPath())
......@@ -93,4 +100,33 @@ public class AgES {
.build();
return parameters;
}
private Map<String, Object> generateParameterMap() {
Map<String, Object> parameterMap = new HashMap<>(params);
parameterMap.remove("params");
parameterMap.remove("fmt_date");
parameterMap.remove("fmt_double");
return parameterMap;
}
private Map<String, Class<?>> generateTypeMap() {
Map<String, Class<?>> typeMap = new HashMap<>();
for (String parameterName : params.keySet()) {
Map<String, String> parameterInfo = params.getInfo(parameterName);
if (parameterInfo == null) {
continue;
}
String typeName = parameterInfo.get(DataIO.KEY_TYPE);
Class<?> type = TypeFinder.findType(typeName);
if (type != null) {
typeMap.put(parameterName, type);
}
}
return typeMap;
}
}
......
......@@ -410,6 +410,8 @@ public class AgESModel {
private void initializeAgesContext() {
agesContext = new AlterableContext(new FieldContext(parameters, true));
parameters.addAdditionalParameters(agesContext);
double basinArea = 0.0;
for (HRU hru : hrus) {
basinArea += hru.area;
......
......@@ -4,6 +4,7 @@ package ages;
import annotations.Range;
import gov.usda.jcf.annotations.Description;
import gov.usda.jcf.annotations.Units;
import gov.usda.jcf.core.Context;
import gov.usda.jcf.util.conversion.TypeConverters;
import java.io.BufferedReader;
import java.io.IOException;
......@@ -12,7 +13,9 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
......@@ -654,6 +657,14 @@ public class AgESParameters {
@Description("parallel execution flag")
public final String flagParallel;
private Map<String, Parameter> additionalParameterMap;
public void addAdditionalParameters(Context context) {
for (Parameter p : additionalParameterMap.values()) {
p.addToContext(context);
}
}
private AgESParameters(Builder builder) {
// parameters
hruFilePath = builder.hruFilePath;
......@@ -879,6 +890,8 @@ public class AgESParameters {
flagSort = builder.flagSort;
flagSplit = builder.flagSplit;
flagParallel = builder.flagParallel;
additionalParameterMap = builder.additionalParameterMap;
}
public static class Builder {
......@@ -1107,6 +1120,8 @@ public class AgESParameters {
private boolean flagSplit;
private String flagParallel;
private Map<String, Parameter> additionalParameterMap = new HashMap<>();
public AgESParameters build() {
flagIrrigation = (irrigationFilePath != null && managementIrrigationFilePath != null);
return new AgESParameters(this);
......@@ -1146,6 +1161,29 @@ public class AgESParameters {
return this;
}
public Builder parameters(Map<String, Object> parameterMap, Map<String, Class<?>> typeMap) {
// handle field values
parameters(parameterMap);
// handle additional parameters
// find additional parameter names
Set<String> additionalParameters = new HashSet<>(parameterMap.keySet());
Field[] fields = Builder.class.getDeclaredFields();
for (Field field : fields) {
additionalParameters.remove(field.getName());
}
// Create Additional parameter objects
for (String parameterName : additionalParameters) {
Parameter p = new Parameter(parameterName,
parameterMap.get(parameterName),
typeMap.get(parameterName));
additionalParameterMap.put(parameterName, p);
}
return this;
}
private void setParameter(Field field, Object rawValue) throws IllegalArgumentException, IllegalAccessException {
Object value = TypeConverters.INSTANCE.convert(rawValue, field.getType());
field.setAccessible(true);
......@@ -2172,4 +2210,25 @@ public class AgESParameters {
return this;
}
}
private static class Parameter {
private String name;
private Object rawValue;
private Class<?> type;
public Parameter(String name, Object rawValue, Class<?> type) {
this.name = name;
this.rawValue = rawValue;
this.type = type;
}
public void addToContext(Context context) {
if (type == null) {
context.put(name, rawValue);
} else {
Object value = TypeConverters.INSTANCE.convert(rawValue, type);
context.put(name, value, type);
}
}
}
}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!