diff options
Diffstat (limited to 'subprojects/store/src/main/java/tools')
44 files changed, 787 insertions, 165 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java b/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java index 0a94d449..5e69e7af 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java +++ b/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java | |||
@@ -5,6 +5,9 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.map; | 6 | package tools.refinery.store.map; |
7 | 7 | ||
8 | import java.util.Iterator; | ||
9 | import java.util.Map; | ||
10 | |||
8 | public final class Cursors { | 11 | public final class Cursors { |
9 | private Cursors() { | 12 | private Cursors() { |
10 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | 13 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); |
@@ -14,6 +17,18 @@ public final class Cursors { | |||
14 | return new Empty<>(); | 17 | return new Empty<>(); |
15 | } | 18 | } |
16 | 19 | ||
20 | public static <K, V> Cursor<K, V> singleton(K key, V value) { | ||
21 | return new Singleton<>(key, value); | ||
22 | } | ||
23 | |||
24 | public static <K, V> Cursor<K, V> of(Iterator<Map.Entry<K, V>> iterator) { | ||
25 | return new IteratorBasedCursor<>(iterator); | ||
26 | } | ||
27 | |||
28 | public static <K, V> Cursor<K, V> of(Map<K, V> map) { | ||
29 | return of(map.entrySet().iterator()); | ||
30 | } | ||
31 | |||
17 | private static class Empty<K, V> implements Cursor<K, V> { | 32 | private static class Empty<K, V> implements Cursor<K, V> { |
18 | private boolean terminated = false; | 33 | private boolean terminated = false; |
19 | 34 | ||
@@ -38,4 +53,53 @@ public final class Cursors { | |||
38 | return false; | 53 | return false; |
39 | } | 54 | } |
40 | } | 55 | } |
56 | |||
57 | private static class Singleton<K, V> implements Cursor<K, V> { | ||
58 | private State state = State.INITIAL; | ||
59 | private final K key; | ||
60 | private final V value; | ||
61 | |||
62 | public Singleton(K key, V value) { | ||
63 | this.key = key; | ||
64 | this.value = value; | ||
65 | } | ||
66 | |||
67 | @Override | ||
68 | public K getKey() { | ||
69 | if (state == State.STARTED) { | ||
70 | return key; | ||
71 | } | ||
72 | return null; | ||
73 | } | ||
74 | |||
75 | @Override | ||
76 | public V getValue() { | ||
77 | if (state == State.STARTED) { | ||
78 | return value; | ||
79 | } | ||
80 | return null; | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public boolean isTerminated() { | ||
85 | return state == State.TERMINATED; | ||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public boolean move() { | ||
90 | if (state == State.INITIAL) { | ||
91 | state = State.STARTED; | ||
92 | return true; | ||
93 | } | ||
94 | state = State.TERMINATED; | ||
95 | return false; | ||
96 | } | ||
97 | |||
98 | |||
99 | private enum State { | ||
100 | INITIAL, | ||
101 | STARTED, | ||
102 | TERMINATED | ||
103 | } | ||
104 | } | ||
41 | } | 105 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/IteratorBasedCursor.java b/subprojects/store/src/main/java/tools/refinery/store/map/IteratorBasedCursor.java new file mode 100644 index 00000000..0ed9b730 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/map/IteratorBasedCursor.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.map; | ||
7 | |||
8 | import java.util.Iterator; | ||
9 | import java.util.Map; | ||
10 | |||
11 | public class IteratorBasedCursor<K, V> implements Cursor<K, V> { | ||
12 | private final Iterator<Map.Entry<K, V>> iterator; | ||
13 | private Map.Entry<K, V> entry; | ||
14 | private boolean terminated; | ||
15 | |||
16 | public IteratorBasedCursor(Iterator<Map.Entry<K, V>> iterator) { | ||
17 | this.iterator = iterator; | ||
18 | } | ||
19 | |||
20 | @Override | ||
21 | public K getKey() { | ||
22 | return entry.getKey(); | ||
23 | } | ||
24 | |||
25 | @Override | ||
26 | public V getValue() { | ||
27 | return entry.getValue(); | ||
28 | } | ||
29 | |||
30 | @Override | ||
31 | public boolean isTerminated() { | ||
32 | return terminated; | ||
33 | } | ||
34 | |||
35 | @Override | ||
36 | public boolean move() { | ||
37 | if (!terminated && iterator.hasNext()) { | ||
38 | entry = iterator.next(); | ||
39 | return true; | ||
40 | } | ||
41 | terminated = true; | ||
42 | return false; | ||
43 | } | ||
44 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/internal/delta/VersionedMapDeltaImpl.java b/subprojects/store/src/main/java/tools/refinery/store/map/internal/delta/VersionedMapDeltaImpl.java index c19cc817..d530ae87 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/map/internal/delta/VersionedMapDeltaImpl.java +++ b/subprojects/store/src/main/java/tools/refinery/store/map/internal/delta/VersionedMapDeltaImpl.java | |||
@@ -5,10 +5,9 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.map.internal.delta; | 6 | package tools.refinery.store.map.internal.delta; |
7 | 7 | ||
8 | import java.util.*; | ||
9 | |||
10 | import tools.refinery.store.map.*; | 8 | import tools.refinery.store.map.*; |
11 | import tools.refinery.store.map.IteratorAsCursor; | 9 | |
10 | import java.util.*; | ||
12 | 11 | ||
13 | public class VersionedMapDeltaImpl<K, V> implements VersionedMap<K, V> { | 12 | public class VersionedMapDeltaImpl<K, V> implements VersionedMap<K, V> { |
14 | protected final VersionedMapStoreDeltaImpl<K, V> store; | 13 | protected final VersionedMapStoreDeltaImpl<K, V> store; |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/AnyInterpretation.java b/subprojects/store/src/main/java/tools/refinery/store/model/AnyInterpretation.java index f906b48a..d650bd06 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/AnyInterpretation.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/AnyInterpretation.java | |||
@@ -13,4 +13,6 @@ public sealed interface AnyInterpretation permits Interpretation { | |||
13 | AnySymbol getSymbol(); | 13 | AnySymbol getSymbol(); |
14 | 14 | ||
15 | long getSize(); | 15 | long getSize(); |
16 | |||
17 | int getAdjacentSize(int slot, int node); | ||
16 | } | 18 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/Interpretation.java b/subprojects/store/src/main/java/tools/refinery/store/model/Interpretation.java index 72f188d3..1b15e4cf 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/Interpretation.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/Interpretation.java | |||
@@ -19,6 +19,8 @@ public non-sealed interface Interpretation<T> extends AnyInterpretation { | |||
19 | 19 | ||
20 | Cursor<Tuple, T> getAll(); | 20 | Cursor<Tuple, T> getAll(); |
21 | 21 | ||
22 | Cursor<Tuple, T> getAdjacent(int slot, int node); | ||
23 | |||
22 | T put(Tuple key, T value); | 24 | T put(Tuple key, T value); |
23 | 25 | ||
24 | void putAll(Cursor<Tuple, T> cursor); | 26 | void putAll(Cursor<Tuple, T> cursor); |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreBuilder.java b/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreBuilder.java index 3a4024b5..8f652762 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreBuilder.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreBuilder.java | |||
@@ -29,7 +29,9 @@ public interface ModelStoreBuilder { | |||
29 | 29 | ||
30 | <T> ModelStoreBuilder symbol(Symbol<T> symbol); | 30 | <T> ModelStoreBuilder symbol(Symbol<T> symbol); |
31 | 31 | ||
32 | <T extends ModelAdapterBuilder> ModelStoreBuilder with(T adapterBuilder); | 32 | ModelStoreBuilder with(ModelAdapterBuilder adapterBuilder); |
33 | |||
34 | ModelStoreBuilder with(ModelStoreConfiguration configuration); | ||
33 | 35 | ||
34 | <T extends ModelAdapterBuilder> Optional<T> tryGetAdapter(Class<? extends T> adapterType); | 36 | <T extends ModelAdapterBuilder> Optional<T> tryGetAdapter(Class<? extends T> adapterType); |
35 | 37 | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreConfiguration.java b/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreConfiguration.java new file mode 100644 index 00000000..e94af5f8 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreConfiguration.java | |||
@@ -0,0 +1,11 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.model; | ||
7 | |||
8 | @FunctionalInterface | ||
9 | public interface ModelStoreConfiguration { | ||
10 | void apply(ModelStoreBuilder storeBuilder); | ||
11 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/BaseIndexer.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/BaseIndexer.java new file mode 100644 index 00000000..3d7f59d7 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/BaseIndexer.java | |||
@@ -0,0 +1,102 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.model.internal; | ||
7 | |||
8 | import org.eclipse.collections.api.factory.Maps; | ||
9 | import org.eclipse.collections.api.factory.primitive.IntObjectMaps; | ||
10 | import org.eclipse.collections.api.map.MutableMap; | ||
11 | import org.eclipse.collections.api.map.primitive.MutableIntObjectMap; | ||
12 | import tools.refinery.store.map.*; | ||
13 | import tools.refinery.store.tuple.Tuple; | ||
14 | |||
15 | import java.util.Set; | ||
16 | |||
17 | class BaseIndexer<T> { | ||
18 | private final MutableIntObjectMap<MutableMap<Tuple, T>>[] maps; | ||
19 | private final VersionedMap<Tuple, T> versionedMap; | ||
20 | |||
21 | public BaseIndexer(int arity, VersionedMap<Tuple, T> map) { | ||
22 | if (arity < 2) { | ||
23 | throw new IllegalArgumentException("Only arity >= 2 symbols need to be indexed"); | ||
24 | } | ||
25 | // There is no way in Java to create a generic array in a checked way. | ||
26 | @SuppressWarnings({"unchecked", "squid:S1905"}) | ||
27 | var uncheckedMaps = (MutableIntObjectMap<MutableMap<Tuple, T>>[]) new MutableIntObjectMap[arity]; | ||
28 | maps = uncheckedMaps; | ||
29 | for (int i = 0; i < arity; i++) { | ||
30 | maps[i] = IntObjectMaps.mutable.empty(); | ||
31 | } | ||
32 | this.versionedMap = map; | ||
33 | if (map != null) { | ||
34 | var cursor = map.getAll(); | ||
35 | while (cursor.move()) { | ||
36 | put(cursor.getKey(), cursor.getValue()); | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
41 | public void put(Tuple key, T value) { | ||
42 | for (int i = 0; i < maps.length; i++) { | ||
43 | var map = maps[i]; | ||
44 | int element = key.get(i); | ||
45 | var adjacentTuples = map.getIfAbsentPut(element, Maps.mutable::empty); | ||
46 | adjacentTuples.put(key, value); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | public void remove(Tuple key) { | ||
51 | for (int i = 0; i < maps.length; i++) { | ||
52 | var map = maps[i]; | ||
53 | int element = key.get(i); | ||
54 | var adjacentTuples = map.get(element); | ||
55 | if (adjacentTuples == null) { | ||
56 | continue; | ||
57 | } | ||
58 | adjacentTuples.remove(key); | ||
59 | if (adjacentTuples.isEmpty()) { | ||
60 | map.remove(element); | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | private MutableMap<Tuple, T> getAdjacentMap(int slot, int node) { | ||
66 | if (slot < 0 || slot >= maps.length) { | ||
67 | throw new IllegalArgumentException("Invalid index: " + slot); | ||
68 | } | ||
69 | var map = maps[slot]; | ||
70 | return map.get(node); | ||
71 | } | ||
72 | |||
73 | public int getAdjacentSize(int slot, int node) { | ||
74 | var adjacentTuples = getAdjacentMap(slot, node); | ||
75 | if (adjacentTuples == null) { | ||
76 | return 0; | ||
77 | } | ||
78 | return adjacentTuples.size(); | ||
79 | } | ||
80 | |||
81 | public Cursor<Tuple, T> getAdjacent(int slot, int node) { | ||
82 | var adjacentTuples = getAdjacentMap(slot, node); | ||
83 | if (adjacentTuples == null) { | ||
84 | return Cursors.empty(); | ||
85 | } | ||
86 | return new IndexCursor<>(adjacentTuples, versionedMap); | ||
87 | } | ||
88 | |||
89 | private static class IndexCursor<T> extends IteratorBasedCursor<Tuple, T> { | ||
90 | private final Set<AnyVersionedMap> dependingMaps; | ||
91 | |||
92 | public IndexCursor(MutableMap<Tuple, T> map, VersionedMap<Tuple, T> versionedMap) { | ||
93 | super(map.entrySet().iterator()); | ||
94 | dependingMaps = versionedMap == null ? Set.of() : Set.of(versionedMap); | ||
95 | } | ||
96 | |||
97 | @Override | ||
98 | public Set<AnyVersionedMap> getDependingMaps() { | ||
99 | return dependingMaps; | ||
100 | } | ||
101 | } | ||
102 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/IndexedVersionedInterpretation.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/IndexedVersionedInterpretation.java new file mode 100644 index 00000000..0be16f77 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/IndexedVersionedInterpretation.java | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.model.internal; | ||
7 | |||
8 | import tools.refinery.store.map.Cursor; | ||
9 | import tools.refinery.store.map.VersionedMap; | ||
10 | import tools.refinery.store.representation.Symbol; | ||
11 | import tools.refinery.store.tuple.Tuple; | ||
12 | |||
13 | import java.util.Objects; | ||
14 | |||
15 | class IndexedVersionedInterpretation<T> extends VersionedInterpretation<T> { | ||
16 | private final BaseIndexer<T> indexer; | ||
17 | |||
18 | public IndexedVersionedInterpretation(ModelImpl model, Symbol<T> symbol, VersionedMap<Tuple, T> map) { | ||
19 | super(model, symbol, map); | ||
20 | indexer = new BaseIndexer<>(symbol.arity(), map); | ||
21 | } | ||
22 | |||
23 | @Override | ||
24 | public Cursor<Tuple, T> getAdjacent(int slot, int node) { | ||
25 | return indexer.getAdjacent(slot, node); | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public int getAdjacentSize(int slot, int node) { | ||
30 | return indexer.getAdjacentSize(slot, node); | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | protected boolean shouldNotifyRestoreListeners() { | ||
35 | // Always call the {@code valueChanged} method to update the index. | ||
36 | return true; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | protected void valueChanged(Tuple key, T fromValue, T toValue, boolean restoring) { | ||
41 | if (Objects.equals(toValue, getSymbol().defaultValue())) { | ||
42 | indexer.remove(key); | ||
43 | } else { | ||
44 | indexer.put(key, toValue); | ||
45 | } | ||
46 | super.valueChanged(key, fromValue, toValue, restoring); | ||
47 | } | ||
48 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreBuilderImpl.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreBuilderImpl.java index 2bd187a8..2dde7a4c 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreBuilderImpl.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreBuilderImpl.java | |||
@@ -12,6 +12,7 @@ import tools.refinery.store.map.VersionedMapStoreFactory; | |||
12 | import tools.refinery.store.map.VersionedMapStoreFactoryBuilder; | 12 | import tools.refinery.store.map.VersionedMapStoreFactoryBuilder; |
13 | import tools.refinery.store.model.ModelStore; | 13 | import tools.refinery.store.model.ModelStore; |
14 | import tools.refinery.store.model.ModelStoreBuilder; | 14 | import tools.refinery.store.model.ModelStoreBuilder; |
15 | import tools.refinery.store.model.ModelStoreConfiguration; | ||
15 | import tools.refinery.store.representation.AnySymbol; | 16 | import tools.refinery.store.representation.AnySymbol; |
16 | import tools.refinery.store.representation.Symbol; | 17 | import tools.refinery.store.representation.Symbol; |
17 | import tools.refinery.store.tuple.Tuple; | 18 | import tools.refinery.store.tuple.Tuple; |
@@ -26,7 +27,8 @@ public class ModelStoreBuilderImpl implements ModelStoreBuilder { | |||
26 | @Override | 27 | @Override |
27 | public <T> ModelStoreBuilder symbol(Symbol<T> symbol) { | 28 | public <T> ModelStoreBuilder symbol(Symbol<T> symbol) { |
28 | if (!allSymbols.add(symbol)) { | 29 | if (!allSymbols.add(symbol)) { |
29 | throw new IllegalArgumentException("Symbol %s already added".formatted(symbol)); | 30 | // No need to add symbol twice. |
31 | return this; | ||
30 | } | 32 | } |
31 | var equivalenceClass = new SymbolEquivalenceClass<>(symbol); | 33 | var equivalenceClass = new SymbolEquivalenceClass<>(symbol); |
32 | var symbolsInEquivalenceClass = equivalenceClasses.computeIfAbsent(equivalenceClass, | 34 | var symbolsInEquivalenceClass = equivalenceClasses.computeIfAbsent(equivalenceClass, |
@@ -36,7 +38,7 @@ public class ModelStoreBuilderImpl implements ModelStoreBuilder { | |||
36 | } | 38 | } |
37 | 39 | ||
38 | @Override | 40 | @Override |
39 | public <T extends ModelAdapterBuilder> ModelStoreBuilder with(T adapterBuilder) { | 41 | public ModelStoreBuilder with(ModelAdapterBuilder adapterBuilder) { |
40 | for (var existingAdapter : adapters) { | 42 | for (var existingAdapter : adapters) { |
41 | if (existingAdapter.getClass().equals(adapterBuilder.getClass())) { | 43 | if (existingAdapter.getClass().equals(adapterBuilder.getClass())) { |
42 | throw new IllegalArgumentException("%s adapter was already configured for store builder" | 44 | throw new IllegalArgumentException("%s adapter was already configured for store builder" |
@@ -48,6 +50,12 @@ public class ModelStoreBuilderImpl implements ModelStoreBuilder { | |||
48 | } | 50 | } |
49 | 51 | ||
50 | @Override | 52 | @Override |
53 | public ModelStoreBuilder with(ModelStoreConfiguration configuration) { | ||
54 | configuration.apply(this); | ||
55 | return this; | ||
56 | } | ||
57 | |||
58 | @Override | ||
51 | public <T extends ModelAdapterBuilder> Optional<T> tryGetAdapter(Class<? extends T> adapterType) { | 59 | public <T extends ModelAdapterBuilder> Optional<T> tryGetAdapter(Class<? extends T> adapterType) { |
52 | return AdapterUtils.tryGetAdapter(adapters, adapterType); | 60 | return AdapterUtils.tryGetAdapter(adapters, adapterType); |
53 | } | 61 | } |
@@ -59,6 +67,7 @@ public class ModelStoreBuilderImpl implements ModelStoreBuilder { | |||
59 | 67 | ||
60 | @Override | 68 | @Override |
61 | public ModelStore build() { | 69 | public ModelStore build() { |
70 | // First configure adapters and let them register any symbols we don't know about yet. | ||
62 | for (int i = adapters.size() - 1; i >= 0; i--) { | 71 | for (int i = adapters.size() - 1; i >= 0; i--) { |
63 | adapters.get(i).configure(this); | 72 | adapters.get(i).configure(this); |
64 | } | 73 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/NullaryVersionedInterpretation.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/NullaryVersionedInterpretation.java new file mode 100644 index 00000000..4a8e6752 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/NullaryVersionedInterpretation.java | |||
@@ -0,0 +1,27 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.model.internal; | ||
7 | |||
8 | import tools.refinery.store.map.Cursor; | ||
9 | import tools.refinery.store.map.VersionedMap; | ||
10 | import tools.refinery.store.representation.Symbol; | ||
11 | import tools.refinery.store.tuple.Tuple; | ||
12 | |||
13 | class NullaryVersionedInterpretation<T> extends VersionedInterpretation<T> { | ||
14 | public NullaryVersionedInterpretation(ModelImpl model, Symbol<T> symbol, VersionedMap<Tuple, T> map) { | ||
15 | super(model, symbol, map); | ||
16 | } | ||
17 | |||
18 | @Override | ||
19 | public Cursor<Tuple, T> getAdjacent(int slot, int node) { | ||
20 | throw new IllegalArgumentException("Invalid index: " + slot); | ||
21 | } | ||
22 | |||
23 | @Override | ||
24 | public int getAdjacentSize(int slot, int node) { | ||
25 | throw new IllegalArgumentException("Invalid index: " + slot); | ||
26 | } | ||
27 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/UnaryVersionedInterpretation.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/UnaryVersionedInterpretation.java new file mode 100644 index 00000000..75946680 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/UnaryVersionedInterpretation.java | |||
@@ -0,0 +1,48 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.model.internal; | ||
7 | |||
8 | import tools.refinery.store.map.Cursor; | ||
9 | import tools.refinery.store.map.Cursors; | ||
10 | import tools.refinery.store.map.VersionedMap; | ||
11 | import tools.refinery.store.representation.Symbol; | ||
12 | import tools.refinery.store.tuple.Tuple; | ||
13 | |||
14 | import java.util.Objects; | ||
15 | |||
16 | class UnaryVersionedInterpretation<T> extends VersionedInterpretation<T> { | ||
17 | public UnaryVersionedInterpretation(ModelImpl model, Symbol<T> symbol, VersionedMap<Tuple, T> map) { | ||
18 | super(model, symbol, map); | ||
19 | } | ||
20 | |||
21 | private void validateSlot(int slot) { | ||
22 | if (slot != 0) { | ||
23 | throw new IllegalArgumentException("Invalid index: " + slot); | ||
24 | } | ||
25 | } | ||
26 | |||
27 | @Override | ||
28 | public Cursor<Tuple, T> getAdjacent(int slot, int node) { | ||
29 | validateSlot(slot); | ||
30 | var key = Tuple.of(node); | ||
31 | var value = get(key); | ||
32 | if (Objects.equals(value, getSymbol().defaultValue())) { | ||
33 | return Cursors.empty(); | ||
34 | } | ||
35 | return Cursors.singleton(key, value); | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public int getAdjacentSize(int slot, int node) { | ||
40 | validateSlot(slot); | ||
41 | var key = Tuple.of(node); | ||
42 | var value = get(key); | ||
43 | if (Objects.equals(value, getSymbol().defaultValue())) { | ||
44 | return 0; | ||
45 | } | ||
46 | return 1; | ||
47 | } | ||
48 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/VersionedInterpretation.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/VersionedInterpretation.java index 76e3baea..71df3962 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/model/internal/VersionedInterpretation.java +++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/VersionedInterpretation.java | |||
@@ -16,14 +16,14 @@ import tools.refinery.store.tuple.Tuple; | |||
16 | import java.util.ArrayList; | 16 | import java.util.ArrayList; |
17 | import java.util.List; | 17 | import java.util.List; |
18 | 18 | ||
19 | public class VersionedInterpretation<T> implements Interpretation<T> { | 19 | public abstract class VersionedInterpretation<T> implements Interpretation<T> { |
20 | private final ModelImpl model; | 20 | private final ModelImpl model; |
21 | private final Symbol<T> symbol; | 21 | private final Symbol<T> symbol; |
22 | private final VersionedMap<Tuple, T> map; | 22 | private final VersionedMap<Tuple, T> map; |
23 | private final List<InterpretationListener<T>> listeners = new ArrayList<>(); | 23 | private final List<InterpretationListener<T>> listeners = new ArrayList<>(); |
24 | private final List<InterpretationListener<T>> restoreListeners = new ArrayList<>(); | 24 | private final List<InterpretationListener<T>> restoreListeners = new ArrayList<>(); |
25 | 25 | ||
26 | private VersionedInterpretation(ModelImpl model, Symbol<T> symbol, VersionedMap<Tuple, T> map) { | 26 | protected VersionedInterpretation(ModelImpl model, Symbol<T> symbol, VersionedMap<Tuple, T> map) { |
27 | this.model = model; | 27 | this.model = model; |
28 | this.symbol = symbol; | 28 | this.symbol = symbol; |
29 | this.map = map; | 29 | this.map = map; |
@@ -50,6 +50,7 @@ public class VersionedInterpretation<T> implements Interpretation<T> { | |||
50 | .formatted(symbol, symbol.arity())); | 50 | .formatted(symbol, symbol.arity())); |
51 | } | 51 | } |
52 | } | 52 | } |
53 | |||
53 | @Override | 54 | @Override |
54 | public T get(Tuple key) { | 55 | public T get(Tuple key) { |
55 | checkKey(key); | 56 | checkKey(key); |
@@ -61,7 +62,7 @@ public class VersionedInterpretation<T> implements Interpretation<T> { | |||
61 | return map.getAll(); | 62 | return map.getAll(); |
62 | } | 63 | } |
63 | 64 | ||
64 | private void notifyListeners(Tuple key, T fromValue, T toValue, boolean restoring) { | 65 | protected void valueChanged(Tuple key, T fromValue, T toValue, boolean restoring) { |
65 | var listenerList = restoring ? restoreListeners : listeners; | 66 | var listenerList = restoring ? restoreListeners : listeners; |
66 | int listenerCount = listenerList.size(); | 67 | int listenerCount = listenerList.size(); |
67 | // Use a for loop instead of a for-each loop to avoid <code>Iterator</code> allocation overhead. | 68 | // Use a for loop instead of a for-each loop to avoid <code>Iterator</code> allocation overhead. |
@@ -76,7 +77,7 @@ public class VersionedInterpretation<T> implements Interpretation<T> { | |||
76 | checkKey(key); | 77 | checkKey(key); |
77 | model.markAsChanged(); | 78 | model.markAsChanged(); |
78 | var oldValue = map.put(key, value); | 79 | var oldValue = map.put(key, value); |
79 | notifyListeners(key, oldValue, value, false); | 80 | valueChanged(key, oldValue, value, false); |
80 | return oldValue; | 81 | return oldValue; |
81 | } | 82 | } |
82 | 83 | ||
@@ -114,11 +115,16 @@ public class VersionedInterpretation<T> implements Interpretation<T> { | |||
114 | Version commit() { | 115 | Version commit() { |
115 | return map.commit(); | 116 | return map.commit(); |
116 | } | 117 | } |
117 | void restore(Version state) { | 118 | |
118 | if (!restoreListeners.isEmpty()) { | 119 | protected boolean shouldNotifyRestoreListeners() { |
120 | return !restoreListeners.isEmpty(); | ||
121 | } | ||
122 | |||
123 | public void restore(Version state) { | ||
124 | if (shouldNotifyRestoreListeners()) { | ||
119 | var diffCursor = getDiffCursor(state); | 125 | var diffCursor = getDiffCursor(state); |
120 | while (diffCursor.move()) { | 126 | while (diffCursor.move()) { |
121 | notifyListeners(diffCursor.getKey(), diffCursor.getFromValue(), diffCursor.getToValue(), true); | 127 | valueChanged(diffCursor.getKey(), diffCursor.getFromValue(), diffCursor.getToValue(), true); |
122 | } | 128 | } |
123 | } | 129 | } |
124 | map.restore(state); | 130 | map.restore(state); |
@@ -142,7 +148,7 @@ public class VersionedInterpretation<T> implements Interpretation<T> { | |||
142 | @SuppressWarnings("unchecked") | 148 | @SuppressWarnings("unchecked") |
143 | var typedSymbol = (Symbol<T>) symbol; | 149 | var typedSymbol = (Symbol<T>) symbol; |
144 | var map = store.createMap(); | 150 | var map = store.createMap(); |
145 | return new VersionedInterpretation<>(model, typedSymbol, map); | 151 | return of(model, typedSymbol, map); |
146 | } | 152 | } |
147 | 153 | ||
148 | static <T> VersionedInterpretation<T> of(ModelImpl model, AnySymbol symbol, VersionedMapStore<Tuple, T> store, | 154 | static <T> VersionedInterpretation<T> of(ModelImpl model, AnySymbol symbol, VersionedMapStore<Tuple, T> store, |
@@ -150,6 +156,15 @@ public class VersionedInterpretation<T> implements Interpretation<T> { | |||
150 | @SuppressWarnings("unchecked") | 156 | @SuppressWarnings("unchecked") |
151 | var typedSymbol = (Symbol<T>) symbol; | 157 | var typedSymbol = (Symbol<T>) symbol; |
152 | var map = store.createMap(state); | 158 | var map = store.createMap(state); |
153 | return new VersionedInterpretation<>(model, typedSymbol, map); | 159 | return of(model, typedSymbol, map); |
160 | } | ||
161 | |||
162 | private static <T> VersionedInterpretation<T> of(ModelImpl model, Symbol<T> typedSymbol, | ||
163 | VersionedMap<Tuple, T> map) { | ||
164 | return switch (typedSymbol.arity()) { | ||
165 | case 0 -> new NullaryVersionedInterpretation<>(model, typedSymbol, map); | ||
166 | case 1 -> new UnaryVersionedInterpretation<>(model, typedSymbol, map); | ||
167 | default -> new IndexedVersionedInterpretation<>(model, typedSymbol, map); | ||
168 | }; | ||
154 | } | 169 | } |
155 | } | 170 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/AbstractDomain.java b/subprojects/store/src/main/java/tools/refinery/store/representation/AbstractDomain.java index 52c740e8..dfdb43bd 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/AbstractDomain.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/AbstractDomain.java | |||
@@ -5,6 +5,7 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.representation; | 6 | package tools.refinery.store.representation; |
7 | 7 | ||
8 | import java.util.Objects; | ||
8 | import java.util.Optional; | 9 | import java.util.Optional; |
9 | 10 | ||
10 | public non-sealed interface AbstractDomain<A, C> extends AnyAbstractDomain { | 11 | public non-sealed interface AbstractDomain<A, C> extends AnyAbstractDomain { |
@@ -22,7 +23,9 @@ public non-sealed interface AbstractDomain<A, C> extends AnyAbstractDomain { | |||
22 | return toConcrete(abstractValue).isPresent(); | 23 | return toConcrete(abstractValue).isPresent(); |
23 | } | 24 | } |
24 | 25 | ||
25 | boolean isRefinement(A originalValue, A refinedValue); | 26 | default boolean isRefinement(A originalValue, A refinedValue) { |
27 | return Objects.equals(commonRefinement(originalValue, refinedValue), refinedValue); | ||
28 | } | ||
26 | 29 | ||
27 | A commonRefinement(A leftValue, A rightValue); | 30 | A commonRefinement(A leftValue, A rightValue); |
28 | 31 | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValue.java b/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValue.java index 40baf9a5..f81ee9a4 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValue.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValue.java | |||
@@ -36,6 +36,10 @@ public enum TruthValue { | |||
36 | return this != UNKNOWN; | 36 | return this != UNKNOWN; |
37 | } | 37 | } |
38 | 38 | ||
39 | public boolean isConcrete() { | ||
40 | return this == TRUE || this == FALSE; | ||
41 | } | ||
42 | |||
39 | public boolean must() { | 43 | public boolean must() { |
40 | return this == TRUE || this == ERROR; | 44 | return this == TRUE || this == ERROR; |
41 | } | 45 | } |
@@ -55,9 +59,18 @@ public enum TruthValue { | |||
55 | public TruthValue merge(TruthValue other) { | 59 | public TruthValue merge(TruthValue other) { |
56 | return switch (this) { | 60 | return switch (this) { |
57 | case TRUE -> other == UNKNOWN || other == TRUE ? TRUE : ERROR; | 61 | case TRUE -> other == UNKNOWN || other == TRUE ? TRUE : ERROR; |
58 | case FALSE -> other == TruthValue.UNKNOWN || other == TruthValue.FALSE ? FALSE : ERROR; | 62 | case FALSE -> other == UNKNOWN || other == FALSE ? FALSE : ERROR; |
59 | case UNKNOWN -> other; | 63 | case UNKNOWN -> other; |
60 | default -> ERROR; | 64 | case ERROR -> ERROR; |
65 | }; | ||
66 | } | ||
67 | |||
68 | public TruthValue join(TruthValue other) { | ||
69 | return switch (this) { | ||
70 | case TRUE -> other == ERROR || other == TRUE ? TRUE : UNKNOWN; | ||
71 | case FALSE -> other == ERROR || other == FALSE ? FALSE : UNKNOWN; | ||
72 | case UNKNOWN -> UNKNOWN; | ||
73 | case ERROR -> other; | ||
61 | }; | 74 | }; |
62 | } | 75 | } |
63 | } | 76 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValueDomain.java b/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValueDomain.java index 89f8dd19..61696dca 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValueDomain.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/TruthValueDomain.java | |||
@@ -7,6 +7,8 @@ package tools.refinery.store.representation; | |||
7 | 7 | ||
8 | import java.util.Optional; | 8 | import java.util.Optional; |
9 | 9 | ||
10 | // Singleton pattern, because there is only one domain for truth values. | ||
11 | @SuppressWarnings("squid:S6548") | ||
10 | public final class TruthValueDomain implements AbstractDomain<TruthValue, Boolean> { | 12 | public final class TruthValueDomain implements AbstractDomain<TruthValue, Boolean> { |
11 | public static final TruthValueDomain INSTANCE = new TruthValueDomain(); | 13 | public static final TruthValueDomain INSTANCE = new TruthValueDomain(); |
12 | 14 | ||
@@ -15,51 +17,50 @@ public final class TruthValueDomain implements AbstractDomain<TruthValue, Boolea | |||
15 | 17 | ||
16 | @Override | 18 | @Override |
17 | public Class<TruthValue> abstractType() { | 19 | public Class<TruthValue> abstractType() { |
18 | return null; | 20 | return TruthValue.class; |
19 | } | 21 | } |
20 | 22 | ||
21 | @Override | 23 | @Override |
22 | public Class<Boolean> concreteType() { | 24 | public Class<Boolean> concreteType() { |
23 | return null; | 25 | return Boolean.class; |
24 | } | 26 | } |
25 | 27 | ||
26 | @Override | 28 | @Override |
27 | public TruthValue toAbstract(Boolean concreteValue) { | 29 | public TruthValue toAbstract(Boolean concreteValue) { |
28 | return null; | 30 | return TruthValue.toTruthValue(concreteValue); |
29 | } | 31 | } |
30 | 32 | ||
31 | @Override | 33 | @Override |
32 | public Optional<Boolean> toConcrete(TruthValue abstractValue) { | 34 | public Optional<Boolean> toConcrete(TruthValue abstractValue) { |
33 | return Optional.empty(); | 35 | return switch (abstractValue) { |
36 | case TRUE -> Optional.of(true); | ||
37 | case FALSE -> Optional.of(false); | ||
38 | default -> Optional.empty(); | ||
39 | }; | ||
34 | } | 40 | } |
35 | 41 | ||
36 | @Override | 42 | @Override |
37 | public boolean isConcrete(TruthValue abstractValue) { | 43 | public boolean isConcrete(TruthValue abstractValue) { |
38 | return AbstractDomain.super.isConcrete(abstractValue); | 44 | return abstractValue.isConcrete(); |
39 | } | ||
40 | |||
41 | @Override | ||
42 | public boolean isRefinement(TruthValue originalValue, TruthValue refinedValue) { | ||
43 | return false; | ||
44 | } | 45 | } |
45 | 46 | ||
46 | @Override | 47 | @Override |
47 | public TruthValue commonRefinement(TruthValue leftValue, TruthValue rightValue) { | 48 | public TruthValue commonRefinement(TruthValue leftValue, TruthValue rightValue) { |
48 | return null; | 49 | return leftValue.merge(rightValue); |
49 | } | 50 | } |
50 | 51 | ||
51 | @Override | 52 | @Override |
52 | public TruthValue commonAncestor(TruthValue leftValue, TruthValue rightValue) { | 53 | public TruthValue commonAncestor(TruthValue leftValue, TruthValue rightValue) { |
53 | return null; | 54 | return leftValue.join(rightValue); |
54 | } | 55 | } |
55 | 56 | ||
56 | @Override | 57 | @Override |
57 | public TruthValue unknown() { | 58 | public TruthValue unknown() { |
58 | return null; | 59 | return TruthValue.UNKNOWN; |
59 | } | 60 | } |
60 | 61 | ||
61 | @Override | 62 | @Override |
62 | public boolean isError(TruthValue abstractValue) { | 63 | public boolean isError(TruthValue abstractValue) { |
63 | return false; | 64 | return !abstractValue.isConsistent(); |
64 | } | 65 | } |
65 | } | 66 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityDomain.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityDomain.java new file mode 100644 index 00000000..7ae2d935 --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityDomain.java | |||
@@ -0,0 +1,68 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.representation.cardinality; | ||
7 | |||
8 | import tools.refinery.store.representation.AbstractDomain; | ||
9 | |||
10 | import java.util.Optional; | ||
11 | |||
12 | // Singleton pattern, because there is only one domain for truth values. | ||
13 | @SuppressWarnings("squid:S6548") | ||
14 | public class CardinalityDomain implements AbstractDomain<CardinalityInterval, Integer> { | ||
15 | public static final CardinalityDomain INSTANCE = new CardinalityDomain(); | ||
16 | |||
17 | private CardinalityDomain() { | ||
18 | } | ||
19 | |||
20 | @Override | ||
21 | public Class<CardinalityInterval> abstractType() { | ||
22 | return CardinalityInterval.class; | ||
23 | } | ||
24 | |||
25 | @Override | ||
26 | public Class<Integer> concreteType() { | ||
27 | return Integer.class; | ||
28 | } | ||
29 | |||
30 | @Override | ||
31 | public CardinalityInterval toAbstract(Integer concreteValue) { | ||
32 | return CardinalityIntervals.exactly(concreteValue); | ||
33 | } | ||
34 | |||
35 | @Override | ||
36 | public Optional<Integer> toConcrete(CardinalityInterval abstractValue) { | ||
37 | return isConcrete(abstractValue) ? Optional.of(abstractValue.lowerBound()) : Optional.empty(); | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public boolean isConcrete(CardinalityInterval abstractValue) { | ||
42 | if (!(abstractValue instanceof NonEmptyCardinalityInterval nonEmptyValue) || | ||
43 | !((nonEmptyValue.upperBound()) instanceof FiniteUpperCardinality finiteUpperCardinality)) { | ||
44 | return false; | ||
45 | } | ||
46 | return nonEmptyValue.lowerBound() == finiteUpperCardinality.finiteUpperBound(); | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public CardinalityInterval commonRefinement(CardinalityInterval leftValue, CardinalityInterval rightValue) { | ||
51 | return leftValue.meet(rightValue); | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public CardinalityInterval commonAncestor(CardinalityInterval leftValue, CardinalityInterval rightValue) { | ||
56 | return leftValue.join(rightValue); | ||
57 | } | ||
58 | |||
59 | @Override | ||
60 | public CardinalityInterval unknown() { | ||
61 | return CardinalityIntervals.SET; | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public boolean isError(CardinalityInterval abstractValue) { | ||
66 | return abstractValue.isEmpty(); | ||
67 | } | ||
68 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityInterval.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityInterval.java index 704ca2fc..b20c685a 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityInterval.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityInterval.java | |||
@@ -18,6 +18,8 @@ public sealed interface CardinalityInterval permits NonEmptyCardinalityInterval, | |||
18 | 18 | ||
19 | CardinalityInterval add(CardinalityInterval other); | 19 | CardinalityInterval add(CardinalityInterval other); |
20 | 20 | ||
21 | CardinalityInterval take(int count); | ||
22 | |||
21 | CardinalityInterval multiply(CardinalityInterval other); | 23 | CardinalityInterval multiply(CardinalityInterval other); |
22 | 24 | ||
23 | CardinalityInterval meet(CardinalityInterval other); | 25 | CardinalityInterval meet(CardinalityInterval other); |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityIntervals.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityIntervals.java index ad16a3e8..855fd248 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityIntervals.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/CardinalityIntervals.java | |||
@@ -30,7 +30,7 @@ public final class CardinalityIntervals { | |||
30 | } | 30 | } |
31 | 31 | ||
32 | public static CardinalityInterval between(int lowerBound, int upperBound) { | 32 | public static CardinalityInterval between(int lowerBound, int upperBound) { |
33 | return between(lowerBound, UpperCardinalities.valueOf(upperBound)); | 33 | return between(lowerBound, UpperCardinalities.atMost(upperBound)); |
34 | } | 34 | } |
35 | 35 | ||
36 | public static CardinalityInterval atMost(UpperCardinality upperBound) { | 36 | public static CardinalityInterval atMost(UpperCardinality upperBound) { |
@@ -38,7 +38,7 @@ public final class CardinalityIntervals { | |||
38 | } | 38 | } |
39 | 39 | ||
40 | public static CardinalityInterval atMost(int upperBound) { | 40 | public static CardinalityInterval atMost(int upperBound) { |
41 | return atMost(UpperCardinalities.valueOf(upperBound)); | 41 | return atMost(UpperCardinalities.atMost(upperBound)); |
42 | } | 42 | } |
43 | 43 | ||
44 | public static CardinalityInterval atLeast(int lowerBound) { | 44 | public static CardinalityInterval atLeast(int lowerBound) { |
@@ -46,6 +46,6 @@ public final class CardinalityIntervals { | |||
46 | } | 46 | } |
47 | 47 | ||
48 | public static CardinalityInterval exactly(int lowerBound) { | 48 | public static CardinalityInterval exactly(int lowerBound) { |
49 | return new NonEmptyCardinalityInterval(lowerBound, UpperCardinalities.valueOf(lowerBound)); | 49 | return new NonEmptyCardinalityInterval(lowerBound, UpperCardinalities.atMost(lowerBound)); |
50 | } | 50 | } |
51 | } | 51 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/EmptyCardinalityInterval.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/EmptyCardinalityInterval.java index 49911c29..9e371e21 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/EmptyCardinalityInterval.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/EmptyCardinalityInterval.java | |||
@@ -5,6 +5,8 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.representation.cardinality; | 6 | package tools.refinery.store.representation.cardinality; |
7 | 7 | ||
8 | // Singleton implementation, because there is only a single empty interval. | ||
9 | @SuppressWarnings("squid:S6548") | ||
8 | public final class EmptyCardinalityInterval implements CardinalityInterval { | 10 | public final class EmptyCardinalityInterval implements CardinalityInterval { |
9 | static final EmptyCardinalityInterval INSTANCE = new EmptyCardinalityInterval(); | 11 | static final EmptyCardinalityInterval INSTANCE = new EmptyCardinalityInterval(); |
10 | 12 | ||
@@ -43,6 +45,11 @@ public final class EmptyCardinalityInterval implements CardinalityInterval { | |||
43 | } | 45 | } |
44 | 46 | ||
45 | @Override | 47 | @Override |
48 | public CardinalityInterval take(int count) { | ||
49 | return this; | ||
50 | } | ||
51 | |||
52 | @Override | ||
46 | public CardinalityInterval multiply(CardinalityInterval other) { | 53 | public CardinalityInterval multiply(CardinalityInterval other) { |
47 | return this; | 54 | return this; |
48 | } | 55 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/FiniteUpperCardinality.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/FiniteUpperCardinality.java index 82afdbbc..088e3925 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/FiniteUpperCardinality.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/FiniteUpperCardinality.java | |||
@@ -6,6 +6,7 @@ | |||
6 | package tools.refinery.store.representation.cardinality; | 6 | package tools.refinery.store.representation.cardinality; |
7 | 7 | ||
8 | import org.jetbrains.annotations.NotNull; | 8 | import org.jetbrains.annotations.NotNull; |
9 | import org.jetbrains.annotations.Nullable; | ||
9 | 10 | ||
10 | import java.util.function.IntBinaryOperator; | 11 | import java.util.function.IntBinaryOperator; |
11 | 12 | ||
@@ -22,6 +23,15 @@ public record FiniteUpperCardinality(int finiteUpperBound) implements UpperCardi | |||
22 | } | 23 | } |
23 | 24 | ||
24 | @Override | 25 | @Override |
26 | @Nullable | ||
27 | public UpperCardinality take(int count) { | ||
28 | if (finiteUpperBound < count) { | ||
29 | return null; | ||
30 | } | ||
31 | return new FiniteUpperCardinality(finiteUpperBound - count); | ||
32 | } | ||
33 | |||
34 | @Override | ||
25 | public UpperCardinality multiply(UpperCardinality other) { | 35 | public UpperCardinality multiply(UpperCardinality other) { |
26 | return lift(other, (a, b) -> a * b); | 36 | return lift(other, (a, b) -> a * b); |
27 | } | 37 | } |
@@ -49,7 +59,7 @@ public record FiniteUpperCardinality(int finiteUpperBound) implements UpperCardi | |||
49 | 59 | ||
50 | private UpperCardinality lift(@NotNull UpperCardinality other, IntBinaryOperator operator) { | 60 | private UpperCardinality lift(@NotNull UpperCardinality other, IntBinaryOperator operator) { |
51 | if (other instanceof FiniteUpperCardinality finiteUpperCardinality) { | 61 | if (other instanceof FiniteUpperCardinality finiteUpperCardinality) { |
52 | return UpperCardinalities.valueOf(operator.applyAsInt(finiteUpperBound, | 62 | return UpperCardinalities.atMost(operator.applyAsInt(finiteUpperBound, |
53 | finiteUpperCardinality.finiteUpperBound)); | 63 | finiteUpperCardinality.finiteUpperBound)); |
54 | } | 64 | } |
55 | if (other instanceof UnboundedUpperCardinality) { | 65 | if (other instanceof UnboundedUpperCardinality) { |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/NonEmptyCardinalityInterval.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/NonEmptyCardinalityInterval.java index 38bd53bf..bfaeea25 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/NonEmptyCardinalityInterval.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/NonEmptyCardinalityInterval.java | |||
@@ -53,6 +53,16 @@ public record NonEmptyCardinalityInterval(int lowerBound, UpperCardinality upper | |||
53 | return lift(other, Math::min, UpperCardinality::max, this); | 53 | return lift(other, Math::min, UpperCardinality::max, this); |
54 | } | 54 | } |
55 | 55 | ||
56 | @Override | ||
57 | public CardinalityInterval take(int count) { | ||
58 | int newLowerBound = Math.max(lowerBound - count, 0); | ||
59 | var newUpperBound = upperBound.take(count); | ||
60 | if (newUpperBound == null) { | ||
61 | return CardinalityIntervals.ERROR; | ||
62 | } | ||
63 | return CardinalityIntervals.between(newLowerBound, newUpperBound); | ||
64 | } | ||
65 | |||
56 | private CardinalityInterval lift(CardinalityInterval other, IntBinaryOperator lowerOperator, | 66 | private CardinalityInterval lift(CardinalityInterval other, IntBinaryOperator lowerOperator, |
57 | BinaryOperator<UpperCardinality> upperOperator, | 67 | BinaryOperator<UpperCardinality> upperOperator, |
58 | CardinalityInterval whenEmpty) { | 68 | CardinalityInterval whenEmpty) { |
@@ -73,7 +83,10 @@ public record NonEmptyCardinalityInterval(int lowerBound, UpperCardinality upper | |||
73 | 83 | ||
74 | @Override | 84 | @Override |
75 | public String toString() { | 85 | public String toString() { |
76 | var closeBracket = upperBound instanceof UnboundedUpperCardinality ? ")" : "]"; | 86 | if (upperBound instanceof FiniteUpperCardinality finiteUpperCardinality && |
77 | return "[%d..%s%s".formatted(lowerBound, upperBound, closeBracket); | 87 | finiteUpperCardinality.finiteUpperBound() == lowerBound) { |
88 | return "[%d]".formatted(lowerBound); | ||
89 | } | ||
90 | return "[%d..%s]".formatted(lowerBound, upperBound); | ||
78 | } | 91 | } |
79 | } | 92 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UnboundedUpperCardinality.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UnboundedUpperCardinality.java index a5634020..e3a334cd 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UnboundedUpperCardinality.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UnboundedUpperCardinality.java | |||
@@ -7,6 +7,8 @@ package tools.refinery.store.representation.cardinality; | |||
7 | 7 | ||
8 | import org.jetbrains.annotations.NotNull; | 8 | import org.jetbrains.annotations.NotNull; |
9 | 9 | ||
10 | // Singleton implementation, because there is only a single countable infinity. | ||
11 | @SuppressWarnings("squid:S6548") | ||
10 | public final class UnboundedUpperCardinality implements UpperCardinality { | 12 | public final class UnboundedUpperCardinality implements UpperCardinality { |
11 | static final UnboundedUpperCardinality INSTANCE = new UnboundedUpperCardinality(); | 13 | static final UnboundedUpperCardinality INSTANCE = new UnboundedUpperCardinality(); |
12 | 14 | ||
@@ -20,10 +22,17 @@ public final class UnboundedUpperCardinality implements UpperCardinality { | |||
20 | } | 22 | } |
21 | 23 | ||
22 | @Override | 24 | @Override |
25 | public UpperCardinality take(int count) { | ||
26 | return this; | ||
27 | } | ||
28 | |||
29 | @Override | ||
23 | public UpperCardinality multiply(UpperCardinality other) { | 30 | public UpperCardinality multiply(UpperCardinality other) { |
24 | return this; | 31 | return this; |
25 | } | 32 | } |
26 | 33 | ||
34 | // This should always be greater than any finite cardinality. | ||
35 | @SuppressWarnings("ComparatorMethodParameterNotUsed") | ||
27 | @Override | 36 | @Override |
28 | public int compareTo(@NotNull UpperCardinality upperCardinality) { | 37 | public int compareTo(@NotNull UpperCardinality upperCardinality) { |
29 | if (upperCardinality instanceof FiniteUpperCardinality) { | 38 | if (upperCardinality instanceof FiniteUpperCardinality) { |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinalities.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinalities.java index 1e18dde0..17d1b292 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinalities.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinalities.java | |||
@@ -26,7 +26,7 @@ public final class UpperCardinalities { | |||
26 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | 26 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); |
27 | } | 27 | } |
28 | 28 | ||
29 | public static UpperCardinality valueOf(int upperBound) { | 29 | public static UpperCardinality atMost(int upperBound) { |
30 | if (upperBound < 0) { | 30 | if (upperBound < 0) { |
31 | return UNBOUNDED; | 31 | return UNBOUNDED; |
32 | } | 32 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinality.java b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinality.java index 5dbaa922..3f0db028 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinality.java +++ b/subprojects/store/src/main/java/tools/refinery/store/representation/cardinality/UpperCardinality.java | |||
@@ -5,6 +5,8 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.representation.cardinality; | 6 | package tools.refinery.store.representation.cardinality; |
7 | 7 | ||
8 | import org.jetbrains.annotations.Nullable; | ||
9 | |||
8 | public sealed interface UpperCardinality extends Comparable<UpperCardinality> permits FiniteUpperCardinality, | 10 | public sealed interface UpperCardinality extends Comparable<UpperCardinality> permits FiniteUpperCardinality, |
9 | UnboundedUpperCardinality { | 11 | UnboundedUpperCardinality { |
10 | default UpperCardinality min(UpperCardinality other) { | 12 | default UpperCardinality min(UpperCardinality other) { |
@@ -17,11 +19,14 @@ public sealed interface UpperCardinality extends Comparable<UpperCardinality> pe | |||
17 | 19 | ||
18 | UpperCardinality add(UpperCardinality other); | 20 | UpperCardinality add(UpperCardinality other); |
19 | 21 | ||
22 | @Nullable | ||
23 | UpperCardinality take(int count); | ||
24 | |||
20 | UpperCardinality multiply(UpperCardinality other); | 25 | UpperCardinality multiply(UpperCardinality other); |
21 | 26 | ||
22 | int compareToInt(int value); | 27 | int compareToInt(int value); |
23 | 28 | ||
24 | static UpperCardinality of(int upperBound) { | 29 | static UpperCardinality of(int upperBound) { |
25 | return UpperCardinalities.valueOf(upperBound); | 30 | return UpperCardinalities.atMost(upperBound); |
26 | } | 31 | } |
27 | } | 32 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCoderAdapter.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCoderAdapter.java index 8cfd24d5..cb73d27e 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCoderAdapter.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCoderAdapter.java | |||
@@ -17,7 +17,7 @@ public interface StateCoderAdapter extends ModelAdapter { | |||
17 | return calculateStateCode().objectCode(); | 17 | return calculateStateCode().objectCode(); |
18 | } | 18 | } |
19 | 19 | ||
20 | static StateCoderBuilderImpl builder() { | 20 | static StateCoderBuilder builder() { |
21 | return new StateCoderBuilderImpl(); | 21 | return new StateCoderBuilderImpl(); |
22 | } | 22 | } |
23 | } | 23 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateEquivalenceChecker.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateEquivalenceChecker.java index 3fd8c8d8..0f0023ed 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateEquivalenceChecker.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateEquivalenceChecker.java | |||
@@ -6,7 +6,7 @@ | |||
6 | package tools.refinery.store.statecoding; | 6 | package tools.refinery.store.statecoding; |
7 | 7 | ||
8 | import org.eclipse.collections.api.set.primitive.IntSet; | 8 | import org.eclipse.collections.api.set.primitive.IntSet; |
9 | import tools.refinery.store.model.Interpretation; | 9 | import tools.refinery.store.model.AnyInterpretation; |
10 | 10 | ||
11 | import java.util.List; | 11 | import java.util.List; |
12 | 12 | ||
@@ -16,9 +16,6 @@ public interface StateEquivalenceChecker { | |||
16 | } | 16 | } |
17 | 17 | ||
18 | EquivalenceResult constructMorphism( | 18 | EquivalenceResult constructMorphism( |
19 | IntSet individuals, | 19 | IntSet individuals, List<? extends AnyInterpretation> interpretations1, ObjectCode code1, |
20 | List<? extends Interpretation<?>> interpretations1, | 20 | List<? extends AnyInterpretation> interpretations2, ObjectCode code2); |
21 | ObjectCode code1, List<? | ||
22 | extends Interpretation<?>> interpretations2, | ||
23 | ObjectCode code2); | ||
24 | } | 21 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java index 05b47c52..eed591e7 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java | |||
@@ -5,61 +5,61 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.statecoding.internal; | 6 | package tools.refinery.store.statecoding.internal; |
7 | 7 | ||
8 | import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet; | 8 | import org.eclipse.collections.api.factory.primitive.IntSets; |
9 | import org.eclipse.collections.api.set.primitive.MutableIntSet; | ||
10 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; | ||
9 | import tools.refinery.store.model.ModelStore; | 11 | import tools.refinery.store.model.ModelStore; |
10 | import tools.refinery.store.model.ModelStoreBuilder; | ||
11 | import tools.refinery.store.representation.AnySymbol; | 12 | import tools.refinery.store.representation.AnySymbol; |
12 | import tools.refinery.store.representation.Symbol; | 13 | import tools.refinery.store.representation.Symbol; |
13 | import tools.refinery.store.statecoding.*; | 14 | import tools.refinery.store.statecoding.StateCodeCalculatorFactory; |
15 | import tools.refinery.store.statecoding.StateCoderBuilder; | ||
16 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; | ||
17 | import tools.refinery.store.statecoding.StateEquivalenceChecker; | ||
14 | import tools.refinery.store.statecoding.neighbourhood.NeighbourhoodCalculator; | 18 | import tools.refinery.store.statecoding.neighbourhood.NeighbourhoodCalculator; |
15 | import tools.refinery.store.statecoding.stateequivalence.StateEquivalenceCheckerImpl; | 19 | import tools.refinery.store.statecoding.stateequivalence.StateEquivalenceCheckerImpl; |
16 | import tools.refinery.store.tuple.Tuple1; | 20 | import tools.refinery.store.tuple.Tuple1; |
17 | 21 | ||
18 | import java.util.*; | 22 | import java.util.HashSet; |
23 | import java.util.LinkedHashSet; | ||
24 | import java.util.Set; | ||
19 | 25 | ||
20 | public class StateCoderBuilderImpl implements StateCoderBuilder { | 26 | public class StateCoderBuilderImpl extends AbstractModelAdapterBuilder<StateCoderStoreAdapter> |
21 | Set<AnySymbol> excluded = new HashSet<>(); | 27 | implements StateCoderBuilder { |
22 | IntHashSet individuals = new IntHashSet(); | 28 | private final Set<AnySymbol> excluded = new HashSet<>(); |
23 | 29 | private final MutableIntSet individuals = IntSets.mutable.empty(); | |
24 | StateCodeCalculatorFactory calculator = NeighbourhoodCalculator::new; | 30 | private StateCodeCalculatorFactory calculator = NeighbourhoodCalculator::new; |
25 | StateEquivalenceChecker checker = new StateEquivalenceCheckerImpl(); | 31 | private StateEquivalenceChecker checker = new StateEquivalenceCheckerImpl(); |
26 | 32 | ||
27 | @Override | 33 | @Override |
28 | public StateCoderBuilder exclude(AnySymbol symbol) { | 34 | public StateCoderBuilder exclude(AnySymbol symbol) { |
35 | checkNotConfigured(); | ||
29 | excluded.add(symbol); | 36 | excluded.add(symbol); |
30 | return this; | 37 | return this; |
31 | } | 38 | } |
32 | 39 | ||
33 | @Override | 40 | @Override |
34 | public StateCoderBuilder individual(Tuple1 tuple) { | 41 | public StateCoderBuilder individual(Tuple1 tuple) { |
42 | checkNotConfigured(); | ||
35 | individuals.add(tuple.get(0)); | 43 | individuals.add(tuple.get(0)); |
36 | return this; | 44 | return this; |
37 | } | 45 | } |
38 | 46 | ||
39 | @Override | 47 | @Override |
40 | public StateCoderBuilder stateEquivalenceChecker(StateEquivalenceChecker stateEquivalenceChecker) { | 48 | public StateCoderBuilder stateEquivalenceChecker(StateEquivalenceChecker stateEquivalenceChecker) { |
49 | checkNotConfigured(); | ||
41 | this.checker = stateEquivalenceChecker; | 50 | this.checker = stateEquivalenceChecker; |
42 | return this; | 51 | return this; |
43 | } | 52 | } |
44 | 53 | ||
45 | @Override | 54 | @Override |
46 | public StateCoderBuilder stateCodeCalculatorFactory(StateCodeCalculatorFactory codeCalculatorFactory) { | 55 | public StateCoderBuilder stateCodeCalculatorFactory(StateCodeCalculatorFactory codeCalculatorFactory) { |
56 | checkNotConfigured(); | ||
47 | this.calculator = codeCalculatorFactory; | 57 | this.calculator = codeCalculatorFactory; |
48 | return this; | 58 | return this; |
49 | } | 59 | } |
50 | 60 | ||
51 | @Override | 61 | @Override |
52 | public boolean isConfigured() { | 62 | protected StateCoderStoreAdapter doBuild(ModelStore store) { |
53 | return true; | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public void configure(ModelStoreBuilder storeBuilder) { | ||
58 | // It does not modify the build process | ||
59 | } | ||
60 | |||
61 | @Override | ||
62 | public StateCoderStoreAdapter build(ModelStore store) { | ||
63 | Set<Symbol<?>> symbols = new LinkedHashSet<>(); | 63 | Set<Symbol<?>> symbols = new LinkedHashSet<>(); |
64 | for (AnySymbol symbol : store.getSymbols()) { | 64 | for (AnySymbol symbol : store.getSymbols()) { |
65 | if (!excluded.contains(symbol) && (symbol instanceof Symbol<?> typed)) { | 65 | if (!excluded.contains(symbol) && (symbol instanceof Symbol<?> typed)) { |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java index 0a40a19d..5d390da2 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java | |||
@@ -5,8 +5,10 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.statecoding.neighbourhood; | 6 | package tools.refinery.store.statecoding.neighbourhood; |
7 | 7 | ||
8 | import org.eclipse.collections.api.factory.primitive.IntLongMaps; | ||
9 | import org.eclipse.collections.api.map.primitive.MutableIntLongMap; | ||
8 | import org.eclipse.collections.api.set.primitive.IntSet; | 10 | import org.eclipse.collections.api.set.primitive.IntSet; |
9 | import org.eclipse.collections.impl.map.mutable.primitive.IntLongHashMap; | 11 | import tools.refinery.store.model.AnyInterpretation; |
10 | import tools.refinery.store.model.Interpretation; | 12 | import tools.refinery.store.model.Interpretation; |
11 | import tools.refinery.store.statecoding.ObjectCode; | 13 | import tools.refinery.store.statecoding.ObjectCode; |
12 | import tools.refinery.store.tuple.Tuple; | 14 | import tools.refinery.store.tuple.Tuple; |
@@ -15,25 +17,25 @@ import tools.refinery.store.tuple.Tuple0; | |||
15 | import java.util.*; | 17 | import java.util.*; |
16 | 18 | ||
17 | public abstract class AbstractNeighbourhoodCalculator { | 19 | public abstract class AbstractNeighbourhoodCalculator { |
18 | protected final List<Interpretation<?>> nullImpactValues; | 20 | protected final List<AnyInterpretation> nullImpactValues; |
19 | protected final LinkedHashMap<Interpretation<?>, long[]> impactValues; | 21 | protected final LinkedHashMap<AnyInterpretation, long[]> impactValues; |
20 | protected final IntLongHashMap individualHashValues; | 22 | protected final MutableIntLongMap individualHashValues = IntLongMaps.mutable.empty(); |
21 | 23 | ||
22 | protected static final long PRIME = 31; | 24 | protected static final long PRIME = 31; |
23 | 25 | ||
24 | protected AbstractNeighbourhoodCalculator(List<? extends Interpretation<?>> interpretations, IntSet individuals) { | 26 | protected AbstractNeighbourhoodCalculator(List<? extends AnyInterpretation> interpretations, IntSet individuals) { |
25 | this.nullImpactValues = new ArrayList<>(); | 27 | this.nullImpactValues = new ArrayList<>(); |
26 | this.impactValues = new LinkedHashMap<>(); | 28 | this.impactValues = new LinkedHashMap<>(); |
29 | // Random isn't used for cryptographical purposes but just to assign distinguishable identifiers to symbols. | ||
27 | @SuppressWarnings("squid:S2245") | 30 | @SuppressWarnings("squid:S2245") |
28 | Random random = new Random(1); | 31 | Random random = new Random(1); |
29 | 32 | ||
30 | individualHashValues = new IntLongHashMap(); | ||
31 | var individualsInOrder = individuals.toSortedList(Integer::compare); | 33 | var individualsInOrder = individuals.toSortedList(Integer::compare); |
32 | for(int i = 0; i<individualsInOrder.size(); i++) { | 34 | for(int i = 0; i<individualsInOrder.size(); i++) { |
33 | individualHashValues.put(individualsInOrder.get(i), random.nextLong()); | 35 | individualHashValues.put(individualsInOrder.get(i), random.nextLong()); |
34 | } | 36 | } |
35 | 37 | ||
36 | for (Interpretation<?> interpretation : interpretations) { | 38 | for (AnyInterpretation interpretation : interpretations) { |
37 | int arity = interpretation.getSymbol().arity(); | 39 | int arity = interpretation.getSymbol().arity(); |
38 | if (arity == 0) { | 40 | if (arity == 0) { |
39 | nullImpactValues.add(interpretation); | 41 | nullImpactValues.add(interpretation); |
@@ -86,7 +88,7 @@ public abstract class AbstractNeighbourhoodCalculator { | |||
86 | protected long calculateModelCode(long lastSum) { | 88 | protected long calculateModelCode(long lastSum) { |
87 | long result = 0; | 89 | long result = 0; |
88 | for (var nullImpactValue : nullImpactValues) { | 90 | for (var nullImpactValue : nullImpactValues) { |
89 | result = result * PRIME + Objects.hashCode(nullImpactValue.get(Tuple0.INSTANCE)); | 91 | result = result * PRIME + Objects.hashCode(((Interpretation<?>) nullImpactValue).get(Tuple0.INSTANCE)); |
90 | } | 92 | } |
91 | result += lastSum; | 93 | result += lastSum; |
92 | return result; | 94 | return result; |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/LazyNeighbourhoodCalculator.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/LazyNeighbourhoodCalculator.java index 2ffbef5e..c188a839 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/LazyNeighbourhoodCalculator.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/LazyNeighbourhoodCalculator.java | |||
@@ -5,10 +5,12 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.statecoding.neighbourhood; | 6 | package tools.refinery.store.statecoding.neighbourhood; |
7 | 7 | ||
8 | import org.eclipse.collections.api.factory.primitive.LongIntMaps; | ||
8 | import org.eclipse.collections.api.map.primitive.LongIntMap; | 9 | import org.eclipse.collections.api.map.primitive.LongIntMap; |
10 | import org.eclipse.collections.api.map.primitive.MutableLongIntMap; | ||
9 | import org.eclipse.collections.api.set.primitive.IntSet; | 11 | import org.eclipse.collections.api.set.primitive.IntSet; |
10 | import org.eclipse.collections.impl.map.mutable.primitive.LongIntHashMap; | ||
11 | import tools.refinery.store.map.Cursor; | 12 | import tools.refinery.store.map.Cursor; |
13 | import tools.refinery.store.model.AnyInterpretation; | ||
12 | import tools.refinery.store.model.Interpretation; | 14 | import tools.refinery.store.model.Interpretation; |
13 | import tools.refinery.store.statecoding.StateCodeCalculator; | 15 | import tools.refinery.store.statecoding.StateCodeCalculator; |
14 | import tools.refinery.store.statecoding.StateCoderResult; | 16 | import tools.refinery.store.statecoding.StateCoderResult; |
@@ -17,13 +19,13 @@ import tools.refinery.store.tuple.Tuple; | |||
17 | import java.util.List; | 19 | import java.util.List; |
18 | 20 | ||
19 | public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator implements StateCodeCalculator { | 21 | public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator implements StateCodeCalculator { |
20 | public LazyNeighbourhoodCalculator(List<? extends Interpretation<?>> interpretations, IntSet individuals) { | 22 | public LazyNeighbourhoodCalculator(List<? extends AnyInterpretation> interpretations, IntSet individuals) { |
21 | super(interpretations, individuals); | 23 | super(interpretations, individuals); |
22 | } | 24 | } |
23 | 25 | ||
24 | public StateCoderResult calculateCodes() { | 26 | public StateCoderResult calculateCodes() { |
25 | ObjectCodeImpl previousObjectCode = new ObjectCodeImpl(); | 27 | ObjectCodeImpl previousObjectCode = new ObjectCodeImpl(); |
26 | LongIntHashMap prevHash2Amount = new LongIntHashMap(); | 28 | MutableLongIntMap prevHash2Amount = LongIntMaps.mutable.empty(); |
27 | 29 | ||
28 | long lastSum; | 30 | long lastSum; |
29 | // All hash code is 0, except to the individuals. | 31 | // All hash code is 0, except to the individuals. |
@@ -42,7 +44,7 @@ public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator | |||
42 | } | 44 | } |
43 | constructNextObjectCodes(previousObjectCode, nextObjectCode, prevHash2Amount); | 45 | constructNextObjectCodes(previousObjectCode, nextObjectCode, prevHash2Amount); |
44 | 46 | ||
45 | LongIntHashMap nextHash2Amount = new LongIntHashMap(); | 47 | MutableLongIntMap nextHash2Amount = LongIntMaps.mutable.empty(); |
46 | lastSum = calculateLastSum(previousObjectCode, nextObjectCode, prevHash2Amount, nextHash2Amount); | 48 | lastSum = calculateLastSum(previousObjectCode, nextObjectCode, prevHash2Amount, nextHash2Amount); |
47 | 49 | ||
48 | int nextSize = nextHash2Amount.size(); | 50 | int nextSize = nextHash2Amount.size(); |
@@ -60,7 +62,7 @@ public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator | |||
60 | } | 62 | } |
61 | 63 | ||
62 | private long calculateLastSum(ObjectCodeImpl previous, ObjectCodeImpl next, LongIntMap hash2Amount, | 64 | private long calculateLastSum(ObjectCodeImpl previous, ObjectCodeImpl next, LongIntMap hash2Amount, |
63 | LongIntHashMap nextHash2Amount) { | 65 | MutableLongIntMap nextHash2Amount) { |
64 | long lastSum = 0; | 66 | long lastSum = 0; |
65 | for (int i = 0; i < next.getSize(); i++) { | 67 | for (int i = 0; i < next.getSize(); i++) { |
66 | final long hash; | 68 | final long hash; |
@@ -84,7 +86,7 @@ public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator | |||
84 | 86 | ||
85 | private void constructNextObjectCodes(ObjectCodeImpl previous, ObjectCodeImpl next, LongIntMap hash2Amount) { | 87 | private void constructNextObjectCodes(ObjectCodeImpl previous, ObjectCodeImpl next, LongIntMap hash2Amount) { |
86 | for (var impactValueEntry : this.impactValues.entrySet()) { | 88 | for (var impactValueEntry : this.impactValues.entrySet()) { |
87 | Interpretation<?> interpretation = impactValueEntry.getKey(); | 89 | Interpretation<?> interpretation = (Interpretation<?>) impactValueEntry.getKey(); |
88 | var cursor = interpretation.getAll(); | 90 | var cursor = interpretation.getAll(); |
89 | int arity = interpretation.getSymbol().arity(); | 91 | int arity = interpretation.getSymbol().arity(); |
90 | long[] impactValue = impactValueEntry.getValue(); | 92 | long[] impactValue = impactValueEntry.getValue(); |
@@ -114,7 +116,8 @@ public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator | |||
114 | return amount == 1; | 116 | return amount == 1; |
115 | } | 117 | } |
116 | 118 | ||
117 | private void lazyImpactCalculation1(LongIntMap hash2Amount, ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, Cursor<Tuple, ?> cursor) { | 119 | private void lazyImpactCalculation1(LongIntMap hash2Amount, ObjectCodeImpl previous, ObjectCodeImpl next, |
120 | long[] impactValues, Cursor<Tuple, ?> cursor) { | ||
118 | 121 | ||
119 | Tuple tuple = cursor.getKey(); | 122 | Tuple tuple = cursor.getKey(); |
120 | int o = tuple.get(0); | 123 | int o = tuple.get(0); |
@@ -129,7 +132,8 @@ public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator | |||
129 | } | 132 | } |
130 | } | 133 | } |
131 | 134 | ||
132 | private void lazyImpactCalculation2(LongIntMap hash2Amount, ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, Cursor<Tuple, ?> cursor) { | 135 | private void lazyImpactCalculation2(LongIntMap hash2Amount, ObjectCodeImpl previous, ObjectCodeImpl next, |
136 | long[] impactValues, Cursor<Tuple, ?> cursor) { | ||
133 | final Tuple tuple = cursor.getKey(); | 137 | final Tuple tuple = cursor.getKey(); |
134 | final int o1 = tuple.get(0); | 138 | final int o1 = tuple.get(0); |
135 | final int o2 = tuple.get(1); | 139 | final int o2 = tuple.get(1); |
@@ -155,7 +159,8 @@ public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator | |||
155 | } | 159 | } |
156 | } | 160 | } |
157 | 161 | ||
158 | private void lazyImpactCalculationN(LongIntMap hash2Amount, ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, Cursor<Tuple, ?> cursor) { | 162 | private void lazyImpactCalculationN(LongIntMap hash2Amount, ObjectCodeImpl previous, ObjectCodeImpl next, |
163 | long[] impactValues, Cursor<Tuple, ?> cursor) { | ||
159 | final Tuple tuple = cursor.getKey(); | 164 | final Tuple tuple = cursor.getKey(); |
160 | 165 | ||
161 | final boolean[] uniques = new boolean[tuple.getSize()]; | 166 | final boolean[] uniques = new boolean[tuple.getSize()]; |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java index 5b3e5ea3..785fda7a 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java | |||
@@ -43,7 +43,7 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp | |||
43 | private long calculateLastSum(ObjectCode codes) { | 43 | private long calculateLastSum(ObjectCode codes) { |
44 | long result = 0; | 44 | long result = 0; |
45 | for (var nullImpactValue : nullImpactValues) { | 45 | for (var nullImpactValue : nullImpactValues) { |
46 | result = result * 31 + Objects.hashCode(nullImpactValue.get(Tuple0.INSTANCE)); | 46 | result = result * 31 + Objects.hashCode(((Interpretation<?>) nullImpactValue).get(Tuple0.INSTANCE)); |
47 | } | 47 | } |
48 | 48 | ||
49 | for (int i = 0; i < codes.getSize(); i++) { | 49 | for (int i = 0; i < codes.getSize(); i++) { |
@@ -56,7 +56,7 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp | |||
56 | 56 | ||
57 | private void constructNextObjectCodes(ObjectCodeImpl previous, ObjectCodeImpl next) { | 57 | private void constructNextObjectCodes(ObjectCodeImpl previous, ObjectCodeImpl next) { |
58 | for (var impactValueEntry : this.impactValues.entrySet()) { | 58 | for (var impactValueEntry : this.impactValues.entrySet()) { |
59 | Interpretation<?> interpretation = impactValueEntry.getKey(); | 59 | Interpretation<?> interpretation = (Interpretation<?>) impactValueEntry.getKey(); |
60 | var cursor = interpretation.getAll(); | 60 | var cursor = interpretation.getAll(); |
61 | int arity = interpretation.getSymbol().arity(); | 61 | int arity = interpretation.getSymbol().arity(); |
62 | long[] impactValue = impactValueEntry.getValue(); | 62 | long[] impactValue = impactValueEntry.getValue(); |
@@ -78,7 +78,8 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp | |||
78 | } | 78 | } |
79 | 79 | ||
80 | 80 | ||
81 | private void impactCalculation1(ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, Cursor<Tuple, ?> cursor) { | 81 | private void impactCalculation1(ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, |
82 | Cursor<Tuple, ?> cursor) { | ||
82 | 83 | ||
83 | Tuple tuple = cursor.getKey(); | 84 | Tuple tuple = cursor.getKey(); |
84 | int o = tuple.get(0); | 85 | int o = tuple.get(0); |
@@ -87,7 +88,8 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp | |||
87 | addHash(next, o, impactValues[0], tupleHash); | 88 | addHash(next, o, impactValues[0], tupleHash); |
88 | } | 89 | } |
89 | 90 | ||
90 | private void impactCalculation2(ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, Cursor<Tuple, ?> cursor) { | 91 | private void impactCalculation2(ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, |
92 | Cursor<Tuple, ?> cursor) { | ||
91 | final Tuple tuple = cursor.getKey(); | 93 | final Tuple tuple = cursor.getKey(); |
92 | final int o1 = tuple.get(0); | 94 | final int o1 = tuple.get(0); |
93 | final int o2 = tuple.get(1); | 95 | final int o2 = tuple.get(1); |
@@ -99,7 +101,8 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp | |||
99 | addHash(next, o2, impactValues[1], tupleHash); | 101 | addHash(next, o2, impactValues[1], tupleHash); |
100 | } | 102 | } |
101 | 103 | ||
102 | private void impactCalculationN(ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, Cursor<Tuple, ?> cursor) { | 104 | private void impactCalculationN(ObjectCodeImpl previous, ObjectCodeImpl next, long[] impactValues, |
105 | Cursor<Tuple, ?> cursor) { | ||
103 | final Tuple tuple = cursor.getKey(); | 106 | final Tuple tuple = cursor.getKey(); |
104 | 107 | ||
105 | Object value = cursor.getValue(); | 108 | Object value = cursor.getValue(); |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairing.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairing.java index decff1d5..0682e1a4 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairing.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairing.java | |||
@@ -8,18 +8,14 @@ package tools.refinery.store.statecoding.stateequivalence; | |||
8 | import org.eclipse.collections.api.factory.primitive.IntIntMaps; | 8 | import org.eclipse.collections.api.factory.primitive.IntIntMaps; |
9 | import org.eclipse.collections.api.map.primitive.IntIntMap; | 9 | import org.eclipse.collections.api.map.primitive.IntIntMap; |
10 | import org.eclipse.collections.api.set.primitive.IntSet; | 10 | import org.eclipse.collections.api.set.primitive.IntSet; |
11 | import org.eclipse.collections.impl.list.Interval; | ||
12 | import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet; | ||
13 | 11 | ||
14 | import java.util.ArrayList; | 12 | import java.util.*; |
15 | import java.util.Arrays; | ||
16 | import java.util.List; | ||
17 | 13 | ||
18 | public class CombinationNodePairing implements NodePairing { | 14 | public class CombinationNodePairing implements NodePairing { |
19 | private final int[] left; | 15 | private final int[] left; |
20 | private final int[] right; | 16 | private final int[] right; |
21 | 17 | ||
22 | CombinationNodePairing(IntSet left, IntHashSet right) { | 18 | CombinationNodePairing(IntSet left, IntSet right) { |
23 | this.left = left.toArray(); | 19 | this.left = left.toArray(); |
24 | this.right = right.toArray(); | 20 | this.right = right.toArray(); |
25 | 21 | ||
@@ -32,59 +28,33 @@ public class CombinationNodePairing implements NodePairing { | |||
32 | return left.length; | 28 | return left.length; |
33 | } | 29 | } |
34 | 30 | ||
35 | static final int LIMIT = 5; | 31 | private static final int LIMIT = 5; |
36 | static final List<List<int[]>> permutations = new ArrayList<>(); | 32 | // Enum-based singleton used to delay generating all permutations until they are first needed. |
37 | 33 | @SuppressWarnings("squid:S6548") | |
38 | /** | 34 | private enum PermutationsHolder { |
39 | * Generates and stores permutations up to a given size. If the number would be more than a limit, it provides a | 35 | INSTANCE; |
40 | * single permutation only. | 36 | |
41 | * | 37 | final CombinationNodePairingPermutations permutations = new CombinationNodePairingPermutations(LIMIT); |
42 | * @param max The max number in the permutation | ||
43 | * @return A complete list of permutations of numbers 0...max, or a single permutation. | ||
44 | */ | ||
45 | public static List<int[]> getPermutations(int max) { | ||
46 | if (max < permutations.size()) { | ||
47 | return permutations.get(max); | ||
48 | } | ||
49 | if (max == 0) { | ||
50 | List<int[]> result = new ArrayList<>(); | ||
51 | result.add(new int[1]); | ||
52 | permutations.add(result); | ||
53 | return result; | ||
54 | } | ||
55 | List<int[]> result = new ArrayList<>(); | ||
56 | List<int[]> previousPermutations = getPermutations(max - 1); | ||
57 | for (var permutation : previousPermutations) { | ||
58 | for (int pos = 0; pos <= max; pos++) { | ||
59 | int[] newPermutation = new int[max + 1]; | ||
60 | System.arraycopy(permutation, 0, newPermutation, 0, pos); | ||
61 | newPermutation[pos] = max; | ||
62 | if (max - (pos + 1) >= 0) | ||
63 | System.arraycopy(permutation, pos + 1, newPermutation, pos + 1 + 1, max - (pos + 1)); | ||
64 | result.add(newPermutation); | ||
65 | } | ||
66 | } | ||
67 | permutations.add(result); | ||
68 | return result; | ||
69 | } | 38 | } |
70 | 39 | ||
71 | @Override | 40 | @Override |
72 | public List<IntIntMap> permutations() { | 41 | public List<IntIntMap> permutations() { |
73 | final var interval = Interval.zeroTo(this.size() - 1); | 42 | int limit = this.size(); |
43 | Iterable<Integer> interval = () -> new IntervalIterator(limit); | ||
74 | 44 | ||
75 | if (isComplete()) { | 45 | if (isComplete()) { |
76 | final List<int[]> p = getPermutations(this.size() - 1); | 46 | final List<int[]> p = PermutationsHolder.INSTANCE.permutations.getPermutations(this.size() - 1); |
77 | return p.stream().map(x -> constructPermutationMap(interval, x)).toList(); | 47 | return p.stream().map(x -> constructPermutationMap(interval, x)).toList(); |
78 | } else { | 48 | } else { |
79 | return List.of(constructTrivialMap(interval)); | 49 | return List.of(constructTrivialMap(interval)); |
80 | } | 50 | } |
81 | } | 51 | } |
82 | 52 | ||
83 | private IntIntMap constructTrivialMap(Interval interval) { | 53 | private IntIntMap constructTrivialMap(Iterable<Integer> interval) { |
84 | return IntIntMaps.immutable.from(interval, l -> left[l], r -> right[r]); | 54 | return IntIntMaps.immutable.from(interval, l -> left[l], r -> right[r]); |
85 | } | 55 | } |
86 | 56 | ||
87 | private IntIntMap constructPermutationMap(Interval interval, int[] permutation) { | 57 | private IntIntMap constructPermutationMap(Iterable<Integer> interval, int[] permutation) { |
88 | return IntIntMaps.immutable.from(interval, l -> left[l], r -> right[permutation[r]]); | 58 | return IntIntMaps.immutable.from(interval, l -> left[l], r -> right[permutation[r]]); |
89 | } | 59 | } |
90 | 60 | ||
@@ -92,4 +62,28 @@ public class CombinationNodePairing implements NodePairing { | |||
92 | public boolean isComplete() { | 62 | public boolean isComplete() { |
93 | return this.size() <= LIMIT; | 63 | return this.size() <= LIMIT; |
94 | } | 64 | } |
65 | |||
66 | private static class IntervalIterator implements Iterator<Integer> { | ||
67 | private final int limit; | ||
68 | private int value = 0; | ||
69 | |||
70 | private IntervalIterator(int max) { | ||
71 | this.limit = max; | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public boolean hasNext() { | ||
76 | return value < limit; | ||
77 | } | ||
78 | |||
79 | @Override | ||
80 | public Integer next() { | ||
81 | if (value >= limit) { | ||
82 | throw new NoSuchElementException("End of interval"); | ||
83 | } | ||
84 | int next = value; | ||
85 | value++; | ||
86 | return next; | ||
87 | } | ||
88 | } | ||
95 | } | 89 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java new file mode 100644 index 00000000..eacd3a2a --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java | |||
@@ -0,0 +1,57 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.statecoding.stateequivalence; | ||
7 | |||
8 | import java.util.ArrayList; | ||
9 | import java.util.List; | ||
10 | |||
11 | class CombinationNodePairingPermutations { | ||
12 | private final List<List<int[]>> permutations = new ArrayList<>(); | ||
13 | |||
14 | public CombinationNodePairingPermutations(int max) { | ||
15 | initializePermutations(max); | ||
16 | } | ||
17 | |||
18 | public List<int[]> getPermutations(int max) { | ||
19 | if (max >= permutations.size()) { | ||
20 | throw new IllegalArgumentException("Only permutations up to %d elements are supported".formatted(max)); | ||
21 | } | ||
22 | return permutations.get(max); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * Generates and stores permutations up to a given size. If the number would be more than a limit, it provides a | ||
27 | * single permutation only. | ||
28 | * | ||
29 | * @param max The max number in the permutation | ||
30 | * @return A complete list of permutations of numbers 0...max, or a single permutation. | ||
31 | */ | ||
32 | private List<int[]> initializePermutations(int max) { | ||
33 | if (max < permutations.size()) { | ||
34 | return permutations.get(max); | ||
35 | } | ||
36 | if (max == 0) { | ||
37 | List<int[]> result = new ArrayList<>(); | ||
38 | result.add(new int[1]); | ||
39 | permutations.add(result); | ||
40 | return result; | ||
41 | } | ||
42 | List<int[]> result = new ArrayList<>(); | ||
43 | List<int[]> previousPermutations = initializePermutations(max - 1); | ||
44 | for (var permutation : previousPermutations) { | ||
45 | for (int pos = 0; pos <= max; pos++) { | ||
46 | int[] newPermutation = new int[max + 1]; | ||
47 | System.arraycopy(permutation, 0, newPermutation, 0, pos); | ||
48 | newPermutation[pos] = max; | ||
49 | if (max - (pos + 1) >= 0) | ||
50 | System.arraycopy(permutation, pos + 1, newPermutation, pos + 1 + 1, max - (pos + 1)); | ||
51 | result.add(newPermutation); | ||
52 | } | ||
53 | } | ||
54 | permutations.add(result); | ||
55 | return result; | ||
56 | } | ||
57 | } | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/NodePairing.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/NodePairing.java index 7e5db7a3..f45f0d2e 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/NodePairing.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/NodePairing.java | |||
@@ -6,18 +6,18 @@ | |||
6 | package tools.refinery.store.statecoding.stateequivalence; | 6 | package tools.refinery.store.statecoding.stateequivalence; |
7 | 7 | ||
8 | import org.eclipse.collections.api.map.primitive.IntIntMap; | 8 | import org.eclipse.collections.api.map.primitive.IntIntMap; |
9 | import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet; | 9 | import org.eclipse.collections.api.set.primitive.IntSet; |
10 | 10 | ||
11 | import java.util.List; | 11 | import java.util.List; |
12 | 12 | ||
13 | public interface NodePairing { | 13 | public interface NodePairing { |
14 | |||
15 | int size(); | 14 | int size(); |
15 | |||
16 | List<IntIntMap> permutations(); | 16 | List<IntIntMap> permutations(); |
17 | 17 | ||
18 | boolean isComplete(); | 18 | boolean isComplete(); |
19 | 19 | ||
20 | static NodePairing constructNodePairing(IntHashSet left, IntHashSet right){ | 20 | static NodePairing constructNodePairing(IntSet left, IntSet right){ |
21 | if(left.size() != right.size()) { | 21 | if(left.size() != right.size()) { |
22 | return null; | 22 | return null; |
23 | } | 23 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/PermutationMorphism.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/PermutationMorphism.java index 6be0f3e7..bc4d723a 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/PermutationMorphism.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/PermutationMorphism.java | |||
@@ -12,12 +12,12 @@ import java.util.List; | |||
12 | 12 | ||
13 | public class PermutationMorphism implements Morphism { | 13 | public class PermutationMorphism implements Morphism { |
14 | private final IntIntMap object2PermutationGroup; | 14 | private final IntIntMap object2PermutationGroup; |
15 | private final List<List<IntIntMap>> permutationsGroups; | 15 | private final List<? extends List<? extends IntIntMap>> permutationsGroups; |
16 | private final int[] selection; | 16 | private final int[] selection; |
17 | private boolean hasNext; | 17 | private boolean hasNext; |
18 | 18 | ||
19 | PermutationMorphism(IntIntMap object2PermutationGroup, | 19 | PermutationMorphism(IntIntMap object2PermutationGroup, |
20 | List<List<IntIntMap>> permutationsGroups) { | 20 | List<? extends List<? extends IntIntMap>> permutationsGroups) { |
21 | this.object2PermutationGroup = object2PermutationGroup; | 21 | this.object2PermutationGroup = object2PermutationGroup; |
22 | this.permutationsGroups = permutationsGroups; | 22 | this.permutationsGroups = permutationsGroups; |
23 | 23 | ||
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/StateEquivalenceCheckerImpl.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/StateEquivalenceCheckerImpl.java index ef0d76a7..5a62d8a0 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/StateEquivalenceCheckerImpl.java +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/StateEquivalenceCheckerImpl.java | |||
@@ -6,11 +6,14 @@ | |||
6 | package tools.refinery.store.statecoding.stateequivalence; | 6 | package tools.refinery.store.statecoding.stateequivalence; |
7 | 7 | ||
8 | import org.eclipse.collections.api.factory.primitive.IntIntMaps; | 8 | import org.eclipse.collections.api.factory.primitive.IntIntMaps; |
9 | import org.eclipse.collections.api.factory.primitive.IntSets; | ||
10 | import org.eclipse.collections.api.factory.primitive.LongObjectMaps; | ||
9 | import org.eclipse.collections.api.map.primitive.IntIntMap; | 11 | import org.eclipse.collections.api.map.primitive.IntIntMap; |
12 | import org.eclipse.collections.api.map.primitive.MutableIntIntMap; | ||
13 | import org.eclipse.collections.api.map.primitive.MutableLongObjectMap; | ||
10 | import org.eclipse.collections.api.set.primitive.IntSet; | 14 | import org.eclipse.collections.api.set.primitive.IntSet; |
11 | import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap; | 15 | import org.eclipse.collections.api.set.primitive.MutableIntSet; |
12 | import org.eclipse.collections.impl.map.mutable.primitive.LongObjectHashMap; | 16 | import tools.refinery.store.model.AnyInterpretation; |
13 | import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet; | ||
14 | import tools.refinery.store.model.Interpretation; | 17 | import tools.refinery.store.model.Interpretation; |
15 | import tools.refinery.store.statecoding.Morphism; | 18 | import tools.refinery.store.statecoding.Morphism; |
16 | import tools.refinery.store.statecoding.ObjectCode; | 19 | import tools.refinery.store.statecoding.ObjectCode; |
@@ -26,12 +29,11 @@ public class StateEquivalenceCheckerImpl implements StateEquivalenceChecker { | |||
26 | 29 | ||
27 | @Override | 30 | @Override |
28 | public EquivalenceResult constructMorphism(IntSet individuals, | 31 | public EquivalenceResult constructMorphism(IntSet individuals, |
29 | List<? extends Interpretation<?>> interpretations1, | 32 | List<? extends AnyInterpretation> interpretations1, |
30 | ObjectCode code1, | 33 | ObjectCode code1, |
31 | List<? extends Interpretation<?>> interpretations2, | 34 | List<? extends AnyInterpretation> interpretations2, |
32 | ObjectCode code2) | 35 | ObjectCode code2) { |
33 | { | 36 | MutableIntIntMap object2PermutationGroup = IntIntMaps.mutable.empty(); |
34 | IntIntHashMap object2PermutationGroup = new IntIntHashMap(); | ||
35 | List<List<IntIntMap>> permutationsGroups = new ArrayList<>(); | 37 | List<List<IntIntMap>> permutationsGroups = new ArrayList<>(); |
36 | 38 | ||
37 | final EquivalenceResult permutations = constructPermutationNavigation(individuals, | 39 | final EquivalenceResult permutations = constructPermutationNavigation(individuals, |
@@ -50,7 +52,7 @@ public class StateEquivalenceCheckerImpl implements StateEquivalenceChecker { | |||
50 | return permutations; | 52 | return permutations; |
51 | } | 53 | } |
52 | 54 | ||
53 | if(tried >= LIMIT) { | 55 | if (tried >= LIMIT) { |
54 | return EquivalenceResult.UNKNOWN; | 56 | return EquivalenceResult.UNKNOWN; |
55 | } | 57 | } |
56 | 58 | ||
@@ -58,22 +60,22 @@ public class StateEquivalenceCheckerImpl implements StateEquivalenceChecker { | |||
58 | tried++; | 60 | tried++; |
59 | } while (hasNext); | 61 | } while (hasNext); |
60 | 62 | ||
61 | if(permutations == EquivalenceResult.UNKNOWN) { | 63 | if (permutations == EquivalenceResult.UNKNOWN) { |
62 | return EquivalenceResult.UNKNOWN; | 64 | return EquivalenceResult.UNKNOWN; |
63 | } else { | 65 | } else { |
64 | return EquivalenceResult.DIFFERENT; | 66 | return EquivalenceResult.DIFFERENT; |
65 | } | 67 | } |
66 | } | 68 | } |
67 | 69 | ||
68 | private LongObjectHashMap<IntHashSet> indexByHash(ObjectCode code, IntSet individuals) { | 70 | private MutableLongObjectMap<MutableIntSet> indexByHash(ObjectCode code, IntSet individuals) { |
69 | LongObjectHashMap<IntHashSet> result = new LongObjectHashMap<>(); | 71 | MutableLongObjectMap<MutableIntSet> result = LongObjectMaps.mutable.empty(); |
70 | for (int o = 0; o < code.getSize(); o++) { | 72 | for (int o = 0; o < code.getSize(); o++) { |
71 | if(! individuals.contains(o)){ | 73 | if (!individuals.contains(o)) { |
72 | long hash = code.get(o); | 74 | long hash = code.get(o); |
73 | if(hash != 0) { | 75 | if (hash != 0) { |
74 | var equivalenceClass = result.get(hash); | 76 | var equivalenceClass = result.get(hash); |
75 | if (equivalenceClass == null) { | 77 | if (equivalenceClass == null) { |
76 | equivalenceClass = new IntHashSet(); | 78 | equivalenceClass = IntSets.mutable.empty(); |
77 | result.put(hash, equivalenceClass); | 79 | result.put(hash, equivalenceClass); |
78 | } | 80 | } |
79 | equivalenceClass.add(o); | 81 | equivalenceClass.add(o); |
@@ -83,11 +85,9 @@ public class StateEquivalenceCheckerImpl implements StateEquivalenceChecker { | |||
83 | return result; | 85 | return result; |
84 | } | 86 | } |
85 | 87 | ||
86 | private EquivalenceResult constructPermutationNavigation(IntSet individuals, | 88 | private EquivalenceResult constructPermutationNavigation( |
87 | LongObjectHashMap<IntHashSet> map1, | 89 | IntSet individuals, MutableLongObjectMap<MutableIntSet> map1, MutableLongObjectMap<MutableIntSet> map2, |
88 | LongObjectHashMap<IntHashSet> map2, | 90 | MutableIntIntMap object2OptionIndex, List<List<IntIntMap>> listOfOptions) { |
89 | IntIntHashMap object2OptionIndex, | ||
90 | List<List<IntIntMap>> listOfOptions) { | ||
91 | if (map1.size() != map2.size()) { | 91 | if (map1.size() != map2.size()) { |
92 | return EquivalenceResult.DIFFERENT; | 92 | return EquivalenceResult.DIFFERENT; |
93 | } | 93 | } |
@@ -116,27 +116,28 @@ public class StateEquivalenceCheckerImpl implements StateEquivalenceChecker { | |||
116 | listOfOptions.add(pairing.permutations()); | 116 | listOfOptions.add(pairing.permutations()); |
117 | } | 117 | } |
118 | 118 | ||
119 | individuals.forEach(o -> listOfOptions.add(o,List.of(IntIntMaps.immutable.of(o,o)))); | 119 | individuals.forEach(o -> listOfOptions.add(o, List.of(IntIntMaps.immutable.of(o, o)))); |
120 | 120 | ||
121 | if(allComplete) { | 121 | if (allComplete) { |
122 | return EquivalenceResult.ISOMORPHIC; | 122 | return EquivalenceResult.ISOMORPHIC; |
123 | } else { | 123 | } else { |
124 | return EquivalenceResult.UNKNOWN; | 124 | return EquivalenceResult.UNKNOWN; |
125 | } | 125 | } |
126 | } | 126 | } |
127 | 127 | ||
128 | private boolean testMorphism(List<? extends Interpretation<?>> s, List<? extends Interpretation<?>> t, Morphism m) { | 128 | private boolean testMorphism(List<? extends AnyInterpretation> s, List<? extends AnyInterpretation> t, |
129 | Morphism m) { | ||
129 | for (int interpretationIndex = 0; interpretationIndex < s.size(); interpretationIndex++) { | 130 | for (int interpretationIndex = 0; interpretationIndex < s.size(); interpretationIndex++) { |
130 | var sI = s.get(interpretationIndex); | 131 | var sI = s.get(interpretationIndex); |
131 | var tI = t.get(interpretationIndex); | 132 | var tI = t.get(interpretationIndex); |
132 | 133 | ||
133 | var cursor = sI.getAll(); | 134 | var cursor = ((Interpretation<?>) sI).getAll(); |
134 | while (cursor.move()) { | 135 | while (cursor.move()) { |
135 | final Tuple sTuple = cursor.getKey(); | 136 | final Tuple sTuple = cursor.getKey(); |
136 | final Object sValue = cursor.getValue(); | 137 | final Object sValue = cursor.getValue(); |
137 | 138 | ||
138 | final Tuple tTuple = apply(sTuple, m); | 139 | final Tuple tTuple = apply(sTuple, m); |
139 | final Object tValue = tI.get(tTuple); | 140 | final Object tValue = ((Interpretation<?>) tI).get(tTuple); |
140 | 141 | ||
141 | if (!Objects.equals(sValue, tValue)) { | 142 | if (!Objects.equals(sValue, tValue)) { |
142 | return false; | 143 | return false; |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple.java index aae7b344..e9761763 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple.java | |||
@@ -12,6 +12,8 @@ public sealed interface Tuple extends Comparable<Tuple> permits Tuple0, Tuple1, | |||
12 | 12 | ||
13 | int get(int element); | 13 | int get(int element); |
14 | 14 | ||
15 | Tuple set(int element, int value); | ||
16 | |||
15 | @Override | 17 | @Override |
16 | default int compareTo(@NotNull Tuple other) { | 18 | default int compareTo(@NotNull Tuple other) { |
17 | int size = getSize(); | 19 | int size = getSize(); |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple0.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple0.java index a9aa9bf2..5f525798 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple0.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple0.java | |||
@@ -29,6 +29,11 @@ public final class Tuple0 implements Tuple { | |||
29 | } | 29 | } |
30 | 30 | ||
31 | @Override | 31 | @Override |
32 | public Tuple set(int element, int value) { | ||
33 | throw new IndexOutOfBoundsException(element); | ||
34 | } | ||
35 | |||
36 | @Override | ||
32 | public String toString() { | 37 | public String toString() { |
33 | return TUPLE_BEGIN + TUPLE_END; | 38 | return TUPLE_BEGIN + TUPLE_END; |
34 | } | 39 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple1.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple1.java index 388ee3a9..fb9497d2 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple1.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple1.java | |||
@@ -38,6 +38,14 @@ public final class Tuple1 implements Tuple { | |||
38 | } | 38 | } |
39 | 39 | ||
40 | @Override | 40 | @Override |
41 | public Tuple set(int element, int value) { | ||
42 | if (element == 0) { | ||
43 | return Tuple.of(value); | ||
44 | } | ||
45 | throw new IndexOutOfBoundsException(element); | ||
46 | } | ||
47 | |||
48 | @Override | ||
41 | public String toString() { | 49 | public String toString() { |
42 | return TUPLE_BEGIN + value0 + TUPLE_END; | 50 | return TUPLE_BEGIN + value0 + TUPLE_END; |
43 | } | 51 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple2.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple2.java index 6d886fd3..2213df97 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple2.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple2.java | |||
@@ -25,6 +25,15 @@ public record Tuple2(int value0, int value1) implements Tuple { | |||
25 | } | 25 | } |
26 | 26 | ||
27 | @Override | 27 | @Override |
28 | public Tuple set(int element, int value) { | ||
29 | return switch (element) { | ||
30 | case 0 -> Tuple.of(value, value1); | ||
31 | case 1 -> Tuple.of(value0, value); | ||
32 | default -> throw new ArrayIndexOutOfBoundsException(element); | ||
33 | }; | ||
34 | } | ||
35 | |||
36 | @Override | ||
28 | public String toString() { | 37 | public String toString() { |
29 | return TUPLE_BEGIN + value0 + TUPLE_SEPARATOR + value1 + TUPLE_END; | 38 | return TUPLE_BEGIN + value0 + TUPLE_SEPARATOR + value1 + TUPLE_END; |
30 | } | 39 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple3.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple3.java index 734e45c2..417770e8 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple3.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple3.java | |||
@@ -26,6 +26,16 @@ public record Tuple3(int value0, int value1, int value2) implements Tuple { | |||
26 | } | 26 | } |
27 | 27 | ||
28 | @Override | 28 | @Override |
29 | public Tuple set(int element, int value) { | ||
30 | return switch (element) { | ||
31 | case 0 -> Tuple.of(value, value1, value2); | ||
32 | case 1 -> Tuple.of(value0, value, value2); | ||
33 | case 2 -> Tuple.of(value0, value1, value); | ||
34 | default -> throw new ArrayIndexOutOfBoundsException(element); | ||
35 | }; | ||
36 | } | ||
37 | |||
38 | @Override | ||
29 | public String toString() { | 39 | public String toString() { |
30 | return TUPLE_BEGIN + value0 + TUPLE_SEPARATOR + value1 + TUPLE_SEPARATOR + value2 + TUPLE_END; | 40 | return TUPLE_BEGIN + value0 + TUPLE_SEPARATOR + value1 + TUPLE_SEPARATOR + value2 + TUPLE_END; |
31 | } | 41 | } |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple4.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple4.java index e1b93e7b..c4915198 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple4.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/Tuple4.java | |||
@@ -27,6 +27,17 @@ public record Tuple4(int value0, int value1, int value2, int value3) implements | |||
27 | } | 27 | } |
28 | 28 | ||
29 | @Override | 29 | @Override |
30 | public Tuple set(int element, int value) { | ||
31 | return switch (element) { | ||
32 | case 0 -> Tuple.of(value, value1, value2, value3); | ||
33 | case 1 -> Tuple.of(value0, value, value2, value3); | ||
34 | case 2 -> Tuple.of(value0, value1, value, value3); | ||
35 | case 3 -> Tuple.of(value0, value1, value2, value); | ||
36 | default -> throw new ArrayIndexOutOfBoundsException(element); | ||
37 | }; | ||
38 | } | ||
39 | |||
40 | @Override | ||
30 | public String toString() { | 41 | public String toString() { |
31 | return TUPLE_BEGIN + value0 + TUPLE_SEPARATOR + value1 + TUPLE_SEPARATOR + value2 + TUPLE_SEPARATOR + value3 + | 42 | return TUPLE_BEGIN + value0 + TUPLE_SEPARATOR + value1 + TUPLE_SEPARATOR + value2 + TUPLE_SEPARATOR + value3 + |
32 | TUPLE_END; | 43 | TUPLE_END; |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/tuple/TupleN.java b/subprojects/store/src/main/java/tools/refinery/store/tuple/TupleN.java index b66af491..b42b4b6a 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/tuple/TupleN.java +++ b/subprojects/store/src/main/java/tools/refinery/store/tuple/TupleN.java | |||
@@ -32,6 +32,16 @@ public final class TupleN implements Tuple { | |||
32 | } | 32 | } |
33 | 33 | ||
34 | @Override | 34 | @Override |
35 | public Tuple set(int element, int value) { | ||
36 | int size = getSize(); | ||
37 | var newValues = new int[size]; | ||
38 | for (int i = 0; i < size; i++) { | ||
39 | newValues[i] = element == i ? value : values[i]; | ||
40 | } | ||
41 | return Tuple.of(newValues); | ||
42 | } | ||
43 | |||
44 | @Override | ||
35 | public String toString() { | 45 | public String toString() { |
36 | var valuesString = Arrays.stream(values) | 46 | var valuesString = Arrays.stream(values) |
37 | .mapToObj(Integer::toString) | 47 | .mapToObj(Integer::toString) |
diff --git a/subprojects/store/src/main/java/tools/refinery/store/util/CycleDetectingMapper.java b/subprojects/store/src/main/java/tools/refinery/store/util/CycleDetectingMapper.java index 78ad2ad7..2e302663 100644 --- a/subprojects/store/src/main/java/tools/refinery/store/util/CycleDetectingMapper.java +++ b/subprojects/store/src/main/java/tools/refinery/store/util/CycleDetectingMapper.java | |||
@@ -20,6 +20,10 @@ public class CycleDetectingMapper<T, R> { | |||
20 | 20 | ||
21 | private final Map<T, R> results = new HashMap<>(); | 21 | private final Map<T, R> results = new HashMap<>(); |
22 | 22 | ||
23 | public CycleDetectingMapper(Function<T, R> doMap) { | ||
24 | this(Objects::toString, doMap); | ||
25 | } | ||
26 | |||
23 | public CycleDetectingMapper(Function<T, String> getName, Function<T, R> doMap) { | 27 | public CycleDetectingMapper(Function<T, String> getName, Function<T, R> doMap) { |
24 | this.getName = getName; | 28 | this.getName = getName; |
25 | this.doMap = doMap; | 29 | this.doMap = doMap; |