summaryrefslogtreecommitdiff
path: root/java/src/org/singinst/uf/model/CalculationRelation.java
blob: e4b3004a64d4de5e734153afd791b47c931afb80 (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
package org.singinst.uf.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.singinst.uf.math.SimplePoint;
import org.singinst.uf.presenter.Completion;
import org.singinst.uf.presenter.ScalarValuePointList;
import org.singinst.uf.presenter.LineBounds;

public abstract class CalculationRelation extends ScalarRelation {

	private final Node node;

	private final List<ScalarSchema> scalarSchemata = new ArrayList<ScalarSchema>();
	private final List<ScalarValueHolder> scalarValueHolders = new ArrayList<ScalarValueHolder>();
	private final StringBuilder htmlConsole = new StringBuilder();
	private final ScalarValueDependency dependency = new ScalarValueDependency();

	private final ConclusionReportGenerator conclusion = new ConclusionReportGenerator() {

		public String getText(ScalarValueHolder scalarValueHolder, double value) {
			return htmlConsole.toString();
		}

	};

	private final ScalarValuePointList curveList = new ScalarValuePointList() {

		@Override
		public Runnable updater() {
			return new Runnable() {

				public void run() {
					Completion completion = Completion.getInstance();
					if (completion.init("Calculating " + node.getIdString(), ModelUtil.LATEST_YEAR - ModelUtil.EARLIEST_YEAR)) {
						for (int year = ModelUtil.EARLIEST_YEAR; year <= ModelUtil.LATEST_YEAR; year++) {
							ScalarValueHolder scalarValueHolder = ScalarValueHolder.findById(node.getIdString(), ScalarSubIDString.yearProbabilityID(year));
							scalarValueHolder.getValue();
							completion.tick();
						}
					}
				}

			};
		}

	};

	public CalculationRelation(Node node) {
		super(new Axis(new LineBounds(ModelUtil.EARLIEST_YEAR, ModelUtil.LATEST_YEAR)),
				new Axis(new LineBounds(0, 1)));
		this.node = node;
		
		List<Double> yearList = new ArrayList<Double>();
		for (double year = ModelUtil.EARLIEST_YEAR; year <= ModelUtil.LATEST_UNACCELERATED_YEAR; year += ModelUtil.YEAR_STEPSIZE) {
			yearList.add((double) year);
		}
		List<Calculation> calculations = getCalculations(yearList);
		
		for (double year = ModelUtil.EARLIEST_YEAR; year <= ModelUtil.LATEST_UNACCELERATED_YEAR; year += ModelUtil.YEAR_STEPSIZE) {
			ScalarSchema scalarSchema = new ScalarSchema(
					node, ScalarSubIDString.yearProbabilityID(year), new LineBounds(0, 1), "", "", "", null, true);
			scalarSchemata.add(scalarSchema);
			ScalarValueHolder scalarValueHolder = scalarSchema.getScalarValueHolder();
			if (year <= ModelUtil.LATEST_YEAR && ((year % 1d) == 0d) /* could use elementOf to ModelUtil.BASIC_MODEL_YEARS? */ ) {
				scalarValueHolders.add(scalarValueHolder);
			}
			scalarValueHolder.setCalculation(calculations.remove(0));
		}
	}

	protected abstract List<Calculation> getCalculations(List<Double> year);

	@Override
	public List<ScalarValuePointList> getPointLists() {
		List<SimplePoint> pointList = new ArrayList<SimplePoint>();
		ScalarValueHolder lastScalarValue = null;
		Boolean needsCalc = null;
		for (int year = ModelUtil.EARLIEST_YEAR; year <= ModelUtil.LATEST_YEAR; year++) {
			lastScalarValue = ScalarValueHolder.findById(node.getIdString(), ScalarSubIDString.yearProbabilityID(year));
			if (needsCalc == null) {
				if (lastScalarValue.needsCalc(new NeedsCalcCache())) {
					curveList.setHypothesisPoints(null);
					return Collections.singletonList(curveList);
				} else {
					needsCalc = false;
				}
			}
			double value = lastScalarValue.getValue();

			// TODO4
			//				dependency.validate();

			pointList.add(new SimplePoint(year, value));
		}
		if (lastScalarValue != null) {
			StringBuilder newConsoleText = lastScalarValue.getCalculation().getHtmlConsole();;
			if (this instanceof AiRelation)  {
				newConsoleText = new StringBuilder();
				lastScalarValue.getCalculation().setHtmlConsole( newConsoleText );
			}
			
			if (!htmlConsole.toString().equals(newConsoleText.toString())) {
				htmlConsole.setLength(0);
				htmlConsole.append(newConsoleText);
				lastScalarValue.notifyListeners();
			}
		}
		curveList.setHypothesisPoints(pointList);
		return Collections.singletonList(curveList);
	}	

	public List<? extends ConclusionReportGenerator> getConclusionGenerators() {
		return Collections.singletonList(conclusion);
	}

	public List<ScalarValueHolder> getScalarValues() {
		return scalarValueHolders;
	}

	protected ScalarValueDependency getDependency() {
		return dependency;
	}

	protected Calculation probabilityAiIsPossible() {
		return new Calculation("Probability that AI is possible in principle") {
			@Override
			protected double rawEvaluate(StringBuilder htmlConsole) {
				return getDependency().value(NodeIDString.Q1_1, ScalarSubIDString.PROBABILITY) / 100;
			}
			
			@Override
			public Color getColor(){
				//return Color.GREEN;
				return new Color(0, 100, 50); // Dark green
			}
			
			
		};
	}
}