use of org.eclipse.titanium.graph.components.EdgeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class GraphHandler method changeLayout.
/**
* This function changes the layout for the graph set in the
* {@link GraphHandler} class
*
* @param newLayout
* : The chosen layout's code
* @param newWindowSize
* : The size of the parent window where to draw (or any
* resolution bigger than this)
* @throws BadLayoutException On wrong layout code or bad graph
*/
public void changeLayout(final LayoutEntry newLayout, final Dimension newWindowSize) throws BadLayoutException {
if (g == null) {
throw new BadLayoutException("You must draw a graph before!", ErrorType.NO_OBJECT);
}
Dimension extSize = null;
if (g.getVertexCount() >= 20) {
extSize = new Dimension(newWindowSize.height * (g.getVertexCount() / 20), newWindowSize.width * (g.getVertexCount() / 20));
} else {
extSize = newWindowSize;
}
layout = new LayoutBuilder(g, newLayout, extSize).clusters(clusters).build();
actVisualisator = new CustomVisualizationViewer(layout, popupMenu);
actVisualisator.setPreferredSize(new Dimension(newWindowSize.width, newWindowSize.height));
actVisualisator.getRenderContext().setVertexLabelTransformer(NODE_LABELER);
final GraphRenderer<NodeDescriptor, EdgeDescriptor> rnd = new GraphRenderer<NodeDescriptor, EdgeDescriptor>(NODE_LABELER, actVisualisator.getPickedVertexState(), actVisualisator.getPickedEdgeState());
setNodeRenderer(rnd, actVisualisator);
renderer = rnd;
actVisualisator.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
actVisualisator.setBackground(Color.white);
actVisualisator.setDoubleBuffered(false);
initSatView();
}
use of org.eclipse.titanium.graph.components.EdgeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class GraphHandler method saveToImage.
/**
* Exports the graph set for this class to a PNG file
*
* @param path
* : The PNG file's path
* @param mode
* : The way of export, see {@link GraphHandler}
* <code>public static</code> fields for possible values (EXPORT_
* named fields)
* @param size
* : This parameter sets the size of the exported image in pixels
* @throws Exception
* on file handling error
*/
public void saveToImage(final String path, final ImageExportType mode) throws BadLayoutException {
if (layout == null || actVisualisator == null) {
throw new BadLayoutException("Either the layout or the visuaizer is not set (is null)", ErrorType.NO_OBJECT);
}
VisualizationViewer<NodeDescriptor, EdgeDescriptor> tempVisualisator = null;
Dimension size = null;
switch(mode) {
case EXPORT_SEEN_GRAPH:
{
tempVisualisator = actVisualisator;
size = actVisualisator.getPreferredSize();
}
break;
case EXPORT_WHOLE_GRAPH:
{
layout = actVisualisator.getGraphLayout();
if (size == null) {
size = new Dimension(layout.getSize().width, layout.getSize().height);
}
final Function<NodeDescriptor, Point2D> trf = new Function<NodeDescriptor, Point2D>() {
@Override
public Point2D apply(final NodeDescriptor v) {
return layout.apply(v);
}
};
tempVisualisator = new VisualizationViewer<NodeDescriptor, EdgeDescriptor>(new LayoutBuilder(g, Layouts.LAYOUT_STATIC, size).transformer(trf).build());
tempVisualisator.setPreferredSize(size);
tempVisualisator.setSize(size);
tempVisualisator.getRenderContext().setVertexLabelTransformer(NODE_LABELER);
final GraphRenderer<NodeDescriptor, EdgeDescriptor> rnd = new GraphRenderer<NodeDescriptor, EdgeDescriptor>(NODE_LABELER, tempVisualisator.getPickedVertexState(), tempVisualisator.getPickedEdgeState());
setNodeRenderer(rnd, tempVisualisator);
tempVisualisator.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
tempVisualisator.setBackground(Color.white);
tempVisualisator.setDoubleBuffered(false);
}
break;
case EXPORT_SATELLITE:
{
tempVisualisator = satView;
size = tempVisualisator.getSize();
}
break;
default:
ErrorReporter.logError("Unexpected image export type " + mode);
return;
}
BufferedImage image;
final GUIErrorHandler errorHandler = new GUIErrorHandler();
try {
image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
} catch (OutOfMemoryError e) {
final long needed = (long) size.width * (long) size.height * 4;
String temp;
if (needed < 1024) {
temp = needed + " bytes";
} else if (needed < 1024 * 1024) {
temp = needed / 1024 + " Kbytes";
} else {
temp = needed / 1024 / 1024 + " Mbytes";
}
final String errorText = "Could not save an image of " + size.width + "*" + size.height + " size as there was not enough free memory (" + temp + ")";
errorHandler.reportErrorMessage(errorText);
ErrorReporter.logExceptionStackTrace(errorText, e);
return;
}
final Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
tempVisualisator.paint(g2);
g2.dispose();
try {
ImageIO.write(image, "png", new File(path));
} catch (IOException e) {
final String message = "Error while writing to file" + path;
ErrorReporter.logExceptionStackTrace(message, e);
errorHandler.reportException(message, e);
}
}
use of org.eclipse.titanium.graph.components.EdgeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class LayoutBuilder method build.
/**
* This method implements the building of the chosen layout with chosen parameters
* @return The built layout
* @throws BadLayoutException If the chosen layout doesn't exist (invalid code was provided)
*/
public Layout<NodeDescriptor, EdgeDescriptor> build() throws BadLayoutException {
Layout<NodeDescriptor, EdgeDescriptor> layout;
final String layoutCode = layoutEntry.getCode();
if (layoutCode.equals(Layouts.LAYOUT_ISOM.getCode())) {
layout = new TitaniumISOMLayout<NodeDescriptor, EdgeDescriptor>(g);
((TitaniumISOMLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
} else if (layoutCode.equals(Layouts.LAYOUT_KK.getCode())) {
layout = new KKLayout<NodeDescriptor, EdgeDescriptor>(g);
((KKLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
} else if (layoutCode.equals(Layouts.LAYOUT_FR.getCode())) {
layout = new FRLayout<NodeDescriptor, EdgeDescriptor>(g);
((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setAttractionMultiplier(0.6);
((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setRepulsionMultiplier(0.8);
((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
} else if (layoutCode.equals(Layouts.LAYOUT_SPRING.getCode())) {
layout = new SpringLayout<NodeDescriptor, EdgeDescriptor>(g);
} else if (layoutCode.equals(Layouts.LAYOUT_CIRCLE.getCode())) {
layout = new CircleLayout<NodeDescriptor, EdgeDescriptor>(g);
} else if (layoutCode.equals(Layouts.LAYOUT_RTDAG.getCode())) {
layout = new ReverseDAGLayout<NodeDescriptor, EdgeDescriptor>(g, size);
} else if (layoutCode.equals(Layouts.LAYOUT_TDAG.getCode())) {
layout = new TitaniumDAGLayout<NodeDescriptor, EdgeDescriptor>(g, size);
} else if (layoutCode.equals(Layouts.METRIC_LAYOUT_CODE)) {
if (!(layoutEntry instanceof MetricsLayoutEntry)) {
throw new IllegalStateException("A metric must be chosen before using metric layout!");
}
layout = new MetricLayout<EdgeDescriptor>(g, size, ((MetricsLayoutEntry) layoutEntry).getMetric());
} else if (layoutCode.equals(Layouts.S_LAYOUT_CLUSTER)) {
if (clusters == null) {
throw new IllegalStateException("A clustering must be set before using cluster layout!");
}
final ClusterTransformer trf = new ClusterTransformer(new FRLayout<NodeDescriptor, EdgeDescriptor>(g), clusters, size);
layout = new StaticLayout<NodeDescriptor, EdgeDescriptor>(g, trf);
} else if ("STATIC".equals(layoutCode)) {
if (pointTransformer == null) {
throw new IllegalStateException("A point transformer must be set before using static layout!");
}
layout = new StaticLayout<NodeDescriptor, EdgeDescriptor>(g, pointTransformer);
} else {
throw new BadLayoutException("There is no such layout! (Layout=" + layoutCode + ")", ErrorType.NOT_EXISITING_LAYOUT);
}
layout.setSize(size);
return layout;
}
use of org.eclipse.titanium.graph.components.EdgeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class AutomaticCluster method init.
/**
* Initializes the clustering algorithm.
*/
private void init() {
mapBetweenArcs = new HashMap<Integer, Map<Integer, Integer>>();
indices = new HashSet<Integer>();
mapIndexCluster = new HashMap<Integer, Set<NodeDescriptor>>();
mapClusterIndex = new HashMap<NodeDescriptor, Integer>();
size = new HashMap<Integer, Integer>();
index = 0;
clusternum = 0;
// remove unneeded clusters, create indices for the clusters
final Set<Set<NodeDescriptor>> clustersToRemove = new HashSet<Set<NodeDescriptor>>();
for (final Set<NodeDescriptor> cluster : clustersToCheck) {
if (cluster == null || cluster.isEmpty()) {
clustersToRemove.add(cluster);
} else {
indices.add(index);
mapIndexCluster.put(index, cluster);
size.put(index, cluster.size());
for (final NodeDescriptor v : cluster) {
mapClusterIndex.put(v, index);
}
index++;
clusternum++;
}
}
for (final Set<NodeDescriptor> cluster : clustersToRemove) {
clustersToCheck.remove(cluster);
}
// create matrix of components
for (final int i : indices) {
final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (final int j : indices) {
map.put(j, 0);
}
mapBetweenArcs.put(i, map);
}
for (final EdgeDescriptor e : moduleGraph.getEdges()) {
final NodeDescriptor v = moduleGraph.getSource(e);
final NodeDescriptor w = moduleGraph.getDest(e);
final int i = mapClusterIndex.get(v);
final int j = mapClusterIndex.get(w);
changeCell(i, j, 1);
}
mq = MQ();
}
use of org.eclipse.titanium.graph.components.EdgeDescriptor in project titan.EclipsePlug-ins by eclipse.
the class ClusteringTools method generateClusterGraph.
/**
* It creates the cluster graph from a module graph and the clusters with
* their names.
*
* @param moduleGraph
* The module graph
* @param mapNameCluster
* The clusters mapped to their names
* @return The cluster graph
*/
public static DirectedSparseGraph<NodeDescriptor, EdgeDescriptor> generateClusterGraph(final DirectedSparseGraph<NodeDescriptor, EdgeDescriptor> moduleGraph, final Map<String, Set<NodeDescriptor>> mapNameCluster) {
final Map<Set<NodeDescriptor>, ClusterNode> mapClusterNode = new HashMap<Set<NodeDescriptor>, ClusterNode>();
final DirectedSparseGraph<NodeDescriptor, EdgeDescriptor> clusterGraph = new DirectedSparseGraph<NodeDescriptor, EdgeDescriptor>();
for (final Entry<String, Set<NodeDescriptor>> entry : mapNameCluster.entrySet()) {
final String name = entry.getKey();
final Set<NodeDescriptor> cluster = entry.getValue();
for (final NodeDescriptor v : cluster) {
v.setCluster(cluster);
}
final ClusterNode clusternode = new ClusterNode(name, cluster);
clusterGraph.addVertex(clusternode);
mapClusterNode.put(cluster, clusternode);
}
for (final EdgeDescriptor e : moduleGraph.getEdges()) {
final NodeDescriptor v = moduleGraph.getSource(e);
final NodeDescriptor w = moduleGraph.getDest(e);
final Set<NodeDescriptor> clusterv = v.getCluster();
final Set<NodeDescriptor> clusterw = w.getCluster();
if (clusterv == null || clusterw == null) {
continue;
}
if (clusterv != clusterw) {
final ClusterNode clusterNodev = mapClusterNode.get(clusterv);
final ClusterNode clusterNodew = mapClusterNode.get(clusterw);
if (clusterNodev == null || clusterNodew == null) {
continue;
}
EdgeDescriptor ce = clusterGraph.findEdge(clusterNodev, clusterNodew);
if (ce != null) {
ce.setWeight((Integer) ce.getWeight() + 1);
} else {
ce = new ClusterEdge(clusterNodev.getName() + "-" + clusterNodew.getName(), 1);
clusterGraph.addEdge(ce, clusterNodev, clusterNodew);
}
}
}
return clusterGraph;
}
Aggregations