diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningStoreAdapterImpl.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningStoreAdapterImpl.java | 93 |
1 files changed, 86 insertions, 7 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningStoreAdapterImpl.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningStoreAdapterImpl.java index cdddd8d6..e8b581c6 100644 --- a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningStoreAdapterImpl.java +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningStoreAdapterImpl.java | |||
@@ -5,19 +5,45 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.reasoning.internal; | 6 | package tools.refinery.store.reasoning.internal; |
7 | 7 | ||
8 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | ||
9 | import tools.refinery.store.model.Model; | 8 | import tools.refinery.store.model.Model; |
10 | import tools.refinery.store.model.ModelStore; | 9 | import tools.refinery.store.model.ModelStore; |
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | ||
12 | import tools.refinery.store.reasoning.interpretation.PartialInterpretation; | ||
13 | import tools.refinery.store.reasoning.refinement.PartialInterpretationRefiner; | ||
14 | import tools.refinery.store.reasoning.refinement.PartialModelInitializer; | ||
15 | import tools.refinery.store.reasoning.refinement.StorageRefiner; | ||
11 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; | 16 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; |
12 | import tools.refinery.store.query.dnf.Dnf; | 17 | import tools.refinery.store.representation.AnySymbol; |
18 | import tools.refinery.store.representation.Symbol; | ||
19 | import tools.refinery.store.tuple.Tuple; | ||
13 | 20 | ||
14 | import java.util.Collection; | 21 | import java.util.Collection; |
22 | import java.util.List; | ||
23 | import java.util.Map; | ||
15 | 24 | ||
16 | public class ReasoningStoreAdapterImpl implements ReasoningStoreAdapter { | 25 | class ReasoningStoreAdapterImpl implements ReasoningStoreAdapter { |
17 | private final ModelStore store; | 26 | private final ModelStore store; |
27 | private final Map<AnyPartialSymbol, PartialInterpretation.Factory<?, ?>> symbolInterpreters; | ||
28 | private final Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> symbolRefiners; | ||
29 | private final Map<AnySymbol, StorageRefiner.Factory<?>> representationRefiners; | ||
30 | private final Object initialModelLock = new Object(); | ||
31 | private final int initialNodeCount; | ||
32 | private List<PartialModelInitializer> initializers; | ||
33 | private long initialCommitId = Model.NO_STATE_ID; | ||
18 | 34 | ||
19 | ReasoningStoreAdapterImpl(ModelStore store) { | 35 | ReasoningStoreAdapterImpl(ModelStore store, |
36 | int initialNodeCount, | ||
37 | Map<AnyPartialSymbol, PartialInterpretation.Factory<?, ?>> symbolInterpreters, | ||
38 | Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> symbolRefiners, | ||
39 | Map<AnySymbol, StorageRefiner.Factory<?>> representationRefiners, | ||
40 | List<PartialModelInitializer> initializers) { | ||
20 | this.store = store; | 41 | this.store = store; |
42 | this.initialNodeCount = initialNodeCount; | ||
43 | this.symbolInterpreters = symbolInterpreters; | ||
44 | this.symbolRefiners = symbolRefiners; | ||
45 | this.representationRefiners = representationRefiners; | ||
46 | this.initializers = initializers; | ||
21 | } | 47 | } |
22 | 48 | ||
23 | @Override | 49 | @Override |
@@ -27,12 +53,65 @@ public class ReasoningStoreAdapterImpl implements ReasoningStoreAdapter { | |||
27 | 53 | ||
28 | @Override | 54 | @Override |
29 | public Collection<AnyPartialSymbol> getPartialSymbols() { | 55 | public Collection<AnyPartialSymbol> getPartialSymbols() { |
30 | return null; | 56 | return symbolInterpreters.keySet(); |
31 | } | 57 | } |
32 | 58 | ||
33 | @Override | 59 | @Override |
34 | public Collection<Dnf> getLiftedQueries() { | 60 | public Collection<AnyPartialSymbol> getRefinablePartialSymbols() { |
35 | return null; | 61 | return symbolRefiners.keySet(); |
62 | } | ||
63 | |||
64 | // Use of wildcard return value only in internal method not exposed as API, so there is less chance of confusion. | ||
65 | @SuppressWarnings("squid:S1452") | ||
66 | Map<AnyPartialSymbol, PartialInterpretation.Factory<?, ?>> getSymbolInterpreters() { | ||
67 | return symbolInterpreters; | ||
68 | } | ||
69 | |||
70 | // Use of wildcard return value only in internal method not exposed as API, so there is less chance of confusion. | ||
71 | @SuppressWarnings("squid:S1452") | ||
72 | Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> getSymbolRefiners() { | ||
73 | return symbolRefiners; | ||
74 | } | ||
75 | |||
76 | StorageRefiner[] createRepresentationRefiners(Model model) { | ||
77 | var refiners = new StorageRefiner[representationRefiners.size()]; | ||
78 | int i = 0; | ||
79 | for (var entry : representationRefiners.entrySet()) { | ||
80 | var symbol = entry.getKey(); | ||
81 | var factory = entry.getValue(); | ||
82 | refiners[i] = createRepresentationRefiner(factory, model, symbol); | ||
83 | } | ||
84 | return refiners; | ||
85 | } | ||
86 | |||
87 | private <T> StorageRefiner createRepresentationRefiner( | ||
88 | StorageRefiner.Factory<T> factory, Model model, AnySymbol symbol) { | ||
89 | // The builder only allows well-typed assignment of refiners to symbols. | ||
90 | @SuppressWarnings("unchecked") | ||
91 | var typedSymbol = (Symbol<T>) symbol; | ||
92 | return factory.create(typedSymbol, model); | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public Model createInitialModel() { | ||
97 | synchronized (initialModelLock) { | ||
98 | if (initialCommitId == Model.NO_STATE_ID) { | ||
99 | return doCreateInitialModel(); | ||
100 | } | ||
101 | return store.createModelForState(initialCommitId); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | private Model doCreateInitialModel() { | ||
106 | var model = store.createEmptyModel(); | ||
107 | model.getInterpretation(ReasoningAdapterImpl.NODE_COUNT_SYMBOL).put(Tuple.of(), initialNodeCount); | ||
108 | for (var initializer : initializers) { | ||
109 | initializer.initialize(model, initialNodeCount); | ||
110 | } | ||
111 | model.getAdapter(ModelQueryAdapter.class).flushChanges(); | ||
112 | initialCommitId = model.commit(); | ||
113 | initializers = null; | ||
114 | return model; | ||
36 | } | 115 | } |
37 | 116 | ||
38 | @Override | 117 | @Override |