-
Notifications
You must be signed in to change notification settings - Fork 188
/
ModelReactorContext.java
221 lines (177 loc) · 8.45 KB
/
ModelReactorContext.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
package com.evolveum.axiom.lang.impl;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
import com.evolveum.axiom.api.AxiomIdentifier;
import com.evolveum.axiom.api.AxiomValue;
import com.evolveum.axiom.api.AxiomValueFactory;
import com.evolveum.axiom.api.schema.AxiomItemDefinition;
import com.evolveum.axiom.api.schema.AxiomSchemaContext;
import com.evolveum.axiom.api.schema.AxiomTypeDefinition;
import com.evolveum.axiom.api.schema.AxiomIdentifierDefinition.Scope;
import com.evolveum.axiom.concepts.Lazy;
import com.evolveum.axiom.lang.api.AxiomBuiltIn.Type;
import com.evolveum.axiom.lang.antlr.AxiomModelStatementSource;
import com.evolveum.axiom.lang.api.AxiomBuiltIn;
import com.evolveum.axiom.lang.api.IdentifierSpaceKey;
import com.evolveum.axiom.lang.spi.AxiomIdentifierDefinitionImpl;
import com.evolveum.axiom.lang.spi.AxiomIdentifierResolver;
import com.evolveum.axiom.lang.spi.AxiomItemDefinitionImpl;
import com.evolveum.axiom.lang.spi.AxiomSemanticException;
import com.evolveum.axiom.lang.spi.AxiomTypeDefinitionImpl;
import com.evolveum.axiom.reactor.Dependency;
import com.evolveum.axiom.reactor.RuleReactorContext;
import com.google.common.base.Strings;
import org.jetbrains.annotations.Nullable;
public class ModelReactorContext extends
RuleReactorContext<AxiomSemanticException, ValueContext<?>, ValueActionImpl<?>, RuleContextImpl>
implements AxiomIdentifierResolver {
private static final AxiomIdentifier ROOT = AxiomIdentifier.from("root", "root");
private static final String AXIOM_LANG_RESOURCE = "/axiom-lang.axiom";
private static final String AXIOM_BUILTIN_RESOURCE = "/axiom-base-types.axiom";
private static final Lazy<AxiomModelStatementSource> BASE_LANGUAGE_SOURCE = Lazy.from(() -> {
InputStream stream = AxiomBuiltIn.class.getResourceAsStream(AXIOM_LANG_RESOURCE);
try {
return AxiomModelStatementSource.from(AXIOM_LANG_RESOURCE, stream);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
private static final Lazy<AxiomModelStatementSource> BASE_TYPES_SOURCE = Lazy.from(() -> {
InputStream stream = AxiomBuiltIn.class.getResourceAsStream(AXIOM_BUILTIN_RESOURCE);
try {
return AxiomModelStatementSource.from(AXIOM_BUILTIN_RESOURCE, stream);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
public static final Lazy<AxiomSchemaContext> BASE_LANGUAGE = Lazy.from(() -> {
ModelReactorContext reactor = boostrapReactor();
return reactor.computeSchemaContext();
});
public static final ModelReactorContext reactor(AxiomSchemaContext context) {
ModelReactorContext reactorContext = new ModelReactorContext(context);
defaults(reactorContext);
return reactorContext;
}
public static final ModelReactorContext boostrapReactor() {
ModelReactorContext reactorContext = new ModelReactorContext(AxiomSchemaContextImpl.boostrapContext());
defaults(reactorContext);
return reactorContext;
}
public static final ModelReactorContext defaultReactor() {
return reactor(BASE_LANGUAGE.get());
}
private static void defaults(ModelReactorContext reactorContext) {
reactorContext.addRules(BasicStatementRule.values());
reactorContext.addStatementFactory(Type.TYPE_DEFINITION.name(), AxiomTypeDefinitionImpl.FACTORY);
reactorContext.addStatementFactory(Type.ITEM_DEFINITION.name(), AxiomItemDefinitionImpl.FACTORY);
reactorContext.addStatementFactory(Type.IDENTIFIER_DEFINITION.name(), AxiomIdentifierDefinitionImpl.FACTORY);
reactorContext.loadModelFromSource(BASE_LANGUAGE_SOURCE.get());
reactorContext.loadModelFromSource(BASE_TYPES_SOURCE.get());
}
Collection<RuleContextImpl> rules = new ArrayList<>();
private final AxiomSchemaContext boostrapContext;
private final Map<IdentifierSpaceKey, CompositeIdentifierSpace> exported = new HashMap<>();
Map<Object, AxiomValueContext<?>> globalItems = new HashMap<>();
IdentifierSpaceHolderImpl globalSpace = new IdentifierSpaceHolderImpl(Scope.GLOBAL);
Map<AxiomIdentifier, AxiomValueFactory<?, ?>> typeFactories = new HashMap<>();
List<AxiomValueContext<?>> roots = new ArrayList<>();
public ModelReactorContext(AxiomSchemaContext boostrapContext) {
this.boostrapContext = boostrapContext;
}
public AxiomSchemaContext computeSchemaContext() throws AxiomSemanticException {
compute();
return createSchemaContext();
}
@Override
protected void failOutstanding(Collection<ValueActionImpl<?>> outstanding) throws AxiomSemanticException {
StringBuilder messages = new StringBuilder("Can not complete models, following errors occured:\n");
for (ValueActionImpl<?> rule : outstanding) {
Exception exception = rule.errorMessage();
messages.append("Rule: ").append(rule.name()).append("\n");
if (exception != null) {
messages.append(exception.getMessage()).append("\n");
}
for (Dependency<?> req : rule.dependencies()) {
if (!req.isSatisfied() && req.errorMessage() != null) {
messages.append(req.errorMessage().getMessage()).append("\n");
}
}
}
throw new AxiomSemanticException(messages.toString());
}
private AxiomSchemaContext createSchemaContext() {
return new AxiomSchemaContextImpl(globalSpace.build());
}
public IdentifierSpaceHolderImpl space() {
return globalSpace;
}
@Override
protected void addOutstanding(ValueActionImpl<?> action) {
super.addOutstanding(action);
}
public void addRules(AxiomStatementRule<?>... rules) {
for (AxiomStatementRule<?> statementRule : rules) {
this.rules.add(new RuleContextImpl(statementRule));
}
}
public void loadModelFromSource(AxiomModelStatementSource source) {
SourceContext sourceCtx = new SourceContext(this, source, source.imports(), new CompositeIdentifierSpace());
source.stream(new ItemStreamContextBuilder(sourceCtx), Optional.empty());
}
@Override
public AxiomIdentifier resolveIdentifier(@Nullable String prefix, @NotNull String localName) {
if (Strings.isNullOrEmpty(prefix)) {
return AxiomIdentifier.axiom(localName);
}
return null;
}
public void addStatementFactory(AxiomIdentifier statementType, AxiomValueFactory<?, ?> factory) {
typeFactories.put(statementType, factory);
}
public <V> AxiomValueFactory<V, AxiomValue<V>> typeFactory(AxiomTypeDefinition statementType) {
Optional<AxiomTypeDefinition> current = Optional.of(statementType);
do {
@SuppressWarnings("unchecked")
AxiomValueFactory<V, ?> maybe = (AxiomValueFactory<V, ?>) typeFactories.get(current.get().name());
if (maybe != null) {
return (AxiomValueFactory) maybe;
}
current = current.get().superType();
} while (current.isPresent());
return ItemValueImpl.factory();
}
public void exportIdentifierSpace(IdentifierSpaceKey namespace, IdentifierSpaceHolder localSpace) {
exported(namespace).add(localSpace);
}
private CompositeIdentifierSpace exported(IdentifierSpaceKey namespace) {
return exported.computeIfAbsent(namespace, k -> new CompositeIdentifierSpace());
}
public Dependency<NamespaceContext> namespace(AxiomIdentifier name, IdentifierSpaceKey namespaceId) {
return Dependency.orNull(exported.get(namespaceId));
}
@Override
protected AxiomSemanticException createException() {
return null;
}
@Override
protected Collection<RuleContextImpl> rulesFor(ValueContext<?> context) {
// TODO: Add smart filters if neccessary
return rules;
}
public Optional<AxiomItemDefinition> rootDefinition(AxiomIdentifier statement) {
return boostrapContext.getRoot(statement);
}
public void applyRuleDefinitions(ValueContext<?> valueContext) {
addActionsFor(valueContext);
}
public void register(AxiomIdentifier space, Scope scope, IdentifierSpaceKey key, ValueContext<?> context) {
globalSpace.register(space, scope, key, context);
}
}