1
2
3
4 package net.sf.javabdd;
5
6 import java.util.Collection;
7 import java.math.BigInteger;
8
9 /***
10 * <p>An implementation of BDDFactory that relies on the CAL library through a
11 * native interface. You can use this by calling the "CALFactory.init()"
12 * method with the desired arguments. This will return you an instance of the
13 * BDDFactory class that you can use. Call "done()" on that instance when you
14 * are finished.</p>
15 *
16 * <p>CAL does not have much of the functionality that BuDDy has, and it has
17 * not been well-tested. Furthermore, it is slower than BuDDy. Therefore, it
18 * is recommended that you use the BuDDy library instead.</p>
19 *
20 * <p>This class (and the CAL library) do NOT support multithreading.
21 * Furthermore, there can be only one instance active at a time. You can only
22 * call "init()" again after you have called "done()" on the original instance.
23 * It is not recommended to call "init()" again after calling "done()" unless
24 * you are _completely_ sure that all BDD objects that reference the old
25 * factory have been freed.</p>
26 *
27 * <p>If you really need multiple BDD factories, consider using the JavaFactory
28 * class for the additional BDD factories --- JavaFactory can have multiple
29 * factory instances active at a time.</p>
30 *
31 * @see net.sf.javabdd.BDDFactory
32 * @see net.sf.javabdd.BuDDyFactory
33 *
34 * @author John Whaley
35 * @version $Id: CALFactory.java 454 2006-07-17 03:45:42Z joewhaley $
36 */
37 public class CALFactory extends BDDFactory {
38
39 public static BDDFactory init(int nodenum, int cachesize) {
40 CALFactory f = new CALFactory();
41 f.initialize(nodenum/256, cachesize);
42 return f;
43 }
44
45 private static CALFactory INSTANCE;
46
47 static {
48 String libname = "cal";
49 try {
50 System.loadLibrary(libname);
51 } catch (java.lang.UnsatisfiedLinkError x) {
52
53 libname = System.mapLibraryName(libname);
54 String currentdir = getProperty("user.dir", ".");
55 String sep = getProperty("file.separator", "/");
56 System.load(currentdir+sep+libname);
57 }
58 registerNatives();
59 }
60
61 private static native void registerNatives();
62
63 private CALFactory() {}
64
65 private static long zero;
66 private static long one;
67
68
69
70
71 public BDD zero() {
72 return new CALBDD(zero);
73 }
74
75
76
77
78 public BDD one() {
79 return new CALBDD(one);
80 }
81
82
83
84
85 protected void initialize(int nodenum, int cachesize) {
86 if (INSTANCE != null) {
87 throw new InternalError("Error: CALFactory already initialized.");
88 }
89 INSTANCE = this;
90 initialize0(nodenum, cachesize);
91 }
92 private static native void initialize0(int nodenum, int cachesize);
93
94
95
96
97 public boolean isInitialized() {
98 return isInitialized0();
99 }
100 private static native boolean isInitialized0();
101
102
103
104
105 public void done() {
106 INSTANCE = null;
107 done0();
108 }
109 private static native void done0();
110
111
112
113
114 public void setError(int code) {
115
116 }
117
118
119
120
121 public void clearError() {
122
123 }
124
125
126
127
128 public int setMaxNodeNum(int size) {
129
130 System.err.println("Warning: setMaxNodeNum() not yet implemented");
131 return 1000000;
132 }
133
134
135
136
137 public double setMinFreeNodes(double x) {
138
139 System.err.println("Warning: setMinFreeNodes() not yet implemented");
140 return 0.;
141 }
142
143
144
145
146 public int setMaxIncrease(int x) {
147
148 System.err.println("Warning: setMaxIncrease() not yet implemented");
149 return 50000;
150 }
151
152
153
154
155 public double setCacheRatio(double x) {
156
157 System.err.println("Warning: setCacheRatio() not yet implemented");
158 return 0;
159 }
160
161
162
163
164 public int varNum() {
165 return varNum0();
166 }
167 private static native int varNum0();
168
169
170
171
172 public int setVarNum(int num) {
173 return setVarNum0(num);
174 }
175 private static native int setVarNum0(int num);
176
177
178
179
180 public int duplicateVar(int var) {
181
182 throw new UnsupportedOperationException();
183 }
184
185
186
187
188 public BDD ithVar(int var) {
189 long id = ithVar0(var);
190 return new CALBDD(id);
191 }
192 private static native long ithVar0(int var);
193
194
195
196
197 public BDD nithVar(int var) {
198 BDD b = ithVar(var);
199 BDD c = b.not(); b.free();
200 return c;
201 }
202
203
204
205
206 public void swapVar(int v1, int v2) {
207
208 throw new UnsupportedOperationException();
209 }
210
211
212
213
214 public BDDPairing makePair() {
215 return new CALBDDPairing();
216 }
217
218
219
220
221 public void printAll() {
222
223 throw new UnsupportedOperationException();
224 }
225
226
227
228
229 public void printTable(BDD b) {
230
231 throw new UnsupportedOperationException();
232 }
233
234
235
236
237 public int level2Var(int level) {
238 return level2Var0(level);
239 }
240 private static native int level2Var0(int level);
241
242
243
244
245 public int var2Level(int var) {
246 return var2Level0(var);
247 }
248 private static native int var2Level0(int var);
249
250
251
252
253 public void reorder(ReorderMethod m) {
254
255 throw new UnsupportedOperationException();
256 }
257
258
259
260
261 public void autoReorder(ReorderMethod method) {
262
263 throw new UnsupportedOperationException();
264 }
265
266
267
268
269 public void autoReorder(ReorderMethod method, int max) {
270
271 throw new UnsupportedOperationException();
272 }
273
274
275
276
277 public ReorderMethod getReorderMethod() {
278
279 throw new UnsupportedOperationException();
280 }
281
282
283
284
285 public int getReorderTimes() {
286
287 throw new UnsupportedOperationException();
288 }
289
290
291
292
293 public void disableReorder() {
294
295 System.err.println("Warning: disableReorder() not yet implemented");
296 }
297
298
299
300
301 public void enableReorder() {
302
303 System.err.println("Warning: enableReorder() not yet implemented");
304 }
305
306
307
308
309 public int reorderVerbose(int v) {
310
311 throw new UnsupportedOperationException();
312 }
313
314
315
316
317 public void setVarOrder(int[] neworder) {
318 setVarOrder0(neworder);
319 }
320 private static native void setVarOrder0(int[] neworder);
321
322
323
324
325 public void addVarBlock(BDD var, boolean fixed) {
326
327 throw new UnsupportedOperationException();
328 }
329
330
331
332
333 public void addVarBlock(int first, int last, boolean fixed) {
334
335 throw new UnsupportedOperationException();
336 }
337
338
339
340
341 public void varBlockAll() {
342
343 throw new UnsupportedOperationException();
344 }
345
346
347
348
349 public void clearVarBlocks() {
350
351 throw new UnsupportedOperationException();
352 }
353
354
355
356
357 public void printOrder() {
358
359 throw new UnsupportedOperationException();
360 }
361
362
363
364
365 public int nodeCount(Collection r) {
366
367 throw new UnsupportedOperationException();
368 }
369
370
371
372
373 public int getNodeTableSize() {
374
375 throw new UnsupportedOperationException();
376 }
377
378
379
380
381 public int getCacheSize() {
382
383 throw new UnsupportedOperationException();
384 }
385
386
387
388
389 public int getNodeNum() {
390
391 throw new UnsupportedOperationException();
392 }
393
394
395
396
397 public int reorderGain() {
398
399 throw new UnsupportedOperationException();
400 }
401
402
403
404
405 public void printStat() {
406
407 throw new UnsupportedOperationException();
408 }
409
410
411
412
413 protected BDDDomain createDomain(int a, BigInteger b) {
414 return new CALBDDDomain(a, b);
415 }
416
417
418
419
420 private static class CALBDD extends BDD {
421
422 /*** The pointer used by the BDD library. */
423 private long _ddnode_ptr;
424
425 /*** An invalid id, for use in invalidating BDDs. */
426 static final long INVALID_BDD = -1;
427
428 private CALBDD(long ddnode) {
429 this._ddnode_ptr = ddnode;
430 addRef(ddnode);
431 }
432
433
434
435
436 public BDDFactory getFactory() {
437 return INSTANCE;
438 }
439
440
441
442
443 public boolean isZero() {
444 return this._ddnode_ptr == zero;
445 }
446
447
448
449
450 public boolean isOne() {
451 return this._ddnode_ptr == one;
452 }
453
454
455
456
457 public int var() {
458 return var0(_ddnode_ptr);
459 }
460 private static native int var0(long b);
461
462
463
464
465 public BDD high() {
466 long b = high0(_ddnode_ptr);
467 return new CALBDD(b);
468 }
469 private static native long high0(long b);
470
471
472
473
474 public BDD low() {
475 long b = low0(_ddnode_ptr);
476 return new CALBDD(b);
477 }
478 private static native long low0(long b);
479
480
481
482
483 public BDD id() {
484 return new CALBDD(_ddnode_ptr);
485 }
486
487
488
489
490 public BDD not() {
491 long b = not0(_ddnode_ptr);
492 return new CALBDD(b);
493 }
494 private static native long not0(long b);
495
496
497
498
499 public BDD ite(BDD thenBDD, BDD elseBDD) {
500 CALBDD c = (CALBDD) thenBDD;
501 CALBDD d = (CALBDD) elseBDD;
502 long b = ite0(_ddnode_ptr, c._ddnode_ptr, d._ddnode_ptr);
503 return new CALBDD(b);
504 }
505 private static native long ite0(long b, long c, long d);
506
507
508
509
510 public BDD relprod(BDD that, BDDVarSet var) {
511 CALBDD c = (CALBDD) that;
512 CALBDD d = (CALBDD) ((BDDVarSet.DefaultImpl) var).b;
513 long b = relprod0(_ddnode_ptr, c._ddnode_ptr, d._ddnode_ptr);
514 return new CALBDD(b);
515 }
516 private static native long relprod0(long b, long c, long d);
517
518
519
520
521 public BDD compose(BDD that, int var) {
522 CALBDD c = (CALBDD) that;
523 long b = compose0(_ddnode_ptr, c._ddnode_ptr, var);
524 return new CALBDD(b);
525 }
526 private static native long compose0(long b, long c, int var);
527
528
529
530
531 public BDD constrain(BDD that) {
532
533 throw new UnsupportedOperationException();
534 }
535
536
537
538
539 public BDD exist(BDDVarSet var) {
540
541 throw new UnsupportedOperationException();
542 }
543
544
545
546
547 public BDD forAll(BDDVarSet var) {
548
549 throw new UnsupportedOperationException();
550 }
551
552
553
554
555 public BDD unique(BDDVarSet var) {
556
557 throw new UnsupportedOperationException();
558 }
559
560
561
562
563 public BDD restrict(BDD var) {
564 CALBDD c = (CALBDD) var;
565 long b = restrict0(_ddnode_ptr, c._ddnode_ptr);
566 return new CALBDD(b);
567 }
568 private static native long restrict0(long b, long var);
569
570
571
572
573 public BDD restrictWith(BDD var) {
574 CALBDD c = (CALBDD) var;
575 long b = restrict0(_ddnode_ptr, c._ddnode_ptr);
576 addRef(b);
577 delRef(_ddnode_ptr);
578 if (this != c) {
579 delRef(c._ddnode_ptr);
580 c._ddnode_ptr = INVALID_BDD;
581 }
582 _ddnode_ptr = b;
583 return this;
584 }
585
586
587
588
589 public BDD simplify(BDDVarSet d) {
590
591 throw new UnsupportedOperationException();
592 }
593
594
595
596
597 public BDDVarSet support() {
598 long b = support0(_ddnode_ptr);
599 return new BDDVarSet.DefaultImpl(new CALBDD(b));
600 }
601 private static native long support0(long b);
602
603
604
605
606 public BDD apply(BDD that, BDDFactory.BDDOp opr) {
607 CALBDD c = (CALBDD) that;
608 long b = apply0(_ddnode_ptr, c._ddnode_ptr, opr.id);
609 return new CALBDD(b);
610 }
611 private static native long apply0(long b, long c, int opr);
612
613
614
615
616 public BDD applyWith(BDD that, BDDFactory.BDDOp opr) {
617 CALBDD c = (CALBDD) that;
618 long b = apply0(_ddnode_ptr, c._ddnode_ptr, opr.id);
619 addRef(b);
620 delRef(_ddnode_ptr);
621 if (this != c) {
622 delRef(c._ddnode_ptr);
623 c._ddnode_ptr = INVALID_BDD;
624 }
625 _ddnode_ptr = b;
626 return this;
627 }
628
629
630
631
632 public BDD applyAll(BDD that, BDDOp opr, BDDVarSet var) {
633
634 throw new UnsupportedOperationException();
635 }
636
637
638
639
640 public BDD applyEx(BDD that, BDDOp opr, BDDVarSet var) {
641
642 throw new UnsupportedOperationException();
643 }
644
645
646
647
648 public BDD applyUni(BDD that, BDDOp opr, BDDVarSet var) {
649
650 throw new UnsupportedOperationException();
651 }
652
653
654
655
656 public BDD satOne() {
657 long b = satOne0(_ddnode_ptr);
658 return new CALBDD(b);
659 }
660 private static native long satOne0(long b);
661
662
663
664
665 public BDD fullSatOne() {
666
667 throw new UnsupportedOperationException();
668 }
669
670
671
672
673 public BDD satOne(BDDVarSet var, boolean pol) {
674
675 throw new UnsupportedOperationException();
676 }
677
678
679
680
681 public int nodeCount() {
682 return nodeCount0(_ddnode_ptr);
683 }
684 private static native int nodeCount0(long b);
685
686
687
688
689 public double pathCount() {
690 return pathCount0(_ddnode_ptr);
691 }
692 private static native double pathCount0(long b);
693
694
695
696
697 public double satCount() {
698 return satCount0(_ddnode_ptr);
699 }
700
701 private static native double satCount0(long b);
702
703
704
705
706 public int[] varProfile() {
707
708 throw new UnsupportedOperationException();
709 }
710
711 private static native void addRef(long p);
712
713 private static native void delRef(long p);
714
715 static final boolean USE_FINALIZER = false;
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739 public void free() {
740 delRef(_ddnode_ptr);
741 _ddnode_ptr = INVALID_BDD;
742 }
743
744
745
746
747 public BDD veccompose(BDDPairing pair) {
748 CALBDDPairing p = (CALBDDPairing) pair;
749 long b = veccompose0(_ddnode_ptr, p._ptr);
750 return new CALBDD(b);
751 }
752 private static native long veccompose0(long b, long p);
753
754
755
756
757 public BDD replace(BDDPairing pair) {
758 CALBDDPairing p = (CALBDDPairing) pair;
759 long b = replace0(_ddnode_ptr, p._ptr);
760 return new CALBDD(b);
761 }
762 private static native long replace0(long b, long p);
763
764
765
766
767 public BDD replaceWith(BDDPairing pair) {
768 CALBDDPairing p = (CALBDDPairing) pair;
769 long b = replace0(_ddnode_ptr, p._ptr);
770 addRef(b);
771 delRef(_ddnode_ptr);
772 _ddnode_ptr = b;
773 return this;
774 }
775
776
777
778
779 public boolean equals(BDD that) {
780 return this._ddnode_ptr == ((CALBDD) that)._ddnode_ptr;
781 }
782
783
784
785
786 public int hashCode() {
787 return (int) this._ddnode_ptr;
788 }
789
790 public BDDVarSet toVarSet() {
791 return new BDDVarSet.DefaultImpl(new CALBDD(this._ddnode_ptr));
792 }
793
794 }
795
796
797
798
799 private static class CALBDDDomain extends BDDDomain {
800
801 private CALBDDDomain(int index, BigInteger range) {
802 super(index, range);
803 }
804
805
806
807
808 public BDDFactory getFactory() {
809 return INSTANCE;
810 }
811
812 }
813
814
815
816
817 private static class CALBDDPairing extends BDDPairing {
818
819 long _ptr;
820
821 private CALBDDPairing() {
822 _ptr = alloc();
823 }
824
825 private static native long alloc();
826
827
828
829
830 public void set(int oldvar, int newvar) {
831 set0(_ptr, oldvar, newvar);
832 }
833 private static native void set0(long p, int oldvar, int newvar);
834
835
836
837
838 public void set(int oldvar, BDD newvar) {
839 CALBDD c = (CALBDD) newvar;
840 set2(_ptr, oldvar, c._ddnode_ptr);
841 }
842 private static native void set2(long p, int oldvar, long newbdd);
843
844
845
846
847 public void reset() {
848 reset0(_ptr);
849 }
850 private static native void reset0(long ptr);
851
852 /***
853 * Free the memory allocated for this pair.
854 */
855 private static native void free0(long ptr);
856 }
857
858
859
860
861 protected BDDBitVector createBitVector(int a) {
862 return new CALBDDBitVector(a);
863 }
864
865
866
867
868 private static class CALBDDBitVector extends BDDBitVector {
869
870 private CALBDDBitVector(int a) {
871 super(a);
872 }
873
874 public BDDFactory getFactory() { return INSTANCE; }
875
876 }
877
878
879
880
881 public double setIncreaseFactor(double x) {
882 System.err.println("Warning: setIncreaseFactor() not yet implemented");
883 return 0;
884 }
885
886
887
888
889 public int setNodeTableSize(int n) {
890 System.err.println("Warning: setNodeTableSize() not yet implemented");
891 return 0;
892 }
893
894
895
896
897 public int setCacheSize(int n) {
898 System.err.println("Warning: setCacheSize() not yet implemented");
899 return 0;
900 }
901
902 public static final String REVISION = "$Revision: 454 $";
903
904
905
906
907 public String getVersion() {
908 return "CAL "+REVISION.substring(11, REVISION.length()-2);
909 }
910
911 }