blob: 5ab258537c1e430efba57bcdfc0a5b8d2553d18f (
plain)
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
|
package org.singinst.uf.model;
import java.util.List;
public class NormalConstraint {
private final double lowerBound;
private final List<ScalarSchema> scalarSchemata;
private boolean disable;
public NormalConstraint(List<ScalarSchema> scalarSchemata, double lowerBound) {
this.scalarSchemata = scalarSchemata;
this.lowerBound = lowerBound;
}
private final ValueListener leftAnchorListener = new MooreUpdateListener() {
void adjust() {
getRightConjecture().setValue(extrapolate(getLeftConjecture(), getMiddleConjecture()));
}
};
private final ValueListener rightAnchorListener = new MooreUpdateListener() {
void adjust() {
getLeftConjecture().setValue(extrapolate(getRightConjecture(), getMiddleConjecture()));
}
};
public void constrain() {
getLeftConjecture().addUpdateListener(leftAnchorListener);
getMiddleConjecture().addUpdateListener(leftAnchorListener);
getRightConjecture().addUpdateListener(rightAnchorListener);
}
protected double extrapolate(ScalarValueHolder firstConjecture,
ScalarValueHolder secondConjecture) {
return secondConjecture.getValue() * 2 - firstConjecture.getValue();
}
private ScalarValueHolder getConjecture(int i) {
return scalarSchemata.get(i).getScalarValueHolder();
}
private ScalarValueHolder getLeftConjecture() {
return getConjecture(0);
}
private ScalarValueHolder getMiddleConjecture() {
return getConjecture(1);
}
private ScalarValueHolder getRightConjecture() {
return getConjecture(2);
}
private abstract class MooreUpdateListener implements ValueListener {
public void fireUpdate(double value) {
if (!disable) {
disable = true;
try {
adjust();
fixLeftToRight();
} finally {
disable = false;
}
}
}
abstract void adjust();
private void fixLeftToRight() {
double left = getLeftConjecture().getValue();
double right = getRightConjecture().getValue();
if (left > right) {
getLeftConjecture().setValue(right);
getRightConjecture().setValue(left);
}
if (getLeftConjecture().getValue() < lowerBound) {
if (getMiddleConjecture().getValue() < lowerBound) {
getMiddleConjecture().setValue(lowerBound);
}
getLeftConjecture().setValue(lowerBound);
getRightConjecture().setValue(extrapolate(getLeftConjecture(), getMiddleConjecture()));
}
}
}
}
|