diff options
author | 2021-09-29 02:45:57 +0200 | |
---|---|---|
committer | 2021-09-29 03:16:01 +0200 | |
commit | a155f6ba02e08a75ce6e474a86900b8363f506e8 (patch) | |
tree | b78804c1c0f0968a9625f0656e08f5dadc16924c /store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java | |
parent | Simplify branding (diff) | |
download | refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.gz refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.zst refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.zip |
build: migration to Gradle 7
Diffstat (limited to 'store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java')
-rw-r--r-- | store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java new file mode 100644 index 00000000..f5e3b141 --- /dev/null +++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java | |||
@@ -0,0 +1,127 @@ | |||
1 | package org.eclipse.viatra.solver.data.model; | ||
2 | |||
3 | import java.util.HashMap; | ||
4 | import java.util.HashSet; | ||
5 | import java.util.LinkedList; | ||
6 | import java.util.List; | ||
7 | import java.util.Map; | ||
8 | import java.util.Map.Entry; | ||
9 | import java.util.Set; | ||
10 | |||
11 | import org.eclipse.viatra.solver.data.map.ContinousHashProvider; | ||
12 | import org.eclipse.viatra.solver.data.map.DiffCursor; | ||
13 | import org.eclipse.viatra.solver.data.map.VersionedMap; | ||
14 | import org.eclipse.viatra.solver.data.map.VersionedMapStore; | ||
15 | import org.eclipse.viatra.solver.data.map.VersionedMapStoreImpl; | ||
16 | import org.eclipse.viatra.solver.data.model.internal.ModelImpl; | ||
17 | import org.eclipse.viatra.solver.data.model.internal.SimilarRelationEquivalenceClass; | ||
18 | import org.eclipse.viatra.solver.data.model.representation.AuxilaryData; | ||
19 | import org.eclipse.viatra.solver.data.model.representation.DataRepresentation; | ||
20 | import org.eclipse.viatra.solver.data.model.representation.Relation; | ||
21 | |||
22 | public class ModelStoreImpl implements ModelStore { | ||
23 | |||
24 | private final Map<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> stores; | ||
25 | |||
26 | public ModelStoreImpl(Set<DataRepresentation<?, ?>> dataRepresentations) { | ||
27 | stores = initStores(dataRepresentations); | ||
28 | } | ||
29 | |||
30 | private Map<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> initStores( | ||
31 | Set<DataRepresentation<?, ?>> dataRepresentations) { | ||
32 | Map<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> result = new HashMap<>(); | ||
33 | |||
34 | Map<SimilarRelationEquivalenceClass, List<Relation<?>>> symbolRepresentationsPerHashPerArity = new HashMap<>(); | ||
35 | |||
36 | for (DataRepresentation<?, ?> dataRepresentation : dataRepresentations) { | ||
37 | if (dataRepresentation instanceof Relation<?>) { | ||
38 | Relation<?> symbolRepresentation = (Relation<?>) dataRepresentation; | ||
39 | addOrCreate(symbolRepresentationsPerHashPerArity, | ||
40 | new SimilarRelationEquivalenceClass(symbolRepresentation), symbolRepresentation); | ||
41 | } else if (dataRepresentation instanceof AuxilaryData<?, ?>) { | ||
42 | VersionedMapStoreImpl<?, ?> store = new VersionedMapStoreImpl<>(dataRepresentation.getHashProvider(), | ||
43 | dataRepresentation.getDefaultValue()); | ||
44 | result.put(dataRepresentation, store); | ||
45 | } else { | ||
46 | throw new UnsupportedOperationException( | ||
47 | "Model store does not have strategy to use " + dataRepresentation.getClass() + "!"); | ||
48 | } | ||
49 | } | ||
50 | for (List<Relation<?>> symbolGroup : symbolRepresentationsPerHashPerArity.values()) { | ||
51 | initRepresentationGroup(result, symbolGroup); | ||
52 | } | ||
53 | |||
54 | return result; | ||
55 | } | ||
56 | |||
57 | private void initRepresentationGroup(Map<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> result, | ||
58 | List<Relation<?>> symbolGroup) { | ||
59 | final ContinousHashProvider<Tuple> hashProvider = symbolGroup.get(0).getHashProvider(); | ||
60 | final Object defaultValue = symbolGroup.get(0).getDefaultValue(); | ||
61 | |||
62 | List<VersionedMapStore<Tuple, Object>> maps = VersionedMapStoreImpl | ||
63 | .createSharedVersionedMapStores(symbolGroup.size(), hashProvider, defaultValue); | ||
64 | |||
65 | for (int i = 0; i < symbolGroup.size(); i++) { | ||
66 | result.put(symbolGroup.get(i), maps.get(i)); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | private static <K, V> void addOrCreate(Map<K, List<V>> map, K key, V value) { | ||
71 | List<V> list; | ||
72 | if (map.containsKey(key)) { | ||
73 | list = map.get(key); | ||
74 | } else { | ||
75 | list = new LinkedList<>(); | ||
76 | map.put(key, list); | ||
77 | } | ||
78 | list.add(value); | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public Set<DataRepresentation<?, ?>> getDataRepresentations() { | ||
83 | return this.stores.keySet(); | ||
84 | } | ||
85 | |||
86 | @Override | ||
87 | public ModelImpl createModel() { | ||
88 | Map<DataRepresentation<?, ?>, VersionedMap<?, ?>> maps = new HashMap<>(); | ||
89 | for (Entry<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> entry : this.stores.entrySet()) { | ||
90 | maps.put(entry.getKey(), entry.getValue().createMap()); | ||
91 | } | ||
92 | return new ModelImpl(this, maps); | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public synchronized ModelImpl createModel(long state) { | ||
97 | Map<DataRepresentation<?, ?>, VersionedMap<?, ?>> maps = new HashMap<>(); | ||
98 | for (Entry<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> entry : this.stores.entrySet()) { | ||
99 | maps.put(entry.getKey(), entry.getValue().createMap(state)); | ||
100 | } | ||
101 | return new ModelImpl(this, maps); | ||
102 | } | ||
103 | |||
104 | @Override | ||
105 | @SuppressWarnings("squid:S1751") | ||
106 | public synchronized Set<Long> getStates() { | ||
107 | // if not empty, return first | ||
108 | for(VersionedMapStore<?, ?> store : stores.values()) { | ||
109 | return new HashSet<>(store.getStates()); | ||
110 | } | ||
111 | // if empty | ||
112 | Set<Long> result = new HashSet<>(); | ||
113 | result.add(0l); | ||
114 | return result; | ||
115 | } | ||
116 | |||
117 | @Override | ||
118 | public synchronized ModelDiffCursor getDiffCursor(long from, long to) { | ||
119 | Map<DataRepresentation<?, ?>,DiffCursor<?,?>> diffcursors = new HashMap<>(); | ||
120 | for(Entry<DataRepresentation<?, ?>, VersionedMapStore<?, ?>> entry : stores.entrySet()) { | ||
121 | DataRepresentation<?, ?> representation = entry.getKey(); | ||
122 | DiffCursor<?, ?> diffCursor = entry.getValue().getDiffCursor(from, to); | ||
123 | diffcursors.put(representation, diffCursor); | ||
124 | } | ||
125 | return new ModelDiffCursor(diffcursors); | ||
126 | } | ||
127 | } | ||