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

update

parent c6e4892e
......@@ -17,118 +17,53 @@ 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;
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;
}
//
......@@ -137,14 +72,14 @@ public class FCGraphTools {
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);
}
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]);
set.add(cells[i]);
return set;
}
......@@ -153,30 +88,28 @@ public class FCGraphTools {
//
public interface CostFunction {
public double getCost(JGraph graph, Object cell);
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);
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;
}
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;
}
......@@ -188,109 +121,49 @@ public class FCGraphTools {
public class UnionFind {
protected Hashtable sets = new Hashtable(),
cells = new Hashtable();
cells = new Hashtable();
/* Return the number of distinct sets. */
public int getSetCount() {
return sets.size();
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;
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;
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);
}
}
}
}
\ No newline at end of file
/**
* $Id: FCMarqueeHandler.java,v 1.34 2002-06-14 13:33:08 frederic Exp $
* $Id: FCMarqueeHandler.java,v 1.35 2002-06-14 15:19:21 frederic Exp $
*
* Transmorpher
*
......@@ -60,7 +60,7 @@ public class FCMarqueeHandler extends BasicMarqueeHandler {
protected FlowComposer flowcomposer;
protected PortView port, firstPort, lastPort;
protected FCGraphTools gt;
protected Call currentCall;
public transient JToggleButton marquee = new JToggleButton();
......@@ -84,7 +84,8 @@ public class FCMarqueeHandler extends BasicMarqueeHandler {
public FCMarqueeHandler(FlowComposer flowcomposer){
this.flowcomposer = flowcomposer;
this.flowcomposer = flowcomposer;
gt = new FCGraphTools();
}
public void setCurrentProcess(Object process){
......@@ -127,13 +128,13 @@ public class FCMarqueeHandler extends BasicMarqueeHandler {
public void mousePressed(MouseEvent e) {
//System.out.println("MousePressed Start...");
if(e.getButton() == MouseEvent.BUTTON3)
{
{
if (!e.isConsumed() && marquee.isSelected()) {
System.out.println("MousePressed Start...");
e.consume();
e.consume();
}
}
else{
......@@ -141,7 +142,7 @@ public class FCMarqueeHandler extends BasicMarqueeHandler {
if (!e.isConsumed() && !marquee.isSelected()) {
start = currentProcess.snap(e.getPoint());
firstPort = port;
if ((edgeNormal.isSelected())&&(firstPort != null)) {
double scale = currentProcess.getScale();
start = firstPort.getLocation(null);
......@@ -149,10 +150,10 @@ public class FCMarqueeHandler extends BasicMarqueeHandler {
}
e.consume();
}
}
}
super.mousePressed(e);
//System.out.println("MousePressed End...");
......@@ -343,6 +344,12 @@ public class FCMarqueeHandler extends BasicMarqueeHandler {
((Port)portOut.getUserObject()).setChannel(channel);
channel.setName(name);
((Process)currentTransformation).addChannel(channel);
// Object[] tmp = gt.getSpanningTree(currentProcess,null);
// currentProcess.setSelectionCells(tmp);
// Object[] all = currentProcess.getAll();
// if (tmp.length!=all.length)
// System.out.println("le graphe comporte un circuit");
}
......
/**
* $Id: ProcessGraph.java,v 1.33 2002-06-14 09:26:48 gchomat Exp $
* $Id: ProcessGraph.java,v 1.34 2002-06-14 15:19:21 frederic Exp $
*
* Transmorpher
*
......@@ -1101,9 +1101,9 @@ addVertex(call, position,FCConstants.MERGE);
Object[] cells = getAll();
file.Writeln(4,"<rdf:Description about=\""+((Process)userObject).getName()+"\">");
for (int i = 0; i < cells.length; i++){
if (cells[i] instanceof NormalEdge)
if (cells[i] instanceof DefaultEdge)
ChannelRDF(cells[i],file);
else
else if (!(cells[i] instanceof PortProcessCell)&&(!(cells[i] instanceof PortCell)))
CellRDF(cells[i],file);
}
file.Writeln(4,"</rdf:Description>");
......@@ -1128,14 +1128,14 @@ addVertex(call, position,FCConstants.MERGE);
}
public void ChannelRDF(Object cell,ReadWrite file) {
Object call = ((DefaultGraphCell)cell).getUserObject();
Object userObject = ((DefaultGraphCell)cell).getUserObject();
GraphView v = getView();
CellView cellview = v.getMapping(cell,false);
Map map = cellview.getAttributes();
//Color color = GraphConstants.getLineColor(map);
//System.out.println(color);
file.Writeln(8,"<hasChannel>");
file.Writeln(11,"<rdf:Description about=\""+call.toString()+"\">");
file.Writeln(11,"<rdf:Description about=\""+userObject.toString()+"\">");
//file.Writeln(14,"<s:color>"+color.toString()+"</s:color>");
file.Writeln(11,"</rdf:Description>");
file.Writeln(8,"</hasChannel>");
......
/**
* $Id: ProcessGraphModel.java,v 1.10 2002-06-14 13:33:08 frederic Exp $
* $Id: ProcessGraphModel.java,v 1.11 2002-06-14 15:19:21 frederic Exp $
*
* Transmorpher
*
......@@ -65,14 +65,18 @@ public class ProcessGraphModel extends DefaultGraphModel{
Object portTarget = ((Edge)edge).getTarget();
Object cellTarget = getParent(portTarget);
Object cell = getParent(port);
if (!((DefaultGraphCell)cellTarget instanceof PortProcessCell)){
if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()!=((PortCell)port).getType()));
else return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()==((PortCell)port).getType()));
Iterator it = edges(port);
if (lenght(it)>0) return false;
else {
if (!((DefaultGraphCell)cellTarget instanceof PortProcessCell)){
if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()!=((PortCell)port).getType()));
else return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()==((PortCell)port).getType()));
}
else if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()==((PortCell)port).getType()));
else return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()!=((PortCell)port).getType()));
}
else if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()==((PortCell)port).getType()));
else return ((!cellTarget.equals(cell))&&(((PortCell)portTarget).getType()!=((PortCell)port).getType()));
}
public boolean acceptsTarget(Object edge,Object port) {
......@@ -80,13 +84,29 @@ public class ProcessGraphModel extends DefaultGraphModel{
Object portSource = ((Edge)edge).getSource();
Object cellSource = getParent(portSource);
Object cell = getParent(port);
if (!((DefaultGraphCell)cellSource instanceof PortProcessCell)){
if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()!=((PortCell)port).getType()));
else return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()==((PortCell)port).getType()));
}
else if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()==((PortCell)port).getType()));
else return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()!=((PortCell)port).getType()));
Iterator it = edges(port);
if (lenght(it)>0) return false;
else {
if (!((DefaultGraphCell)cellSource instanceof PortProcessCell)){
if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()!=((PortCell)port).getType()));
else return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()==((PortCell)port).getType()));
}
else if (!((DefaultGraphCell)cell instanceof PortProcessCell))
return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()==((PortCell)port).getType()));
else return ((!cellSource.equals(cell))&&(((PortCell)portSource).getType()!=((PortCell)port).getType()));
}
}
public int lenght(java.util.Iterator it) {
int compteur = 0;
while (it.hasNext()){
it.next();
compteur++;
}
return compteur;
}
}
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