1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package optkl.util;
 26 
 27 import optkl.jdot.ui.JDot;
 28 import java.util.Collections;
 29 import java.util.HashMap;
 30 import java.util.LinkedHashMap;
 31 import java.util.LinkedHashSet;
 32 import java.util.LinkedList;
 33 import java.util.List;
 34 import java.util.Map;
 35 import java.util.Queue;
 36 import java.util.Set;
 37 import java.util.function.Consumer;
 38 import java.util.function.Function;
 39 
 40 public abstract class Dag<N> extends Interner<N> {
 41     public final Map<N, Set<N>> fromTo = new LinkedHashMap<>();
 42     public final Map<N, Set<N>> toFrom = new LinkedHashMap<>();
 43 
 44     public void add(N from, N to, Consumer<N> ifAbsent) {
 45         toFrom.computeIfAbsent(to,_->new LinkedHashSet<>()).add(from);
 46         fromTo.computeIfAbsent(from,_->new LinkedHashSet<>()).add(to);
 47         if (add(from)){
 48             ifAbsent.accept(from);
 49         }
 50         if (add(to)){
 51             ifAbsent.accept(to);
 52         }
 53     }
 54 
 55     public void add(N from, N to) {
 56         add(from,to,_->{});
 57     }
 58     public final List<N> rankOrdered = new LinkedList<>();
 59 
 60     public void view(String name, Function<N, String> dotNodeLabelRenderer) {
 61         JDot.digraph(name, $ ->
 62                 fromTo.forEach((l, r) ->
 63                         r.forEach(e ->
 64                                 $.edge(dotNodeLabelRenderer.apply(l), dotNodeLabelRenderer.apply(e))
 65                         )
 66                 ));
 67     }
 68 
 69     public boolean isDag() {
 70         return !fromTo.isEmpty();
 71     }
 72 
 73     public void closeRanks() {
 74         if (fromTo.isEmpty()){
 75             rankOrdered.addAll(interned.values());
 76         }else {
 77             Map<N, Integer> outDegree = new HashMap<>();
 78             fromTo.keySet().forEach(parent -> {
 79                 outDegree.put(parent, fromTo.get(parent).size());
 80                 fromTo.get(parent).forEach(child ->
 81                         outDegree.putIfAbsent(child, 0)
 82                 );
 83             });
 84 
 85             Queue<N> queue = new LinkedList<>(outDegree.entrySet().stream()
 86                     .filter(e -> e.getValue() == 0)
 87                     .map(Map.Entry::getKey)
 88                     .toList()
 89             );
 90 
 91             while (queue.poll() instanceof N current) { // basically a null check
 92                 rankOrdered.add(current);
 93                 toFrom.getOrDefault(current, Collections.emptySet())
 94                         .forEach(parent -> {
 95                             int remainingChildren = outDegree.get(parent) - 1;
 96                             outDegree.put(parent, remainingChildren);
 97                             if (remainingChildren == 0) {
 98                                 queue.add(parent);
 99                             }
100                         });
101             }
102         }
103     }
104 }