2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.impl;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.List;
25 import org.wamblee.xmlrouter.common.Id;
26 import org.wamblee.xmlrouter.config.Transformation;
29 * This class manages transformations and computes the shortest transformations
30 * paths based on the provided transformations.
32 * @author Erik Brakkee
35 public class TransformationPaths {
37 private Map<Id<Transformation>, Transformation> transformations;
38 private List<String> vertices;
39 private TransformationPath[][] matrix;
41 private Map<String, List<TransformationPath>> sequences;
44 * Construct the transformations.
46 public TransformationPaths(
47 Map<Id<Transformation>, Transformation> aTransformations) {
48 transformations = aTransformations;
49 vertices = new ArrayList<String>();
50 matrix = new TransformationPath[0][0];
51 computeTransformationSequences();
55 * Gets the possible target types based on an input type.
59 * @return Possible target types.
61 public Collection<String> getPossibleTargetTypes(String aType) {
62 int index = vertices.indexOf(aType);
63 Set<String> res = new HashSet<String>();
64 for (int j = 0; j < vertices.size(); j++) {
65 if (matrix[index][j] != null) {
66 String value = matrix[index][j].getToType();
78 * Gets the transformation path from A to B.
84 * @return Transformatkon path or null if not found.
86 public TransformationPath getPath(String aFrom, String aTo) {
87 int i = vertices.indexOf(aFrom);
89 if (aFrom.equals(aTo)) {
90 return new TransformationPath();
95 int j = vertices.indexOf(aTo);
103 * Computest the transformation sequences using Floyd's algorithm.
105 private void computeTransformationSequences() {
106 vertices = new ArrayList<String>();
108 // Obtain possible starting points.
109 Set<String> v = new HashSet<String>();
110 for (Transformation transformation : transformations.values()) {
111 v.add(transformation.getFromType());
112 v.add(transformation.getToType());
117 matrix = new TransformationPath[vertices.size()][vertices.size()];
119 // Floyd's algorithm.
120 int nvertices = vertices.size();
121 for (int i = 0; i < nvertices; i++) {
122 matrix[i][i] = new TransformationPath();
124 for (Transformation transformation : transformations.values()) {
125 int from = vertices.indexOf(transformation.getFromType());
126 int to = vertices.indexOf(transformation.getToType());
127 TransformationPath path = new TransformationPath(transformation);
128 matrix[from][to] = path;
131 for (int k = 0; k < nvertices; k++) {
132 for (int i = 0; i < nvertices; i++) {
133 for (int j = 0; j < nvertices; j++) {
134 // if the path from i to j through k is shorter then the
135 // existing path then
137 int lij = getPathLength(i, j);
138 int lik = getPathLength(i, k);
139 int lkj = getPathLength(k, j);
140 if (lik + lkj < lij) {
141 matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
148 private int getPathLength(int i, int j) {
149 // We use MAX_INT/3 as infinity. This ensures that the default integer
150 // comparison in Floyd's algorithm does not lead to overflow.
151 return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
156 public String toString() {
157 StringBuffer buf = new StringBuffer();
158 buf.append("Transformations(");
159 int nvertices = vertices.size();
160 for (int i = 0; i < nvertices; i++) {
161 for (int j = 0; j < nvertices; j++) {
162 TransformationPath path = matrix[i][j];
164 buf.append(vertices.get(i));
166 buf.append(vertices.get(j));
174 return buf.toString();