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 | 92 |
1 files changed, 85 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..9ef6fb16 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,46 @@ | |||
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; | 8 | import tools.refinery.store.dse.propagation.PropagationAdapter; |
9 | import tools.refinery.store.model.Model; | 9 | import tools.refinery.store.model.Model; |
10 | import tools.refinery.store.model.ModelStore; | 10 | import tools.refinery.store.model.ModelStore; |
11 | import tools.refinery.store.query.ModelQueryAdapter; | ||
12 | import tools.refinery.store.reasoning.ReasoningStoreAdapter; | ||
13 | import tools.refinery.store.reasoning.interpretation.PartialInterpretation; | ||
14 | import tools.refinery.store.reasoning.literal.Concreteness; | ||
15 | import tools.refinery.store.reasoning.refinement.PartialInterpretationRefiner; | ||
16 | import tools.refinery.store.reasoning.refinement.PartialModelInitializer; | ||
17 | import tools.refinery.store.reasoning.refinement.StorageRefiner; | ||
11 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; | 18 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; |
12 | import tools.refinery.store.query.dnf.Dnf; | 19 | import tools.refinery.store.reasoning.seed.ModelSeed; |
20 | import tools.refinery.store.representation.AnySymbol; | ||
21 | import tools.refinery.store.representation.Symbol; | ||
22 | import tools.refinery.store.tuple.Tuple; | ||
13 | 23 | ||
14 | import java.util.Collection; | 24 | import java.util.Collection; |
25 | import java.util.List; | ||
26 | import java.util.Map; | ||
27 | import java.util.Set; | ||
15 | 28 | ||
16 | public class ReasoningStoreAdapterImpl implements ReasoningStoreAdapter { | 29 | class ReasoningStoreAdapterImpl implements ReasoningStoreAdapter { |
17 | private final ModelStore store; | 30 | private final ModelStore store; |
31 | private final Set<Concreteness> supportedInterpretations; | ||
32 | private final Map<AnyPartialSymbol, PartialInterpretation.Factory<?, ?>> symbolInterpreters; | ||
33 | private final Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> symbolRefiners; | ||
34 | private final Map<AnySymbol, StorageRefiner.Factory<?>> storageRefiners; | ||
35 | private final List<PartialModelInitializer> initializers; | ||
18 | 36 | ||
19 | ReasoningStoreAdapterImpl(ModelStore store) { | 37 | ReasoningStoreAdapterImpl(ModelStore store, Set<Concreteness> supportedInterpretations, |
38 | Map<AnyPartialSymbol, PartialInterpretation.Factory<?, ?>> symbolInterpreters, | ||
39 | Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> symbolRefiners, | ||
40 | Map<AnySymbol, StorageRefiner.Factory<?>> storageRefiners, | ||
41 | List<PartialModelInitializer> initializers) { | ||
20 | this.store = store; | 42 | this.store = store; |
43 | this.supportedInterpretations = supportedInterpretations; | ||
44 | this.symbolInterpreters = symbolInterpreters; | ||
45 | this.symbolRefiners = symbolRefiners; | ||
46 | this.storageRefiners = storageRefiners; | ||
47 | this.initializers = initializers; | ||
21 | } | 48 | } |
22 | 49 | ||
23 | @Override | 50 | @Override |
@@ -26,13 +53,64 @@ public class ReasoningStoreAdapterImpl implements ReasoningStoreAdapter { | |||
26 | } | 53 | } |
27 | 54 | ||
28 | @Override | 55 | @Override |
56 | public Set<Concreteness> getSupportedInterpretations() { | ||
57 | return supportedInterpretations; | ||
58 | } | ||
59 | |||
60 | @Override | ||
29 | public Collection<AnyPartialSymbol> getPartialSymbols() { | 61 | public Collection<AnyPartialSymbol> getPartialSymbols() { |
30 | return null; | 62 | return symbolInterpreters.keySet(); |
31 | } | 63 | } |
32 | 64 | ||
33 | @Override | 65 | @Override |
34 | public Collection<Dnf> getLiftedQueries() { | 66 | public Collection<AnyPartialSymbol> getRefinablePartialSymbols() { |
35 | return null; | 67 | return symbolRefiners.keySet(); |
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, PartialInterpretation.Factory<?, ?>> getSymbolInterpreters() { | ||
73 | return symbolInterpreters; | ||
74 | } | ||
75 | |||
76 | // Use of wildcard return value only in internal method not exposed as API, so there is less chance of confusion. | ||
77 | @SuppressWarnings("squid:S1452") | ||
78 | Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> getSymbolRefiners() { | ||
79 | return symbolRefiners; | ||
80 | } | ||
81 | |||
82 | StorageRefiner[] createStorageRefiner(Model model) { | ||
83 | var refiners = new StorageRefiner[storageRefiners.size()]; | ||
84 | int i = 0; | ||
85 | for (var entry : storageRefiners.entrySet()) { | ||
86 | var symbol = entry.getKey(); | ||
87 | var factory = entry.getValue(); | ||
88 | refiners[i] = createStorageRefiner(factory, model, symbol); | ||
89 | i++; | ||
90 | } | ||
91 | return refiners; | ||
92 | } | ||
93 | |||
94 | private <T> StorageRefiner createStorageRefiner(StorageRefiner.Factory<T> factory, Model model, AnySymbol symbol) { | ||
95 | // The builder only allows well-typed assignment of refiners to symbols. | ||
96 | @SuppressWarnings("unchecked") | ||
97 | var typedSymbol = (Symbol<T>) symbol; | ||
98 | return factory.create(typedSymbol, model); | ||
99 | } | ||
100 | |||
101 | public Model createInitialModel(ModelSeed modelSeed) { | ||
102 | var model = store.createEmptyModel(); | ||
103 | model.getInterpretation(ReasoningAdapterImpl.NODE_COUNT_SYMBOL).put(Tuple.of(), modelSeed.getNodeCount()); | ||
104 | for (var initializer : initializers) { | ||
105 | initializer.initialize(model, modelSeed); | ||
106 | } | ||
107 | model.tryGetAdapter(PropagationAdapter.class).ifPresent(propagationAdapter -> { | ||
108 | if (propagationAdapter.propagate().isRejected()) { | ||
109 | throw new IllegalArgumentException("Inconsistent initial mode: propagation failed"); | ||
110 | } | ||
111 | }); | ||
112 | model.getAdapter(ModelQueryAdapter.class).flushChanges(); | ||
113 | return model; | ||
36 | } | 114 | } |
37 | 115 | ||
38 | @Override | 116 | @Override |