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 
 29 import java.util.Collections;
 30 import java.util.HashMap;
 31 import java.util.LinkedHashMap;
 32 import java.util.LinkedHashSet;
 33 import java.util.LinkedList;
 34 import java.util.List;
 35 import java.util.Map;
 36 import java.util.Queue;
 37 import java.util.Set;
 38 import java.util.function.Consumer;
 39 import java.util.function.Function;
 40 
 41 public abstract class Dag<N> extends Interner<N> {
 42     public final Map<N, Set<N>> fromTo = new LinkedHashMap<>();
 43     public final Map<N, Set<N>> toFrom = new LinkedHashMap<>();
 44 
 45     public void add(N from, N to, Consumer<N> ifAbsent) {
 46         toFrom.computeIfAbsent(to, _ -> new LinkedHashSet<>()).add(from);
 47         fromTo.computeIfAbsent(from, _ -> new LinkedHashSet<>()).add(to);
 48         if (add(from)) {
 49             ifAbsent.accept(from);
 50         }
 51         if (add(to)) {
 52             ifAbsent.accept(to);
 53         }
 54     }
 55 
 56     public void add(N from, N to) {
 57         add(from, to, _ -> {});
 58     }
 59 
 60     public final List<N> rankOrdered = new LinkedList<>();
 61 
 62     public void view(String name, Function<N, String> dotNodeLabelRenderer) {
 63         JDot.digraph(name, $ ->
 64                 fromTo.forEach((l, r) ->
 65                         r.forEach(e ->
 66                                 $.edge(dotNodeLabelRenderer.apply(l), dotNodeLabelRenderer.apply(e))
 67                         )
 68                 ));
 69     }
 70 
 71     public boolean isDag() {
 72         return !fromTo.isEmpty();
 73     }
 74 
 75     public void closeRanks() {
 76         if (fromTo.isEmpty()) {
 77             rankOrdered.addAll(interned.values());
 78         } else {
 79             Map<N, Integer> outDegree = new HashMap<>();
 80             fromTo.keySet().forEach(parent -> {
 81                 outDegree.put(parent, fromTo.get(parent).size());
 82                 fromTo.get(parent).forEach(child ->
 83                         outDegree.putIfAbsent(child, 0)
 84                 );
 85             });
 86 
 87             Queue<N> queue = new LinkedList<>(outDegree.entrySet().stream()
 88                     .filter(e -> e.getValue() == 0)
 89                     .map(Map.Entry::getKey)
 90                     .toList()
 91             );
 92 
 93             while (queue.poll() instanceof N current) { // basically a null check
 94                 rankOrdered.add(current);
 95                 toFrom.getOrDefault(current, Collections.emptySet())
 96                         .forEach(parent -> {
 97                             int remainingChildren = outDegree.get(parent) - 1;
 98                             outDegree.put(parent, remainingChildren);
 99                             if (remainingChildren == 0) {
100                                 queue.add(parent);
101                             }
102                         });
103             }
104         }
105     }
106 }