Commit c6e4892e authored by SAINT-MARCEL Frederic's avatar SAINT-MARCEL Frederic
Browse files

add class

parent b3915a4b
/*
* Created by IntelliJ IDEA.
* User: Administrateur
* Date: 14 juin 2002
* Time: 16:01:30
* To change template for new class use
* Code Style | Class Templates options (Tools | IDE Options).
*/
package fr.fluxmedia.flowcomposer;
import com.jgraph.JGraph;
import com.jgraph.graph.*;
import java.awt.Point;
import java.util.*;
import javax.swing.JOptionPane;
public class FCGraphTools {
public CostFunction createDefaultCostFunction() {
return new DefaultCostFunction();
}
//
// Component Counting
//
public int getComponentCount(ProcessGraph graph) {
GraphModel m = graph.getModel();
UnionFind uf = new UnionFind();
Object[] all = graph.getAll();
// Vertices
Object[] v = graph.getVertices(all);
for (int i = 0; i < v.length; i++)
uf.find(v[i]);
// Edges
Object[] e = graph.getEdges(all);
for (int i = 0; i < e.length; i++) {
Object source = graph.getSourceVertex(e[i]);
Object target = graph.getTargetVertex(e[i]);
uf.union(uf.find(source), uf.find(target));
}
return uf.getSetCount();
}
//
// Dijkstra Algorithm
//
// FIX: Debug needed. Clicking twice in JGraphpad helps.
public Object[] getShortestPath(ProcessGraph graph, Object from,
Object to, CostFunction cf)
{
if (cf == null)
cf = createDefaultCostFunction();
GraphModel model = graph.getModel();
PriorityQueue q = new PriorityQueue();
Hashtable pred = new Hashtable();
q.setPrio(from, 0);
// Main Loop
Object[] all = graph.getAll();
int jmax = graph.getVertices(all).length;
for (int j = 0; j < jmax; j++) {
double prio = q.getPrio();
Object obj = q.pop();
Object[] tmp = new Object[]{obj};
Object[] e = DefaultGraphModel.getEdges(model, tmp).toArray();
if (e != null) {
for (int i = 0; i < e.length; i++) {
Object neighbour = graph.getNeighbour(e[i], obj);
double newPrio = prio + cf.getCost(graph, e[i]);
if (neighbour != null && newPrio < q.getPrio(neighbour)) {
pred.put(neighbour, e[i]);
q.setPrio(neighbour, newPrio);
}
}
}
}
// Return Path-Array
ArrayList list = new ArrayList();
Object obj = to;
while (obj != null) {
list.add(obj);
Object edge = pred.get(obj);
if (edge != null) {
list.add(edge);
obj = graph.getNeighbour(edge, obj);
} else
obj = null;
}
return list.toArray();
}
//
// Kruskal Algorithm
//
public Object[] getSpanningTree(ProcessGraph graph, CostFunction cf) {
if (cf == null)
cf = createDefaultCostFunction();
Object[] all = graph.getAll();
SortedSet edges = sort(graph, graph.getEdges(all), cf);
UnionFind uf = new UnionFind();
HashSet result = new HashSet();
while (!edges.isEmpty()) {
Object edge = edges.first();
edges.remove(edge);
Object setA, setB;
setA = uf.find(graph.getSourceVertex(edge));
setB = uf.find(graph.getTargetVertex(edge));
if (setA == null || setB == null || setA != setB) {
uf.union(setA, setB);
result.add(edge);
}
}
// Create set of vertices
HashSet v = new HashSet();
Iterator it = result.iterator();
while (it.hasNext()) {
Object edge = it.next();
Object source = graph.getSourceVertex(edge);
Object target = graph.getTargetVertex(edge);
if (source != null)
v.add(source);
if (target != null)
v.add(target);
}
Object[] cells = new Object[result.size()+v.size()];
System.arraycopy(result.toArray(), 0, cells, 0, result.size());
System.arraycopy(v.toArray(), 0, cells, result.size(), v.size());
return cells;
}
//
// Sorting With CostFunction
//
public SortedSet sort(final JGraph graph, Object[] cells, final CostFunction cf) {
TreeSet set = new TreeSet(new Comparator() {
public int compare(Object o1, Object o2) {
Double d1 = new Double(cf.getCost(graph, o1));
Double d2 = new Double(cf.getCost(graph, o2));
return d1.compareTo(d2);
}
});
for (int i = 0; i < cells.length; i++)
set.add(cells[i]);
return set;
}
//
// Cost Function
//
public interface CostFunction {
public double getCost(JGraph graph, Object cell);
}
public class DefaultCostFunction implements CostFunction {
public double getCost(JGraph graph, Object cell) {
CellView view = graph.getView().getMapping(cell, false);
return getLength(view);
}
}
public static double getLength(CellView view) {
double cost = 1;
if (view instanceof EdgeView) {
EdgeView edge = (EdgeView) view;
Point last = null, current = null;
for (int i = 0; i < edge.getPointCount(); i++) {
current = edge.getPoint(i);
if (last != null)
cost += last.distance(current);
last = current;
}
}
return cost;
}
//
// Union-Find
//
public class UnionFind {
protected Hashtable sets = new Hashtable(),
cells = new Hashtable();
/* Return the number of distinct sets. */
public int getSetCount() {
return sets.size();
}
/* Return an object identifying the set that contains the given cell. */
public Object find(Object cell) {
Object set = null;
if (cell != null) {
set = cells.get(cell);
if (set == null) {
set = cell;
cells.put(cell, set);
HashSet contents = new HashSet();
contents.add(cell);
sets.put(set, contents);
}
}
return set;
}
/* Union the given sets such that all elements belong to the same set. */
public Object union(Object set1, Object set2) {
if (set1 != null && set2 != null && set1 != set2) {
HashSet tmp1 = (HashSet) sets.get(set1);
HashSet tmp2 = (HashSet) sets.get(set2);
if (tmp1 != null && tmp2 != null) {
if (tmp1.size() < tmp2.size()) {
Object tmp = tmp1; tmp1 = tmp2; tmp2 = (HashSet) tmp;
tmp = set1; set1 = set2; set2 = tmp;
}
tmp1.addAll(tmp2);
sets.remove(set2);
Iterator it = tmp2.iterator();
while (it.hasNext())
cells.put(it.next(), set1);
}
}
return set1;
}
}
//
// Priority Queue
//
public class PriorityQueue implements Comparator {
protected Hashtable prio = new Hashtable();
protected TreeSet data = new TreeSet(this);
// Removes element but holds prio
public Object pop() {
if (data.isEmpty())
return null;
Object first = data.first();
data.remove(first);
return first;
}
/* Return the priority of the top element. */
public double getPrio() {
if (data.isEmpty())
return getPrio(null);
else
return getPrio(data.first());
}
/* Return the priority of the given element. */
public double getPrio(Object obj) {
if (obj != null) {
Double d = (Double) prio.get(obj);
if (d != null)
return d.doubleValue();
}
return Double.MAX_VALUE;
}
/* Set the priority of the given element and add to queue if necessary. */
public void setPrio(Object obj, double prio) {
Double d = new Double(prio);
this.prio.put(obj, d);
if (data.contains(obj))
data.remove(obj);
data.add(obj);
}
// Comparator
public int compare(Object o1, Object o2) {
Double d1 = (Double) prio.get(o1);
Double d2 = (Double) prio.get(o2);
// INV: d1 != null && d2 != null
return d1.compareTo(d2);
}
// Comparator
public boolean equals(Object obj) {
return (obj == this);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment