Skip to content

Commit

Permalink
Use caching for /flexmi2plantuml and /xmi2plantuml
Browse files Browse the repository at this point in the history
  • Loading branch information
agarciadom committed Dec 9, 2024
1 parent f8be3ad commit babff67
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,18 +14,14 @@
public class Xmi2PlantUMLController {
public static final String PATH = "/xmi2plantuml";

@Inject
ModelLoader modelLoader;

@Inject
ModelDiagramRenderer renderer;

@ExecuteOn(TaskExecutors.IO)
@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());
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit babff67

Please sign in to comment.