-
Notifications
You must be signed in to change notification settings - Fork 188
/
StatementContextImpl.java
361 lines (288 loc) · 11.1 KB
/
StatementContextImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.evolveum.axiom.lang.impl;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.concepts.Optionals;
import com.evolveum.axiom.lang.api.AxiomIdentifierDefinition.Scope;
import com.evolveum.axiom.lang.api.AxiomItemDefinition;
import com.evolveum.axiom.lang.api.AxiomTypeDefinition;
import com.evolveum.axiom.lang.api.IdentifierSpaceKey;
import com.evolveum.axiom.lang.spi.AxiomSemanticException;
import com.evolveum.axiom.lang.spi.AxiomStatement;
import com.evolveum.axiom.lang.spi.AxiomStatementBuilder;
import com.evolveum.axiom.lang.spi.SourceLocation;
import com.evolveum.axiom.lang.spi.AxiomStatementImpl.Factory;
import com.evolveum.axiom.reactor.Requirement;
public abstract class StatementContextImpl<V> implements StatementContext<V>, StatementTreeBuilder, IdentifierSpaceHolder {
private final AxiomItemDefinition definition;
private final IdentifierSpaceHolder localSpace;
private SourceLocation startLocation;
private SourceLocation endLocation;
private SourceLocation valueLocation;
private Requirement<AxiomStatement<V>> result;
StatementContextImpl(AxiomItemDefinition definition, SourceLocation loc, Scope scope, Scope... rest) {
this.definition = definition;
this.startLocation = loc;
this.localSpace = new IdentifierSpaceHolderImpl(scope, rest);
}
protected void initResult() {
AxiomStatementBuilder<V> builder = new AxiomStatementBuilder<>(definition.name(), typeFactory(definition.type()));
this.result = new StatementContextResult<>(definition, builder);
}
protected <T> Factory<T, ? extends AxiomStatement<T>> typeFactory(AxiomTypeDefinition type) {
return parent().typeFactory(type);
}
@Override
public void setValue(Object value) {
mutableResult().setValue((V) value);
}
private StatementContextResult<V> mutableResult() {
if(result instanceof StatementContextResult) {
return (StatementContextResult) result;
}
return null;
}
boolean isChildAllowed(AxiomIdentifier child) {
return definition.type().item(child).isPresent();
}
@SuppressWarnings("rawtypes")
@Override
public StatementContextImpl createChildNode(AxiomIdentifier identifier, SourceLocation loc) {
StatementContextImpl<V> childCtx = new StatementContextImpl.Child<V>(this, childDef(identifier).get(), loc);
mutableResult().add(childCtx);
return childCtx;
}
public IdentifierSpaceHolder localSpace() {
return localSpace;
}
@Override
public String toString() {
return "Context(" + definition.name() + ")";
}
@Override
public Optional<AxiomItemDefinition> childDef(AxiomIdentifier child) {
return definition.type().item(child);
}
@Override
public AxiomIdentifier identifier() {
return definition.name();
}
@Override
public V requireValue() throws AxiomSemanticException {
return AxiomSemanticException.checkNotNull(mutableResult().value(), definition, "must have argument specified.");
}
@Override
public AxiomItemDefinition definition() {
return definition;
}
@Override
public <V> StatementContext<V> createEffectiveChild(AxiomIdentifier axiomIdentifier, V value) {
StatementContextImpl<V> child = createChildNode(axiomIdentifier, null);
child.setValue(value);
return child;
}
public void registerRule(StatementRuleContextImpl<V> rule) {
mutableResult().addRule(rule);
addOutstanding(rule);
}
protected void addOutstanding(StatementRuleContextImpl<?> rule) {
parent().addOutstanding(rule);
}
public SourceLocation startLocation() {
return startLocation;
}
public <V> Optional<StatementContextImpl<V>> firstChild(AxiomItemDefinition child) {
return Optionals.first(children(child.name()));
}
private <V> Collection<StatementContextImpl<V>> children(AxiomIdentifier identifier) {
return (Collection) mutableResult().get(identifier);
}
public void addRule(StatementRule<V> statementRule) throws AxiomSemanticException {
statementRule.apply(new StatementRuleContextImpl<V>(this,statementRule));
}
@Override
public Optional<V> optionalValue() {
return Optional.ofNullable(mutableResult().value());
}
@Override
public void replace(Requirement<AxiomStatement<?>> supplier) {
this.result = (Requirement) supplier;
}
@Override
abstract public StatementContextImpl<?> parent();
@Override
public void setValue(Object value, SourceLocation loc) {
setValue(value);
this.valueLocation = loc;
}
public Requirement<AxiomStatement<V>> asRequirement() {
if (result instanceof StatementContextResult) {
return Requirement.deffered(result);
}
return result;
}
public Supplier<? extends AxiomStatement<?>> asLazy() {
return Lazy.from(() -> result.get());
}
public boolean isSatisfied() {
return result.isSatisfied();
}
@Override
public void register(AxiomIdentifier space, Scope scope, IdentifierSpaceKey key) {
register(space, scope, key, this);
}
@Override
public void register(AxiomIdentifier space, Scope scope, IdentifierSpaceKey key, StatementContextImpl<?> context) {
switch (scope) {
case GLOBAL:
parentNs().register(space, scope, key, context);
break;
case LOCAL:
localSpace.register(space, scope, key, context);
break;
default:
throw new IllegalStateException("Unsupported scope");
}
}
abstract IdentifierSpaceHolder parentNs();
@Override
public StatementContextImpl<?> lookup(AxiomIdentifier space, IdentifierSpaceKey key) {
StatementContextImpl<?> maybe = localSpace.lookup(space, key);
if(maybe != null) {
return maybe;
}
return parentNs().lookup(space, key);
}
@Override
public Map<IdentifierSpaceKey, StatementContextImpl<?>> space(AxiomIdentifier space) {
return localSpace.space(space);
}
private void registerLocalParent(AxiomIdentifier space, IdentifierSpaceKey key) {
// TODO Auto-generated method stub
}
@Override
public V requireValue(Class<V> type) {
if(result instanceof StatementContextResult) {
return type.cast(((StatementContextResult) result).value());
}
return null;
}
public Requirement<AxiomStatement<?>> requireChild(AxiomItemDefinition required) {
return Requirement.retriableDelegate(() -> {
if(mutableResult() != null) {
return (Requirement) firstChild(required).map(StatementContextImpl::asRequirement).orElse(null);
}
Optional<AxiomStatement<?>> maybe = result.get().first(required);
return Requirement.from(maybe);
});
}
protected IdentifierSpaceHolder exports() {
throw new IllegalStateException("Statement does not provide exports");
}
static class Child<V> extends StatementContextImpl<V> {
private StatementContextImpl<?> parent;
public Child(StatementContextImpl<?> parent, AxiomItemDefinition definition, SourceLocation loc) {
super(definition, loc, Scope.LOCAL);
this.parent = parent;
initResult();
}
@Override
public StatementContextImpl<?> parent() {
return parent;
}
@Override
IdentifierSpaceHolder parentNs() {
return parent;
}
@Override
public void exportIdentifierSpace(IdentifierSpaceKey namespace) {
throw new UnsupportedOperationException("Child can not export identifier space");
}
@Override
public void importIdentifierSpace(NamespaceContext namespaceContext) {
throw new UnsupportedOperationException("Child can not import identifier space");
}
}
static class Root<V> extends StatementContextImpl<V> {
private static final AxiomIdentifier IMPORTED_MODEL = AxiomIdentifier.axiom("ImportedModel");
private final ModelReactorContext reactor;
private Set<IdentifierSpaceHolder> imports;
public Root(ModelReactorContext reactor, AxiomItemDefinition definition,
SourceLocation loc) {
super(definition, loc, Scope.GLOBAL, Scope.LOCAL);
this.reactor = reactor;
this.imports = new HashSet<>();
initResult();
}
@Override
protected IdentifierSpaceHolder parentNs() {
throw new UnsupportedOperationException();
}
@Override
protected void addOutstanding(StatementRuleContextImpl<?> rule) {
reactor.addOutstanding(rule);
}
@Override
public void register(AxiomIdentifier space, Scope scope, IdentifierSpaceKey key,
StatementContextImpl<?> context) {
if (Scope.GLOBAL.equals(scope)) {
reactor.space().register(space, scope, key, context);
}
localSpace().register(space, scope, key, context);
}
@Override
public StatementContextImpl<?> lookup(AxiomIdentifier space, IdentifierSpaceKey key) {
StatementContextImpl<?> maybe = localSpace().lookup(space, key);
if(maybe != null) {
return maybe;
}
for(IdentifierSpaceHolder model : imports) {
maybe = model.lookup(space, key);
if(maybe != null) {
return maybe;
}
}
return null;
}
@Override
protected <T> Factory<T, ? extends AxiomStatement<T>> typeFactory(AxiomTypeDefinition type) {
return reactor.typeFactory(type);
}
@Override
public StatementContextImpl<?> parent() {
return this;
}
@Override
protected IdentifierSpaceHolder exports() {
return localSpace();
}
@Override
public void exportIdentifierSpace(IdentifierSpaceKey namespace) {
reactor.exportIdentifierSpace(namespace, localSpace());
}
@Override
public void importIdentifierSpace(NamespaceContext namespaceContext) {
if(namespaceContext instanceof IdentifierSpaceHolder) {
imports.add((IdentifierSpaceHolder) namespaceContext);
}
}
@Override
protected Root<?> root() {
return this;
}
public Requirement<NamespaceContext> requireNamespace(AxiomIdentifier name, IdentifierSpaceKey namespaceId) {
return Requirement.retriableDelegate(() -> {
return reactor.namespace(name, namespaceId);
});
}
}
protected Root<?> root() {
return parent().root();
}
}