|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 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 io.openmanufacturing.sds.aspectmodel; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileInputStream; |
| 18 | +import java.io.FileNotFoundException; |
| 19 | +import java.io.FileOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.io.PrintWriter; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import org.apache.maven.plugin.AbstractMojo; |
| 31 | +import org.apache.maven.plugin.MojoExecutionException; |
| 32 | +import org.apache.maven.plugins.annotations.Parameter; |
| 33 | + |
| 34 | +import io.openmanufacturing.sds.aspectmodel.resolver.AspectModelResolver; |
| 35 | +import io.openmanufacturing.sds.aspectmodel.resolver.FileSystemStrategy; |
| 36 | +import io.openmanufacturing.sds.aspectmodel.resolver.ModelResolutionException; |
| 37 | +import io.openmanufacturing.sds.aspectmodel.resolver.services.SdsAspectMetaModelResourceResolver; |
| 38 | +import io.openmanufacturing.sds.aspectmodel.resolver.services.TurtleLoader; |
| 39 | +import io.openmanufacturing.sds.aspectmodel.resolver.services.VersionedModel; |
| 40 | +import io.openmanufacturing.sds.aspectmodel.urn.AspectModelUrn; |
| 41 | +import io.openmanufacturing.sds.aspectmodel.validation.report.ValidationReport; |
| 42 | +import io.openmanufacturing.sds.aspectmodel.validation.services.AspectModelValidator; |
| 43 | +import io.vavr.control.Try; |
| 44 | + |
| 45 | +public abstract class AspectModelMojo extends AbstractMojo { |
| 46 | + |
| 47 | + @Parameter( defaultValue = "${basedir}/src/main/resources/aspects" ) |
| 48 | + private String modelsRootDirectory = System.getProperty("user.dir") + "/src/main/resources/aspects"; |
| 49 | + |
| 50 | + @Parameter( required = true, property = "include" ) |
| 51 | + private Set<String> includes; |
| 52 | + |
| 53 | + @Parameter |
| 54 | + protected String outputDirectory = ""; |
| 55 | + |
| 56 | + protected void validateParameters() throws MojoExecutionException { |
| 57 | + if ( includes == null || includes.isEmpty() ) { |
| 58 | + throw new MojoExecutionException( "Missing configuration. Please provide Aspect Models to be included." ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected Set<Try<VersionedModel>> loadAndResolveModels() throws MojoExecutionException { |
| 63 | + final Path modelsRoot = Path.of( modelsRootDirectory ); |
| 64 | + return includes.stream().map( AspectModelUrn::fromUrn ) |
| 65 | + .map( urn -> new AspectModelResolver().resolveAspectModel( new FileSystemStrategy( modelsRoot ), urn ) ) |
| 66 | + .collect( Collectors.toSet() ); |
| 67 | + } |
| 68 | + |
| 69 | + protected Set<VersionedModel> loadModelsOrFail() throws MojoExecutionException { |
| 70 | + final Set<VersionedModel> versionedModels = new HashSet<>(); |
| 71 | + final Set<Try<VersionedModel>> resolvedModels = loadAndResolveModels(); |
| 72 | + for ( final Try<VersionedModel> versionedModel : resolvedModels ) { |
| 73 | + if ( versionedModel.isSuccess() ) { |
| 74 | + versionedModels.add( versionedModel.get() ); |
| 75 | + continue; |
| 76 | + } |
| 77 | + handleFailedModelResolution( versionedModel ); |
| 78 | + } |
| 79 | + return versionedModels; |
| 80 | + } |
| 81 | + |
| 82 | + private void handleFailedModelResolution( final Try<VersionedModel> failedModel ) throws MojoExecutionException { |
| 83 | + final Throwable loadModelFailureCause = failedModel.getCause(); |
| 84 | + |
| 85 | + // Model can not be loaded, root cause e.g. File not found |
| 86 | + if ( loadModelFailureCause instanceof IllegalArgumentException ) { |
| 87 | + throw new MojoExecutionException( "Can not open file in models root directory.", loadModelFailureCause ); |
| 88 | + } |
| 89 | + |
| 90 | + if ( loadModelFailureCause instanceof ModelResolutionException ) { |
| 91 | + throw new MojoExecutionException( "Could not resolve all model elements", loadModelFailureCause ); |
| 92 | + } |
| 93 | + |
| 94 | + // Another exception, e.g. syntax error. Let the validator handle this |
| 95 | + final AspectModelValidator validator = new AspectModelValidator(); |
| 96 | + final ValidationReport report = validator.validate( failedModel ); |
| 97 | + throw new MojoExecutionException( report.toString(), loadModelFailureCause ); |
| 98 | + } |
| 99 | + |
| 100 | + protected Map<AspectModelUrn,VersionedModel> loadButNotResolveModels() throws MojoExecutionException { |
| 101 | + final Map<AspectModelUrn,VersionedModel> versionedModels = new HashMap<>(); |
| 102 | + for ( final String urn : includes ) { |
| 103 | + final AspectModelUrn aspectModelUrn = AspectModelUrn.fromUrn( urn ); |
| 104 | + final String aspectModelFilePath = String.format( "%s/%s/%s/%s.ttl", modelsRootDirectory, aspectModelUrn.getNamespace(), aspectModelUrn.getVersion(), |
| 105 | + aspectModelUrn.getName() ); |
| 106 | + |
| 107 | + final File inputFile = new File( aspectModelFilePath ).getAbsoluteFile(); |
| 108 | + try ( final InputStream inputStream = new FileInputStream( inputFile ) ) { |
| 109 | + final SdsAspectMetaModelResourceResolver metaModelResourceResolver = new SdsAspectMetaModelResourceResolver(); |
| 110 | + final Try<VersionedModel> versionedModel = TurtleLoader.loadTurtle( inputStream ) |
| 111 | + .flatMap( model -> metaModelResourceResolver.getBammVersion( model ) |
| 112 | + .flatMap( metaModelVersion -> metaModelResourceResolver.mergeMetaModelIntoRawModel( model, metaModelVersion ) ) ); |
| 113 | + if ( versionedModel.isFailure() ) { |
| 114 | + final String errorMessage = String.format( "Failed to load Aspect Model %s.", aspectModelUrn.getName() ); |
| 115 | + throw new MojoExecutionException( errorMessage, versionedModel.getCause() ); |
| 116 | + } |
| 117 | + versionedModels.put( aspectModelUrn, versionedModel.get() ); |
| 118 | + } catch ( final IOException exception ) { |
| 119 | + final String errorMessage = String.format( "Failed to load Aspect Model %s.", aspectModelUrn.getName() ); |
| 120 | + throw new MojoExecutionException( errorMessage, exception ); |
| 121 | + } |
| 122 | + } |
| 123 | + return versionedModels; |
| 124 | + } |
| 125 | + |
| 126 | + protected FileOutputStream getStreamForFile( final String artifactName, final String outputDirectory ) { |
| 127 | + try { |
| 128 | + final File directory = new File( outputDirectory ); |
| 129 | + directory.mkdirs(); |
| 130 | + final File file = new File( directory.getPath() + File.separator + artifactName ); |
| 131 | + return new FileOutputStream( file ); |
| 132 | + } catch ( final FileNotFoundException exception ) { |
| 133 | + throw new RuntimeException( "Output file for Aspect Model documentation generation not found.", exception ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + protected PrintWriter initializePrintWriter( final AspectModelUrn aspectModelUrn ) { |
| 138 | + final String aspectModelFileName = String.format( "%s.ttl", aspectModelUrn.getName() ); |
| 139 | + final FileOutputStream streamForFile = getStreamForFile( aspectModelFileName, outputDirectory ); |
| 140 | + return new PrintWriter( streamForFile ); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments