@@ -125,35 +125,33 @@ public BavetConstraintSession<Score_> buildSession(Solution_ workingSolution, Co
125125 buildNodeNetwork (workingSolution , constraintStreamSet , scoreInliner , nodeNetworkVisualizationConsumer ));
126126 }
127127
128- @ SuppressWarnings ("unchecked" )
129128 private static <Solution_ , Score_ extends Score <Score_ >> NodeNetwork buildNodeNetwork (Solution_ workingSolution ,
130129 Set <BavetAbstractConstraintStream <Solution_ >> constraintStreamSet , AbstractScoreInliner <Score_ > scoreInliner ,
131130 Consumer <String > nodeNetworkVisualizationConsumer ) {
132- /*
133- * Build constraintStreamSet in reverse order to create downstream nodes first
134- * so every node only has final variables (some of which have downstream node method references).
135- */
136131 var buildHelper = new NodeBuildHelper <>(constraintStreamSet , scoreInliner );
137- var nodeList = buildNodeList (constraintStreamSet , buildHelper );
132+ var declaredClassToNodeMap = new LinkedHashMap <Class <?>, List <AbstractForEachUniNode <?>>>();
133+ var nodeList = buildNodeList (constraintStreamSet , buildHelper , node -> {
134+ if (!(node instanceof AbstractForEachUniNode <?> forEachUniNode )) {
135+ return ;
136+ }
137+ var forEachClass = forEachUniNode .getForEachClass ();
138+ var forEachUniNodeList =
139+ declaredClassToNodeMap .computeIfAbsent (forEachClass , k -> new ArrayList <>(2 ));
140+ if (forEachUniNodeList .size () == 2 ) {
141+ // Each class can have at most two forEach nodes: one including null vars, the other excluding them.
142+ throw new IllegalStateException (
143+ "Impossible state: For class (%s) there are already 2 nodes (%s), not adding another (%s)."
144+ .formatted (forEachClass , forEachUniNodeList , forEachUniNode ));
145+ }
146+ forEachUniNodeList .add (forEachUniNode );
147+ });
138148 if (nodeNetworkVisualizationConsumer != null ) {
139- var visualisation = visualizeNodeNetwork (workingSolution , buildHelper , scoreInliner , nodeList );
149+ var constraintSet = scoreInliner .getConstraints ();
150+ var visualisation = NodeGraph .of (workingSolution , nodeList , constraintSet , buildHelper ::getNodeCreatingStream ,
151+ buildHelper ::findParentNode )
152+ .buildGraphvizDOT ();
140153 nodeNetworkVisualizationConsumer .accept (visualisation );
141154 }
142- var declaredClassToNodeMap = new LinkedHashMap <Class <?>, List <AbstractForEachUniNode <Object >>>();
143- for (var node : nodeList ) {
144- if (node instanceof AbstractForEachUniNode <?> forEachUniNode ) {
145- var forEachClass = forEachUniNode .getForEachClass ();
146- var forEachUniNodeList =
147- declaredClassToNodeMap .computeIfAbsent (forEachClass , k -> new ArrayList <>());
148- if (forEachUniNodeList .size () == 2 ) {
149- // Each class can have at most two forEach nodes: one including null vars, the other excluding them.
150- throw new IllegalStateException ("Impossible state: For class (" + forEachClass
151- + ") there are already 2 nodes (" + forEachUniNodeList + "), not adding another ("
152- + forEachUniNode + ")." );
153- }
154- forEachUniNodeList .add ((AbstractForEachUniNode <Object >) forEachUniNode );
155- }
156- }
157155 var layerMap = new TreeMap <Long , List <Propagator >>();
158156 for (var node : nodeList ) {
159157 layerMap .computeIfAbsent (node .getLayerIndex (), k -> new ArrayList <>())
@@ -169,7 +167,8 @@ private static <Solution_, Score_ extends Score<Score_>> NodeNetwork buildNodeNe
169167 }
170168
171169 private static <Solution_ , Score_ extends Score <Score_ >> List <AbstractNode > buildNodeList (
172- Set <BavetAbstractConstraintStream <Solution_ >> constraintStreamSet , NodeBuildHelper <Score_ > buildHelper ) {
170+ Set <BavetAbstractConstraintStream <Solution_ >> constraintStreamSet , NodeBuildHelper <Score_ > buildHelper ,
171+ Consumer <AbstractNode > nodeProcessor ) {
173172 /*
174173 * Build constraintStreamSet in reverse order to create downstream nodes first
175174 * so every node only has final variables (some of which have downstream node method references).
@@ -188,16 +187,11 @@ private static <Solution_, Score_ extends Score<Score_>> List<AbstractNode> buil
188187 */
189188 node .setId (nextNodeId ++);
190189 node .setLayerIndex (determineLayerIndex (node , buildHelper ));
190+ nodeProcessor .accept (node );
191191 }
192192 return nodeList ;
193193 }
194194
195- public static <Solution_ , Score_ extends Score <Score_ >> String visualizeNodeNetwork (Solution_ solution ,
196- NodeBuildHelper <Score_ > buildHelper , AbstractScoreInliner <Score_ > scoreInliner , List <AbstractNode > nodeList ) {
197- return NodeGraph .of (solution , buildHelper , nodeList , scoreInliner )
198- .buildGraphvizDOT ();
199- }
200-
201195 /**
202196 * Nodes are propagated in layers.
203197 * See {@link PropagationQueue} and {@link AbstractNode} for details.
0 commit comments