-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkExtractE2.java
324 lines (267 loc) · 11.9 KB
/
LinkExtractE2.java
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package ParcoursDeGrapheSansMemoireTotal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import ParcoursDeGrapheSansMemoireUneGeneration.LinkExtract;
import ParcoursDeGrapheSansMemoireUneGeneration.LirePage;
import ParcoursDeGrapheSansMemoireUneGeneration.TaskRead;
import ParcoursDeGrapheSansMemoireUneGeneration.TaskReadBis;
import ParcoursDeGrapheSansMemoireUneGeneration.TaskReadConnected;
import ParcoursDeGrapheSansMemoireUneGeneration.TaskVerificationBis;
import ParcoursDeGrapheSansMemoireUneGeneration.TaskVerificationConnected;
import ParcoursDeGrapheSansMemoireUneGeneration.TaskVerificationNoLirePage;
import util.LockfreeQueue;
import javax.swing.text.html.HTMLEditorKit;
public class LinkExtractE2 {
public static LinkedList<String> RepererMot (int debut, int fin, String motini, String motfin, String page) {
int k = page.indexOf(motini,debut);
LinkedList<String> res = new LinkedList<String>();
while(k<fin && k>=0) {
int begin = k;
int end = page.indexOf(motfin,begin+1);
String nouv = page.substring(begin + motini.length(), end);
res.add(nouv);
k = page.indexOf(motini,end+1);
}
return res;
}
public static LockfreeQueue<String> RepererMot2 (int debut, int fin, String motini, String motfin, String page) {
int k = page.indexOf(motini,debut);
LockfreeQueue<String> res = new LockfreeQueue<String>();
while(k<fin && k>=0) {
int begin = k;
int end = page.indexOf(motfin,begin+1);
String nouv = page.substring(begin + motini.length(), end);
//System.out.println(nouv);
res.add(nouv);
k = page.indexOf(motini,end+1);
}
return res;
}
public static LockfreeQueue<CoupleResult> getLinks9E2(String _url, int N1, int N2,int NombreGeneration) { //idem que getLinks 7 mais on charge la page initiale
LockfreeQueue<CoupleResult> res = new LockfreeQueue<CoupleResult>(); //Fonctionne rapidement avec 350 et 4-5
HashMap<String,Couple> rempli= new HashMap<String,Couple>();
Couple initial=new Couple (true,0);
rempli.put(_url, initial);
LockfreeQueue<Trio> UrlEtPage=new LockfreeQueue<Trio>();
String Page = LirePage.getTextFile("http://starwars.wikia.com" +_url);
int debut = Page.indexOf("/wiki/Template:Character",0);
int fin = Page.indexOf("div>In other languages</div>",debut);
String motini = "<a href=\"";
String motfin = "\" title=";
LockfreeQueue<String> RepererMot = RepererMot2 (debut, fin, motini, motfin,Page);
//for (String r:RepererMot){System.out.println(r);}
TaskReadE2[] threads=new TaskReadE2[N1] ;
TaskVerificationBisE2[] threads2=new TaskVerificationBisE2[N2] ;
for (int i=1; i<=NombreGeneration;i++){
for (int NumeroThread1=0;NumeroThread1<N1;NumeroThread1++){
threads[NumeroThread1] = new TaskReadE2(RepererMot,rempli,UrlEtPage,i);
threads[NumeroThread1].start();
}
for (int I=0;I<N1;I++) {
try {
threads[I].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int j=0;j<N2;j++){
TaskVerificationBisE2 thread2 = new TaskVerificationBisE2 (rempli,UrlEtPage,RepererMot);
threads2[j]=thread2;
thread2.start();
}
for (Thread thread2:threads2) {
try {
thread2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Set<String> S= rempli.keySet();
for (String s:S){
Couple c= rempli.get(s);
if (c.b){
CoupleResult R= new CoupleResult(s,c.i);
res.add(R);
}
}
return res;
}
public static LockfreeQueue<CoupleResult> getLinks11E2(String _url, int N1, int N2,int NombreGeneration) { //avec lockfreequeue pour les pages a lire et AtomicInteger liant les readers et les vérificateurs
LockfreeQueue<CoupleResult> res = new LockfreeQueue<CoupleResult>();
HashMap<String,Couple> rempli= new HashMap<String,Couple>();
Couple initial=new Couple (true,0);
rempli.put(_url, initial);
LockfreeQueue<Trio> UrlEtPage=new LockfreeQueue<Trio>();
String Page = LirePage.getTextFile("http://starwars.wikia.com"+_url);
int debut = Page.indexOf("/wiki/Template:Character",0);
int fin = Page.indexOf("div>In other languages</div>",debut);
String motini = "<a href=\"";
String motfin = "\" title=";
LockfreeQueue<String> ALire1 = RepererMot2 (debut, fin, motini, motfin,Page);
LockfreeQueue<CoupleResult> ALire= new LockfreeQueue<CoupleResult>();
while(!ALire1.isEmpty()){ALire.add(new CoupleResult(ALire1.take(),1));}
AtomicInteger N=new AtomicInteger(N1);
TaskReadConnectedE2 [] threads = new TaskReadConnectedE2[N1];
TaskVerificationConnectedE2 [] threads2 = new TaskVerificationConnectedE2[N2];
while (!ALire.isEmpty()){
for (int i =0;i<N1;i++) {
TaskReadConnectedE2 r= new TaskReadConnectedE2(ALire, rempli, UrlEtPage,N,NombreGeneration);
r.start();
threads[i]=r;
}
for (int i =0;i<N2;i++) {
TaskVerificationConnectedE2 t= new TaskVerificationConnectedE2(rempli,ALire,UrlEtPage,N,NombreGeneration);
threads2[i] = t;
threads2[i].start();
}
for (int i =0;i<N2;i++) {
try {
threads2[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
N.set(N1);
}
Set<String> S= rempli.keySet();
for (String s:S){
Couple c= rempli.get(s);
if (c.b){
CoupleResult R= new CoupleResult(s,c.i);
res.add(R);
}
}
return res;
}
public static LockfreeQueue<CoupleResult> getLinks12E2(String _url, int N1, int N2,int NombreGeneration) { //avec lockfreequeue pour les pages a lire et AtomicInteger liant les readers et les vérificateurs
LockfreeQueue<CoupleResult> res = new LockfreeQueue<CoupleResult>();
HashMap<String,Couple> rempli= new HashMap<String,Couple>();
Couple initial=new Couple (true,0);
rempli.put(_url, initial);
LockfreeQueue<Trio> UrlEtPage=new LockfreeQueue<Trio>();
String Page = LirePage.getTextFile("http://starwars.wikia.com"+_url);
int debut = Page.indexOf("/wiki/Template:Character",0);
int fin = Page.indexOf("div>In other languages</div>",debut);
String motini = "<a href=\"";
String motfin = "\" title=";
LockfreeQueue<String> ALire1 = RepererMot2 (debut, fin, motini, motfin,Page);
LockfreeQueue<CoupleResult> ALire= new LockfreeQueue<CoupleResult>();
while(!ALire1.isEmpty()){ALire.add(new CoupleResult(ALire1.take(),1));}
AtomicInteger N=new AtomicInteger(N1);
TaskReadConnectedE22 [] threads = new TaskReadConnectedE22[N1];
TaskVerificationConnectedE22 [] threads2 = new TaskVerificationConnectedE22[N2];
for(int GenerationCourante=0;GenerationCourante<NombreGeneration;GenerationCourante++){
for (int i =0;i<N1;i++) {
TaskReadConnectedE22 r= new TaskReadConnectedE22(ALire, rempli, UrlEtPage,N,NombreGeneration,GenerationCourante);
r.start();
threads[i]=r;
}
for (int i =0;i<N2;i++) {
TaskVerificationConnectedE22 t= new TaskVerificationConnectedE22(rempli,ALire,UrlEtPage,N,NombreGeneration,GenerationCourante);
threads2[i] = t;
threads2[i].start();
}
for (int i =0;i<N2;i++) {
try {
threads2[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
N.set(N1);
ALire=threads2[0].Verifier;
}
Set<String> S= rempli.keySet();
for (String s:S){
Couple c= rempli.get(s);
if (c.b){
CoupleResult R= new CoupleResult(s,c.i);
res.add(R);
}
}
return res;
}
public static LockfreeQueue<CoupleResultSucc> getLinks13E2(String _url, int N1, int N2,int NombreGeneration) { //avec lockfreequeue pour les pages a lire et AtomicInteger liant les readers et les vérificateurs
LockfreeQueue<CoupleResultSucc> res = new LockfreeQueue<CoupleResultSucc>();
HashMap<String,CoupleSucc> rempli= new HashMap<String,CoupleSucc>();
Couple initial=new Couple (true,0);
CoupleSucc init= new CoupleSucc(initial,new LockfreeQueue<String>());
rempli.put(_url, init);
LockfreeQueue<TrioSucc> UrlEtPage=new LockfreeQueue<TrioSucc>();
String Page = LirePage.getTextFile("http://starwars.wikia.com"+_url);
int debut = Page.indexOf("/wiki/Template:Character",0);
int fin = Page.indexOf("div>In other languages</div>",debut);
String motini = "<a href=\"";
String motfin = "\" title=";
LockfreeQueue<String> ALire1 = RepererMot2 (debut, fin, motini, motfin,Page);
LockfreeQueue<CoupleResultSucc> ALire= new LockfreeQueue<CoupleResultSucc>();
while(!ALire1.isEmpty()){
CoupleResult CR=new CoupleResult(ALire1.take(),1);
LockfreeQueue<String> queue=new LockfreeQueue<String>();
queue.add(_url);
ALire.add(new CoupleResultSucc(CR,queue));}
AtomicInteger N=new AtomicInteger(N1);
TaskReadConnectedE22Succ [] threads = new TaskReadConnectedE22Succ[N1];
TaskVerificationConnectedE22Succ [] threads2 = new TaskVerificationConnectedE22Succ[N2];
int GenerationCourante=0;
while(GenerationCourante<NombreGeneration){
for (int i =0;i<N1;i++) {
TaskReadConnectedE22Succ r= new TaskReadConnectedE22Succ(ALire, rempli, UrlEtPage,N,NombreGeneration,GenerationCourante);
r.start();
threads[i]=r;
}
for (int i =0;i<N2;i++) {
TaskVerificationConnectedE22Succ t= new TaskVerificationConnectedE22Succ(rempli,ALire,UrlEtPage,N,NombreGeneration,GenerationCourante);
threads2[i] = t;
threads2[i].start();
}
for (int i =0;i<N2;i++) {
try {
threads2[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
N.set(N1);
ALire=threads2[0].Verifier;
GenerationCourante++;
}
Set<String> S= rempli.keySet();
for (String s:S){
CoupleSucc C= rempli.get(s);
Couple c= C.id;
if (c.b){
CoupleResult R= new CoupleResult(s,c.i);
res.add(new CoupleResultSucc(R,C.parents));
}
}
return res;
}
}