From 4e57d4dfb4e5ec8adb4f498499bbd54fac4d7a26 Mon Sep 17 00:00:00 2001 From: rfeng Date: Thu, 4 Feb 2010 19:36:42 +0000 Subject: Fix for TUSCANY-3456 (only register direct transformer with lower weight) git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@906613 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/databinding/impl/DirectedGraph.java | 19 ++++++++++++++++++- .../sca/databinding/impl/DirectedGraphTestCase.java | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'sca-java-2.x/trunk/modules') diff --git a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DirectedGraph.java b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DirectedGraph.java index 01916dfb96..44c42010ba 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DirectedGraph.java +++ b/sca-java-2.x/trunk/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/DirectedGraph.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; /** * Directed, weighted graph @@ -37,6 +38,7 @@ import java.util.concurrent.ConcurrentHashMap; * @version $Rev$ $Date$ */ public class DirectedGraph implements Cloneable { + private final static Logger logger = Logger.getLogger(DirectedGraph.class.getName()); private final Map vertices = new HashMap(); /** @@ -199,6 +201,21 @@ public class DirectedGraph implements Cloneable { } public void addEdge(V source, V target, E edgeValue, int weight, boolean publicEdge) { + // Fix for TUSCANY-3456 + // First check if we already has an edge + Edge edge = getEdge(source, target); + if (edge != null) { + // An existing edge has higher weight, let's replace it + if (edge.weight > weight) { + logger.warning("An edge exists with higher weight: " + edge); + removeEdge(edge); + } else { + // Don't add this edge + logger.warning("An edge exists with lower weight: " + edge); + return; + } + } + Vertex s = getVertex(source); if (s == null) { s = new Vertex(source); @@ -209,7 +226,7 @@ public class DirectedGraph implements Cloneable { t = new Vertex(target); vertices.put(target, t); } - Edge edge = new Edge(s, t, edgeValue, weight, publicEdge); + edge = new Edge(s, t, edgeValue, weight, publicEdge); s.outEdges.put(t, edge); t.inEdges.put(s, edge); } diff --git a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DirectedGraphTestCase.java b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DirectedGraphTestCase.java index d48711cd64..9ab2b51c95 100644 --- a/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DirectedGraphTestCase.java +++ b/sca-java-2.x/trunk/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/DirectedGraphTestCase.java @@ -44,7 +44,11 @@ public class DirectedGraphTestCase { @Test public void testGraph() { + graph.addEdge("a", "b", null, 4, true); + graph.addEdge("a", "b", null, 5, true); + Assert.assertEquals(4, graph.getEdge("a", "b").getWeight()); graph.addEdge("a", "b", null, 3, true); + Assert.assertEquals(3, graph.getEdge("a", "b").getWeight()); graph.addEdge("b", "c", null, 1, true); // graph.addEdge("a", "c", null, 8, true); graph.addEdge("a", "d", null, 3, true); -- cgit v1.2.3