Skip to content

Commit

Permalink
minors fixes
Browse files Browse the repository at this point in the history
- add unit test
- resolve some issues in loop
  • Loading branch information
NourEldin-Ali committed Jul 31, 2024
1 parent 50fcfc2 commit 706c884
Show file tree
Hide file tree
Showing 65 changed files with 9,380 additions and 3,050 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,14 @@ private void addXorGateway(BPMNElementNode targetElement) throws BPMNModelExcept
// TODO: validate
String gatewayType;
String gatewayNum;
if (splitGateway == null) {
// if (splitGateway == null) {
gatewayType = BPMNTypes.EXCLUSIVE_GATEWAY;
gatewayNum = "-3";
} else {
gatewayType = splitGateway.getType();
gatewayNum = splitGateway.getAttribute(GATEWAY_NUM);
}
// }
// else {
// gatewayType = splitGateway.getType();
// gatewayNum = splitGateway.getAttribute(GATEWAY_NUM);
// }

// add gateway before activity
Gateway newGateway = process.addGateway("gt-" + gatewayId.toString(), "", gatewayType);
Expand Down Expand Up @@ -816,7 +817,7 @@ private boolean addNewTarget(SequenceFlow selectedSequence, Gateway selectedGate

} else if (acceptedSequenceFlows.size() >= 1) {
isAdded = true;
System.out.print("Add new gateway after existing gateway (" + selectedGateway.getId() + "): gt-"
System.out.println("Add new gateway after existing gateway (" + selectedGateway.getId() + "): gt-"
+ gatewayId.toString());
String gatewayType = probablyRelationType.get(GATEWAY_TYPE);
// TODO: general gateway
Expand Down Expand Up @@ -1399,7 +1400,7 @@ private void addLoopGateway(List<BPMNElementNode> sourceElements, List<BPMNEleme
}

if (sourceElement == null) {
System.out.println("Error in detect loop targets relation");
System.out.println("Error in detect loop sources relation");
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public LinkedList<LinkedList<String>> getDecisions() {
// get all source of activity
Set<DefaultWeightedEdge> incomingEdgeActivity = dependencyGraph.incomingEdgesOf(activity);
Set<String> sourcesActivity = new HashSet<>();
incomingEdgeActivity.stream().forEach(edge -> sourcesActivity.add(dependencyGraph.getEdgeSource(edge)));
incomingEdgeActivity.forEach(edge -> sourcesActivity.add(dependencyGraph.getEdgeSource(edge)));
for (String sourceElement : sourcesActivity) {
if (sortedBySource.containsKey(sourceElement)) {
if (!sortedBySource.get(sourceElement).containsKey(activity)) {
sortedBySource.get(sourceElement).putIfAbsent(activity, new HashSet<String>());
}
} else {
sortedBySource.putIfAbsent(sourceElement, new HashMap());
sortedBySource.putIfAbsent(sourceElement, new HashMap<>());
}
}
}
Expand All @@ -53,7 +53,7 @@ public LinkedList<LinkedList<String>> getDecisions() {
}
}

Set<Set<String>> finalDecisionList = new HashSet();
Set<Set<String>> finalDecisionList = new HashSet<>();

// get most frequent element
for (Map.Entry<String, Map<String, Set<String>>> source : sortedBySource.entrySet()) {
Expand All @@ -72,7 +72,7 @@ public LinkedList<LinkedList<String>> getDecisions() {
}
}
}
if (maxFreqElements.size() > 0) {
if (!maxFreqElements.isEmpty()) {
maxFreqElements.add(frequentElement);
finalDecisionList.add(maxFreqElements);
source.getValue().remove(frequentElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,28 @@ public void findAndRemoveLoops() {

}
}
// //if there is a loop in the graph that not removed like a->b, b->a, and a is connected to other activity and b is connected to other activity so should remove both loops
// for (DefaultWeightedEdge edge : tempGraph.edgeSet()) {
// String source = tempGraph.getEdgeSource(edge);
// String target = tempGraph.getEdgeTarget(edge);
//
// DefaultWeightedEdge reverseEdge = tempGraph.getEdge(target, source);
// if (reverseEdge != null) {
// // Add to loops
// loops.add(new ArrayList<>(Arrays.asList(source, target)));
//
// // Remove marked edges=
// tempGraph.removeEdge(edge);
// tempGraph.removeEdge(reverseEdge);
// }
// }



}



// remove loops from dependency graph
dependencyGraph = (DirectedWeightedPseudograph<String, DefaultWeightedEdge>) dependencyGraphWithLoop.clone();
loops.stream().forEach(edge -> {
Expand Down Expand Up @@ -667,9 +687,50 @@ public static boolean haveSamePredecessors(DirectedWeightedPseudograph<String, D
// Get predecessor lists for both vertices
var predecessorsV1 = Graphs.predecessorListOf(graph, vertex1);
var predecessorsV2 = Graphs.predecessorListOf(graph, vertex2);

System.out.println("haveSamePredecessors: "+vertex1 + " and " + vertex2);
System.out.println("Predecessors of "+vertex1+": "+predecessorsV1);
System.out.println("Predecessors of "+vertex2+": "+predecessorsV2);
System.out.println("******");
// Check if the predecessor lists are equal
return predecessorsV1.containsAll(predecessorsV2) && predecessorsV2.containsAll(predecessorsV1);

}

public static boolean haveIntersectSuccessors(DirectedWeightedPseudograph<String, DefaultWeightedEdge> graph, String vertex1, String vertex2) {
// Get successor lists for both vertices
var successorsV1 = Graphs.successorListOf(graph, vertex1);
var successorsV2 = Graphs.successorListOf(graph, vertex2);

// Check if the lists are equal
boolean areEqual = successorsV1.containsAll(successorsV2) && successorsV2.containsAll(successorsV1);

// Check if one list is a subset of the other
boolean isSubsetV1inV2 = successorsV2.containsAll(successorsV1);
boolean isSubsetV2inV1 = successorsV1.containsAll(successorsV2);

// Check if either the lists are equal or one is a subset of the other
boolean result = areEqual || isSubsetV1inV2 || isSubsetV2inV1;
return result;
}

public static boolean haveIntersectPredecessors(DirectedWeightedPseudograph<String, DefaultWeightedEdge> graph, String vertex1, String vertex2) {
// Get predecessor lists for both vertices
var predecessorsV1 = Graphs.predecessorListOf(graph, vertex1);
var predecessorsV2 = Graphs.predecessorListOf(graph, vertex2);
System.out.println("haveSamePredecessors: "+vertex1 + " and " + vertex2);
System.out.println("Predecessors of "+vertex1+": "+predecessorsV1);
System.out.println("Predecessors of "+vertex2+": "+predecessorsV2);
System.out.println("******");
// Check if the lists are equal
boolean areEqual = predecessorsV1.containsAll(predecessorsV2) && predecessorsV2.containsAll(predecessorsV1);

// Check if one list is a subset of the other
boolean isSubsetV1inV2 = predecessorsV2.containsAll(predecessorsV1);
boolean isSubsetV2inV1 = predecessorsV1.containsAll(predecessorsV2);

// Check if either the lists are equal or one is a subset of the other
boolean result = areEqual || isSubsetV1inV2 || isSubsetV2inV1;
return result;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jgrapht.graph.DirectedWeightedPseudograph;

import java.util.*;
import java.util.stream.Collectors;

public class LoopMerger {
private Set<List<String>> loops;
Expand All @@ -17,9 +18,12 @@ public LoopMerger(Set<List<String>> loops, DirectedWeightedPseudograph<String, D
}

public List<Pair<Set<String>, Set<String>>> getMergedLoop(){
System.out.println("before merge");
System.out.println(this.loops);
//merge loops by source
List<Pair<String, Set<String>>> mergetBySource = mergeLoopsBySource();
// System.out.println(mergetBySource);
System.out.println("after by source");
System.out.println(mergetBySource);
//merge loops by target
List<Pair<Set<String>, Set<String>>> mergetloop = mergeLoopsByTarget(mergetBySource);
// System.out.println(mergetloop);
Expand Down Expand Up @@ -55,7 +59,7 @@ public List<Pair<String, Set<String>>> mergeLoopsBySource() {
String otherSource = otherLoop.get(0);
String otherTarget = otherLoop.get(1);

if (source.contentEquals(otherSource) && DependencyGraph.haveSameSuccessors(dependencyGraph, target, otherTarget)) {
if (source.contentEquals(otherSource) && DependencyGraph.haveIntersectPredecessors(dependencyGraph, target, otherTarget)) {
targets.add(otherTarget);
it.remove(); // Remove the merged loop
}
Expand All @@ -81,7 +85,7 @@ public List<Pair<Set<String>,Set<String>>> mergeLoopsByTarget(List<Pair<String,
Pair<String, Set<String>> lPrime = it.next();
if (l == lPrime) continue; // Skip the same element

if (l.getTarget().equals(lPrime.getTarget()) && DependencyGraph.haveSamePredecessors(dependencyGraph,l.getSource(), lPrime.getSource())) {
if (l.getTarget().equals(lPrime.getTarget()) && DependencyGraph.haveIntersectSuccessors(dependencyGraph,l.getSource(), lPrime.getSource())) {
sources.add(lPrime.getSource());
it.remove(); // Remove l' from mergedLoopBySource
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ public LinkedList<LinkedList<String>> getParallelims() {
// Converting set back to list
LinkedList<LinkedList<String>> result = new LinkedList<>();
Set<Set<String>> mergedPara = mergeParallelism();
System.out.println(mergedPara);
// TODO: check why there exists a missing data?
// check if any missing data, and add
for (Set<String> check : parallelism) {
if (!mergedPara.containsAll(check)) {
boolean shouldAdd = true;
for (Set<String> megrgedParallelism : mergedPara) {
if (megrgedParallelism.containsAll(check)) {
shouldAdd = false;
break;

}
}
if (shouldAdd) {
mergedPara.add(check);
}
}
Expand Down Expand Up @@ -52,7 +63,7 @@ public Set<Set<String>> mergeParallelism() {
Set<String> sourceActivity = new HashSet<>();
incomingEdgeActivity.stream().forEach(edge -> sourceActivity.add(dependencyGraph.getEdgeSource(edge)));
sourceActivity.stream().forEach(source -> {
sortedBySource.putIfAbsent(source, new HashSet());
sortedBySource.putIfAbsent(source, new HashSet<>());
});
}
}
Expand All @@ -71,13 +82,13 @@ public Set<Set<String>> mergeParallelism() {
}
}
if (isAllSameSource) {
source.getValue().add(new HashSet(pair));
source.getValue().add(new HashSet<>(pair));
}
}
}
// Step 2 & 3: Calculate frequency and identify the highest frequency element
String frequentElement = "";
Set<Set<String>> maxFreqElements = new HashSet();
Set<Set<String>> maxFreqElements = new HashSet<>();
for (Map.Entry<String, Set<Set<String>>> entry : sortedBySource.entrySet()) {
if (frequentElement.isEmpty()) {
frequentElement = entry.getKey();
Expand All @@ -103,11 +114,13 @@ public Set<Set<String>> mergeParallelism() {
}
}
}

// System.out.println("print test sortedBySource: ");
// System.out.println(sortedBySource);
// union
Set<Set<String>> finalParalellList = new LinkedHashSet<>();
for (Map.Entry<String, Set<Set<String>>> source : sortedBySource.entrySet()) {
finalParalellList.addAll(unionSetsWithCommonElements(new ArrayList(source.getValue())));
// System.out.println(source.getKey());
finalParalellList.addAll(unionSetsWithCommonElements(new ArrayList<>(source.getValue())));
}
return finalParalellList;
}
Expand Down Expand Up @@ -147,6 +160,8 @@ public static List<Set<String>> unionSetsWithCommonElements(List<Set<String>> or

originalSets = newSets; // Update the list for the next iteration
} while (mergeOccurred);
// System.out.println("test unionSetsWithCommonElements: :");
// System.out.println(originalSets);
return originalSets;
}

Expand Down
Loading

0 comments on commit 706c884

Please sign in to comment.