refactoring of the config interface towards more reuse in the implementation and...
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / Transformations.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.Config;
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 Transformations {
37
38     private Config<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 Transformations() {
48         transformations = new ConfigImpl<Transformation>() {
49             @Override
50             public Transformation wrap(Id<Transformation> aId,
51                 Transformation aType) {
52                 return new RobustTransformation(aId, aType);
53             }
54         };
55         vertices = new ArrayList<String>();
56         matrix = new TransformationPath[0][0];
57     }
58
59     public Config<Transformation> getTransformationConfig() {
60         return new Config<Transformation>() {
61             @Override
62             public Id<Transformation> add(Transformation aT) {
63                 return addTransformation(aT);
64             }
65
66             @Override
67             public Transformation get(Id<Transformation> aId) {
68                 return transformations.get(aId);
69             }
70
71             @Override
72             public Collection<Id<Transformation>> ids() {
73                 return transformations.ids();
74             }
75
76             @Override
77             public boolean remove(Id<Transformation> aId) {
78                 return transformations.remove(aId);
79             }
80         };
81     }
82
83     /**
84      * Adds a transformation. Leads to recomputation of shortest paths.
85      * 
86      * @param aTransformation
87      *            Transformation to add.
88      * @return Id of the transformation.
89      */
90     public Id<Transformation> addTransformation(Transformation aTransformation) {
91         Id<Transformation> id = transformations.add(aTransformation);
92         computeTransformationSequences();
93         return id;
94     }
95
96     /**
97      * Gets the possible target types based on an input type.
98      * 
99      * @param aType
100      *            Input type.
101      * @return Possible target types.
102      */
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();
109                 if (value == null) {
110                     value = aType;
111                 }
112                 res.add(value);
113             }
114         }
115         res.add(aType);
116         return res;
117     }
118
119     /**
120      * Gets the transformation path from A to B.
121      * 
122      * @param aFrom
123      *            From
124      * @param aTo
125      *            To
126      * @return Transformatkon path or null if not found.
127      */
128     public TransformationPath getPath(String aFrom, String aTo) {
129         int i = vertices.indexOf(aFrom);
130         if (i < 0) {
131             if (aFrom.equals(aTo)) {
132                 return new TransformationPath();
133             }
134             return null;
135         }
136
137         int j = vertices.indexOf(aTo);
138         if (j < 0) {
139             return null;
140         }
141         return matrix[i][j];
142     }
143
144     /**
145      * Computest the transformation sequences using Floyd's algorithm.
146      */
147     private void computeTransformationSequences() {
148         vertices = new ArrayList<String>();
149
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());
156         }
157
158         vertices.addAll(v);
159
160         matrix = new TransformationPath[vertices.size()][vertices.size()];
161
162         // Floyd's algorithm.
163         int nvertices = vertices.size();
164         for (int i = 0; i < nvertices; i++) {
165             matrix[i][i] = new TransformationPath();
166         }
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;
173         }
174
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
180                     // replace it.
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]);
186                     }
187                 }
188             }
189         }
190     }
191
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]
196             .size();
197     }
198
199     /**
200      * Removes a transformation.
201      * 
202      * @param aId
203      *            Id of the transformation.
204      */
205     public void removeTransformation(Id<Transformation> aId) {
206         transformations.remove(aId);
207         computeTransformationSequences();
208     }
209
210     @Override
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];
218                 if (path != null) {
219                     buf.append(vertices.get(i));
220                     buf.append(" -> ");
221                     buf.append(vertices.get(j));
222                     buf.append(": ");
223                     buf.append(path);
224                     buf.append("\n");
225                 }
226             }
227         }
228         buf.append(")");
229         return buf.toString();
230     }
231 }