From: lempinen Date: Wed, 31 Aug 2011 06:05:51 +0000 (+0000) Subject: New SD Model handler X-Git-Tag: simantics-1.5~49 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c39d3eef755f6c5d1a8f77df6fdf341bc9bf1c9f;p=simantics%2Fsysdyn.git New SD Model handler git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21973 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/icons/chart_organisation_add.png b/org.simantics.sysdyn.ui/icons/chart_organisation_add.png new file mode 100644 index 00000000..f0dba4ac Binary files /dev/null and b/org.simantics.sysdyn.ui/icons/chart_organisation_add.png differ diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 0fe3695b..4d985f89 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -556,6 +556,16 @@ + + + + @@ -687,6 +697,10 @@ id="org.simantics.sysdyn.ui.newSpreadSheet" name="New SpreadSheet"> + + @@ -991,6 +1005,10 @@ class="org.simantics.sysdyn.ui.handlers.NewSpreadSheetHandler" commandId="org.simantics.sysdyn.ui.newSpreadSheet"> + + @@ -1424,7 +1442,7 @@ value="plugin_customization.ini"/> + value="icons/sysdyn.png"> diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java new file mode 100644 index 00000000..90778d94 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewModelHandler.java @@ -0,0 +1,45 @@ +package org.simantics.sysdyn.ui.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.sysdyn.ui.utils.ModelUtils; +import org.simantics.ui.SimanticsUI; + +public class NewModelHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + + Job job = new Job("Creating System Dynamics Model") { + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + ModelUtils.createModel(graph); + } + }); + return Status.OK_STATUS; + } catch (DatabaseException e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e); + } + } + }; + job.setUser(true); + job.schedule(); + + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java index 023783d3..b5c30574 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java @@ -1,35 +1,134 @@ package org.simantics.sysdyn.ui.utils; +import java.util.UUID; + +import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.Template; +import org.simantics.document.DocumentResource; import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ModelingUtils; +import org.simantics.operation.Layer0X; +import org.simantics.project.ontology.ProjectResource; +import org.simantics.spreadsheet.resource.SpreadsheetResource; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.ui.SimanticsUI; +import org.simantics.utils.datastructures.ArrayMap; public class ModelUtils { - /** - * Returns the SysdynModel where the given variable is located - * - * @param graph ReadGraph - * @param variable Variable whose model is wanted - * @return SysdynModel where variable is located - * @throws DatabaseException - */ - public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - Resource configuration = graph.getPossibleObject(variable, l0.PartOf); - if(configuration == null) - return null; - SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); - SysdynModel model = sdm.getModel(graph, configuration); - try { - model.update(graph); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - return model; - } + /** + * Returns the SysdynModel where the given variable is located + * + * @param graph ReadGraph + * @param variable Variable whose model is wanted + * @return SysdynModel where variable is located + * @throws DatabaseException + */ + public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource configuration = graph.getPossibleObject(variable, l0.PartOf); + if(configuration == null) + return null; + SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); + SysdynModel model = sdm.getModel(graph, configuration); + try { + model.update(graph); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + return model; + } + + public static void createModel(WriteGraph graph) { + createModelAt(graph, SimanticsUI.getProject().get()); + } + + public static void createModelAt(WriteGraph g, Resource library) { + try { + Layer0 l0 = Layer0.getInstance(g); + Layer0X L0X = Layer0X.getInstance(g); + SysdynResource sr = SysdynResource.getInstance(g); + SpreadsheetResource SHEET = SpreadsheetResource.getInstance(g); + ModelingUtils mu = new ModelingUtils(g); + DocumentResource DOC = DocumentResource.getInstance(g); + + String modelName = NameUtils.findFreshName(g, "Model", library, l0.ConsistsOf, "%s%d"); + + Resource model = g.newResource(); + g.claimLiteral(model, l0.HasName, modelName); + g.claimLiteral(model, l0.HasLabel, modelName); + g.claim(model, l0.InstanceOf, sr.SysdynModel); + g.claim(model, l0.PartOf, SimanticsUI.getProject().get()); + + Resource conf = GraphUtils.create2(g, + sr.Configuration, + l0.PartOf, model, + L0X.IsBaseRealizationOf, model, + l0.HasName, modelName + ); + + Resource diagram = g.newResource(); + g.adapt(sr.ConfigurationDiagramTemplate, Template.class).apply(g, + ArrayMap + .keys("", "diagram", "name") + .values(conf, diagram, "Diagrammi") + ); + + g.claim(model, mu.SIMU.HasConfiguration, conf); + + Resource book = g.newResource(); + g.claim(book, l0.InstanceOf, null, SHEET.Book); + g.addLiteral(book, l0.HasName, l0.NameOf, l0.String, "Book" + UUID.randomUUID().toString(), Bindings.STRING); + g.claim(conf, l0.ConsistsOf, l0.PartOf, book); + + SheetUtils.createSheet(g, book, "Sheet1", new String[] { }, new int[] { 50 }); + + + + ModelingResources mr = ModelingResources.getInstance(g); + // Remove default mapping and add sysdyn mapping + for(Resource trigger : g.getObjects(diagram, L0X.HasTrigger)) { + if(g.isInstanceOf(trigger, mr.DiagramToCompositeMapping)) { + g.deny(diagram, L0X.HasTrigger, trigger); + } + } + Resource mapping = g.newResource(); + g.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping); + g.claim(diagram, L0X.HasTrigger, mapping); + + Resource report = GraphUtils.create2(g, DOC.Report, DOC.HasDocumentation, "===Report==="); + + GraphUtils.create2(g, mu.SIMU.Experiment, + l0.HasName, "Experiment", + l0.HasLabel, "Experiment", + DOC.HasReportFactory, report, + l0.PartOf, model); + + ProjectResource PROJ = ProjectResource.getInstance(g); + for(Resource dep : g.getObjects(library, l0.IsLinkedTo)) { + if(g.isInstanceOf(dep, PROJ.NamespaceRequirement)) { + for(Resource req : g.getObjects(dep, PROJ.RequiresNamespace)) { + String uri = g.getPossibleValue(req, Bindings.STRING); + if(uri != null) { + Resource target = g.getResource(uri); + if(target != null) { + g.claim(model, l0.IsLinkedTo, null, target); + } + } + } + } + } + } catch (DatabaseException e ) { + + } + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java index dcf0887a..f0030c8c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -93,10 +93,10 @@ public class StockExpression extends Expression { } } else { - for(Valve valve : incoming) - b.append("\n + ").append(valve.getName() + range); for(Valve valve : outgoing) b.append("\n - ").append(valve.getName() + range); + for(Valve valve : incoming) + b.append("\n + ").append(valve.getName() + range); } b.append(";\n"); return b.toString();