This is an automated email from the ASF dual-hosted git repository. jqin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/flink-ml.git
commit 1f0fe565f8f79ac084d7763f5f0da3fdb36bfa00 Author: Dong Lin <[email protected]> AuthorDate: Sun Sep 26 21:37:32 2021 +0800 [FLINK-24354][FLIP-174] Remove old param-related classes --- .../apache/flink/ml/api/misc/param/ParamInfo.java | 151 ----------- .../flink/ml/api/misc/param/ParamInfoFactory.java | 134 ---------- .../flink/ml/api/misc/param/ParamValidator.java | 39 --- .../org/apache/flink/ml/api/misc/param/Params.java | 277 --------------------- .../apache/flink/ml/api/misc/param/WithParams.java | 60 ----- .../flink/ml/util/param/ExtractParamInfosUtil.java | 71 ------ .../org/apache/flink/ml/api/misc/ParamsTest.java | 179 ------------- .../ml/util/param/ExtractParamInfosUtilTest.java | 109 -------- 8 files changed, 1020 deletions(-) diff --git a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamInfo.java b/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamInfo.java deleted file mode 100644 index b0f7ce9..0000000 --- a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamInfo.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.api.misc.param; - -import org.apache.flink.annotation.PublicEvolving; -import org.apache.flink.util.Preconditions; - -/** - * Definition of a parameter, including name, type, default value, validator and so on. - * - * <p>A parameter can either be optional or non-optional. - * - * <ul> - * <li>A non-optional parameter should not have a default value. Instead, its value must be - * provided by the users. - * <li>An optional parameter may or may not have a default value. - * </ul> - * - * <p>Please see {@link Params#get(ParamInfo)} and {@link Params#contains(ParamInfo)} for more - * details about the behavior. - * - * <p>A parameter may have aliases in addition to the parameter name for convenience and - * compatibility purposes. One should not set values for both parameter name and an alias. One and - * only one value should be set either under the parameter name or one of the alias. - * - * @param <V> the type of the param value - */ -@PublicEvolving -public class ParamInfo<V> { - private final String name; - private final String[] alias; - private final String description; - private final boolean isOptional; - private final boolean hasDefaultValue; - private final V defaultValue; - private final ParamValidator<V> validator; - private final Class<V> valueClass; - - ParamInfo( - String name, - String[] alias, - String description, - boolean isOptional, - boolean hasDefaultValue, - V defaultValue, - ParamValidator<V> validator, - Class<V> valueClass) { - this.name = name; - this.alias = alias; - this.description = description; - this.isOptional = isOptional; - this.hasDefaultValue = hasDefaultValue; - this.defaultValue = defaultValue; - this.validator = validator; - this.valueClass = valueClass; - } - - /** - * Returns the name of the parameter. The name must be unique in the stage the ParamInfo belongs - * to. - * - * @return the name of the parameter - */ - public String getName() { - return name; - } - - /** - * Returns the aliases of the parameter. The alias will be an empty string array by default. - * - * @return the aliases of the parameter - */ - public String[] getAlias() { - Preconditions.checkNotNull(alias); - return alias; - } - - /** - * Returns the description of the parameter. - * - * @return the description of the parameter - */ - public String getDescription() { - return description; - } - - /** - * Returns whether the parameter is optional. - * - * @return {@code true} if the param is optional, {@code false} otherwise - */ - public boolean isOptional() { - return isOptional; - } - - /** - * Returns whether the parameter has a default value. Since {@code null} may also be a valid - * default value of a parameter, the return of getDefaultValue may be {@code null} even when - * this method returns true. - * - * @return {@code true} if the param is has a default value(even if it's a {@code null}), {@code - * false} otherwise - */ - public boolean hasDefaultValue() { - return hasDefaultValue; - } - - /** - * Returns the default value of the parameter. The default value should be defined whenever - * possible. The default value can be a {@code null} even if hasDefaultValue returns true. - * - * @return the default value of the param, {@code null} if not defined - */ - public V getDefaultValue() { - return defaultValue; - } - - /** - * Returns the validator to validate the value of the parameter. - * - * @return the validator to validate the value of the parameter. - */ - public ParamValidator<V> getValidator() { - return validator; - } - - /** - * Returns the class of the param value. It's usually needed in serialization. - * - * @return the class of the param value - */ - public Class<V> getValueClass() { - return valueClass; - } -} diff --git a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamInfoFactory.java b/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamInfoFactory.java deleted file mode 100644 index 5199a5d..0000000 --- a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamInfoFactory.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.api.misc.param; - -/** Factory to create ParamInfo, all ParamInfos should be created via this class. */ -public class ParamInfoFactory { - /** - * Returns a ParamInfoBuilder to configure and build a new ParamInfo. - * - * @param name name of the new ParamInfo - * @param valueClass value class of the new ParamInfo - * @param <V> value type of the new ParamInfo - * @return a ParamInfoBuilder - */ - public static <V> ParamInfoBuilder<V> createParamInfo(String name, Class<V> valueClass) { - return new ParamInfoBuilder<>(name, valueClass); - } - - /** - * Builder to build a new ParamInfo. Builder is created by ParamInfoFactory with name and - * valueClass set. - * - * @param <V> value type of the new ParamInfo - */ - public static class ParamInfoBuilder<V> { - private String name; - private String[] alias = new String[0]; - private String description; - private boolean isOptional = true; - private boolean hasDefaultValue = false; - private V defaultValue; - private ParamValidator<V> validator; - private Class<V> valueClass; - - ParamInfoBuilder(String name, Class<V> valueClass) { - this.name = name; - this.valueClass = valueClass; - } - - /** - * Sets the aliases of the parameter. - * - * @return the builder itself - */ - public ParamInfoBuilder<V> setAlias(String[] alias) { - this.alias = alias; - return this; - } - - /** - * Sets the description of the parameter. - * - * @return the builder itself - */ - public ParamInfoBuilder<V> setDescription(String description) { - this.description = description; - return this; - } - - /** - * Sets the flag indicating the parameter is optional. The parameter is optional by default. - * - * @return the builder itself - */ - public ParamInfoBuilder<V> setOptional() { - this.isOptional = true; - return this; - } - - /** - * Sets the flag indicating the parameter is required. - * - * @return the builder itself - */ - public ParamInfoBuilder<V> setRequired() { - this.isOptional = false; - return this; - } - - /** - * Sets the flag indicating the parameter has default value, and sets the default value. - * - * @return the builder itself - */ - public ParamInfoBuilder<V> setHasDefaultValue(V defaultValue) { - this.hasDefaultValue = true; - this.defaultValue = defaultValue; - return this; - } - - /** - * Sets the validator to validate the parameter value set by users. - * - * @return the builder itself - */ - public ParamInfoBuilder<V> setValidator(ParamValidator<V> validator) { - this.validator = validator; - return this; - } - - /** - * Builds the defined ParamInfo and returns it. The ParamInfo will be immutable. - * - * @return the defined ParamInfo - */ - public ParamInfo<V> build() { - return new ParamInfo<>( - name, - alias, - description, - isOptional, - hasDefaultValue, - defaultValue, - validator, - valueClass); - } - } -} diff --git a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamValidator.java b/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamValidator.java deleted file mode 100644 index d41c709..0000000 --- a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/ParamValidator.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.api.misc.param; - -import org.apache.flink.annotation.PublicEvolving; - -import java.io.Serializable; - -/** - * An interface used by {@link ParamInfo} to do validation when a parameter value is set. - * - * @param <V> the type of the value to validate - */ -@PublicEvolving -public interface ParamValidator<V> extends Serializable { - /** - * Validates a parameter value. - * - * @param value value to validate - * @return {@code true} if the value is valid, {@code false} otherwise - */ - boolean validate(V value); -} diff --git a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/Params.java b/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/Params.java deleted file mode 100644 index e173a66..0000000 --- a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/Params.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.api.misc.param; - -import org.apache.flink.annotation.PublicEvolving; - -import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException; -import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * The map-like container class for parameter. This class is provided to unify the interaction with - * parameters. - */ -@PublicEvolving -public class Params implements Serializable, Cloneable { - private static final long serialVersionUID = 1L; - - /** - * A mapping from param name to its value. - * - * <p>The value is stored in map using json format. - */ - private final Map<String, String> params; - - private transient ObjectMapper mapper; - - public Params() { - this.params = new HashMap<>(); - } - - /** - * Return the number of params. - * - * @return Return the number of params. - */ - public int size() { - return params.size(); - } - - /** Removes all of the params. The params will be empty after this call returns. */ - public void clear() { - params.clear(); - } - - /** - * Returns <tt>true</tt> if this params contains no mappings. - * - * @return <tt>true</tt> if this map contains no mappings - */ - public boolean isEmpty() { - return params.isEmpty(); - } - - /** - * Returns the value of the specific parameter, or default value defined in the {@code info} if - * this Params doesn't have a value set for the parameter. An exception will be thrown in the - * following cases because no value could be found for the specified parameter. - * - * <ul> - * <li>Non-optional parameter: no value is defined in this params for a non-optional - * parameter. - * <li>Optional parameter: no value is defined in this params and no default value is defined. - * </ul> - * - * @param info the info of the specific parameter, usually with default value - * @param <V> the type of the specific parameter - * @return the value of the specific parameter, or default value defined in the {@code info} if - * this Params doesn't contain the parameter - * @throws IllegalArgumentException if no value can be found for specified parameter - */ - public <V> V get(ParamInfo<V> info) { - String value = null; - String usedParamName = null; - for (String nameOrAlias : getParamNameAndAlias(info)) { - if (params.containsKey(nameOrAlias)) { - if (usedParamName != null) { - throw new IllegalArgumentException( - String.format( - "Duplicate parameters of %s and %s", - usedParamName, nameOrAlias)); - } - usedParamName = nameOrAlias; - value = params.get(nameOrAlias); - } - } - - if (usedParamName != null) { - // The param value was set by the user. - return valueFromJson(value, info.getValueClass()); - } else { - // The param value was not set by the user. - if (!info.isOptional()) { - throw new IllegalArgumentException( - "Missing non-optional parameter " + info.getName()); - } else if (!info.hasDefaultValue()) { - throw new IllegalArgumentException( - "Cannot find default value for optional parameter " + info.getName()); - } - return info.getDefaultValue(); - } - } - - /** - * Set the value of the specific parameter. - * - * @param info the info of the specific parameter to set. - * @param value the value to be set to the specific parameter. - * @param <V> the type of the specific parameter. - * @return the previous value of the specific parameter, or null if this Params didn't contain - * the parameter before - * @throws RuntimeException if the {@code info} has a validator and the {@code value} is - * evaluated as illegal by the validator - */ - public <V> Params set(ParamInfo<V> info, V value) { - if (info.getValidator() != null && !info.getValidator().validate(value)) { - throw new RuntimeException( - "Setting " + info.getName() + " as a invalid value:" + value); - } - params.put(info.getName(), valueToJson(value)); - return this; - } - - /** - * Removes the specific parameter from this Params. - * - * @param info the info of the specific parameter to remove - * @param <V> the type of the specific parameter - */ - public <V> void remove(ParamInfo<V> info) { - params.remove(info.getName()); - for (String a : info.getAlias()) { - params.remove(a); - } - } - - /** - * Check whether this params has a value set for the given {@code info}. - * - * @return <tt>true</tt> if this params has a value set for the specified {@code info}, false - * otherwise. - */ - public <V> boolean contains(ParamInfo<V> info) { - return params.containsKey(info.getName()) - || Arrays.stream(info.getAlias()).anyMatch(params::containsKey); - } - - /** - * Returns a json containing all parameters in this Params. The json should be human-readable if - * possible. - * - * @return a json containing all parameters in this Params - */ - public String toJson() { - assertMapperInited(); - try { - return mapper.writeValueAsString(params); - } catch (JsonProcessingException e) { - throw new RuntimeException("Failed to serialize params to json", e); - } - } - - /** - * Restores the parameters from the given json. The parameters should be exactly the same with - * the one who was serialized to the input json after the restoration. - * - * @param json the json String to restore from - */ - @SuppressWarnings("unchecked") - public void loadJson(String json) { - assertMapperInited(); - Map<String, String> params; - try { - params = mapper.readValue(json, Map.class); - } catch (IOException e) { - throw new RuntimeException("Failed to deserialize json:" + json, e); - } - this.params.putAll(params); - } - - /** - * Factory method for constructing params. - * - * @param json the json string to load - * @return the {@code Params} loaded from the json string. - */ - public static Params fromJson(String json) { - Params params = new Params(); - params.loadJson(json); - return params; - } - - /** - * Merge other params into this. - * - * @param otherParams other params - * @return this - */ - public Params merge(Params otherParams) { - if (otherParams != null) { - this.params.putAll(otherParams.params); - } - return this; - } - - /** - * Creates and returns a deep clone of this Params. - * - * @return a deep clone of this Params - */ - @Override - public Params clone() { - Params newParams = new Params(); - newParams.params.putAll(this.params); - return newParams; - } - - private void assertMapperInited() { - if (mapper == null) { - mapper = new ObjectMapper(); - } - } - - private String valueToJson(Object value) { - assertMapperInited(); - try { - if (value == null) { - return null; - } - return mapper.writeValueAsString(value); - } catch (JsonProcessingException e) { - throw new RuntimeException("Failed to serialize to json:" + value, e); - } - } - - private <T> T valueFromJson(String json, Class<T> clazz) { - assertMapperInited(); - try { - if (json == null) { - return null; - } - return mapper.readValue(json, clazz); - } catch (IOException e) { - throw new RuntimeException("Failed to deserialize json:" + json, e); - } - } - - private <V> List<String> getParamNameAndAlias(ParamInfo<V> info) { - List<String> paramNames = new ArrayList<>(info.getAlias().length + 1); - paramNames.add(info.getName()); - paramNames.addAll(Arrays.asList(info.getAlias())); - return paramNames; - } -} diff --git a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/WithParams.java b/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/WithParams.java deleted file mode 100644 index dfbf614..0000000 --- a/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/WithParams.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.api.misc.param; - -/** - * Parameters are widely used in machine learning realm. This class defines a common interface to - * interact with classes with parameters. - * - * @param <T> the actual type of this WithParams, as the return type of setter - */ -public interface WithParams<T> { - /** - * Returns the all the parameters. - * - * @return all the parameters. - */ - Params getParams(); - - /** - * Set the value of a specific parameter. - * - * @param info the info of the specific param to set - * @param value the value to be set to the specific param - * @param <V> the type of the specific param - * @return the WithParams itself - */ - @SuppressWarnings("unchecked") - default <V> T set(ParamInfo<V> info, V value) { - getParams().set(info, value); - return (T) this; - } - - /** - * Returns the value of the specific param. - * - * @param info the info of the specific param, usually with default value - * @param <V> the type of the specific param - * @return the value of the specific param, or default value defined in the {@code info} if the - * inner Params doesn't contains this param - */ - default <V> V get(ParamInfo<V> info) { - return getParams().get(info); - } -} diff --git a/flink-ml-api/src/main/java/org/apache/flink/ml/util/param/ExtractParamInfosUtil.java b/flink-ml-api/src/main/java/org/apache/flink/ml/util/param/ExtractParamInfosUtil.java deleted file mode 100644 index 5206c0a..0000000 --- a/flink-ml-api/src/main/java/org/apache/flink/ml/util/param/ExtractParamInfosUtil.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.util.param; - -import org.apache.flink.ml.api.misc.param.ParamInfo; -import org.apache.flink.ml.api.misc.param.WithParams; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; - -/** Utility to extract all ParamInfos defined in a WithParams, mainly used in persistence. */ -public final class ExtractParamInfosUtil { - private static final Logger LOG = LoggerFactory.getLogger(ExtractParamInfosUtil.class); - - /** - * Extracts all ParamInfos defined in the given WithParams, including those in its superclasses - * and interfaces. - * - * @param s the WithParams to extract ParamInfos from - * @return the list of all ParamInfos defined in s - */ - public static List<ParamInfo> extractParamInfos(WithParams s) { - return extractParamInfos(s, s.getClass()); - } - - private static List<ParamInfo> extractParamInfos(WithParams s, Class clz) { - List<ParamInfo> result = new ArrayList<>(); - if (clz == null) { - return result; - } - - Field[] fields = clz.getDeclaredFields(); - for (Field f : fields) { - f.setAccessible(true); - if (ParamInfo.class.isAssignableFrom(f.getType())) { - try { - result.add((ParamInfo) f.get(s)); - } catch (IllegalAccessException e) { - LOG.warn("Failed to extract param info {}, ignore it", f.getName(), e); - } - } - } - - result.addAll(extractParamInfos(s, clz.getSuperclass())); - for (Class c : clz.getInterfaces()) { - result.addAll(extractParamInfos(s, c)); - } - - return result; - } -} diff --git a/flink-ml-api/src/test/java/org/apache/flink/ml/api/misc/ParamsTest.java b/flink-ml-api/src/test/java/org/apache/flink/ml/api/misc/ParamsTest.java deleted file mode 100644 index 080e22b..0000000 --- a/flink-ml-api/src/test/java/org/apache/flink/ml/api/misc/ParamsTest.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.api.misc; - -import org.apache.flink.ml.api.misc.param.ParamInfo; -import org.apache.flink.ml.api.misc.param.ParamInfoFactory; -import org.apache.flink.ml.api.misc.param.Params; - -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -/** Test for the behavior and validator of {@link Params}. */ -public class ParamsTest { - @Rule public ExpectedException thrown = ExpectedException.none(); - - @Test - public void testDefaultBehavior() { - Params params = new Params(); - - ParamInfo<String> optionalWithoutDefault = - ParamInfoFactory.createParamInfo("a", String.class).build(); - - // It should call params.contain to check when get the param in this case. - thrown.expect(RuntimeException.class); - thrown.expectMessage("Cannot find default value for optional parameter a"); - params.get(optionalWithoutDefault); - - ParamInfo<String> optionalWithDefault = - ParamInfoFactory.createParamInfo("a", String.class) - .setHasDefaultValue("def") - .build(); - assert params.get(optionalWithDefault).equals("def"); - - ParamInfo<String> requiredWithDefault = - ParamInfoFactory.createParamInfo("a", String.class) - .setRequired() - .setHasDefaultValue("def") - .build(); - assert params.get(requiredWithDefault).equals("def"); - - ParamInfo<String> requiredWithoutDefault = - ParamInfoFactory.createParamInfo("a", String.class).setRequired().build(); - thrown.expect(RuntimeException.class); - thrown.expectMessage("a not exist which is not optional and don't have a default value"); - params.get(requiredWithoutDefault); - } - - @Test - public void testValidator() { - Params params = new Params(); - - ParamInfo<Integer> intParam = - ParamInfoFactory.createParamInfo("a", Integer.class) - .setValidator(i -> i > 0) - .build(); - params.set(intParam, 1); - - thrown.expect(RuntimeException.class); - thrown.expectMessage("Setting a as a invalid value:0"); - params.set(intParam, 0); - } - - @Test - public void getOptionalParam() { - ParamInfo<String> key = - ParamInfoFactory.createParamInfo("key", String.class) - .setHasDefaultValue(null) - .setDescription("") - .build(); - - Params params = new Params(); - Assert.assertNull(params.get(key)); - - String val = "3"; - params.set(key, val); - Assert.assertEquals(params.get(key), val); - - params.set(key, null); - Assert.assertNull(params.get(key)); - } - - @Test - public void getOptionalWithoutDefaultParam() { - ParamInfo<String> key = - ParamInfoFactory.createParamInfo("key", String.class) - .setOptional() - .setDescription("") - .build(); - Params params = new Params(); - - try { - String val = params.get(key); - Assert.fail("Should throw exception."); - } catch (IllegalArgumentException ex) { - Assert.assertTrue( - ex.getMessage().startsWith("Cannot find default value for optional parameter")); - } - - Assert.assertFalse(params.contains(key)); - - String val = "3"; - params.set(key, val); - Assert.assertEquals(params.get(key), val); - - Assert.assertTrue(params.contains(key)); - - params.set(key, null); - Assert.assertNull(params.get(key)); - } - - @Test - public void getRequiredParam() { - ParamInfo<String> labelWithRequired = - ParamInfoFactory.createParamInfo("label", String.class) - .setDescription("") - .setRequired() - .build(); - Params params = new Params(); - try { - params.get(labelWithRequired); - Assert.fail("failure"); - } catch (IllegalArgumentException ex) { - Assert.assertTrue(ex.getMessage().startsWith("Missing non-optional parameter")); - } - - params.set(labelWithRequired, null); - Assert.assertNull(params.get(labelWithRequired)); - - String val = "3"; - params.set(labelWithRequired, val); - Assert.assertEquals(params.get(labelWithRequired), val); - } - - @Test - public void testGetAliasParam() { - ParamInfo<String> predResultColName = - ParamInfoFactory.createParamInfo("predResultColName", String.class) - .setDescription("Column name of predicted result.") - .setRequired() - .setAlias(new String[] {"predColName", "outputColName"}) - .build(); - - Params params = Params.fromJson("{\"predResultColName\":\"\\\"f0\\\"\"}"); - - Assert.assertEquals("f0", params.get(predResultColName)); - - params = - Params.fromJson( - "{\"predResultColName\":\"\\\"f0\\\"\", \"predColName\":\"\\\"f0\\\"\"}"); - - try { - params.get(predResultColName); - Assert.fail("failure"); - } catch (IllegalArgumentException ex) { - Assert.assertTrue( - ex.getMessage() - .startsWith( - "Duplicate parameters of predResultColName and predColName")); - } - } -} diff --git a/flink-ml-api/src/test/java/org/apache/flink/ml/util/param/ExtractParamInfosUtilTest.java b/flink-ml-api/src/test/java/org/apache/flink/ml/util/param/ExtractParamInfosUtilTest.java deleted file mode 100644 index 1010bfb..0000000 --- a/flink-ml-api/src/test/java/org/apache/flink/ml/util/param/ExtractParamInfosUtilTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.flink.ml.util.param; - -import org.apache.flink.ml.api.misc.param.ParamInfo; -import org.apache.flink.ml.api.misc.param.ParamInfoFactory; -import org.apache.flink.ml.api.misc.param.Params; -import org.apache.flink.ml.api.misc.param.WithParams; - -import org.junit.Test; - -import java.util.Arrays; -import java.util.List; - -/** Test for {@link ExtractParamInfosUtil}. */ -public class ExtractParamInfosUtilTest { - - @Test - public void testExtractParamInfos() { - List<ParamInfo> noParamInfos = - ExtractParamInfosUtil.extractParamInfos(new WithNoParamInfo()); - assert noParamInfos.isEmpty(); - - List<ParamInfo> classParamInfos = - ExtractParamInfosUtil.extractParamInfos(new WithTestParamInfo()); - assert classParamInfos.size() == 1 && classParamInfos.get(0).getName().equals("KSC"); - - List<ParamInfo> allParamInfos = - ExtractParamInfosUtil.extractParamInfos(new TestParamInfoWithInheritedParamInfos()); - String[] sortedCorrectParamNames = new String[] {"KCP", "KI", "KSC"}; - assert allParamInfos.size() == 3 - && Arrays.equals( - sortedCorrectParamNames, - allParamInfos.stream() - .map(ParamInfo::getName) - .sorted() - .toArray(String[]::new)); - } - - /** Mock WithParams implementation with no ParamInfo. Only for test. */ - public static class WithNoParamInfo implements WithParams<WithNoParamInfo> { - - @Override - public Params getParams() { - return null; - } - } - - /** - * Mock WithParams implementation with one ParamInfo. Only for test. - * - * @param <T> subclass of WithTestParamInfo - */ - public static class WithTestParamInfo<T extends WithTestParamInfo> implements WithParams<T> { - public static final ParamInfo<String> KSC = - ParamInfoFactory.createParamInfo("KSC", String.class) - .setDescription("key from super class") - .build(); - - @Override - public Params getParams() { - return null; - } - } - - /** - * Mock interface extending WithParams with one ParamInfo. Only for test. - * - * @param <T> implementation class of InterfaceWithParamInfo - */ - public interface InterfaceWithParamInfo<T extends InterfaceWithParamInfo> - extends WithParams<T> { - ParamInfo<String> KI = - ParamInfoFactory.createParamInfo("KI", String.class) - .setDescription("key from interface") - .build(); - } - - /** Mock WithParams inheriting ParamInfos from superclass and interface. Only for test. */ - public static class TestParamInfoWithInheritedParamInfos - extends WithTestParamInfo<TestParamInfoWithInheritedParamInfos> - implements InterfaceWithParamInfo<TestParamInfoWithInheritedParamInfos> { - private static final ParamInfo<String> KCP = - ParamInfoFactory.createParamInfo("KCP", String.class) - .setDescription("key in the class which is private") - .build(); - - @Override - public Params getParams() { - return null; - } - } -}
