-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use caching for /flexmi2plantuml and /xmi2plantuml
- Loading branch information
1 parent
f8be3ad
commit babff67
Showing
4 changed files
with
86 additions
and
81 deletions.
There are no files selected for viewing
147 changes: 81 additions & 66 deletions
147
core/src/main/java/org/eclipse/epsilon/labs/playground/fn/ModelDiagramRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters