1
|
1 package de.mpiwg.itgroup.ismi.search.beans;
|
|
2
|
|
3 import java.io.Serializable;
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.Collections;
|
|
6 import java.util.List;
|
|
7 import java.util.Map;
|
|
8
|
|
9 import org.apache.commons.lang.StringUtils;
|
|
10 import org.mpi.openmind.repository.bo.Attribute;
|
|
11 import org.mpi.openmind.repository.bo.Entity;
|
|
12 import org.mpi.openmind.repository.bo.Relation;
|
|
13 import org.mpi.openmind.repository.bo.utils.EntitySortByNormalizedOwnValue;
|
|
14 import org.mpi.openmind.repository.services.utils.AttributeFilter;
|
|
15 import org.mpi.openmind.repository.utils.NormalizerUtils;
|
|
16
|
|
17 import de.mpiwg.itgroup.ismi.auxObjects.ResultSet;
|
|
18 import de.mpiwg.itgroup.ismi.entry.beans.AbstractISMIBean;
|
|
19
|
|
20
|
|
21 public class SearchResultBean extends AbstractISMIBean {
|
|
22
|
|
23 /**
|
|
24 *
|
|
25 */
|
|
26 private static final long serialVersionUID = 7072264955252613769L;
|
|
27 public static Long SEC_05 = new Long(5000);
|
|
28 public static Long SEC_10 = new Long(10000);
|
|
29 public static Long SEC_20 = new Long(20000);
|
|
30 public static Long SEC_30 = new Long(30000);
|
|
31
|
|
32 private List<ResultSet> resultSetList = new ArrayList<ResultSet>();
|
|
33
|
|
34 private int maxResult = -1 ;
|
|
35 private int counter = 0;
|
|
36
|
|
37 public void searchAttributes(String term, String mode){
|
|
38 long start = System.currentTimeMillis();
|
|
39 this.resultSetList = new ArrayList<ResultSet>();
|
|
40 this.counter = 0;
|
|
41 try{
|
|
42 if (StringUtils.isNotEmpty(term)) {
|
|
43
|
|
44 boolean includeTitles = (SimpleSearchBean.TITLES.equals(mode) || SimpleSearchBean.AUTHORS_TITLES.equals(mode)) ? true : false;
|
|
45 boolean includeAuthors = (SimpleSearchBean.AUTHORS.equals(mode) || SimpleSearchBean.AUTHORS_TITLES.equals(mode) ? true : false);
|
|
46
|
|
47 List<AttributeFilter> filters = new ArrayList<AttributeFilter>();
|
|
48
|
|
49 AttributeFilter filter1 = new AttributeFilter();
|
|
50 filter1.setEntObjectClass("PERSON");
|
|
51 filter1.setName("name");
|
|
52 //filter1.setNormalize(true);
|
|
53
|
|
54 AttributeFilter filter2 = new AttributeFilter();
|
|
55 filter2.setEntObjectClass("TEXT");
|
|
56 filter2.setName("title");
|
|
57 //filter2.setNormalize(true);
|
|
58
|
|
59 AttributeFilter filter3 = new AttributeFilter();
|
|
60 filter3.setEntObjectClass("PERSON");
|
|
61 filter3.setName("name_translit");
|
|
62
|
|
63 //filter3.setNormalize(true);
|
|
64
|
|
65 AttributeFilter filter4 = new AttributeFilter();
|
|
66 filter4.setEntObjectClass("TEXT");
|
|
67 filter4.setName("title_translit");
|
|
68
|
|
69 //filter4.setNormalize(true);
|
|
70
|
|
71 AttributeFilter filter5 = new AttributeFilter();
|
|
72 filter5.setEntObjectClass("TEXT");
|
|
73 filter5.setName("full_title");
|
|
74
|
|
75 //filter4.setNormalize(true);
|
|
76
|
|
77
|
|
78 AttributeFilter filter6 = new AttributeFilter();
|
|
79 filter6.setEntObjectClass("TEXT");
|
|
80 filter6.setName("full_title_translit");
|
|
81
|
|
82
|
|
83 //filers for alias
|
|
84 AttributeFilter filter7 = new AttributeFilter();
|
|
85 filter7.setEntObjectClass("ALIAS");
|
|
86 filter7.setName("alias");
|
|
87
|
|
88 /*
|
|
89 filter1.setOwnValue(search);
|
|
90 filter2.setOwnValue(search);
|
|
91 filter3.setOwnValue(search);
|
|
92 filter4.setOwnValue(search);
|
|
93 filter5.setOwnValue(search);
|
|
94 filter6.setOwnValue(search);
|
|
95 filter7.setOwnValue(search);
|
|
96 */
|
|
97
|
|
98 filters.add(filter1);
|
|
99 filters.add(filter2);
|
|
100 filters.add(filter3);
|
|
101 filters.add(filter4);
|
|
102 filters.add(filter5);
|
|
103 filters.add(filter6);
|
|
104 filters.add(filter7);
|
|
105
|
|
106 long startQuery = System.currentTimeMillis();
|
|
107 Map<Attribute, Entity> map;
|
|
108
|
|
109 if(getCache().isMapDirty()){
|
|
110 map = getWrapper().searchAttEntityByAttributeFilter(filters, getMaxResult());
|
|
111 System.out.println();
|
|
112 System.out.println("###########################################################");
|
|
113 System.out.println();
|
|
114 System.out.println("MAP SEARCH SIZEx= " + map.size() + " max result " + getMaxResult());
|
|
115 /*
|
|
116 for(Attribute a : map.keySet()){
|
|
117 System.out.println(a);
|
|
118 }*/
|
|
119
|
|
120 getCache().setAttResultMap(map);
|
|
121 }else{
|
|
122 map = getCache().getAttResultMap();
|
|
123 }
|
|
124
|
|
125 long endQuery = System.currentTimeMillis();
|
|
126 String normalizedString = NormalizerUtils.normalize(term);
|
|
127
|
|
128 System.out.println("");
|
|
129 System.out.println("***********************");
|
|
130 System.out.println("Word: " + term);
|
|
131 System.out.println("Normalized: " + normalizedString);
|
|
132 System.out.println("Query Execution= " + (endQuery - startQuery));
|
|
133
|
|
134 ResultSet nameResult = new ResultSet("Name");
|
|
135 ResultSet titleResult = new ResultSet("Title");
|
|
136
|
|
137 List<Long> idList = new ArrayList<Long>();
|
|
138
|
|
139 int countCached = 0;
|
|
140 int countNoCached = 0;
|
|
141 int noCachedAlias = 0;
|
|
142 int noCachedTextPerson = 0;
|
|
143 int putInCache = 0;
|
|
144 int putInCacheError = 0;
|
|
145 for(Attribute att : map.keySet()){
|
|
146
|
|
147 //limiting the execution time.
|
|
148 if((System.currentTimeMillis() - start) > SEC_20 || counter >= 1000)
|
|
149 break;
|
|
150
|
|
151 //Attribute att = map.get(ent);
|
|
152 Entity ent = map.get(att);
|
|
153
|
|
154 if(StringUtils.isNotEmpty(att.getNormalizedOwnValue()) && !getCache().ignoreAttribute(att) && att.getNormalizedOwnValue().contains(normalizedString)){
|
|
155 if(getCache().containsAttribute(att)){
|
|
156 countCached++;
|
|
157 //if the attributed was saved in the cache
|
|
158 Entity ee = getCache().getEntMap().get(att.getId());
|
|
159 String description = getCache().getDescriptionMap().get(att.getId() + "-" + ee.getId());
|
|
160 if(includeAuthors && ee.getObjectClass().equals("PERSON") && !idList.contains(ee.getId())){
|
|
161 nameResult.setTuple(ee, att);
|
|
162 nameResult.setDescription(ee, description);
|
|
163 //nameResult.setDescription(ee, att.getObjectClass() + "=" + att.getOwnValue());
|
|
164 counter++;
|
|
165 idList.add(ee.getId());
|
|
166 }else if(includeTitles && ee.getObjectClass().equals("TEXT") && !idList.contains(ee.getId())){
|
|
167 titleResult.setTuple(ee, att);
|
|
168 titleResult.setDescription(ee, description);
|
|
169 //titleResult.setDescription(ee, att.getObjectClass() + "=" + att.getOwnValue());
|
|
170 counter++;
|
|
171 idList.add(ee.getId());
|
|
172 }
|
|
173 }else{
|
|
174
|
|
175 countNoCached++;
|
|
176 if(includeAuthors && !idList.contains(ent.getId()) && att.getName().contains("name") && att.getSourceObjectClass().equals("PERSON")){
|
|
177 //OC: PERSON
|
|
178 String d = att.getObjectClass() + "=" + att.getOwnValue();
|
|
179 nameResult.setDescription(ent, d);
|
|
180 nameResult.setTuple(ent, att);
|
|
181 getCache().setTuple(ent, att, d);
|
|
182 counter++;
|
|
183 noCachedTextPerson++;
|
|
184 idList.add(ent.getId());
|
|
185 }else if(includeTitles && !idList.contains(ent.getId()) && (att.getName().contains("title") || att.getName().contains("full_title"))){
|
|
186 //OC: TEXT
|
|
187 String d = att.getObjectClass() + "=" + att.getOwnValue();
|
|
188 titleResult.setDescription(ent, d);
|
|
189 titleResult.setTuple(ent, att);
|
|
190 getCache().setTuple(ent, att, d);
|
|
191 counter++;
|
|
192 noCachedTextPerson++;
|
|
193 idList.add(ent.getId());
|
|
194 }else if(att.getSourceObjectClass().equals("ALIAS")){
|
|
195 noCachedAlias++;
|
|
196 //OC: ALIAS
|
|
197 AliasStructure aliasStructure = getAliasStructure(ent, att);
|
|
198 if(aliasStructure.target != null){
|
|
199 getCache().setTuple(aliasStructure.target, att, aliasStructure.description);
|
|
200 putInCache++;
|
|
201 if(!idList.contains(aliasStructure.target.getId())){
|
|
202 if(includeAuthors && aliasStructure.target.getObjectClass().equals("PERSON")){
|
|
203 nameResult.setTuple(aliasStructure.target, att);
|
|
204 nameResult.setDescription(aliasStructure.target, aliasStructure.description);
|
|
205 counter++;
|
|
206 }else if(includeTitles && aliasStructure.target.getObjectClass().equals("TEXT")){
|
|
207 titleResult.setTuple(aliasStructure.target, att);
|
|
208 titleResult.setDescription(aliasStructure.target, aliasStructure.description);
|
|
209 counter++;
|
|
210 }
|
|
211 idList.add(aliasStructure.target.getId());
|
|
212 }
|
|
213 }else{
|
|
214 putInCacheError++;
|
|
215 this.getCache().getIgnoredAttIdList().add(att.getId());
|
|
216 System.out.println("cacheError++ : " + att);
|
|
217 }
|
|
218 }
|
|
219
|
|
220 }
|
|
221 }
|
|
222 }
|
|
223
|
|
224 System.out.println();
|
|
225 System.out.println("countCached: " + countCached + " - countNoCached: " + countNoCached + " - total: " + (countCached + countNoCached));
|
|
226 System.out.println("noCachedTextPerson: " + noCachedTextPerson++ + " - noCachedAlias: " + noCachedAlias);
|
|
227 System.out.println("putInCacheError: " + putInCacheError + " - putInCache: " + putInCache);
|
|
228 System.out.println("nameResult: " + nameResult.getResults().size());
|
|
229 System.out.println("titleResult: " + titleResult.getResults().size());
|
|
230
|
|
231 if(nameResult.getResults().size() > 0){
|
|
232 Collections.sort(nameResult.getResults(), new EntitySortByNormalizedOwnValue());
|
|
233 this.resultSetList.add(nameResult);
|
|
234 }
|
|
235 if(titleResult.getResults().size() > 0){
|
|
236 Collections.sort(titleResult.getResults(), new EntitySortByNormalizedOwnValue());
|
|
237 this.resultSetList.add(titleResult);
|
|
238 }
|
|
239 }
|
|
240
|
|
241 }catch(Exception e){
|
|
242 e.printStackTrace();
|
|
243 }
|
|
244
|
|
245 long end = System.currentTimeMillis();
|
|
246 System.out.println("Simple search time execution= " + (end - start));
|
|
247 }
|
|
248
|
|
249 /**
|
|
250 * TODO do it more clever!
|
|
251 * rules:
|
|
252 * ----Text
|
|
253 * is_prime_alias_title_of
|
|
254 * is_alias_title_of
|
|
255 * is_alias_incipit_of
|
|
256 * is_alias_explicit_of
|
|
257 * ----Person
|
|
258 * is_prime_alias_name_of
|
|
259 * is_alias_name_of
|
|
260 * @param alias
|
|
261 * @return
|
|
262 */
|
|
263 private AliasStructure getAliasStructure(Entity alias, Attribute att) throws Exception{
|
|
264 AliasStructure structure = new AliasStructure();
|
|
265 structure.aliasAtt = att;
|
|
266 List<Relation> list = null;
|
|
267 list = getWrapper().getSourceRelations(alias, "is_prime_alias_title_of", "TEXT", 1);
|
|
268 if(list.size() > 0)
|
|
269 structure.setRelation(list.get(0));
|
|
270 list = getWrapper().getSourceRelations(alias, "is_alias_title_of", "TEXT", 1);
|
|
271 if(list.size() > 0)
|
|
272 structure.setRelation(list.get(0));
|
|
273 list = getWrapper().getSourceRelations(alias, "is_alias_incipit_of", "TEXT", 1);
|
|
274 if(list.size() > 0)
|
|
275 structure.setRelation(list.get(0));
|
|
276 list = getWrapper().getSourceRelations(alias, "is_alias_explicit_of", "TEXT", 1);
|
|
277 if(list.size() > 0)
|
|
278 structure.setRelation(list.get(0));
|
|
279 list = getWrapper().getSourceRelations(alias, "is_prime_alias_name_of", "PERSON", 1);
|
|
280 if(list.size() > 0)
|
|
281 structure.setRelation(list.get(0));
|
|
282 list = getWrapper().getSourceRelations(alias, "is_alias_name_of", "PERSON", 1);
|
|
283 if(list.size() > 0)
|
|
284 structure.setRelation(list.get(0));
|
|
285 return structure;
|
|
286 }
|
|
287
|
|
288 public List<ResultSet> getResultSetList() {
|
|
289 return resultSetList;
|
|
290 }
|
|
291
|
|
292 public void setResultSetList(List<ResultSet> resultSetList) {
|
|
293 this.resultSetList = resultSetList;
|
|
294 }
|
|
295 public int getMaxResult() {
|
|
296 return maxResult;
|
|
297 }
|
|
298
|
|
299 public void setMaxResult(int maxResult) {
|
|
300 this.maxResult = maxResult;
|
|
301 }
|
|
302 public int getCounter() {
|
|
303 return counter;
|
|
304 }
|
|
305
|
|
306 public void setCounter(int counter) {
|
|
307 this.counter = counter;
|
|
308 }
|
|
309 private class AliasStructure implements Serializable{
|
|
310 private static final long serialVersionUID = -833933447985472058L;
|
|
311
|
|
312 public Entity target = null;
|
|
313 public Entity alias = null;
|
|
314 public Relation rel = null;
|
|
315 public String description = "";
|
|
316 public Attribute aliasAtt = null;
|
|
317
|
|
318 public void setRelation(Relation rel){
|
|
319 this.rel = rel;
|
|
320 this.target = rel.getTarget();
|
|
321 this.alias = rel.getSource();
|
|
322 this.description = rel.getOwnValue() + " <- ALIAS [alias=" + aliasAtt.getOwnValue() + "]";
|
|
323 }
|
|
324 }
|
|
325 }
|