--- /dev/null
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.ui.Activator;\r
+import org.simantics.sysdyn.ui.utils.ModelUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class NewModelHandler extends AbstractHandler {\r
+\r
+ @Override\r
+ public Object execute(ExecutionEvent event) throws ExecutionException {\r
+ \r
+ \r
+ Job job = new Job("Creating System Dynamics Model") {\r
+ @Override\r
+ protected IStatus run(IProgressMonitor monitor) {\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ ModelUtils.createModel(graph);\r
+ }\r
+ });\r
+ return Status.OK_STATUS;\r
+ } catch (DatabaseException e) {\r
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);\r
+ }\r
+ }\r
+ };\r
+ job.setUser(true);\r
+ job.schedule();\r
+\r
+ return null;\r
+ }\r
+\r
+}\r
package org.simantics.sysdyn.ui.utils;\r
\r
+import java.util.UUID;\r
+\r
+import org.simantics.databoard.Bindings;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Template;\r
+import org.simantics.document.DocumentResource;\r
import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ModelingUtils;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.project.ontology.ProjectResource;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.manager.SysdynModel;\r
import org.simantics.sysdyn.manager.SysdynModelManager;\r
import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.ArrayMap;\r
\r
public class ModelUtils {\r
\r
- /**\r
- * Returns the SysdynModel where the given variable is located\r
- * \r
- * @param graph ReadGraph\r
- * @param variable Variable whose model is wanted\r
- * @return SysdynModel where variable is located\r
- * @throws DatabaseException\r
- */\r
- public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException {\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- Resource configuration = graph.getPossibleObject(variable, l0.PartOf);\r
- if(configuration == null)\r
- return null;\r
- SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
- SysdynModel model = sdm.getModel(graph, configuration);\r
- try {\r
- model.update(graph);\r
- } catch (DatabaseException e1) {\r
- e1.printStackTrace();\r
- }\r
- return model;\r
- }\r
+ /**\r
+ * Returns the SysdynModel where the given variable is located\r
+ * \r
+ * @param graph ReadGraph\r
+ * @param variable Variable whose model is wanted\r
+ * @return SysdynModel where variable is located\r
+ * @throws DatabaseException\r
+ */\r
+ public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ Resource configuration = graph.getPossibleObject(variable, l0.PartOf);\r
+ if(configuration == null)\r
+ return null;\r
+ SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
+ SysdynModel model = sdm.getModel(graph, configuration);\r
+ try {\r
+ model.update(graph);\r
+ } catch (DatabaseException e1) {\r
+ e1.printStackTrace();\r
+ }\r
+ return model;\r
+ }\r
+\r
+ public static void createModel(WriteGraph graph) {\r
+ createModelAt(graph, SimanticsUI.getProject().get());\r
+ }\r
+\r
+ public static void createModelAt(WriteGraph g, Resource library) {\r
+ try {\r
+ Layer0 l0 = Layer0.getInstance(g);\r
+ Layer0X L0X = Layer0X.getInstance(g);\r
+ SysdynResource sr = SysdynResource.getInstance(g);\r
+ SpreadsheetResource SHEET = SpreadsheetResource.getInstance(g);\r
+ ModelingUtils mu = new ModelingUtils(g);\r
+ DocumentResource DOC = DocumentResource.getInstance(g);\r
+\r
+ String modelName = NameUtils.findFreshName(g, "Model", library, l0.ConsistsOf, "%s%d");\r
+\r
+ Resource model = g.newResource();\r
+ g.claimLiteral(model, l0.HasName, modelName);\r
+ g.claimLiteral(model, l0.HasLabel, modelName);\r
+ g.claim(model, l0.InstanceOf, sr.SysdynModel);\r
+ g.claim(model, l0.PartOf, SimanticsUI.getProject().get());\r
+\r
+ Resource conf = GraphUtils.create2(g,\r
+ sr.Configuration,\r
+ l0.PartOf, model,\r
+ L0X.IsBaseRealizationOf, model,\r
+ l0.HasName, modelName\r
+ );\r
+\r
+ Resource diagram = g.newResource();\r
+ g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g,\r
+ ArrayMap\r
+ .keys("", "diagram", "name")\r
+ .values(conf, diagram, "Diagrammi")\r
+ );\r
+\r
+ g.claim(model, mu.SIMU.HasConfiguration, conf);\r
+\r
+ Resource book = g.newResource();\r
+ g.claim(book, l0.InstanceOf, null, SHEET.Book);\r
+ g.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING);\r
+ g.claim(conf, l0.ConsistsOf, l0.PartOf, book);\r
+\r
+ SheetUtils.createSheet(g, book, "Sheet1", new String[] { }, new int[] { 50 });\r
+\r
+\r
+\r
+ ModelingResources mr = ModelingResources.getInstance(g);\r
+ // Remove default mapping and add sysdyn mapping\r
+ for(Resource trigger : g.getObjects(diagram, L0X.HasTrigger)) {\r
+ if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) {\r
+ g.deny(diagram, L0X.HasTrigger, trigger);\r
+ }\r
+ }\r
+ Resource mapping = g.newResource();\r
+ g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping);\r
+ g.claim(diagram, L0X.HasTrigger, mapping);\r
+\r
+ Resource report = GraphUtils.create2(g, DOC.Report, DOC.HasDocumentation, "===Report===");\r
+\r
+ GraphUtils.create2(g, mu.SIMU.Experiment,\r
+ l0.HasName, "Experiment",\r
+ l0.HasLabel, "Experiment",\r
+ DOC.HasReportFactory, report,\r
+ l0.PartOf, model);\r
+\r
+ ProjectResource PROJ = ProjectResource.getInstance(g);\r
+ for(Resource dep : g.getObjects(library, l0.IsLinkedTo)) {\r
+ if(g.isInstanceOf(dep, PROJ.NamespaceRequirement)) {\r
+ for(Resource req : g.getObjects(dep, PROJ.RequiresNamespace)) {\r
+ String uri = g.getPossibleValue(req, Bindings.STRING);\r
+ if(uri != null) {\r
+ Resource target = g.getResource(uri);\r
+ if(target != null) {\r
+ g.claim(model, l0.IsLinkedTo, null, target);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ } catch (DatabaseException e ) {\r
+\r
+ }\r
+ }\r
}\r