Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Fixes: #468
  • Loading branch information
Yauhenikapl committed Feb 8, 2024
1 parent edc466d commit 453b839
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ The available options and their meaning can also be seen in the help text of the
.3+| aas <aas file> to aspect | Translate Asset Administration Shell (AAS) Submodel Templates to Aspect Models | `samm aas AssetAdminShell.aasx to aspect`
| _--output-directory, -d_ : output directory to write files to (default:
current directory) |
| _--submodel-template, -s_ : selected submodel for generating. For getting these options
run `aas <aas file> list` command. | `samm aas AssetAdminShell.aasx to aspect -s 1 -s 2`
| _--submodel-template, -s_ : selected submodel template for generating;
run `samm aas <aas file> list` to list them. | `samm aas AssetAdminShell.aasx to aspect -s 1 -s 2`
.1+| aas <aas file> list | Retrieve a list of submodel templates contained within the provided
Asset Administration Shell (AAS) file. | `samm ass AssetAdminShell.aasx list`
Asset Administration Shell (AAS) file. | `samm ass AssetAdminShell.aasx list`

|===

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.eclipse.esmf.aas;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.eclipse.esmf.AbstractCommand;
import org.eclipse.esmf.LoggingMixin;
import org.eclipse.esmf.aspectmodel.aas.AasFileFormat;
import org.eclipse.esmf.aspectmodel.aas.AasToAspectModelGenerator;
import org.eclipse.esmf.exception.CommandException;

import picocli.CommandLine;

@CommandLine.Command( name = AasListSubmodelsCommand.COMMAND_NAME, description = "Get list submodel templates of AAS input.",
@CommandLine.Command( name = AasListSubmodelsCommand.COMMAND_NAME, description = "Get list of submodel templates of AAS input",
descriptionHeading = "%n@|bold Description|@:%n%n",
parameterListHeading = "%n@|bold Parameters|@:%n",
optionListHeading = "%n@|bold Options|@:%n",
Expand All @@ -30,19 +32,16 @@ public class AasListSubmodelsCommand extends AbstractCommand {
public void run() {
final String path = parentCommand.getInput();
final String extension = FilenameUtils.getExtension( path );
if ( !extension.equals( "xml" ) && !extension.equals( "json" ) && !extension.equals( "aasx" ) ) {
throw new CommandException( "Input file name must be an .xml, .aasx or .json file" );

if ( Arrays.stream( AasFileFormat.values() ).noneMatch( format -> format.toString().equals( extension ) ) ) {
throw new CommandException( "Input file name must be one of " + AasFileFormat.allValues() );
}

List<String> submodelNames = AasToAspectModelGenerator.fromFile( new File( path ) ).getSubmodelNames();
final List<String> submodelNames = AasToAspectModelGenerator.fromFile( new File( path ) ).getSubmodelNames();

for ( String submodelName : submodelNames ) {
int count = submodelNames.indexOf( submodelName );
if ( count < 10 ) {
System.out.printf( " %s: %s%n", count + 1, submodelName );
} else {
System.out.printf( "%s: %s%n", count + 1, submodelName );
}
for ( final String submodelName : submodelNames ) {
final int index = submodelNames.indexOf( submodelName );
System.out.printf( "%2s: %s%n", index + 1, submodelName );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import org.apache.commons.io.FilenameUtils;
import org.eclipse.esmf.AbstractCommand;
Expand Down Expand Up @@ -40,8 +41,7 @@ public class AasToAspectCommand extends AbstractCommand {
@CommandLine.Option( names = { "--output-directory", "-d" }, description = "Output directory to write files to" )
private String outputPath = ".";

@CommandLine.Option( names = { "--submodel-template", "-s" },
description = "Select one or a few options for generation an Aspect Model from AAS. Before run 'list' command for getting possible options." )
@CommandLine.Option( names = { "--submodel-template", "-s" }, description = "Select the submodel template(s) to include, as returned by the aas list command" )
private List<Integer> selectedOptions = new ArrayList<>();

@CommandLine.Mixin
Expand All @@ -61,12 +61,10 @@ private void generateAspects( final AasToAspectModelGenerator generator ) {
final StructuredModelsRoot modelsRoot = new StructuredModelsRoot( Path.of( outputPath ) );
final List<Aspect> generatedAspects = generator.generateAspects();

List<Aspect> filteredAspects = generatedAspects;
if ( !this.selectedOptions.isEmpty() ) {
filteredAspects = this.selectedOptions.stream()
.map( index -> generatedAspects.get( index - 1 ) )
.toList();
}
final List<Aspect> filteredAspects = IntStream.range( 0, generatedAspects.size() )
.filter( index -> selectedOptions.contains( index + 1 ) )
.mapToObj( generatedAspects::get )
.toList();

for ( final Aspect aspect : filteredAspects ) {
final String aspectString = AspectSerializer.INSTANCE.apply( aspect );
Expand Down

0 comments on commit 453b839

Please sign in to comment.