07d086b65a8dcbd03e3b5d47bdd1b623792ba854
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / Transformations.java
1 package org.wamblee.xmlrouter.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.util.concurrent.atomic.AtomicInteger;
12
13 import org.wamblee.xmlrouter.common.Id;
14 import org.wamblee.xmlrouter.config.Transformation;
15
16 public class Transformations {
17
18     private AtomicInteger sequenceNumber;
19     private Map<Integer, Transformation> transformations;
20     private List<String> vertices;
21     private TransformationPath[][] matrix;
22
23     private Map<String, List<TransformationPath>> sequences;
24
25     public Transformations() {
26         sequenceNumber = new AtomicInteger(1);
27         transformations = new LinkedHashMap<Integer, Transformation>();
28         vertices = new ArrayList<String>();
29         matrix = new TransformationPath[0][0];
30     }
31
32     public Id<Transformation> addTransformation(Transformation aTransformation) {
33         int seqno = sequenceNumber.getAndIncrement();
34         Id<Transformation> id = new Id<Transformation>(seqno);
35         transformations.put(seqno,
36             new RobustTransformation(id, aTransformation));
37         computeTransformationSequences();
38         return id;
39     }
40
41     public Collection<String> getPossibleTargetTypes(String aType) {
42         int index = vertices.indexOf(aType);
43         Set<String> res = new HashSet<String>();
44         for (int j = 0; j < vertices.size(); j++) {
45             if (matrix[index][j] != null) {
46                 String value = matrix[index][j].getToType();
47                 if (value == null) {
48                     value = aType;
49                 }
50                 res.add(value);
51             }
52         }
53         res.add(aType);
54         return res;
55     }
56
57     /**
58      * Gets the transformation path from A to B.
59      * 
60      * @param aFrom
61      *            From
62      * @param aTo
63      *            To
64      * @return Transformatkon path or null if not found.
65      */
66     public TransformationPath getPath(String aFrom, String aTo) {
67         int i = vertices.indexOf(aFrom);
68         if (i == -1) {
69             if (aFrom.equals(aTo)) {
70                 return new TransformationPath();
71             }
72             return null;
73         }
74
75         int j = vertices.indexOf(aTo);
76         return matrix[i][j];
77     }
78
79     private void computeTransformationSequences() {
80         vertices = new ArrayList<String>();
81
82         // Obtain possible starting points.
83         Set<String> v = new HashSet<String>();
84         for (Transformation transformation : transformations.values()) {
85             v.add(transformation.getFromType());
86             v.add(transformation.getToType());
87         }
88
89         vertices.addAll(v);
90
91         matrix = new TransformationPath[vertices.size()][vertices.size()];
92
93         // Floyd's algorithm.
94         int nvertices = vertices.size();
95         for (int i = 0; i < nvertices; i++) {
96             matrix[i][i] = new TransformationPath();
97         }
98         for (Transformation transformation : transformations.values()) {
99             int from = vertices.indexOf(transformation.getFromType());
100             int to = vertices.indexOf(transformation.getToType());
101             TransformationPath path = new TransformationPath(transformation);
102             matrix[from][to] = path;
103         }
104
105         for (int k = 0; k < nvertices; k++) {
106             for (int i = 0; i < nvertices; i++) {
107                 for (int j = 0; j < nvertices; j++) {
108                     // if the path from i to j through k is shorter then the
109                     // existing path then
110                     // replace it.
111                     int lij = getPathLength(i, j);
112                     int lik = getPathLength(i, k);
113                     int lkj = getPathLength(k, j);
114                     if (lik + lkj < lij) {
115                         matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
116                     }
117                 }
118             }
119         }
120     }
121
122     private int getPathLength(int i, int j) {
123         // We use MAX_INT/3 as infinity. This ensures that the default integer
124         // comparison in Floyd's algorithm does not lead to overflow.
125         return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
126             .size();
127     }
128
129     public void removeTransformation(Id<Transformation> aId) {
130         transformations.remove(aId.getId());
131         computeTransformationSequences();
132     }
133
134     public Collection<Transformation> getTransformations() {
135         return Collections.unmodifiableCollection(transformations.values());
136     }
137
138     @Override
139     public String toString() {
140         StringBuffer buf = new StringBuffer();
141         buf.append("Transformations(");
142         int nvertices = vertices.size();
143         for (int i = 0; i < nvertices; i++) {
144             for (int j = 0; j < nvertices; j++) {
145                 TransformationPath path = matrix[i][j];
146                 if (path != null) {
147                     buf.append(vertices.get(i));
148                     buf.append(" -> ");
149                     buf.append(vertices.get(j));
150                     buf.append(": ");
151                     buf.append(path);
152                     buf.append("\n");
153                 }
154             }
155         }
156         buf.append(")");
157         return buf.toString();
158     }
159 }