use of water.exceptions.H2OIllegalArgumentException in project h2o-3 by h2oai.
the class Schema method parse.
// URL parameter parse
static <E> Object parse(String field_name, String s, Class fclz, boolean required, Class schemaClass) {
if (fclz.isPrimitive() || String.class.equals(fclz)) {
try {
return parsePrimitve(s, fclz);
} catch (NumberFormatException ne) {
String msg = "Illegal argument for field: " + field_name + " of schema: " + schemaClass.getSimpleName() + ": cannot convert \"" + s + "\" to type " + fclz.getSimpleName();
throw new H2OIllegalArgumentException(msg);
}
}
// An array?
if (fclz.isArray()) {
// Get component type
Class<E> afclz = (Class<E>) fclz.getComponentType();
// Result
E[] a = null;
// Handle simple case with null-array
if (s.equals("null") || s.length() == 0)
return null;
// Splitted values
// "".split(",") => {""} so handle the empty case explicitly
String[] splits;
if (s.startsWith("[") && s.endsWith("]")) {
// It looks like an array
read(s, 0, '[', fclz);
read(s, s.length() - 1, ']', fclz);
String inside = s.substring(1, s.length() - 1).trim();
if (inside.length() == 0)
splits = new String[] {};
else
splits = splitArgs(inside);
} else {
// Lets try to parse single value as an array!
// See PUBDEV-1955
splits = new String[] { s.trim() };
}
// Can't cast an int[] to an Object[]. Sigh.
if (afclz == int.class) {
// TODO: other primitive types. . .
a = (E[]) Array.newInstance(Integer.class, splits.length);
} else if (afclz == double.class) {
a = (E[]) Array.newInstance(Double.class, splits.length);
} else if (afclz == float.class) {
a = (E[]) Array.newInstance(Float.class, splits.length);
} else {
// Fails with primitive classes; need the wrapper class. Thanks, Java.
a = (E[]) Array.newInstance(afclz, splits.length);
}
for (int i = 0; i < splits.length; i++) {
if (String.class == afclz || KeyV3.class.isAssignableFrom(afclz)) {
// strip quotes off string values inside array
String stripped = splits[i].trim();
if ("null".equals(stripped.toLowerCase()) || "na".equals(stripped.toLowerCase())) {
a[i] = null;
continue;
}
// Quotes are now optional because standard clients will send arrays of length one as just strings.
if (stripped.startsWith("\"") && stripped.endsWith("\"")) {
stripped = stripped.substring(1, stripped.length() - 1);
}
a[i] = (E) parse(field_name, stripped, afclz, required, schemaClass);
} else {
a[i] = (E) parse(field_name, splits[i].trim(), afclz, required, schemaClass);
}
}
return a;
}
// Are we parsing an object from a string? NOTE: we might want to make this check more restrictive.
if (!fclz.isAssignableFrom(Schema.class) && s != null && s.startsWith("{") && s.endsWith("}")) {
return gson.fromJson(s, fclz);
}
if (fclz.equals(Key.class))
if ((s == null || s.length() == 0) && required)
throw new H2OKeyNotFoundArgumentException(field_name, s);
else if (!required && (s == null || s.length() == 0))
return null;
else
// If the key name is in an array we need to trim surrounding quotes.
return Key.make(s.startsWith("\"") ? s.substring(1, s.length() - 1) : s);
if (KeyV3.class.isAssignableFrom(fclz)) {
if ((s == null || s.length() == 0) && required)
throw new H2OKeyNotFoundArgumentException(field_name, s);
if (!required && (s == null || s.length() == 0))
return null;
// If the key name is in an array we need to trim surrounding quotes.
return KeyV3.make(fclz, Key.make(s.startsWith("\"") ? s.substring(1, s.length() - 1) : s));
}
if (Enum.class.isAssignableFrom(fclz)) {
return EnumUtils.valueOf(fclz, s);
}
// TODO: these can be refactored into a single case using the facilities in Schema:
if (FrameV3.class.isAssignableFrom(fclz)) {
if ((s == null || s.length() == 0) && required)
throw new H2OKeyNotFoundArgumentException(field_name, s);
else if (!required && (s == null || s.length() == 0))
return null;
else {
Value v = DKV.get(s);
// not required
if (null == v)
return null;
if (!v.isFrame())
throw H2OIllegalArgumentException.wrongKeyType(field_name, s, "Frame", v.get().getClass());
// TODO: version!
return new FrameV3((Frame) v.get());
}
}
if (JobV3.class.isAssignableFrom(fclz)) {
if ((s == null || s.length() == 0) && required)
throw new H2OKeyNotFoundArgumentException(s);
else if (!required && (s == null || s.length() == 0))
return null;
else {
Value v = DKV.get(s);
// not required
if (null == v)
return null;
if (!v.isJob())
throw H2OIllegalArgumentException.wrongKeyType(field_name, s, "Job", v.get().getClass());
// TODO: version!
return new JobV3().fillFromImpl((Job) v.get());
}
}
// where the frame name is also specified.
if (FrameV3.ColSpecifierV3.class.isAssignableFrom(fclz)) {
return new FrameV3.ColSpecifierV3(s);
}
if (ModelSchemaV3.class.isAssignableFrom(fclz))
throw H2O.fail("Can't yet take ModelSchemaV3 as input.");
/*
if( (s==null || s.length()==0) && required ) throw new IllegalArgumentException("Missing key");
else if (!required && (s == null || s.length() == 0)) return null;
else {
Value v = DKV.get(s);
if (null == v) return null; // not required
if (! v.isModel()) throw new IllegalArgumentException("Model argument points to a non-model object.");
return v.get();
}
*/
throw H2O.fail("Unimplemented schema fill from " + fclz.getSimpleName());
}
use of water.exceptions.H2OIllegalArgumentException in project h2o-3 by h2oai.
the class Frame method export.
public static Job export(Frame fr, String path, String frameName, boolean overwrite, int nParts) {
boolean forceSingle = nParts == 1;
// Validate input
if (forceSingle) {
boolean fileExists = H2O.getPM().exists(path);
if (overwrite && fileExists) {
Log.warn("File " + path + " exists, but will be overwritten!");
} else if (!overwrite && fileExists) {
throw new H2OIllegalArgumentException(path, "exportFrame", "File " + path + " already exists!");
}
} else {
if (!H2O.getPM().isEmptyDirectoryAllNodes(path)) {
throw new H2OIllegalArgumentException(path, "exportFrame", "Cannot use path " + path + " to store part files! The target needs to be either an existing empty directory or not exist yet.");
}
}
Job job = new Job<>(fr._key, "water.fvec.Frame", "Export dataset");
FrameUtils.ExportTaskDriver t = new FrameUtils.ExportTaskDriver(fr, path, frameName, overwrite, job, nParts);
return job.start(t, fr.anyVec().nChunks());
}
use of water.exceptions.H2OIllegalArgumentException in project h2o-3 by h2oai.
the class KeyV3 method fillFromImpl.
@Override
public S fillFromImpl(Iced i) {
if (!(i instanceof Key))
throw new H2OIllegalArgumentException("fillFromImpl", "key", i);
Key key = (Key) i;
if (null == key)
return (S) this;
this.name = key.toString();
// Our type is generally determined by our type parameter, but some APIs use raw untyped KeyV1s to return multiple types.
this.type = "Key<" + this.getKeyedClassType() + ">";
if ("Keyed".equals(this.type)) {
// get the actual type, if the key points to a value in the DKV
String vc = key.valueClassSimple();
if (null != vc) {
this.type = "Key<" + vc + ">";
}
}
Class<? extends Keyed> keyed_class = this.getKeyedClass();
if (Job.class.isAssignableFrom(keyed_class))
this.URL = "/3/Jobs/" + key.toString();
else if (Frame.class.isAssignableFrom(keyed_class))
this.URL = "/3/Frames/" + key.toString();
else if (Model.class.isAssignableFrom(keyed_class))
this.URL = "/3/Models/" + key.toString();
else if (PartialDependence.class.isAssignableFrom(keyed_class))
this.URL = "/3/PartialDependence/" + key.toString();
else if (Vec.class.isAssignableFrom(keyed_class))
this.URL = null;
else
this.URL = null;
return (S) this;
}
use of water.exceptions.H2OIllegalArgumentException in project h2o-3 by h2oai.
the class Schema method fillFromParms.
/**
* Fill this Schema from a set of (generally HTTP) parameters.
* <p>
* Using reflection this process determines the type of the target field and
* conforms the types if possible. For example, if the field is a Keyed type
* the name (ID) will be looked up in the DKV and mapped appropriately.
* <p>
* The process ignores parameters which are not fields in the schema, and it
* verifies that all fields marked as required are present in the parameters
* list.
* <p>
* It also does various sanity checks for broken Schemas, for example fields must
* not be private, and since input fields get filled here they must not be final.
* @param parms Properties map of parameter values
* @param checkRequiredFields perform check for missing required fields
* @return this schema
* @throws H2OIllegalArgumentException for bad/missing parameters
*/
public S fillFromParms(Properties parms, boolean checkRequiredFields) {
// Get passed-in fields, assign into Schema
Class thisSchemaClass = this.getClass();
Map<String, Field> fields = new HashMap<>();
// declare here so we can print in catch{}
Field current = null;
try {
Class clz = thisSchemaClass;
do {
Field[] some_fields = clz.getDeclaredFields();
for (Field f : some_fields) {
current = f;
if (null == fields.get(f.getName()))
fields.put(f.getName(), f);
}
clz = clz.getSuperclass();
} while (Iced.class.isAssignableFrom(clz.getSuperclass()));
} catch (SecurityException e) {
throw H2O.fail("Exception accessing field: " + current + " in class: " + this.getClass() + ": " + e);
}
for (String key : parms.stringPropertyNames()) {
try {
// No such field error, if parm is junk
Field f = fields.get(key);
if (null == f) {
throw new H2OIllegalArgumentException("Unknown parameter: " + key, "Unknown parameter in fillFromParms: " + key + " for class: " + this.getClass().toString());
}
int mods = f.getModifiers();
if (Modifier.isTransient(mods) || Modifier.isStatic(mods)) {
// Attempting to set a transient or static; treat same as junk fieldname
throw new H2OIllegalArgumentException("Bad parameter for field: " + key + " for class: " + this.getClass().toString(), "Bad parameter definition for field: " + key + " in fillFromParms for class: " + this.getClass().toString() + " (field was declared static or transient)");
}
// Only support a single annotation which is an API, and is required
Annotation[] apis = f.getAnnotations();
if (apis.length == 0)
throw H2O.fail("Broken internal schema; missing API annotation for field: " + key);
API api = (API) apis[0];
// Must have one of these set to be an input field
if (api.direction() == API.Direction.OUTPUT) {
throw new H2OIllegalArgumentException("Attempting to set output field: " + key + " for class: " + this.getClass().toString(), "Attempting to set output field: " + key + " in fillFromParms for class: " + this.getClass().toString() + " (field was annotated as API.Direction.OUTPUT)");
}
// Parse value and set the field
setField(this, f, key, parms.getProperty(key), api.required(), thisSchemaClass);
} catch (IllegalAccessException iae) {
// Come here if field is final or private
throw H2O.fail("Broken internal schema; field cannot be private nor final: " + key);
}
}
// Confirm required fields are set
if (checkRequiredFields) {
for (Field f : fields.values()) {
int mods = f.getModifiers();
if (Modifier.isTransient(mods) || Modifier.isStatic(mods))
// Ignore transient & static
continue;
try {
// TODO: is there a more specific way we can do this?
API api = (API) f.getAnnotations()[0];
if (api.required()) {
if (parms.getProperty(f.getName()) == null) {
IcedHashMapGeneric.IcedHashMapStringObject values = new IcedHashMapGeneric.IcedHashMapStringObject();
values.put("schema", this.getClass().getSimpleName());
values.put("argument", f.getName());
throw new H2OIllegalArgumentException("Required field " + f.getName() + " not specified", "Required field " + f.getName() + " not specified for schema class: " + this.getClass(), values);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
throw H2O.fail("Missing annotation for API field: " + f.getName());
}
}
}
//noinspection unchecked (parameter <S> should be the derived class itself)
return (S) this;
}
use of water.exceptions.H2OIllegalArgumentException in project h2o-3 by h2oai.
the class AvroParserProvider method createParserSetup.
@Override
public ParseSetup createParserSetup(Key[] inputs, ParseSetup requiredSetup) {
// Also expect that files are not compressed
assert inputs != null && inputs.length > 0 : "Inputs cannot be empty!";
Key firstInput = inputs[0];
Iced ice = DKV.getGet(firstInput);
if (ice == null)
throw new H2OIllegalArgumentException("Missing data", "Did not find any data under key " + firstInput);
ByteVec bv = (ByteVec) (ice instanceof ByteVec ? ice : ((Frame) ice).vecs()[0]);
byte[] bits = bv.getFirstBytes();
try {
AvroParser.AvroInfo avroInfo = AvroParser.extractAvroInfo(bits, requiredSetup);
return new AvroParser.AvroParseSetup(requiredSetup, avroInfo.header, avroInfo.firstBlockSize, avroInfo.domains);
} catch (Throwable e) {
throw new H2OIllegalArgumentException("Wrong data", "Cannot find Avro header in input file: " + firstInput, e);
}
}
Aggregations