Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/DatabaseProximityStore.java |
— | — | @@ -0,0 +1,100 @@ |
| 2 | +package de.brightbyte.wikiword.store; |
| 3 | + |
| 4 | +import java.sql.ResultSet; |
| 5 | +import java.sql.SQLException; |
| 6 | + |
| 7 | +import de.brightbyte.data.LabeledVector; |
| 8 | +import de.brightbyte.data.MapLabeledVector; |
| 9 | +import de.brightbyte.data.cursor.DataSet; |
| 10 | +import de.brightbyte.db.EntityTable; |
| 11 | +import de.brightbyte.db.QueryDataSet; |
| 12 | +import de.brightbyte.db.RelationTable; |
| 13 | +import de.brightbyte.util.PersistenceException; |
| 14 | +import de.brightbyte.wikiword.TweakSet; |
| 15 | +import de.brightbyte.wikiword.model.LocalConcept; |
| 16 | +import de.brightbyte.wikiword.model.WikiWordConcept; |
| 17 | +import de.brightbyte.wikiword.model.WikiWordConceptReference; |
| 18 | +import de.brightbyte.wikiword.schema.ProximityStoreSchema; |
| 19 | +import de.brightbyte.wikiword.store.DatabaseWikiWordConceptStore.DatabaseConceptInfoStore.ConceptFactory; |
| 20 | + |
| 21 | +public class DatabaseProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> |
| 22 | + extends DatabaseWikiWordStore |
| 23 | + implements ProximityStore<T, R> { |
| 24 | + |
| 25 | + private DatabaseWikiWordConceptStore conceptStore; |
| 26 | + private RelationTable featureTable; |
| 27 | + private RelationTable proximityTable; |
| 28 | + private EntityTable conceptTable; |
| 29 | + |
| 30 | + protected DatabaseProximityStore(DatabaseWikiWordConceptStore conceptStore, ProximityStoreSchema database, TweakSet tweaks) throws SQLException { |
| 31 | + super(database, tweaks); |
| 32 | + |
| 33 | + this.conceptStore = conceptStore; |
| 34 | + |
| 35 | + this.conceptTable = (EntityTable)conceptStore.getDatabaseAccess().getTable("concept"); |
| 36 | + this.featureTable = (RelationTable)database.getTable("feature"); |
| 37 | + this.proximityTable = (RelationTable)database.getTable("proximity"); |
| 38 | + } |
| 39 | + |
| 40 | + public DataSet<? extends R> getEnvironment(int concept, double minProximity) throws PersistenceException { |
| 41 | + String sql = "SELECT C.*, proximity FROM " + conceptTable.getSQLName() + " as C "; |
| 42 | + sql += " JOIN "+proximityTable.getSQLName()+" as P ON P.concept2 = C.id "; |
| 43 | + sql += " WHERE concept1 = "+concept; |
| 44 | + |
| 45 | + return new QueryDataSet<R>(database, referenceFactory, "getEnvironment", sql, false); |
| 46 | + } |
| 47 | + |
| 48 | + public LabeledVector<Integer> getEnvironmentVector(int concept, double minProximity) throws PersistenceException { |
| 49 | + String sql = "SELECT concept2, proximity FROM " +proximityTable.getSQLName()+" as P "; |
| 50 | + sql += " WHERE concept1 = "+concept; |
| 51 | + |
| 52 | + return readVector("getEnvironmentVector", sql, "concept2", "proximity"); |
| 53 | + } |
| 54 | + |
| 55 | + public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException { |
| 56 | + String sql = "SELECT feature, normal_value FROM " +featureTable.getSQLName()+" as F "; |
| 57 | + sql += " WHERE concept = "+concept; |
| 58 | + |
| 59 | + return readVector("getFeatireVector", sql, "feature", "normal_value"); |
| 60 | + } |
| 61 | + |
| 62 | + protected <K> LabeledVector<K> readVector(String name, String sql, String keyField, String valueField) throws PersistenceException { |
| 63 | + try { |
| 64 | + ResultSet rs = database.executeQuery(name, sql); |
| 65 | + try { |
| 66 | + return readVector(rs, keyField, valueField, new MapLabeledVector<K>()); |
| 67 | + } finally { |
| 68 | + rs.close(); |
| 69 | + } |
| 70 | + } catch (SQLException e) { |
| 71 | + throw new PersistenceException(e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + protected <K> LabeledVector<K> readVector(ResultSet rs, String keyField, String valueField, LabeledVector<K> v) throws SQLException { |
| 76 | + if (v==null) v = new MapLabeledVector<K>(); |
| 77 | + |
| 78 | + while (rs.next()) { |
| 79 | + K k = (K)rs.getObject(keyField); |
| 80 | + Number n = (Number)rs.getObject(valueField); |
| 81 | + |
| 82 | + v.set(k, n.doubleValue()); |
| 83 | + } |
| 84 | + |
| 85 | + return v; |
| 86 | + } |
| 87 | + |
| 88 | + public double getProximity(int concept1, int concept2) throws PersistenceException { |
| 89 | + String sql = "SELECT proximity FROM " +proximityTable.getSQLName()+" as P "; |
| 90 | + sql += " WHERE concept1 = "+concept1; |
| 91 | + sql += " AND concept2 = "+concept2; |
| 92 | + |
| 93 | + try { |
| 94 | + return ((Number)database.executeSingleValueQuery("getProximity", sql)).doubleValue(); |
| 95 | + } catch (SQLException e) { |
| 96 | + throw new PersistenceException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +} |
\ No newline at end of file |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/ProximityStore.java |
— | — | @@ -0,0 +1,19 @@ |
| 2 | +package de.brightbyte.wikiword.store; |
| 3 | + |
| 4 | +import de.brightbyte.data.LabeledVector; |
| 5 | +import de.brightbyte.data.cursor.DataSet; |
| 6 | +import de.brightbyte.util.PersistenceException; |
| 7 | +import de.brightbyte.wikiword.model.WikiWordConcept; |
| 8 | +import de.brightbyte.wikiword.model.WikiWordConceptReference; |
| 9 | + |
| 10 | +public interface ProximityStore<T extends WikiWordConcept, R extends WikiWordConceptReference<T>> { |
| 11 | + |
| 12 | + public LabeledVector<Integer> getFeatureVector(int concept) throws PersistenceException; |
| 13 | + |
| 14 | + public double getProximity(int concept1, int concept2) throws PersistenceException; |
| 15 | + |
| 16 | + public DataSet<? extends R> getEnvironment(int concept, double minProximity) throws PersistenceException; |
| 17 | + |
| 18 | + public LabeledVector<Integer> getEnvironmentVector(int concept, double minProximity) throws PersistenceException; |
| 19 | + |
| 20 | +} |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/store/LocalConceptStore.java |
— | — | @@ -2,7 +2,6 @@ |
3 | 3 | |
4 | 4 | import de.brightbyte.data.cursor.DataSet; |
5 | 5 | import de.brightbyte.util.PersistenceException; |
6 | | -import de.brightbyte.wikiword.Corpus; |
7 | 6 | import de.brightbyte.wikiword.model.LocalConcept; |
8 | 7 | import de.brightbyte.wikiword.model.LocalConceptReference; |
9 | 8 | import de.brightbyte.wikiword.model.TermReference; |
Index: trunk/WikiWord/WikiWord/src/main/java/de/brightbyte/wikiword/schema/ProximityStoreSchema.java |
— | — | @@ -11,6 +11,7 @@ |
12 | 12 | import de.brightbyte.db.KeyType; |
13 | 13 | import de.brightbyte.db.ReferenceField; |
14 | 14 | import de.brightbyte.db.RelationTable; |
| 15 | +import de.brightbyte.util.PersistenceException; |
15 | 16 | import de.brightbyte.wikiword.DatasetIdentifier; |
16 | 17 | import de.brightbyte.wikiword.TweakSet; |
17 | 18 | |
— | — | @@ -97,21 +98,23 @@ |
98 | 99 | protected EntityTable featureMagnitudeTable; |
99 | 100 | protected RelationTable featureProductTable; |
100 | 101 | protected RelationTable proximityTable; |
| 102 | + protected String suffix; |
| 103 | + private TweakSet tweaks; |
| 104 | + private boolean isGlobal; |
| 105 | + private boolean useFlushQueue; |
101 | 106 | |
102 | | - |
103 | | - public ProximityStoreSchema(DatasetIdentifier dataset, Connection connection, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException { |
| 107 | + public ProximityStoreSchema(DatasetIdentifier dataset, Connection connection, String suffix, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException { |
104 | 108 | super(dataset, connection, tweaks, useFlushQueue ); |
105 | | - init(global, tweaks); |
| 109 | + init(global, tweaks, suffix, useFlushQueue); |
106 | 110 | } |
107 | 111 | |
108 | | - public ProximityStoreSchema(DatasetIdentifier dataset, DataSource connectionInfo, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException { |
| 112 | + public ProximityStoreSchema(DatasetIdentifier dataset, DataSource connectionInfo, String suffix, boolean global, TweakSet tweaks, boolean useFlushQueue) throws SQLException { |
109 | 113 | super(dataset, connectionInfo, tweaks, useFlushQueue); |
110 | | - init(global, tweaks); |
| 114 | + init(global, tweaks, suffix, useFlushQueue); |
111 | 115 | } |
112 | 116 | |
113 | | - public RelationTable makeFeatureTable(String suffix) { |
| 117 | + public RelationTable makeFeatureTable() { |
114 | 118 | String name = "feature"; |
115 | | - if (suffix!=null) name = name+suffix; |
116 | 119 | |
117 | 120 | RelationTable featureTable = new RelationTable(this, name, getDefaultTableAttributes()); |
118 | 121 | featureTable.addField( new ReferenceField(this, "concept", "INT", null, true, null, "concept", "id", null )); |
— | — | @@ -124,9 +127,8 @@ |
125 | 128 | return featureTable; |
126 | 129 | } |
127 | 130 | |
128 | | - public RelationTable makeProximityTable(String suffix) { |
| 131 | + public RelationTable makeProximityTable() { |
129 | 132 | String name = "proximity"; |
130 | | - if (suffix!=null) name = name+suffix; |
131 | 133 | |
132 | 134 | RelationTable proximityTable = new RelationTable(this, name, getDefaultTableAttributes()); |
133 | 135 | proximityTable.addField( new ReferenceField(this, "concept1", "INT", null, true, null, "concept", "id", null )); |
— | — | @@ -140,7 +142,7 @@ |
141 | 143 | return proximityTable; |
142 | 144 | } |
143 | 145 | |
144 | | - public EntityTable makeFeatureMagnitudeTable(String suffix) { |
| 146 | + public EntityTable makeFeatureMagnitudeTable() { |
145 | 147 | String name = "feature_magnitude"; |
146 | 148 | if (suffix!=null) name = name+suffix; |
147 | 149 | |
— | — | @@ -152,8 +154,13 @@ |
153 | 155 | return featureMagnitudeTable; |
154 | 156 | } |
155 | 157 | |
156 | | - private void init(boolean global, TweakSet tweaks) { |
157 | | - featureTable = makeFeatureTable(null); |
| 158 | + private void init(boolean global, TweakSet tweaks, String suffix, boolean useFlushQueue) { |
| 159 | + this.suffix = suffix; |
| 160 | + this.tweaks = tweaks; |
| 161 | + this.isGlobal = global; |
| 162 | + this.useFlushQueue = useFlushQueue; |
| 163 | + |
| 164 | + featureTable = makeFeatureTable(); |
158 | 165 | addTable(featureTable); |
159 | 166 | |
160 | 167 | /* |
— | — | @@ -169,12 +176,20 @@ |
170 | 177 | featureProductTable.setAutomaticField(null); |
171 | 178 | addTable(featureProductTable);*/ |
172 | 179 | |
173 | | - featureMagnitudeTable = makeFeatureMagnitudeTable(null); |
| 180 | + featureMagnitudeTable = makeFeatureMagnitudeTable(); |
174 | 181 | addTable(featureMagnitudeTable); |
175 | 182 | |
176 | | - proximityTable = makeProximityTable(null); |
| 183 | + proximityTable = makeProximityTable(); |
177 | 184 | addTable(proximityTable); |
178 | 185 | } |
| 186 | + |
| 187 | + public ProximityStoreSchema derive(String suffix) throws PersistenceException { |
| 188 | + try { |
| 189 | + return new ProximityStoreSchema(getDataset(), getConnection(), suffix, isGlobal, tweaks, useFlushQueue); |
| 190 | + } catch (SQLException e) { |
| 191 | + throw new PersistenceException(e); |
| 192 | + } |
| 193 | + } |
179 | 194 | |
180 | 195 | @Override |
181 | 196 | public void checkConsistency() throws SQLException { |
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseProximityStoreBuilder.java |
— | — | @@ -14,14 +14,11 @@ |
15 | 15 | import de.brightbyte.wikiword.TweakSet; |
16 | 16 | import de.brightbyte.wikiword.processor.ImportProgressTracker; |
17 | 17 | import de.brightbyte.wikiword.schema.ProximityStoreSchema; |
18 | | -import de.brightbyte.wikiword.schema.WikiWordConceptStoreSchema; |
19 | 18 | |
20 | 19 | public class DatabaseProximityStoreBuilder |
21 | 20 | extends DatabaseWikiWordStoreBuilder |
22 | 21 | implements ProximityStoreBuilder { |
23 | 22 | |
24 | | - protected WikiWordConceptStoreSchema conceptDatabase; |
25 | | - |
26 | 23 | private DatabaseWikiWordConceptStoreBuilder conceptStore; |
27 | 24 | private int proximityExpansionPasses; |
28 | 25 | |
— | — | @@ -90,12 +87,16 @@ |
91 | 88 | protected ProximityStoreBuilder.ProximityParameters proximityParameters; |
92 | 89 | |
93 | 90 | protected FeatureBuilder( ProximityStoreSchema database, String suffix, ProximityStoreBuilder.ProximityParameters proximityParameters, boolean temp) throws PersistenceException { |
94 | | - this( database.makeFeatureTable(suffix), |
95 | | - database.makeFeatureMagnitudeTable(suffix), |
96 | | - database.makeProximityTable(suffix), |
| 91 | + this(database.derive(suffix), proximityParameters, temp) ; |
| 92 | + } |
| 93 | + |
| 94 | + protected FeatureBuilder( ProximityStoreSchema database, ProximityStoreBuilder.ProximityParameters proximityParameters, boolean temp) throws PersistenceException { |
| 95 | + this( database.makeFeatureTable(), |
| 96 | + database.makeFeatureMagnitudeTable(), |
| 97 | + database.makeProximityTable(), |
97 | 98 | proximityParameters ); |
98 | 99 | |
99 | | - if (suffix!=null) { |
| 100 | + if (database.getTableSuffix()!=null && database.getTableSuffix().length()>0) { |
100 | 101 | try { |
101 | 102 | database.createTable(proximityTable, !temp, temp); |
102 | 103 | database.createTable(featureMagnitudeTable, !temp, temp); |
— | — | @@ -509,11 +510,13 @@ |
510 | 511 | |
511 | 512 | } |
512 | 513 | |
513 | | - protected FeatureBuilder makeBuilder(ProximityParameters params, String suffix) { |
514 | | - RelationTable targetFeatureTable = ((ProximityStoreSchema)database).makeFeatureTable(suffix); |
515 | | - EntityTable targetFeatureMagnitudeTable = ((ProximityStoreSchema)database).makeFeatureMagnitudeTable(suffix); |
516 | | - RelationTable targetProximityTable = ((ProximityStoreSchema)database).makeProximityTable(suffix); |
| 514 | + protected FeatureBuilder makeBuilder(ProximityParameters params, String suffix) throws PersistenceException { |
| 515 | + ProximityStoreSchema db = ((ProximityStoreSchema)database).derive(suffix); |
517 | 516 | |
| 517 | + RelationTable targetFeatureTable = db.makeFeatureTable(); |
| 518 | + EntityTable targetFeatureMagnitudeTable = db.makeFeatureMagnitudeTable(); |
| 519 | + RelationTable targetProximityTable = db.makeProximityTable(); |
| 520 | + |
518 | 521 | return new FeatureBuilder(targetFeatureTable, targetFeatureMagnitudeTable, targetProximityTable, params); |
519 | 522 | } |
520 | 523 | |
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseLocalConceptStoreBuilder.java |
— | — | @@ -1443,7 +1443,7 @@ |
1444 | 1444 | } |
1445 | 1445 | |
1446 | 1446 | protected DatabaseProximityStoreBuilder newProximityStoreBuilder() throws SQLException { |
1447 | | - ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), false, tweaks, false); |
| 1447 | + ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), null, false, tweaks, false); |
1448 | 1448 | schema.setBackgroundErrorHandler(database.getBackgroundErrorHandler()); |
1449 | 1449 | return new DatabaseProximityStoreBuilder(this, schema, tweaks, getAgenda()); |
1450 | 1450 | } |
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/store/builder/DatabaseGlobalConceptStoreBuilder.java |
— | — | @@ -958,7 +958,7 @@ |
959 | 959 | } |
960 | 960 | |
961 | 961 | protected DatabaseProximityStoreBuilder newProximityStoreBuilder() throws SQLException { |
962 | | - ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), true, tweaks, false); |
| 962 | + ProximityStoreSchema schema = new ProximityStoreSchema(getDatasetIdentifier(), getDatabaseAccess().getConnection(), null, true, tweaks, false); |
963 | 963 | schema.setBackgroundErrorHandler(database.getBackgroundErrorHandler()); |
964 | 964 | return new DatabaseProximityStoreBuilder(this, schema, tweaks, getAgenda()); |
965 | 965 | } |