1 package org.wamblee.xmlrouter.impl;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.LinkedHashMap;
11 import java.util.concurrent.atomic.AtomicInteger;
13 import org.wamblee.xmlrouter.common.Id;
14 import org.wamblee.xmlrouter.config.Transformation;
16 public class Transformations {
18 private AtomicInteger sequenceNumber;
19 private Map<Integer, Transformation> transformations;
20 private List<String> vertices;
21 private TransformationPath[][] matrix;
23 private Map<String, List<TransformationPath>> sequences;
25 public Transformations() {
26 sequenceNumber = new AtomicInteger(1);
27 transformations = new LinkedHashMap<Integer, Transformation>();
28 vertices = new ArrayList<String>();
29 matrix = new TransformationPath[0][0];
32 public Id<Transformation> addTransformation(Transformation aTransformation) {
33 int seqno = sequenceNumber.getAndIncrement();
34 Id<Transformation> id = new Id<Transformation>(seqno);
35 transformations.put(seqno,
36 new RobustTransformation(id, aTransformation));
37 computeTransformationSequences();
41 public Collection<String> getPossibleTargetTypes(String aType) {
42 int index = vertices.indexOf(aType);
43 Set<String> res = new HashSet<String>();
44 for (int j = 0; j < vertices.size(); j++) {
45 if (matrix[index][j] != null) {
46 String value = matrix[index][j].getToType();
58 * Gets the transformation path from A to B.
64 * @return Transformatkon path or null if not found.
66 public TransformationPath getPath(String aFrom, String aTo) {
67 int i = vertices.indexOf(aFrom);
69 if (aFrom.equals(aTo)) {
70 return new TransformationPath();
75 int j = vertices.indexOf(aTo);
79 private void computeTransformationSequences() {
80 vertices = new ArrayList<String>();
82 // Obtain possible starting points.
83 Set<String> v = new HashSet<String>();
84 for (Transformation transformation : transformations.values()) {
85 v.add(transformation.getFromType());
86 v.add(transformation.getToType());
91 matrix = new TransformationPath[vertices.size()][vertices.size()];
94 int nvertices = vertices.size();
95 for (int i = 0; i < nvertices; i++) {
96 matrix[i][i] = new TransformationPath();
98 for (Transformation transformation : transformations.values()) {
99 int from = vertices.indexOf(transformation.getFromType());
100 int to = vertices.indexOf(transformation.getToType());
101 TransformationPath path = new TransformationPath(transformation);
102 matrix[from][to] = path;
105 for (int k = 0; k < nvertices; k++) {
106 for (int i = 0; i < nvertices; i++) {
107 for (int j = 0; j < nvertices; j++) {
108 // if the path from i to j through k is shorter then the
109 // existing path then
111 int lij = getPathLength(i, j);
112 int lik = getPathLength(i, k);
113 int lkj = getPathLength(k, j);
114 if (lik + lkj < lij) {
115 matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
122 private int getPathLength(int i, int j) {
123 // We use MAX_INT/3 as infinity. This ensures that the default integer
124 // comparison in Floyd's algorithm does not lead to overflow.
125 return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
129 public void removeTransformation(Id<Transformation> aId) {
130 transformations.remove(aId.getId());
131 computeTransformationSequences();
134 public Collection<Transformation> getTransformations() {
135 return Collections.unmodifiableCollection(transformations.values());
139 public String toString() {
140 StringBuffer buf = new StringBuffer();
141 buf.append("Transformations(");
142 int nvertices = vertices.size();
143 for (int i = 0; i < nvertices; i++) {
144 for (int j = 0; j < nvertices; j++) {
145 TransformationPath path = matrix[i][j];
147 buf.append(vertices.get(i));
149 buf.append(vertices.get(j));
157 return buf.toString();