CombineFilter.java revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 2008, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.filter;
25
26import com.sun.hotspot.igv.graph.Connection;
27import com.sun.hotspot.igv.graph.Diagram;
28import com.sun.hotspot.igv.graph.Figure;
29import com.sun.hotspot.igv.graph.InputSlot;
30import com.sun.hotspot.igv.graph.OutputSlot;
31import com.sun.hotspot.igv.data.Properties;
32import com.sun.hotspot.igv.data.Properties.PropertyMatcher;
33import java.util.ArrayList;
34import java.util.HashSet;
35import java.util.List;
36import java.util.Set;
37
38/**
39 *
40 * @author Thomas Wuerthinger
41 */
42public class CombineFilter extends AbstractFilter {
43
44    private List<CombineRule> rules;
45    private String name;
46
47    public CombineFilter(String name) {
48        this.name = name;
49        rules = new ArrayList<CombineRule>();
50    }
51
52    public String getName() {
53        return name;
54    }
55
56    public void apply(Diagram diagram) {
57
58        Properties.PropertySelector<Figure> selector = new Properties.PropertySelector<Figure>(diagram.getFigures());
59        for (CombineRule r : rules) {
60
61            List<Figure> list = selector.selectMultiple(r.getFirstMatcher());
62            Set<Figure> figuresToRemove = new HashSet<Figure>();
63            for (Figure f : list) {
64
65                List<Figure> successors = new ArrayList<Figure>(f.getSuccessors());
66                if (r.isReversed()) {
67                    if (successors.size() == 1) {
68                        Figure succ = successors.get(0);
69                        InputSlot slot = null;
70
71                        for (InputSlot s : succ.getInputSlots()) {
72                            for (Connection c : s.getConnections()) {
73                                if (c.getOutputSlot().getFigure() == f) {
74                                    slot = s;
75                                }
76                            }
77                        }
78
79                        assert slot != null;
80                        slot.setName(f.getProperties().get("dump_spec"));
81                        if (f.getProperties().get("short_name") != null) {
82                            slot.setShortName(f.getProperties().get("short_name"));
83                        } else {
84                            String s = f.getProperties().get("dump_spec");
85                            if (s != null && s.length() <= 5) {
86                                slot.setShortName(s);
87                            }
88
89                        }
90
91                        for (InputSlot s : f.getInputSlots()) {
92                            for (Connection c : s.getConnections()) {
93                                Connection newConn = diagram.createConnection(slot, c.getOutputSlot());
94                                newConn.setColor(c.getColor());
95                                newConn.setStyle(c.getStyle());
96                            }
97                        }
98
99                        figuresToRemove.add(f);
100                    }
101                } else {
102
103                    for (Figure succ : successors) {
104                        if (succ.getPredecessors().size() == 1) {
105                            if (succ.getProperties().selectSingle(r.getSecondMatcher()) != null && succ.getOutputSlots().size() == 1) {
106
107
108                                OutputSlot oldSlot = null;
109                                for (OutputSlot s : f.getOutputSlots()) {
110                                    for (Connection c : s.getConnections()) {
111                                        if (c.getInputSlot().getFigure() == succ) {
112                                            oldSlot = s;
113                                        }
114                                    }
115                                }
116
117                                assert oldSlot != null;
118
119                                OutputSlot nextSlot = succ.getOutputSlots().get(0);
120                                int pos = 0;
121                                if (succ.getProperties().get("con") != null) {
122                                    pos = Integer.parseInt(succ.getProperties().get("con"));
123                                }
124                                OutputSlot slot = f.createOutputSlot(pos);
125                                slot.setName(succ.getProperties().get("dump_spec"));
126                                if (succ.getProperties().get("short_name") != null) {
127                                    slot.setShortName(succ.getProperties().get("short_name"));
128                                } else {
129                                    String s = succ.getProperties().get("dump_spec");
130                                    if (s != null && s.length() <= 2) {
131                                        slot.setShortName(s);
132                                    } else {
133                                        String tmpName = succ.getProperties().get("name");
134                                        if (tmpName != null && tmpName.length() > 0) {
135                                            slot.setShortName(tmpName.substring(0, 1));
136                                        }
137                                    }
138                                }
139                                for (Connection c : nextSlot.getConnections()) {
140                                    Connection newConn = diagram.createConnection(c.getInputSlot(), slot);
141                                    newConn.setColor(c.getColor());
142                                    newConn.setStyle(c.getStyle());
143                                }
144
145
146                                figuresToRemove.add(succ);
147
148                                if (oldSlot.getConnections().size() == 0) {
149                                    f.removeSlot(oldSlot);
150                                }
151                            }
152                        }
153                    }
154                }
155            }
156
157            diagram.removeAllFigures(figuresToRemove);
158        }
159    }
160
161    public void addRule(CombineRule combineRule) {
162        rules.add(combineRule);
163    }
164
165    public static class CombineRule {
166
167        private PropertyMatcher first;
168        private PropertyMatcher second;
169        private boolean reversed;
170
171        public CombineRule(PropertyMatcher first, PropertyMatcher second) {
172            this(first, second, false);
173
174        }
175
176        public CombineRule(PropertyMatcher first, PropertyMatcher second, boolean reversed) {
177            this.first = first;
178            this.second = second;
179            this.reversed = reversed;
180        }
181
182        public boolean isReversed() {
183            return reversed;
184        }
185
186        public PropertyMatcher getFirstMatcher() {
187            return first;
188        }
189
190        public PropertyMatcher getSecondMatcher() {
191            return second;
192        }
193    }
194}
195