Skip to content

Commit bc99d93

Browse files
authored
Merge pull request #656 from zinggAI/issue655
refactoring to remove static methods in Arguments issue #655
2 parents 0b3de0e + 6fba545 commit bc99d93

File tree

11 files changed

+283
-237
lines changed

11 files changed

+283
-237
lines changed

Diff for: common/client/src/main/java/zingg/common/client/Arguments.java

+4-203
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ public class Arguments implements Serializable {
112112
long blockSize = 100L;
113113
String column;
114114
String obviousDupeCondition;
115-
private static final String ENV_VAR_MARKER_START = "$";
116-
private static final String ENV_VAR_MARKER_END = "$";
117-
private static final String ESC = "\\";
118-
private static final String PATTERN_ENV_VAR = ESC + ENV_VAR_MARKER_START + "(.+?)" + ESC + ENV_VAR_MARKER_END;
119115

120116

121117
public void setThreshold(double threshold) {
@@ -129,173 +125,6 @@ public void setThreshold(double threshold) {
129125
*/
130126
public Arguments() {
131127
}
132-
133-
/**
134-
* Create arguments from a json file
135-
*
136-
* @param filePath
137-
* json file containing arguments
138-
* @return Arguments object populated through JSON
139-
* @throws ZinggClientException
140-
* in case of invlaid/wrong json/file not found
141-
*/
142-
public static final Arguments createArgumentsFromJSON(String filePath)
143-
throws ZinggClientException {
144-
return Arguments.createArgumentsFromJSON(filePath, "match");
145-
}
146-
147-
/**
148-
* Create arguments from a json file
149-
*
150-
* @param filePath
151-
* json file containing arguments
152-
* @return Arguments object populated through JSON
153-
* @throws ZinggClientException
154-
* in case of invlaid/wrong json/file not found
155-
*/
156-
public static final Arguments createArgumentsFromJSON(String filePath, String phase)
157-
throws ZinggClientException {
158-
try {
159-
ObjectMapper mapper = new ObjectMapper();
160-
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
161-
true);
162-
LOG.warn("Config Argument is " + filePath);
163-
/*SimpleModule module = new SimpleModule();
164-
module.addDeserializer(List<MatchType>.class, new FieldDefinition.MatchTypeDeserializer());
165-
mapper.registerModule(module);
166-
*/
167-
Arguments args = mapper.readValue(new File(filePath), Arguments.class);
168-
LOG.warn("phase is " + phase);
169-
checkValid(args, phase);
170-
return args;
171-
} catch (Exception e) {
172-
//e.printStackTrace();
173-
throw new ZinggClientException("Unable to parse the configuration at " + filePath +
174-
". The error is " + e.getMessage(), e);
175-
}
176-
}
177-
178-
/**
179-
* Write arguments to a json file
180-
*
181-
* @param filePath
182-
* json file where arguments shall be written to
183-
* @return Arguments object
184-
* @throws ZinggClientException
185-
* in case there is an error in writing to file
186-
*/
187-
public static final void writeArgumentsToJSON(String filePath, Arguments args)
188-
throws ZinggClientException {
189-
try {
190-
ObjectMapper mapper = new ObjectMapper();
191-
mapper.enable(SerializationFeature.INDENT_OUTPUT);
192-
mapper.getFactory().configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(),true);
193-
LOG.warn("Arguments are written to file: " + filePath);
194-
mapper.writeValue(new File(filePath), args);
195-
} catch (Exception e) {
196-
throw new ZinggClientException("Unable to write the configuration to " + filePath +
197-
". The error is " + e.getMessage(), e);
198-
}
199-
}
200-
201-
public static void checkValid(Arguments args, String phase) throws ZinggClientException {
202-
if (phase.equals("train") || phase.equals("match") || phase.equals("trainMatch") || phase.equals("link")) {
203-
checkIsValid(args);
204-
}
205-
else if(phase.equals("seed") || phase.equals("seedDB")){
206-
checkIsValidForLabelling(args);
207-
}
208-
else if (!phase.equalsIgnoreCase("WEB")){
209-
checkIsValidForOthers(args);
210-
}
211-
}
212-
213-
public static final Arguments createArgumentsFromJSONString(String data, String phase)
214-
throws ZinggClientException {
215-
try {
216-
ObjectMapper mapper = new ObjectMapper();
217-
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
218-
true);
219-
Arguments args = mapper.readValue(data, Arguments.class);
220-
LOG.warn("phase is " + phase);
221-
checkValid(args, phase);
222-
return args;
223-
} catch (Exception e) {
224-
//e.printStackTrace();
225-
throw new ZinggClientException("Unable to parse the configuration at " + data +
226-
". The error is " + e.getMessage());
227-
}
228-
}
229-
230-
public static final Arguments createArgumentsFromJSONTemplate(String filePath, String phase)
231-
throws ZinggClientException {
232-
try {
233-
LOG.warn("Config Argument is " + filePath);
234-
byte[] encoded = Files.readAllBytes(Paths.get(filePath));
235-
String template = new String(encoded, StandardCharsets.UTF_8);
236-
Map<String, String> env = System.getenv();
237-
String updatedJson = substituteVariables(template, env);
238-
Arguments args = createArgumentsFromJSONString(updatedJson, phase);
239-
return args;
240-
} catch (Exception e) {
241-
//e.printStackTrace();
242-
throw new ZinggClientException("Unable to parse the configuration at " + filePath +
243-
". The error is " + e.getMessage());
244-
}
245-
}
246-
247-
public static String substituteVariables(String template, Map<String, String> variables) throws ZinggClientException {
248-
Pattern pattern = Pattern.compile(PATTERN_ENV_VAR);
249-
Matcher matcher = pattern.matcher(template);
250-
// StringBuilder cannot be used here because Matcher expects StringBuffer
251-
StringBuffer buffer = new StringBuffer();
252-
while (matcher.find()) {
253-
if (variables.containsKey(matcher.group(1))) {
254-
String replacement = variables.get(matcher.group(1));
255-
if (replacement == null || replacement.equals("")) {
256-
throw new ZinggClientException("The environment variable for " + ENV_VAR_MARKER_START
257-
+ matcher.group(1) + ENV_VAR_MARKER_END + " is not set or is empty string");
258-
}
259-
// quote to work properly with $ and {,} signs
260-
matcher.appendReplacement(buffer, replacement != null ? Matcher.quoteReplacement(replacement) : "null");
261-
LOG.warn("The variable " + ENV_VAR_MARKER_START + matcher.group(1) + ENV_VAR_MARKER_END
262-
+ " has been substituted");
263-
} else {
264-
throw new ZinggClientException("The environment variable for " + ENV_VAR_MARKER_START + matcher.group(1)
265-
+ ENV_VAR_MARKER_END + " is not set");
266-
}
267-
}
268-
matcher.appendTail(buffer);
269-
return buffer.toString();
270-
}
271-
272-
public static final void writeArgumentstoJSON(String filePath, Arguments args) throws ZinggClientException {
273-
try{
274-
ObjectMapper mapper = new ObjectMapper();
275-
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
276-
true);
277-
mapper.registerModule(new DefaultScalaModule());
278-
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(filePath), args);
279-
}
280-
catch(Exception e) {
281-
e.printStackTrace();
282-
throw new ZinggClientException("Unable to create arguments for the job");
283-
}
284-
}
285-
286-
public static final String writeArgumentstoJSONString(Arguments args) throws ZinggClientException {
287-
try{
288-
ObjectMapper mapper = new ObjectMapper();
289-
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
290-
true);
291-
mapper.registerModule(new DefaultScalaModule());
292-
return mapper.writeValueAsString(args);
293-
}
294-
catch(Exception e) {
295-
e.printStackTrace();
296-
throw new ZinggClientException("Unable to create arguments for the job");
297-
}
298-
}
299128

300129
public int getNumPartitions() {
301130
return numPartitions;
@@ -458,44 +287,16 @@ public void setData(Pipe[] dataFile) throws ZinggClientException {
458287
checkNullBlankEmpty(dataFile, "file to be matched");
459288
this.data = dataFile;
460289
}
461-
462-
463-
/**
464-
* Checks if the given arguments are correct or not
465-
* @param args
466-
* @throws ZinggClientException
467-
*/
468-
public static void checkIsValid(Arguments args) throws ZinggClientException {
469-
Arguments arg = new Arguments();
470-
arg.setTrainingSamples(args.getTrainingSamples());
471-
arg.setData(args.getData());
472-
arg.setNumPartitions(args.getNumPartitions());
473-
arg.setFieldDefinition(args.getFieldDefinition());
474-
}
475290

476-
public static void checkIsValidForOthers(Arguments args) throws ZinggClientException {
477-
Arguments arg = new Arguments();
478-
arg.setData(args.getData());
479-
arg.setNumPartitions(args.getNumPartitions());
480-
}
481-
482-
483-
public static void checkIsValidForLabelling(Arguments args) throws ZinggClientException {
484-
Arguments arg = new Arguments();
485-
//arg.setPositiveTrainingSamples(args.getPositiveTrainingSamples());
486-
//arg.setNegativeTrainingSamples(args.getNegativeTrainingSamples());
487-
arg.setData(args.getData());
488-
arg.setNumPartitions(args.getNumPartitions());
489-
arg.setFieldDefinition(args.getFieldDefinition());
490-
}
491-
492-
public static void checkNullBlankEmpty(String field, String fieldName) throws ZinggClientException {
291+
@JsonIgnore
292+
public void checkNullBlankEmpty(String field, String fieldName) throws ZinggClientException {
493293
if (field == null || field.trim().length() == 0) {
494294
throw new ZinggClientException("Missing value for " + fieldName + ". Trying to set " + field);
495295
}
496296
}
497297

498-
public static void checkNullBlankEmpty(Pipe[] field, String fieldName) throws ZinggClientException {
298+
@JsonIgnore
299+
public void checkNullBlankEmpty(Pipe[] field, String fieldName) throws ZinggClientException {
499300
if (field == null || field.length == 0) {
500301
throw new ZinggClientException("Missing value for " + fieldName + ". Trying to set " + field);
501302
}

0 commit comments

Comments
 (0)