summaryrefslogtreecommitdiff
path: root/java/src/org/singinst/uf/model/ScalarValueHolder.java
blob: bdf9890e5a85a4ca0fac4f10dd640f2f42ab6165 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
191
192
193
194
195
196
197
198
199
200
201
202
package org.singinst.uf.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.singinst.uf.common.LogUtil;
import org.singinst.uf.common.StringUtil;
import org.singinst.uf.math.MathUtil;
import org.singinst.uf.presenter.LineBounds;
import org.singinst.uf.presenter.Store;
import org.singinst.uf.view.ViewUtil;

public class ScalarValueHolder implements Evaluable {
	@Override
	public String toString() {
		return getScalar().getKey();
	}

	private static final Map<String, ScalarValueHolder> store = new HashMap<String, ScalarValueHolder>();
	private final List<ValueListener> valueListeners = new ArrayList<ValueListener>();
	private double value;
	private final ScalarSchema scalarSchema;
	private double timestamp;
	private Calculation calculation;

	public ScalarValueHolder(ScalarSchema scalarSchema, double value) {
		this.scalarSchema = scalarSchema;
		try {
			Store s = Store.getInstance();
			if (s == null) {
				LogUtil.info("ScalarValueHolder.init(): Store instance is null!");
			}
			if (scalarSchema == null) {
				LogUtil.info("ScalarValueHolder.init(): scalarSchema is null!");
			}
			Double storedValue = s.get(scalarSchema.getKey());
			if (storedValue == null) {
				this.value = value;
			} else {
				this.value = storedValue;
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.value = value;
		}
	}
	
	public synchronized void setValueFromUser(double requestedValue) {
		double fromDisplay = getScalar().getBounds().toDisplay().invert(requestedValue);
		setValue(fromDisplay);
	}
		
	public synchronized void setValue(double requestedValue) {
		double constrainedValue = constrain(requestedValue);
		timestamp = System.currentTimeMillis();
		this.value = constrainedValue;
		notifyListeners();
		
		LogUtil.info("storing with key \"" + scalarSchema.getKey() + "\" and value of "+value);
		try {
			Store s = Store.getInstance();
			s.put(scalarSchema.getKey(), value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void notifyListeners() {
		for (ValueListener valueListener : valueListeners) {
			valueListener.fireUpdate(value);
		}
	}

	private double constrain(double requestedValue) {
		double rounded = MathUtil.round(requestedValue, ScalarSchema.getMaxDecimalDigits());
		LineBounds boundsConstraint = getScalar().getBoundsConstraint();
		if (boundsConstraint != null) {
			return boundsConstraint.constrain(rounded);
		} else {
			return rounded;
		}
	}

	public void addUpdateListener(ValueListener valueListener) {
		valueListeners.add(valueListener);
		valueListener.fireUpdate(getCachedValue());
	}

	public void unshiftUpdateListener(ValueListener valueListener) {
		valueListeners.add(0, valueListener);
		valueListener.fireUpdate(getCachedValue());
	}

	public static ScalarValueHolder findById(ScalarSchema scalarSchema) {
		String key = scalarSchema.getKey();
		ScalarValueHolder retVal = store.get(key);
		if (retVal == null) {
			LineBounds bounds = scalarSchema.getLineBounds();
			retVal = new ScalarValueHolder(scalarSchema, bounds.getMidpoint());
			store.put(key, retVal);
		}
		return retVal;
	}

	public synchronized double getValue() {
		if (calculation != null && needsCalc(new NeedsCalcCache())) {
			calculation.getHtmlConsole().setLength(0);
			setValue(calculation.evaluate());
		}
		return value;
	}

	public synchronized double getCachedValue() {
		return value;
	}

	private static String constructKey(String namespace, String name) {
		return namespace + "." + name;
	}

	public static ScalarValueHolder findById(String namespace, String name) {
		String key = constructKey(namespace, name);
		return findById(key);
	}

	public static ScalarValueHolder findById(String key) {
		ScalarValueHolder scalarValueHolder = store.get(key);
		if (scalarValueHolder == null) {
			throw new NullPointerException("Key not found: " + key + "; available keys are: " + store.keySet());
		} else {
			return scalarValueHolder;
		}
	}

	public static Calculation getCalculation(String namespace, String name) {
		return findById(namespace, name).getCalculation();
	}

	public synchronized boolean needsCalc(NeedsCalcCache needsCalcCache) {
		if (getScalar().getDependencies().isEmpty()) {
			return false;
		}
		if (timestamp == 0) {
			return true;
		}
		
		Set<ScalarValueHolder> otherConjectures = new HashSet<ScalarValueHolder>();
		for (NodeMetadata nodeMetadata : getScalar().getDependencies()) {
			otherConjectures.addAll(nodeMetadata.getScalarValueHolders());
//			for (ScalarSchema otherScalar : nodeMetadata.getScalars()) {
//				otherConjectures.add(otherScalar.getScalarValueHolder());
//			}
			for (ScalarRelation relation : nodeMetadata.getScalarRelations()) {
				otherConjectures.addAll(relation.getScalarValues());
			}
		}
		for (ScalarValueHolder otherConjecture : otherConjectures) {
			if (needsCalcCache.needsCalc(otherConjecture) || timestamp < otherConjecture.timestamp) {
				return true;
			}
		}
		return false;
	}

	public void setCalculation(Calculation calculation) {
		this.calculation = calculation;
	}

	public Calculation getCalculation() {
		return calculation;
	}

	public double evaluate(StringBuilder htmlConsole) {
		double retVal = getValue();
		if (calculation != null) {
//			String percentage = MathUtil.round(retVal * 100, 2) + "%"; //FIXME
//			htmlConsole.append(calculation.getDescription() + " = " + percentage + "<br>");
			htmlConsole.append(calculation.getDescription() + " = " + 
					StringUtil.formatProbability(retVal) + "<br>");
		}
		return retVal;
	}

	public ScalarSchema getScalar() {
		return scalarSchema;
	}

	public static void destroyAll() {
		for (ScalarValueHolder holder: store.values()) {
			holder.destroy();
		}
		store.clear();
	}

	private void destroy() {
		valueListeners.clear();
	}
}