diff --git a/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/ModelDiagramRenderer.java b/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/ModelDiagramRenderer.java index 61f2859..19b1745 100644 --- a/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/ModelDiagramRenderer.java +++ b/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/ModelDiagramRenderer.java @@ -1,85 +1,100 @@ package org.eclipse.epsilon.labs.playground.fn; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; - import io.micronaut.cache.annotation.Cacheable; -import org.eclipse.epsilon.egl.EglModule; -import org.eclipse.epsilon.eol.execute.context.Variable; -import org.eclipse.epsilon.eol.models.Model; -import org.eclipse.epsilon.labs.playground.execution.ScriptTimeoutTerminator; - import jakarta.inject.Inject; import jakarta.inject.Singleton; import net.sourceforge.plantuml.FileFormat; import net.sourceforge.plantuml.FileFormatOption; import net.sourceforge.plantuml.SourceStringReader; +import org.eclipse.epsilon.egl.EglModule; +import org.eclipse.epsilon.eol.execute.context.Variable; +import org.eclipse.epsilon.eol.models.Model; +import org.eclipse.epsilon.labs.playground.execution.ScriptTimeoutTerminator; import org.eclipse.epsilon.labs.playground.fn.emfatic2plantuml.MetamodelDiagramResponse; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + @Singleton public class ModelDiagramRenderer { - @Inject - ModelLoader modelLoader; - - @Inject - ScriptTimeoutTerminator timeoutTerminator; - - public MetamodelDiagramResponse generateMetamodelDiagram(String emfatic) throws Exception { - MetamodelDiagramResponse response = new MetamodelDiagramResponse(); - response.setMetamodelDiagram(renderPlantUML(emfatic2plantuml(emfatic))); - return response; + @Inject + ModelLoader modelLoader; + + @Inject + ScriptTimeoutTerminator timeoutTerminator; + + public MetamodelDiagramResponse generateMetamodelDiagram(String emfatic) throws Exception { + MetamodelDiagramResponse response = new MetamodelDiagramResponse(); + response.setMetamodelDiagram(renderPlantUML(emfatic2plantuml(emfatic))); + return response; + } + + public ModelDiagramResponse generateModelDiagram(Model model, Variable... variables) throws Exception { + ModelDiagramResponse diag = new ModelDiagramResponse(); + diag.setModelDiagram(renderPlantUML(model2plantuml(model, variables))); + return diag; + } + + @Cacheable("flexmi-to-svg") + public ModelDiagramResponse generateDiagramFromFlexmi(String flexmi, String emfatic) throws Exception { + Model model = modelLoader.getInMemoryFlexmiModel(flexmi, emfatic); + ModelDiagramResponse diag = new ModelDiagramResponse(); + diag.setModelDiagram(renderPlantUML(model2plantuml(model))); + return diag; + } + + @Cacheable("xmi-to-svg") + public ModelDiagramResponse generateDiagramFromXmi(String xmi, String emfatic) throws Exception { + Model model = modelLoader.getInMemoryXmiModel(xmi, emfatic); + ModelDiagramResponse diag = new ModelDiagramResponse(); + diag.setModelDiagram(renderPlantUML(model2plantuml(model))); + return diag; + } + + protected String model2plantuml(Model model, Variable... variables) throws Exception { + EglModule module = new EglModule(); + module.parse(getClass().getResource("/flexmi2plantuml.egl").toURI()); + model.setName("M"); + module.getContext().getModelRepository().addModel(model); + module.getContext().getFrameStack().put(variables); + timeoutTerminator.scheduleScriptTimeout(module); + + try { + return module.execute() + ""; + } finally { + module.getContext().getModelRepository().dispose(); + module.getContext().dispose(); } - - public ModelDiagramResponse generateModelDiagram(Model model, Variable... variables) throws Exception { - EglModule module = new EglModule(); - module.parse(getClass().getResource("/flexmi2plantuml.egl").toURI()); - model.setName("M"); - module.getContext().getModelRepository().addModel(model); - module.getContext().getFrameStack().put(variables); - timeoutTerminator.scheduleScriptTimeout(module); - - try { - String plantUml = module.execute() + ""; - - String output = renderPlantUML(plantUml); - ModelDiagramResponse diag = new ModelDiagramResponse(); - diag.setModelDiagram(output); - - return diag; - } finally { - module.getContext().getModelRepository().dispose(); - module.getContext().dispose(); - } + } + + @Cacheable("emfatic-to-plantuml") + protected String emfatic2plantuml(String emfatic) throws Exception { + Model model = modelLoader.getInMemoryEmfaticModel(emfatic); + model.setName("M"); + + EglModule module = new EglModule(); + module.parse(getClass().getResource("/emfatic2plantuml.egl").toURI()); + module.getContext().getModelRepository().addModel(model); + timeoutTerminator.scheduleScriptTimeout(module); + + try { + return module.execute() + ""; + } finally { + module.getContext().getModelRepository().dispose(); + module.getContext().dispose(); } + } - @Cacheable("emfatic-to-plantuml") - protected String emfatic2plantuml(String emfatic) throws Exception { - Model model = modelLoader.getInMemoryEmfaticModel(emfatic); - model.setName("M"); - - EglModule module = new EglModule(); - module.parse(getClass().getResource("/emfatic2plantuml.egl").toURI()); - module.getContext().getModelRepository().addModel(model); - timeoutTerminator.scheduleScriptTimeout(module); - - try { - return module.execute() + ""; - } finally { - module.getContext().getModelRepository().dispose(); - module.getContext().dispose(); - } - } + @Cacheable("plantuml-to-svg") + protected String renderPlantUML(String plantUml) throws IOException { + SourceStringReader reader = new SourceStringReader(plantUml); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + reader.outputImage(os, new FileFormatOption(FileFormat.SVG)); + os.close(); - @Cacheable("plantuml-to-svg") - protected String renderPlantUML(String plantUml) throws IOException { - SourceStringReader reader = new SourceStringReader(plantUml); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - reader.outputImage(os, new FileFormatOption(FileFormat.SVG)); - os.close(); - - return os.toString(StandardCharsets.UTF_8); - } + return os.toString(StandardCharsets.UTF_8); + } } diff --git a/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/flexmi2plantuml/Flexmi2PlantUMLController.java b/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/flexmi2plantuml/Flexmi2PlantUMLController.java index ea78d0d..5299b38 100644 --- a/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/flexmi2plantuml/Flexmi2PlantUMLController.java +++ b/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/flexmi2plantuml/Flexmi2PlantUMLController.java @@ -1,9 +1,7 @@ package org.eclipse.epsilon.labs.playground.fn.flexmi2plantuml; -import org.eclipse.epsilon.eol.models.Model; import org.eclipse.epsilon.labs.playground.fn.ModelDiagramRenderer; import org.eclipse.epsilon.labs.playground.fn.ModelDiagramResponse; -import org.eclipse.epsilon.labs.playground.fn.ModelLoader; import io.micronaut.http.annotation.Body; import io.micronaut.http.annotation.Controller; @@ -16,18 +14,14 @@ public class Flexmi2PlantUMLController { public static final String PATH = "/flexmi2plantuml"; - @Inject - ModelLoader modelLoader; - @Inject ModelDiagramRenderer renderer; @ExecuteOn(TaskExecutors.IO) - @Post("/") + @Post public ModelDiagramResponse convert(@Body Flexmi2PlantUMLRequest request) { try { - Model model = modelLoader.getInMemoryFlexmiModel(request.getFlexmi(), request.getEmfatic()); - return renderer.generateModelDiagram(model); + return renderer.generateDiagramFromFlexmi(request.getFlexmi(), request.getEmfatic()); } catch (Throwable e) { var response = new ModelDiagramResponse(); response.setError(e.getMessage()); diff --git a/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/xmi2plantuml/Xmi2PlantUMLController.java b/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/xmi2plantuml/Xmi2PlantUMLController.java index 6486265..ab36bd8 100644 --- a/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/xmi2plantuml/Xmi2PlantUMLController.java +++ b/core/src/main/java/org/eclipse/epsilon/labs/playground/fn/xmi2plantuml/Xmi2PlantUMLController.java @@ -1,9 +1,7 @@ package org.eclipse.epsilon.labs.playground.fn.xmi2plantuml; -import org.eclipse.epsilon.eol.models.Model; import org.eclipse.epsilon.labs.playground.fn.ModelDiagramRenderer; import org.eclipse.epsilon.labs.playground.fn.ModelDiagramResponse; -import org.eclipse.epsilon.labs.playground.fn.ModelLoader; import io.micronaut.http.annotation.Body; import io.micronaut.http.annotation.Controller; @@ -16,9 +14,6 @@ public class Xmi2PlantUMLController { public static final String PATH = "/xmi2plantuml"; - @Inject - ModelLoader modelLoader; - @Inject ModelDiagramRenderer renderer; @@ -26,8 +21,7 @@ public class Xmi2PlantUMLController { @Post("/") public ModelDiagramResponse convert(@Body Xmi2PlantUMLRequest request) { try { - Model model = modelLoader.getInMemoryXmiModel(request.getXmi(), request.getEmfatic()); - return renderer.generateModelDiagram(model); + return renderer.generateDiagramFromXmi(request.getXmi(), request.getEmfatic()); } catch (Throwable e) { var response = new ModelDiagramResponse(); response.setError(e.getMessage()); diff --git a/core/src/main/resources/application.properties b/core/src/main/resources/application.properties index e1bde90..9d6ca54 100644 --- a/core/src/main/resources/application.properties +++ b/core/src/main/resources/application.properties @@ -8,7 +8,9 @@ micronaut.openapi.generator.extensions.enabled=true # Size of the cache for the diagram rendering steps micronaut.caches.emfatic-to-plantuml.maximum-size=20 +micronaut.caches.flexmi-to-svg.maximum-size=20 micronaut.caches.plantuml-to-svg.maximum-size=20 +micronaut.caches.xmi-to-svg.maximum-size=20 # Timeout for all Epsilon scripts running in the Playground playground.timeout.millis=60000 \ No newline at end of file