There are cases where you may need a tighter integration of MDWorkbench into your Java application. For example, you may build a user interface that let the end-user execute multiple time a code generator. Using a launch configuration, the MDWorkbench project and the input models are loaded on each execution. If the code generator does not alter the input models, you may want to load only once the models used.
MDWorkbench offers APIs to have a finer control on the execution using a subclass of
the interface Evaluable
:
import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.sodius.mdw.core.CoreException; import com.sodius.mdw.core.MDWorkbench; import com.sodius.mdw.core.MDWorkbenchFactory; import com.sodius.mdw.core.eval.Evaluable; import com.sodius.mdw.core.eval.EvaluationContext; import com.sodius.mdw.core.eval.EvaluationManager; import com.sodius.mdw.core.eval.project.Project; import com.sodius.mdw.core.model.Metamodel; import com.sodius.mdw.core.model.Model; public class LaunchSnippet { public static void main(String[] args) throws CoreException { // Create an MDWorkbench engine MDWorkbench mdw = MDWorkbenchFactory.create(); EvaluationManager manager = mdw.getEvaluationManager(); try { // Load a project (.classpath file) File file = new File("c:\\deploy\\myLaunch.classpath"); Project project = manager.loadProject(file); // Load a UML 2.1 XMI model Metamodel metamodel = mdw.getMetamodelManager().getMetamodel("uml21"); String modelUri = args[0]; // Here we expect a model file path in the command line final Model model = metamodel.readModel("XMI", modelUri); // Launch a code generation on the loaded model generate(manager, project, model); } finally { // Shutdown MDWorkbench (to release licenses) mdw.shutdown(); } } private static void generate(EvaluationManager manager, Project project, final Model model) throws CoreException { manager.evaluate(project, new Evaluable() { public void evaluate(EvaluationContext context) throws CoreException { // Build ruleset arguments List ruleSetArgs = new ArrayList(); ruleSetArgs.add(model); // Build rule argument List ruleArgs = Collections.EMPTY_LIST; // Launch the rule evaluation context.evaluateRule("myPackage.myRuleSet", ruleSetArgs, "myRule", ruleArgs); } }, null); } }
Here the MDWorkbench and the UML model are loaded once.
Then we could invoke the generate()
method more than once.