-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImperativeTransaction.java
255 lines (226 loc) · 10.9 KB
/
ImperativeTransaction.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
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// (C) Copyright 2018-2024 Modeling Value Group B.V. (http://modelingvalue.org) ~
// ~
// Licensed under the GNU Lesser General Public License v3.0 (the 'License'). You may not use this file except in ~
// compliance with the License. You may obtain a copy of the License at: https://choosealicense.com/licenses/lgpl-3.0 ~
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on ~
// an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the ~
// specific language governing permissions and limitations under the License. ~
// ~
// Maintainers: ~
// Wim Bast, Tom Brus ~
// ~
// Contributors: ~
// Ronald Krijgsheld ✝, Arjan Kok, Carel Bast ~
// --------------------------------------------------------------------------------------------------------------------- ~
// In Memory of Ronald Krijgsheld, 1972 - 2023 ~
// Ronald was suddenly and unexpectedly taken from us. He was not only our long-term colleague and team member ~
// but also our friend. "He will live on in many of the lines of code you see below." ~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package org.modelingvalue.dclare;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import org.modelingvalue.collections.DefaultMap;
import org.modelingvalue.collections.Entry;
import org.modelingvalue.collections.Set;
import org.modelingvalue.collections.util.NamedIdentity;
import org.modelingvalue.dclare.Priority.Queued;
public final class ImperativeTransaction extends LeafTransaction implements IImperativeTransaction {
@SuppressWarnings("rawtypes")
protected static final DefaultMap<Object, Set<Setable>> SETTED_MAP = DefaultMap.of(k -> Set.of());
@SuppressWarnings("rawtypes")
public static ImperativeTransaction of(Imperative cls, State init, UniverseTransaction universeTransaction, Consumer<Runnable> scheduler, StateDeltaHandler diffHandler, boolean keepTransaction) {
return new ImperativeTransaction(cls, init, universeTransaction, scheduler, diffHandler, keepTransaction);
}
private final static Setable<ImperativeTransaction, Long> CHANGE_NR = Setable.of("$CHANGE_NR", 0L);
private final Consumer<Runnable> scheduler;
@SuppressWarnings("rawtypes")
private final StateDeltaHandler diffHandler;
private final NamedIdentity actionId;
private final Direction direction;
private final MutableState state;
private boolean active;
private boolean commiting;
@SuppressWarnings("rawtypes")
private DefaultMap<Object, Set<Setable>> setted;
@SuppressWarnings("rawtypes")
private DefaultMap<Object, Set<Setable>> allSetted;
@SuppressWarnings("rawtypes")
protected ImperativeTransaction(Imperative cls, State init, UniverseTransaction universeTransaction, Consumer<Runnable> scheduler, StateDeltaHandler diffHandler, boolean keepTransaction) {
super(universeTransaction);
this.state = universeTransaction.createMutableState(init);
this.setted = SETTED_MAP;
this.allSetted = SETTED_MAP;
this.diffHandler = diffHandler;
this.direction = Direction.of(cls.id());
this.actionId = NamedIdentity.of(this, cls.id().toString());
super.start(cls, universeTransaction);
this.scheduler = keepTransaction ? r -> scheduler.accept(() -> {
if (isOpen()) {
LeafTransaction.getContext().setOnThread(this);
try {
r.run();
} catch (Throwable t) {
universeTransaction.handleException(t);
}
}
}) : r -> scheduler.accept(() -> {
if (isOpen()) {
try {
LeafTransaction.getContext().run(this, r);
} catch (Throwable t) {
universeTransaction.handleException(t);
}
}
});
}
@Override
public void stop() {
if (isOpen()) {
super.stop();
}
}
public final Imperative imperative() {
return (Imperative) leaf();
}
public void schedule(Runnable action) {
scheduler.accept(action);
}
@Override
public State state() {
return state.state();
}
public State preState() {
return state.preState();
}
public MutableState mutableState() {
return state;
}
public final boolean commit(State dclare, boolean timeTraveling) {
commiting = true;
boolean insync = setted.isEmpty() && dclare.get(this, CHANGE_NR).equals(state.get(this, CHANGE_NR));
if (preState() != dclare) {
dclare2imper(dclare, timeTraveling, insync);
}
if (!setted.isEmpty()) {
insync = false;
imper2dclare();
} else if (insync && active) {
active = false;
universeTransaction().removeActive(this);
}
commiting = false;
return insync;
}
@SuppressWarnings({"rawtypes", "unchecked"})
private void dclare2imper(State dclare, boolean timeTraveling, boolean insync) {
DefaultMap<Object, Set<Setable>> finalAllSetted = allSetted;
State imper = state();
if (insync) {
allSetted = SETTED_MAP;
} else {
for (Entry<Object, Set<Setable>> e : finalAllSetted) {
Object object = e.getKey();
DefaultMap<Setable, Object> dclareProps = dclare.getProperties(object);
DefaultMap<Setable, Object> imperProps = imper.getProperties(object);
for (Setable setable : e.getValue()) {
dclareProps = StateMap.setProperties(object, dclareProps, setable, imperProps.get(setable));
}
dclare = dclare.set(object, dclareProps);
}
}
imper = state.setState(dclare);
diffHandler.handleDelta(imper, dclare, insync, finalAllSetted);
}
@SuppressWarnings({"rawtypes", "unchecked"})
private void imper2dclare() {
State imper = state();
DefaultMap<Object, Set<Setable>> finalSetted = setted;
setted = SETTED_MAP;
universeTransaction().put(Action.of(actionId, u -> {
try {
finalSetted.forEachOrdered(e -> {
DefaultMap<Setable, Object> props = imper.getProperties(e.getKey());
for (Setable p : e.getValue()) {
if (p instanceof Queued && ((Queued) p).actions()) {
Priority prio = imper.priority((Queued) p);
for (Action action : (Set<Action>) props.get(p)) {
action.trigger((Mutable) e.getKey(), prio);
}
} else {
p.set(e.getKey(), props.get(p));
}
}
});
} catch (Throwable t) {
CHANGE_NR.set(ImperativeTransaction.this, imper.get(ImperativeTransaction.this, CHANGE_NR));
universeTransaction().handleException(t);
}
}, direction, LeafModifier.preserved));
}
@Override
public <O extends Mutable> void trigger(O target, Action<O> action, Priority priority) {
set(target, state.actions(priority), Set::add, action);
}
@SuppressWarnings("unchecked")
@Override
public <O, T> T set(O object, Setable<O, T> property, T post) {
T[] oldNew = (T[]) new Object[1];
state.set(object, property, post, oldNew);
change(object, property, oldNew[0], post);
return oldNew[0];
}
@SuppressWarnings("unchecked")
@Override
public <O, T, E> T set(O object, Setable<O, T> property, BiFunction<T, E, T> function, E element) {
T[] oldNew = (T[]) new Object[2];
state.set(object, property, function, element, oldNew);
change(object, property, oldNew[0], oldNew[1]);
return oldNew[0];
}
@SuppressWarnings("unchecked")
@Override
public <O, T> T set(O object, Setable<O, T> property, UnaryOperator<T> oper) {
T[] oldNew = (T[]) new Object[2];
state.set(object, property, oper, oldNew);
change(object, property, oldNew[0], oldNew[1]);
return oldNew[0];
}
@SuppressWarnings("rawtypes")
private <O, T> void change(O object, Setable<O, T> property, T preValue, T postValue) {
if (!Objects.equals(preValue, postValue)) {
boolean first = setted.isEmpty();
Set<Setable> set = Set.of(property);
allSetted = allSetted.add(object, set, Set::addAll);
setted = setted.add(object, set, Set::addAll);
if (first) {
set(this, CHANGE_NR, (BiFunction<Long, Long, Long>) Long::sum, 1l);
if (!commiting) {
if (!active) {
active = true;
universeTransaction().addActive(this);
}
universeTransaction().commit();
}
}
}
}
@Override
protected State run(State state) {
throw new UnsupportedOperationException();
}
@Override
public ActionInstance actionInstance() {
throw new UnsupportedOperationException();
}
@Override
public Direction direction() {
return direction;
}
@Override
protected String getCurrentTypeForTrace() {
return "IM";
}
}