use of oms3.annotations.Bound in project hortonmachine by TheHortonMachine.
the class Components method figureOutParamDeclarations.
public static void figureOutParamDeclarations(PrintStream w, Object... comps) {
Map<String, Access> m = new HashMap<String, Access>();
for (Object c : comps) {
ComponentAccess cp = new ComponentAccess(c);
// over all input slots.
for (Access fin : cp.inputs()) {
Role role = fin.getField().getAnnotation(Role.class);
if (role != null && Annotations.plays(role, Role.PARAMETER)) {
// make sure parameter is only there once.
m.put(fin.getField().getName(), fin);
}
}
}
List<String> sl = new ArrayList<String>(m.keySet());
Collections.sort(sl, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (String key : sl) {
Access fin = m.get(key);
Description d = fin.getField().getAnnotation(Description.class);
if (d != null) {
w.println(" @Description(\"" + d.value() + "\")");
}
Bound b = fin.getField().getAnnotation(Bound.class);
if (b != null) {
w.println(" @Bound(\"" + b.value() + "\")");
}
w.println(" @Role(\"" + fin.getField().getAnnotation(Role.class).value() + "\")");
w.println(" @In public " + fin.getField().getType().getSimpleName() + " " + fin.getField().getName() + ";");
w.println();
}
}
use of oms3.annotations.Bound in project hortonmachine by TheHortonMachine.
the class ComponentAccess method setInputData.
/**
* Set the input data as map.
* @param inp
* @param comp
* @param log
*/
@SuppressWarnings("unchecked")
public static boolean setInputData(Map<String, Object> inp, Object comp, Logger log) {
PrintWriter w = null;
File file = null;
boolean success = true;
ComponentAccess cp = new ComponentAccess(comp);
inp = convert(inp);
for (Access in : cp.inputs()) {
String fieldName = in.getField().getName();
Class fieldType = in.getField().getType();
Object inpValue = inp.get(fieldName);
if (inpValue != null) {
// convert them
try {
inpValue = conv(inpValue, fieldType);
// check the range if possible.
if (Number.class.isAssignableFrom(fieldType) || fieldType == double.class || fieldType == float.class || fieldType == int.class) {
Range range = in.getField().getAnnotation(Range.class);
if (range != null) {
double v = ((Number) inpValue).doubleValue();
if (!Annotations.inRange(range, v)) {
if (log.isLoggable(Level.WARNING)) {
log.warning("Value '" + v + "' not in Range: " + range);
}
}
}
}
in.setFieldValue(inpValue);
if (log.isLoggable(Level.CONFIG)) {
log.config("@In " + comp.getClass().getName() + "@" + fieldName + " <- '" + inpValue + "'");
}
} catch (Exception ex) {
throw new ComponentException("Failed setting '" + fieldName + "' type " + in.getField().getType().getCanonicalName() + " <- " + ex.getMessage());
}
continue;
} else {
if (System.getProperty("oms.check_params") != null) {
try {
if (w == null) {
file = new File(System.getProperty("oms3.work", System.getProperty("user.dir")), "missing_params.csv");
w = new PrintWriter(new FileWriter(file));
w.println("# Missing parameter, copy those entries into one of your parameter files.");
}
String val = null;
Bound b = null;
Object o = in.getFieldValue();
if (o != null) {
val = o.toString();
} else {
b = in.getField().getAnnotation(Bound.class);
if (b != null) {
try {
Object v = inp.get(b.value());
if (v == null) {
v = new Integer(0);
}
int dim = Integer.parseInt(v.toString());
int[] d = new int[dim];
val = Conversions.convert(d, String.class);
} catch (NumberFormatException E) {
val = "?";
}
} else {
val = "?";
}
}
w.println("@P, " + fieldName + ", \"" + val + "\"");
if (b != null) {
w.println(" bound, " + b.value());
}
} catch (Exception E) {
throw new RuntimeException(E);
}
}
if (log.isLoggable(Level.WARNING)) {
log.warning("No Input for '" + fieldName + "'");
}
}
}
if (w != null) {
w.close();
System.out.println("Missing parameter [" + file + "]");
success = false;
}
return success;
}
Aggregations