aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/ModelImpl.java
blob: 6d7f4e973650c87c1e0ab8a1886de828aa4fe6e8 (plain) (blame)
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
package org.eclipse.viatra.solver.data.model.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
import org.eclipse.viatra.solver.data.map.Cursor;
import org.eclipse.viatra.solver.data.map.DiffCursor;
import org.eclipse.viatra.solver.data.map.VersionedMap;
import org.eclipse.viatra.solver.data.map.internal.MapDiffCursor;
import org.eclipse.viatra.solver.data.model.Model;
import org.eclipse.viatra.solver.data.model.ModelDiffCursor;
import org.eclipse.viatra.solver.data.model.ModelStore;
import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;

public class ModelImpl implements Model {
	private final ModelStore store;
	private final Map<DataRepresentation<?, ?>, VersionedMap<?, ?>> maps;

	public ModelImpl(ModelStore store, Map<DataRepresentation<?, ?>, VersionedMap<?, ?>> maps) {
		this.store = store;
		this.maps = maps;
	}

	@Override
	public Set<DataRepresentation<?, ?>> getDataRepresentations() {
		return maps.keySet();
	}

	@SuppressWarnings("unchecked")
	private <K, V> VersionedMap<K, V> getMap(DataRepresentation<K, V> representation) {
		if (maps.containsKey(representation)) {
			return (VersionedMap<K, V>) maps.get(representation);
		} else {
			throw new IllegalArgumentException("Model does have representation " + representation);
		}
	}

	private <K, V> VersionedMap<K, V> getMapValidateKey(DataRepresentation<K, V> representation, K key) {
		if (representation.isValidKey(key)) {
			return getMap(representation);
		} else {
			throw new IllegalArgumentException(
					"Key is not valid for representation! (representation=" + representation + ", key=" + key + ");");
		}
	}

	@Override
	public <K, V> V get(DataRepresentation<K, V> representation, K key) {
		return getMapValidateKey(representation, key).get(key);
	}

	@Override
	public <K, V> Cursor<K, V> getAll(DataRepresentation<K, V> representation) {
		return getMap(representation).getAll();
	}

	@Override
	public <K, V> V put(DataRepresentation<K, V> representation, K key, V value) {
		return getMapValidateKey(representation, key).put(key, value);
	}

	@Override
	public <K, V> void putAll(DataRepresentation<K, V> representation, Cursor<K, V> cursor) {
		getMap(representation).putAll(cursor);
	}

	@Override
	public <K, V> long getSize(DataRepresentation<K, V> representation) {
		return getMap(representation).getSize();
	}

	@Override
	public ModelDiffCursor getDiffCursor(long to) {
		Model toModel = store.createModel(to);
		Map<DataRepresentation<?, ?>, DiffCursor<?, ?>> diffCursors = new HashMap<>();
		for (DataRepresentation<?, ?> representation : this.maps.keySet()) {
			MapDiffCursor<?, ?> diffCursor = constructDiffCursor(toModel, representation);
			diffCursors.put(representation, diffCursor);
		}
		return new ModelDiffCursor(diffCursors);
	}

	private <K, V> MapDiffCursor<K, V> constructDiffCursor(Model toModel, DataRepresentation<K, V> representation) {
		@SuppressWarnings("unchecked")
		Cursor<K, V> fromCursor = (Cursor<K, V>) this.maps.get(representation).getAll();
		Cursor<K, V> toCursor = toModel.getAll(representation);

		ContinousHashProvider<K> hashProvider = representation.getHashProvider();
		V defaultValue = representation.getDefaultValue();
		return new MapDiffCursor<>(hashProvider, defaultValue, fromCursor, toCursor);
	}

	@Override
	public long commit() {
		long version = 0;
		boolean versionSet = false;
		for (VersionedMap<?, ?> map : maps.values()) {
			long newVersion = map.commit();
			if (versionSet) {
				if (version != newVersion) {
					throw new IllegalStateException(
							"Maps in model have different versions! (" + version + " and" + newVersion + ")");
				}
			} else {
				version = newVersion;
				versionSet = true;
			}
		}
		return version;
	}

	@Override
	public void restore(long state) {
		if(store.getStates().contains(state)) {
			for (VersionedMap<?, ?> map : maps.values()) {
				map.restore(state);
			}
		} else {
			throw new IllegalArgumentException("Map does not contain state "+state+"!");
		}
	}
}