Hello everyone,
I got a code from Travis Cox where he showed me how I can add an Expression function. The name of the example class is "ConvertBase". I used that as a template and coded the functions I was asked to implement. However, for whatever reason, The getArgDocString can be seen in inductive automation with Travis' example but not with the functions I implemented. Does anyone know why?
public class ConvertBase implements Function:
@Override public QualifiedValue execute(Expression[] expressions) throws ExpressionException { QualifiedValue from = expressions[0].execute(); QualifiedValue to = expressions[1].execute(); QualifiedValue numberToConvert = expressions[2].execute();
Quality quality = DataQuality.worstOf(from.getQuality(), to.getQuality()); quality = DataQuality.worstOf(quality, numberToConvert.getQuality());
Integer inputInt = Integer.valueOf(numberToConvert.getValue().toString(), (Integer) from.getValue()); String output = Integer.toString(inputInt, (Integer) to.getValue());
return new BasicQualifiedValue(output, quality); }
@Override public void initArgs(Expression[] args) { if (!validateNumArgs(args.length)) { throw new RuntimeException("Function '" + "ConvertBase" + "(" + getArgDocString() + ")' does not work with " + args.length + " arguments."); } else { for (int i = 0; i < args.length; i++) { if (!validateArgType(i, args[i].getType())) { throw new RuntimeException("Type mismatch: Argument " + i + " for function '" + "ConvertBase" + "(" + getArgDocString() + ")' is an invalid type."); } } } }
protected boolean validateNumArgs(int num) { return num == 3; }
protected boolean validateArgType(int argNum, Class<?> type) { boolean ret = false;
switch (argNum) { case 0: ret = type == Integer.class; break; case 1: ret = type == Integer.class; break; case 2: ret = type == String.class; break; }
return ret; }
@Override public String getArgDocString() { return "from, to, numberToConvert"; }
}
public class TagValue implements Function :
@Override
public QualifiedValue execute(Expression args) throws ExpressionException {
//Initialization phase
String database = args[0].execute().getValue().toString();
String tagName = args[1].execute().getValue().toString();
if(args.length == 3){
this.defaultErrorValue = Double.parseDouble(args[2].execute().getValue().toString());
}PWPreparedStatement preparedStatement = new PWPreparedStatement(server, database, tagName); return new BasicQualifiedValue(preparedStatement.getResult(defaultErrorValue));
}
@Override
public String getArgDocString() {
return "db, tagname, defaultError(optional)";
}protected boolean validateNumArgs(int num) {
if (num == 2 || num == 3){
return true;
}
return false;
}
protected boolean validateArgType(int argNum, Class<?> type) { boolean ret = false; switch (argNum) { case 0: ret = type == String.class; break; case 1: ret = type == String.class; break; case 2: ret = type == Double.class; break; }
return ret; }
@Override
public void initArgs(Expression args) {
if (!validateNumArgs(args.length)) {
throw new RuntimeException("Function '" + "TagValue" + "(" + getArgDocString()
+ ")' does not work with " + args.length + " arguments.");
} else {
for (int i = 0; i < args.length; i++) {
if (!validateArgType(i, args[i].getType())) {
throw new RuntimeException("Type mismatch: Argument " + i + " for function '" + "TagValue"
+ "(" + getArgDocString() + ")' is an invalid type.");
}
}
}
}
}
Here the picture.
Thank you