Repository navigation
Expand file tree
/
Copy pathExpressionResultConsultationTest.java
More file actions
575 lines (541 loc) · 25.5 KB
/
Copy pathExpressionResultConsultationTest.java
File metadata and controls
575 lines (541 loc) · 25.5 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*******************************************************************************
* SysML 2 Pilot Implementation
* Copyright (c) 2026 Obeo
* Copyright (c) 2026 Model Driven Solutions, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.omg.sysml.interactive.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.xtext.EcoreUtil2;
import org.junit.Test;
import org.omg.sysml.lang.sysml.ConstructorExpression;
import org.omg.sysml.lang.sysml.Element;
import org.omg.sysml.lang.sysml.Expression;
import org.omg.sysml.lang.sysml.Feature;
import org.omg.sysml.lang.sysml.FeatureDirectionKind;
import org.omg.sysml.lang.sysml.FeatureReferenceExpression;
import org.omg.sysml.lang.sysml.IndexExpression;
import org.omg.sysml.lang.sysml.InvocationExpression;
import org.omg.sysml.lang.sysml.Membership;
import org.omg.sysml.lang.sysml.Relationship;
import org.omg.sysml.lang.sysml.ReturnParameterMembership;
import org.omg.sysml.lang.sysml.SysMLFactory;
import org.omg.sysml.lang.sysml.SysMLPackage;
import org.omg.sysml.lang.sysml.Type;
import org.omg.sysml.util.ElementUtil;
import org.omg.sysml.util.FeatureUtil;
import org.omg.sysml.util.TypeUtil;
import org.omg.sysml.xtext.postprocessing.SysMLParserPostProcessorFactory;
/** Tests the semantic contract of expression results across consultation and explicit transformation. */
public class ExpressionResultConsultationTest extends SysMLInteractiveTest {
private static <T> T findSingle(Resource resource, Class<T> kind) {
T found = null;
for (var contents = resource.getAllContents(); contents.hasNext();) {
var object = contents.next();
if (kind.isInstance(object)) {
if (found != null) {
throw new AssertionError("Expected exactly one " + kind.getSimpleName() + ", found a second");
}
found = kind.cast(object);
}
}
if (found == null) {
throw new AssertionError("Missing " + kind.getSimpleName());
}
return found;
}
/**
* A feature reference exposes a stable output result typed by the referent's
* classifier after semantic transformation, including after cache clearing and
* repeated materialization.
*/
@Test
public void referenceResultRetainsReferentAndType() throws Exception {
Resource resource = parse("referenceResult.kerml", """
package Reference {
classifier Value;
feature original : Value;
feature copy = original;
}
""");
Feature original = findByName(resource, "original", Feature.class);
Type value = findByName(resource, "Value", Type.class);
FeatureReferenceExpression expression = findSingle(resource, FeatureReferenceExpression.class);
// The public result must specialize the exact referent, not an unrelated library feature.
Feature result = expression.getResult();
assertNotNull(result);
// Result subsetting is populated by semantic transformation.
ElementUtil.transform(expression);
assertTrue(TypeUtil.getGeneralTypesOf(result).contains(original));
assertTrue(result.getType().contains(value));
assertStableThroughTransformation(resource, expression, result);
assertTrue(result.getType().contains(value));
}
/**
* A constructor result retains the instantiated classifier and its identity
* through repeated consultations and explicit materialization.
*/
@Test
public void constructorResultRetainsInstantiatedType() throws Exception {
Resource resource = parse("constructorResult.kerml", """
package Construction {
classifier Product;
feature product = new Product();
}
""");
ConstructorExpression expression = findSingle(resource, ConstructorExpression.class);
Type product = findByName(resource, "Product", Type.class);
// The constructor's result denotes a Product both before and after transformation.
Feature result = expression.getResult();
assertNotNull(result);
assertTrue(result.getType().contains(product));
assertStableThroughTransformation(resource, expression, result);
assertTrue(result.getType().contains(product));
}
/**
* Invoking a behavior that is not a function exposes a result typed by that
* behavior after explicit semantic transformation.
*/
@Test
public void behaviorInvocationResultRetainsBehaviorType() throws Exception {
Resource resource = parse("behaviorResult.sysml", """
action def Move;
part p { ref movement = Move(); }
""");
InvocationExpression invocation = findSingle(resource, InvocationExpression.class);
Type move = findByName(resource, "Move", Type.class);
// This is the behavior-result rule, distinct from a function's return parameter.
Feature result = invocation.getResult();
assertNotNull(result);
// Behavior result typing is populated by semantic transformation.
ElementUtil.transform(invocation);
assertTrue(result.getType().contains(move));
assertStableThroughTransformation(resource, invocation, result);
assertTrue(result.getType().contains(move));
}
/**
* Consulting a function invocation's result must preserve positional arguments
* and the result's type when the argument list is queried before and after it.
*/
@Test
public void functionResultPreservesArgumentOrder() throws Exception {
Resource resource = parse("functionResult.sysml", """
calc def Choose {
in leftValue : ScalarValues::Integer;
in rightValue : ScalarValues::Integer;
return selectedValue : ScalarValues::Integer = leftValue;
}
part p { ref choice = Choose(11, 22); }
""");
Feature choice = findByName(resource, "choice", Feature.class);
InvocationExpression invocation = (InvocationExpression) FeatureUtil.getValuationFor(choice).getValue();
// Resolve arguments first: result redefinitions participate in their positional ordering.
List<Expression> arguments = List.copyOf(invocation.getArgument());
assertEquals(2, arguments.size());
assertEquals(List.of(11, 22), arguments.stream()
.map(argument -> ((org.omg.sysml.lang.sysml.LiteralInteger) argument).getValue()).toList());
Feature result = invocation.getResult();
assertNotNull(result);
assertEquals(List.of("ScalarValues::Integer"), result.getType().stream().map(Type::getQualifiedName).toList());
assertStableThroughTransformation(resource, invocation, result);
assertEquals(arguments, invocation.getArgument());
}
/**
* An index result and its sequence result remain distinct and stable during
* nested consultations; the outer result retains the sequence element type.
*/
@Test
public void nestedResultConsultationsPreserveSequenceResult() throws Exception {
Resource resource = parse("nestedResult.kerml", """
package Indexing {
classifier Value;
feature values : Value[*];
feature selected = values#(1);
}
""");
IndexExpression expression = findSingle(resource, IndexExpression.class);
Type value = findByName(resource, "Value", Type.class);
// Index transformation computes result typing through its sequence argument.
Feature result = expression.getResult();
assertNotNull(result);
ElementUtil.transform(expression);
assertTrue(result.getType().contains(value));
Expression sequence = expression.getArgument().get(0);
Feature sequenceResult = sequence.getResult();
assertTrue(result != sequenceResult);
assertTrue(TypeUtil.getGeneralTypesOf(result).contains(sequenceResult));
assertStableThroughTransformation(resource, expression, result);
assertSame(sequenceResult, sequence.getResult());
}
/**
* Programmatically created expressions explicitly prepare an output result
* once and return the same object on subsequent reads.
*/
@Test
public void explicitlyPreparedProgrammaticExpressionsProvideStableResults() {
// Incomplete editor states have no textual equivalent: no referent or instantiated type yet.
List<Expression> expressions = List.of(SysMLFactory.eINSTANCE.createFeatureReferenceExpression(),
SysMLFactory.eINSTANCE.createInvocationExpression(), SysMLFactory.eINSTANCE.createConstructorExpression());
for (Expression expression : expressions) {
assertNull(expression.getResult());
assertTrue(expression.getOwnedRelationship().isEmpty());
TypeUtil.addResultParameterTo(expression);
Feature result = expression.getResult();
assertNotNull(result);
assertEquals(FeatureDirectionKind.OUT, result.getDirection());
// No resource, parser or transformation is needed to retrieve an existing result again.
assertSame(result, expression.getResult());
}
}
/**
* An explicitly supplied return parameter is preserved for each expression
* kind; consulting the result must never replace it with a synthesized feature.
*/
@Test
public void explicitReturnParametersArePreserved() {
// Insert the explicit result directly to isolate the abstract-syntax contract from parsing.
List<Expression> expressions = List.of(SysMLFactory.eINSTANCE.createFeatureReferenceExpression(),
SysMLFactory.eINSTANCE.createInvocationExpression(), SysMLFactory.eINSTANCE.createConstructorExpression());
for (Expression expression : expressions) {
Feature explicit = SysMLFactory.eINSTANCE.createFeature();
explicit.setDeclaredName("explicitResult");
explicit.setDirection(FeatureDirectionKind.OUT);
ReturnParameterMembership membership = SysMLFactory.eINSTANCE.createReturnParameterMembership();
membership.setOwnedMemberParameter(explicit);
expression.getOwnedRelationship().add(membership);
// Repeated preparation and reads must retain the supplied identity and membership.
TypeUtil.addResultParameterTo(expression);
SysMLParserPostProcessorFactory.getPostProcessor(expression).postProcess();
assertSame(explicit, expression.getResult());
assertSame(explicit, expression.getResult());
assertEquals(List.of(membership), expression.getOwnedRelationship());
}
}
/**
* An XMI-loaded constructor receives a correctly typed result after explicit
* preparation; materialization remains idempotent without parser callbacks.
*/
@Test
public void xmiConstructorProvidesResultAfterExplicitPreparation() throws Exception {
Resource parsed = parse("xmiResult.kerml", """
package Construction {
classifier Product;
feature product = new Product();
}
""");
EcoreUtil2.resolveLazyCrossReferences(parsed, null);
ConstructorExpression source = findSingle(parsed, ConstructorExpression.class);
// Remove the result prepared by parser post-processing to serialize an unprepared abstract-syntax model.
source.getOwnedRelationship().removeIf(ReturnParameterMembership.class::isInstance);
Resource serialized = new XMIResourceImpl(URI.createURI("memory:/serialized.xmi"));
serialized.getContents().addAll(parsed.getContents());
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
serialized.save(bytes, Map.of());
Resource loaded = new XMIResourceImpl(URI.createURI("memory:/loaded.xmi"));
try {
parsed.getResourceSet().getResources().add(loaded);
loaded.load(new ByteArrayInputStream(bytes.toByteArray()), Map.of());
ConstructorExpression expression = findSingle(loaded, ConstructorExpression.class);
Type product = findByName(loaded, "Product", Type.class);
// XMI loading does not run the textual parser's post-processors.
assertFalse(expression.getOwnedRelationship().stream().anyMatch(ReturnParameterMembership.class::isInstance));
expression.getResult();
assertFalse(expression.getOwnedRelationship().stream().anyMatch(ReturnParameterMembership.class::isInstance));
TypeUtil.addResultParameterTo(expression);
Feature result = expression.getResult();
assertNotNull(result);
assertTrue(result.getType().contains(product));
assertStableThroughTransformation(loaded, expression, result);
} finally {
loaded.unload();
parsed.getResourceSet().getResources().remove(loaded);
}
}
/**
* Parser preparation creates results before resolving reference and instantiation
* targets, remains idempotent, and leaves literal results inherited.
*/
@Test
public void parserPreparesResultsWithoutResolvingTargets() throws Exception {
Resource resource = parse("preparedResults.kerml", """
package Prepared {
classifier Product;
feature original : Product;
feature copy = original;
feature product = new Product();
function choose { return selected : Product; }
feature chosen = choose();
feature literal = 1;
}
""");
for (Expression expression : List.of(findSingle(resource, FeatureReferenceExpression.class),
findSingle(resource, ConstructorExpression.class), findSingle(resource, InvocationExpression.class))) {
// Inspect stored relationships only: no getResult or semantic lookup may prepare the result.
ReturnParameterMembership resultMembership = expression.getOwnedRelationship().stream()
.filter(ReturnParameterMembership.class::isInstance).map(ReturnParameterMembership.class::cast)
.findFirst().orElseThrow();
Feature result = resultMembership.getOwnedMemberParameter();
assertEquals(FeatureDirectionKind.OUT, result.getDirection());
Membership reference = expression.getOwnedRelationship().stream()
.filter(relationship -> relationship.eClass() == SysMLPackage.Literals.MEMBERSHIP)
.map(Membership.class::cast).findFirst().orElseThrow();
EObject target = (EObject) reference.eGet(SysMLPackage.Literals.MEMBERSHIP__MEMBER_ELEMENT, false);
assertTrue(target.eIsProxy());
List<Relationship> relationships = List.copyOf(expression.getOwnedRelationship());
// Re-entering parser preparation must neither resolve the target nor replace the result.
SysMLParserPostProcessorFactory.getPostProcessor(expression).postProcess();
assertSame(target, reference.eGet(SysMLPackage.Literals.MEMBERSHIP__MEMBER_ELEMENT, false));
assertTrue(target.eIsProxy());
assertEquals(relationships, expression.getOwnedRelationship());
assertSame(result, resultMembership.getOwnedMemberParameter());
}
Expression literal = findSingle(resource, org.omg.sysml.lang.sysml.LiteralInteger.class);
assertFalse(literal.getOwnedRelationship().stream().anyMatch(ReturnParameterMembership.class::isInstance));
assertNotNull(literal.getResult());
}
/**
* Removing a prepared result models an incomplete edit. Result and membership
* queries must not recreate it, including after clearing derived caches.
*/
@Test
public void consultationDoesNotRecreateRemovedResults() throws Exception {
Resource resource = parse("removedResults.kerml", """
package Removed {
classifier Product;
feature original : Product;
feature copy = original;
feature product = new Product();
function choose { return selected : Product; }
feature chosen = choose();
}
""");
for (Expression expression : List.of(findSingle(resource, FeatureReferenceExpression.class),
findSingle(resource, ConstructorExpression.class), findSingle(resource, InvocationExpression.class))) {
assertTrue(expression.getOwnedRelationship().removeIf(ReturnParameterMembership.class::isInstance));
invalidateEditedModel(resource);
List<Relationship> relationships = List.copyOf(expression.getOwnedRelationship());
// Inherited results may still be available; no owned result may be synthesized by a read.
expression.getResult();
expression.getFeature();
expression.getFeatureMembership();
assertEquals(relationships, expression.getOwnedRelationship());
assertFalse(expression.getOwnedRelationship().stream().anyMatch(ReturnParameterMembership.class::isInstance));
}
}
/**
* Retargeting a reference A -> B -> A changes its result specialization and type,
* while retaining the result prepared once before the edits. Model adapters
* are explicitly removed to invalidate caches, then semantic transformation is
* rerun to update result subsetting without replacing the structural result.
*/
@Test
public void preparedReferenceResultFollowsReferentEdits() throws Exception {
Resource resource = parse("editedReference.kerml", """
package EditedReference {
classifier A;
classifier B;
feature a : A;
feature b : B;
feature copy = a;
}
""");
FeatureReferenceExpression expression = findSingle(resource, FeatureReferenceExpression.class);
Feature a = findByName(resource, "a", Feature.class);
Feature b = findByName(resource, "b", Feature.class);
Membership reference = expression.getOwnedMembership().stream()
.filter(membership -> membership.getMemberElement() == a).findFirst().orElseThrow();
// Prepare the structural result once, as a parser or an editor creation command would.
TypeUtil.addResultParameterTo(expression);
Feature result = TypeUtil.getOwnedResultParameterOf(expression);
assertNotNull(result);
List<Relationship> relationships = List.copyOf(expression.getOwnedRelationship());
for (Feature referent : List.of(a, b, a)) {
// Equivalent successive texts: copy = a; copy = b; copy = a, without reparsing.
reference.setMemberElement(referent);
invalidateEditedModel(resource);
assertSame(referent, expression.getReferent());
assertPreparedResultUnchanged(expression, result, relationships);
// Refresh result subsetting through the semantic transformation lifecycle.
ElementUtil.transform(expression);
assertEquals(referent.getType(), result.getType());
List<Type> generalTypes = TypeUtil.getGeneralTypesOf(result);
assertTrue(generalTypes.contains(referent));
// The old referent must disappear, rather than accumulate as another subsetting.
for (Feature other : List.of(a, b)) {
if (other != referent) {
assertFalse(generalTypes.contains(other));
}
}
assertPreparedResultUnchanged(expression, result, relationships);
}
}
/**
* Editing the type of an unchanged referent updates its result transitively;
* semantic transformation restores result subsetting after cache invalidation,
* while the reference, result and return membership retain their identities.
*/
@Test
public void preparedReferenceResultFollowsReferentTypeEdits() throws Exception {
Resource resource = parse("editedReferentType.kerml", """
package EditedReferentType {
classifier A;
classifier B;
feature original : A;
feature copy = original;
}
""");
FeatureReferenceExpression expression = findSingle(resource, FeatureReferenceExpression.class);
Feature original = findByName(resource, "original", Feature.class);
Type a = findByName(resource, "A", Type.class);
Type b = findByName(resource, "B", Type.class);
var typing = original.getOwnedTyping().get(0);
TypeUtil.addResultParameterTo(expression);
Feature result = TypeUtil.getOwnedResultParameterOf(expression);
assertNotNull(result);
List<Relationship> relationships = List.copyOf(expression.getOwnedRelationship());
for (Type type : List.of(a, b, a)) {
// Only original's explicit typing changes; the expression still refers to original.
typing.setType(type);
invalidateEditedModel(resource);
assertSame(original, expression.getReferent());
ElementUtil.transform(expression);
assertEquals(List.of(type), result.getType());
assertPreparedResultUnchanged(expression, result, relationships);
}
}
/**
* A constructor keeps its prepared result when its classifier changes, including
* an intermediate missing target. Restoring a target updates typing without a
* second preparation or a transformation.
*/
@Test
public void preparedConstructorResultSurvivesTargetReplacement() throws Exception {
Resource resource = parse("editedConstructor.kerml", """
package EditedConstructor {
classifier A;
classifier B;
feature product = new A();
}
""");
ConstructorExpression expression = findSingle(resource, ConstructorExpression.class);
Type a = findByName(resource, "A", Type.class);
Type b = findByName(resource, "B", Type.class);
Membership reference = expression.getOwnedMembership().stream()
.filter(membership -> membership.getMemberElement() == a).findFirst().orElseThrow();
TypeUtil.addResultParameterTo(expression);
Feature result = TypeUtil.getOwnedResultParameterOf(expression);
assertNotNull(result);
List<Relationship> relationships = List.copyOf(expression.getOwnedRelationship());
assertEquals(List.of(a), result.getType());
// Intermediate editor state: the instantiated type is temporarily absent.
reference.setMemberElement(null);
invalidateEditedModel(resource);
assertNull(expression.getInstantiatedType());
assertPreparedResultUnchanged(expression, result, relationships);
assertFalse(result.getType().contains(a));
for (Type type : List.of(b, a)) {
// Equivalent to new B(), then new A(), while retaining the same constructor object.
reference.setMemberElement(type);
invalidateEditedModel(resource);
assertSame(type, expression.getInstantiatedType());
assertEquals(List.of(type), result.getType());
assertPreparedResultUnchanged(expression, result, relationships);
}
}
/**
* Changing an invoked function updates the result's return-parameter redefinition
* and type while retaining its identity and positional argument. Returning to
* the original function must remove the intervening function's result semantics.
*/
@Test
public void preparedInvocationResultFollowsFunctionEdits() throws Exception {
Resource resource = parse("editedInvocation.sysml", """
calc def NumberFunction {
in inputValue : ScalarValues::Integer;
return number : ScalarValues::Integer;
}
calc def FlagFunction {
in inputValue : ScalarValues::Integer;
return flag : ScalarValues::Boolean;
}
part p { ref value = NumberFunction(11); }
""");
InvocationExpression expression = findSingle(resource, InvocationExpression.class);
Type numberFunction = findByName(resource, "NumberFunction", Type.class);
Type flagFunction = findByName(resource, "FlagFunction", Type.class);
Feature number = findByName(resource, "number", Feature.class);
Feature flag = findByName(resource, "flag", Feature.class);
Membership reference = expression.getOwnedMembership().stream()
.filter(membership -> membership.getMemberElement() == numberFunction).findFirst().orElseThrow();
TypeUtil.addResultParameterTo(expression);
Feature result = TypeUtil.getOwnedResultParameterOf(expression);
assertNotNull(result);
List<Expression> arguments = List.copyOf(expression.getArgument());
assertEquals(1, arguments.size());
List<Relationship> relationships = List.copyOf(expression.getOwnedRelationship());
for (Feature returnParameter : List.of(number, flag, number)) {
// Change NumberFunction(11) to FlagFunction(11) and back, without rebuilding arguments.
Type function = returnParameter.getOwningType();
assertTrue(function == numberFunction || function == flagFunction);
reference.setMemberElement(function);
invalidateEditedModel(resource);
assertSame(function, expression.getInstantiatedType());
List<Feature> redefined = FeatureUtil.getRedefinedFeaturesWithComputedOf(result);
// Redefinitions also include library results; the function-specific target must be unique.
assertEquals(List.of(returnParameter), redefined.stream()
.filter(feature -> feature == number || feature == flag).toList());
assertEquals(returnParameter.getType(), result.getType());
assertEquals(arguments, expression.getArgument());
assertPreparedResultUnchanged(expression, result, relationships);
}
}
/** Removes model adapters to invalidate cached semantics without preparing or transforming the model. */
private void invalidateEditedModel(Resource resource) {
for (var contents = resource.getAllContents(); contents.hasNext();) {
if (contents.next() instanceof Element element) {
ElementUtil.clean(element);
}
}
}
/** Checks that edits do not require replacing the prepared result or its membership. */
private void assertPreparedResultUnchanged(Expression expression, Feature result, List<Relationship> relationships) {
boolean transformed = ElementUtil.isTransformed(expression);
assertSame(result, expression.getResult());
assertEquals(FeatureDirectionKind.OUT, result.getDirection());
assertEquals(relationships, expression.getOwnedRelationship());
assertEquals(1, relationships.stream().filter(ReturnParameterMembership.class::isInstance).count());
// Consultation must not transform the expression as a side effect.
assertEquals(transformed, ElementUtil.isTransformed(expression));
}
/** Checks the observable result contract without requiring consultation-time containment. */
private void assertStableThroughTransformation(Resource resource, Expression expression, Feature result) {
assertEquals(FeatureDirectionKind.OUT, result.getDirection());
assertSame(result, expression.getResult());
// Clearing derived caches must not replace the result observed by the caller.
ElementUtil.clearCachesOf(expression);
assertSame(result, expression.getResult());
// Explicit transformation owns exactly one result; repeating it must not add another.
ElementUtil.transformAll(resource, true);
ElementUtil.transformAll(resource, true);
assertSame(result, expression.getResult());
assertEquals(1, expression.getOwnedRelationship().stream().filter(ReturnParameterMembership.class::isInstance).count());
}
}