Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/CollapsingAssociationCursor.java |
— | — | @@ -1,47 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import de.brightbyte.data.cursor.DataCursor; |
5 | | -import de.brightbyte.util.PersistenceException; |
6 | | - |
7 | | -public class CollapsingAssociationCursor implements DataCursor<Association> { |
8 | | - |
9 | | - protected DataCursor<Association> cursor; |
10 | | - protected Association prev; |
11 | | - |
12 | | - protected String sourceKeyField; |
13 | | - protected String targetKeyField; |
14 | | - |
15 | | - public CollapsingAssociationCursor(DataCursor<Association> cursor, String sourceKeyField, String targetKeyField) { |
16 | | - if (cursor==null) throw new NullPointerException(); |
17 | | - if (sourceKeyField==null) throw new NullPointerException(); |
18 | | - if (targetKeyField==null) throw new NullPointerException(); |
19 | | - |
20 | | - this.cursor = cursor; |
21 | | - this.sourceKeyField = sourceKeyField; |
22 | | - this.targetKeyField = targetKeyField; |
23 | | - } |
24 | | - |
25 | | - public void close() { |
26 | | - cursor.close(); |
27 | | - } |
28 | | - |
29 | | - public Association next() throws PersistenceException { |
30 | | - if (prev==null) prev = cursor.next(); |
31 | | - if (prev==null) return null; |
32 | | - |
33 | | - Association a = prev; |
34 | | - |
35 | | - while (true) { |
36 | | - prev = cursor.next(); |
37 | | - if (prev==null) break; |
38 | | - |
39 | | - if (!prev.getSourceItem().overlaps(a.getSourceItem(), sourceKeyField)) break; |
40 | | - if (!prev.getTargetItem().overlaps(a.getTargetItem(), targetKeyField)) break; |
41 | | - |
42 | | - a = Association.merge(a, prev); |
43 | | - } |
44 | | - |
45 | | - return a; |
46 | | - } |
47 | | - |
48 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureSetValueSplitter.java |
— | — | @@ -1,146 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import java.text.ParseException; |
5 | | -import java.util.ArrayList; |
6 | | -import java.util.List; |
7 | | -import java.util.Map; |
8 | | -import java.util.regex.Pattern; |
9 | | - |
10 | | -import de.brightbyte.text.Chunker; |
11 | | -import de.brightbyte.text.RegularExpressionChunker; |
12 | | - |
13 | | - |
14 | | -public class FeatureSetValueSplitter implements FeatureSetMangler { |
15 | | - |
16 | | - public static MultiMangler multi(FeatureSetValueSplitter... splitters) { |
17 | | - return new MultiMangler((FeatureSetMangler[])splitters); |
18 | | - } |
19 | | - |
20 | | - public static MultiMangler multiFromSplitters(Iterable<FeatureSetValueSplitter> splitters) { |
21 | | - MultiMangler m = new MultiMangler(); |
22 | | - |
23 | | - for (FeatureSetValueSplitter s: splitters) { |
24 | | - m.addMangler(s); |
25 | | - } |
26 | | - |
27 | | - return m; |
28 | | - } |
29 | | - |
30 | | - public static MultiMangler multiFromPatternMap(Map<String, Pattern> splitters) { |
31 | | - MultiMangler m = new MultiMangler(); |
32 | | - |
33 | | - for (Map.Entry<String, Pattern>e: splitters.entrySet()) { |
34 | | - m.addMangler(new FeatureSetValueSplitter(e.getKey(), e.getValue())); |
35 | | - } |
36 | | - |
37 | | - return m; |
38 | | - } |
39 | | - |
40 | | - public static MultiMangler multiFromChunkerMap(Map<String, Chunker> splitters) { |
41 | | - MultiMangler m = new MultiMangler(); |
42 | | - |
43 | | - for (Map.Entry<String, Chunker>e: splitters.entrySet()) { |
44 | | - m.addMangler(new FeatureSetValueSplitter(e.getKey(), e.getValue())); |
45 | | - } |
46 | | - |
47 | | - return m; |
48 | | - } |
49 | | - |
50 | | - public static MultiMangler multiFromStringMap(Map<String, String> splitters, int flags) { |
51 | | - MultiMangler m = new MultiMangler(); |
52 | | - |
53 | | - for (Map.Entry<String, String>e: splitters.entrySet()) { |
54 | | - m.addMangler(new FeatureSetValueSplitter(e.getKey(), e.getValue(), flags)); |
55 | | - } |
56 | | - |
57 | | - return m; |
58 | | - } |
59 | | - |
60 | | - protected String field; |
61 | | - protected Chunker chunker; |
62 | | - |
63 | | - public FeatureSetValueSplitter(String field, String regex, int flags) { |
64 | | - this(field, Pattern.compile(regex, flags)); |
65 | | - } |
66 | | - |
67 | | - public FeatureSetValueSplitter(String field, Pattern pattern) { |
68 | | - this(field, new RegularExpressionChunker(pattern)); |
69 | | - } |
70 | | - |
71 | | - public FeatureSetValueSplitter(String field, Chunker chunker) { |
72 | | - this.field = field; |
73 | | - this.chunker = chunker; |
74 | | - } |
75 | | - |
76 | | - @Override |
77 | | - public int hashCode() { |
78 | | - final int PRIME = 31; |
79 | | - int result = 1; |
80 | | - result = PRIME * result + ((field == null) ? 0 : field.hashCode()); |
81 | | - result = PRIME * result + ((chunker == null) ? 0 : chunker.hashCode()); |
82 | | - return result; |
83 | | - } |
84 | | - |
85 | | - @Override |
86 | | - public boolean equals(Object obj) { |
87 | | - if (this == obj) |
88 | | - return true; |
89 | | - if (obj == null) |
90 | | - return false; |
91 | | - if (getClass() != obj.getClass()) |
92 | | - return false; |
93 | | - final FeatureSetValueSplitter other = (FeatureSetValueSplitter) obj; |
94 | | - if (field == null) { |
95 | | - if (other.field != null) |
96 | | - return false; |
97 | | - } else if (!field.equals(other.field)) |
98 | | - return false; |
99 | | - if (chunker == null) { |
100 | | - if (other.chunker != null) |
101 | | - return false; |
102 | | - } else if (!chunker.equals(chunker)) |
103 | | - return false; |
104 | | - return true; |
105 | | - } |
106 | | - |
107 | | - public FeatureSet apply(FeatureSet fts) { |
108 | | - List<Object> v = fts.get(field); |
109 | | - if (v==null || v.size()==0) return fts; |
110 | | - |
111 | | - List<Object> w = null; |
112 | | - |
113 | | - int c = 0; |
114 | | - for (Object obj: v) { |
115 | | - String s = obj.toString(); |
116 | | - |
117 | | - try { |
118 | | - Iterable<String> vv = chunker.chunk(s); |
119 | | - |
120 | | - //TODO: detecting trivial items might speed things up. |
121 | | - //if (w!=null || vv.size()!=1 || !vv.get(0).equals(s)) { |
122 | | - if (w==null) { |
123 | | - w = new ArrayList<Object>(v.size()*2); |
124 | | - if (c>0) w.addAll(v.subList(0, c)); |
125 | | - } |
126 | | - |
127 | | - for (String a: vv) |
128 | | - w.add(a); |
129 | | - //} |
130 | | - } catch (ParseException e) { |
131 | | - //split failed, so keep value as-is |
132 | | - //XXX: somehow report? |
133 | | - if (w!=null) w.add(s); |
134 | | - } |
135 | | - |
136 | | - c++; |
137 | | - } |
138 | | - |
139 | | - if (w!=null) { |
140 | | - fts.remove(field); |
141 | | - fts.putAll(field, w); |
142 | | - } |
143 | | - |
144 | | - return fts; |
145 | | - } |
146 | | - |
147 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureSetCursor.java |
— | — | @@ -1,49 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import de.brightbyte.data.cursor.DataCursor; |
5 | | -import de.brightbyte.util.PersistenceException; |
6 | | - |
7 | | -public class FeatureSetCursor<R> implements DataCursor<FeatureSet> { |
8 | | - protected DataCursor<R> source; |
9 | | - protected PropertyMapping<R> mapping; |
10 | | - |
11 | | - protected FeatureSetCursor(DataCursor<R> source) { |
12 | | - if (source==null) throw new NullPointerException(); |
13 | | - this.source = source; |
14 | | - } |
15 | | - |
16 | | - public FeatureSetCursor(DataCursor<R> source, PropertyMapping<R> mapping) { |
17 | | - this(source); |
18 | | - if (mapping==null) throw new NullPointerException(); |
19 | | - this.mapping = mapping; |
20 | | - } |
21 | | - |
22 | | - public void close() { |
23 | | - source.close(); |
24 | | - } |
25 | | - |
26 | | - public FeatureSet next() throws PersistenceException { |
27 | | - R r = source.next(); |
28 | | - if (r==null) return null; |
29 | | - return record(r); |
30 | | - } |
31 | | - |
32 | | - protected FeatureSet record(R row) { |
33 | | - if (mapping==null) throw new IllegalStateException("no peoperty mapping defined yet!"); |
34 | | - |
35 | | - FeatureSet ft = new DefaultFeatureSet(); |
36 | | - |
37 | | - for (String f : mapping.fields()) { |
38 | | - Object v = mapping.getValue(row, f, null); //XXX: extra type conversion?! |
39 | | - |
40 | | - ft.put(f, v); |
41 | | - } |
42 | | - |
43 | | - return ft; |
44 | | - } |
45 | | - |
46 | | - protected void finalize() { |
47 | | - close(); |
48 | | - } |
49 | | - |
50 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureSetValueMapper.java |
— | — | @@ -1,51 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import java.util.Collection; |
5 | | - |
6 | | -import de.brightbyte.abstraction.PropertyAccessor; |
7 | | -import de.brightbyte.data.Functor; |
8 | | -import de.brightbyte.data.Functors; |
9 | | -import de.brightbyte.wikiword.integrator.FeatureSetSourceDescriptor; |
10 | | - |
11 | | -public class FeatureSetValueMapper implements FeatureSetMangler { |
12 | | - |
13 | | - protected FeatureMapping mapping; |
14 | | - |
15 | | - public FeatureSetValueMapper() { |
16 | | - this(new FeatureMapping()); |
17 | | - } |
18 | | - |
19 | | - public FeatureSetValueMapper(FeatureMapping mapping) { |
20 | | - if (mapping==null) throw new NullPointerException(); |
21 | | - this.mapping = mapping; |
22 | | - } |
23 | | - |
24 | | - public FeatureSet apply(FeatureSet features) { |
25 | | - FeatureSet ft = new DefaultFeatureSet(); |
26 | | - |
27 | | - for (String f : mapping.fields()) { |
28 | | - Object v = mapping.getValue(features, f, null); //XXX: extra type conversion?! |
29 | | - |
30 | | - ft.put(f, v); |
31 | | - } |
32 | | - |
33 | | - return ft; |
34 | | - } |
35 | | - |
36 | | - public <T> void addMapping(String field, FeatureSetSourceDescriptor source, String option, Class<T> type, Functor<?, ? extends Collection<?>> aggregator) { |
37 | | - mapping.addMapping(field, source, option, type, aggregator); |
38 | | - } |
39 | | - |
40 | | - public void addMapping(String field, PropertyAccessor<FeatureSet, ?> accessor) { |
41 | | - mapping.addMapping(field, accessor); |
42 | | - } |
43 | | - |
44 | | - public <T> void addMapping(String field, String feature, Class<T> type, Functor<?, ? extends Collection<?>> aggregator) { |
45 | | - mapping.addMapping(field, feature, type, aggregator); |
46 | | - } |
47 | | - |
48 | | - public <T> void addMapping(String field, String feature) { |
49 | | - mapping.addMapping(field, feature); |
50 | | - } |
51 | | - |
52 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/AssemblingFeatureSetCursor.java |
— | — | @@ -1,55 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import java.util.List; |
5 | | - |
6 | | -import de.brightbyte.data.cursor.DataCursor; |
7 | | -import de.brightbyte.util.PersistenceException; |
8 | | - |
9 | | -public class AssemblingFeatureSetCursor implements DataCursor<FeatureSet> { |
10 | | - |
11 | | - protected DataCursor<FeatureSet> cursor; |
12 | | - protected FeatureSet prev; |
13 | | - |
14 | | - protected String recordIdField; |
15 | | - protected String propertyNameField; |
16 | | - protected String propertyValueField; |
17 | | - |
18 | | - public AssemblingFeatureSetCursor(DataCursor<FeatureSet> cursor, String recordIdField, String propertyNameField, String propertyValueField) { |
19 | | - if (cursor==null) throw new NullPointerException(); |
20 | | - if (recordIdField==null) throw new NullPointerException(); |
21 | | - if (propertyNameField==null) throw new NullPointerException(); |
22 | | - if (propertyValueField==null) throw new NullPointerException(); |
23 | | - |
24 | | - this.cursor = cursor; |
25 | | - this.recordIdField = recordIdField; |
26 | | - this.propertyNameField = propertyNameField; |
27 | | - this.propertyValueField = propertyValueField; |
28 | | - } |
29 | | - |
30 | | - public void close() { |
31 | | - cursor.close(); |
32 | | - } |
33 | | - |
34 | | - public FeatureSet next() throws PersistenceException { |
35 | | - if (prev==null) prev = cursor.next(); |
36 | | - if (prev==null) return null; |
37 | | - |
38 | | - FeatureSet a = new DefaultFeatureSet();; |
39 | | - a.putAll(recordIdField, prev.get(recordIdField)); |
40 | | - |
41 | | - while (prev!=null) { |
42 | | - List<Object> keys = prev.get(propertyNameField); |
43 | | - List<Object> values = prev.get(propertyValueField); |
44 | | - |
45 | | - for (Object k: keys) { |
46 | | - a.putAll(k.toString(), values); |
47 | | - } |
48 | | - |
49 | | - prev = cursor.next(); |
50 | | - if (prev==null || !prev.overlaps(a, recordIdField)) break; |
51 | | - } |
52 | | - |
53 | | - return a; |
54 | | - } |
55 | | - |
56 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureMapping.java |
— | — | @@ -1,29 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import java.util.Collection; |
5 | | - |
6 | | -import de.brightbyte.abstraction.MultiMapAbstractor; |
7 | | -import de.brightbyte.abstraction.PropertyAccessor; |
8 | | -import de.brightbyte.data.Functor; |
9 | | -import de.brightbyte.data.Functors; |
10 | | -import de.brightbyte.wikiword.integrator.FeatureSetSourceDescriptor; |
11 | | - |
12 | | -public class FeatureMapping extends PropertyMapping<FeatureSet> { |
13 | | - public <T>void addMapping(String field, String feature, Class<T> type, Functor<?, ? extends Collection<?>> aggregator) { |
14 | | - PropertyAccessor<FeatureSet, T> accessor; |
15 | | - |
16 | | - if (aggregator==null) accessor = FeatureSets.fieldAccessor(feature, type); |
17 | | - else accessor = (PropertyAccessor<FeatureSet, T>)(Object)MultiMapAbstractor.accessor(feature, (Functor<T, ? extends Collection<T>>)aggregator, type); |
18 | | - |
19 | | - addMapping(field, accessor); |
20 | | - } |
21 | | - |
22 | | - public <T>void addMapping(String field, FeatureSetSourceDescriptor source, String option, Class<T> type, Functor<?, ? extends Collection<?>> aggregator) { |
23 | | - String feature = source.getTweak(option, null); |
24 | | - if (feature!=null) addMapping(field, feature, type, aggregator); |
25 | | - } |
26 | | - |
27 | | - public void addMapping(String field, String feature) { |
28 | | - addMapping(field, feature, Object.class, Functors.<String>firstElement()); |
29 | | - } |
30 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/CollapsingFeatureSetCursor.java |
— | — | @@ -1,42 +0,0 @@ |
2 | | -package de.brightbyte.wikiword.integrator.data; |
3 | | - |
4 | | -import de.brightbyte.data.cursor.DataCursor; |
5 | | -import de.brightbyte.util.PersistenceException; |
6 | | - |
7 | | -public class CollapsingFeatureSetCursor implements DataCursor<FeatureSet> { |
8 | | - |
9 | | - protected DataCursor<FeatureSet> cursor; |
10 | | - protected FeatureSet prev; |
11 | | - |
12 | | - protected String recordIdField; |
13 | | - |
14 | | - public CollapsingFeatureSetCursor(DataCursor<FeatureSet> cursor, String sourceKeyField) { |
15 | | - if (cursor==null) throw new NullPointerException(); |
16 | | - if (sourceKeyField==null) throw new NullPointerException(); |
17 | | - |
18 | | - this.cursor = cursor; |
19 | | - this.recordIdField = sourceKeyField; |
20 | | - } |
21 | | - |
22 | | - public void close() { |
23 | | - cursor.close(); |
24 | | - } |
25 | | - |
26 | | - public FeatureSet next() throws PersistenceException { |
27 | | - if (prev==null) prev = cursor.next(); |
28 | | - if (prev==null) return null; |
29 | | - |
30 | | - FeatureSet a = prev; |
31 | | - |
32 | | - while (true) { |
33 | | - prev = cursor.next(); |
34 | | - if (prev==null) break; |
35 | | - |
36 | | - if (!prev.overlaps(a, recordIdField)) break; |
37 | | - a = FeatureSets.merge(a, prev); |
38 | | - } |
39 | | - |
40 | | - return a; |
41 | | - } |
42 | | - |
43 | | -} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureSet.java |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
4 | | -import java.util.List; |
| 4 | +import java.util.Collection; |
5 | 5 | |
6 | 6 | import de.brightbyte.data.LabeledVector; |
7 | 7 | |
— | — | @@ -15,7 +15,7 @@ |
16 | 16 | |
17 | 17 | public <V>void addFeature(String key, V value, Record qualifiers); |
18 | 18 | |
19 | | - public <V>List<? extends Feature<? extends V>> getFeatures(String key); |
| 19 | + public <V>Collection<? extends Feature<? extends V>> getFeatures(String key); |
20 | 20 | |
21 | 21 | public Iterable<String> keys(); |
22 | 22 | |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/AggregatingAssociationCursor.java |
— | — | @@ -0,0 +1,47 @@ |
| 2 | +package de.brightbyte.wikiword.integrator.data; |
| 3 | + |
| 4 | +import de.brightbyte.data.cursor.DataCursor; |
| 5 | +import de.brightbyte.util.PersistenceException; |
| 6 | + |
| 7 | +public class AggregatingAssociationCursor implements DataCursor<Association> { |
| 8 | + |
| 9 | + protected DataCursor<Association> cursor; |
| 10 | + protected Association prev; |
| 11 | + |
| 12 | + protected String sourceKeyField; |
| 13 | + protected String targetKeyField; |
| 14 | + |
| 15 | + public AggregatingAssociationCursor(DataCursor<Association> cursor, String sourceKeyField, String targetKeyField) { |
| 16 | + if (cursor==null) throw new NullPointerException(); |
| 17 | + if (sourceKeyField==null) throw new NullPointerException(); |
| 18 | + if (targetKeyField==null) throw new NullPointerException(); |
| 19 | + |
| 20 | + this.cursor = cursor; |
| 21 | + this.sourceKeyField = sourceKeyField; |
| 22 | + this.targetKeyField = targetKeyField; |
| 23 | + } |
| 24 | + |
| 25 | + public void close() { |
| 26 | + cursor.close(); |
| 27 | + } |
| 28 | + |
| 29 | + public Association next() throws PersistenceException { |
| 30 | + if (prev==null) prev = cursor.next(); |
| 31 | + if (prev==null) return null; |
| 32 | + |
| 33 | + Association a = prev; |
| 34 | + |
| 35 | + while (true) { |
| 36 | + prev = cursor.next(); |
| 37 | + if (prev==null) break; |
| 38 | + |
| 39 | + if (!prev.getSourceItem().overlaps(a.getSourceItem(), sourceKeyField)) break; |
| 40 | + if (!prev.getTargetItem().overlaps(a.getTargetItem(), targetKeyField)) break; |
| 41 | + |
| 42 | + a = Association.merge(a, prev); |
| 43 | + } |
| 44 | + |
| 45 | + return a; |
| 46 | + } |
| 47 | + |
| 48 | +} |
Property changes on: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/AggregatingAssociationCursor.java |
___________________________________________________________________ |
Added: svn:mergeinfo |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureSets.java |
— | — | @@ -1,5 +1,6 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
| 4 | +import java.util.Collection; |
4 | 5 | import java.util.List; |
5 | 6 | |
6 | 7 | import de.brightbyte.abstraction.AbstractedAccessor; |
— | — | @@ -28,7 +29,7 @@ |
29 | 30 | return f; |
30 | 31 | } |
31 | 32 | |
32 | | - public static <T>LabeledVector<T> histogram(List<? extends Feature<? extends T>> list) { |
| 33 | + public static <T>LabeledVector<T> histogram(Collection<? extends Feature<? extends T>> list) { |
33 | 34 | LabeledVector<T> v = new MapLabeledVector<T>(); |
34 | 35 | |
35 | 36 | for (FeatureSet.Feature<? extends T> f: list) { |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/PropertyMapping.java |
— | — | @@ -1,76 +1,21 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
4 | | -import java.sql.Blob; |
5 | | -import java.sql.Clob; |
6 | | -import java.util.Collections; |
7 | | -import java.util.HashMap; |
8 | | -import java.util.Map; |
9 | | - |
10 | 4 | import de.brightbyte.abstraction.PropertyAccessor; |
11 | | -import de.brightbyte.db.DatabaseUtil; |
12 | 5 | |
13 | | -public class PropertyMapping<R> { |
14 | | - protected Map<String, PropertyAccessor<R, ?>> accessors = new HashMap<String, PropertyAccessor<R, ?>>(); |
15 | | - |
16 | | - public PropertyMapping() { |
17 | | - |
18 | | - } |
19 | | - |
20 | | - public String toString() { |
21 | | - return accessors.toString(); |
22 | | - } |
23 | | - |
24 | | - public void addMapping(String field, PropertyAccessor<R, ?> accessor) { |
25 | | - accessors.put(field, accessor); |
26 | | - } |
| 6 | +public interface PropertyMapping<R> { |
| 7 | + public void addMapping(String field, PropertyAccessor<R, ?> accessor); |
27 | 8 | |
28 | | - public void assertAccessor(String field) { |
29 | | - if (!hasAccessor(field)) throw new IllegalArgumentException("Mapping must provide a feature name for "+field); |
30 | | - } |
| 9 | + public void assertAccessor(String field); |
31 | 10 | |
32 | | - public boolean hasAccessor(String field) { |
33 | | - return accessors.containsKey(field); |
34 | | - } |
| 11 | + public boolean hasAccessor(String field); |
35 | 12 | |
36 | | - public PropertyAccessor<R, ?> getAccessor(String field) { |
37 | | - return accessors.get(field); |
38 | | - } |
| 13 | + public PropertyAccessor<R, ?> getAccessor(String field); |
39 | 14 | |
40 | | - public <T> T requireValue(R row, String field, Class<T> type) { |
41 | | - T v = getValue(row, field, type); |
42 | | - |
43 | | - if (v==null) { |
44 | | - if (!hasAccessor(field)) throw new IllegalArgumentException("no accessor for "+field); |
45 | | - else throw new IllegalArgumentException("no value for "+field+" using "+getAccessor(field)); |
46 | | - } |
47 | | - |
48 | | - return v; |
49 | | - } |
| 15 | + public <T> T requireValue(R row, String field, Class<T> type); |
50 | 16 | |
51 | | - public <T> T getValue(R row, String field, Class<T> type) { |
52 | | - return getValue(row, field, type, null); |
53 | | - } |
| 17 | + public <T> T getValue(R row, String field, Class<T> type); |
54 | 18 | |
55 | | - public <T> T getValue(R row, String field, Class<T> type, T def) { |
56 | | - |
57 | | - PropertyAccessor<R, ?> accessor = getAccessor(field); |
58 | | - if (accessor==null) throw new IllegalArgumentException("no accessor defined for field "+field); |
59 | | - |
60 | | - Object v = accessor.getValue(row); |
61 | | - if (v==null) return def; |
62 | | - |
63 | | - if (type==null) { |
64 | | - if (v instanceof byte[] || v instanceof char[] || v instanceof Clob || v instanceof Blob) { //XXX: UGLY HACK! |
65 | | - type = (Class<T>)String.class; |
66 | | - } else { |
67 | | - type = ((PropertyAccessor<R, T>)accessor).getType(); |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - return DatabaseUtil.as(v, type); //NOTE: convert if necessary //XXX: charset... |
72 | | - } |
| 19 | + public <T> T getValue(R row, String field, Class<T> type, T def); |
73 | 20 | |
74 | | - public Iterable<String> fields() { |
75 | | - return Collections.unmodifiableSet(accessors.keySet()); |
76 | | - } |
| 21 | + public Iterable<String> fields(); |
77 | 22 | } |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureBuilderCursor.java |
— | — | @@ -0,0 +1,58 @@ |
| 2 | +package de.brightbyte.wikiword.integrator.data; |
| 3 | + |
| 4 | +import de.brightbyte.data.cursor.DataCursor; |
| 5 | +import de.brightbyte.util.PersistenceException; |
| 6 | + |
| 7 | +public class FeatureBuilderCursor<R> implements DataCursor<FeatureSet> { |
| 8 | + |
| 9 | + protected DataCursor<R> cursor; |
| 10 | + protected R prev; |
| 11 | + |
| 12 | + protected String recordIdField; |
| 13 | + protected FeatureBuilder<R> mapping; |
| 14 | + |
| 15 | + public FeatureBuilderCursor(DataCursor<R> cursor, FeatureBuilder<R> mapping, String recordIdField) { |
| 16 | + if (cursor==null) throw new NullPointerException(); |
| 17 | + if (mapping==null) throw new NullPointerException(); |
| 18 | + |
| 19 | + this.cursor = cursor; |
| 20 | + this.mapping = mapping; |
| 21 | + this.recordIdField = recordIdField; |
| 22 | + } |
| 23 | + |
| 24 | + public void close() { |
| 25 | + cursor.close(); |
| 26 | + } |
| 27 | + |
| 28 | + public FeatureSet next() throws PersistenceException { |
| 29 | + if (prev==null) prev = cursor.next(); |
| 30 | + if (prev==null) return null; |
| 31 | + |
| 32 | + FeatureSet f = new DefaultFeatureSet(); |
| 33 | + Object id = null; |
| 34 | + |
| 35 | + if (recordIdField!=null) { |
| 36 | + id = mapping.requireValue(prev, recordIdField, Object.class); |
| 37 | + if (id==null) throw new PersistenceException("id field "+id+" must have non-null value!"); |
| 38 | + } |
| 39 | + |
| 40 | + while (prev!=null) { |
| 41 | + mapping.addFeatures(prev, f); |
| 42 | + |
| 43 | + prev = cursor.next(); |
| 44 | + if (prev==null) break; |
| 45 | + |
| 46 | + if (recordIdField!=null) { |
| 47 | + Object nextId = mapping.requireValue(prev, recordIdField, Object.class); |
| 48 | + if (nextId==null) throw new PersistenceException("id field "+id+" must have non-null value!"); |
| 49 | + |
| 50 | + if (!nextId.equals(id)) break; |
| 51 | + } else { |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return f; |
| 57 | + } |
| 58 | + |
| 59 | +} |
Property changes on: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureBuilderCursor.java |
___________________________________________________________________ |
Added: svn:mergeinfo |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureBuilder.java |
— | — | @@ -0,0 +1,92 @@ |
| 2 | +package de.brightbyte.wikiword.integrator.data; |
| 3 | + |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import de.brightbyte.abstraction.PropertyAccessor; |
| 8 | + |
| 9 | +public class FeatureBuilder<R> { |
| 10 | + |
| 11 | + protected PropertyMapping<R> dataMapping; |
| 12 | + |
| 13 | + protected Map<String, PropertyMapping<R>> qualifierMappings = new HashMap<String, PropertyMapping<R>>(); |
| 14 | + |
| 15 | + public FeatureBuilder() { |
| 16 | + this(new DefaultPropertyMapping<R>()); |
| 17 | + } |
| 18 | + |
| 19 | + public FeatureBuilder(PropertyMapping<R> dataMapping) { |
| 20 | + if (dataMapping==null) throw new NullPointerException(); |
| 21 | + this.dataMapping = dataMapping; |
| 22 | + } |
| 23 | + |
| 24 | + public PropertyMapping<R> getQualifierMappings(String field) { |
| 25 | + return qualifierMappings.get(field); |
| 26 | + } |
| 27 | + |
| 28 | + public void addMapping(String field, PropertyAccessor<R, ?> accessor, PropertyMapping<R> qualifiers) { |
| 29 | + dataMapping.addMapping(field, accessor); |
| 30 | + if (qualifiers!=null) qualifierMappings.put(field, qualifiers); |
| 31 | + } |
| 32 | + |
| 33 | + public void addFeatures(R rec, FeatureSet features) { |
| 34 | + for(String field: this.fields()) { |
| 35 | + addFeature(rec, field, features); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public void addFeature(R rec, String field, FeatureSet features) { |
| 40 | + PropertyAccessor<R, ?> accessor = getAccessor(field); |
| 41 | + if (accessor==null) throw new IllegalArgumentException("unknown field: "+field); |
| 42 | + |
| 43 | + PropertyMapping<R> qm = getQualifierMappings(field); |
| 44 | + DefaultRecord qualifiers = null; |
| 45 | + |
| 46 | + Object v = accessor.getValue(rec); |
| 47 | + if (v==null) return; //XXX: always?! |
| 48 | + |
| 49 | + if (qm != null) { |
| 50 | + qualifiers = new DefaultRecord(); |
| 51 | + addFields(rec, qm, qualifiers); |
| 52 | + } |
| 53 | + |
| 54 | + features.addFeature(field, v, qualifiers); |
| 55 | + } |
| 56 | + |
| 57 | + private static <R>void addFields(R rec, PropertyMapping<R> qualifierMappings, Record qualifiers) { |
| 58 | + for(String q: qualifierMappings.fields()) { |
| 59 | + PropertyAccessor<R, ?> accessor = qualifierMappings.getAccessor(q); |
| 60 | + Object v = accessor.getValue(rec); |
| 61 | + qualifiers.add(q, v); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void assertAccessor(String field) { |
| 66 | + dataMapping.assertAccessor(field); |
| 67 | + } |
| 68 | + |
| 69 | + public Iterable<String> fields() { |
| 70 | + return dataMapping.fields(); |
| 71 | + } |
| 72 | + |
| 73 | + public PropertyAccessor<R, ?> getAccessor(String field) { |
| 74 | + return dataMapping.getAccessor(field); |
| 75 | + } |
| 76 | + |
| 77 | + public <T> T getValue(R row, String field, Class<T> type, T def) { |
| 78 | + return dataMapping.getValue(row, field, type, def); |
| 79 | + } |
| 80 | + |
| 81 | + public <T> T getValue(R row, String field, Class<T> type) { |
| 82 | + return dataMapping.getValue(row, field, type); |
| 83 | + } |
| 84 | + |
| 85 | + public boolean hasAccessor(String field) { |
| 86 | + return dataMapping.hasAccessor(field); |
| 87 | + } |
| 88 | + |
| 89 | + public <T> T requireValue(R row, String field, Class<T> type) { |
| 90 | + return dataMapping.requireValue(row, field, type); |
| 91 | + } |
| 92 | + |
| 93 | +} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/DefaultPropertyMapping.java |
— | — | @@ -0,0 +1,76 @@ |
| 2 | +package de.brightbyte.wikiword.integrator.data; |
| 3 | + |
| 4 | +import java.sql.Blob; |
| 5 | +import java.sql.Clob; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import de.brightbyte.abstraction.PropertyAccessor; |
| 11 | +import de.brightbyte.db.DatabaseUtil; |
| 12 | + |
| 13 | +public class DefaultPropertyMapping<R> implements PropertyMapping<R>{ |
| 14 | + protected Map<String, PropertyAccessor<R, ?>> accessors = new HashMap<String, PropertyAccessor<R, ?>>(); |
| 15 | + |
| 16 | + public DefaultPropertyMapping() { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + public String toString() { |
| 21 | + return accessors.toString(); |
| 22 | + } |
| 23 | + |
| 24 | + public void addMapping(String field, PropertyAccessor<R, ?> accessor) { |
| 25 | + accessors.put(field, accessor); |
| 26 | + } |
| 27 | + |
| 28 | + public void assertAccessor(String field) { |
| 29 | + if (!hasAccessor(field)) throw new IllegalArgumentException("Mapping must provide a feature name for "+field); |
| 30 | + } |
| 31 | + |
| 32 | + public boolean hasAccessor(String field) { |
| 33 | + return accessors.containsKey(field); |
| 34 | + } |
| 35 | + |
| 36 | + public PropertyAccessor<R, ?> getAccessor(String field) { |
| 37 | + return accessors.get(field); |
| 38 | + } |
| 39 | + |
| 40 | + public <T> T requireValue(R row, String field, Class<T> type) { |
| 41 | + T v = getValue(row, field, type); |
| 42 | + |
| 43 | + if (v==null) { |
| 44 | + if (!hasAccessor(field)) throw new IllegalArgumentException("no accessor for "+field); |
| 45 | + else throw new IllegalArgumentException("no value for "+field+" using "+getAccessor(field)); |
| 46 | + } |
| 47 | + |
| 48 | + return v; |
| 49 | + } |
| 50 | + |
| 51 | + public <T> T getValue(R row, String field, Class<T> type) { |
| 52 | + return getValue(row, field, type, null); |
| 53 | + } |
| 54 | + |
| 55 | + public <T> T getValue(R row, String field, Class<T> type, T def) { |
| 56 | + |
| 57 | + PropertyAccessor<R, ?> accessor = getAccessor(field); |
| 58 | + if (accessor==null) throw new IllegalArgumentException("no accessor defined for field "+field); |
| 59 | + |
| 60 | + Object v = accessor.getValue(row); |
| 61 | + if (v==null) return def; |
| 62 | + |
| 63 | + if (type==null) { |
| 64 | + if (v instanceof byte[] || v instanceof char[] || v instanceof Clob || v instanceof Blob) { //XXX: UGLY HACK! |
| 65 | + type = (Class<T>)String.class; |
| 66 | + } else { |
| 67 | + type = ((PropertyAccessor<R, T>)accessor).getType(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return DatabaseUtil.as(v, type); //NOTE: convert if necessary //XXX: charset... |
| 72 | + } |
| 73 | + |
| 74 | + public Iterable<String> fields() { |
| 75 | + return Collections.unmodifiableSet(accessors.keySet()); |
| 76 | + } |
| 77 | +} |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/AssociationCursor.java |
— | — | @@ -1,63 +1,40 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
4 | | -import java.util.Arrays; |
5 | | -import java.util.List; |
6 | | - |
7 | 4 | import de.brightbyte.data.cursor.DataCursor; |
8 | | -import de.brightbyte.util.CollectionUtils; |
9 | 5 | import de.brightbyte.util.PersistenceException; |
10 | 6 | |
11 | 7 | public class AssociationCursor implements DataCursor<Association> { |
12 | 8 | |
13 | | - private DataCursor<FeatureSet> source; |
| 9 | + private DataCursor<Record> source; |
14 | 10 | |
15 | | - protected Iterable<String> foreignFields; |
16 | | - protected Iterable<String> conceptFields; |
17 | | - protected Iterable<String> propertyFields; |
| 11 | + protected String foreignAuthorityField; |
| 12 | + protected String foreignIdField; |
| 13 | + protected String foreignNameField; |
| 14 | + protected String conceptIdField; |
| 15 | + protected String conceptNameField; |
18 | 16 | |
19 | | - public AssociationCursor(DataCursor<FeatureSet> source, String[] sourceFields, String[] targetFields, String[] propertyFields) { |
20 | | - this(source, |
21 | | - Arrays.asList(sourceFields), |
22 | | - Arrays.asList(targetFields), |
23 | | - Arrays.asList(propertyFields)); |
| 17 | + public AssociationCursor(DataCursor<Record> source, String foreignAuthorityField, String foreignIdField, String foreignNameField, String conceptIdField, String conceptNameField) { |
| 18 | + super(); |
| 19 | + this.source = source; |
| 20 | + this.foreignAuthorityField = foreignAuthorityField; |
| 21 | + this.foreignIdField = foreignIdField; |
| 22 | + this.foreignNameField = foreignNameField; |
| 23 | + this.conceptIdField = conceptIdField; |
| 24 | + this.conceptNameField = conceptNameField; |
24 | 25 | } |
25 | | - |
26 | | - public AssociationCursor(DataCursor<FeatureSet> source, Iterable<String> sourceFields, Iterable<String> targetFields, Iterable<String> propertyFields) { |
27 | | - if (source==null) throw new NullPointerException(); |
28 | | - this.source = source; |
29 | | - //FIXME: use FeatureMappings or at least lists of accessors! |
30 | | - this.foreignFields = CollectionUtils.toCleanIterable(sourceFields, false, false); |
31 | | - this.conceptFields = CollectionUtils.toCleanIterable(targetFields, false, false); |
32 | | - this.propertyFields = CollectionUtils.toCleanIterable(propertyFields, false, false); |
33 | | - } |
34 | 26 | |
35 | 27 | public Association next() throws PersistenceException { |
36 | | - FeatureSet row = source.next(); |
| 28 | + Record row = source.next(); |
37 | 29 | if (row==null) return null; |
38 | 30 | |
39 | 31 | return newAssociation(row); |
40 | 32 | } |
41 | 33 | |
42 | | - public Association newAssociation(FeatureSet row) throws PersistenceException { |
43 | | - FeatureSet source = foreignFields==null ? row : newFeatureSet(row, foreignFields); |
44 | | - FeatureSet target = conceptFields==null ? row : newFeatureSet(row, conceptFields); |
45 | | - FeatureSet props = propertyFields==null ? row : newFeatureSet(row, propertyFields); |
| 34 | + public Association newAssociation(Record row) throws PersistenceException { |
46 | 35 | |
47 | | - return new Association(source, target, props); |
| 36 | + return new DefaultAssociation(row, foreignAuthorityField, foreignIdField, foreignNameField, conceptIdField, conceptNameField); |
48 | 37 | } |
49 | 38 | |
50 | | - protected FeatureSet newFeatureSet(FeatureSet row, Iterable<String> fields) { |
51 | | - FeatureSet m = new DefaultFeatureSet(); |
52 | | - |
53 | | - for (String f: fields) { |
54 | | - if (f==null) continue; |
55 | | - List<Object> values = row.get(f); |
56 | | - m.putAll(f, values); |
57 | | - } |
58 | | - |
59 | | - return m; |
60 | | - } |
61 | | - |
62 | 39 | public void close() { |
63 | 40 | source.close(); |
64 | 41 | } |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/DefaultFeatureSet.java |
— | — | @@ -1,9 +1,10 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
4 | | -import java.util.List; |
| 4 | +import java.util.Collection; |
5 | 5 | |
6 | 6 | import de.brightbyte.data.LabeledVector; |
7 | | -import de.brightbyte.data.ValueListMultiMap; |
| 7 | +import de.brightbyte.data.MultiMap; |
| 8 | +import de.brightbyte.data.ValueSetMultiMap; |
8 | 9 | |
9 | 10 | public class DefaultFeatureSet implements FeatureSet { |
10 | 11 | |
— | — | @@ -59,7 +60,8 @@ |
60 | 61 | |
61 | 62 | } |
62 | 63 | |
63 | | - protected ValueListMultiMap<String, FeatureSet.Feature<?>> data = new ValueListMultiMap<String, FeatureSet.Feature<?>>(); |
| 64 | + protected MultiMap<String, FeatureSet.Feature<?>, ? extends Collection<? extends FeatureSet.Feature<?>>> data |
| 65 | + = new ValueSetMultiMap<String, FeatureSet.Feature<?>>(); |
64 | 66 | |
65 | 67 | public DefaultFeatureSet() { |
66 | 68 | } |
— | — | @@ -69,8 +71,8 @@ |
70 | 72 | } |
71 | 73 | |
72 | 74 | public boolean overlaps(FeatureSet item, String feature) { |
73 | | - List<? extends FeatureSet.Feature<?>> a = getFeatures(feature); |
74 | | - List<? extends FeatureSet.Feature<?>> b = item.getFeatures(feature); |
| 75 | + Collection<? extends FeatureSet.Feature<?>> a = getFeatures(feature); |
| 76 | + Collection<? extends FeatureSet.Feature<?>> b = item.getFeatures(feature); |
75 | 77 | |
76 | 78 | for (Object obj: a) { |
77 | 79 | if (b.contains(obj)) return true; |
— | — | @@ -80,7 +82,7 @@ |
81 | 83 | } |
82 | 84 | |
83 | 85 | public LabeledVector<Object> getHistogram(String key) { |
84 | | - List<? extends Feature<? extends Object>> list = this.<Object>getFeatures(key); |
| 86 | + Collection<? extends Feature<? extends Object>> list = this.<Object>getFeatures(key); |
85 | 87 | return FeatureSets.<Object>histogram(list); |
86 | 88 | } |
87 | 89 | |
— | — | @@ -104,9 +106,9 @@ |
105 | 107 | |
106 | 108 | } |
107 | 109 | |
108 | | - public <V> List<? extends Feature<? extends V>> getFeatures(String key) { |
109 | | - List<FeatureSet.Feature<?>> features = data.get(key); |
110 | | - return (List<? extends Feature<? extends V>>)features; //XXX: unmodifiable?! |
| 110 | + public <V> Collection<? extends Feature<? extends V>> getFeatures(String key) { |
| 111 | + Collection<FeatureSet.Feature<?>> features = data.get(key); |
| 112 | + return (Collection<? extends Feature<? extends V>>)features; //XXX: unmodifiable?! |
111 | 113 | } |
112 | 114 | |
113 | 115 | public Iterable<String> keys() { |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/DefaultAssociation.java |
— | — | @@ -1,62 +1,31 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
4 | 4 | |
5 | | -public class DefaultAssociation extends DefaultRecord implements ConceptEntityRecord{ |
| 5 | +public class DefaultAssociation extends DefaultRecord implements Association { |
6 | 6 | |
7 | | - protected String idField; |
8 | | - protected String nameField; |
| 7 | + protected ConceptEntityRecord conceptEntity; |
| 8 | + protected ForeignEntityRecord foreignEntity; |
9 | 9 | |
10 | | - public DefaultAssociation(String idField, String nameField, FeatureSet properties) { |
11 | | - if (idField==null) throw new NullPointerException(); |
12 | | - if (nameField==null) nameField = idField; |
13 | | - if (properties==null) properties = new DefaultFeatureSet(); |
| 10 | + public DefaultAssociation(Record data, |
| 11 | + String foreignAuthorityField, String foreignIdField, String foreignNameField, |
| 12 | + String conceptIdField, String conceptNameField) { |
14 | 13 | |
15 | | - this.idField = idField; |
16 | | - this.nameField = nameField; |
| 14 | + addAll(data); |
| 15 | + |
| 16 | + foreignEntity = new DefaultForeignEntityRecord(this.data, foreignAuthorityField, foreignIdField, foreignNameField); |
| 17 | + conceptEntity = new DefaultConceptEntityRecord(this.data, conceptIdField, conceptNameField); |
17 | 18 | } |
18 | 19 | |
19 | | - public String getID() { |
20 | | - return idField==null ? null : getStringProperty(idField); |
| 20 | + public ConceptEntityRecord getConceptEntity() { |
| 21 | + return conceptEntity; |
21 | 22 | } |
22 | 23 | |
23 | | - public String getName() { |
24 | | - return nameField==null ? null : getStringProperty(nameField); |
| 24 | + public ForeignEntityRecord getForeignEntity() { |
| 25 | + return foreignEntity; |
25 | 26 | } |
26 | | - |
27 | | - private String getStringProperty(String field) { |
28 | | - Object v = get(field); |
29 | | - return String.valueOf(v); |
| 27 | + |
| 28 | + public String toString() { |
| 29 | + return foreignEntity + " <-> " + conceptEntity; |
30 | 30 | } |
31 | 31 | |
32 | | - @Override |
33 | | - public int hashCode() { |
34 | | - final int PRIME = 31; |
35 | | - int result = super.hashCode(); |
36 | | - result = PRIME * result + ((idField == null) ? 0 : idField.hashCode()); |
37 | | - result = PRIME * result + ((nameField == null) ? 0 : nameField.hashCode()); |
38 | | - return result; |
39 | | - } |
40 | | - |
41 | | - @Override |
42 | | - public boolean equals(Object obj) { |
43 | | - if (this == obj) |
44 | | - return true; |
45 | | - if (obj == null) |
46 | | - return false; |
47 | | - if (getClass() != obj.getClass()) |
48 | | - return false; |
49 | | - final DefaultAssociation other = (DefaultAssociation) obj; |
50 | | - if (idField == null) { |
51 | | - if (other.idField != null) |
52 | | - return false; |
53 | | - } else if (!idField.equals(other.idField)) |
54 | | - return false; |
55 | | - if (nameField == null) { |
56 | | - if (other.nameField != null) |
57 | | - return false; |
58 | | - } else if (!nameField.equals(other.nameField)) |
59 | | - return false; |
60 | | - return super.equals(obj); |
61 | | - } |
62 | | - |
63 | 32 | } |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/DefaultForeignEntityFeatureSet.java |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | package de.brightbyte.wikiword.integrator.data; |
3 | 3 | |
4 | | -import java.util.List; |
| 4 | +import java.util.Collection; |
5 | 5 | |
6 | 6 | |
7 | 7 | public class DefaultForeignEntityFeatureSet extends DefaultFeatureSet implements ForeignEntityFeatureSet { |
— | — | @@ -32,9 +32,10 @@ |
33 | 33 | } |
34 | 34 | |
35 | 35 | private String getStringProperty(String field) { |
36 | | - List<? extends Feature<? extends Object>> features = getFeatures(field); |
| 36 | + Collection<? extends Feature<? extends Object>> features = getFeatures(field); |
37 | 37 | if (features==null || features.isEmpty()) return null; |
38 | | - Object v = features.iterator().next().getValue(); |
| 38 | + Feature<? extends Object> f = features.iterator().next(); |
| 39 | + Object v = f.getValue(); |
39 | 40 | return v==null ? null : String.valueOf(v); |
40 | 41 | } |
41 | 42 | |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureAssemblingCursor.java |
— | — | @@ -0,0 +1,80 @@ |
| 2 | +package de.brightbyte.wikiword.integrator.data; |
| 3 | + |
| 4 | +import de.brightbyte.abstraction.PropertyAccessor; |
| 5 | +import de.brightbyte.data.cursor.DataCursor; |
| 6 | +import de.brightbyte.util.PersistenceException; |
| 7 | + |
| 8 | +public class FeatureAssemblingCursor<R> implements DataCursor<FeatureSet> { |
| 9 | + |
| 10 | + protected DataCursor<R> cursor; |
| 11 | + protected PropertyMapping<R> qualfierMapping = null; |
| 12 | + protected R prev; |
| 13 | + |
| 14 | + protected PropertyAccessor<R, Object> recordIdAccessor; |
| 15 | + protected PropertyAccessor<R, String> propertyNameAccessor; |
| 16 | + protected PropertyAccessor<R, Object> propertyValueAccessor; |
| 17 | + |
| 18 | + public FeatureAssemblingCursor(DataCursor<R> cursor, |
| 19 | + PropertyAccessor<R, Object> recordIdAccessor, |
| 20 | + PropertyAccessor<R, String> propertyNameAccessor, |
| 21 | + PropertyAccessor<R, Object> propertyValueAccessor) { |
| 22 | + if (cursor==null) throw new NullPointerException(); |
| 23 | + if (recordIdAccessor==null) throw new NullPointerException(); |
| 24 | + if (propertyNameAccessor==null) throw new NullPointerException(); |
| 25 | + if (propertyValueAccessor==null) throw new NullPointerException(); |
| 26 | + |
| 27 | + this.cursor = cursor; |
| 28 | + this.recordIdAccessor = recordIdAccessor; |
| 29 | + this.propertyNameAccessor = propertyNameAccessor; |
| 30 | + this.propertyValueAccessor = propertyValueAccessor; |
| 31 | + } |
| 32 | + |
| 33 | + public void setQualifierMapping(PropertyMapping<R> qm) { |
| 34 | + this.qualfierMapping = qm; |
| 35 | + } |
| 36 | + |
| 37 | + public void close() { |
| 38 | + cursor.close(); |
| 39 | + } |
| 40 | + |
| 41 | + public FeatureSet next() throws PersistenceException { |
| 42 | + if (prev==null) prev = cursor.next(); |
| 43 | + if (prev==null) return null; |
| 44 | + |
| 45 | + FeatureSet f = new DefaultFeatureSet(); |
| 46 | + Object currentId = recordIdAccessor.getValue(prev); |
| 47 | + f.addFeature(recordIdAccessor.getName(), currentId, null); |
| 48 | + |
| 49 | + while (prev!=null) { |
| 50 | + String key = propertyNameAccessor.getValue(prev); |
| 51 | + Object value = propertyValueAccessor.getValue(prev); |
| 52 | + |
| 53 | + Record qualifiers = getQualifiers(prev); |
| 54 | + |
| 55 | + f.addFeature(key, value, qualifiers); |
| 56 | + |
| 57 | + prev = cursor.next(); |
| 58 | + Object id = recordIdAccessor.getValue(prev); |
| 59 | + if (prev==null || !currentId.equals(id)) break; |
| 60 | + } |
| 61 | + |
| 62 | + return f; |
| 63 | + } |
| 64 | + |
| 65 | + private Record getQualifiers(R rec) { |
| 66 | + if (qualfierMapping==null) return null; |
| 67 | + |
| 68 | + Record qualifiers = null; |
| 69 | + |
| 70 | + Iterable<String> fields = qualfierMapping.fields(); |
| 71 | + for (String field: fields) { |
| 72 | + Object value = qualfierMapping.getValue(rec, field, null); |
| 73 | + if (qualifiers==null) qualifiers = new DefaultRecord(); |
| 74 | + qualifiers.add(field, value); |
| 75 | + } |
| 76 | + |
| 77 | + return qualifiers; |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
\ No newline at end of file |
Property changes on: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/data/FeatureAssemblingCursor.java |
___________________________________________________________________ |
Added: svn:mergeinfo |
Index: trunk/WikiWord/WikiWordIntegrator/src/main/java/de/brightbyte/wikiword/integrator/AbstractIntegratorApp.java |
— | — | @@ -34,7 +34,7 @@ |
35 | 35 | import de.brightbyte.wikiword.StoreBackedApp; |
36 | 36 | import de.brightbyte.wikiword.TweakSet; |
37 | 37 | import de.brightbyte.wikiword.builder.InputFileHelper; |
38 | | -import de.brightbyte.wikiword.integrator.data.AssemblingFeatureSetCursor; |
| 38 | +import de.brightbyte.wikiword.integrator.data.FeatureAssemblingCursor; |
39 | 39 | import de.brightbyte.wikiword.integrator.data.Association; |
40 | 40 | import de.brightbyte.wikiword.integrator.data.AssociationCursor; |
41 | 41 | import de.brightbyte.wikiword.integrator.data.FeatureSet; |
— | — | @@ -299,7 +299,7 @@ |
300 | 300 | if (propField!=null) { |
301 | 301 | String valueField = sourceDescriptor.getPropertyValueField(); |
302 | 302 | String subjectField = sourceDescriptor.getPropertySubjectField(); |
303 | | - fsc = new AssemblingFeatureSetCursor(fsc, subjectField, propField, valueField); |
| 303 | + fsc = new FeatureAssemblingCursor(fsc, subjectField, propField, valueField); |
304 | 304 | } |
305 | 305 | |
306 | 306 | FeatureSetMangler mangler = sourceDescriptor.getRowMangler(); |