diff options
Diffstat (limited to 'subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningBuilderImpl.java')
-rw-r--r-- | subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningBuilderImpl.java | 153 |
1 files changed, 148 insertions, 5 deletions
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningBuilderImpl.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningBuilderImpl.java index aa71496c..722458c8 100644 --- a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningBuilderImpl.java +++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/internal/ReasoningBuilderImpl.java | |||
@@ -6,26 +6,169 @@ | |||
6 | package tools.refinery.store.reasoning.internal; | 6 | package tools.refinery.store.reasoning.internal; |
7 | 7 | ||
8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; | 8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; |
9 | import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder; | ||
10 | import tools.refinery.store.dse.transition.objectives.Objective; | ||
11 | import tools.refinery.store.dse.transition.objectives.Objectives; | ||
9 | import tools.refinery.store.model.ModelStore; | 12 | import tools.refinery.store.model.ModelStore; |
13 | import tools.refinery.store.model.ModelStoreBuilder; | ||
14 | import tools.refinery.store.query.ModelQueryBuilder; | ||
10 | import tools.refinery.store.query.dnf.Dnf; | 15 | import tools.refinery.store.query.dnf.Dnf; |
16 | import tools.refinery.store.query.dnf.FunctionalQuery; | ||
17 | import tools.refinery.store.query.dnf.Query; | ||
18 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
11 | import tools.refinery.store.reasoning.ReasoningBuilder; | 19 | import tools.refinery.store.reasoning.ReasoningBuilder; |
20 | import tools.refinery.store.reasoning.interpretation.PartialInterpretation; | ||
21 | import tools.refinery.store.reasoning.lifting.DnfLifter; | ||
22 | import tools.refinery.store.reasoning.literal.Concreteness; | ||
12 | import tools.refinery.store.reasoning.literal.Modality; | 23 | import tools.refinery.store.reasoning.literal.Modality; |
24 | import tools.refinery.store.reasoning.refinement.DefaultStorageRefiner; | ||
25 | import tools.refinery.store.reasoning.refinement.PartialInterpretationRefiner; | ||
26 | import tools.refinery.store.reasoning.refinement.PartialModelInitializer; | ||
27 | import tools.refinery.store.reasoning.refinement.StorageRefiner; | ||
28 | import tools.refinery.store.reasoning.representation.AnyPartialSymbol; | ||
29 | import tools.refinery.store.reasoning.translator.AnyPartialSymbolTranslator; | ||
30 | import tools.refinery.store.reasoning.translator.PartialRelationTranslator; | ||
31 | import tools.refinery.store.representation.AnySymbol; | ||
32 | import tools.refinery.store.representation.Symbol; | ||
33 | import tools.refinery.store.statecoding.StateCoderBuilder; | ||
34 | |||
35 | import java.util.*; | ||
13 | 36 | ||
14 | public class ReasoningBuilderImpl extends AbstractModelAdapterBuilder<ReasoningStoreAdapterImpl> | 37 | public class ReasoningBuilderImpl extends AbstractModelAdapterBuilder<ReasoningStoreAdapterImpl> |
15 | implements ReasoningBuilder { | 38 | implements ReasoningBuilder { |
39 | private final DnfLifter lifter = new DnfLifter(); | ||
40 | private final PartialQueryRewriter queryRewriter = new PartialQueryRewriter(lifter); | ||
41 | private Set<Concreteness> requiredInterpretations = Set.of(Concreteness.values()); | ||
42 | private final Map<AnyPartialSymbol, AnyPartialSymbolTranslator> translators = new LinkedHashMap<>(); | ||
43 | private final Map<AnyPartialSymbol, PartialInterpretation.Factory<?, ?>> symbolInterpreters = | ||
44 | new LinkedHashMap<>(); | ||
45 | private final Map<AnyPartialSymbol, PartialInterpretationRefiner.Factory<?, ?>> symbolRefiners = | ||
46 | new LinkedHashMap<>(); | ||
47 | private final Map<AnySymbol, StorageRefiner.Factory<?>> registeredStorageRefiners = new LinkedHashMap<>(); | ||
48 | private final List<PartialModelInitializer> initializers = new ArrayList<>(); | ||
49 | private final List<Objective> objectives = new ArrayList<>(); | ||
50 | |||
51 | @Override | ||
52 | public ReasoningBuilder requiredInterpretations(Collection<Concreteness> requiredInterpretations) { | ||
53 | this.requiredInterpretations = Set.copyOf(requiredInterpretations); | ||
54 | return this; | ||
55 | } | ||
56 | |||
57 | @Override | ||
58 | public ReasoningBuilder partialSymbol(AnyPartialSymbolTranslator translator) { | ||
59 | var partialSymbol = translator.getPartialSymbol(); | ||
60 | var oldConfiguration = translators.put(partialSymbol, translator); | ||
61 | if (oldConfiguration != null && oldConfiguration != translator) { | ||
62 | throw new IllegalArgumentException("Duplicate configuration for symbol: " + partialSymbol); | ||
63 | } | ||
64 | return this; | ||
65 | } | ||
66 | |||
16 | @Override | 67 | @Override |
17 | public ReasoningBuilder liftedQuery(Dnf liftedQuery) { | 68 | public <T> ReasoningBuilder storageRefiner(Symbol<T> symbol, StorageRefiner.Factory<T> refiner) { |
18 | return null; | 69 | checkNotConfigured(); |
70 | if (registeredStorageRefiners.put(symbol, refiner) != null) { | ||
71 | throw new IllegalArgumentException("Duplicate representation refiner for symbol: " + symbol); | ||
72 | } | ||
73 | return this; | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public ReasoningBuilder initializer(PartialModelInitializer initializer) { | ||
78 | checkNotConfigured(); | ||
79 | initializers.add(initializer); | ||
80 | return this; | ||
19 | } | 81 | } |
20 | 82 | ||
21 | @Override | 83 | @Override |
22 | public Dnf lift(Modality modality, Dnf query) { | 84 | public ReasoningBuilder objective(Objective objective) { |
23 | checkNotConfigured(); | 85 | checkNotConfigured(); |
24 | return null; | 86 | objectives.add(objective); |
87 | return this; | ||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public <T> Query<T> lift(Modality modality, Concreteness concreteness, Query<T> query) { | ||
92 | return lifter.lift(modality, concreteness, query); | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public RelationalQuery lift(Modality modality, Concreteness concreteness, RelationalQuery query) { | ||
97 | return lifter.lift(modality, concreteness, query); | ||
98 | } | ||
99 | |||
100 | @Override | ||
101 | public <T> FunctionalQuery<T> lift(Modality modality, Concreteness concreteness, FunctionalQuery<T> query) { | ||
102 | return lifter.lift(modality, concreteness, query); | ||
103 | } | ||
104 | |||
105 | @Override | ||
106 | public Dnf lift(Modality modality, Concreteness concreteness, Dnf dnf) { | ||
107 | return lifter.lift(modality, concreteness, dnf); | ||
108 | } | ||
109 | |||
110 | @Override | ||
111 | protected void doConfigure(ModelStoreBuilder storeBuilder) { | ||
112 | storeBuilder.symbols(ReasoningAdapterImpl.NODE_COUNT_SYMBOL); | ||
113 | storeBuilder.tryGetAdapter(StateCoderBuilder.class) | ||
114 | .ifPresent(stateCoderBuilder -> stateCoderBuilder.exclude(ReasoningAdapterImpl.NODE_COUNT_SYMBOL)); | ||
115 | for (var translator : translators.values()) { | ||
116 | translator.configure(storeBuilder); | ||
117 | if (translator instanceof PartialRelationTranslator relationConfiguration) { | ||
118 | doConfigure(storeBuilder, relationConfiguration); | ||
119 | } else { | ||
120 | throw new IllegalArgumentException("Unknown partial symbol translator %s for partial symbol %s" | ||
121 | .formatted(translator, translator.getPartialSymbol())); | ||
122 | } | ||
123 | } | ||
124 | storeBuilder.symbols(registeredStorageRefiners.keySet()); | ||
125 | var queryBuilder = storeBuilder.getAdapter(ModelQueryBuilder.class); | ||
126 | queryBuilder.rewriter(queryRewriter); | ||
127 | if (!objectives.isEmpty()) { | ||
128 | storeBuilder.tryGetAdapter(DesignSpaceExplorationBuilder.class) | ||
129 | .ifPresent(dseBuilder -> dseBuilder.objective(Objectives.sum(objectives))); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | private void doConfigure(ModelStoreBuilder storeBuilder, PartialRelationTranslator relationConfiguration) { | ||
134 | var partialRelation = relationConfiguration.getPartialRelation(); | ||
135 | queryRewriter.addRelationRewriter(partialRelation, relationConfiguration.getRewriter()); | ||
136 | var interpretationFactory = relationConfiguration.getInterpretationFactory(); | ||
137 | interpretationFactory.configure(storeBuilder, requiredInterpretations); | ||
138 | symbolInterpreters.put(partialRelation, interpretationFactory); | ||
139 | var refiner = relationConfiguration.getInterpretationRefiner(); | ||
140 | if (refiner != null) { | ||
141 | symbolRefiners.put(partialRelation, refiner); | ||
142 | } | ||
25 | } | 143 | } |
26 | 144 | ||
27 | @Override | 145 | @Override |
28 | public ReasoningStoreAdapterImpl doBuild(ModelStore store) { | 146 | public ReasoningStoreAdapterImpl doBuild(ModelStore store) { |
29 | return null; | 147 | return new ReasoningStoreAdapterImpl(store, requiredInterpretations, |
148 | Collections.unmodifiableMap(symbolInterpreters), Collections.unmodifiableMap(symbolRefiners), | ||
149 | getStorageRefiners(store), Collections.unmodifiableList(initializers)); | ||
150 | } | ||
151 | |||
152 | private Map<AnySymbol, StorageRefiner.Factory<?>> getStorageRefiners(ModelStore store) { | ||
153 | var symbols = store.getSymbols(); | ||
154 | var storageRefiners = new LinkedHashMap<AnySymbol, StorageRefiner.Factory<?>>(symbols.size()); | ||
155 | for (var symbol : symbols) { | ||
156 | var refiner = registeredStorageRefiners.remove(symbol); | ||
157 | if (refiner == null) { | ||
158 | if (symbol.arity() == 0) { | ||
159 | // Arity-0 symbols don't need a default refiner, because they are unaffected by object | ||
160 | // creation/removal. Only a custom refiner ({@code refiner != null}) might need to update them. | ||
161 | continue; | ||
162 | } | ||
163 | // By default, copy over all affected tuples on object creation and remove all affected tuples on | ||
164 | // object removal. | ||
165 | refiner = DefaultStorageRefiner.factory(); | ||
166 | } | ||
167 | storageRefiners.put(symbol, refiner); | ||
168 | } | ||
169 | if (!registeredStorageRefiners.isEmpty()) { | ||
170 | throw new IllegalArgumentException("Unused storage refiners: " + registeredStorageRefiners.keySet()); | ||
171 | } | ||
172 | return Collections.unmodifiableMap(storageRefiners); | ||
30 | } | 173 | } |
31 | } | 174 | } |