-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLearningModulesPool.java
More file actions
121 lines (101 loc) · 3.24 KB
/
LearningModulesPool.java
File metadata and controls
121 lines (101 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.machine.learning;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import com.learningmodule.association.conceptdrug.PredictionResult;
import com.machine.learning.interfaces.LearningModuleInterface;
import com.machine.learning.request.SearchAttribute;
/*
* The class contains the list of learning modules that are added by ContextListner on start of this service
*/
public class LearningModulesPool {
// LinkedList of Learning Modules in this pool
private static LinkedList<LearningModuleInterface> modules = new LinkedList<LearningModuleInterface>();
private static Logger log = Logger.getLogger(LearningModulesPool.class);
// method to add a learning module in this pool
public static void addLearningModule(LearningModuleInterface module) {
modules.add(module);
log.debug("Module Added: " + module.getClass());
}
/*
* method to get the results for a query from all modules in the pool and
* merge them to return all the results
*/
public static LinkedList<PredictionResult> predict(String query, SearchAttribute[] features) {
LinkedList<PredictionResult> results = new LinkedList<PredictionResult>();
// get the sorted results from a module and merge them with all results.
for (int i = 0; i < modules.size(); i++) {
results = join(modules.get(i).predict(query, features), results);
}
return results;
}
// method to merge/join two list of sorted prediction results
private static LinkedList<PredictionResult> join(List<PredictionResult> list1,
List<PredictionResult> list2) {
// iterator on list1
Iterator<PredictionResult> it1 = list1.iterator();
// iteraton on list2
Iterator<PredictionResult> it2 = list2.iterator();
// results after joining
LinkedList<PredictionResult> results = new LinkedList<PredictionResult>();
PredictionResult temp1, temp2;
if (it1.hasNext() && it2.hasNext()) {
temp1 = it1.next();
temp2 = it2.next();
// if item in list1 has more confidence add it to the results else
// add item of list2
if (temp1.getConfidence() > temp2.getConfidence()) {
results.add(temp1);
temp1 = it1.next();
} else {
results.add(temp2);
temp2 = it2.next();
}
// while one of the iterator reach its end
while (it1.hasNext() && it2.hasNext()) {
if (temp1.getConfidence() < temp2.getConfidence()) {
results.add(temp1);
temp1 = it1.next();
} else {
results.add(temp2);
temp2 = it2.next();
}
}
}
// add items for list1 if any left
while (it1.hasNext()) {
results.add(it1.next());
}
// add items for list2 if any left
while (it2.hasNext()) {
results.add(it2.next());
}
return results;
}
/*
* method to call all the learning modules to initiate there learning
* algorithm.
*/
public static void learn() {
Thread[] threads = new Thread[modules.size()];
for (int i = 0; i < modules.size(); i++) {
threads[i] = runLearningThread(i);
}
for (int i = 0; i < modules.size(); i++) {
while (threads[i].isAlive())
;
}
}
private static Thread runLearningThread(int i) {
final int j = i;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
modules.get(j).learn();
}
});
thread.run();
return thread;
}
}