aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/main/java/org/eclipse/viatra/solver/data/model
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <marussy@mit.bme.hu>2021-09-29 02:45:57 +0200
committerLibravatar Kristóf Marussy <marussy@mit.bme.hu>2021-09-29 03:16:01 +0200
commita155f6ba02e08a75ce6e474a86900b8363f506e8 (patch)
treeb78804c1c0f0968a9625f0656e08f5dadc16924c /store/src/main/java/org/eclipse/viatra/solver/data/model
parentSimplify branding (diff)
downloadrefinery-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')
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/Model.java20
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/ModelCursor.java25
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/ModelDiffCursor.java26
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStore.java16
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java127
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/Tuple.java148
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProvider.java65
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProviderBitMagic.java28
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/internal/ModelImpl.java124
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/internal/SimilarRelationEquivalenceClass.java33
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/representation/AuxilaryData.java22
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/representation/DataRepresentation.java24
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/representation/Relation.java31
-rw-r--r--store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java44
14 files changed, 733 insertions, 0 deletions
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/Model.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/Model.java
new file mode 100644
index 00000000..2b21e3e7
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/Model.java
@@ -0,0 +1,20 @@
1package org.eclipse.viatra.solver.data.model;
2
3import java.util.Set;
4
5import org.eclipse.viatra.solver.data.map.Cursor;
6import org.eclipse.viatra.solver.data.map.Versioned;
7import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;
8
9public interface Model extends Versioned{
10 @SuppressWarnings("squid:S1452")
11 Set<DataRepresentation<?, ?>> getDataRepresentations();
12
13 <K,V> V get(DataRepresentation<K,V> representation, K key);
14 <K,V> Cursor<K,V> getAll(DataRepresentation<K,V> representation);
15 <K,V> V put(DataRepresentation<K,V> representation, K key, V value);
16 <K,V> void putAll(DataRepresentation<K,V> representation, Cursor<K,V> cursor);
17 <K,V> long getSize(DataRepresentation<K,V> representation);
18
19 ModelDiffCursor getDiffCursor(long to);
20}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelCursor.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelCursor.java
new file mode 100644
index 00000000..3157c9f0
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelCursor.java
@@ -0,0 +1,25 @@
1package org.eclipse.viatra.solver.data.model;
2
3import java.util.Map;
4
5import org.eclipse.viatra.solver.data.map.Cursor;
6import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;
7
8public class ModelCursor {
9 final Map<DataRepresentation<?, ?>,Cursor<?,?>> cursors;
10
11 public ModelCursor(Map<DataRepresentation<?, ?>, Cursor<?, ?>> cursors) {
12 super();
13 this.cursors = cursors;
14 }
15
16 @SuppressWarnings("unchecked")
17 public <K,V> Cursor<K,V> getCursor(DataRepresentation<K, V> representation) {
18 Cursor<?, ?> cursor = cursors.get(representation);
19 if(cursor != null) {
20 return (Cursor<K, V>) cursor;
21 } else {
22 throw new IllegalArgumentException("ModelCursor does not contain cursor for representation "+representation);
23 }
24 }
25}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelDiffCursor.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelDiffCursor.java
new file mode 100644
index 00000000..d3551e47
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelDiffCursor.java
@@ -0,0 +1,26 @@
1package org.eclipse.viatra.solver.data.model;
2
3import java.util.Map;
4
5import org.eclipse.viatra.solver.data.map.Cursor;
6import org.eclipse.viatra.solver.data.map.DiffCursor;
7import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;
8
9public class ModelDiffCursor {
10 final Map<DataRepresentation<?, ?>,DiffCursor<?,?>> diffcursors;
11
12 public ModelDiffCursor(Map<DataRepresentation<?, ?>, DiffCursor<?, ?>> diffcursors) {
13 super();
14 this.diffcursors = diffcursors;
15 }
16
17 @SuppressWarnings("unchecked")
18 public <K,V> DiffCursor<K,V> getCursor(DataRepresentation<K, V> representation) {
19 Cursor<?, ?> cursor = diffcursors.get(representation);
20 if(cursor != null) {
21 return (DiffCursor<K, V>) cursor;
22 } else {
23 throw new IllegalArgumentException("ModelCursor does not contain cursor for representation "+representation);
24 }
25 }
26}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStore.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStore.java
new file mode 100644
index 00000000..35ac72b5
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStore.java
@@ -0,0 +1,16 @@
1package org.eclipse.viatra.solver.data.model;
2
3import java.util.Set;
4
5import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;
6
7public interface ModelStore {
8 @SuppressWarnings("squid:S1452")
9 Set<DataRepresentation<?, ?>> getDataRepresentations();
10
11 Model createModel();
12 Model createModel(long state);
13
14 Set<Long> getStates();
15 ModelDiffCursor getDiffCursor(long from, long to);
16} \ No newline at end of file
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 @@
1package org.eclipse.viatra.solver.data.model;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8import java.util.Map.Entry;
9import java.util.Set;
10
11import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
12import org.eclipse.viatra.solver.data.map.DiffCursor;
13import org.eclipse.viatra.solver.data.map.VersionedMap;
14import org.eclipse.viatra.solver.data.map.VersionedMapStore;
15import org.eclipse.viatra.solver.data.map.VersionedMapStoreImpl;
16import org.eclipse.viatra.solver.data.model.internal.ModelImpl;
17import org.eclipse.viatra.solver.data.model.internal.SimilarRelationEquivalenceClass;
18import org.eclipse.viatra.solver.data.model.representation.AuxilaryData;
19import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;
20import org.eclipse.viatra.solver.data.model.representation.Relation;
21
22public 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}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/Tuple.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/Tuple.java
new file mode 100644
index 00000000..ca6548a4
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/Tuple.java
@@ -0,0 +1,148 @@
1package org.eclipse.viatra.solver.data.model;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.List;
6
7public abstract class Tuple {
8 private static final int CUSTOMTUPLESIZE = 2;
9 protected static final List<Tuple1> tuple1Cash = new ArrayList<>(1024);
10
11 public abstract int getSize();
12 public abstract int get(int element);
13 public abstract int[] toArray();
14
15 @Override
16 public String toString() {
17 StringBuilder b = new StringBuilder();
18 b.append("[");
19 for(int i = 0; i<getSize(); i++) {
20 if(i!=0) {
21 b.append(",");
22 }
23 b.append(get(i));
24 }
25 b.append("]");
26 return b.toString();
27 }
28
29 public static Tuple1 of1(int value) {
30 if(value < tuple1Cash.size()) {
31 return tuple1Cash.get(value);
32 } else {
33 Tuple1 newlyCreated = null;
34 while(value >= tuple1Cash.size()) {
35 newlyCreated = new Tuple1(tuple1Cash.size());
36 tuple1Cash.add(newlyCreated);
37 }
38 return newlyCreated;
39 }
40 }
41
42 public static Tuple of(int... values) {
43 if(values.length == 0) {
44 return new Tuple0();
45 } else if(values.length == 1) {
46 return of1(values[0]);
47 } else if(values.length == 2) {
48 return new Tuple2(values[0],values[1]);
49 } else return new TupleN(values);
50 }
51
52 protected IllegalArgumentException doesNotContain(int element) {
53 return new IllegalArgumentException("Tuple does not contain element "+element);
54 }
55
56 public static class Tuple0 extends Tuple{
57 protected Tuple0() { }
58 @Override public int getSize() { return 0; }
59 @Override public int get(int element) {
60 throw doesNotContain(element);
61 }
62 @Override public int[] toArray() {return new int[]{};}
63 @Override public int hashCode() { return TupleHashProvider.singleton().getHash(this, 0); }
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj)
67 return true;
68 if (obj == null)
69 return false;
70 if (getClass() != obj.getClass())
71 return false;
72 return true;
73 }
74 }
75 public static class Tuple1 extends Tuple{
76 final int value0;
77 protected Tuple1(int value0) { this.value0 = value0; }
78 @Override public int getSize() { return 1; }
79 @Override public int get(int element) {
80 if(element == 0) return value0;
81 throw doesNotContain(element);
82 }
83 @Override public int[] toArray() {return new int[]{ value0 };}
84 @Override public int hashCode() { return TupleHashProvider.singleton().getHash(this, 0); }
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj)
88 return true;
89 if (obj == null)
90 return false;
91 if (getClass() != obj.getClass())
92 return false;
93 Tuple1 other = (Tuple1) obj;
94 return value0 == other.value0;
95 }
96 }
97 public static class Tuple2 extends Tuple{
98 final int value0;
99 final int value1;
100 protected Tuple2(int value0, int value1) { this.value0 = value0; this.value1 = value1; }
101 @Override public int getSize() { return 2; }
102 @Override public int get(int element) {
103 if(element == 0) return value0;
104 else if(element == 1) return value1;
105 throw doesNotContain(element);
106 }
107 @Override public int[] toArray() {return new int[]{ value0,value1 };}
108 @Override public int hashCode() { return TupleHashProvider.singleton().getHash(this, 0); }
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj)
112 return true;
113 if (obj == null)
114 return false;
115 if (getClass() != obj.getClass())
116 return false;
117 Tuple2 other = (Tuple2) obj;
118 return value0 == other.value0 && value1 == other.value1;
119 }
120 }
121 public static class TupleN extends Tuple{
122 final int[] values;
123 protected TupleN(int[] values) {
124 if(values.length<CUSTOMTUPLESIZE)
125 throw new IllegalArgumentException();
126 this.values = Arrays.copyOf(values, values.length);
127 }
128 @Override public int getSize() { return values.length; }
129 @Override public int get(int element) {
130 if(0<=element && element < values.length) {
131 return values[element];
132 } else throw doesNotContain(element);
133 }
134 @Override public int[] toArray() { return values; }
135 @Override public int hashCode() { return TupleHashProvider.singleton().getHash(this, 0); }
136 @Override
137 public boolean equals(Object obj) {
138 if (this == obj)
139 return true;
140 if (obj == null)
141 return false;
142 if (getClass() != obj.getClass())
143 return false;
144 TupleN other = (TupleN) obj;
145 return Arrays.equals(values, other.values);
146 }
147 }
148}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProvider.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProvider.java
new file mode 100644
index 00000000..6c37aa37
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProvider.java
@@ -0,0 +1,65 @@
1package org.eclipse.viatra.solver.data.model;
2
3import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
4
5public class TupleHashProvider implements ContinousHashProvider<Tuple> {
6 protected static TupleHashProvider instance;
7
8 public static TupleHashProvider singleton() {
9 if (instance == null) {
10 instance = new TupleHashProvider();
11 }
12 return instance;
13 }
14
15 protected static final int[] primes = new int[] { 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
16 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211,
17 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337,
18 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461,
19 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
20 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739,
21 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881,
22 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021,
23 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
24 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283,
25 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429,
26 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549,
27 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667,
28 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811,
29 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973,
30 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089,
31 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
32 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377,
33 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531,
34 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677,
35 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
36 2797, 2801, 2803, 2819, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217,
37 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359,
38 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517,
39 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637,
40 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793,
41 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911 };
42
43 protected static final long LARGESTPRIME30BITS = 1073741789;
44
45 public TupleHashProvider() {
46 if (primes.length < MAX_PRACTICAL_DEPTH) {
47 throw new UnsupportedOperationException(
48 "Not enough prime numbers to support the practical depth of continuous hash!");
49 }
50 }
51
52 @Override
53 public int getHash(Tuple key, int index) {
54 if (index >= primes.length) {
55 throw new IllegalArgumentException("Not enough prime numbers to support index");
56 }
57 long accumulator = 0;
58 final int prime = primes[index];
59 for (int i = 0; i < key.getSize(); i++) {
60 accumulator = (prime * accumulator + key.get(i)) % LARGESTPRIME30BITS;
61 }
62
63 return (int) accumulator;
64 }
65}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProviderBitMagic.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProviderBitMagic.java
new file mode 100644
index 00000000..2a514d66
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/TupleHashProviderBitMagic.java
@@ -0,0 +1,28 @@
1package org.eclipse.viatra.solver.data.model;
2
3import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
4
5public class TupleHashProviderBitMagic implements ContinousHashProvider<Tuple> {
6
7 @Override
8 public int getHash(Tuple key, int index) {
9 if(key.getSize() == 1) {
10 return key.get(0);
11 }
12
13 int result = 0;
14 final int startBitIndex = index*30;
15 final int finalBitIndex = startBitIndex+30;
16 final int arity = key.getSize();
17
18 for(int i = startBitIndex; i<=finalBitIndex; i++) {
19 final int selectedKey = key.get(i%arity);
20 final int selectedPosition = 1<<(i/arity);
21 if((selectedKey&selectedPosition) != 0) {
22 result |= 1<<(i%30);
23 }
24 }
25
26 return result;
27 }
28}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/ModelImpl.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/ModelImpl.java
new file mode 100644
index 00000000..6d7f4e97
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/ModelImpl.java
@@ -0,0 +1,124 @@
1package org.eclipse.viatra.solver.data.model.internal;
2
3import java.util.HashMap;
4import java.util.Map;
5import java.util.Set;
6
7import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
8import org.eclipse.viatra.solver.data.map.Cursor;
9import org.eclipse.viatra.solver.data.map.DiffCursor;
10import org.eclipse.viatra.solver.data.map.VersionedMap;
11import org.eclipse.viatra.solver.data.map.internal.MapDiffCursor;
12import org.eclipse.viatra.solver.data.model.Model;
13import org.eclipse.viatra.solver.data.model.ModelDiffCursor;
14import org.eclipse.viatra.solver.data.model.ModelStore;
15import org.eclipse.viatra.solver.data.model.representation.DataRepresentation;
16
17public class ModelImpl implements Model {
18 private final ModelStore store;
19 private final Map<DataRepresentation<?, ?>, VersionedMap<?, ?>> maps;
20
21 public ModelImpl(ModelStore store, Map<DataRepresentation<?, ?>, VersionedMap<?, ?>> maps) {
22 this.store = store;
23 this.maps = maps;
24 }
25
26 @Override
27 public Set<DataRepresentation<?, ?>> getDataRepresentations() {
28 return maps.keySet();
29 }
30
31 @SuppressWarnings("unchecked")
32 private <K, V> VersionedMap<K, V> getMap(DataRepresentation<K, V> representation) {
33 if (maps.containsKey(representation)) {
34 return (VersionedMap<K, V>) maps.get(representation);
35 } else {
36 throw new IllegalArgumentException("Model does have representation " + representation);
37 }
38 }
39
40 private <K, V> VersionedMap<K, V> getMapValidateKey(DataRepresentation<K, V> representation, K key) {
41 if (representation.isValidKey(key)) {
42 return getMap(representation);
43 } else {
44 throw new IllegalArgumentException(
45 "Key is not valid for representation! (representation=" + representation + ", key=" + key + ");");
46 }
47 }
48
49 @Override
50 public <K, V> V get(DataRepresentation<K, V> representation, K key) {
51 return getMapValidateKey(representation, key).get(key);
52 }
53
54 @Override
55 public <K, V> Cursor<K, V> getAll(DataRepresentation<K, V> representation) {
56 return getMap(representation).getAll();
57 }
58
59 @Override
60 public <K, V> V put(DataRepresentation<K, V> representation, K key, V value) {
61 return getMapValidateKey(representation, key).put(key, value);
62 }
63
64 @Override
65 public <K, V> void putAll(DataRepresentation<K, V> representation, Cursor<K, V> cursor) {
66 getMap(representation).putAll(cursor);
67 }
68
69 @Override
70 public <K, V> long getSize(DataRepresentation<K, V> representation) {
71 return getMap(representation).getSize();
72 }
73
74 @Override
75 public ModelDiffCursor getDiffCursor(long to) {
76 Model toModel = store.createModel(to);
77 Map<DataRepresentation<?, ?>, DiffCursor<?, ?>> diffCursors = new HashMap<>();
78 for (DataRepresentation<?, ?> representation : this.maps.keySet()) {
79 MapDiffCursor<?, ?> diffCursor = constructDiffCursor(toModel, representation);
80 diffCursors.put(representation, diffCursor);
81 }
82 return new ModelDiffCursor(diffCursors);
83 }
84
85 private <K, V> MapDiffCursor<K, V> constructDiffCursor(Model toModel, DataRepresentation<K, V> representation) {
86 @SuppressWarnings("unchecked")
87 Cursor<K, V> fromCursor = (Cursor<K, V>) this.maps.get(representation).getAll();
88 Cursor<K, V> toCursor = toModel.getAll(representation);
89
90 ContinousHashProvider<K> hashProvider = representation.getHashProvider();
91 V defaultValue = representation.getDefaultValue();
92 return new MapDiffCursor<>(hashProvider, defaultValue, fromCursor, toCursor);
93 }
94
95 @Override
96 public long commit() {
97 long version = 0;
98 boolean versionSet = false;
99 for (VersionedMap<?, ?> map : maps.values()) {
100 long newVersion = map.commit();
101 if (versionSet) {
102 if (version != newVersion) {
103 throw new IllegalStateException(
104 "Maps in model have different versions! (" + version + " and" + newVersion + ")");
105 }
106 } else {
107 version = newVersion;
108 versionSet = true;
109 }
110 }
111 return version;
112 }
113
114 @Override
115 public void restore(long state) {
116 if(store.getStates().contains(state)) {
117 for (VersionedMap<?, ?> map : maps.values()) {
118 map.restore(state);
119 }
120 } else {
121 throw new IllegalArgumentException("Map does not contain state "+state+"!");
122 }
123 }
124}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/SimilarRelationEquivalenceClass.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/SimilarRelationEquivalenceClass.java
new file mode 100644
index 00000000..7054e4db
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/internal/SimilarRelationEquivalenceClass.java
@@ -0,0 +1,33 @@
1package org.eclipse.viatra.solver.data.model.internal;
2
3import java.util.Objects;
4
5import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
6import org.eclipse.viatra.solver.data.model.Tuple;
7import org.eclipse.viatra.solver.data.model.representation.Relation;
8
9public class SimilarRelationEquivalenceClass {
10 final ContinousHashProvider<Tuple> hashProvider;
11 final Object defaultValue;
12 final int arity;
13 public SimilarRelationEquivalenceClass(Relation<?> representation) {
14 this.hashProvider = representation.getHashProvider();
15 this.defaultValue = representation.getDefaultValue();
16 this.arity = representation.getArity();
17 }
18 @Override
19 public int hashCode() {
20 return Objects.hash(arity, defaultValue, hashProvider);
21 }
22 @Override
23 public boolean equals(Object obj) {
24 if (this == obj)
25 return true;
26 if (!(obj instanceof SimilarRelationEquivalenceClass))
27 return false;
28 SimilarRelationEquivalenceClass other = (SimilarRelationEquivalenceClass) obj;
29 return arity == other.arity && Objects.equals(defaultValue, other.defaultValue)
30 && Objects.equals(hashProvider, other.hashProvider);
31 }
32
33}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/AuxilaryData.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/AuxilaryData.java
new file mode 100644
index 00000000..7fc79348
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/AuxilaryData.java
@@ -0,0 +1,22 @@
1package org.eclipse.viatra.solver.data.model.representation;
2
3import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
4
5public class AuxilaryData<K,V> extends DataRepresentation<K, V> {
6 private final String name;
7
8 public AuxilaryData(String name, ContinousHashProvider<K> hashProvider, V defaultValue) {
9 super(hashProvider, defaultValue);
10 this.name = name;
11 }
12
13 @Override
14 public String getName() {
15 return name;
16 }
17
18 @Override
19 public boolean isValidKey(K key) {
20 return true;
21 }
22}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/DataRepresentation.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/DataRepresentation.java
new file mode 100644
index 00000000..fd48eb94
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/DataRepresentation.java
@@ -0,0 +1,24 @@
1package org.eclipse.viatra.solver.data.model.representation;
2
3import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
4
5public abstract class DataRepresentation<K, V> {
6 protected final ContinousHashProvider<K> hashProvider;
7 protected final V defaultValue;
8
9 protected DataRepresentation(ContinousHashProvider<K> hashProvider, V defaultValue) {
10 this.hashProvider = hashProvider;
11 this.defaultValue = defaultValue;
12 }
13
14 public abstract String getName();
15
16 public ContinousHashProvider<K> getHashProvider() {
17 return hashProvider;
18 }
19 public abstract boolean isValidKey(K key);
20
21 public V getDefaultValue() {
22 return defaultValue;
23 }
24}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/Relation.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/Relation.java
new file mode 100644
index 00000000..eafb5c56
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/Relation.java
@@ -0,0 +1,31 @@
1package org.eclipse.viatra.solver.data.model.representation;
2
3import org.eclipse.viatra.solver.data.model.Tuple;
4import org.eclipse.viatra.solver.data.model.TupleHashProvider;
5
6public class Relation<D> extends DataRepresentation<Tuple, D> {
7 private final String name;
8 private final int arity;
9
10 public Relation(String name, int arity, D defaultValue) {
11 super(TupleHashProvider.singleton(), defaultValue);
12 this.name = name;
13 this.arity = arity;
14 }
15
16 @Override
17 public String getName() {
18 return name;
19 }
20
21 public int getArity() {
22 return arity;
23 }
24
25 @Override
26 public boolean isValidKey(Tuple key) {
27 if(key == null) {
28 return false;
29 } else return key.getSize() == getArity();
30 }
31}
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java
new file mode 100644
index 00000000..aeccde9e
--- /dev/null
+++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java
@@ -0,0 +1,44 @@
1package org.eclipse.viatra.solver.data.model.representation;
2
3public class TruthValue {
4 public static final TruthValue True = new TruthValue("true");
5 public static final TruthValue False = new TruthValue("false");
6 public static final TruthValue Unknown = new TruthValue("unknown");
7 public static final TruthValue Error = new TruthValue("error");
8
9 private final String name;
10 protected TruthValue(String name) {
11 this.name = name;
12 }
13
14 public String getName() {
15 return name;
16 }
17
18 public static TruthValue toTruthValue(boolean value) {
19 if(value) return True;
20 else return False;
21 }
22 public boolean isConsistent() {
23 return this != Error;
24 }
25 public boolean isComplete() {
26 return this != Unknown;
27 }
28 public boolean must() {
29 return this == True || this == Error;
30 }
31 public boolean may() {
32 return this == True || this == Unknown;
33 }
34
35 public TruthValue not() {
36 if(this == True) {
37 return False;
38 } else if(this == False) {
39 return True;
40 } else {
41 return this;
42 }
43 }
44}