@@ -23,6 +23,8 @@ public class ConcreteSolutionServiceImpl implements ConcreteSolutionService {
2323 private final ConcreteSolutionRepository concreteSolutionRepository ;
2424 private final DesignModelEdgeTypeRepository designModelEdgeTypeRepository ;
2525
26+ private UUID lastSourceNodeUUID = null ;
27+
2628
2729 public ConcreteSolutionServiceImpl (ConcreteSolutionRepository concreteSolutionRepository ,
2830 DesignModelEdgeTypeRepository designModelEdgeTypeRepository ) {
@@ -58,7 +60,7 @@ private void linkConcreteSolutionsToPatternInstances(List<DesignModelPatternInst
5860 }
5961
6062
61- private void swapEdgeDirections (List <DesignModelPatternEdge > edges ) {
63+ private void normalizeEdgeDirections (List <DesignModelPatternEdge > edges ) {
6264 Set <String > edgeTypesToSwapDirections = this .designModelEdgeTypeRepository .findAll ().stream ()
6365 .filter (DesignModelEdgeType ::getSwap )
6466 .map (DesignModelEdgeType ::getName )
@@ -74,72 +76,54 @@ private void swapEdgeDirections(List<DesignModelPatternEdge> edges) {
7476 }
7577
7678
77- private Set <UUID > findRootNodes (List <DesignModelPatternInstance > patternInstances , List <DesignModelPatternEdge > edges ) {
78- Set <UUID > rootNodes = new HashSet <>();
79+ private Set <UUID > findSourceNodes (List <DesignModelPatternInstance > patternInstances , List <DesignModelPatternEdge > edges ) {
80+ Set <UUID > sourceNodes = new HashSet <>();
7981
8082 for (DesignModelPatternInstance patternInstance : patternInstances ) {
81- rootNodes .add (patternInstance .getPatternInstanceId ());
83+ sourceNodes .add (patternInstance .getPatternInstanceId ());
8284 }
8385
8486 for (DesignModelPatternEdge edge : edges ) {
85- rootNodes .remove (edge .getPatternInstance1 ().getPatternInstanceId ());
87+ sourceNodes .remove (edge .getPatternInstance2 ().getPatternInstanceId ());
8688 }
8789
88- log .info ("Root nodes: " + rootNodes .toString ());
89- return rootNodes ;
90+ return sourceNodes ;
9091 }
9192
9293
93- private Set < UUID > findLeafNodes (List <DesignModelPatternInstance > patternInstances , List <DesignModelPatternEdge > edges ) {
94- Set <UUID > leafNodes = new HashSet <>( );
94+ private AggregationDataAndPatternEdge getSourceNodeAndNeighbor (List <DesignModelPatternInstance > patternInstances , List <DesignModelPatternEdge > edges ) {
95+ Set <UUID > sourceNodes = findSourceNodes ( patternInstances , edges );
9596
96- for ( DesignModelPatternInstance patternInstance : patternInstances ) {
97- leafNodes . add ( patternInstance . getPatternInstanceId () );
97+ if ( sourceNodes . isEmpty () ) {
98+ throw new RuntimeException ( "No source node found" );
9899 }
99100
100- for (DesignModelPatternEdge edge : edges ) {
101- leafNodes .remove (edge .getPatternInstance2 ().getPatternInstanceId ());
102- }
103-
104- return leafNodes ;
105- }
106-
101+ final UUID sourceNodeUUID = sourceNodes .contains (lastSourceNodeUUID ) ? lastSourceNodeUUID : sourceNodes .iterator ().next ();
102+ lastSourceNodeUUID = sourceNodeUUID ;
107103
108- private UUID lastLeafUUID = null ;
109-
110- private AggregationData getLeafAndPredecessor (List <DesignModelPatternInstance > patternInstances , List <DesignModelPatternEdge > edges ) {
111- Set <UUID > leafNodes = findLeafNodes (patternInstances , edges );
112-
113- if (leafNodes .isEmpty ()) {
114- throw new RuntimeException ("No leaf node found" );
115- }
116-
117- final UUID leafNodeUUID = leafNodes .contains (lastLeafUUID ) ? lastLeafUUID : leafNodes .iterator ().next ();
118- lastLeafUUID = leafNodeUUID ;
119-
120- DesignModelPatternInstance leafPatternInstance = patternInstances .stream ()
121- .filter (designModelPatternInstance -> leafNodeUUID .equals (designModelPatternInstance .getPatternInstanceId ()))
104+ DesignModelPatternInstance sourcePatternInstance = patternInstances .stream ()
105+ .filter (designModelPatternInstance -> sourceNodeUUID .equals (designModelPatternInstance .getPatternInstanceId ()))
122106 .findAny ().orElse (null );
123107
124108 if (patternInstances .size () == 1 ) {
125- return new AggregationData (leafPatternInstance , null , null );
109+ return new AggregationDataAndPatternEdge ( new AggregationData (sourcePatternInstance , null ) , null );
126110 }
127111
128- DesignModelPatternEdge edge = edges .stream ().filter (designModelPatternEdge -> leafNodeUUID .equals (designModelPatternEdge .getPatternInstance1 ().getPatternInstanceId ())).findAny ().orElse (null );
112+ DesignModelPatternEdge edge = edges .stream ().filter (designModelPatternEdge -> sourceNodeUUID .equals (designModelPatternEdge .getPatternInstance1 ().getPatternInstanceId ())).findAny ().orElse (null );
129113
130- DesignModelPatternInstance predecessorPatternInstance = null ;
114+ DesignModelPatternInstance neighborPatternInstance = null ;
131115
132116 if (edge != null ) {
133- UUID predecessorNodeUUID = edge .getPatternInstance2 ().getPatternInstanceId ();
117+ UUID neighborNodeUUID = edge .getPatternInstance2 ().getPatternInstanceId ();
134118
135- predecessorPatternInstance = patternInstances .stream ()
136- .filter (designModelPatternInstance -> predecessorNodeUUID .equals (designModelPatternInstance .getPatternInstanceId ()))
119+ neighborPatternInstance = patternInstances .stream ()
120+ .filter (designModelPatternInstance -> neighborNodeUUID .equals (designModelPatternInstance .getPatternInstanceId ()))
137121 .findAny ().orElse (null );
138122
139- log .info ("Found leaf [" + leafNodeUUID .toString () + "] and predecessor [" + predecessorNodeUUID .toString () + "]: " + leafPatternInstance .getPattern ().getName () + " ---" + edge .getType () + "--- " + predecessorPatternInstance .getPattern ().getName ());
123+ log .info ("Found source [" + sourceNodeUUID .toString () + "] and neighbor [" + neighborNodeUUID .toString () + "]: " + sourcePatternInstance .getPattern ().getName () + " ---" + edge .getType () + "--- " + neighborPatternInstance .getPattern ().getName ());
140124 }
141125
142- return new AggregationData (leafPatternInstance , predecessorPatternInstance , edge );
126+ return new AggregationDataAndPatternEdge ( new AggregationData (sourcePatternInstance , neighborPatternInstance ) , edge );
143127 }
144128
145129
@@ -170,13 +154,14 @@ public List<FileDTO> aggregate(List<DesignModelPatternInstance> patternInstances
170154
171155 linkConcreteSolutionsToPatternInstances (patternInstances , concreteSolutionMapping );
172156
173- swapEdgeDirections (edges );
157+ normalizeEdgeDirections (edges );
174158
175159 Map <String , Object > templateContext = new HashMap <>();
176- List <FileDTO > aggregatedFiles = new ArrayList <>();
160+ List <FileDTO > artefacts = new ArrayList <>();
177161
178162 while (!patternInstances .isEmpty ()) {
179- AggregationData aggregationData = getLeafAndPredecessor (patternInstances , edges );
163+ AggregationDataAndPatternEdge aggregationDataAndPatternEdge = getSourceNodeAndNeighbor (patternInstances , edges );
164+ AggregationData aggregationData = aggregationDataAndPatternEdge .getAggregationData ();
180165
181166 aggregationData .setTemplateContext (templateContext );
182167
@@ -185,16 +170,15 @@ public List<FileDTO> aggregate(List<DesignModelPatternInstance> patternInstances
185170 templateContext = aggregationData .getTemplateContext ();
186171
187172 if (aggregationData .getResult () != null ) {
188- aggregatedFiles .add (aggregationData .getResult ());
173+ artefacts .add (aggregationData .getResult ());
189174 }
190175
191- edges .remove (aggregationData .getEdge ());
176+ edges .remove (aggregationDataAndPatternEdge .getEdge ());
192177 if (!edges .stream ().anyMatch (edge -> aggregationData .getSource ().getPatternInstanceId ().equals (edge .getPatternInstance1 ().getPatternInstanceId ()))) {
193178 patternInstances .remove (aggregationData .getSource ());
194179 }
195180 }
196181
197-
198- return aggregatedFiles ;
182+ return artefacts ;
199183 }
200184}
0 commit comments