Skip to content

Commit 46c9350

Browse files
author
woortwij
committed
#66 Adding callback mechanisms for saturation
1 parent bd49a74 commit 46c9350

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/main/java/com/github/javabdd/BDDFactory.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,34 @@ public static interface ContinuousStatsCallback {
21852185
public void continuous(int usedBddNodes, long opMiss);
21862186
}
21872187

2188+
/** Saturation callback with simple information. */
2189+
@FunctionalInterface
2190+
public static interface SaturationSimpleCallback {
2191+
/**
2192+
* The callback function that is invoked after every transition application performed by saturation.
2193+
*
2194+
* @param transition The index of the transition that was applied.
2195+
*/
2196+
public void accept(int transition);
2197+
}
2198+
2199+
/**
2200+
* Saturation callback with debug information, notably the BDDs before and after applying a transition.
2201+
*
2202+
* @param <T> The BDD representation type.
2203+
*/
2204+
@FunctionalInterface
2205+
public static interface SaturationDebugCallback<T> {
2206+
/**
2207+
* The callback function that is invoked after every transition application performed by saturation.
2208+
*
2209+
* @param transition The index of the transition that was applied.
2210+
* @param before The BDD to which the transition was applied.
2211+
* @param after The resulting BDD after applying the transition.
2212+
*/
2213+
public void accept(int transition, T before, T after);
2214+
}
2215+
21882216
/** The registered garbage collection statistics callbacks, or {@code null} if none registered. */
21892217
protected List<GCStatsCallback> gcCallbacks = null;
21902218

@@ -2293,6 +2321,23 @@ public void registerContinuousStatsCallback(ContinuousStatsCallback callback) {
22932321
continuousCallbacks.add(callback);
22942322
}
22952323

2324+
/**
2325+
* Sets the callback function that is invoked after every transition application performed by saturation.
2326+
*
2327+
* @param callback The non-{@code null} callback function.
2328+
*/
2329+
public abstract void setSaturationCallback(SaturationSimpleCallback callback);
2330+
2331+
/**
2332+
* Sets the callback function that is invoked after every transition application performed by saturation.
2333+
*
2334+
* @param callback The non-{@code null} callback function.
2335+
*/
2336+
public abstract void setSaturationCallback(SaturationDebugCallback<BDD> callback);
2337+
2338+
/** Unsets the saturation callback function. */
2339+
public abstract void unsetSaturationCallback();
2340+
22962341
/**
22972342
* Unregister a garbage collection statistics callback.
22982343
*

src/main/java/com/github/javabdd/BDDFactoryIntImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ public abstract class BDDFactoryIntImpl extends BDDFactory {
132132

133133
protected abstract void printTable_impl(/* bdd */int v);
134134

135+
protected abstract void setSaturationCallback_impl(SaturationDebugCallback<Integer> callback);
136+
137+
@Override
138+
public void setSaturationCallback(SaturationSimpleCallback callback) {
139+
setSaturationCallback_impl((transition, before, after) -> callback.accept(transition));
140+
}
141+
142+
@Override
143+
public void setSaturationCallback(SaturationDebugCallback<BDD> callback) {
144+
setSaturationCallback_impl((transition, before, after) -> {
145+
BDD beforeBdd = makeBDD(before);
146+
BDD afterBdd = makeBDD(after);
147+
callback.accept(transition, beforeBdd, afterBdd);
148+
beforeBdd.free();
149+
afterBdd.free();
150+
});
151+
}
152+
153+
@Override
154+
public void unsetSaturationCallback() {
155+
setSaturationCallback_impl(null);
156+
}
157+
135158
public class IntBDD extends BDD {
136159
protected /* bdd */int v;
137160

src/main/java/com/github/javabdd/JFactory.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public class JFactory extends BDDFactoryIntImpl {
5050

5151
static final boolean SWAPCOUNT = false;
5252

53+
/** The default saturation callback function that does nothing. */
54+
private static final SaturationDebugCallback<Integer> DEFAULT_SATURATION_CALLBACK = (t, b, a) -> { };
55+
56+
/**
57+
* The non-{@code null} saturation callback function that is invoked after every transition application performed by
58+
* saturation.
59+
*/
60+
private SaturationDebugCallback<Integer> saturationCallback = DEFAULT_SATURATION_CALLBACK;
61+
5362
private JFactory() {
5463
}
5564

@@ -4061,7 +4070,9 @@ int saturationForward_rec(int states, int[] relations, int[] vars, int instance,
40614070

40624071
for (int i = current; i < next; i++) {
40634072
PUSHREF(result);
4073+
int prevResult = result;
40644074
result = relnextUnion_rec(result, relations[i], result, vars[i]);
4075+
saturationCallback.accept(i, prevResult, result);
40654076
POPREF(1);
40664077
}
40674078

@@ -4229,7 +4240,9 @@ int boundedSaturationForward_rec(int states, int bound, int[] relations, int[] v
42294240

42304241
for (int i = current; i < next; i++) {
42314242
PUSHREF(result);
4243+
int prevResult = result;
42324244
result = or_rec(PUSHREF(relnextIntersection_rec(result, relations[i], bound, vars[i])), result);
4245+
saturationCallback.accept(i, prevResult, result);
42334246
POPREF(2);
42344247
}
42354248

@@ -4369,7 +4382,9 @@ int saturationBackward_rec(int states, int[] relations, int[] vars, int instance
43694382

43704383
for (int i = current; i < next; i++) {
43714384
PUSHREF(result);
4385+
int prevResult = result;
43724386
result = relprevUnion_rec(relations[i], result, result, vars[i]);
4387+
saturationCallback.accept(i, prevResult, result);
43734388
POPREF(1);
43744389
}
43754390

@@ -4537,7 +4552,9 @@ int boundedSaturationBackward_rec(int states, int bound, int[] relations, int[]
45374552

45384553
for (int i = current; i < next; i++) {
45394554
PUSHREF(result);
4555+
int prevResult = result;
45404556
result = or_rec(PUSHREF(relprevIntersection_rec(relations[i], result, bound, vars[i])), result);
4557+
saturationCallback.accept(i, prevResult, result);
45414558
POPREF(2);
45424559
}
45434560

@@ -4560,6 +4577,11 @@ int boundedSaturationBackward_rec(int states, int bound, int[] relations, int[]
45604577
return result;
45614578
}
45624579

4580+
@Override
4581+
protected void setSaturationCallback_impl(SaturationDebugCallback<Integer> callback) {
4582+
saturationCallback = callback == null ? DEFAULT_SATURATION_CALLBACK : callback;
4583+
}
4584+
45634585
int relprod_rec(int l, int r) {
45644586
BddCacheDataI entry;
45654587
int res;

0 commit comments

Comments
 (0)