Now using RouterConfig internally inside the XML Router.
[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.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.Transformation;
28
29 /**
30  * This class manages transformations and computes the shortest transformations
31  * paths based on the provided transformations.
32  * 
33  * @author Erik Brakkee
34  * 
35  */
36 public class TransformationPaths {
37
38     private Map<Id<Transformation>, Transformation> transformations;
39     private List<String> vertices;
40     private TransformationPath[][] matrix;
41
42     private Map<String, List<TransformationPath>> sequences;
43
44     /**
45      * Construct the transformations.
46      */
47     public TransformationPaths() {
48         transformations = new LinkedHashMap<Id<Transformation>, Transformation>();
49         vertices = new ArrayList<String>();
50         matrix = new TransformationPath[0][0];
51     }
52
53     public void replaceTransformations(
54         Map<Id<Transformation>, Transformation> aTransformations) {
55         transformations = aTransformations;
56         computeTransformationSequences();
57     }
58
59     /**
60      * Gets the possible target types based on an input type.
61      * 
62      * @param aType
63      *            Input type.
64      * @return Possible target types.
65      */
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();
72                 if (value == null) {
73                     value = aType;
74                 }
75                 res.add(value);
76             }
77         }
78         res.add(aType);
79         return res;
80     }
81
82     /**
83      * Gets the transformation path from A to B.
84      * 
85      * @param aFrom
86      *            From
87      * @param aTo
88      *            To
89      * @return Transformatkon path or null if not found.
90      */
91     public TransformationPath getPath(String aFrom, String aTo) {
92         int i = vertices.indexOf(aFrom);
93         if (i < 0) {
94             if (aFrom.equals(aTo)) {
95                 return new TransformationPath();
96             }
97             return null;
98         }
99
100         int j = vertices.indexOf(aTo);
101         if (j < 0) {
102             return null;
103         }
104         return matrix[i][j];
105     }
106
107     /**
108      * Computest the transformation sequences using Floyd's algorithm.
109      */
110     private void computeTransformationSequences() {
111         vertices = new ArrayList<String>();
112
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());
118         }
119
120         vertices.addAll(v);
121
122         matrix = new TransformationPath[vertices.size()][vertices.size()];
123
124         // Floyd's algorithm.
125         int nvertices = vertices.size();
126         for (int i = 0; i < nvertices; i++) {
127             matrix[i][i] = new TransformationPath();
128         }
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;
134         }
135
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
141                     // replace it.
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]);
147                     }
148                 }
149             }
150         }
151     }
152
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]
157             .size();
158     }
159
160     @Override
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];
168                 if (path != null) {
169                     buf.append(vertices.get(i));
170                     buf.append(" -> ");
171                     buf.append(vertices.get(j));
172                     buf.append(": ");
173                     buf.append(path);
174                     buf.append("\n");
175                 }
176             }
177         }
178         buf.append(")");
179         return buf.toString();
180     }
181 }