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.Collections;
21 import java.util.HashSet;
22 import java.util.LinkedHashMap;
23 import java.util.List;
26 import java.util.concurrent.atomic.AtomicInteger;
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Transformation;
31 public class Transformations {
33 private AtomicInteger sequenceNumber;
34 private Map<Integer, Transformation> transformations;
35 private List<String> vertices;
36 private TransformationPath[][] matrix;
38 private Map<String, List<TransformationPath>> sequences;
40 public Transformations() {
41 sequenceNumber = new AtomicInteger(1);
42 transformations = new LinkedHashMap<Integer, Transformation>();
43 vertices = new ArrayList<String>();
44 matrix = new TransformationPath[0][0];
47 public Id<Transformation> addTransformation(Transformation aTransformation) {
48 int seqno = sequenceNumber.getAndIncrement();
49 Id<Transformation> id = new Id<Transformation>(seqno);
50 transformations.put(seqno,
51 new RobustTransformation(id, aTransformation));
52 computeTransformationSequences();
56 public Collection<String> getPossibleTargetTypes(String aType) {
57 int index = vertices.indexOf(aType);
58 Set<String> res = new HashSet<String>();
59 for (int j = 0; j < vertices.size(); j++) {
60 if (matrix[index][j] != null) {
61 String value = matrix[index][j].getToType();
73 * Gets the transformation path from A to B.
79 * @return Transformatkon path or null if not found.
81 public TransformationPath getPath(String aFrom, String aTo) {
82 int i = vertices.indexOf(aFrom);
84 if (aFrom.equals(aTo)) {
85 return new TransformationPath();
90 int j = vertices.indexOf(aTo);
94 private void computeTransformationSequences() {
95 vertices = new ArrayList<String>();
97 // Obtain possible starting points.
98 Set<String> v = new HashSet<String>();
99 for (Transformation transformation : transformations.values()) {
100 v.add(transformation.getFromType());
101 v.add(transformation.getToType());
106 matrix = new TransformationPath[vertices.size()][vertices.size()];
108 // Floyd's algorithm.
109 int nvertices = vertices.size();
110 for (int i = 0; i < nvertices; i++) {
111 matrix[i][i] = new TransformationPath();
113 for (Transformation transformation : transformations.values()) {
114 int from = vertices.indexOf(transformation.getFromType());
115 int to = vertices.indexOf(transformation.getToType());
116 TransformationPath path = new TransformationPath(transformation);
117 matrix[from][to] = path;
120 for (int k = 0; k < nvertices; k++) {
121 for (int i = 0; i < nvertices; i++) {
122 for (int j = 0; j < nvertices; j++) {
123 // if the path from i to j through k is shorter then the
124 // existing path then
126 int lij = getPathLength(i, j);
127 int lik = getPathLength(i, k);
128 int lkj = getPathLength(k, j);
129 if (lik + lkj < lij) {
130 matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
137 private int getPathLength(int i, int j) {
138 // We use MAX_INT/3 as infinity. This ensures that the default integer
139 // comparison in Floyd's algorithm does not lead to overflow.
140 return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
144 public void removeTransformation(Id<Transformation> aId) {
145 transformations.remove(aId.getId());
146 computeTransformationSequences();
149 public Collection<Transformation> getTransformations() {
150 return Collections.unmodifiableCollection(transformations.values());
154 public String toString() {
155 StringBuffer buf = new StringBuffer();
156 buf.append("Transformations(");
157 int nvertices = vertices.size();
158 for (int i = 0; i < nvertices; i++) {
159 for (int j = 0; j < nvertices; j++) {
160 TransformationPath path = matrix[i][j];
162 buf.append(vertices.get(i));
164 buf.append(vertices.get(j));
172 return buf.toString();