From 7825554bfef145457306a8a147c9b29ff242b312 Mon Sep 17 00:00:00 2001 From: "Textor Andreas (BCI/ESW17)" Date: Fri, 4 Aug 2023 10:41:05 +0200 Subject: [PATCH] Adapt samm-cli to updated diagram generation --- .../org/eclipse/esmf/AbstractCommand.java | 2 +- .../main/java/org/eclipse/esmf/SammCli.java | 8 --- .../eclipse/esmf/aspect/AspectToCommand.java | 2 - .../esmf/aspect/to/AspectToDotCommand.java | 59 ------------------- .../substitution/GraalJavascriptEngine.java | 51 ---------------- .../substitution/GraalVmJsGraphvizEngine.java | 38 ------------ ...u_nidi_graphviz_engine_GraphvizLoader.java | 45 -------------- .../samm-cli/reflect-config.json | 15 ----- .../samm-cli/resource-config.json | 5 +- .../java/org/eclipse/esmf/SammCliTest.java | 49 --------------- 10 files changed, 2 insertions(+), 272 deletions(-) delete mode 100644 tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToDotCommand.java delete mode 100644 tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalJavascriptEngine.java delete mode 100644 tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalVmJsGraphvizEngine.java delete mode 100644 tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/Target_guru_nidi_graphviz_engine_GraphvizLoader.java diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/AbstractCommand.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/AbstractCommand.java index c828eac74..fbafbe273 100644 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/AbstractCommand.java +++ b/tools/samm-cli/src/main/java/org/eclipse/esmf/AbstractCommand.java @@ -89,7 +89,7 @@ protected AspectContext loadModelOrFail( final String modelFileName, final Exter protected void generateDiagram( final String inputFileName, final AspectModelDiagramGenerator.Format targetFormat, final String outputFileName, final String languageTag, final ExternalResolverMixin resolverConfig ) throws IOException { final AspectContext context = loadModelOrFail( inputFileName, resolverConfig ); - final AspectModelDiagramGenerator generator = new AspectModelDiagramGenerator( context.rdfModel() ); + final AspectModelDiagramGenerator generator = new AspectModelDiagramGenerator( context ); final Set targetFormats = new HashSet<>(); targetFormats.add( targetFormat ); final Set languagesUsedInModel = LanguageCollector.collectUsedLanguages( context.rdfModel().getModel() ); diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/SammCli.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/SammCli.java index 31f97dda3..67ecf34aa 100644 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/SammCli.java +++ b/tools/samm-cli/src/main/java/org/eclipse/esmf/SammCli.java @@ -18,9 +18,7 @@ import org.fusesource.jansi.AnsiConsole; -import guru.nidi.graphviz.engine.Graphviz; import org.eclipse.esmf.aspect.AspectCommand; -import org.eclipse.esmf.substitution.GraalVmJsGraphvizEngine; import picocli.CommandLine; @CommandLine.Command( name = SammCli.COMMAND_NAME, @@ -88,8 +86,6 @@ int runWithExceptionHandler( final CommandLine.IExecutionExceptionHandler except public static void main( final String[] argv ) { NativeImageHelpers.ensureRequiredEnvironment(); - setupGraphvizJava(); - // The disabling color switch needs to be checked before PicoCLI initialization boolean disableColor = false; for ( final String arg : argv ) { @@ -110,10 +106,6 @@ public static void main( final String[] argv ) { System.exit( exitCode ); } - private static void setupGraphvizJava() { - Graphviz.useEngine( new GraalVmJsGraphvizEngine() ); - } - protected String format( final String string ) { return commandLine.getColorScheme().ansi().string( string ); } diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectToCommand.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectToCommand.java index c2b683f0d..96a641660 100644 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectToCommand.java +++ b/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/AspectToCommand.java @@ -21,7 +21,6 @@ import org.eclipse.esmf.AbstractCommand; import org.eclipse.esmf.LoggingMixin; -import org.eclipse.esmf.aspect.to.AspectToDotCommand; import org.eclipse.esmf.aspect.to.AspectToHtmlCommand; import org.eclipse.esmf.aspect.to.AspectToJavaCommand; import org.eclipse.esmf.aspect.to.AspectToOpenapiCommand; @@ -32,7 +31,6 @@ @CommandLine.Command( name = AspectToCommand.COMMAND_NAME, description = "Transforms an Aspect Model into another format", subcommands = { CommandLine.HelpCommand.class, - AspectToDotCommand.class, AspectToHtmlCommand.class, AspectToJavaCommand.class, AspectToJsonCommand.class, diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToDotCommand.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToDotCommand.java deleted file mode 100644 index ded517dc7..000000000 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToDotCommand.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH - * - * See the AUTHORS file(s) distributed with this work for additional - * information regarding authorship. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * SPDX-License-Identifier: MPL-2.0 - */ - -package org.eclipse.esmf.aspect.to; - -import java.io.IOException; - -import org.eclipse.esmf.AbstractCommand; -import org.eclipse.esmf.ExternalResolverMixin; -import org.eclipse.esmf.LoggingMixin; -import org.eclipse.esmf.aspect.AspectToCommand; -import org.eclipse.esmf.aspectmodel.generator.diagram.AspectModelDiagramGenerator; -import org.eclipse.esmf.exception.CommandException; -import picocli.CommandLine; - -@CommandLine.Command( name = AspectToDotCommand.COMMAND_NAME, - description = "Generate DOT (graphviz) diagram for an Aspect Model", - descriptionHeading = "%n@|bold Description|@:%n%n", - parameterListHeading = "%n@|bold Parameters|@:%n", - optionListHeading = "%n@|bold Options|@:%n", - mixinStandardHelpOptions = true -) -public class AspectToDotCommand extends AbstractCommand { - public static final String COMMAND_NAME = "dot"; - - @CommandLine.Option( names = { "--output", "-o" }, description = "Output file path (default: stdout)" ) - private String outputFilePath = "-"; - - @CommandLine.Option( names = { "--language", "-l" }, description = "The language from the model for which the diagram should be generated (default: en)" ) - private String language = "en"; - - @CommandLine.ParentCommand - private AspectToCommand parentCommand; - - @CommandLine.Mixin - private LoggingMixin loggingMixin; - - @CommandLine.Mixin - private ExternalResolverMixin customResolver; - - @Override - public void run() { - try { - generateDiagram( parentCommand.parentCommand.getInput(), AspectModelDiagramGenerator.Format.DOT, outputFilePath, language, customResolver ); - } catch ( final IOException e ) { - throw new CommandException( e ); - } - } -} diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalJavascriptEngine.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalJavascriptEngine.java deleted file mode 100644 index 66f79bb16..000000000 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalJavascriptEngine.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH - * - * See the AUTHORS file(s) distributed with this work for additional - * information regarding authorship. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * SPDX-License-Identifier: MPL-2.0 - */ - -package org.eclipse.esmf.substitution; - -import org.graalvm.polyglot.Context; -import org.graalvm.polyglot.PolyglotException; - -import guru.nidi.graphviz.engine.AbstractJavascriptEngine; -import guru.nidi.graphviz.engine.GraphvizException; -import guru.nidi.graphviz.engine.ResultHandler; - -/** - * This class is a copy of guru.nidi.graphviz.engine.GraalJavascriptEngine, which needs to be instantiated from - * the {@link GraalVmJsGraphvizEngine}, because the original class is not public. - */ -public class GraalJavascriptEngine extends AbstractJavascriptEngine { - private final ResultHandler resultHandler = new ResultHandler(); - private final Context context = Context.newBuilder( "js" ).allowAllAccess( true ).build(); - - GraalJavascriptEngine() { - context.getPolyglotBindings().putMember( "handler", resultHandler ); - eval( "function result(r){ Polyglot.import('handler').setResult(r); }" - + "function error(r){ Polyglot.import('handler').setError(r); }" - + "function log(r){ Polyglot.import('handler').log(r); }" ); - } - - @Override - protected String execute( final String js ) { - try { - eval( js ); - return resultHandler.waitFor(); - } catch ( final PolyglotException e ) { - throw new GraphvizException( "Problem executing javascript", e ); - } - } - - private void eval( final String code ) { - context.eval( "js", code ); - } -} diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalVmJsGraphvizEngine.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalVmJsGraphvizEngine.java deleted file mode 100644 index a798b6063..000000000 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/GraalVmJsGraphvizEngine.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH - * - * See the AUTHORS file(s) distributed with this work for additional - * information regarding authorship. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * SPDX-License-Identifier: MPL-2.0 - */ - -package org.eclipse.esmf.substitution; - -import guru.nidi.graphviz.engine.AbstractJsGraphvizEngine; -import guru.nidi.graphviz.engine.JavascriptEngine; -import guru.nidi.graphviz.engine.MissingDependencyException; - -/** - * Custom graphviz-java {@link JavascriptEngine} implementation based on the GraalVM JavaScript engine - */ -public final class GraalVmJsGraphvizEngine extends AbstractJsGraphvizEngine { - public GraalVmJsGraphvizEngine() { - super( true, GraalVmJsGraphvizEngine::buildEngine ); - super.doInit(); - } - - @SuppressWarnings( "squid:S1166" ) - // Can't retrow exception, because logic (including throwing MissingDependencyException) is adapted from graphivz-java core - private static JavascriptEngine buildEngine() { - try { - return new GraalJavascriptEngine(); - } catch ( final ExceptionInInitializerError | NoClassDefFoundError | IllegalStateException exception ) { - throw new MissingDependencyException( "GraalVM JavaScript engine is not available.", "org.graalvm.js:js" ); - } - } -} diff --git a/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/Target_guru_nidi_graphviz_engine_GraphvizLoader.java b/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/Target_guru_nidi_graphviz_engine_GraphvizLoader.java deleted file mode 100644 index d38afe59c..000000000 --- a/tools/samm-cli/src/main/java/org/eclipse/esmf/substitution/Target_guru_nidi_graphviz_engine_GraphvizLoader.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH - * - * See the AUTHORS file(s) distributed with this work for additional - * information regarding authorship. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * SPDX-License-Identifier: MPL-2.0 - */ - -package org.eclipse.esmf.substitution; - -import com.oracle.svm.core.annotate.Substitute; -import com.oracle.svm.core.annotate.TargetClass; - -import guru.nidi.graphviz.engine.GraphvizLoader; - -/** - * This is a GraalVM substitution class - * for {@link GraphvizLoader}. - * Reason: The
isOnClassPath
method is used to check availability of dependencies at runtime using resource lookup. However, this won't work in - * the native image build, instead, dependencies are wired at build time, so we can provide the relevant information statically. - */ -@TargetClass( GraphvizLoader.class ) -@SuppressWarnings( { - "unused", - "squid:S00101" // Class name uses GraalVM substitution class naming schema, see - // https://github.com/oracle/graal/tree/master/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk -} ) -public final class Target_guru_nidi_graphviz_engine_GraphvizLoader { - @Substitute - static boolean isOnClasspath( final String resource ) { - return switch ( resource ) { - case "org/apache/batik/transcoder/Transcoder.class" -> true; - case "com/kitfox/svg/SVGDiagram.class" -> false; - case "net/arnx/nashorn/lib/PromiseException.class" -> false; - case "org/apache/commons/exec/CommandLine.class" -> false; - case "com/eclipsesource/v8/V8.class" -> false; - default -> Target_guru_nidi_graphviz_engine_GraphvizLoader.class.getClassLoader().getResource( resource ) != null; - }; - } -} diff --git a/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/reflect-config.json b/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/reflect-config.json index 93c618ad3..aa47cf8ce 100644 --- a/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/reflect-config.json +++ b/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/reflect-config.json @@ -1,19 +1,4 @@ [ - { - "name": "guru.nidi.graphviz.attribute.Style", - "allDeclaredFields": true, - "allDeclaredMethods": true - }, - { - "name": "guru.nidi.graphviz.attribute.Shape", - "allDeclaredFields": true, - "allDeclaredMethods": true - }, - { - "name": "guru.nidi.graphviz.engine.ResultHandler", - "allDeclaredFields": true, - "allDeclaredMethods": true - }, { "name": "java.awt.Color", "allDeclaredFields": true, diff --git a/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/resource-config.json b/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/resource-config.json index c89181de3..87fedacf3 100644 --- a/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/resource-config.json +++ b/tools/samm-cli/src/main/resources/META-INF/native-image/org.eclipse.esmf/samm-cli/resource-config.json @@ -17,10 +17,7 @@ {"pattern":"\\Qrdf/dash.ttl\\E"}, {"pattern":"\\Qrdf/shacl.ttl\\E"}, {"pattern":"\\Qrdf/system-triples.ttl\\E"}, - {"pattern":"\\Qrdf/tosh.ttl\\E"}, - {"pattern":"\\Qguru/nidi/graphviz/engine/render.js\\E"}, - {"pattern":"\\QMETA-INF/resources/webjars/viz.js-graphviz-java/2.1.3/viz.js\\E"}, - {"pattern":"\\QMETA-INF/resources/webjars/viz.js-graphviz-java/2.1.3/full.render.js\\E"} + {"pattern":"\\Qrdf/tosh.ttl\\E"} ], "bundles":[{"name":"org.apache.jena.ext.xerces.impl.xpath.regex.message"}] } diff --git a/tools/samm-cli/src/test/java/org/eclipse/esmf/SammCliTest.java b/tools/samm-cli/src/test/java/org/eclipse/esmf/SammCliTest.java index 29509279f..c109afe00 100644 --- a/tools/samm-cli/src/test/java/org/eclipse/esmf/SammCliTest.java +++ b/tools/samm-cli/src/test/java/org/eclipse/esmf/SammCliTest.java @@ -254,55 +254,6 @@ public void testAspectToAasJsonToStdout() { assertThat( contentType( result.stdoutRaw() ) ).isEqualTo( MediaType.text( "plain" ) ); } - @Test - public void testAspectToDotWithDefaultLanguage() { - final File targetFile = outputFile( "output.dot" ); - final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "dot", "-o", - targetFile.getAbsolutePath() ); - assertThat( result.stdout() ).isEmpty(); - assertThat( result.stderr() ).isEmpty(); - assertThat( targetFile ).exists(); - assertThat( targetFile ).content().contains( "digraph AspectModel" ); - } - - @Test - public void testAspectToDotWithGivenLanguage() { - final File targetFile = outputFile( "output.dot" ); - final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "dot", "-o", - targetFile.getAbsolutePath(), "--language", - "en" ); - assertThat( result.stdout() ).isEmpty(); - assertThat( result.stderr() ).isEmpty(); - assertThat( targetFile ).exists(); - assertThat( targetFile ).content().startsWith( "digraph AspectModel" ); - } - - @Test - public void testAspectToDotWithNonExistentLanguage() { - final File targetFile = outputFile( "output.dot" ); - final ExecutionResult result = sammCli.apply( "--disable-color", "aspect", defaultInputFile, "to", "dot", "-o", targetFile.getAbsolutePath(), - "--language", "de" ); - assertThat( result.exitStatus() ).isEqualTo( 1 ); - assertThat( result.stdout() ).isEmpty(); - assertThat( result.stderr() ).contains( "The model does not contain the desired language" ); - assertThat( targetFile ).doesNotExist(); - } - - @Test - public void testAspectToDotToStdout() { - final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "dot" ); - assertThat( result.stdout() ).startsWith( "digraph AspectModel" ); - assertThat( result.stderr() ).isEmpty(); - } - - @Test - public void testAspectToDotWithCustomResolver() { - final ExecutionResult result = sammCli.runAndExpectSuccess( "--disable-color", "aspect", defaultInputFile, "to", "dot", "--custom-resolver", - resolverCommand() ); - assertThat( result.stdout() ).startsWith( "digraph AspectModel" ); - assertThat( result.stderr() ).isEmpty(); - } - @Test public void testAspectToHtmlWithDefaultLanguageToFile() { final File targetFile = outputFile( "output.html" );