## 4 Algorithm Description
### Second Stage: Simplifying the Model  
In this stage, the goal is to simplify the outcome from the previous stage to obtain a simpler version. As the reader can note from Figure 8b, the workflow produced in the first stage can include unneeded BPMN elements and, therefore, it can be simplified to conduct more efficiently further operations on it. This is because the first stage eliminates the activities creating connections between previous and upcoming elements.  
Figure 9 shows the general architecture for the second stage. As the reader can see, different elements must work together to conduct the simplification of the model. The pattern repository uses a \(Bpmn\) class containing general utility functions to perform operations over the BPMN model. The Patterns Engine, in the centre of the figure, can use the patterns included in the repository to offer a high level API that is used to perform the simplification.  
An excerpt of the Java implementation for this engine and the API deployed is shown in the Listing 4. The piece of code includes some essential methods that will be used for the second stage algorithm. The engine maintains a list of the patterns available in the repository (check line 3). This list is dynamically populated in the constructor method of the engine. It allows looping over all the patterns simplifying each one in the model. The engine also offers methods to simplify of the next pattern in the list (lines 20-31), to check if the list has been entirely explored (lines 33-38), and to rewind the pattern iterator (lines 40-42).  
```
1 publicclassPatternsEngine
2{
3 ArrayList<SimplifiablePattern>patterns;
4 intcurrent;
5
6//Engineconstructor
7 PatternsEngine(){
8//Loadimicallyallpatternsinrepository
9//Exceptionhandlingomited
10 Reflectionsr=newReflections(\"repository.package\");
11 Set<Class<?extendsSimplifiablePattern>repo=
12r.getSubTypesOf(SimplifiablePattern.class);
13
14 patterns=newArrayList<>();
15 for(Class<?extendsSimplifiablePattern>pat:repo){
16 patterns.add(pat.newInstance());
17}
18}
19
20//SimplifynextpatternintheBPMNmodel
21 publicBpmnsimplifyNext(Bpmnmodel){
22 SimplifiablePatternsPattern=patterns.get(current);
23 if(this.hasNext()){
24 currentPattern++;
25}
26 BpmnIdstartElmnt=aPPattern.searchPattern(model);
27 if(startElmnt!=null){
28 model=aPattern.reducePattern(model,startElmnt);
29}
21 returnmodel;
22
23}
24 publicbooleanhasNext(){
25 if(currentPattern<patterns.size())
26 returntrue;
27 else
28 returnfalse;
29}
30 publicvoidrewindPatterns(){
31 currentPattern=0;
32}
33}
```  
Listing 4: Patterns Engine Java code.  
In this stage, the algorithm, using the API offered by the \(PatternsEngine\), looks for existing sub-models in the input model that matches any of the existing ones in the repository. Upon each matching, the corresponding transformation is conducted. This process keeps on going until no more changes on the model are possible. The process of searching and reducing a pattern is performed until no more reductions can be evaluated over the model.  
Finally, as an example, in Figure 10 the reader can see the outcome of the second stage on the model from Figure 8.