1 package org.thegalactic.lattice;
2
3
4
5
6
7
8
9
10
11
12
13
14 import org.thegalactic.rule.ImplicationalSystem;
15 import java.io.BufferedWriter;
16 import java.io.File;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.util.Date;
20 import java.util.TreeSet;
21
22 import org.thegalactic.context.Context;
23 import org.thegalactic.dgraph.ConcreteDGraph;
24 import org.thegalactic.util.ComparableSet;
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 public class BijectiveComponents {
64
65
66
67
68
69
70
71 private ClosureSystem closureSystem;
72
73
74
75
76
77
78
79
80 private ConceptLattice lattice = null;
81
82
83
84
85 private Lattice reducedLattice = null;
86
87
88
89
90 private ConcreteDGraph dependencyGraph = null;
91
92
93
94
95 private TreeSet<ComparableSet> minimalGenerators = null;
96
97
98
99
100 private ImplicationalSystem canonicalDirectBasis = null;
101
102
103
104
105 private ImplicationalSystem canonicalBasis = null;
106
107
108
109
110 private Context table = null;
111
112
113
114
115
116
117
118 public BijectiveComponents(ClosureSystem closureSystem) {
119 this.initialise(closureSystem);
120 }
121
122
123
124
125
126
127
128
129 public BijectiveComponents initialise(ClosureSystem closureSystem) {
130 this.closureSystem = closureSystem;
131 this.lattice = null;
132 this.reducedLattice = null;
133 this.table = null;
134 this.dependencyGraph = null;
135 this.minimalGenerators = null;
136 this.canonicalDirectBasis = null;
137 this.canonicalBasis = null;
138 return this;
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 public long compute() {
191 this.initialise(closureSystem);
192 long debut = new Date().getTime();
193 this.getLattice();
194 this.getReducedLattice();
195 this.getTable();
196 this.getDependencyGraph();
197 this.getMinimalGenerators();
198 this.getCanonicalDirectBasis();
199 this.getCanonicalBasis();
200 long fin = new Date().getTime();
201 return fin - debut;
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216 public void save(String directory, String name) throws IOException {
217
218 String realDirectory = directory + File.separator + name + "BijectiveComponents" + File.separator;
219 File f = new File(realDirectory);
220 f.mkdir();
221 realDirectory += name;
222 BufferedWriter file = new BufferedWriter(new FileWriter(realDirectory + "Readme.txt"));
223
224 String nameInit = realDirectory + "InitialClosureSystem.txt";
225 this.closureSystem.save(nameInit);
226 String newLine = System.getProperty("line.separator");
227 file.write("-> Initial closure system saved in " + nameInit + ": " + newLine);
228 file.write(this.closureSystem.toString() + newLine);
229
230 String nameLattice = realDirectory + "Lattice.dot";
231 this.getLattice().save(nameLattice);
232 file.write("-> Closed set or concept lattice saved in " + nameLattice + newLine);
233
234 String nameReducedLattice = realDirectory + "ReducedLattice.dot";
235 this.getReducedLattice().save(nameReducedLattice);
236 file.write("-> Reduced lattice saved in " + nameReducedLattice + newLine);
237
238 String nameTable = realDirectory + "Table.txt";
239 this.getTable().save(nameTable);
240 file.write("-> Table of the reduced lattice saved in " + nameTable + newLine);
241 file.write(this.table.toString() + newLine);
242
243 String nameCB = realDirectory + "CanonicalBasis.txt";
244 this.getCanonicalBasis().save(nameCB);
245 file.write("-> Canonical basis saved in " + nameCB + ": " + newLine);
246 file.write(this.canonicalBasis.toString() + newLine);
247
248 String nameCDB = realDirectory + "CanonicalDirectBasis.txt";
249 this.getCanonicalDirectBasis().save(nameCDB);
250 file.write("-> Canonical direct basis of the reduced lattice saved in " + nameCDB + ": " + newLine);
251 file.write(this.canonicalDirectBasis.toString() + newLine);
252
253 String nameODGraph = realDirectory + "DependencyGraph.dot";
254 this.getDependencyGraph().save(nameODGraph);
255 file.write("-> Dependency Graph of the reduced lattice saved in " + nameODGraph + " " + newLine);
256
257 file.write("-> Minimal generators of the reduced lattice are " + this.minimalGenerators + newLine);
258 file.close();
259 }
260
261
262
263
264
265
266 public ClosureSystem getClosureSystem() {
267 return closureSystem;
268 }
269
270
271
272
273
274
275
276
277 protected BijectiveComponents setClosureSystem(ClosureSystem closureSystem) {
278 this.closureSystem = closureSystem;
279 return this;
280 }
281
282
283
284
285
286
287 public ConceptLattice getLattice() {
288 if (lattice == null) {
289 lattice = this.closureSystem.lattice();
290 }
291 return lattice;
292 }
293
294
295
296
297
298
299
300
301 protected BijectiveComponents setLattice(ConceptLattice lattice) {
302 this.lattice = lattice;
303 return this;
304 }
305
306
307
308
309
310
311 public Lattice getReducedLattice() {
312 if (reducedLattice == null) {
313 reducedLattice = this.getLattice().getIrreduciblesReduction();
314 }
315 return reducedLattice;
316 }
317
318
319
320
321
322
323
324
325 protected BijectiveComponents setReducedLattice(Lattice reducedLattice) {
326 this.reducedLattice = reducedLattice;
327 return this;
328 }
329
330
331
332
333
334
335 public ConcreteDGraph getDependencyGraph() {
336 if (dependencyGraph == null) {
337
338 dependencyGraph = this.getReducedLattice().getDependencyGraph();
339 }
340 return dependencyGraph;
341 }
342
343
344
345
346
347
348
349
350 protected BijectiveComponents setDependencyGraph(ConcreteDGraph dependencyGraph) {
351 this.dependencyGraph = dependencyGraph;
352 return this;
353 }
354
355
356
357
358
359
360 public TreeSet<ComparableSet> getMinimalGenerators() {
361 if (minimalGenerators == null) {
362
363 minimalGenerators = this.getReducedLattice().getMinimalGenerators();
364 }
365 return minimalGenerators;
366 }
367
368
369
370
371
372
373 public ImplicationalSystem getCanonicalDirectBasis() {
374 if (canonicalDirectBasis == null) {
375
376 canonicalDirectBasis = this.getReducedLattice().getCanonicalDirectBasis();
377 }
378 return canonicalDirectBasis;
379 }
380
381
382
383
384
385
386 public ImplicationalSystem getCanonicalBasis() {
387 if (canonicalBasis == null) {
388 canonicalBasis = new ImplicationalSystem(this.getCanonicalDirectBasis());
389 canonicalBasis.makeCanonicalBasis();
390 }
391 return canonicalBasis;
392 }
393
394
395
396
397
398
399 public Context getTable() {
400 if (table == null) {
401
402 table = this.getReducedLattice().getTable();
403 }
404 return table;
405 }
406
407
408
409
410
411
412
413
414 protected BijectiveComponents setMinimalGenerators(TreeSet<ComparableSet> minimalGenerators) {
415 this.minimalGenerators = minimalGenerators;
416 return this;
417 }
418
419
420
421
422
423
424
425
426 protected BijectiveComponents setCanonicalDirectBasis(ImplicationalSystem canonicalDirectBasis) {
427 this.canonicalDirectBasis = canonicalDirectBasis;
428 return this;
429 }
430
431
432
433
434
435
436
437
438 protected BijectiveComponents setCanonicalBasis(ImplicationalSystem canonicalBasis) {
439 this.canonicalBasis = canonicalBasis;
440 return this;
441 }
442
443
444
445
446
447
448
449
450 protected BijectiveComponents setTable(Context table) {
451 this.table = table;
452 return this;
453 }
454
455
456
457
458
459
460
461
462
463
464
465
466 }