Skip to content

Commit

Permalink
Optimize comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yusu committed Apr 28, 2024
1 parent e96a052 commit f454dc7
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/alibaba/compileflow/engine/FlowModel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.alibaba.compileflow.engine;

/**
* Represents a flow model abstraction in the CompileFlow Engine.
* This interface defines the fundamental structure for various types of flow models,
* enabling the engine to handle and process them accordingly.
*
* @author wuxiang
* @author yusu
*/
public interface FlowModel {

// No methods are defined in this interface, as it serves as a marker for flow model classes.

}
118 changes: 116 additions & 2 deletions src/main/java/com/alibaba/compileflow/engine/ProcessEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,158 @@
import java.util.Map;

/**
* Provides methods for executing, triggering, and managing process flows.
*
* @param <T> The type extending {@link FlowModel} representing a flow model
* @author wuxiang
* @author yusu
*/
public interface ProcessEngine<T extends FlowModel> {

/**
* Executes a process flow with the given code and context.
*
* @param code Unique identifier of the process flow
* @param context Key-value pairs representing the execution context
* @return Result of the execution as a map of key-value pairs
*/
Map<String, Object> execute(String code, Map<String, Object> context);

/**
* Triggers a process flow with a specific tag and context.
*
* @param code Unique identifier of the process flow
* @param tag Identifier of the flow's tag
* @param context Key-value pairs representing the execution context
* @return Result of the trigger as a map of key-value pairs
*/
Map<String, Object> trigger(String code, String tag, Map<String, Object> context);

/**
* Triggers a process flow with a specific tag, event, and context.
*
* @param code Unique identifier of the process flow
* @param tag Identifier of the flow's tag
* @param event Event to trigger
* @param context Key-value pairs representing the execution context
* @return Result of the trigger as a map of key-value pairs
*/
Map<String, Object> trigger(String code, String tag, String event, Map<String, Object> context);

/**
* Pre-compiles a set of process flows by their codes.
*
* @param codes Array of unique identifiers for process flows to compile
*/
void preCompile(String... codes);

/**
* Pre-compiles a set of process flows with a custom class loader.
*
* @param classLoader Custom class loader to use during compilation
* @param codes Array of unique identifiers for process flows to compile
*/
void preCompile(ClassLoader classLoader, String... codes);

/**
* Loads a process flow by its code.
*
* @param code Unique identifier of the process flow to load
* @return A flow model instance representing the loaded process flow
*/
T load(String code);

/**
* Retrieves Java code representation of a process flow.
*
* @param code Unique identifier of the process flow
* @return Java code as a string
*/
String getJavaCode(String code);

/**
* Retrieves test code for a process flow.
*
* @param code Unique identifier of the process flow
* @return Test code as a string
*/
String getTestCode(String code);

/***
** --- Compared with APIs above, following APIs add content of bpm as input directly ---
// --- Methods with content input ---

/**
* Executes a process flow with the given code, context, and BPM content.
*
* @param code Unique identifier of the process flow
* @param context Key-value pairs representing the execution context
* @param content BPM content in string format
* @return Result of the execution as a map of key-value pairs
*/
Map<String, Object> execute(String code, Map<String, Object> context, String content);

/**
* Triggers a process flow with a specific tag, context, and BPM content.
*
* @param code Unique identifier of the process flow
* @param tag Identifier of the flow's tag
* @param context Key-value pairs representing the execution context
* @param content BPM content in string format
* @return Result of the trigger as a map of key-value pairs
*/
Map<String, Object> trigger(String code, String tag, Map<String, Object> context, String content);

/**
* Triggers a process flow with a specific tag, event, context, and BPM content.
*
* @param code Unique identifier of the process flow
* @param tag Identifier of the flow's tag
* @param event Event to trigger
* @param context Key-value pairs representing the execution context
* @param content BPM content in string format
* @return Result of the trigger as a map of key-value pairs
*/
Map<String, Object> trigger(String code, String tag, String event, Map<String, Object> context, String content);

/**
* Pre-compiles a set of process flows with their corresponding BPM content.
*
* @param code2ContentMap Mapping between unique identifiers and BPM content strings
*/
void preCompile(Map<String, String> code2ContentMap);

/**
* Pre-compiles a set of process flows with their corresponding BPM content and a custom class loader.
*
* @param classLoader Custom class loader to use during compilation
* @param code2ContentMap Mapping between unique identifiers and BPM content strings
*/
void preCompile(ClassLoader classLoader, Map<String, String> code2ContentMap);

/**
* Loads a process flow by its code and BPM content.
*
* @param code Unique identifier of the process flow to load
* @param content BPM content in string format
* @return A flow model instance representing the loaded process flow with given content
*/
T load(String code, String content);

/**
* Retrieves Java code representation of a process flow with its BPM content.
*
* @param code Unique identifier of the process flow
* @param content BPM content in string format
* @return Java code as a string
*/
String getJavaCode(String code, String content);

/**
* Retrieves test code for a process flow with its BPM content.
*
* @param code Unique identifier of the process flow
* @param content BPM content in string format
* @return Test code as a string
*/
String getTestCode(String code, String content);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.util.stream.Collectors;

/**
* Manages extensions and extension points, implementing the {@link Lifecycle} interface.
* It loads, sorts, and retrieves extensions based on their priorities.
*
* @author yusu
*/
public class ExtensionManager implements Lifecycle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.util.stream.Collectors;

/**
* Manages plugins, including loading, dependency checking, and registration of extension points and extensions.
* Implements the {@link Lifecycle} interface for initialization and stopping.
*
* @author yusu
*/
public class PluginManager implements Lifecycle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ public T load(String code, String content) {
throw new RuntimeException("No valid flow model found, code is " + code);
}

checkCycle(flowModel);
checkContinuous(flowModel);
validateFlowModel(flowModel);
sortTransition(flowModel);

return flowModel;
}

private void validateFlowModel(T flowModel) {
checkCycle(flowModel);
checkContinuous(flowModel);
}

@Override
public String getJavaCode(String code) {
return getJavaCode(code, null);
Expand Down Expand Up @@ -179,14 +183,7 @@ private void checkContinuous(TransitionNode node, List<TransitionNode> visitedNo

@SuppressWarnings("unchecked")
private void checkCycle(T flowModel) {
DirectedGraph<TransitionNode> directedGraph = new DirectedGraph<>();
for (TransitionNode node : flowModel.getAllNodes()) {
List<TransitionNode> outgoingNodes = node.getOutgoingNodes();
if (CollectionUtils.isNotEmpty(outgoingNodes)) {
outgoingNodes.forEach(
outgoingNode -> directedGraph.add(DirectedGraph.Edge.of(node, outgoingNode)));
}
}
DirectedGraph<TransitionNode> directedGraph = buildDirectedGraph(flowModel);
List<TransitionNode> cyclicVertexList = directedGraph.findCyclicVertexList();
if (CollectionUtils.isNotEmpty(cyclicVertexList)) {
throw new CompileFlowException("Cyclic nodes found in flow " + flowModel.getCode()
Expand All @@ -195,6 +192,17 @@ private void checkCycle(T flowModel) {
}
}

private DirectedGraph<TransitionNode> buildDirectedGraph(T flowModel) {
DirectedGraph<TransitionNode> directedGraph = new DirectedGraph<>();
for (TransitionNode node : flowModel.getAllNodes()) {
List<TransitionNode> outgoingNodes = node.getOutgoingNodes();
if (CollectionUtils.isNotEmpty(outgoingNodes)) {
outgoingNodes.forEach(outgoingNode -> directedGraph.add(DirectedGraph.Edge.of(node, outgoingNode)));
}
}
return directedGraph;
}

private void sortTransition(T flowModel) {
flowModel.getAllNodes().forEach(node -> node.getTransitions()
.sort(Comparator.comparing(TransitionSupport::getPriority).reversed()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
package com.alibaba.compileflow.engine.process.preruntime.compiler;

/**
* Represents a compiler interface responsible for compiling Java source code into classes.
*
* @author yusu
*/
public interface Compiler {

/**
* Compiles the given Java source code into a class, using the specified fully qualified class name and class loader.
*
* @param fullClassName The fully qualified name of the class to be compiled
* @param sourceCode The Java source code to compile
* @param classLoader The class loader used for loading the compiled class
* @return The generated class after successful compilation
*/
Class<?> compileJavaCode(String fullClassName, String sourceCode, ClassLoader classLoader);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@
import java.io.File;

/**
* Represents an extension point for compiling Java source code into bytecode.
* Implementations of this interface are responsible for transforming Java source
* into class files according to the specified options.
* @author yusu
*/
public interface JavaCompiler extends IExtensionPoint {

String EXT_COMPILE_CODE = "com.alibaba.compileflow.engine.process.preruntime.compiler.JavaCompiler.compile";

/**
* Compiles the given Java source and writes the compiled output to a file.
*
* @param javaSource The Java source object containing source code and meta-data
* @param outputFile The target file to write the compiled class
* @param compileOption Options for the compilation process
* @throws Exception Any exception that occurs during the compilation process
*/
@ExtensionPoint(code = EXT_COMPILE_CODE, reducePolicy = ReducePolicy.FISRT_MATCH)
void compile(JavaSource javaSource, File outputFile, CompileOption compileOption) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@
import java.io.OutputStream;

/**
* Interface for converting a FlowStreamSource to a model and vice versa.
* Provides methods to convert a flow stream source into a model object and
* to generate a stream representation of the model.
* @param <T> The type of the converted model
* @author yusu
*/
public interface FlowModelConverter<T> {

/**
* Converts a FlowStreamSource into a model object.
*
* @param flowStreamSource The input flow stream source
* @return The converted model object
*/
T convertToModel(FlowStreamSource flowStreamSource);

/**
* Converts a model object into an OutputStream representing the model's data.
*
* @param model The input model object
* @return An OutputStream containing the serialized representation of the model
*/
OutputStream convertToStream(T model);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@
import com.alibaba.compileflow.engine.process.preruntime.generator.code.CodeTargetSupport;

/**
* Represents an interface for generating code based on a given target support.
* Implementations of this interface are responsible for generating code snippets
* that can be integrated into a larger code structure.
* @author wuxiang
* @author yusu
*/
public interface Generator {

/**
* Generates code using the provided code target support.
*
* @param codeTargetSupport The support object for generating code
*/
void generateCode(CodeTargetSupport codeTargetSupport);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,27 @@
import com.alibaba.compileflow.engine.process.preruntime.generator.Generator;

/**
* Interface defining a provider for generators associated with specific nodes.
* This provider manages the mapping between nodes and their corresponding code generation logic.
* @author yusu
*/
public interface NodeGeneratorProvider {

/**
* Retrieves the generator responsible for generating code for the given node.
*
* @param node The node for which a generator is needed
* @return The generator instance capable of handling the given node
*/
Generator getGenerator(Node node);

/**
* Registers a generator to be used for the specified node.
*
* @param node The node to associate with the generator
* @param generator The generator to handle code generation for the node
*/
void registerGenerator(Node node, Generator generator);

}
}
Loading

0 comments on commit f454dc7

Please sign in to comment.