aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2023-09-11 19:22:26 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2023-09-11 19:22:26 +0200
commit4d365b54dad8d066bba2a2b1a05092b4802b9970 (patch)
tree12e5ae36b0041463ded54e7a69dcdc9f3662794f /subprojects/store
parentfix: build failures after integrating generation (diff)
downloadrefinery-4d365b54dad8d066bba2a2b1a05092b4802b9970.tar.gz
refinery-4d365b54dad8d066bba2a2b1a05092b4802b9970.tar.zst
refinery-4d365b54dad8d066bba2a2b1a05092b4802b9970.zip
feat: cancellation token for ModelStore
Diffstat (limited to 'subprojects/store')
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/Model.java4
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/ModelStore.java2
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/ModelStoreBuilder.java3
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelImpl.java16
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreBuilderImpl.java21
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreImpl.java15
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/model/internal/VersionedInterpretation.java6
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCodeCalculatorFactory.java3
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderStoreAdapterImpl.java2
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java6
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/LazyNeighbourhoodCalculator.java6
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java8
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/util/CancellationToken.java13
-rw-r--r--subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java3
-rw-r--r--subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderBuildTest.java3
15 files changed, 90 insertions, 21 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/Model.java b/subprojects/store/src/main/java/tools/refinery/store/model/Model.java
index e2ab72e7..c4ce5207 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/model/Model.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/model/Model.java
@@ -8,11 +8,9 @@ package tools.refinery.store.model;
8import tools.refinery.store.adapter.ModelAdapter; 8import tools.refinery.store.adapter.ModelAdapter;
9import tools.refinery.store.map.Version; 9import tools.refinery.store.map.Version;
10import tools.refinery.store.map.Versioned; 10import tools.refinery.store.map.Versioned;
11import tools.refinery.store.model.internal.VersionedInterpretation;
12import tools.refinery.store.representation.AnySymbol; 11import tools.refinery.store.representation.AnySymbol;
13import tools.refinery.store.representation.Symbol; 12import tools.refinery.store.representation.Symbol;
14 13
15import java.util.Map;
16import java.util.Optional; 14import java.util.Optional;
17 15
18public interface Model extends Versioned { 16public interface Model extends Versioned {
@@ -38,4 +36,6 @@ public interface Model extends Versioned {
38 void addListener(ModelListener listener); 36 void addListener(ModelListener listener);
39 37
40 void removeListener(ModelListener listener); 38 void removeListener(ModelListener listener);
39
40 void checkCancelled();
41} 41}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/ModelStore.java b/subprojects/store/src/main/java/tools/refinery/store/model/ModelStore.java
index 89382b3a..61abf126 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/model/ModelStore.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/model/ModelStore.java
@@ -26,6 +26,8 @@ public interface ModelStore {
26 26
27 <T extends ModelStoreAdapter> T getAdapter(Class<T> adapterType); 27 <T extends ModelStoreAdapter> T getAdapter(Class<T> adapterType);
28 28
29 void checkCancelled();
30
29 static ModelStoreBuilder builder() { 31 static ModelStoreBuilder builder() {
30 return new ModelStoreBuilderImpl(); 32 return new ModelStoreBuilderImpl();
31 } 33 }
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 8f652762..9b2b38c3 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
@@ -8,12 +8,15 @@ package tools.refinery.store.model;
8import tools.refinery.store.adapter.ModelAdapterBuilder; 8import tools.refinery.store.adapter.ModelAdapterBuilder;
9import tools.refinery.store.representation.AnySymbol; 9import tools.refinery.store.representation.AnySymbol;
10import tools.refinery.store.representation.Symbol; 10import tools.refinery.store.representation.Symbol;
11import tools.refinery.store.util.CancellationToken;
11 12
12import java.util.Collection; 13import java.util.Collection;
13import java.util.List; 14import java.util.List;
14import java.util.Optional; 15import java.util.Optional;
15 16
16public interface ModelStoreBuilder { 17public interface ModelStoreBuilder {
18 ModelStoreBuilder cancellationToken(CancellationToken cancellationToken);
19
17 default ModelStoreBuilder symbols(AnySymbol... symbols) { 20 default ModelStoreBuilder symbols(AnySymbol... symbols) {
18 return symbols(List.of(symbols)); 21 return symbols(List.of(symbols));
19 } 22 }
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelImpl.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelImpl.java
index 2b12d5a6..d11e431b 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelImpl.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelImpl.java
@@ -13,23 +13,26 @@ import tools.refinery.store.model.*;
13import tools.refinery.store.representation.AnySymbol; 13import tools.refinery.store.representation.AnySymbol;
14import tools.refinery.store.representation.Symbol; 14import tools.refinery.store.representation.Symbol;
15import tools.refinery.store.tuple.Tuple; 15import tools.refinery.store.tuple.Tuple;
16import tools.refinery.store.util.CancellationToken;
16 17
17import java.util.*; 18import java.util.*;
18 19
19public class ModelImpl implements Model { 20public class ModelImpl implements Model {
20 private final ModelStore store; 21 private final ModelStoreImpl store;
21 private Version state; 22 private Version state;
22 private LinkedHashMap<? extends AnySymbol, ? extends VersionedInterpretation<?>> interpretations; 23 private LinkedHashMap<? extends AnySymbol, ? extends VersionedInterpretation<?>> interpretations;
23 private final List<ModelAdapter> adapters; 24 private final List<ModelAdapter> adapters;
24 private final List<ModelListener> listeners = new ArrayList<>(); 25 private final List<ModelListener> listeners = new ArrayList<>();
26 private final CancellationToken cancellationToken;
25 private boolean uncommittedChanges; 27 private boolean uncommittedChanges;
26 private ModelAction pendingAction = ModelAction.NONE; 28 private ModelAction pendingAction = ModelAction.NONE;
27 private Version restoringToState = null; 29 private Version restoringToState = null;
28 30
29 ModelImpl(ModelStore store, Version state, int adapterCount) { 31 ModelImpl(ModelStoreImpl store, Version state, int adapterCount) {
30 this.store = store; 32 this.store = store;
31 this.state = state; 33 this.state = state;
32 adapters = new ArrayList<>(adapterCount); 34 adapters = new ArrayList<>(adapterCount);
35 cancellationToken = store.getCancellationToken();
33 } 36 }
34 37
35 void setInterpretations(LinkedHashMap<? extends AnySymbol, ? extends VersionedInterpretation<?>> interpretations) { 38 void setInterpretations(LinkedHashMap<? extends AnySymbol, ? extends VersionedInterpretation<?>> interpretations) {
@@ -88,6 +91,7 @@ public class ModelImpl implements Model {
88 91
89 @Override 92 @Override
90 public Version commit() { 93 public Version commit() {
94 checkCancelled();
91 if (hasPendingAction()) { 95 if (hasPendingAction()) {
92 throw pendingActionError("commit"); 96 throw pendingActionError("commit");
93 } 97 }
@@ -106,6 +110,7 @@ public class ModelImpl implements Model {
106 Version[] interpretationVersions = new Version[interpretations.size()]; 110 Version[] interpretationVersions = new Version[interpretations.size()];
107 int j = 0; 111 int j = 0;
108 for (var interpretationEntry : interpretations.entrySet()) { 112 for (var interpretationEntry : interpretations.entrySet()) {
113 checkCancelled();
109 interpretationVersions[j++] = interpretationEntry.getValue().commit(); 114 interpretationVersions[j++] = interpretationEntry.getValue().commit();
110 } 115 }
111 ModelVersion modelVersion = new ModelVersion(interpretationVersions); 116 ModelVersion modelVersion = new ModelVersion(interpretationVersions);
@@ -125,6 +130,7 @@ public class ModelImpl implements Model {
125 130
126 @Override 131 @Override
127 public void restore(Version version) { 132 public void restore(Version version) {
133 checkCancelled();
128 if (hasPendingAction()) { 134 if (hasPendingAction()) {
129 throw pendingActionError("restore to %s".formatted(version)); 135 throw pendingActionError("restore to %s".formatted(version));
130 } 136 }
@@ -140,6 +146,7 @@ public class ModelImpl implements Model {
140 } 146 }
141 int j = 0; 147 int j = 0;
142 for (var interpretation : interpretations.values()) { 148 for (var interpretation : interpretations.values()) {
149 checkCancelled();
143 interpretation.restore(ModelVersion.getInternalVersion(version, j++)); 150 interpretation.restore(ModelVersion.getInternalVersion(version, j++));
144 } 151 }
145 152
@@ -187,4 +194,9 @@ public class ModelImpl implements Model {
187 public void removeListener(ModelListener listener) { 194 public void removeListener(ModelListener listener) {
188 listeners.remove(listener); 195 listeners.remove(listener);
189 } 196 }
197
198 @Override
199 public void checkCancelled() {
200 cancellationToken.checkCancelled();
201 }
190} 202}
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 2dde7a4c..53ecde5e 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
@@ -16,15 +16,29 @@ import tools.refinery.store.model.ModelStoreConfiguration;
16import tools.refinery.store.representation.AnySymbol; 16import tools.refinery.store.representation.AnySymbol;
17import tools.refinery.store.representation.Symbol; 17import tools.refinery.store.representation.Symbol;
18import tools.refinery.store.tuple.Tuple; 18import tools.refinery.store.tuple.Tuple;
19import tools.refinery.store.util.CancellationToken;
19 20
20import java.util.*; 21import java.util.*;
21 22
22public class ModelStoreBuilderImpl implements ModelStoreBuilder { 23public class ModelStoreBuilderImpl implements ModelStoreBuilder {
24 private CancellationToken cancellationToken;
23 private final LinkedHashSet<AnySymbol> allSymbols = new LinkedHashSet<>(); 25 private final LinkedHashSet<AnySymbol> allSymbols = new LinkedHashSet<>();
24 private final LinkedHashMap<SymbolEquivalenceClass<?>, List<AnySymbol>> equivalenceClasses = new LinkedHashMap<>(); 26 private final LinkedHashMap<SymbolEquivalenceClass<?>, List<AnySymbol>> equivalenceClasses = new LinkedHashMap<>();
25 private final List<ModelAdapterBuilder> adapters = new ArrayList<>(); 27 private final List<ModelAdapterBuilder> adapters = new ArrayList<>();
26 28
27 @Override 29 @Override
30 public ModelStoreBuilder cancellationToken(CancellationToken cancellationToken) {
31 if (this.cancellationToken != null) {
32 throw new IllegalStateException("Cancellation token was already set");
33 }
34 if (cancellationToken == null) {
35 throw new IllegalStateException("Cancellation token must not be null");
36 }
37 this.cancellationToken = cancellationToken;
38 return this;
39 }
40
41 @Override
28 public <T> ModelStoreBuilder symbol(Symbol<T> symbol) { 42 public <T> ModelStoreBuilder symbol(Symbol<T> symbol) {
29 if (!allSymbols.add(symbol)) { 43 if (!allSymbols.add(symbol)) {
30 // No need to add symbol twice. 44 // No need to add symbol twice.
@@ -75,7 +89,8 @@ public class ModelStoreBuilderImpl implements ModelStoreBuilder {
75 for (var entry : equivalenceClasses.entrySet()) { 89 for (var entry : equivalenceClasses.entrySet()) {
76 createStores(stores, entry.getKey(), entry.getValue()); 90 createStores(stores, entry.getKey(), entry.getValue());
77 } 91 }
78 var modelStore = new ModelStoreImpl(stores, adapters.size()); 92 var modelStore = new ModelStoreImpl(stores, adapters.size(), cancellationToken == null ?
93 CancellationToken.NONE : cancellationToken);
79 for (var adapterBuilder : adapters) { 94 for (var adapterBuilder : adapters) {
80 var storeAdapter = adapterBuilder.build(modelStore); 95 var storeAdapter = adapterBuilder.build(modelStore);
81 modelStore.addAdapter(storeAdapter); 96 modelStore.addAdapter(storeAdapter);
@@ -86,8 +101,8 @@ public class ModelStoreBuilderImpl implements ModelStoreBuilder {
86 private <T> void createStores(Map<AnySymbol, VersionedMapStore<Tuple, ?>> stores, 101 private <T> void createStores(Map<AnySymbol, VersionedMapStore<Tuple, ?>> stores,
87 SymbolEquivalenceClass<T> equivalenceClass, List<AnySymbol> symbols) { 102 SymbolEquivalenceClass<T> equivalenceClass, List<AnySymbol> symbols) {
88 int size = symbols.size(); 103 int size = symbols.size();
89 VersionedMapStoreFactory<Tuple,T> mapFactory = VersionedMapStore 104 VersionedMapStoreFactory<Tuple, T> mapFactory = VersionedMapStore
90 .<Tuple,T>builder() 105 .<Tuple, T>builder()
91 .strategy(VersionedMapStoreFactoryBuilder.StoreStrategy.DELTA) 106 .strategy(VersionedMapStoreFactoryBuilder.StoreStrategy.DELTA)
92 .defaultValue(equivalenceClass.defaultValue()) 107 .defaultValue(equivalenceClass.defaultValue())
93 .build(); 108 .build();
diff --git a/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreImpl.java b/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreImpl.java
index a320a618..fd4cc160 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreImpl.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/model/internal/ModelStoreImpl.java
@@ -14,16 +14,20 @@ import tools.refinery.store.model.ModelDiffCursor;
14import tools.refinery.store.model.ModelStore; 14import tools.refinery.store.model.ModelStore;
15import tools.refinery.store.representation.AnySymbol; 15import tools.refinery.store.representation.AnySymbol;
16import tools.refinery.store.tuple.Tuple; 16import tools.refinery.store.tuple.Tuple;
17import tools.refinery.store.util.CancellationToken;
17 18
18import java.util.*; 19import java.util.*;
19 20
20public class ModelStoreImpl implements ModelStore { 21public class ModelStoreImpl implements ModelStore {
21 private final LinkedHashMap<? extends AnySymbol, ? extends VersionedMapStore<Tuple, ?>> stores; 22 private final LinkedHashMap<? extends AnySymbol, ? extends VersionedMapStore<Tuple, ?>> stores;
22 private final List<ModelStoreAdapter> adapters; 23 private final List<ModelStoreAdapter> adapters;
24 private final CancellationToken cancellationToken;
23 25
24 ModelStoreImpl(LinkedHashMap<? extends AnySymbol, ? extends VersionedMapStore<Tuple, ?>> stores, int adapterCount) { 26 ModelStoreImpl(LinkedHashMap<? extends AnySymbol, ? extends VersionedMapStore<Tuple, ?>> stores, int adapterCount,
27 CancellationToken cancellationToken) {
25 this.stores = stores; 28 this.stores = stores;
26 adapters = new ArrayList<>(adapterCount); 29 adapters = new ArrayList<>(adapterCount);
30 this.cancellationToken = cancellationToken;
27 } 31 }
28 32
29 @Override 33 @Override
@@ -100,4 +104,13 @@ public class ModelStoreImpl implements ModelStore {
100 void addAdapter(ModelStoreAdapter adapter) { 104 void addAdapter(ModelStoreAdapter adapter) {
101 adapters.add(adapter); 105 adapters.add(adapter);
102 } 106 }
107
108 @Override
109 public void checkCancelled() {
110 cancellationToken.checkCancelled();
111 }
112
113 CancellationToken getCancellationToken() {
114 return cancellationToken;
115 }
103} 116}
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 71df3962..dcf0ad08 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
@@ -75,6 +75,7 @@ public abstract class VersionedInterpretation<T> implements Interpretation<T> {
75 @Override 75 @Override
76 public T put(Tuple key, T value) { 76 public T put(Tuple key, T value) {
77 checkKey(key); 77 checkKey(key);
78 model.checkCancelled();
78 model.markAsChanged(); 79 model.markAsChanged();
79 var oldValue = map.put(key, value); 80 var oldValue = map.put(key, value);
80 valueChanged(key, oldValue, value, false); 81 valueChanged(key, oldValue, value, false);
@@ -83,15 +84,12 @@ public abstract class VersionedInterpretation<T> implements Interpretation<T> {
83 84
84 @Override 85 @Override
85 public void putAll(Cursor<Tuple, T> cursor) { 86 public void putAll(Cursor<Tuple, T> cursor) {
86 if (listeners.isEmpty()) {
87 map.putAll(cursor);
88 return;
89 }
90 model.markAsChanged(); 87 model.markAsChanged();
91 if (cursor.getDependingMaps().contains(map)) { 88 if (cursor.getDependingMaps().contains(map)) {
92 List<Tuple> keys = new ArrayList<>(); 89 List<Tuple> keys = new ArrayList<>();
93 List<T> values = new ArrayList<>(); 90 List<T> values = new ArrayList<>();
94 while (cursor.move()) { 91 while (cursor.move()) {
92 model.checkCancelled();
95 keys.add(cursor.getKey()); 93 keys.add(cursor.getKey());
96 values.add(cursor.getValue()); 94 values.add(cursor.getValue());
97 } 95 }
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCodeCalculatorFactory.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCodeCalculatorFactory.java
index 04e17a13..809205e4 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCodeCalculatorFactory.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/StateCodeCalculatorFactory.java
@@ -7,9 +7,10 @@ package tools.refinery.store.statecoding;
7 7
8import org.eclipse.collections.api.set.primitive.IntSet; 8import org.eclipse.collections.api.set.primitive.IntSet;
9import tools.refinery.store.model.Interpretation; 9import tools.refinery.store.model.Interpretation;
10import tools.refinery.store.model.Model;
10 11
11import java.util.List; 12import java.util.List;
12 13
13public interface StateCodeCalculatorFactory { 14public interface StateCodeCalculatorFactory {
14 StateCodeCalculator create(List<? extends Interpretation<?>> interpretations, IntSet individuals); 15 StateCodeCalculator create(Model model, List<? extends Interpretation<?>> interpretations, IntSet individuals);
15} 16}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderStoreAdapterImpl.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderStoreAdapterImpl.java
index 89586bfb..2cb6f3c1 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderStoreAdapterImpl.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderStoreAdapterImpl.java
@@ -68,7 +68,7 @@ public class StateCoderStoreAdapterImpl implements StateCoderStoreAdapter {
68 @Override 68 @Override
69 public StateCoderAdapter createModelAdapter(Model model) { 69 public StateCoderAdapter createModelAdapter(Model model) {
70 var interpretations = symbols.stream().map(model::getInterpretation).toList(); 70 var interpretations = symbols.stream().map(model::getInterpretation).toList();
71 var coder = codeCalculatorFactory.create(interpretations, individuals); 71 var coder = codeCalculatorFactory.create(model, interpretations, individuals);
72 return new StateCoderAdapterImpl(this, coder, model); 72 return new StateCoderAdapterImpl(this, coder, model);
73 } 73 }
74} 74}
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 5d390da2..4cef6786 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
@@ -10,6 +10,7 @@ import org.eclipse.collections.api.map.primitive.MutableIntLongMap;
10import org.eclipse.collections.api.set.primitive.IntSet; 10import org.eclipse.collections.api.set.primitive.IntSet;
11import tools.refinery.store.model.AnyInterpretation; 11import tools.refinery.store.model.AnyInterpretation;
12import tools.refinery.store.model.Interpretation; 12import tools.refinery.store.model.Interpretation;
13import tools.refinery.store.model.Model;
13import tools.refinery.store.statecoding.ObjectCode; 14import tools.refinery.store.statecoding.ObjectCode;
14import tools.refinery.store.tuple.Tuple; 15import tools.refinery.store.tuple.Tuple;
15import tools.refinery.store.tuple.Tuple0; 16import tools.refinery.store.tuple.Tuple0;
@@ -17,13 +18,16 @@ import tools.refinery.store.tuple.Tuple0;
17import java.util.*; 18import java.util.*;
18 19
19public abstract class AbstractNeighbourhoodCalculator { 20public abstract class AbstractNeighbourhoodCalculator {
21 protected final Model model;
20 protected final List<AnyInterpretation> nullImpactValues; 22 protected final List<AnyInterpretation> nullImpactValues;
21 protected final LinkedHashMap<AnyInterpretation, long[]> impactValues; 23 protected final LinkedHashMap<AnyInterpretation, long[]> impactValues;
22 protected final MutableIntLongMap individualHashValues = IntLongMaps.mutable.empty(); 24 protected final MutableIntLongMap individualHashValues = IntLongMaps.mutable.empty();
23 25
24 protected static final long PRIME = 31; 26 protected static final long PRIME = 31;
25 27
26 protected AbstractNeighbourhoodCalculator(List<? extends AnyInterpretation> interpretations, IntSet individuals) { 28 protected AbstractNeighbourhoodCalculator(Model model, List<? extends AnyInterpretation> interpretations,
29 IntSet individuals) {
30 this.model = model;
27 this.nullImpactValues = new ArrayList<>(); 31 this.nullImpactValues = new ArrayList<>();
28 this.impactValues = new LinkedHashMap<>(); 32 this.impactValues = new LinkedHashMap<>();
29 // Random isn't used for cryptographical purposes but just to assign distinguishable identifiers to symbols. 33 // Random isn't used for cryptographical purposes but just to assign distinguishable identifiers to symbols.
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 c188a839..04335141 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
@@ -12,6 +12,7 @@ import org.eclipse.collections.api.set.primitive.IntSet;
12import tools.refinery.store.map.Cursor; 12import tools.refinery.store.map.Cursor;
13import tools.refinery.store.model.AnyInterpretation; 13import tools.refinery.store.model.AnyInterpretation;
14import tools.refinery.store.model.Interpretation; 14import tools.refinery.store.model.Interpretation;
15import tools.refinery.store.model.Model;
15import tools.refinery.store.statecoding.StateCodeCalculator; 16import tools.refinery.store.statecoding.StateCodeCalculator;
16import tools.refinery.store.statecoding.StateCoderResult; 17import tools.refinery.store.statecoding.StateCoderResult;
17import tools.refinery.store.tuple.Tuple; 18import tools.refinery.store.tuple.Tuple;
@@ -19,8 +20,9 @@ import tools.refinery.store.tuple.Tuple;
19import java.util.List; 20import java.util.List;
20 21
21public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator implements StateCodeCalculator { 22public class LazyNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator implements StateCodeCalculator {
22 public LazyNeighbourhoodCalculator(List<? extends AnyInterpretation> interpretations, IntSet individuals) { 23 public LazyNeighbourhoodCalculator(Model model, List<? extends AnyInterpretation> interpretations,
23 super(interpretations, individuals); 24 IntSet individuals) {
25 super(model, interpretations, individuals);
24 } 26 }
25 27
26 public StateCoderResult calculateCodes() { 28 public StateCoderResult calculateCodes() {
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 1442c915..5e6de53b 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
@@ -8,6 +8,7 @@ package tools.refinery.store.statecoding.neighbourhood;
8import org.eclipse.collections.api.set.primitive.IntSet; 8import org.eclipse.collections.api.set.primitive.IntSet;
9import tools.refinery.store.map.Cursor; 9import tools.refinery.store.map.Cursor;
10import tools.refinery.store.model.Interpretation; 10import tools.refinery.store.model.Interpretation;
11import tools.refinery.store.model.Model;
11import tools.refinery.store.statecoding.ObjectCode; 12import tools.refinery.store.statecoding.ObjectCode;
12import tools.refinery.store.statecoding.StateCodeCalculator; 13import tools.refinery.store.statecoding.StateCodeCalculator;
13import tools.refinery.store.statecoding.StateCoderResult; 14import tools.refinery.store.statecoding.StateCoderResult;
@@ -21,17 +22,19 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp
21 private ObjectCodeImpl previousObjectCode = new ObjectCodeImpl(); 22 private ObjectCodeImpl previousObjectCode = new ObjectCodeImpl();
22 private ObjectCodeImpl nextObjectCode = new ObjectCodeImpl(); 23 private ObjectCodeImpl nextObjectCode = new ObjectCodeImpl();
23 24
24 public NeighbourhoodCalculator(List<? extends Interpretation<?>> interpretations, IntSet individuals) { 25 public NeighbourhoodCalculator(Model model, List<? extends Interpretation<?>> interpretations, IntSet individuals) {
25 super(interpretations, individuals); 26 super(model, interpretations, individuals);
26 } 27 }
27 28
28 public StateCoderResult calculateCodes() { 29 public StateCoderResult calculateCodes() {
30 model.checkCancelled();
29 previousObjectCode.clear(); 31 previousObjectCode.clear();
30 nextObjectCode.clear(); 32 nextObjectCode.clear();
31 initializeWithIndividuals(previousObjectCode); 33 initializeWithIndividuals(previousObjectCode);
32 34
33 int rounds = 0; 35 int rounds = 0;
34 do { 36 do {
37 model.checkCancelled();
35 constructNextObjectCodes(previousObjectCode, nextObjectCode); 38 constructNextObjectCodes(previousObjectCode, nextObjectCode);
36 var tempObjectCode = previousObjectCode; 39 var tempObjectCode = previousObjectCode;
37 previousObjectCode = nextObjectCode; 40 previousObjectCode = nextObjectCode;
@@ -60,6 +63,7 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator imp
60 63
61 private void constructNextObjectCodes(ObjectCodeImpl previous, ObjectCodeImpl next) { 64 private void constructNextObjectCodes(ObjectCodeImpl previous, ObjectCodeImpl next) {
62 for (var impactValueEntry : this.impactValues.entrySet()) { 65 for (var impactValueEntry : this.impactValues.entrySet()) {
66 model.checkCancelled();
63 Interpretation<?> interpretation = (Interpretation<?>) impactValueEntry.getKey(); 67 Interpretation<?> interpretation = (Interpretation<?>) impactValueEntry.getKey();
64 var cursor = interpretation.getAll(); 68 var cursor = interpretation.getAll();
65 int arity = interpretation.getSymbol().arity(); 69 int arity = interpretation.getSymbol().arity();
diff --git a/subprojects/store/src/main/java/tools/refinery/store/util/CancellationToken.java b/subprojects/store/src/main/java/tools/refinery/store/util/CancellationToken.java
new file mode 100644
index 00000000..be294013
--- /dev/null
+++ b/subprojects/store/src/main/java/tools/refinery/store/util/CancellationToken.java
@@ -0,0 +1,13 @@
1/*
2 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.util;
7
8@FunctionalInterface
9public interface CancellationToken {
10 CancellationToken NONE = () -> {};
11
12 void checkCancelled();
13}
diff --git a/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java b/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java
index 3c35849e..f2d2f7b5 100644
--- a/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java
+++ b/subprojects/store/src/test/java/tools/refinery/store/statecoding/EquivalenceTest.java
@@ -192,7 +192,8 @@ class EquivalenceTest {
192 ModelStore store = ModelStore.builder() 192 ModelStore store = ModelStore.builder()
193 .symbols(person, age, friend, parents, population) 193 .symbols(person, age, friend, parents, population)
194 .with(StateCoderAdapter.builder() 194 .with(StateCoderAdapter.builder()
195 .stateCodeCalculatorFactory((p1, p2) -> calculator)) 195 .stateCodeCalculatorFactory((ignoredModel, ignoredInterpretations, ignoredIndividuals) ->
196 calculator))
196 .build(); 197 .build();
197 198
198 var stateCoder = store.getAdapter(StateCoderStoreAdapter.class); 199 var stateCoder = store.getAdapter(StateCoderStoreAdapter.class);
diff --git a/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderBuildTest.java b/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderBuildTest.java
index 0b738005..0928aa8e 100644
--- a/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderBuildTest.java
+++ b/subprojects/store/src/test/java/tools/refinery/store/statecoding/StateCoderBuildTest.java
@@ -124,7 +124,8 @@ class StateCoderBuildTest {
124 var store = ModelStore.builder() 124 var store = ModelStore.builder()
125 .symbols(friend) 125 .symbols(friend)
126 .with(StateCoderAdapter.builder() 126 .with(StateCoderAdapter.builder()
127 .stateCodeCalculatorFactory((interpretations, individuals) -> mock)) 127 .stateCodeCalculatorFactory((ignoredModel, ignoredInterpretations, ignoredIndividuals) ->
128 mock))
128 .build(); 129 .build();
129 130
130 var model = store.createEmptyModel(); 131 var model = store.createEmptyModel();