1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| package org.rblasch.convert; |
17 |
| |
18 |
| import org.rblasch.convert.type.Type; |
19 |
| import org.rblasch.convert.graph.SparseWeightedDirectedGraph; |
20 |
| import org.rblasch.convert.graph.WeightedDirectedGraph; |
21 |
| import org.rblasch.convert.converters.OnceConverterFactory; |
22 |
| |
23 |
| import java.util.Set; |
24 |
| import java.util.HashSet; |
25 |
| import java.util.Iterator; |
26 |
| import java.util.List; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class ShortestPathSearch implements ConverterLookup { |
32 |
| private final Set converters = new HashSet(); |
33 |
| private final Set factories = new HashSet(); |
34 |
| private final ShortestPathLookup lookup = new ShortestPathLookup(new SparseWeightedDirectedGraph()); |
35 |
| |
36 |
0
| private static WeightedDirectedGraph convertersToGraph(final Set cs) {
|
37 |
0
| final WeightedDirectedGraph g = new SparseWeightedDirectedGraph();
|
38 |
0
| for (final Iterator i = cs.iterator(); i.hasNext();) {
|
39 |
0
| final MetaConverter mc = (MetaConverter) i.next();
|
40 |
0
| g.addConnection(new TypeVertex(mc.getSourceType()),
|
41 |
| new ConverterEdge(mc, mc.getWeight()), |
42 |
| new TypeVertex(mc.getDestinationType())); |
43 |
| } |
44 |
0
| return g;
|
45 |
| } |
46 |
| |
47 |
0
| private static void runFactories(final ShortestPathLookup lookup, final List factories, final Type sourceType, final Type destinationType) {
|
48 |
0
| for (final Iterator i = factories.iterator(); i.hasNext();) {
|
49 |
0
| final ConverterFactory f = (ConverterFactory) i.next();
|
50 |
0
| lookup.addGraph(convertersToGraph(f.create(sourceType, destinationType)));
|
51 |
| } |
52 |
| } |
53 |
| |
54 |
14
| private static void runFactoriesOnAllKnownTypes(final ShortestPathLookup lookup, final Set factories) {
|
55 |
14
| final WeightedDirectedGraph g = new SparseWeightedDirectedGraph();
|
56 |
14
| final Set allKnownTypes = lookup.getAllKnownTypes();
|
57 |
14
| for (final Iterator i = allKnownTypes.iterator(); i.hasNext();) {
|
58 |
292
| final Type sourceType = (Type) i.next();
|
59 |
292
| for (final Iterator j = allKnownTypes.iterator(); j.hasNext();) {
|
60 |
7696
| final Type destinationType = (Type) j.next();
|
61 |
7696
| for (final Iterator k = factories.iterator(); k.hasNext();) {
|
62 |
29368
| final ConverterFactory factory = (ConverterFactory) k.next();
|
63 |
29368
| for (final Iterator l = factory.create(sourceType, destinationType).iterator(); l.hasNext();) {
|
64 |
1650
| final MetaConverter mc = (MetaConverter) l.next();
|
65 |
1650
| g.addConnection(new TypeVertex(mc.getSourceType()),
|
66 |
| new ConverterEdge(mc, mc.getWeight()), |
67 |
| new TypeVertex(mc.getDestinationType())); |
68 |
| } |
69 |
| |
70 |
| } |
71 |
| } |
72 |
| } |
73 |
14
| lookup.addGraph(g);
|
74 |
| } |
75 |
| |
76 |
0
| String dumpGraph() {
|
77 |
0
| return lookup.dumpGraph();
|
78 |
| } |
79 |
| |
80 |
14
| public void addConverter(final MetaConverter converter) {
|
81 |
14
| converters.add(converter);
|
82 |
14
| lookup.addConverter(converter);
|
83 |
| } |
84 |
| |
85 |
9
| public void addAllConverters(final Set converters) {
|
86 |
9
| for (final Iterator i = converters.iterator(); i.hasNext();) {
|
87 |
10
| addConverter((MetaConverter) i.next());
|
88 |
| } |
89 |
| } |
90 |
| |
91 |
8
| public void addOnceFactory(final OnceConverterFactory once) {
|
92 |
8
| lookup.addAllConverters(once.createOnce());
|
93 |
| } |
94 |
| |
95 |
21
| public void addFactory(final ConverterFactory factory) {
|
96 |
21
| if (factory instanceof OnceConverterFactory) {
|
97 |
8
| addOnceFactory((OnceConverterFactory) factory);
|
98 |
| } |
99 |
21
| factory.setLookup(lookup);
|
100 |
21
| factories.add(factory);
|
101 |
| } |
102 |
| |
103 |
4
| public void addAllFactories(final Set factories) {
|
104 |
4
| for (final Iterator i = factories.iterator(); i.hasNext();) {
|
105 |
16
| addFactory((ConverterFactory) i.next());
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
18
| public MetaConverter lookup(final Type sourceType, final Type destinationType) {
|
110 |
18
| lookup.addType(sourceType);
|
111 |
18
| lookup.addType(destinationType);
|
112 |
| |
113 |
18
| int numberOfTypes;
|
114 |
18
| NoSuchConversionException pendingEx;
|
115 |
18
| do {
|
116 |
31
| numberOfTypes = lookup.getNumberOfKnownTypes();
|
117 |
31
| try {
|
118 |
31
| return lookup.lookup(sourceType, destinationType);
|
119 |
| } catch (final NoSuchConversionException ex) { |
120 |
14
| pendingEx = ex;
|
121 |
14
| runFactoriesOnAllKnownTypes(lookup, factories);
|
122 |
| } |
123 |
14
| } while (numberOfTypes != lookup.getNumberOfKnownTypes());
|
124 |
1
| throw pendingEx;
|
125 |
| } |
126 |
| } |