Skip to content

Commit 8b0bb48

Browse files
committed
Add Java API documentation section on modifying Aspect Models
1 parent 82b3fbd commit 8b0bb48

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for additional
5+
* information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package examples;
15+
16+
// tag::imports[]
17+
import java.io.File;
18+
import java.text.SimpleDateFormat;
19+
import java.util.Date;
20+
import java.util.List;
21+
import java.util.Optional;
22+
23+
import org.eclipse.esmf.aspectmodel.edit.AspectChangeManager;
24+
import org.eclipse.esmf.aspectmodel.edit.AspectChangeManagerConfig;
25+
import org.eclipse.esmf.aspectmodel.edit.AspectChangeManagerConfigBuilder;
26+
import org.eclipse.esmf.aspectmodel.edit.Change;
27+
import org.eclipse.esmf.aspectmodel.edit.ChangeGroup;
28+
import org.eclipse.esmf.aspectmodel.edit.ChangeReport;
29+
import org.eclipse.esmf.aspectmodel.edit.ChangeReportFormatter;
30+
import org.eclipse.esmf.aspectmodel.edit.change.MoveElementToNewFile;
31+
import org.eclipse.esmf.aspectmodel.edit.change.RenameElement;
32+
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
33+
import org.eclipse.esmf.metamodel.AspectModel;
34+
// end::imports[]
35+
36+
import org.junit.jupiter.api.Test;
37+
38+
public class EditAspectModel {
39+
@Test
40+
void testEditAspectModel() {
41+
// tag::editModel[]
42+
final AspectModel aspectModel = new AspectModelLoader().load(
43+
// a File, InputStream or AspectModelUrn
44+
// end::editModel[]
45+
new File( "aspect-models/org.eclipse.esmf.examples.movement/1.0.0/Movement.ttl" )
46+
// tag::editModel[]
47+
);
48+
49+
// All changes to an Aspect Model are done using an AspectChangeManager.
50+
// Optionally, you can pass a config object as first constructor argument:
51+
final AspectChangeManagerConfig config = AspectChangeManagerConfigBuilder.builder()
52+
.detailedChangeReport( true )
53+
.defaultFileHeader( List.of( "Generated on "
54+
+ new SimpleDateFormat( "dd-MM-yyyy" ).format( new Date() ) ) )
55+
.build();
56+
final AspectChangeManager changeManager = new AspectChangeManager( config, aspectModel );
57+
58+
// You can create a single change, or you can combine multiple changes into a group.
59+
// For all possible refactoring operations, see classes implementing the Change interface.
60+
final Change refactorModel = new ChangeGroup( List.of(
61+
// Rename an element. This works with with samm:Aspect and all other model elements.
62+
new RenameElement( aspectModel.aspect(), "MyAspect" ),
63+
// Move an element to a new Aspect Model file in the same namespace.
64+
new MoveElementToNewFile(
65+
// The element to move.
66+
aspectModel.aspect().getProperties().get( 0 ),
67+
// If you intend writing the model to the file system, set the location
68+
// for the newly created file here.
69+
Optional.empty() )
70+
) );
71+
72+
// Apply the changes and get a report that summerizes the changes.
73+
final ChangeReport changeReport = changeManager.applyChange( refactorModel );
74+
75+
// If you want to display the change report, you can serialize it to a string:
76+
ChangeReportFormatter.INSTANCE.apply( changeReport, config );
77+
78+
// Alternatively, you can also get views on collections containing modified/
79+
// added/removed files for the last applied change.
80+
changeManager.createdFiles().forEach( file -> System.out.println( "Created: " + file ) );
81+
changeManager.modifiedFiles().forEach( file -> System.out.println( "Modified: " + file ) );
82+
changeManager.removedFiles().forEach( file -> System.out.println( "Removed: " + file ) );
83+
84+
// At this point, you could use the AspectChangeManager's undo() method to revert
85+
// the last change (refactorModel in this case); afterwards you can also redo().
86+
// This functionality is mainly interesting when refactoring the Aspect Model
87+
// interactively.
88+
89+
// If you want to write the model changes to the file system, use the AspectSerializer.
90+
// Each AspectModelFile will be written to its respective source location.
91+
// Alternatively, the AspectSerializer also provides a method to write an AspectModelFile
92+
// into a String.
93+
// end::editModel[]
94+
/*
95+
// tag::editModel[]
96+
AspectSerializer.INSTANCE.write( aspectModel );
97+
// end::editModel[]
98+
*/
99+
}
100+
}

documentation/developer-guide/modules/tooling-guide/pages/java-aspect-tooling.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,32 @@ Example:
712712
include::example$sample-file-header.vm[]
713713
----
714714

715+
[[modifying-and-creating-aspect-models]]
716+
== Modifying and creating Aspect Models
717+
718+
You can use the `AspectChangeManager` to modify an Aspect Model. Each modifying operation performed
719+
on an Aspect Model is called a _change_. Instances of classes that implement the `Change` interface
720+
can be passed to the `AspectChangeManager` `applyChange()` method. Available `Change`​s
721+
include renaming Aspect Model files or Model elements, adding and removing Aspect Model files and
722+
moving Aspect Model elements to other or new files in the same or another namespace.
723+
724+
++++
725+
<details>
726+
<summary>Show used imports</summary>
727+
++++
728+
[source,java,indent=0,subs="+macros,+quotes"]
729+
----
730+
include::example$EditAspectModel.java[tags=imports]
731+
----
732+
++++
733+
</details>
734+
++++
735+
736+
[source,java,indent=0,subs="+macros,+quotes"]
737+
----
738+
include::example$EditAspectModel.java[tags=editModel]
739+
----
740+
715741
[[accessing-samm-programmatically]]
716742
== Accessing the SAMM programmatically
717743

0 commit comments

Comments
 (0)