Skip to content

Commit

Permalink
TE-614: Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
trungmaihova committed Jul 1, 2024
1 parent d934244 commit b73c934
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void shouldFindAllTasks() throws Exception {
var start = ProcessGraphHelper.findByElementName(process, "start");
var detectedTasks = processInspector.findAllTasks(start, UseCase.BIGPROJECT);

var expected = Arrays.array("TaskA", "SubA-TaskA", "SubA-TaskC", "SubA-TaskB", "SubD-TaskB", "SubB-TaskA", "SubD-TaskC", "SubC-TaskA", "SubD-TaskB", "SubD-TaskA");
var expected = Arrays.array("TaskA", "SubA-TaskA", "SubA-TaskC", "SubA-TaskB", "SubD-TaskB", "SubB-TaskA", "SubD-TaskC", "SubC-TaskA", "SubD-TaskD", "SubD-TaskA");
var taskNames = getTaskNames(detectedTasks);
assertArrayEquals(expected, taskNames);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static com.axonivy.utils.process.inspector.internal.helper.AnalysisPathHelper.addAllToPath;
import static com.axonivy.utils.process.inspector.internal.helper.AnalysisPathHelper.addToPath;
import static com.axonivy.utils.process.inspector.internal.helper.AnalysisPathHelper.getAnalysisPathFrom;
import static com.axonivy.utils.process.inspector.internal.helper.AnalysisPathHelper.getAnalysisPathTo;
import static com.axonivy.utils.process.inspector.internal.helper.AnalysisPathHelper.getPathByStartElements;
import static com.axonivy.utils.process.inspector.internal.helper.AnalysisPathHelper.replaceFirstElement;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -167,8 +170,7 @@ private ProcessElement findIntersectionTaskSwitchGateway(Map<ProcessElement, Lis

private Map<ProcessElement, List<AnalysisPath>> mergePath(Map<ProcessElement, List<AnalysisPath>> source,
ProcessElement intersection, List<AnalysisPath> subPath) {
Map<ProcessElement, List<AnalysisPath>> pathBeforeIntersection = getPathBeforeIntersection(source,
intersection);
Map<ProcessElement, List<AnalysisPath>> pathBeforeIntersection = getAnalysisPathTo(source, intersection);

Map<ProcessElement, List<AnalysisPath>> pathNotIntersection = source.entrySet().stream()
.filter(entry -> !pathBeforeIntersection.keySet().contains(entry.getKey())).collect(toMap(
Expand Down Expand Up @@ -234,14 +236,14 @@ private List<AnalysisPath> convertToAnalysisPaths(Map<ProcessElement, List<Analy
ProcessElement intersection = intersectionEntry.getKey();
Set<ProcessElement> startElements = intersectionEntry.getValue();

var paths = getPathByStartElements(source, startElements);
Map<ProcessElement, List<AnalysisPath>> paths = getPathByStartElements(source, startElements);

var pathBeforeIntersection = getPathBeforeIntersection(paths, intersection);
Map<ProcessElement, List<AnalysisPath>> pathBeforeIntersection = getAnalysisPathTo(paths, intersection);

// Call recursion inside convertToTaskParallelGroup
TaskParallelGroup taskGroup = convertToTaskParallelGroup(pathBeforeIntersection);

List<AnalysisPath> subPathAfterIntersection = getAnalysisPathFromIntersection(paths, intersection);
List<AnalysisPath> subPathAfterIntersection = getAnalysisPathFrom(paths, intersection);

result.addAll(addToPath(List.of(new AnalysisPath(List.of(taskGroup))), subPathAfterIntersection));
}
Expand All @@ -250,87 +252,31 @@ private List<AnalysisPath> convertToAnalysisPaths(Map<ProcessElement, List<Analy
}

private Map<ProcessElement, List<AnalysisPath>> getPathHaveNoIntersection(
Map<ProcessElement, List<AnalysisPath>> source, Map<ProcessElement, Set<ProcessElement>> intersections) {
List<ProcessElement> startElementWithIntersection = intersections.values().stream().flatMap(Collection::stream)
Map<ProcessElement, List<AnalysisPath>> source,
Map<ProcessElement, Set<ProcessElement>> intersections) {

List<ProcessElement> startElementWithIntersection = intersections.values().stream()
.flatMap(Collection::stream)
.toList();

return source.entrySet().stream().filter(entry -> !startElementWithIntersection.contains(entry.getKey()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue,
LinkedHashMap::new));
return source.entrySet().stream()
.filter(entry -> !startElementWithIntersection.contains(entry.getKey()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
}

private TaskParallelGroup convertToTaskParallelGroupWithInternalPath(
Map<ProcessElement, List<AnalysisPath>> internalPaths) {
private TaskParallelGroup convertToTaskParallelGroupWithInternalPath(Map<ProcessElement, List<AnalysisPath>> internalPaths) {
TaskParallelGroup taskGroup = new TaskParallelGroup(null);
taskGroup.setInternalPaths(convertToInternalPathForTaskParallelGroup(internalPaths));
return taskGroup;
}

private Map<SequenceFlow, List<AnalysisPath>> convertToInternalPathForTaskParallelGroup(
Map<ProcessElement, List<AnalysisPath>> internalPath) {

Map<SequenceFlow, List<AnalysisPath>> result = new LinkedHashMap<>();
internalPath.entrySet().forEach(it -> {
NodeElement element = (NodeElement) it.getKey().getElement();
result.put(element.getIncoming().get(0), it.getValue());
});
return result;
}

private List<AnalysisPath> getAnalysisPathFromIntersection(Map<ProcessElement, List<AnalysisPath>> source,
ProcessElement intersection) {
Set<AnalysisPath> result = new HashSet<>();
List<AnalysisPath> paths = source.values().stream().flatMap(Collection::stream).toList();
for (AnalysisPath path : paths) {
List<ProcessElement> elements = path.getElements();
int index = elements.indexOf(intersection);
if (index >= 0) {
List<ProcessElement> beforeIntersection = elements.subList(index, elements.size() - 1);
result.add(new AnalysisPath(beforeIntersection));
}
}

return new ArrayList<>(result);
}

private Map<ProcessElement, List<AnalysisPath>> getPathByStartElements(
Map<ProcessElement, List<AnalysisPath>> source, Set<ProcessElement> elements) {
Map<ProcessElement, List<AnalysisPath>> result = new LinkedHashMap<>();

elements.forEach(key -> {
List<AnalysisPath> paths = source.get(key);
if (isNotEmpty(paths)) {
result.put(key, paths);
}
internalPaths.entrySet().forEach(it -> {
SequenceFlow sequenceFlow = processGraph.getFirstIncoming(it.getKey().getElement());
result.put(sequenceFlow, it.getValue());
});

return result;
}

private Map<ProcessElement, List<AnalysisPath>> getPathBeforeIntersection(
Map<ProcessElement, List<AnalysisPath>> paths, ProcessElement intersection) {
Map<ProcessElement, List<AnalysisPath>> pathBeforeIntersection = new LinkedHashMap<>();
for (Entry<ProcessElement, List<AnalysisPath>> entry : paths.entrySet()) {
List<AnalysisPath> beforeIntersections = getAnalysisPathBeforeIntersection(entry.getValue(), intersection);
if (isNotEmpty(beforeIntersections)) {
pathBeforeIntersection.put(entry.getKey(), beforeIntersections);
}
}

return pathBeforeIntersection;

taskGroup.setInternalPaths(result);
return taskGroup;
}

private List<AnalysisPath> getAnalysisPathBeforeIntersection(List<AnalysisPath> paths,
ProcessElement intersection) {
List<AnalysisPath> result = new ArrayList<>();
for (AnalysisPath path : paths) {
int index = path.getElements().indexOf(intersection);
if (index >= 0) {
List<ProcessElement> beforeIntersection = path.getElements().subList(0, index);
result.add(new AnalysisPath(beforeIntersection));
}
}
return result;
}

private Map<ProcessElement, Set<ProcessElement>> getLastIntersectionByStartElements(Map<ProcessElement, List<AnalysisPath>> paths) {
var intersections = getAllIntersectionTaskSwitchGatewayWithStartElement(paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ public String getAlternativeNameId(BaseElement alternative) {
.collect(Collectors.joining("-"));
}

public SequenceFlow getFirstIncoming(BaseElement element) {
if(element instanceof NodeElement) {
return ((NodeElement) element).getIncoming().stream().findFirst().orElse(null);
}
return null;
}

private boolean containPrefixs(String content, String... prefix) {
return List.of(prefix).stream().allMatch(it -> content.contains(it));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected List<DetectedElement> convertToDetectedElements(Map<ProcessElement, Li
timeUntilStarts);
result.addAll(elements);
}
//result = keepMaxTimeUtilEndDetectedElement(result);
result = keepMaxTimeUtilEndDetectedElement(result);
return result;
}

Expand Down Expand Up @@ -185,8 +185,8 @@ private List<DetectedElement> convertPathToDetectedElements(ProcessElement start

// It is EmbeddedProcessElement
if (element instanceof SubProcessGroup) {
List<AnalysisPath> internalPaths = ((SubProcessGroup) element).getInternalPaths();
List<AnalysisPath> subPaths = getAnalysisPathBaseOnNextSequenceFlow(internalPaths, nextElement.getElement());
List<AnalysisPath> subPaths = ((SubProcessGroup) element).getInternalPaths();
//List<AnalysisPath> subPaths = getAnalysisPathBaseOnNextSequenceFlow(internalPaths, nextElement.getElement());
List<DetectedElement> allTaskFromSubPath = new ArrayList<>();
for(AnalysisPath subPath : subPaths) {
ProcessElement startSubElement = subPath.getElements().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections4.ListUtils;

Expand Down Expand Up @@ -47,7 +51,8 @@ public static List<AnalysisPath> addAllToPath(List<AnalysisPath> paths, List<Pro
return result;
}

public static List<AnalysisPath> addAllToPath(List<AnalysisPath> paths, Map<SequenceFlow, List<AnalysisPath>> pathOptions) {
public static List<AnalysisPath> addAllToPath(List<AnalysisPath> paths,
Map<SequenceFlow, List<AnalysisPath>> pathOptions) {
List<AnalysisPath> result = new ArrayList<>();
if (pathOptions.isEmpty()) {
result.addAll(paths);
Expand All @@ -68,7 +73,6 @@ public static List<AnalysisPath> addAllToPath(List<AnalysisPath> paths, Map<Sequ
}
return result;
}


public static List<AnalysisPath> replaceFirstElement(ProcessElement element, List<AnalysisPath> subPaths) {
if (subPaths.isEmpty()) {
Expand All @@ -84,82 +88,124 @@ public static List<AnalysisPath> replaceFirstElement(ProcessElement element, Lis

return result;
}

public static <T> int getLastIndex(AnalysisPath path) {
List<ProcessElement> elements = path.getElements();
return elements.size() == 0 ? 0 : elements.size() - 1;
}

public static ProcessElement getLastElement(AnalysisPath path) {
List<ProcessElement> elements = path.getElements();
int size = elements.size();
return size == 0 ? null : elements.get(size - 1);
}

public static NodeElement getFirstNodeElement(List<AnalysisPath> paths) {
NodeElement startNode = AnalysisPathHelper.getAllProcessElement(paths).stream()
.map(ProcessElement::getElement)
.filter(NodeElement.class::isInstance)
.findFirst()
.map(NodeElement.class::cast)
.orElse(null);
NodeElement startNode = AnalysisPathHelper.getAllProcessElement(paths).stream().map(ProcessElement::getElement)
.filter(NodeElement.class::isInstance).findFirst().map(NodeElement.class::cast).orElse(null);
return startNode;
}
public static List<AnalysisPath> removeLastElementByClassType(List<AnalysisPath> paths , Class<?> clazz) {

public static List<AnalysisPath> removeLastElementByClassType(List<AnalysisPath> paths, Class<?> clazz) {

List<AnalysisPath> result = new ArrayList<>();
for (AnalysisPath path : paths) {
int lastIndex = AnalysisPathHelper.getLastIndex(path);
List<ProcessElement> pathElements = new ArrayList<>(path.getElements());
ProcessElement lastElement = pathElements.get(lastIndex);

if (lastElement instanceof CommonElement && clazz.isInstance(lastElement.getElement())) {
pathElements.remove(lastIndex);
}

result.add(new AnalysisPath(pathElements));
}

return result;
}

public static List<ProcessElement> getAllProcessElement(List<AnalysisPath> paths) {
List<ProcessElement> elements = paths.stream()
.map(AnalysisPath::getElements)
.flatMap(List::stream)
.flatMap(it -> getAllProcessElement(it).stream())
.toList();
List<ProcessElement> elements = paths.stream().map(AnalysisPath::getElements).flatMap(List::stream)
.flatMap(it -> getAllProcessElement(it).stream()).toList();
return elements;
}

public static List<ProcessElement> getAllProcessElement(ProcessElement element) {
if(element instanceof CommonElement) {
if (element instanceof CommonElement) {
return List.of(element);
}
if(element instanceof TaskParallelGroup) {

if (element instanceof TaskParallelGroup) {
List<ProcessElement> result = new ArrayList<>();

TaskParallelGroup group = (TaskParallelGroup) element;
if(group.getElement() != null) {
if (group.getElement() != null) {
result.add(new CommonElement(group.getElement()));
}

for(Entry<SequenceFlow, List<AnalysisPath>> entry : group.getInternalPaths().entrySet()) {
List<ProcessElement> allProcessElement = entry.getValue().stream()
.map(AnalysisPath::getElements)
.flatMap(List::stream)
.flatMap(it -> getAllProcessElement(it).stream())
.toList();

result.add(new CommonElement(entry.getKey()));

for (Entry<SequenceFlow, List<AnalysisPath>> entry : group.getInternalPaths().entrySet()) {
List<ProcessElement> allProcessElement = entry.getValue().stream().map(AnalysisPath::getElements)
.flatMap(List::stream).flatMap(it -> getAllProcessElement(it).stream()).toList();

result.add(new CommonElement(entry.getKey()));
result.addAll(allProcessElement);
}

return result;
}

return emptyList();
}

public static <K, V extends List<AnalysisPath>> Map<K, V> getPathByStartElements(Map<K, V> source, Set<K> keys) {
Map<K, V> result = new LinkedHashMap<>();
keys.forEach(key -> {
V paths = source.get(key);
if (isNotEmpty(paths)) {
result.put(key, paths);
}
});
return result;
}

public static List<AnalysisPath> getAnalysisPathFrom(Map<ProcessElement, List<AnalysisPath>> source, ProcessElement from) {
Set<AnalysisPath> result = new HashSet<>();
List<AnalysisPath> paths = source.values().stream().flatMap(Collection::stream).toList();

for (AnalysisPath path : paths) {
List<ProcessElement> elements = path.getElements();
int index = elements.indexOf(from);
if (index >= 0) {
List<ProcessElement> afterFrom = elements.subList(index, elements.size() - 1);
result.add(new AnalysisPath(afterFrom));
}
}

return new ArrayList<>(result);

}

public static Map<ProcessElement, List<AnalysisPath>> getAnalysisPathTo(Map<ProcessElement, List<AnalysisPath>> source, ProcessElement to) {
Map<ProcessElement, List<AnalysisPath>> pathBeforeIntersection = new LinkedHashMap<>();
for (Entry<ProcessElement, List<AnalysisPath>> entry : source.entrySet()) {
List<AnalysisPath> beforeTo = getAnalysisPathTo(entry.getValue(), to);
if (isNotEmpty(beforeTo)) {
pathBeforeIntersection.put(entry.getKey(), beforeTo);
}
}

return pathBeforeIntersection;
}

private static List<AnalysisPath> getAnalysisPathTo(List<AnalysisPath> source, ProcessElement to) {
List<AnalysisPath> result = new ArrayList<>();
for (AnalysisPath path : source) {
int index = path.getElements().indexOf(to);
if (index >= 0) {
List<ProcessElement> beforeIntersection = path.getElements().subList(0, index);
result.add(new AnalysisPath(beforeIntersection));
}
}
return result;
}
}

0 comments on commit b73c934

Please sign in to comment.