[Java] Refactor java api (#8858)

This commit is contained in:
chaokunyang
2020-06-12 10:49:01 +08:00
committed by GitHub
parent cae475c46a
commit dfa4768fc6
64 changed files with 1854 additions and 3394 deletions
@@ -23,6 +23,11 @@ public class RayCallGenerator extends BaseGenerator {
newLine("");
newLine("package io.ray.api;");
newLine("");
newLine("import io.ray.api.call.ActorCreator;");
newLine("import io.ray.api.call.PyActorCreator;");
newLine("import io.ray.api.call.PyTaskCaller;");
newLine("import io.ray.api.call.TaskCaller;");
newLine("import io.ray.api.call.VoidTaskCaller;");
newLine("import io.ray.api.function.PyActorClass;");
newLine("import io.ray.api.function.PyRemoteFunction;");
for (int i = 0; i <= MAX_PARAMETERS; i++) {
@@ -31,43 +36,35 @@ public class RayCallGenerator extends BaseGenerator {
for (int i = 0; i <= MAX_PARAMETERS; i++) {
newLine("import io.ray.api.function.RayFuncVoid" + i + ";");
}
newLine("import io.ray.api.options.ActorCreationOptions;");
newLine("import io.ray.api.options.CallOptions;");
newLine("");
newLine("/**");
newLine(" * This class provides type-safe interfaces for `Ray.call` and `Ray.createActor`.");
newLine(" **/");
newLine("@SuppressWarnings({\"rawtypes\", \"unchecked\"})");
newLine("class RayCall {");
newLine(1, "// =======================================");
newLine(1, "// Methods for remote function invocation.");
newLine(1, "// =======================================");
for (int i = 0; i <= MAX_PARAMETERS; i++) {
buildCalls(i, false, false, true, false);
buildCalls(i, false, false, true, true);
buildCalls(i, false, false, false, false);
buildCalls(i, false, false, false, true);
buildCalls(i, false, false, true);
buildCalls(i, false, false, false);
}
newLine(1, "// ===========================");
newLine(1, "// Methods for actor creation.");
newLine(1, "// ===========================");
for (int i = 0; i <= MAX_PARAMETERS; i++) {
buildCalls(i, false, true, true, false);
buildCalls(i, false, true, true, true);
buildCalls(i, false, true, true);
}
newLine(1, "// ===========================");
newLine(1, "// Cross-language methods.");
newLine(1, "// ===========================");
for (int i = 0; i <= MAX_PARAMETERS; i++) {
buildPyCalls(i, false, false, false);
buildPyCalls(i, false, false, true);
buildPyCalls(i, false, false);
}
for (int i = 0; i <= MAX_PARAMETERS; i++) {
buildPyCalls(i, false, true, false);
buildPyCalls(i, false, true, true);
buildPyCalls(i, false, true);
}
newLine("}");
return sb.toString();
@@ -83,6 +80,8 @@ public class RayCallGenerator extends BaseGenerator {
newLine("");
newLine("package io.ray.api;");
newLine("");
newLine("import io.ray.api.call.ActorTaskCaller;");
newLine("import io.ray.api.call.VoidActorTaskCaller;");
for (int i = 1; i <= MAX_PARAMETERS; i++) {
newLine("import io.ray.api.function.RayFunc" + i + ";");
}
@@ -93,12 +92,11 @@ public class RayCallGenerator extends BaseGenerator {
newLine("/**");
newLine(" * This class provides type-safe interfaces for remote actor calls.");
newLine(" **/");
newLine("@SuppressWarnings({\"rawtypes\", \"unchecked\"})");
newLine("interface ActorCall<A> {");
newLine("");
for (int i = 0; i <= MAX_PARAMETERS - 1; i++) {
buildCalls(i, true, false, true, false);
buildCalls(i, true, false, false, false);
buildCalls(i, true, false, true);
buildCalls(i, true, false, false);
}
newLine("}");
return sb.toString();
@@ -114,16 +112,16 @@ public class RayCallGenerator extends BaseGenerator {
newLine("");
newLine("package io.ray.api;");
newLine("");
newLine("import io.ray.api.call.PyActorTaskCaller;");
newLine("import io.ray.api.function.PyActorMethod;");
newLine("");
newLine("/**");
newLine(" * This class provides type-safe interfaces for remote actor calls.");
newLine(" **/");
newLine("@SuppressWarnings({\"rawtypes\", \"unchecked\"})");
newLine("interface PyActorCall {");
newLine("");
for (int i = 0; i <= MAX_PARAMETERS - 1; i++) {
buildPyCalls(i, true, false, false);
buildPyCalls(i, true, false);
}
newLine("}");
return sb.toString();
@@ -139,11 +137,11 @@ public class RayCallGenerator extends BaseGenerator {
* @param forActorCreation Build `Ray.createActor` when true, otherwise build `Ray.call`.
*/
private void buildCalls(int numParameters, boolean forActor,
boolean forActorCreation, boolean hasReturn, boolean hasOptionsParam) {
boolean forActorCreation, boolean hasReturn) {
// Template of the generated function:
// [modifiers] [genericTypes] [returnType] [callFunc]([argsDeclaration]) {
// Objects[] args = new Object[]{[args]};
// return Ray.internal().[callFunc](f[, getThis()], args[, options]);
// return new [Caller](func, args);
// }
String modifiers = forActor ? "default" : "public static";
@@ -170,9 +168,13 @@ public class RayCallGenerator extends BaseGenerator {
// 2) Construct the `returnType` part.
String returnType;
if (forActorCreation) {
returnType = "ActorHandle<A>";
returnType = "ActorCreator<A>";
} else {
returnType = hasReturn ? "ObjectRef<R>" : "void";
if (forActor) {
returnType = hasReturn ? "ActorTaskCaller<R>" : "VoidActorTaskCaller";
} else {
returnType = hasReturn ? "TaskCaller<R>" : "VoidTaskCaller";
}
}
// 3) Construct the `argsDeclaration` part.
@@ -189,16 +191,21 @@ public class RayCallGenerator extends BaseGenerator {
!forActor ? numParameters : numParameters + 1,
rayFuncGenericTypes);
String callFunc = forActorCreation ? "createActor" : "call";
String internalCallFunc = forActorCreation ? "createActor" : forActor ? "callActor" : "call";
String callFunc = forActorCreation ? "actor" : "task";
String caller;
if (forActorCreation) {
caller = "ActorCreator<>";
} else {
if (forActor) {
caller = hasReturn ? "ActorTaskCaller<>" : "VoidActorTaskCaller";
} else {
caller = hasReturn ? "TaskCaller<>" : "VoidTaskCaller";
}
}
// Enumerate all combinations of the parameters.
for (String param : generateParameters(numParameters)) {
String argsDeclaration = argsDeclarationPrefix + param;
if (hasOptionsParam) {
argsDeclaration +=
forActorCreation ? "ActorCreationOptions options, " : "CallOptions options, ";
}
// Trim trailing ", ";
argsDeclaration = argsDeclaration.substring(0, argsDeclaration.length() - 2);
// Print the first line (method signature).
@@ -220,15 +227,13 @@ public class RayCallGenerator extends BaseGenerator {
newLine(2, String.format("Object[] args = new Object[]{%s};", args));
// 5) Construct the third line.
String callFuncArgs = "";
String ctrArgs = "";
if (forActor) {
callFuncArgs += "(ActorHandle) this, ";
ctrArgs += "(ActorHandle) this, ";
}
callFuncArgs += "f, args, ";
callFuncArgs += forActor ? "" : hasOptionsParam ? "options, " : "null, ";
callFuncArgs = callFuncArgs.substring(0, callFuncArgs.length() - 2);
newLine(2, String.format("%sRay.internal().%s(%s);",
hasReturn ? "return " : "", internalCallFunc, callFuncArgs));
ctrArgs += "f, args, ";
ctrArgs = ctrArgs.substring(0, ctrArgs.length() - 2);
newLine(2, String.format("return new %s(%s);", caller, ctrArgs));
newLine(1, "}");
newLine("");
}
@@ -241,12 +246,9 @@ public class RayCallGenerator extends BaseGenerator {
* @param numParameters the number of parameters
* @param forActor Build `actor.call` when true, otherwise build `Ray.call`.
* @param forActorCreation Build `Ray.createActor` when true, otherwise build `Ray.call`.
* @param hasOptionsParam Add ActorCreationOptions if forActorCreation is true;
* Add CallOptions if forActorCreation is false;
* No additional param if hasOptionsParam is false.
*/
private void buildPyCalls(int numParameters, boolean forActor,
boolean forActorCreation, boolean hasOptionsParam) {
boolean forActorCreation) {
String modifiers = forActor ? "default" : "public static";
String argList = "";
@@ -278,43 +280,26 @@ public class RayCallGenerator extends BaseGenerator {
paramPrefix += ", ";
}
String optionsParam;
if (hasOptionsParam) {
optionsParam = forActorCreation ? ", ActorCreationOptions options" : ", CallOptions options";
} else {
optionsParam = "";
}
String optionsArg;
if (forActor) {
optionsArg = "";
} else {
if (hasOptionsParam) {
optionsArg = ", options";
} else {
optionsArg = ", null";
}
}
String genericType = forActorCreation ? "" : " <R>";
String returnType = !forActorCreation ? "ObjectRef<R>" : "PyActorHandle";
String funcName = forActorCreation ? "createActor" : "call";
String internalCallFunc = forActorCreation ? "createActor" :
forActor ? "callActor" : "call";
String returnType = forActorCreation ? "PyActorCreator" :
forActor ? "PyActorTaskCaller<R>" : "PyTaskCaller<R>";
String funcName = forActorCreation ? "actor" : "task";
String caller = forActorCreation ? "PyActorCreator" :
forActor ? "PyActorTaskCaller<>" : "PyTaskCaller<>";
funcArgs += ", args";
// Method signature.
newLine(1, String.format(
"%s%s %s %s(%s%s) {", modifiers, genericType,
returnType, funcName, paramPrefix + paramList, optionsParam
"%s%s %s %s(%s) {", modifiers, genericType,
returnType, funcName, paramPrefix + paramList
));
// Method body.
newLine(2, String.format("Object[] args = new Object[]{%s};", argList));
if (forActor) {
newLine(2, String.format("return Ray.internal().%s((PyActorHandle)this, %s%s);",
internalCallFunc, funcArgs, optionsArg));
newLine(2, String.format("return new %s((PyActorHandle)this, %s);",
caller, funcArgs));
} else {
newLine(2, String.format("return Ray.internal().%s(%s%s);",
internalCallFunc, funcArgs, optionsArg));
newLine(2, String.format("return new %s(%s);", caller, funcArgs));
}
newLine(1, "}");
newLine("");
@@ -339,15 +324,15 @@ public class RayCallGenerator extends BaseGenerator {
public static void main(String[] args) throws IOException {
String path = System.getProperty("user.dir")
+ "/api/src/main/java/io.ray/api/RayCall.java";
+ "/api/src/main/java/io/ray/api/RayCall.java";
FileUtils.write(new File(path), new RayCallGenerator().generateRayCallDotJava(),
Charset.defaultCharset());
path = System.getProperty("user.dir")
+ "/api/src/main/java/io.ray/api/ActorCall.java";
+ "/api/src/main/java/io/ray/api/ActorCall.java";
FileUtils.write(new File(path), new RayCallGenerator().generateActorCallDotJava(),
Charset.defaultCharset());
path = System.getProperty("user.dir")
+ "/api/src/main/java/io.ray/api/PyActorCall.java";
+ "/api/src/main/java/io/ray/api/PyActorCall.java";
FileUtils.write(new File(path), new RayCallGenerator().generatePyActorCallDotJava(),
Charset.defaultCharset());
}
@@ -44,7 +44,7 @@ public class RayFuncGenerator extends BaseGenerator {
newLine("@FunctionalInterface");
String className = "RayFunc" + (hasReturn ? "" : "Void") + numParameters;
newLine(String.format("public interface %s%s extends %s {",
className, genericTypes, hasReturn ? "RayFunc" : "RayFuncVoid"));
className, genericTypes, hasReturn ? "RayFuncR<R>" : "RayFuncVoid"));
newLine("");
indents(1);
newLine(String.format("%s apply(%s) throws Exception;", hasReturn ? "R" : "void", paramList));