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 2233118
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 68 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,7 @@

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.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 @@ -259,21 +260,17 @@ private Map<ProcessElement, List<AnalysisPath>> getPathHaveNoIntersection(
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());
internalPaths.entrySet().forEach(it -> {
SequenceFlow sequenceFlow = processGraph.getFirstIncoming(it.getKey().getElement());
result.put(sequenceFlow, it.getValue());
});
return result;

taskGroup.setInternalPaths(result);
return taskGroup;
}

private List<AnalysisPath> getAnalysisPathFromIntersection(Map<ProcessElement, List<AnalysisPath>> source,
Expand All @@ -292,20 +289,6 @@ private List<AnalysisPath> getAnalysisPathFromIntersection(Map<ProcessElement, L
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);
}
});

return result;
}

private Map<ProcessElement, List<AnalysisPath>> getPathBeforeIntersection(
Map<ProcessElement, List<AnalysisPath>> paths, ProcessElement intersection) {
Map<ProcessElement, List<AnalysisPath>> pathBeforeIntersection = new LinkedHashMap<>();
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,11 @@
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;

import java.util.ArrayList;
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 +49,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 +71,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 +86,83 @@ 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;
}
}

0 comments on commit 2233118

Please sign in to comment.