source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/gs3build/classifier/ClassifierManager.java@ 12188

Last change on this file since 12188 was 12188, checked in by kjdon, 18 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1package org.greenstone.gsdl3.gs3build.classifier;
2
3import java.util.List;
4import java.util.Iterator;
5import org.w3c.dom.Element;
6import org.w3c.dom.Document;
7
8import org.greenstone.gsdl3.gs3build.doctypes.DocumentID;
9import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
10import org.greenstone.gsdl3.gs3build.doctypes.DocumentList;
11import org.greenstone.gsdl3.gs3build.database.GS3SQLConnection;
12import org.greenstone.gsdl3.util.GSXML;
13
14
15public class ClassifierManager
16{
17 DocumentList documents;
18 ClassifierInterface [] list;
19 int size;
20 int used;
21 GS3SQLConnection database;
22
23 /**
24 * Instantiate a classifier manager that will classify the
25 * documents in a given <code>DocumentList</code> object.
26 * The list of documents should not change during the
27 * <code>classifyDocuments</code> function, and the documents
28 * themselves may be altered at that time.
29 *
30 * @param <code>DocumentList</code> a provider of documents for
31 * classification.
32 */
33 public ClassifierManager(DocumentList documentList, GS3SQLConnection database)
34 { this.list = new ClassifierInterface[10];
35 this.size = 10;
36 this.used = 0;
37 this.documents = documentList;
38 this.database = database;
39 }
40
41 /**
42 * Add a classifier to the end of the list of classifiers...
43 *
44 * @param <code>ClassifierInterface</code> the classifier.
45 */
46 public void addClassifier(ClassifierInterface classifier)
47 { this.ensureSize(this.used + 1);
48 this.list[this.used] = classifier;
49 this.used ++;
50
51 classifier.setDatabase(this.database);
52 }
53
54 /**
55 * Classify an individual document - the document itself may be written
56 * to in consequence, in which case the document <strong>must</strong>
57 * be rewritten to disk. This function does <strong>not</strong> do the
58 * writing itself.
59 *
60 * @param <code>DocumentID</code> the document's identifier.
61 * @param <code>DocumentInterface</code> the document itself.
62 */
63 public void classifyDocument(DocumentID docId, DocumentInterface document)
64 { for (int i = 0; i < this.used; i ++)
65 { this.list[i].classifyDocument(docId, document);
66 }
67 }
68
69 /**
70 * Classify all the documents that the document list is able to provide...
71 */
72 public void classifyDocuments()
73 { for (int i = 0; i < this.used; i ++)
74 { for (int p = 0; p < this.list[i].getClassifierPasses(); p ++)
75 { this.list[i].startClassifierPass(p);
76
77 Iterator iterator = this.documents.iterator();
78
79 while (iterator.hasNext()) {
80 DocumentInterface document = (DocumentInterface) iterator.next();
81
82 if (document.isIndexed()) {
83 if (!this.list[i].classifyDocument(document.getID(), document)) {
84 System.out.println("Ending document");
85 }
86
87 if (document.isChanged()) {
88 // System.out.println("Writing document " + document.getID());
89 this.documents.storeChangedDocument(document);
90 }
91 }
92 }
93 this.list[i].endClassifierPass(p);
94 }
95 this.list[i].completeClassification();
96 }
97 }
98
99 public void ensureSize(int size)
100 { while (size >= this.size)
101 { ClassifierInterface newList [] = new ClassifierInterface[this.size*2];
102 this.size *= 2;
103 System.arraycopy(this.list, 0, newList, 0, this.size);
104 this.list = newList;
105 }
106 }
107
108 public static ClassifierInterface loadClassifier(String classifierName)
109 { System.out.println("Loading classifier " + classifierName);
110 try {
111 ClassifierInterface classifier =
112 (ClassifierInterface) Class.forName("org.greenstone.gsdl3.gs3build.classifier."+classifierName+"Classifier").newInstance();
113 return classifier;
114 }
115 catch (ClassNotFoundException ex) {
116 System.err.println("Attempt to load classifier " + classifierName + " which does not exist");
117 }
118 catch (InstantiationException instEx) {
119 System.err.println("Unable to create classifier " + classifierName);
120 }
121 catch (IllegalAccessException accessEx) {
122 System.err.println("Unable to instantiate classifier " + classifierName + " due to permissions problems " + accessEx.toString());
123 }
124 return null;
125 }
126
127 public static ClassifierInterface loadClassifier(String classifierName, List params)
128 { ClassifierInterface classifier = loadClassifier(classifierName);
129 if (classifier != null) {
130 classifier.configure(params);
131 }
132 return classifier;
133 }
134
135 public boolean addServiceDescriptions(Element service_class_list) {
136 if (this.used==0) {
137 // no classiifers
138 return true;
139 }
140 Document owner_doc = service_class_list.getOwnerDocument();
141 Element browse_service = owner_doc.createElement(GSXML.SERVICE_CLASS_ELEM);
142 browse_service.setAttribute(GSXML.NAME_ATT, "GS3Browse");
143 service_class_list.appendChild(browse_service);
144
145 Element classifier_list = owner_doc.createElement(GSXML.CLASSIFIER_ELEM+GSXML.LIST_MODIFIER);
146 browse_service.appendChild(classifier_list);
147 boolean success = true;
148 for (int i = 0; i < this.used; i ++) {
149 if (!this.list[i].addClassifierDescription(classifier_list)) {
150 success = false;
151 }
152 }
153 return success;
154 }
155}
Note: See TracBrowser for help on using the repository browser.