static List < Fragment > nodeToPlanFragments ( Node node , Map < NodeId , Node > nodes , boolean visited ) { List < Fragment > subplan = new LinkedList <> ( ) ; if ( ! visited ) { node . getFragmentsWithoutDependency ( ) . stream ( ) . min ( Comparator . comparingDouble ( Fragment :: fragmentCost ) ) . ifPresent ( firstNodeFragment -> { subplan . add ( firstNodeFragment ) ; node . getFragmentsWithoutDependency ( ) . remove ( firstNodeFragment ) ; } ) ; } node . getFragmentsWithoutDependency ( ) . addAll ( node . getFragmentsWithDependencyVisited ( ) ) ; subplan . addAll ( node . getFragmentsWithoutDependency ( ) . stream ( ) . sorted ( Comparator . comparingDouble ( Fragment :: fragmentCost ) ) . collect ( Collectors . toList ( ) ) ) ; node . getFragmentsWithoutDependency ( ) . clear ( ) ; node . getFragmentsWithDependencyVisited ( ) . clear ( ) ; // telling their dependants that they have been visited node . getDependants ( ) . forEach ( fragment -> { Node otherNode = nodes . get ( NodeId . of ( NodeId . NodeType . VAR , fragment . start ( ) ) ) ; if ( node . equals ( otherNode ) ) { otherNode = nodes . get ( NodeId . of ( NodeId . NodeType . VAR , fragment . dependencies ( ) . iterator ( ) . next ( ) ) ) ; } otherNode . getDependants ( ) . remove ( fragment . getInverse ( ) ) ; otherNode . getFragmentsWithDependencyVisited ( ) . add ( fragment ) ; } ) ; node . getDependants ( ) . clear ( ) ; return subplan ; }
convert a Node to a sub - plan updating dependants dependency map