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.LinkedHashMap;
22 import java.util.List;
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.Transformation;
30 * This class manages transformations and computes the shortest transformations
31 * paths based on the provided transformations.
33 * @author Erik Brakkee
36 public class Transformations {
38 private Map<Id<Transformation>, Transformation> transformations;
39 private List<String> vertices;
40 private TransformationPath[][] matrix;
42 private Map<String, List<TransformationPath>> sequences;
45 * Construct the transformations.
47 public Transformations() {
48 transformations = new LinkedHashMap<Id<Transformation>, Transformation>();
49 vertices = new ArrayList<String>();
50 matrix = new TransformationPath[0][0];
53 public void replaceTransformations(
54 Map<Id<Transformation>, Transformation> aTransformations) {
55 transformations = aTransformations;
56 computeTransformationSequences();
60 * Gets the possible target types based on an input type.
64 * @return Possible target types.
66 public Collection<String> getPossibleTargetTypes(String aType) {
67 int index = vertices.indexOf(aType);
68 Set<String> res = new HashSet<String>();
69 for (int j = 0; j < vertices.size(); j++) {
70 if (matrix[index][j] != null) {
71 String value = matrix[index][j].getToType();
83 * Gets the transformation path from A to B.
89 * @return Transformatkon path or null if not found.
91 public TransformationPath getPath(String aFrom, String aTo) {
92 int i = vertices.indexOf(aFrom);
94 if (aFrom.equals(aTo)) {
95 return new TransformationPath();
100 int j = vertices.indexOf(aTo);
108 * Computest the transformation sequences using Floyd's algorithm.
110 private void computeTransformationSequences() {
111 vertices = new ArrayList<String>();
113 // Obtain possible starting points.
114 Set<String> v = new HashSet<String>();
115 for (Transformation transformation : transformations.values()) {
116 v.add(transformation.getFromType());
117 v.add(transformation.getToType());
122 matrix = new TransformationPath[vertices.size()][vertices.size()];
124 // Floyd's algorithm.
125 int nvertices = vertices.size();
126 for (int i = 0; i < nvertices; i++) {
127 matrix[i][i] = new TransformationPath();
129 for (Transformation transformation : transformations.values()) {
130 int from = vertices.indexOf(transformation.getFromType());
131 int to = vertices.indexOf(transformation.getToType());
132 TransformationPath path = new TransformationPath(transformation);
133 matrix[from][to] = path;
136 for (int k = 0; k < nvertices; k++) {
137 for (int i = 0; i < nvertices; i++) {
138 for (int j = 0; j < nvertices; j++) {
139 // if the path from i to j through k is shorter then the
140 // existing path then
142 int lij = getPathLength(i, j);
143 int lik = getPathLength(i, k);
144 int lkj = getPathLength(k, j);
145 if (lik + lkj < lij) {
146 matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
153 private int getPathLength(int i, int j) {
154 // We use MAX_INT/3 as infinity. This ensures that the default integer
155 // comparison in Floyd's algorithm does not lead to overflow.
156 return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
161 public String toString() {
162 StringBuffer buf = new StringBuffer();
163 buf.append("Transformations(");
164 int nvertices = vertices.size();
165 for (int i = 0; i < nvertices; i++) {
166 for (int j = 0; j < nvertices; j++) {
167 TransformationPath path = matrix[i][j];
169 buf.append(vertices.get(i));
171 buf.append(vertices.get(j));
179 return buf.toString();