source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/Visualizer.java@ 9874

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/*
2 * Visualiser.java
3 * Copyright (C) 2004 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.gsdl3.service;
21
22import org.greenstone.gsdl3.util.*;
23import org.w3c.dom.Document;
24import org.w3c.dom.Node;
25import org.w3c.dom.Element;
26import org.w3c.dom.Text;
27import java.util.Vector;
28import java.util.HashMap;
29import java.io.File;
30import java.io.*;
31import vishnu.server.*;
32import vishnu.server.Search.*;
33import vishnu.datablock.*;
34import vishnu.util.Base64;
35
36public class Visualizer
37 extends ServiceRack {
38
39 // the services on offer
40 private static final String VIS_SERVICE = "VisApplet";
41
42 // other internal strings
43 private static final String ENGINE_TYPE_ELEM = "engineType";
44 private static final String LUCENE_ENGINE = "LUCENE";
45 private static final String MG_ENGINE = "MG";
46
47 private Element applet_description = null;
48
49 private String engine_type = LUCENE_ENGINE; // lucene is default
50 private String collection_home = null;
51 private SearchInterface engine = null;
52 public Visualizer () {
53
54 }
55 public boolean configure(Element info, Element extra_info)
56 {
57 Element e = this.doc.createElement(GSXML.SERVICE_ELEM);
58 e.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_APPLET);
59 e.setAttribute(GSXML.NAME_ATT, VIS_SERVICE);
60 short_service_info.appendChild(e);
61
62 applet_description = this.doc.createElement(GSXML.SERVICE_ELEM);
63 applet_description.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_APPLET);
64 applet_description.setAttribute(GSXML.NAME_ATT, VIS_SERVICE);
65
66
67 String app_info = "<"+GSXML.APPLET_ELEM+" CODEBASE='lib' CODE='vishnu.testvis.visual.VishnuSingle.class' ARCHIVE='vishnu.jar,gsdl3.jar,xercesImpl.jar,xml-apis.jar' WIDTH='1000' HEIGHT='800'>";
68 app_info += "<PARAM NAME='library' VALUE=''/>"; // filled in by receptionist
69 app_info += "<PARAM NAME='viscgi' VALUE='?";
70 app_info += GSParams.ACTION +"=a&amp;"+GSParams.REQUEST_TYPE +"=r&amp;"+GSParams.SERVICE+"="+VIS_SERVICE+"&amp;"+GSParams.OUTPUT+"=xml&amp;"+GSParams.RESPONSE_ONLY+"=1'/>";
71 app_info += "<PARAM NAME='collection' VALUE='" + this.cluster_name + "'/>";
72 app_info += "<PARAM NAME='engine' VALUE='GSDLEngine' />";
73 // add view info if appropriate
74 app_info += "The visualization applet.</"+GSXML.APPLET_ELEM+">";
75
76 Document dom = converter.getDOM(app_info);
77 if (dom == null) {
78 System.err.println("Visualizer.configure Error: Couldn't parse applet info");
79 return false;
80 }
81 Element app_elem = dom.getDocumentElement();
82 applet_description.appendChild(this.doc.importNode(app_elem, true));
83
84 // get engine type from config file
85 Element engine_elem = (Element)GSXML.getChildByTagName(info, ENGINE_TYPE_ELEM);
86 if (engine_elem != null) {
87 engine_type = engine_elem.getAttribute(GSXML.NAME_ATT);
88 if (engine_type.equals("")) {
89 engine_type = LUCENE_ENGINE;
90 }
91 }
92
93 collection_home = this.site_home + File.separator + "collect"+ File.separator + this.cluster_name + File.separator;
94 if (engine_type.equals(LUCENE_ENGINE)) {
95 // make the full path the arg to MGSearcher??
96 engine = new LUCSearcher(collection_home, null);
97 } else if (engine_type.equals(MG_ENGINE)) {
98 engine = new MGSearcher(collection_home, this.cluster_name, null);
99 } else {
100 System.err.println("Visualiser: invalid engine type: "+engine_type);
101 return false;
102 }
103 return true;
104 }
105
106
107
108 protected Element getServiceDescription(String service, String lang, String subset) {
109 if (!service.equals(VIS_SERVICE)) {
110 return null;
111 }
112 Element describe = (Element) applet_description.cloneNode(true);
113 describe.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME,
114 getTextString(VIS_SERVICE+".name", lang)));
115 describe.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_DESCRIPTION,
116 getTextString(VIS_SERVICE+".description", lang)));
117 return describe;
118 }
119
120 protected Element processVisApplet(Element request)
121 {
122
123 Element param_elem = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
124 HashMap params = GSXML.extractParams(param_elem, false);
125
126 String type = (String)params.get("type");
127
128 // the result element
129 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
130 result.setAttribute(GSXML.FROM_ATT, VIS_SERVICE);
131 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
132
133 // applet result info must be in appletInfo element
134 Element applet_data = this.doc.createElement(GSXML.APPLET_DATA_ELEM);
135 result.appendChild(applet_data);
136 Element vis_data = this.doc.createElement("visData");
137 applet_data.appendChild(vis_data);
138
139 String results = "";
140 if (type.equals("search")) {
141
142 //Element cluster_results = this.doc.createElement("cluster");
143 //vis_data.appendChild(cluster_results);
144 //Element description_results = this.doc.createElement("descriptions");
145 //vis_data.appendChild(description_results);
146
147 String query = (String)params.get("q");
148 System.err.println("the query was "+query);
149
150 Vector doc_nums = new Vector();
151 Vector descriptions = new Vector();
152
153 engine.search(query);
154 doc_nums = engine.getDocIdentifiers();
155 descriptions = engine.getDocDescriptions();
156
157 DataBlock db = generateDataBlock(collection_home, null, doc_nums, descriptions);
158 System.err.println("got back data, now converting to string");
159 try {
160 results = Base64.encodeObject(db);
161 } catch (Exception e) {
162 System.err.println("trying to base64 encode the datablock, but exception happened: "+e);
163 }
164 System.err.println("after converting to string");
165// // serialise the data block
166// try {
167// ByteArrayOutputStream baos = new ByteArrayOutputStream();
168// ObjectOutputStream oos = new ObjectOutputStream(baos);
169// oos.writeObject(db);
170// oos.close();
171
172// //results = baos.toString("UTF-8");
173// results = Base64.encode(baos.toByteArray());
174// } catch (Exception e) {
175// System.err.println("Visualizer serialise data block error: "+e);
176// }
177 //System.err.println("num docs = "+doc_nums.size());
178 //Cluster c = new Cluster();
179 //results = c.getCluster(collection_home,doc_nums);
180 //results.trim();
181 //System.err.println("results = "+results);
182
183 Text t = this.doc.createTextNode(results);
184 vis_data.appendChild(t);
185
186// for (int i=0; i<descriptions.size(); i++) {
187// String d = (String)descriptions.get(i);
188// Element de = GSXML.createTextElement(this.doc, "desc", d);
189// description_results.appendChild(de);
190// }
191 System.err.println("end of search");
192 } else if (type.equals("fetch")) {
193 String docNum = (String)params.get("d");
194
195 Vector doc = engine.getDocContent(Integer.parseInt(docNum));
196 for (int i=0; i<doc.size(); i++) {
197 results += (String)doc.get(i) +"\n";
198 }
199 Text t = this.doc.createTextNode(results);
200 vis_data.appendChild(t);
201 } else {
202 System.err.println("invalid type sent to Visualiser process: "+type);
203 return result;
204 }
205 System.err.println("returning result");
206 return result;
207 }
208
209 private DataBlock generateDataBlock(String collection_home, String view, Vector docNums, Vector descriptions) {
210
211 if (docNums.size()==0) {
212 return null;
213 }
214 DataBlock data = new DataBlock();
215
216 CKServer ck_server = new CKServer(collection_home, view);
217
218 /**** set data fields one by one and pass one what ever gets assembled ****/
219
220 try{
221
222 ck_server.setDescriptions(descriptions);
223
224 /**** get candidate keywords ****/
225
226 String[] keywords = ck_server.computeKeywords(docNums);
227
228 data.words = keywords;
229
230
231 /**** get sparse document * keyword matrix ****/
232
233 SparseMatrix matrix = ck_server.getSparseMatrix(docNums);
234
235 data.matrix = matrix;
236
237
238 /**** get document indices, this is a subset of the original ****/
239 /**** those without keywords are excluded ****/
240
241 int[] docs = ck_server.getHitDocuments();
242
243 data.docs = docs;
244
245
246 String[] desc = ck_server.getHitDescriptions();
247
248 data.descriptions = desc;
249
250
251 /**** get 10 or so clusters ****/
252
253 Vector[] clusters = ck_server.getClusters();
254
255 data.clusters = clusters;
256
257
258 double[][] centroids = ck_server.getCentroids();
259
260
261 /**** send their centroids through sammon mapping ****/
262
263 Point2D[] sammon = ck_server.getSammonMap(centroids);
264
265 System.err.println("Num descriptions: " + descriptions.size());
266 System.err.println("Num docs: " + docs.length);
267 System.err.println("Num desc: " + desc.length);
268
269 data.sammon = sammon;
270 System.err.println("end of try in generatedatablock");
271 } catch (Exception e) {
272 System.err.println("VisServlet: computing clustering Error: "+e);
273 e.printStackTrace();
274 }
275 ck_server = null;
276 System.err.println("returning data");
277 return data;
278 }
279
280}
281
282
283
284
285
286
287
288
289
290
291
Note: See TracBrowser for help on using the repository browser.