From bd55fdab5415999c04b49d4676fc08ec5071a09b Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 15 Apr 2011 08:35:22 +0000 Subject: [PATCH] Continuing with the variable change git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20505 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/META-INF/MANIFEST.MF | 3 +- org.simantics.sysdyn.ui/adapters.xml | 2 +- org.simantics.sysdyn.ui/plugin.xml | 279 +++++++++++++++--- .../sysdyn/ui/browser/BrowserSelection.java | 21 +- .../sysdyn/ui/browser/SysdynBrowser.java | 6 +- .../sysdyn/ui/browser/SysdynModelBrowser.java | 27 ++ .../contributions/AbstractNodeImager.java | 10 +- .../browser/contributions/Configuration.java | 55 ++-- .../ui/browser/contributions/Model.java | 5 +- .../ui/browser/nodes/ConfigurationNode.java | 9 +- .../ui/browser/nodes/EnumerationNode.java | 15 +- .../sysdyn/ui/browser/nodes/InputNode.java | 9 +- .../sysdyn/ui/browser/nodes/ModuleNode.java | 9 +- .../browser/nodes/SimulationResultNode.java | 2 +- .../sysdyn/ui/browser/nodes/VariableNode.java | 90 +----- .../editor/participant/PointerInteractor.java | 6 +- .../sysdyn/ui/elements2/InputFactory.java | 32 +- .../ui/handlers/ExportModelHandler.java | 4 +- .../ui/handlers/ImportModelHandler.java | 29 +- .../sysdyn/ui/handlers/ShowModuleHandler.java | 51 +++- .../ResourceSelectionProcessor.java | 12 + .../SysdynDatasetSelectionListener.java | 25 +- .../sysdyn/adapter/ChildVariable.java | 19 ++ .../sysdyn/adapter/ChildVariableAdapter.java | 11 +- .../sysdyn/adapter/HistoryVariable.java | 73 +++++ .../sysdyn/adapter/ModuleVariable.java | 61 ++++ .../sysdyn/modelica/ModelicaWriter.java | 78 +---- 27 files changed, 640 insertions(+), 303 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynModelBrowser.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ModuleVariable.java diff --git a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF index f55f0da0..70727806 100644 --- a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF @@ -38,7 +38,8 @@ Require-Bundle: org.simantics.layer0.utils;bundle-version="0.6.2", org.simantics.layer0;bundle-version="1.0.0", org.simantics.diagram.ontology;bundle-version="1.0.0", org.simantics.graph;bundle-version="1.0.2", - org.simantics.graph.db;bundle-version="1.0.0" + org.simantics.graph.db;bundle-version="1.0.0", + org.simantics.structural.ui;bundle-version="1.1.1" Bundle-Activator: org.simantics.sysdyn.ui.Activator Bundle-ActivationPolicy: lazy Export-Package: org.simantics.sysdyn.ui.browser.nodes diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 155a6f4e..b3e7e606 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -10,5 +10,5 @@ VTT Technical Research Centre of Finland - initial API and implementation --> - none 0 fill 1 + none 0 fill 1 \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index b1e3e436..c64518a6 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -89,6 +89,17 @@ Variable browser for Sysdyn. + + + Structural model browser view for Sysdyn. + + @@ -304,6 +315,52 @@ id="org.simantics.sysdyn.ui.browser.newFunction" label="Function" style="push"> + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - @@ -369,30 +411,86 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -503,11 +601,11 @@ + name="Export model"> + name="Import model"> + + + + + + + + @@ -593,6 +707,14 @@ args="org.simantics.sysdyn.ui.properties.widgets.externalFiles.ExternalFileNode" property="org.simantics.sysdyn.ui.nodeClass"> + + + + @@ -620,6 +742,14 @@ args="org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode" property="org.simantics.sysdyn.ui.nodeClass"> + + + + @@ -672,6 +802,56 @@ class="org.simantics.sysdyn.ui.handlers.ExportExternalFunctionFilesHandler" commandId="org.simantics.sysdyn.ui.exportExternalFunctionFile"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -706,7 +886,11 @@ preference="2.0"> + + @@ -750,6 +934,13 @@ preference="2.0"> + + + + @@ -799,6 +990,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.FunctionsLabeler" preference="2.0"> + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java index b891e2ad..cfd31c81 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java @@ -20,17 +20,16 @@ import org.simantics.sysdyn.ui.browser.nodes.VariableNode; public class BrowserSelection implements IAdaptable, ISelection { private Resource resource; - private Resource model; - private String URI; private Object originalInput; private Variable variable; - public BrowserSelection(Object originalInput, VariableNode vn) { + public BrowserSelection(Object originalInput, VariableNode vn) { this.originalInput = originalInput; - this.resource = vn.getResource(); - this.URI = vn.getURI(); - this.model = vn.getModel(); - this.variable = vn.getVariable(); + this.resource = vn.data; + Variable variable = vn.getVariable(); + if(variable != null) { + this.variable = variable; + } } @SuppressWarnings("rawtypes") @@ -48,14 +47,6 @@ public class BrowserSelection implements IAdaptable, ISelection { } return null; } - - public Resource getModel() { - return this.model; - } - - public String getURI() { - return this.URI; - } @Override public boolean isEmpty() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java index 8edb5a7b..000a3b1b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java @@ -26,8 +26,8 @@ import org.simantics.browsing.ui.swt.GraphExplorerFactory; import org.simantics.browsing.ui.swt.GraphExplorerView; import org.simantics.browsing.ui.swt.IContextMenuInitializer; import org.simantics.browsing.ui.swt.IPropertyPage; -import org.simantics.db.Resource; import org.simantics.db.layer0.SelectionHints; +import org.simantics.db.layer0.variable.Variable; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.browser.nodes.VariableNode; import org.simantics.sysdyn.ui.properties.SysdynPropertyPage; @@ -49,9 +49,9 @@ public class SysdynBrowser extends GraphExplorerView { NodeContext ctx = (NodeContext)objects[i]; @SuppressWarnings("unchecked") - VariableNode vn = (VariableNode) ctx.getAdapter(VariableNode.class); + VariableNode vn = (VariableNode) ctx.getAdapter(VariableNode.class); IHintContext context; - if(vn != null && vn.getResource() != null) { + if(vn != null && vn.getVariable() != null) { context = new AdaptableHintContext(SelectionHints.KEY_MAIN, SelectionHints.KEY_SELECTION_PROPERTY); context.setHint(SelectionHints.KEY_MAIN, new BrowserSelection(objects[i], vn)); context.setHint(SelectionHints.KEY_SELECTION_PROPERTY, vn.getVariable()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynModelBrowser.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynModelBrowser.java new file mode 100644 index 00000000..5d77dd0c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynModelBrowser.java @@ -0,0 +1,27 @@ +package org.simantics.sysdyn.ui.browser; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.simantics.browsing.ui.swt.IPropertyPage; +import org.simantics.structural.ui.modelBrowser.ModelBrowser; +import org.simantics.sysdyn.ui.properties.SysdynPropertyPage; + +public class SysdynModelBrowser extends ModelBrowser { + + final private Set browseContexts = new HashSet(Arrays.asList("http://www.simantics.org/Sysdyn-1.0/ModelBrowser", "http://www.simantics.org/Operating-1.0/Browser", "http://www.simantics.org/Image-1.0/Browser")); + + @Override + protected IPropertyPage getPropertyPage() { + return new SysdynPropertyPage(getSite(), Collections.singleton("http://www.simantics.org/Sysdyn-1.0/ModelBrowser")); + } + + @Override + protected Set getBrowseContexts() { + return browseContexts; + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java index 82738b66..d00e9e96 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java @@ -21,12 +21,14 @@ import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder; +import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode; import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder; import org.simantics.sysdyn.ui.browser.nodes.InputNode; import org.simantics.sysdyn.ui.browser.nodes.ModelNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode; import org.simantics.sysdyn.ui.browser.nodes.ModulesNode; +import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode; import org.simantics.sysdyn.ui.browser.nodes.VariableNode; public class AbstractNodeImager extends ImagerContributor> { @@ -35,8 +37,12 @@ public class AbstractNodeImager extends ImagerContributor public ImageDescriptor getDescriptor(ReadGraph graph, AbstractNode node) throws DatabaseException { String image = null; - if (node instanceof ExperimentsFolder || - node instanceof ModulesNode || node instanceof FunctionsFolder) + if (node instanceof SharedFunctionLibraryNode) + image = "icons/folder_link.png"; + else if (node instanceof ExperimentsFolder || + node instanceof ModulesNode || + node instanceof FunctionsFolder || + node instanceof FunctionLibraryNode) image = "icons/folder.png"; else if (node instanceof ModuleTypeNode || node instanceof ModuleNode || node instanceof ConfigurationNode) image = "icons/bricks.png"; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java index 7baf1048..d92b3073 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java @@ -19,7 +19,8 @@ import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributo import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; import org.simantics.sysdyn.ui.browser.nodes.EnumerationNode; @@ -31,38 +32,48 @@ public class Configuration extends ViewpointContributor getContribution(ReadGraph graph, ConfigurationNode configuration) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); ArrayList> result = new ArrayList>(); - if (!graph.isInheritedFrom(graph.getSingleObject(configuration.data, l0.InstanceOf), l0.Realization)) + Variable variable = configuration.getVariable(); + + if (variable == null) { return result; + } SysdynResource sr = SysdynResource.getInstance(graph); - ArrayList variables = new ArrayList(); - ArrayList inputs = new ArrayList(); - ArrayList modules = new ArrayList(); - ArrayList enumerations = new ArrayList(); - - for(Resource r : graph.getObjects(configuration.data, l0.ConsistsOf)) { - Resource represents = graph.getSingleObject(r, l0.Represents); + ArrayList variables = new ArrayList(); + ArrayList inputs = new ArrayList(); + ArrayList modules = new ArrayList(); + ArrayList enumerations = new ArrayList(); + + for(Variable child : variable.browseChildren(graph)) { + Resource represents = (Resource)child.getPropertyValue(graph, Variables.REPRESENTS); if(graph.isInstanceOf(represents, sr.IndependentVariable)) { - variables.add(r); + variables.add(child); } else if (graph.isInstanceOf(represents, sr.Input)) { - inputs.add(r); + inputs.add(child); } else if (graph.isInstanceOf(represents, sr.Module)) { - modules.add(r); + modules.add(child); } else if (graph.isInstanceOf(represents, sr.Enumeration)) { - enumerations.add(r); + enumerations.add(child); } } - for (Resource r : variables) - result.add(new VariableNode(r)); - for (Resource r : inputs) - result.add(new InputNode(r)); - for (Resource r : modules) - result.add(new ModuleNode(r)); - for (Resource r : enumerations) - result.add(new EnumerationNode(r)); + for (Variable v : variables) { + Resource represents = (Resource)v.getPropertyValue(graph, Variables.REPRESENTS); + result.add(new VariableNode(v, represents)); + } + for (Variable v : inputs) { + Resource represents = (Resource)v.getPropertyValue(graph, Variables.REPRESENTS); + result.add(new InputNode(v, represents)); + } + for (Variable v : modules) { + Resource represents = (Resource)v.getPropertyValue(graph, Variables.REPRESENTS); + result.add(new ModuleNode(v, represents)); + } + for (Variable v : enumerations) { + Resource represents = (Resource)v.getPropertyValue(graph, Variables.REPRESENTS); + result.add(new EnumerationNode(v, represents)); + } return result; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java index 71db1e0b..13333428 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java @@ -20,6 +20,8 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleObject; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder; @@ -37,7 +39,8 @@ public class Model extends ViewpointContributor { Layer0 l0 = Layer0.getInstance(graph); Resource baseRealization = graph.syncRequest(new PossibleObject(model.data, l0.HasBaseRealization)); if (baseRealization != null) { - result.add(new ConfigurationNode(baseRealization)); + Variable variable = Variables.getVariable(graph, graph.getURI(baseRealization)); + result.add(new ConfigurationNode(variable, baseRealization)); } result.add(new ExperimentsFolder(model.data)); result.add(new ModulesNode(model.data)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ConfigurationNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ConfigurationNode.java index 84b731b0..4b614428 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ConfigurationNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ConfigurationNode.java @@ -13,11 +13,16 @@ package org.simantics.sysdyn.ui.browser.nodes; import org.simantics.browsing.ui.common.node.IDeletable; import org.simantics.db.Resource; +import org.simantics.db.layer0.variable.Variable; public class ConfigurationNode extends VariableNode implements IDeletable { - public ConfigurationNode(final Resource resource) { - super(resource); + public ConfigurationNode(Resource resource) { + super(resource); + } + + public ConfigurationNode(Variable variable, Resource represents) { + super(variable, represents); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/EnumerationNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/EnumerationNode.java index 1d296b62..b570ff86 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/EnumerationNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/EnumerationNode.java @@ -8,6 +8,7 @@ import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; @@ -18,6 +19,10 @@ public class EnumerationNode extends VariableNode implements IDeletabl public EnumerationNode(Resource resource) { super(resource); } + + public EnumerationNode(Variable variable, Resource represents) { + super(variable, represents); + } @Override public void delete() throws DeleteException { @@ -27,17 +32,17 @@ public class EnumerationNode extends VariableNode implements IDeletabl public void perform(WriteGraph graph) throws DatabaseException, CancelTransactionException { Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); - graph.deny(resource, l0.PartOf); - for(Resource redeclaration : graph.getObjects(resource, sr.ReplacedEnumeration_Inverse)) { + graph.deny(data, l0.PartOf); + for(Resource redeclaration : graph.getObjects(data, sr.ReplacedEnumeration_Inverse)) { graph.deny(redeclaration, sr.HasRedeclaration_Inverse); } - for(Resource redeclaration : graph.getObjects(resource, sr.ReplacingEnumeration_Inverse)) { + for(Resource redeclaration : graph.getObjects(data, sr.ReplacingEnumeration_Inverse)) { graph.deny(redeclaration, sr.HasRedeclaration_Inverse); } - for(Resource list : OrderedSetUtils.getOwnerLists(graph, resource, sr.ArrayIndexes)) { - OrderedSetUtils.remove(graph, list, resource); + for(Resource list : OrderedSetUtils.getOwnerLists(graph, data, sr.ArrayIndexes)) { + OrderedSetUtils.remove(graph, list, data); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/InputNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/InputNode.java index c4f0952e..ca731e92 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/InputNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/InputNode.java @@ -12,10 +12,15 @@ package org.simantics.sysdyn.ui.browser.nodes; import org.simantics.db.Resource; +import org.simantics.db.layer0.variable.Variable; public class InputNode extends VariableNode { - public InputNode(Resource resource) { - super(resource); + public InputNode(Resource resource) { + super(resource); + } + + public InputNode(Variable variable, Resource represents) { + super(variable, represents); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java index ba71488d..cf69782a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java @@ -12,10 +12,15 @@ package org.simantics.sysdyn.ui.browser.nodes; import org.simantics.db.Resource; +import org.simantics.db.layer0.variable.Variable; public class ModuleNode extends ConfigurationNode { - public ModuleNode(Resource resource) { - super(resource); + public ModuleNode(Resource resource) { + super(resource); + } + + public ModuleNode(Variable variable, Resource represents) { + super(variable, represents); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultNode.java index 097746c3..4c56fe40 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultNode.java @@ -83,7 +83,7 @@ public class SimulationResultNode extends AbstractNode implements IDo File file = new File(path); file.delete(); File parent = file.getParentFile(); - if(parent.listFiles().length == 0) + if(parent.listFiles() != null && parent.listFiles().length == 0) parent.delete(); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/VariableNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/VariableNode.java index 04fb3bc4..cada925b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/VariableNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/VariableNode.java @@ -12,107 +12,23 @@ package org.simantics.sysdyn.ui.browser.nodes; import org.simantics.browsing.ui.common.node.AbstractNode; -import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; -import org.simantics.db.request.Read; -import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.ui.SimanticsUI; public class VariableNode extends AbstractNode { - Resource model = null; - Resource resource; Variable variable; public VariableNode(Resource resource) { super(resource); - - SimanticsUI.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - - variable = graph.adapt(data, Variable.class); - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - VariableNode.this.resource = graph.getPossibleObject(data, l0.Represents); - - if(VariableNode.this.resource == null) - return; - - // don't search for a parent if the resource is not part of a realization - if(!graph.isInheritedFrom(graph.getSingleObject(data, l0.InstanceOf), l0.Realization)) - return; - - // search for the model parent - Resource parent = graph.getSingleObject(data, l0.PartOf); - while (!graph.isInstanceOf(parent, sr.SysdynModel)) { - parent = graph.getSingleObject(parent, l0.PartOf); - } - model = parent; - } - }); } - public String getURI() { - - try { - return SimanticsUI.getSession().syncRequest(new Read() { - - @Override - public String perform(ReadGraph graph) throws DatabaseException { - return getURI(graph); - } - - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - return null; - } - - public String getURI(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - String uri = getName(graph); - - // don't search for a parent if the resource is not part of a realization - if(!graph.isInheritedFrom(graph.getSingleObject(data, l0.InstanceOf), l0.Realization)) - return null; - - // search for the model parent and build the uri - Resource parent = graph.getSingleObject(data, l0.PartOf); - while (!graph.isInstanceOf(parent, sr.SysdynModel)) { - uri = graph.getRelatedValue(parent, l0.HasName) + "/" + uri; - parent = graph.getSingleObject(parent, l0.PartOf); - } - // cut out the realization name - uri = uri.substring(uri.indexOf("/") + 1); - return uri; + public VariableNode(Variable variable, Resource represents) { + super(represents); + this.variable = variable; } - public Resource getModel() { - return model; - } - public Variable getVariable() { return variable; } - - public String getName(ReadGraph graph) throws DatabaseException { - return (String)graph.getRelatedValue(data, Layer0.getInstance(graph).HasName); - } - - public Resource getParent(ReadGraph graph) throws DatabaseException { - return graph.getSingleObject(data, Layer0.getInstance(graph).PartOf); - } - - public Resource getResource() { - return resource; - } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java index dbaa4014..99ed3fb1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java @@ -18,6 +18,7 @@ import java.util.List; import org.simantics.diagram.participant.ConnectTool2; import org.simantics.g2d.canvas.Hints; +import org.simantics.g2d.canvas.IToolMode; import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; import org.simantics.g2d.canvas.impl.DependencyReflection.Reference; import org.simantics.g2d.connection.IConnectionAdvisor; @@ -77,7 +78,10 @@ public class PointerInteractor extends org.simantics.g2d.diagram.participant.poi @EventHandler(priority = TOOL_PRIORITY) public boolean handlePress(MouseButtonPressedEvent me) { if (!connects()) return false; - if (getHint(Hints.KEY_TOOL) != Hints.POINTERTOOL) return false; + + IToolMode mode = getHint(Hints.KEY_TOOL); + + if (mode != Hints.POINTERTOOL && mode != Hints.CONNECTTOOL) return false; assertDependencies(); Point2D curCanvasPos = util.controlToCanvas(me.controlPosition, null); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java index 09750e4e..2812961b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java @@ -23,6 +23,8 @@ import java.util.Collection; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.diagram.elements.TextNode; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.diagram.ui.DiagramModelHints; @@ -52,6 +54,10 @@ import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.adapter.ChildVariable; +import org.simantics.sysdyn.adapter.ConfigurationVariable; +import org.simantics.sysdyn.adapter.HistoryVariable; +import org.simantics.sysdyn.adapter.RunVariable; import org.simantics.utils.datastructures.hints.IHintContext.Key; public class InputFactory extends SysdynElementFactory { @@ -119,25 +125,25 @@ public class InputFactory extends SysdynElementFactory { } else { Resource runtime = diagram.getHint((DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE)); DiagramResource dr = DiagramResource.getInstance(graph); - String variable = (String)graph.getRelatedValue(runtime, dr.HasVariable); - Resource moduleRepresentation = graph.getPossibleResource(variable); - if(moduleRepresentation != null) - module = graph.getPossibleObject(moduleRepresentation, l0.Represents); + String variable = (String)graph.getPossibleRelatedValue(runtime, dr.HasVariable); + if(variable == null) + return; + Variable v = Variables.getVariable(graph, variable); + module = graph.getResource(v.getURI(graph)); if(module != null) for(Resource dep : graph.getObjects(module, sr.IsHeadOf)) { Resource reference = graph.getPossibleObject(dep, sr.RefersTo); if(reference!= null && reference.equals(component)) { refersTo = graph.getSingleObject(dep, sr.HasTail); referenceName = (String) graph.getPossibleRelatedValue(refersTo, l0.HasName); - moduleRepresentation = graph.getPossibleResource(variable.substring(0, variable.lastIndexOf("/"))); - Resource parent = graph.getPossibleObject(moduleRepresentation, l0.PartOf); - String parentURI = graph.getURI(parent); - String modelURI = diagram.getHint((DiagramModelHints.KEY_DIAGRAM_MODEL_URI)); - module = graph.getPossibleObject(moduleRepresentation, l0.Represents); - if(parentURI.equals(modelURI) || module == null) - moduleName = ""; - else { - moduleName = (String) graph.getPossibleRelatedValue(module, l0.HasName); + Variable parent = null; + if(v instanceof HistoryVariable) + parent = ((HistoryVariable)v).getParent(graph); + else if(v instanceof ChildVariable) + parent = ((ChildVariable)v).getParent(graph); + if(parent != null && !(parent instanceof RunVariable) && !(parent instanceof ConfigurationVariable)) { + String parentURI = parent.getURI(graph); + moduleName = (String) graph.getPossibleRelatedValue(graph.getResource(parentURI), l0.HasName); } break; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java index 51ec9466..5b4d5cc7 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportModelHandler.java @@ -32,7 +32,7 @@ import org.simantics.db.Resource; import org.simantics.db.common.primitiverequest.PossibleRelatedValue; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.TransferableGraphRequest; +import org.simantics.db.layer0.util.TransferableGraphRequest2; import org.simantics.db.request.Read; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; @@ -89,7 +89,7 @@ public class ExportModelHandler extends AbstractHandler { String name = graph.syncRequest(new PossibleRelatedValue(model, l0.HasName, Bindings.STRING )); ArrayList> roots = new ArrayList>(); roots.add(Pair.make(model, name)); - TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest(roots, model)); + TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, model)); try { Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java index 027c4a9a..02ee621a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportModelHandler.java @@ -33,6 +33,8 @@ import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.sysdyn.ui.utils.OldTransferableGraph1; import org.simantics.ui.SimanticsUI; public class ImportModelHandler extends AbstractHandler { @@ -57,8 +59,15 @@ public class ImportModelHandler extends AbstractHandler { tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); } catch (RuntimeBindingConstructionException e) { e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException e) { + try { + OldTransferableGraph1 otg = (OldTransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(OldTransferableGraph1.class)); + tg = new TransferableGraph1(otg.resourceCount, otg.identities, otg.statements, otg.values); + } catch (RuntimeBindingConstructionException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } } if(tg == null) return null; @@ -67,13 +76,15 @@ public class ImportModelHandler extends AbstractHandler { DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project); DefaultPasteHandler.defaultExecute(tg, SimanticsUI.getProject().get(), ia); - // FIXME: the links should be exported and imported automatically final Resource root = ia.getRoot(); SimanticsUI.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); + updateOldConfiguration(graph, root); + + // FIXME: the links should be exported and imported automatically Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.0"); Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.0"); if(!graph.hasStatement(root, l0.IsLinkedTo, sysdyn)) @@ -90,5 +101,17 @@ public class ImportModelHandler extends AbstractHandler { return null; } + + private void updateOldConfiguration(WriteGraph graph, Resource model) { + Layer0 l0 = Layer0.getInstance(graph); + try { + Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration); + if(configuration != null && !graph.hasStatement(configuration, l0.IsBaseRealizationOf, model)) + graph.claim(configuration, l0.IsBaseRealizationOf, model); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java index 4da75f82..89e485e0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ShowModuleHandler.java @@ -26,27 +26,19 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.layer0.Layer0; import org.simantics.modeling.ComponentUtils; import org.simantics.modeling.ModelingResources; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.adapter.ChildVariable; +import org.simantics.sysdyn.adapter.HistoryVariable; import org.simantics.sysdyn.ui.editor.DiagramViewer; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.ResourceAdaptionUtils; import org.simantics.ui.workbench.ResourceEditorInput2; import org.simantics.utils.ui.workbench.WorkbenchUtils; -/** - * Style Edit - * - * TODO : should we have extension point for expanding styles? - * TODO : default ColorChooser is not suitable for this task - * TODO : how to store MetricsFormat template list - * - * @author Marko Luukkainen - * - */ + public class ShowModuleHandler extends AbstractHandler { private static final String EDITOR_ID = "org.simantics.sysdyn.ui.diagramViewer"; @@ -66,14 +58,12 @@ public class ShowModuleHandler extends AbstractHandler { DiagramResource dr = DiagramResource.getInstance(graph); Resource runtime = viewer.getRuntime(); String uri = graph.getPossibleRelatedValue(runtime, dr.HasVariable, Bindings.STRING); - String currentRVI = graph.getPossibleRelatedValue(runtime, dr.HasRVI, Bindings.STRING); - Variable variable = Variables.getVariable(graph, uri); - Resource model = Variables.getModel(graph, variable); + Variable parent = Variables.getVariable(graph, uri); + Resource model = Variables.getModel(graph, parent); final String modelURI = graph.getURI(model); for(Resource element : resources) { - Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); StructuralResource2 st = StructuralResource2.getInstance(graph); @@ -81,11 +71,38 @@ public class ShowModuleHandler extends AbstractHandler { final Resource component = graph.getPossibleObject(element, mr.ElementToComponent); final Resource componentType = graph.getPossibleType(component, sr.Module); final Resource configuration = graph.getPossibleObject(componentType, st.IsDefinedBy); + final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, configuration); + + Variable variable = parent.browsePossible(graph, component); + String rvi = ""; + if(variable instanceof HistoryVariable) + rvi = "/" + ((HistoryVariable)variable).getRvi(graph); + else if (variable instanceof ChildVariable) + rvi = "/" + ((ChildVariable)variable).getRvi(graph); + + final String finalRvi = rvi; + + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + try { + String editorId = EDITOR_ID; + WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, diagram, modelURI, finalRvi)); + } catch (PartInitException e) { + e.printStackTrace(); + } + } + }); + + /* + + final Resource diagram = ComponentUtils.getPossibleCompositeDiagram(graph, configuration); if(diagram == null) return; - String finalRvi = currentRVI + "/" + graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING); + String finalRvi = uri + "/" + graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING); // This is ugly but it has to be done like this because WorkModel example has too PartOfs Resource rootLibrary = graph.getPossibleObject(model, l0.PartOf); @@ -114,6 +131,7 @@ public class ShowModuleHandler extends AbstractHandler { try { String editorId = EDITOR_ID; // System.out.println("Activating diagram: model=" + modelURI + " rvi='" + rvi + "'"); + System.out.println("SHOW MODULE: " + modelURI + ", " + rvi); WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, diagram, modelURI, rvi)); } catch (PartInitException e) { // TODO Auto-generated catch block @@ -122,6 +140,7 @@ public class ShowModuleHandler extends AbstractHandler { // } } }); + */ } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index 7c159ebb..499ea6e5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -246,6 +246,18 @@ public class ResourceSelectionProcessor implements SelectionProcessor loadAllActive(ReadGraph g, Variable variable) throws DatabaseException { ArrayList dataSets = new ArrayList(); HashMap rvis = new HashMap(); - String rvi = Variables.getRVI(g, variable); - if (rvi == null) + + String rvi; + if(variable instanceof ChildVariable) { + ChildVariable var = (ChildVariable)variable; + rvi = var.getRvi(g); + } else if(variable instanceof HistoryVariable) { + HistoryVariable var = (HistoryVariable)variable; + rvi = var.getRvi(g); + } else { return dataSets; + } - rvi = rvi.length() > 0 ? rvi.substring(1) : rvi; rvi = rvi.replace("/", "."); Resource r = variable.getPropertyValue(g, Variables.REPRESENTS); @@ -197,7 +206,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen uri = uri.substring(0, uri.lastIndexOf("/")); Variable v = Variables.getPossibleVariable(graph, uri); if(v != null) { - RepresentsProperty rp = (RepresentsProperty)v.getProperty(graph, Variables.REPRESENTS); + Variable rp = (Variable)v.getProperty(graph, Variables.REPRESENTS); Resource module = rp.getValue(graph); if(module != null && graph.isInheritedFrom(graph.getSingleObject(module, l0.InstanceOf), sr.Module)) { @@ -258,7 +267,11 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null; String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable); Variable compositeVariable = Variables.getVariable(g, variableURI); - return compositeVariable.browsePossible(g, resource); + try { + return compositeVariable.browsePossible(g, resource); + } catch (MissingVariableException e) { + return null; + } } private SysdynModel getSysdynModel(ReadGraph g, Resource model) throws DatabaseException { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariable.java index a572ce8a..31f32cf9 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariable.java @@ -20,4 +20,23 @@ public class ChildVariable extends StandardGraphChildVariable { return parent; } + public String getVariableURI(ReadGraph graph) throws DatabaseException { + Variable parent = getParent(graph); + if(parent instanceof ChildVariable) { + ChildVariable p = (ChildVariable)parent; + return p.getVariableURI(graph) + "/" + this.getName(graph); + } else + return parent.getURI(graph) + "/" + this.getName(graph); + } + + public String getRvi(ReadGraph graph) throws DatabaseException { + Variable parent = getParent(graph); + if(parent instanceof ChildVariable) { + ChildVariable p = (ChildVariable)parent; + return p.getRvi(graph) + "/" + this.getName(graph); + } else { + return this.getName(graph); + } + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariableAdapter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariableAdapter.java index 2bd1a8c3..d5341d23 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariableAdapter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ChildVariableAdapter.java @@ -5,12 +5,19 @@ import org.simantics.db.Resource; import org.simantics.db.common.adaption.SimpleContextualAdapter; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; +import org.simantics.sysdyn.SysdynResource; public class ChildVariableAdapter extends SimpleContextualAdapter{ @Override public Variable adapt(ReadGraph g, Resource source, Variable context) throws DatabaseException { - return new ChildVariable(context, source); + if(context instanceof HistoryVariable) { + return new HistoryVariable(context, source); + } else { + if(g.isInstanceOf(source, SysdynResource.getInstance(g).Module)) + return new ModuleVariable(context, source); + else + return new ChildVariable(context, source); + } } - } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java index c416b5cc..58cdfab6 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java @@ -12,6 +12,8 @@ package org.simantics.sysdyn.adapter; import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; import org.simantics.databoard.Accessors; import org.simantics.databoard.Bindings; @@ -22,11 +24,17 @@ import org.simantics.databoard.accessor.error.AccessorException; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.uri.UnescapedChildMapOfResource; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.exception.MissingVariableException; import org.simantics.db.layer0.variable.ResourceVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; +import org.simantics.layer0.Layer0; import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynDataSet; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; @@ -114,4 +122,69 @@ public class HistoryVariable extends ResourceVariable { } return super.getInterface(graph, clazz); } + + + @Override + public Variable getChild(ReadGraph graph, String name) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource instanceOf = graph.getSingleObject(this.resource, l0.InstanceOf); + if(graph.isInheritedFrom(instanceOf, sr.Module)) { + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + if(instanceOf == null) + throw new MissingVariableException("No instanceof for resource " + NameUtils.getSafeName(graph, resource)); + Resource configuration = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); + if(configuration == null) + throw new MissingVariableException("No configuration for " + NameUtils.getSafeName(graph, instanceOf)); + Map children = graph.syncRequest(new UnescapedChildMapOfResource(configuration)); + Resource child = children.get(name); + return graph.adaptContextual(child, this, Variable.class, Variable.class); + } else { + return super.getChild(graph, name); + } + } + + + @Override + public Collection browseChildren(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource instanceOf = graph.getPossibleObject(this.resource, l0.InstanceOf); + if(graph.isInheritedFrom(instanceOf, sr.Module)) { + ArrayList result = new ArrayList(); + if(instanceOf == null) + return result; + Resource configuration = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); + if(configuration == null) + return result; + + for(Resource child : graph.syncRequest(new UnescapedChildMapOfResource(configuration)).values()) + result.add(graph.adaptContextual(child, this, Variable.class, Variable.class)); + return result; + } else { + return super.browseChildren(graph); + } + } + + + @Override + public Variable getParent(ReadGraph graph) throws DatabaseException { + return parent; + } + + public String getRvi(ReadGraph graph) throws DatabaseException { + Variable parent = getParent(graph); + if(parent instanceof HistoryVariable) { + HistoryVariable p = (HistoryVariable)parent; + return p.getRvi(graph) + "/" + this.getName(graph); + } else { + return this.getName(graph); + } + } + + final public String getName(ReadGraph graph) throws DatabaseException { + return graph.getRelatedValue(resource, graph.getService(Layer0.class).HasName, Bindings.STRING); + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ModuleVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ModuleVariable.java new file mode 100644 index 00000000..edcb78dc --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ModuleVariable.java @@ -0,0 +1,61 @@ +package org.simantics.sysdyn.adapter; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.uri.UnescapedChildMapOfResource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.exception.MissingVariableException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.layer0.Layer0; +import org.simantics.structural.stubs.StructuralResource2; + +public class ModuleVariable extends ChildVariable { + + public ModuleVariable(Variable parent, Resource resource) { + super(parent, resource); + } + + @Override + public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + Resource instanceOf = graph.getPossibleObject(this.resource, l0.InstanceOf); + if(instanceOf == null) + throw new MissingVariableException("No instanceof for resource " + NameUtils.getSafeName(graph, resource)); + Resource configuration = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); + if(configuration == null) + throw new MissingVariableException("No configuration for " + NameUtils.getSafeName(graph, instanceOf)); + Map children = graph.syncRequest(new UnescapedChildMapOfResource(configuration)); + Resource child = children.get(name); + if(child == null) { + return getPossibleSpecialChild(graph, name); + } + else return graph.adaptContextual(child, this, Variable.class, Variable.class); + } + + + @Override + public Collection browseChildren(ReadGraph graph) throws DatabaseException { + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + ArrayList result = new ArrayList(); + + Resource instanceOf = graph.getPossibleObject(this.resource, l0.InstanceOf); + if(instanceOf == null) + return result; + Resource configuration = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); + if(configuration == null) + return result; + + for(Resource child : graph.syncRequest(new UnescapedChildMapOfResource(configuration)).values()) + result.add(graph.adaptContextual(child, this, Variable.class, Variable.class)); + collectSpecialChildren(graph, result); + return result; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index 1c2bc559..f1b60899 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -29,90 +29,19 @@ public class ModelicaWriter { StringBuilder b = new StringBuilder(); String app; - boolean initialEquations = false; - boolean functionsWritten = false; + boolean enumerationsWritten = false; public void write(Configuration conf) { - if(!functionsWritten) { + if(!enumerationsWritten) { b.append("partial class Enumeration_class\n"); b.append(" parameter Integer size;\n"); b.append(" parameter Integer elements[:];\n"); b.append("end Enumeration_class;\n\n"); + enumerationsWritten = true; } writeConfiguration(conf); - - if(false) { - - b.append( - "\nfunction interpolate\n" + - " input Real u \"in1;put value (first column of table)\";\n" + - " input Real table[:, :] \"table to be interpolated\";\n" + - " output Real y \"interpolated input value (icol column of table)\";\n" + - "algorithm \n" + - " y := interpolate2(u, table, 2);" + - "end interpolate;\n\n"); - - b.append( - "\nfunction interpolate2\n" + - " input Real u \"in1;put value (first column of table)\";\n" + - " input Real table[:, :] \"table to be interpolated\";\n" + - " input Integer icol \"column of table to be interpolated\";\n" + - " output Real y \"interpolated input value (icol column of table)\";\n" + - "protected \n" + - " Integer i;\n" + - " Integer n \"number of rows of table\";\n" + - " Real u1;\n" + - " Real u2;\n" + - " Real y1;\n" + - " Real y2;\n" + - "algorithm \n" + - " n := size(table, 1);\n" + - " if n <= 1 then\n" + - " y := table[1, icol];\n" + - " else\n" + - " // Search interval\n" + - " if u <= table[1, 1] then\n" + - " i := 1;\n" + - " else\n" + - " i := 2;\n" + - " while i < n and u > table[i, 1] loop\n" + - " i := i + 1;\n" + - " end while;\n" + - " i := i - 1;\n" + - " end if;\n" + - - " // Get interpolation data\n" + - " u1 := table[i, 1];\n" + - " u2 := table[i + 1, 1];\n" + - " y1 := table[i, icol];\n" + - " y2 := table[i + 1, icol];\n" + - " // Interpolate\n" + - " if u1 >= u2 then\n" + - " y := y1;\n" + - " // since not possible to throw error\n" + - " else\n" + - " y := y1 + (y2 - y1)*(u - u1)/(u2 - u1);\n" + - " end if;\n" + - " end if;\n" + - "end interpolate2;\n"); - - b.append( - "function xidz\n" + - " input Real first;\n" + - " input Real second;\n" + - " input Real x;\n" + - " output Real answer;\n" + - "algorithm\n" + - " if second > 0.0 or second < 0.0 then\n" + - " answer := first / second;\n" + - " else\n" + - " answer := x;\n" + - " end if;\n" + - "end xidz;\n\n\n"); - functionsWritten = true; - } } private void writeConfiguration(Configuration configuration) { @@ -185,6 +114,7 @@ public class ModelicaWriter { } } + boolean initialEquations = false; for(Stock stock : stocks) { app = stock.getInitialEquation(); if (app != null) { -- 2.47.1