io.convergence_platform.common.dag.WorkflowDAG Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.common.dag;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WorkflowDAG {
@JsonProperty("steps")
public List steps = null;
@JsonProperty("dependencies")
public List dependencies = null;
public WorkflowStepInfo getStep(String id, String name) {
WorkflowStepInfo result = new WorkflowStepInfo();
for (WorkflowStepInfo step : steps) {
if (step.id.equals(id) && step.name.equals(name)) {
result = step;
break;
}
}
return result;
}
public static WorkflowDAG from(List steps) {
WorkflowDAG result = new WorkflowDAG();
result.steps = new ArrayList<>();
result.dependencies = new ArrayList<>();
Map map = new HashMap<>();
for (WorkflowStepInfo step : steps) {
result.steps.add(step);
// TODO: Add : and \n to invalid patterns in client
String key = String.format("%s:%s", step.id, step.name);
map.put(key, step);
}
for (WorkflowStepInfo step : steps) {
if (step.dependencies == null) continue;
for (WorkflowStepInfo dep : step.dependencies) {
WorkflowStepDependency dependency = new WorkflowStepDependency();
dependency.step = WorkflowStepNameID.from(step);
dependency.dependency = WorkflowStepNameID.from(dep);
result.dependencies.add(dependency);
}
}
return result;
}
public static WorkflowDAG fromJson(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
WorkflowDAG result = mapper.readerFor(WorkflowDAG.class).readValue(json);
Map map = new HashMap<>();
for (WorkflowStepInfo step : result.steps) {
String key = String.format("%s:%s", step.id, step.name);
map.put(key, step);
}
for (WorkflowStepDependency dependency : result.dependencies) {
WorkflowStepInfo step = map.get(String.format("%s:%s", dependency.step.id, dependency.step.name));
WorkflowStepInfo dep = map.get(String.format("%s:%s", dependency.dependency.id, dependency.dependency.name));
if (step.dependencies == null) {
step.dependencies = new ArrayList<>();
}
step.dependencies.add(dep);
}
return result;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@JsonIgnore
public List getSortedList() {
if (steps == null) {
int h = 0;
}
DirectedAcyclicGraph dag = new DirectedAcyclicGraph<>(steps);
return dag.topologicalSort();
}
}