Skip to content

Commit

Permalink
Use caching for /emfatic2plantuml
Browse files Browse the repository at this point in the history
  • Loading branch information
agarciadom committed Dec 9, 2024
1 parent 570e7bd commit f8be3ad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ bin/
.classpath
.factorypath
core/xmi.xmi
standalone-server/shorturl
standalone-server/shorturl
/standalone-server/xmi.xmi
4 changes: 4 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ dependencies {
annotationProcessor(mn.micronaut.openapi)
compileOnly(mn.micronaut.openapi.annotations)

// Caching
implementation(mn.micronaut.cache.core)
implementation(mn.micronaut.cache.caffeine)

// For testing the functions through the declarative HTTP client
testImplementation(mn.micronaut.http.server.netty)
testImplementation(mn.micronaut.http.client)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.eclipse.epsilon.labs.playground.fn;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
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;
Expand All @@ -13,13 +15,23 @@
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import org.eclipse.epsilon.labs.playground.fn.emfatic2plantuml.MetamodelDiagramResponse;

@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;
}

public ModelDiagramResponse generateModelDiagram(Model model, Variable... variables) throws Exception {
EglModule module = new EglModule();
module.parse(getClass().getResource("/flexmi2plantuml.egl").toURI());
Expand All @@ -31,12 +43,7 @@ public ModelDiagramResponse generateModelDiagram(Model model, Variable... variab
try {
String plantUml = module.execute() + "";

SourceStringReader reader = new SourceStringReader(plantUml);
ByteArrayOutputStream os = new ByteArrayOutputStream();
reader.outputImage(os, new FileFormatOption(FileFormat.SVG));
os.close();

String output = new String(os.toByteArray(), Charset.forName("UTF-8"));
String output = renderPlantUML(plantUml);
ModelDiagramResponse diag = new ModelDiagramResponse();
diag.setModelDiagram(output);

Expand All @@ -47,4 +54,32 @@ public ModelDiagramResponse generateModelDiagram(Model model, Variable... variab
}
}

@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();

return os.toString(StandardCharsets.UTF_8);
}

}
Original file line number Diff line number Diff line change
@@ -1,66 +1,31 @@
package org.eclipse.epsilon.labs.playground.fn.emfatic2plantuml;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

import org.eclipse.epsilon.egl.EglModule;
import org.eclipse.epsilon.labs.playground.execution.ScriptTimeoutTerminator;
import org.eclipse.epsilon.labs.playground.fn.ModelLoader;

import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import jakarta.inject.Inject;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
import org.eclipse.epsilon.labs.playground.fn.ModelDiagramRenderer;

@Controller(Emfatic2PlantUMLController.PATH)
public class Emfatic2PlantUMLController {
public static final String PATH = "/emfatic2plantuml";

@Inject
ScriptTimeoutTerminator timeoutTerminator;

@Inject
ModelLoader loader;
ModelDiagramRenderer renderer;

@ExecuteOn(TaskExecutors.IO)
@Post("/")
@Post
public MetamodelDiagramResponse render(@Body Emfatic2PlantUMLRequest request) {
var response = new MetamodelDiagramResponse();

try {
String plantuml = run(request.getEmfatic());
response.setMetamodelDiagram(plantuml);
return renderer.generateMetamodelDiagram(request.getEmfatic());
} catch (Throwable e) {
var response = new MetamodelDiagramResponse();
response.setError(e.getMessage());
response.setOutput(e.getMessage());
return response;
}

return response;
}

protected String run(String emfatic) throws Exception {
EglModule module = new EglModule();
module.parse(getClass().getResource("/emfatic2plantuml.egl").toURI());
module.getContext().getModelRepository().addModel(loader.getInMemoryEmfaticModel(emfatic));
timeoutTerminator.scheduleScriptTimeout(module);

try {
String plantUml = module.execute() + "";

SourceStringReader reader = new SourceStringReader(plantUml);
ByteArrayOutputStream os = new ByteArrayOutputStream();
reader.outputImage(os, new FileFormatOption(FileFormat.SVG));
os.close();

return new String(os.toByteArray(), Charset.forName("UTF-8"));
} finally {
module.getContext().getModelRepository().dispose();
module.getContext().dispose();
}
}
}
4 changes: 4 additions & 0 deletions core/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ micronaut.server.max-request-size=100KB

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.plantuml-to-svg.maximum-size=20

# Timeout for all Epsilon scripts running in the Playground
playground.timeout.millis=60000

0 comments on commit f8be3ad

Please sign in to comment.