Reduced the time during which the configuration is locked. Computation of paths now...
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / TransformationPaths.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.wamblee.xmlrouter.common.Id;
26 import org.wamblee.xmlrouter.config.Transformation;
27
28 /**
29  * This class manages transformations and computes the shortest transformations
30  * paths based on the provided transformations.
31  * 
32  * @author Erik Brakkee
33  * 
34  */
35 public class TransformationPaths {
36
37     private Map<Id<Transformation>, Transformation> transformations;
38     private List<String> vertices;
39     private TransformationPath[][] matrix;
40
41     private Map<String, List<TransformationPath>> sequences;
42
43     /**
44      * Construct the transformations.
45      */
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();
52     }
53
54     /**
55      * Gets the possible target types based on an input type.
56      * 
57      * @param aType
58      *            Input type.
59      * @return Possible target types.
60      */
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();
67                 if (value == null) {
68                     value = aType;
69                 }
70                 res.add(value);
71             }
72         }
73         res.add(aType);
74         return res;
75     }
76
77     /**
78      * Gets the transformation path from A to B.
79      * 
80      * @param aFrom
81      *            From
82      * @param aTo
83      *            To
84      * @return Transformatkon path or null if not found.
85      */
86     public TransformationPath getPath(String aFrom, String aTo) {
87         int i = vertices.indexOf(aFrom);
88         if (i < 0) {
89             if (aFrom.equals(aTo)) {
90                 return new TransformationPath();
91             }
92             return null;
93         }
94
95         int j = vertices.indexOf(aTo);
96         if (j < 0) {
97             return null;
98         }
99         return matrix[i][j];
100     }
101
102     /**
103      * Computest the transformation sequences using Floyd's algorithm.
104      */
105     private void computeTransformationSequences() {
106         vertices = new ArrayList<String>();
107
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());
113         }
114
115         vertices.addAll(v);
116
117         matrix = new TransformationPath[vertices.size()][vertices.size()];
118
119         // Floyd's algorithm.
120         int nvertices = vertices.size();
121         for (int i = 0; i < nvertices; i++) {
122             matrix[i][i] = new TransformationPath();
123         }
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;
129         }
130
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
136                     // replace it.
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]);
142                     }
143                 }
144             }
145         }
146     }
147
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]
152             .size();
153     }
154
155     @Override
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];
163                 if (path != null) {
164                     buf.append(vertices.get(i));
165                     buf.append(" -> ");
166                     buf.append(vertices.get(j));
167                     buf.append(": ");
168                     buf.append(path);
169                     buf.append("\n");
170                 }
171             }
172         }
173         buf.append(")");
174         return buf.toString();
175     }
176 }