/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */
package com.sun.hotspot.igv.graal.filters;

import com.sun.hotspot.igv.data.Properties;
import com.sun.hotspot.igv.filter.AbstractFilter;
import com.sun.hotspot.igv.graph.Connection;
import com.sun.hotspot.igv.graph.Diagram;
import com.sun.hotspot.igv.graph.Figure;
import com.sun.hotspot.igv.graph.InputSlot;
import java.util.HashSet;
import java.util.Set;

public class GraalCFGFilter extends AbstractFilter {
    
    @Override
    public String getName() {
        return "Graal CFG Filter";
    }

    @Override
    public void apply(Diagram d) {
        Set<Figure> figuresToRemove = new HashSet<>();
        Set<Connection> connectionsToRemove = new HashSet<>();
        for (Figure f : d.getFigures()) {
            final String prop = f.getProperties().get("probability");
            if (prop == null) {
                figuresToRemove.add(f);
            }
        }
        d.removeAllFigures(figuresToRemove);
        
        for (Figure f : d.getFigures()) {
            Properties p = f.getProperties();
            int predCount;
            String predCountString = p.get("predecessorCount");
            if (predCountString != null) {
                predCount = Integer.parseInt(predCountString);
            } else if (Boolean.parseBoolean(p.get("hasPredecessor"))) {
                predCount = 1;
            } else {
                predCount = 0;
            }
            for (InputSlot is : f.getInputSlots()) {
                if (is.getPosition() >= predCount && !"EndNode".equals(is.getProperties().get("class"))) {
                    for (Connection c : is.getConnections()) {
                        if (!"EndNode".equals(c.getOutputSlot().getFigure().getProperties().get("class"))) {
                            connectionsToRemove.add(c);
                        }
                    }
                }
            }
        }
        
        for (Connection c : connectionsToRemove) {
            c.remove();
            System.out.println("rm " + c);
        }
    }
}

@@ -1357,16 +1357,61 @@
 utSlot;%0A
+import com.sun.hotspot.igv.graph.OutputSlot;%0A
 import j
@@ -1428,16 +1428,16 @@
 ashSet;%0A
-
 import j
@@ -1504,21 +1504,17 @@
 ilter %7B%0A
-    
 %0A
+
     @Ove
@@ -1644,63 +1644,8 @@
 ) %7B%0A
-        Set%3CFigure%3E figuresToRemove = new HashSet%3C%3E();%0A
     
@@ -1707,270 +1707,9 @@
 ();%0A
-        for (Figure f : d.getFigures()) %7B%0A            final String prop = f.getProperties().get(%22probability%22);%0A            if (prop == null) %7B%0A                figuresToRemove.add(f);%0A            %7D%0A        %7D%0A        d.removeAllFigures(figuresToRemove);%0A        
 %0A
+
     
@@ -2640,17 +2640,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -2724,52 +2724,554 @@
     
-    System.out.println(%22rm %22 + c);%0A        %7D
+%7D%0A%0A        Set%3CFigure%3E figuresToRemove = new HashSet%3C%3E();%0A        next: for (Figure f : d.getFigures()) %7B%0A            for (InputSlot is : f.getInputSlots()) %7B%0A                if (!is.getConnections().isEmpty()) %7B%0A                    continue next;%0A                %7D%0A            %7D%0A            for (OutputSlot os : f.getOutputSlots()) %7B%0A                if (!os.getConnections().isEmpty()) %7B%0A                    continue next;%0A                %7D%0A            %7D%0A            figuresToRemove.add(f);%0A        %7D%0A        d.removeAllFigures(figuresToRemove);
 %0A   

repair Graal CFG only filter