csip.OMSComponentMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of csip-core Show documentation
Show all versions of csip-core Show documentation
The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.
/*
* $Id: OMSComponentMapper.java 6ec9259c272f 2019-03-13 od $
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2019, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import csip.utils.JSONUtils;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import oms3.Access;
import oms3.ComponentAccess;
import oms3.annotations.Description;
import oms3.annotations.Execute;
import oms3.annotations.Finalize;
import oms3.annotations.Initialize;
import oms3.annotations.Range;
import oms3.annotations.Unit;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author od
*/
class OMSComponentMapper {
ComponentAccess ca;
OMSComponentMapper(Object comp) {
ca = new ComponentAccess(comp);
}
JSONObject getInputs() throws Exception {
Map r = new HashMap<>();
Collection a = ca.inputs();
for (Access field : a) {
Description d = field.getField().getAnnotation(Description.class);
Unit u = field.getField().getAnnotation(Unit.class);
Object v = field.getField().get(ca.getComponent());
r.put(field.getField().getName(), JSONUtils.data(field.getField().getName(),
v == null ? "" : v.toString(),
d == null ? null : d.value(),
u == null ? null : u.value()
));
}
return JSONUtils.newRequest(r);
}
void setInputs(ModelDataService s) throws Exception {
for (String pname : s.parameter().getNames()) {
Access a = ca.input(pname);
if (a != null) {
Class> c = a.getField().getType();
Object par = null;
if (c == int.class) {
par = s.parameter().getInt(pname);
} else if (c == double.class) {
par = s.parameter().getDouble(pname);
} else if (c == boolean.class) {
par = s.parameter().getBoolean(pname);
} else if (c == long.class) {
par = s.parameter().getLong(pname);
} else if (c == File.class) {
par = s.attachments().getFile(pname);
} else if (c == String.class) {
par = s.parameter().getString(pname);
} else {
continue;
}
if (par instanceof Number) {
validate(a, (Number) par);
}
a.setFieldValue(par);
}
}
}
void getOutputs(ModelDataService s) throws Exception {
for (Access out : ca.outputs()) {
if (out.getFieldValue() == null) {
continue;
}
String descr = null;
String unit = null;
Field f = out.getField();
Description d = f.getAnnotation(Description.class);
if (d != null) {
descr = d.value();
}
Unit u = f.getAnnotation(Unit.class);
if (u != null) {
unit = u.value();
}
//TODO add nested components here.
s.results().put(f.getName(), out.getFieldValue().toString(), descr, unit);
}
}
void process() {
ComponentAccess.callAnnotated(ca.getComponent(), Initialize.class, true);
ComponentAccess.callAnnotated(ca.getComponent(), Execute.class, false);
ComponentAccess.callAnnotated(ca.getComponent(), Finalize.class, true);
}
void validate(Access a, Number val) {
Field f = a.getField();
Range r = f.getAnnotation(Range.class);
if (r != null) {
double min = r.min();
if (r.excl_min()) {
if (!(min < val.doubleValue())) {
throw new IllegalArgumentException(f.getName() + ": Range invalid " + min + " < " + val);
}
} else {
if (!(min <= val.doubleValue())) {
throw new IllegalArgumentException(f.getName() + ": Range invalid " + min + " <= " + val);
}
}
double max = r.max();
if (r.excl_max()) {
if (!(max > val.doubleValue())) {
throw new IllegalArgumentException(f.getName() + ": Range invalid " + max + " > " + val);
}
} else {
if (!(max >= val.doubleValue())) {
throw new IllegalArgumentException(f.getName() + ": Range invalid " + max + " >= " + val);
}
}
}
}
// public static void main(String[] args) throws Exception {
// T t = new T();
// OMSComponentMapper m = new OMSComponentMapper(t);
//
// JSONArray a = new JSONArray();
// a.put(JSONUtils.data("msg", "input"));
// a.put(JSONUtils.data("temp", 20));
//
// m.setInputs(a);
// m.process();
//
// JSONArray o = m.getOutputs();
//
// System.out.println(o.toString(2));
// }
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy