1 package org.thegalactic.lattice;
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.Set;
18 import java.util.SortedSet;
19 import java.util.TreeSet;
20
21 import org.thegalactic.context.Context;
22 import org.thegalactic.dgraph.DAGraph;
23 import org.thegalactic.dgraph.ConcreteDGraph;
24 import org.thegalactic.dgraph.Edge;
25 import org.thegalactic.dgraph.Node;
26 import org.thegalactic.util.ComparableSet;
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 public class Concept extends Node {
58
59
60
61
62
63
64
65 private ComparableSet setA = null;
66
67
68
69
70 private ComparableSet setB = null;
71
72
73
74
75
76
77
78
79
80
81
82 public Concept(TreeSet<Comparable> setA, TreeSet<Comparable> setB) {
83 this.setA = new ComparableSet(setA);
84 this.setB = new ComparableSet(setB);
85 }
86
87
88
89
90
91
92
93
94
95 public Concept(boolean setA, boolean setB) {
96 if (setA) {
97 this.setA = new ComparableSet();
98 }
99 if (setB) {
100 this.setB = new ComparableSet();
101 }
102 }
103
104
105
106
107
108
109
110
111
112 public Concept(TreeSet<Comparable> setA, boolean setB) {
113 this.setA = new ComparableSet(setA);
114 if (setB) {
115 this.setB = new ComparableSet();
116 }
117 }
118
119
120
121
122
123
124
125
126
127 public Concept(boolean setA, TreeSet<Comparable> setB) {
128 this.setB = new ComparableSet(setB);
129 if (setA) {
130 this.setA = new ComparableSet();
131 }
132 }
133
134
135
136
137
138
139 public Concept(Concept c) {
140 if (c.hasSetA()) {
141 this.setA = new ComparableSet(c.getSetA());
142 } else {
143 this.setA = null;
144 }
145 if (c.hasSetB()) {
146 this.setB = new ComparableSet(c.getSetB());
147 } else {
148 this.setB = null;
149 }
150 }
151
152
153
154
155
156
157
158
159
160 @Override
161 public Concept clone() {
162 if (this.hasSetA() && this.hasSetB()) {
163 TreeSet setA = (TreeSet) this.getSetA().clone();
164 TreeSet setB = (TreeSet) this.getSetB().clone();
165 return new Concept(setA, setB);
166 }
167 if (this.hasSetA() && !this.hasSetB()) {
168 TreeSet setA = (TreeSet) this.getSetA().clone();
169 return new Concept(setA, false);
170 } else if (this.hasSetB()) {
171 TreeSet setB = (TreeSet) this.getSetB().clone();
172 return new Concept(false, setB);
173 } else {
174 return new Concept(false, false);
175 }
176 }
177
178
179
180
181
182
183 public boolean hasSetB() {
184 return this.setB != null;
185 }
186
187
188
189
190
191
192 public boolean hasSetA() {
193 return this.setA != null;
194 }
195
196
197
198
199
200
201 public TreeSet<Comparable> getSetA() {
202 return this.setA;
203 }
204
205
206
207
208
209
210 public TreeSet<Comparable> getSetB() {
211 return this.setB;
212 }
213
214
215
216
217
218
219
220
221 public boolean containsInA(Comparable x) {
222 if (this.hasSetA()) {
223 return this.setA.contains(x);
224 } else {
225 return false;
226 }
227 }
228
229
230
231
232
233
234
235
236 public boolean containsInB(Comparable x) {
237 if (this.hasSetB()) {
238 return this.setB.contains(x);
239 } else {
240 return false;
241 }
242 }
243
244
245
246
247
248
249
250
251 public boolean containsAllInA(TreeSet x) {
252 if (this.hasSetA()) {
253 return this.setA.containsAll(x);
254 } else {
255 return false;
256 }
257 }
258
259
260
261
262
263
264
265
266 public boolean containsAllInB(TreeSet x) {
267 if (this.hasSetB()) {
268 return this.setB.containsAll(x);
269 } else {
270 return false;
271 }
272 }
273
274
275
276
277
278
279 public void putSetB(ComparableSet x) {
280 if (this.hasSetB()) {
281 this.setB = x;
282 } else {
283 this.setB = new ComparableSet(x);
284 }
285 }
286
287
288
289
290
291
292 public void putSetA(ComparableSet x) {
293 if (this.hasSetA()) {
294 this.setA = x;
295 } else {
296 this.setA = new ComparableSet(x);
297 }
298 }
299
300
301
302
303
304
305
306
307 public boolean addToA(Comparable x) {
308 if (this.hasSetA()) {
309 return this.setA.add(x);
310 } else {
311 return false;
312 }
313 }
314
315
316
317
318
319
320
321
322 public boolean addToB(Comparable x) {
323 if (this.hasSetB()) {
324 return this.setB.add(x);
325 } else {
326 return false;
327 }
328 }
329
330
331
332
333
334
335
336
337 public boolean addAllToA(TreeSet x) {
338 if (this.hasSetA()) {
339 return this.setA.addAll(x);
340 } else {
341 return false;
342 }
343 }
344
345
346
347
348
349
350
351
352 public boolean addAllToB(TreeSet x) {
353 if (this.hasSetB()) {
354 return this.setB.addAll(x);
355 } else {
356 return false;
357 }
358 }
359
360
361
362
363
364
365
366
367 public boolean removeFromA(Comparable x) {
368 if (this.hasSetA()) {
369 return this.setA.remove(x);
370 } else {
371 return false;
372 }
373 }
374
375
376
377
378
379
380
381
382 public boolean removeFromB(Comparable x) {
383 if (this.hasSetB()) {
384 return this.setB.remove(x);
385 } else {
386 return false;
387 }
388 }
389
390
391
392
393
394
395
396
397 public boolean removeAllFromA(TreeSet x) {
398 if (this.hasSetA()) {
399 return this.setA.removeAll(x);
400 } else {
401 return false;
402 }
403 }
404
405
406
407
408
409
410
411
412 public boolean removeAllFromB(TreeSet x) {
413 if (this.hasSetB()) {
414 return this.setB.removeAll(x);
415 } else {
416 return false;
417 }
418 }
419
420
421
422
423
424
425
426
427
428 @Override
429 public String toString() {
430 StringBuilder builder = new StringBuilder();
431 if (this.hasSetA()) {
432 builder.append(this.setA);
433 }
434 if (this.hasSetA() && this.hasSetB()) {
435 builder.append('-');
436 }
437 if (this.hasSetB()) {
438 builder.append(this.setB);
439 }
440 return builder.toString();
441 }
442
443
444
445
446
447
448 @Override
449 public int hashCode() {
450 return super.hashCode();
451 }
452
453
454
455
456
457
458
459
460 @Override
461 public boolean equals(Object o) {
462 if (!(o instanceof Concept)) {
463 return false;
464 }
465 if (!this.hasSetB()) {
466 return this.setA.equals(((Concept) o).setA);
467 }
468 if (!this.hasSetA()) {
469 return this.setB.equals(((Concept) o).setB);
470 }
471 return this.setA.equals(((Concept) o).setA) && this.setB.equals(((Concept) o).setB);
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507 public ArrayList<TreeSet<Comparable>> immediateSuccessorsLOA(Context init) {
508 ArrayList<TreeSet<Comparable>> succB = new ArrayList();
509 TreeSet<Comparable> attributes = (TreeSet<Comparable>) init.getSet().clone();
510 attributes.removeAll(this.getSetA());
511
512 boolean add;
513 for (Comparable x : attributes) {
514 add = true;
515 Iterator it = succB.iterator();
516 while (it.hasNext()) {
517 TreeSet tX = (TreeSet) it.next();
518 TreeSet<Comparable> bx = (TreeSet<Comparable>) this.getSetA().clone();
519 bx.add(x);
520 TreeSet<Comparable> bX = (TreeSet<Comparable>) this.getSetA().clone();
521 bX.addAll(tX);
522 TreeSet<Comparable> bXx = (TreeSet<Comparable>) bX.clone();
523 bXx.add(x);
524 int cBx = count(init, bx);
525 int cBX = count(init, bX);
526 int cBXx = count(init, bXx);
527 if (cBx == cBX) {
528 if (cBXx == cBx) {
529 it.remove();
530 TreeSet<Comparable> xX = new TreeSet();
531 xX.addAll(tX);
532 xX.add(x);
533 succB.add(xX);
534 add = false;
535 break;
536 }
537 }
538 if (cBx < cBX) {
539 if (cBXx == cBx) {
540 add = false;
541 break;
542 }
543 }
544 if (cBx > cBX) {
545 if (cBXx == cBX) {
546 it.remove();
547 }
548 }
549 }
550 if (add) {
551 TreeSet<Comparable> t = new TreeSet();
552 t.add(x);
553 succB.add(new TreeSet(t));
554 }
555 }
556 for (TreeSet t : succB) {
557 t.addAll(this.getSetA());
558 }
559 return succB;
560 }
561
562
563
564
565
566
567
568
569
570
571
572 private int count(Context init, TreeSet<Comparable> attributes) {
573 return init.getExtentNb(attributes);
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604 public ArrayList<TreeSet<Comparable>> immediateSuccessors(ClosureSystem init) {
605
606 ConcreteDGraph<Comparable, ?> dependenceGraph = new ConcreteDGraph<Comparable, Object>();
607 for (Comparable c : init.getSet()) {
608 dependenceGraph.addNode(new Node(c));
609 }
610
611
612
613
614 ComparableSet f = new ComparableSet(this.getSetA());
615 long start = System.currentTimeMillis();
616 System.out.print("Precedence graph... ");
617 ConcreteDGraph prec = init.precedenceGraph();
618 System.out.println(System.currentTimeMillis() - start + "ms");
619 start = System.currentTimeMillis();
620 System.out.print("Srongly connected component... ");
621 DAGraph<SortedSet<Node<Comparable>>, ?> acyclPrec = prec.getStronglyConnectedComponent();
622 System.out.println(System.currentTimeMillis() - start + "ms");
623 ComparableSet newVal = new ComparableSet();
624 newVal.addAll(f);
625 for (Object x : f) {
626
627 Node<SortedSet<Node<Comparable>>> nx = null;
628 for (Node cc : acyclPrec.getNodes()) {
629 TreeSet<Node> cC = (TreeSet<Node>) cc.getContent();
630 for (Node y : cC) {
631 if (x.equals(y.getContent())) {
632 nx = cc;
633 }
634 }
635 }
636
637 SortedSet<Node<SortedSet<Node<Comparable>>>> ccMinNx = acyclPrec.minorants(nx);
638
639 for (Node cc : ccMinNx) {
640 TreeSet<Node> cC = (TreeSet<Node>) cc.getContent();
641 for (Node y : cC) {
642 newVal.remove(y.getContent());
643 }
644 }
645 }
646
647 Set<Node<Comparable>> n = new TreeSet<Node<Comparable>>();
648 for (Node in : dependenceGraph.getNodes()) {
649 if (!f.contains(in.getContent())) {
650 n.add(in);
651 }
652 }
653 System.out.print("Dependance... ");
654 start = System.currentTimeMillis();
655
656
657 TreeSet<Edge> e = new TreeSet<Edge>();
658 for (Node source : n) {
659 for (Node target : n) {
660 if (!source.equals(target)) {
661
662
663 ComparableSet fPlusTo = new ComparableSet(f);
664 fPlusTo.add(target.getContent());
665 fPlusTo = new ComparableSet(init.closure(fPlusTo));
666 if (fPlusTo.contains(source.getContent())) {
667
668
669 Edge ed = dependenceGraph.getEdge(source, target);
670 if (ed == null) {
671 ed = new Edge(source, target, new TreeSet<ComparableSet>());
672 dependenceGraph.addEdge(ed);
673 }
674 e.add(ed);
675
676 ((TreeSet<ComparableSet>) ed.getContent()).add(newVal);
677 TreeSet<ComparableSet> valEd = new TreeSet<ComparableSet>((TreeSet<ComparableSet>) ed.getContent());
678 for (ComparableSet x1 : valEd) {
679 if (x1.containsAll(newVal) && !newVal.containsAll(x1)) {
680 ((TreeSet<ComparableSet>) ed.getContent()).remove(x1);
681 }
682 if (!x1.containsAll(newVal) && newVal.containsAll(x1)) {
683 ((TreeSet<ComparableSet>) ed.getContent()).remove(newVal);
684 }
685 }
686 }
687 }
688 }
689 }
690 System.out.println(System.currentTimeMillis() - start + "ms");
691 System.out.print("Subgraph... ");
692 start = System.currentTimeMillis();
693
694
695 ConcreteDGraph sub = dependenceGraph.getSubgraphByNodes(n);
696 ConcreteDGraph delta = sub.getSubgraphByEdges(e);
697
698
699 DAGraph cfc = delta.getStronglyConnectedComponent();
700 SortedSet<Node> sccmin = cfc.getSinks();
701 System.out.println(System.currentTimeMillis() - start + "ms");
702 ArrayList<TreeSet<Comparable>> immSucc = new ArrayList<TreeSet<Comparable>>();
703 for (Node n1 : sccmin) {
704 TreeSet s = new TreeSet(f);
705 TreeSet<Node> toadd = (TreeSet<Node>) n1.getContent();
706 for (Node n2 : toadd) {
707 s.add(n2.getContent());
708 }
709 immSucc.add(s);
710 }
711 return immSucc;
712 }
713 }