Skip to content

Commit faa5246

Browse files
committed
Fix formatting
1 parent 3ea9240 commit faa5246

25 files changed

+178
-42
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/edit/AspectChangeManager.java

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
2+
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
33
*
44
* See the AUTHORS file(s) distributed with this work for additional
55
* information regarding authorship.
@@ -13,16 +13,25 @@
1313

1414
package org.eclipse.esmf.aspectmodel.edit;
1515

16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.net.URI;
19+
import java.nio.file.Files;
20+
import java.nio.file.Paths;
1621
import java.util.ArrayDeque;
22+
import java.util.ArrayList;
1723
import java.util.Deque;
1824
import java.util.HashMap;
25+
import java.util.List;
1926
import java.util.Map;
2027
import java.util.Optional;
2128
import java.util.stream.Stream;
2229

2330
import org.eclipse.esmf.aspectmodel.AspectModelFile;
2431
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
2532
import org.eclipse.esmf.aspectmodel.resolver.modelfile.RawAspectModelFile;
33+
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
34+
import org.eclipse.esmf.aspectmodel.serializer.SerializationException;
2635
import org.eclipse.esmf.metamodel.AspectModel;
2736
import org.eclipse.esmf.metamodel.impl.DefaultAspectModel;
2837

@@ -38,8 +47,8 @@
3847
* Note the following points:
3948
* <ul>
4049
* <li>Only one AspectChangeManager must wrap a given AspectModel at any time</li>
41-
* <li>All changes are done <i>in-memory</i>. In order to write them to the file system, use the
42-
* {@link org.eclipse.esmf.aspectmodel.serializer.AspectSerializer}</li>
50+
* <li>All changes are done <i>in-memory</i>. In order to write them to the file system, use {@link #writeChangesToDisk(WriteConfig)}
51+
* </li>
4352
* <li>After performing an {@link #applyChange(Change)}, {@link #undoChange()} or {@link #redoChange()} operation, and until the
4453
* next call of one of them, the methods {@link #modifiedFiles()}, {@link #createdFiles()} and {@link #removedFiles()} indicate
4554
* corresponding changes in the AspectModel's files.
@@ -185,4 +194,107 @@ public void indicateFileHasChanged( final AspectModelFile file ) {
185194
fileState.put( file, FileState.CHANGED );
186195
}
187196
}
197+
198+
/**
199+
* Syncs all queued changes to the file system. This is the operation that acutally performs operations such as deleting, creating and
200+
* writing files.
201+
*/
202+
public synchronized WriteResult writeChangesToDisk( final WriteConfig config ) {
203+
final WriteResult writeResult = checkFileSystemConsistency( config );
204+
if ( writeResult instanceof WriteResult.PreconditionsNotMet ) {
205+
return writeResult;
206+
}
207+
208+
final WriteResult result = performFileSystemWrite();
209+
if ( result instanceof WriteResult.Success ) {
210+
resetFileStates();
211+
}
212+
return result;
213+
}
214+
215+
protected WriteResult performFileSystemWrite() {
216+
final List<String> messages = new ArrayList<>();
217+
removedFiles()
218+
.map( fileToRemove -> Paths.get( fileToRemove.sourceLocation().orElseThrow() ).toFile() )
219+
.forEach( file -> {
220+
try {
221+
Files.delete( file.toPath() );
222+
} catch ( final IOException exception ) {
223+
messages.add( "Could not delete file: " + file );
224+
}
225+
} );
226+
227+
createdFiles().forEach( fileToCreate -> {
228+
final File file = Paths.get( fileToCreate.sourceLocation().orElseThrow() ).toFile();
229+
if ( !file.getParentFile().exists() && !file.getParentFile().mkdirs() ) {
230+
messages.add( "Target path to write file could not be created: " + file );
231+
} else {
232+
try {
233+
AspectSerializer.INSTANCE.write( fileToCreate );
234+
} catch ( final SerializationException exception ) {
235+
messages.add( exception.getMessage() );
236+
}
237+
}
238+
} );
239+
240+
modifiedFiles().forEach( aspectModelFile -> {
241+
try {
242+
AspectSerializer.INSTANCE.write( aspectModelFile );
243+
} catch ( final SerializationException exception ) {
244+
messages.add( exception.getMessage() );
245+
}
246+
} );
247+
248+
return messages.isEmpty()
249+
? new WriteResult.Success()
250+
: new WriteResult.WriteFailure( messages );
251+
}
252+
253+
protected WriteResult checkFileSystemConsistency( final WriteConfig config ) {
254+
final List<String> messages = new ArrayList<>();
255+
final boolean[] canBeFixedByOverwriting = new boolean[1];
256+
removedFiles().map( AspectSerializer.INSTANCE::aspectModelFileUrl ).forEach( url -> {
257+
if ( !url.getProtocol().equals( "file" ) ) {
258+
messages.add( "File should be removed, but it is not identified by a file: URL: " + url );
259+
}
260+
final File file = new File( URI.create( url.toString() ) );
261+
if ( !file.exists() ) {
262+
messages.add( "File should be removed, but it does not exist: " + file );
263+
}
264+
} );
265+
266+
createdFiles().map( AspectSerializer.INSTANCE::aspectModelFileUrl ).forEach( url -> {
267+
if ( !url.getProtocol().equals( "file" ) ) {
268+
messages.add( "New file should be written, but it is not identified by a file: URL: " + url );
269+
}
270+
final File file = new File( URI.create( url.toString() ) );
271+
if ( file.exists() && !config.forceOverwrite() ) {
272+
messages.add( "New file should be written, but it already exists: " + file );
273+
canBeFixedByOverwriting[0] = true;
274+
}
275+
if ( file.exists() && config.forceOverwrite() && !file.canWrite() ) {
276+
messages.add( "New file should be written, but it is not writable: " + file );
277+
}
278+
} );
279+
280+
modifiedFiles().map( AspectSerializer.INSTANCE::aspectModelFileUrl ).forEach( url -> {
281+
if ( !url.getProtocol().equals( "file" ) ) {
282+
messages.add( "File should be modified, but it is not identified by a file: URL: " + url );
283+
}
284+
final File file = new File( URI.create( url.toString() ) );
285+
if ( !file.exists() ) {
286+
messages.add( "File should be modified, but it does not exist: " + file );
287+
}
288+
if ( !file.canWrite() ) {
289+
messages.add( "File should be modified, but it is not writable: " + file );
290+
}
291+
if ( !file.isFile() ) {
292+
messages.add( "File should be modified, but it is not a regular file: " + file );
293+
}
294+
} );
295+
296+
return messages.isEmpty()
297+
? new WriteResult.Success()
298+
: new WriteResult.PreconditionsNotMet( messages, canBeFixedByOverwriting[0] );
299+
}
188300
}

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/serializer/AspectSerializer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public URL aspectModelFileUrl( final AspectModelFile aspectModelFile ) {
108108
* Writes the content of an Aspect Model file to its defined source location
109109
*
110110
* @param aspectModelFile the Aspect Model file
111+
* @throws SerializationException if writing the file failed
111112
*/
112113
public void write( final AspectModelFile aspectModelFile ) {
113114
final URL url = aspectModelFileUrl( aspectModelFile );

core/esmf-test-aspect-models/src/main/java/org/eclipse/esmf/test/TestModel.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ default AspectModelUrn getUrn() {
3131
}
3232

3333
static String modelToString( final Model model ) {
34-
// org.apache.jena.riot.RDFWriter x;
35-
final String string = RDFWriter.create().format( RDFFormat.TURTLE ).lang( RDFLanguages.TURTLE ).source( model ).asString();
36-
37-
// final StringWriter stringWriter = new StringWriter();
38-
// model.write( stringWriter, "TURTLE" );
39-
// return stringWriter.toString();
40-
return string;
34+
return RDFWriter.create().format( RDFFormat.TURTLE ).lang( RDFLanguages.TURTLE ).source( model ).asString();
4135
}
4236
}

tools/samm-cli/src/main/java/org/eclipse/esmf/aas/AasCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import org.eclipse.esmf.LoggingMixin;
55
import org.eclipse.esmf.exception.SubCommandException;
66

7+
import lombok.Getter;
78
import picocli.CommandLine;
89

9-
@CommandLine.Command( name = AasCommand.COMMAND_NAME,
10+
@CommandLine.Command(
11+
name = AasCommand.COMMAND_NAME,
1012
description = "Validate and transform AAS Models",
1113
subcommands = {
1214
CommandLine.HelpCommand.class,
@@ -25,10 +27,15 @@ public class AasCommand extends AbstractCommand {
2527
@CommandLine.Mixin
2628
private LoggingMixin loggingMixin;
2729

28-
@CommandLine.Parameters( paramLabel = "INPUT", description = "Input file name of the AAS Model .aasx, .json .xml file", arity = "1",
29-
index = "0" )
30+
@CommandLine.Parameters(
31+
paramLabel = "INPUT",
32+
description = "Input file name of the AAS Model .aasx, .json .xml file",
33+
arity = "1",
34+
index = "0"
35+
)
3036
private String input;
3137

38+
@SuppressWarnings( { "LombokGetterMayBeUsed", "RedundantSuppression" } )
3239
public String getInput() {
3340
return input;
3441
}

tools/samm-cli/src/main/java/org/eclipse/esmf/aas/AasListSubmodelsCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.eclipse.digitaltwin.aas4j.v3.model.Referable;
1717
import picocli.CommandLine;
1818

19-
@CommandLine.Command( name = AasListSubmodelsCommand.COMMAND_NAME, description = "Get list of submodel templates of AAS input",
19+
@CommandLine.Command(
20+
name = AasListSubmodelsCommand.COMMAND_NAME,
21+
description = "Get list of submodel templates of AAS input",
2022
descriptionHeading = "%n@|bold Description|@:%n%n",
2123
parameterListHeading = "%n@|bold Parameters|@:%n",
2224
optionListHeading = "%n@|bold Options|@:%n",

tools/samm-cli/src/main/java/org/eclipse/esmf/aas/AasToCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import picocli.CommandLine;
99

10-
@CommandLine.Command( name = AasToCommand.COMMAND_NAME, description = "Transforms an Aspect Model into another format",
10+
@CommandLine.Command(
11+
name = AasToCommand.COMMAND_NAME,
12+
description = "Transforms an Aspect Model into another format",
1113
subcommands = {
1214
CommandLine.HelpCommand.class,
1315
AasToAspectCommand.class

tools/samm-cli/src/main/java/org/eclipse/esmf/aas/to/AasToAspectCommand.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ public class AasToAspectCommand extends AbstractCommand {
3838
@CommandLine.ParentCommand
3939
private AasToCommand parentCommand;
4040

41-
@CommandLine.Option( names = { "--output-directory", "-d" }, description = "Output directory to write files to" )
41+
@CommandLine.Option(
42+
names = { "--output-directory", "-d" },
43+
description = "Output directory to write files to"
44+
)
4245
private String outputPath = ".";
4346

44-
@CommandLine.Option( names = { "--submodel-template",
45-
"-s" }, description = "Select the submodel template(s) to include, as returned by the aas list command" )
47+
@CommandLine.Option(
48+
names = { "--submodel-template", "-s" },
49+
description = "Select the submodel template(s) to include, as returned by the aas list command"
50+
)
4651
private List<Integer> selectedOptions = new ArrayList<>();
4752

4853
@CommandLine.Mixin
@@ -62,7 +67,7 @@ private void generateAspects( final AasToAspectModelGenerator generator ) {
6267
final StructuredModelsRoot modelsRoot = new StructuredModelsRoot( Path.of( outputPath ) );
6368
final List<Aspect> generatedAspects = generator.generateAspects();
6469

65-
final List<Aspect> filteredAspects = this.selectedOptions.isEmpty() ? generatedAspects :
70+
final List<Aspect> filteredAspects = selectedOptions.isEmpty() ? generatedAspects :
6671
IntStream.range( 0, generatedAspects.size() )
6772
.filter( index -> selectedOptions.contains( index + 1 ) )
6873
.mapToObj( generatedAspects::get )

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
import picocli.CommandLine;
2121

22-
@CommandLine.Command( name = AspectCommand.COMMAND_NAME,
22+
@CommandLine.Command(
23+
name = AspectCommand.COMMAND_NAME,
2324
description = "Validate and transform Aspect Models",
2425
subcommands = {
2526
CommandLine.HelpCommand.class,

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectEditCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import picocli.CommandLine;
2323

24-
@CommandLine.Command( name = AspectEditCommand.COMMAND_NAME,
24+
@CommandLine.Command(
25+
name = AspectEditCommand.COMMAND_NAME,
2526
description = "Edit (refactor) an Aspect Model",
2627
subcommands = {
2728
CommandLine.HelpCommand.class,

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectPrettyPrintCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
import picocli.CommandLine;
2929

30-
@CommandLine.Command( name = AspectPrettyPrintCommand.COMMAND_NAME,
30+
@CommandLine.Command(
31+
name = AspectPrettyPrintCommand.COMMAND_NAME,
3132
description = "Pretty print (format) Aspect Model",
3233
headerHeading = "@|bold Usage|@:%n%n",
3334
descriptionHeading = "%n@|bold Description|@:%n%n",

0 commit comments

Comments
 (0)