lindong28 commented on a change in pull request #10: URL: https://github.com/apache/flink-ml/pull/10#discussion_r744285724
########## File path: flink-ml-api/src/main/java/org/apache/flink/ml/param/Param.java ########## @@ -0,0 +1,98 @@ +/* + * 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.param; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.ml.util.ReadWriteUtils; + +import java.io.IOException; +import java.io.Serializable; + +/** + * Definition of a parameter, including name, class, description, default value and the validator. + * + * @param <T> The class type of the parameter value. + */ +@PublicEvolving +public class Param<T> implements Serializable { + private static final long serialVersionUID = 4396556083935765299L; + + public final String name; + public final Class<T> clazz; + public final String description; + public final T defaultValue; + public final ParamValidator<T> validator; + + public Param( + String name, + Class<T> clazz, + String description, + T defaultValue, + ParamValidator<T> validator) { + this.name = name; + this.clazz = clazz; + this.description = description; + this.defaultValue = defaultValue; + this.validator = validator; + + if (defaultValue != null && !validator.validate(defaultValue)) { Review comment: Hmm.. I think we can not remove the `defaultValue != null` here. The reason is that we should allow the default value of a parameter to be null. This should be OK as long as user explicitly sets a non-null value for this parameter later. But you are right the PR does not ensure this behavior and needs to be fixed. I have updated `WithParams::get` to validate the parameter using the validator if the parameter value is null. Now the latest PR will always check the final parameter value using the validator: - If the parameter value is explicitly set by user, its value will be checked in the `WithParams:set(...)` call. - Otherwise, if its default value is not null, its value will be checked in the Param constructor. - Otherwise, its default value must be null, then its value will be checked in the `WithParams::get(...)` call. The unit test `testParamWithNullDefault` covers this issue. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
