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.Config;
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 Config<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 ConfigImpl<Transformation>() {
50 public Transformation wrap(Id<Transformation> aId,
51 Transformation aType) {
52 return new RobustTransformation(aId, aType);
55 vertices = new ArrayList<String>();
56 matrix = new TransformationPath[0][0];
59 public Config<Transformation> getTransformationConfig() {
60 return new Config<Transformation>() {
62 public Id<Transformation> add(Transformation aT) {
63 return addTransformation(aT);
67 public Transformation get(Id<Transformation> aId) {
68 return transformations.get(aId);
72 public Collection<Id<Transformation>> ids() {
73 return transformations.ids();
77 public boolean remove(Id<Transformation> aId) {
78 return transformations.remove(aId);
84 * Adds a transformation. Leads to recomputation of shortest paths.
86 * @param aTransformation
87 * Transformation to add.
88 * @return Id of the transformation.
90 public Id<Transformation> addTransformation(Transformation aTransformation) {
91 Id<Transformation> id = transformations.add(aTransformation);
92 computeTransformationSequences();
97 * Gets the possible target types based on an input type.
101 * @return Possible target types.
103 public Collection<String> getPossibleTargetTypes(String aType) {
104 int index = vertices.indexOf(aType);
105 Set<String> res = new HashSet<String>();
106 for (int j = 0; j < vertices.size(); j++) {
107 if (matrix[index][j] != null) {
108 String value = matrix[index][j].getToType();
120 * Gets the transformation path from A to B.
126 * @return Transformatkon path or null if not found.
128 public TransformationPath getPath(String aFrom, String aTo) {
129 int i = vertices.indexOf(aFrom);
131 if (aFrom.equals(aTo)) {
132 return new TransformationPath();
137 int j = vertices.indexOf(aTo);
145 * Computest the transformation sequences using Floyd's algorithm.
147 private void computeTransformationSequences() {
148 vertices = new ArrayList<String>();
150 // Obtain possible starting points.
151 Set<String> v = new HashSet<String>();
152 for (Id<Transformation> id : transformations.ids()) {
153 Transformation transformation = transformations.get(id);
154 v.add(transformation.getFromType());
155 v.add(transformation.getToType());
160 matrix = new TransformationPath[vertices.size()][vertices.size()];
162 // Floyd's algorithm.
163 int nvertices = vertices.size();
164 for (int i = 0; i < nvertices; i++) {
165 matrix[i][i] = new TransformationPath();
167 for (Id<Transformation> id : transformations.ids()) {
168 Transformation transformation = transformations.get(id);
169 int from = vertices.indexOf(transformation.getFromType());
170 int to = vertices.indexOf(transformation.getToType());
171 TransformationPath path = new TransformationPath(transformation);
172 matrix[from][to] = path;
175 for (int k = 0; k < nvertices; k++) {
176 for (int i = 0; i < nvertices; i++) {
177 for (int j = 0; j < nvertices; j++) {
178 // if the path from i to j through k is shorter then the
179 // existing path then
181 int lij = getPathLength(i, j);
182 int lik = getPathLength(i, k);
183 int lkj = getPathLength(k, j);
184 if (lik + lkj < lij) {
185 matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
192 private int getPathLength(int i, int j) {
193 // We use MAX_INT/3 as infinity. This ensures that the default integer
194 // comparison in Floyd's algorithm does not lead to overflow.
195 return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
200 * Removes a transformation.
203 * Id of the transformation.
205 public void removeTransformation(Id<Transformation> aId) {
206 transformations.remove(aId);
207 computeTransformationSequences();
211 public String toString() {
212 StringBuffer buf = new StringBuffer();
213 buf.append("Transformations(");
214 int nvertices = vertices.size();
215 for (int i = 0; i < nvertices; i++) {
216 for (int j = 0; j < nvertices; j++) {
217 TransformationPath path = matrix[i][j];
219 buf.append(vertices.get(i));
221 buf.append(vertices.get(j));
229 return buf.toString();