ex00 commented on a change in pull request #8632: [FLINK-12744][ml] add shared
params in ml package
URL: https://github.com/apache/flink/pull/8632#discussion_r293372844
##########
File path:
flink-ml-parent/flink-ml-api/src/main/java/org/apache/flink/ml/api/misc/param/Params.java
##########
@@ -114,38 +171,104 @@ public Params clone() {
* @return a json containing all parameters in this Params
*/
public String toJson() {
- ObjectMapper mapper = new ObjectMapper();
- Map<String, String> stringMap = new HashMap<>();
try {
- for (Map.Entry<String, Object> e : paramMap.entrySet())
{
- stringMap.put(e.getKey(),
mapper.writeValueAsString(e.getValue()));
- }
- return mapper.writeValueAsString(stringMap);
+ 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.
The class mapping of the
- * parameters in the json is required because it is hard to directly
restore a param of a user
- * defined type. Params will be treated as String if it doesn't exist
in the {@code classMap}.
+ * the one who was serialized to the input json after the restoration.
*
- * @param json the json String to restore from
- * @param classMap the classes of the parameters contained in the json
+ * @param json the json String to restore from
*/
@SuppressWarnings("unchecked")
- public void loadJson(String json, Map<String, Class<?>> classMap) {
+ public void loadJson(String json) {
ObjectMapper mapper = new ObjectMapper();
+ Map<String, String> params;
try {
- Map<String, String> m = mapper.readValue(json,
Map.class);
- for (Map.Entry<String, String> e : m.entrySet()) {
- Class<?> valueClass =
classMap.getOrDefault(e.getKey(), String.class);
- paramMap.put(e.getKey(),
mapper.readValue(e.getValue(), valueClass));
+ params = mapper.readValue(json, Map.class);
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to deserialize
json:" + json, e);
+ }
+ this.params.clear();
+ 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> paramInfo) {
+ List<String> paramNames = new ArrayList <>();
Review comment:
please init `ArrayList` by `paramInfo.getAlias().length + 1`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services