From: lempinen Date: Wed, 16 Feb 2011 13:59:11 +0000 (+0000) Subject: Better support for enumerations: Enumerations in module can be created and replaced X-Git-Tag: 2011-04-05-db-merge-trunk~14 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d48fecc10fd79ea6dae6455826dc42982508dd9c;p=simantics%2Fsysdyn.git Better support for enumerations: Enumerations in module can be created and replaced git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19792 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 5c8dceda..b2891c38 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 0193a7a5..04b8db89 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -128,6 +128,7 @@ SYSDYN.Input - - - - @@ -376,6 +369,14 @@ checkEnabled="true"> + + + + @@ -470,10 +471,13 @@ name="Import..."> + + @@ -577,6 +581,29 @@ + + + + + + + + + + + + + + @@ -637,6 +664,13 @@ preference="2.0"> + + + + @@ -708,6 +742,13 @@ 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 60ab9fef..b891e2ad 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 @@ -12,12 +12,13 @@ package org.simantics.sysdyn.ui.browser; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.viewers.ISelection; import org.simantics.browsing.ui.NodeContext; import org.simantics.db.Resource; import org.simantics.db.layer0.variable.Variable; import org.simantics.sysdyn.ui.browser.nodes.VariableNode; -public class BrowserSelection implements IAdaptable { +public class BrowserSelection implements IAdaptable, ISelection { private Resource resource; private Resource model; private String URI; @@ -55,5 +56,12 @@ public class BrowserSelection implements IAdaptable { public String getURI() { return this.URI; } + + @Override + public boolean isEmpty() { + if(originalInput == null) + return true; + return false; + } } 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 fdd38f71..8edb5a7b 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 @@ -51,7 +51,7 @@ public class SysdynBrowser extends GraphExplorerView { @SuppressWarnings("unchecked") VariableNode vn = (VariableNode) ctx.getAdapter(VariableNode.class); IHintContext context; - if(vn != null) { + if(vn != null && vn.getResource() != 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/contributions/ModuleType.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleType.java index 9a6e1440..ed5a9ead 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleType.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleType.java @@ -24,6 +24,7 @@ import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.EnumerationNode; import org.simantics.sysdyn.ui.browser.nodes.InputNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode; @@ -52,6 +53,9 @@ public class ModuleType extends ViewpointContributor { for(Resource r : graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, sr.Module))) { result.add(new ModuleNode(r)); } + for(Resource r : graph.syncRequest(new ObjectsWithType(conf, l0.ConsistsOf, sr.Enumeration))) { + result.add(new EnumerationNode(r)); + } return result; } 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 a069acbb..1d296b62 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 @@ -5,9 +5,11 @@ import org.simantics.browsing.ui.common.node.IDeletableNode; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; 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.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ExceptionUtils; @@ -24,7 +26,20 @@ public class EnumerationNode extends VariableNode implements IDeletabl @Override public void perform(WriteGraph graph) throws DatabaseException, CancelTransactionException { Layer0 l0 = Layer0.getInstance(graph); - graph.deny(resource, l0.PartOf); + SysdynResource sr = SysdynResource.getInstance(graph); + graph.deny(resource, l0.PartOf); + for(Resource redeclaration : graph.getObjects(resource, sr.ReplacedEnumeration_Inverse)) { + graph.deny(redeclaration, sr.HasRedeclaration_Inverse); + } + + for(Resource redeclaration : graph.getObjects(resource, sr.ReplacingEnumeration_Inverse)) { + graph.deny(redeclaration, sr.HasRedeclaration_Inverse); + } + + for(Resource list : OrderedSetUtils.getOwnerLists(graph, resource, sr.ArrayIndexes)) { + OrderedSetUtils.remove(graph, list, resource); + } + } }); } catch (DatabaseException e) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowEdgeClass.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowEdgeClass.java index a3e27016..e002cb23 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowEdgeClass.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/FlowEdgeClass.java @@ -202,7 +202,7 @@ public class FlowEdgeClass { .getCenterX() - FlowRouter.OFFSET, bounds .getCenterY() - FlowRouter.OFFSET, FlowRouter.OFFSET * 2, FlowRouter.OFFSET * 2); -// c.allowedDirections = Constants.EAST_FLAG | Constants.WEST_FLAG; + c.allowedDirections = Constants.EAST_FLAG | Constants.WEST_FLAG; } else { c.parentObstacle = ElementUtils .getElementBoundsOnDiagram(connection.node) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java index bbccf0b7..60a45356 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewEnumerationNodeHandler.java @@ -12,6 +12,8 @@ import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; @@ -23,7 +25,7 @@ public class NewEnumerationNodeHandler extends AbstractHandler { ISelection sel = HandlerUtil.getCurrentSelection(event); - final Resource configuration = AdaptionUtils.adaptToSingle(sel, Resource.class); + final Resource resource = AdaptionUtils.adaptToSingle(sel, Resource.class); SimanticsUI.getSession().asyncRequest(new WriteRequest() { @@ -32,6 +34,20 @@ public class NewEnumerationNodeHandler extends AbstractHandler { SysdynResource sr = SysdynResource.getInstance(g); Layer0 l0 = Layer0.getInstance(g); + Resource configuration = null; + if(g.isInstanceOf(resource, sr.Configuration)) { + configuration = resource; + } else if(g.isInheritedFrom(resource, sr.ModuleSymbol)) { + Resource module = g.getPossibleObject(resource,ModelingResources.getInstance(g).SymbolToComponentType); + configuration = g.getPossibleObject(module, StructuralResource2.getInstance(g).IsDefinedBy); + } else { + Resource instanceOf = g.getSingleObject(resource, l0.InstanceOf); + if(g.isInheritedFrom(instanceOf, sr.Module)) { + configuration = g.getPossibleObject(instanceOf, StructuralResource2.getInstance(g).IsDefinedBy); + } else { + return; + } + } Resource enumerationIndexes = OrderedSetUtils.create(g, sr.EnumerationIndexes); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java index effe57de..2e61a13a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java @@ -28,7 +28,7 @@ import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.properties.widgets.arrays.Keys; +import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys; import org.simantics.utils.datastructures.ArrayMap; public class ArrayIndexesTab extends PropertyTabContributorImpl implements Widget{ @@ -54,7 +54,7 @@ public class ArrayIndexesTab extends PropertyTabContributorImpl implements Widge availableEnumerationsExplorer .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/AvailableVariableIndexes"); - availableEnumerationsExplorer.setColumns(Keys.ENUMERATION_TABLE_COLUMNS); + availableEnumerationsExplorer.setColumns(ColumnKeys.ENUMERATION_TABLE_COLUMNS); availableEnumerationsExplorer.setInputSource(new SingleSelectionInputSource( Resource.class)); @@ -103,7 +103,7 @@ public class ArrayIndexesTab extends PropertyTabContributorImpl implements Widge usedEnumerationsExplorer .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/UsedVariableIndexes"); - usedEnumerationsExplorer.setColumns(Keys.ENUMERATION_TABLE_COLUMNS); + usedEnumerationsExplorer.setColumns(ColumnKeys.ENUMERATION_TABLE_COLUMNS); usedEnumerationsExplorer.setInputSource(new SingleSelectionInputSource( Resource.class)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java index 8573747f..0b210465 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EnumerationTab.java @@ -4,7 +4,6 @@ import java.util.List; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; @@ -36,45 +35,47 @@ import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.db.management.ISessionContext; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.ui.properties.widgets.arrays.EnumerationIndexNode; +import org.simantics.sysdyn.ui.properties.widgets.arrays.ReplaceableIndexesWidget; import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier; import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; import org.simantics.utils.datastructures.ArrayMap; -import org.simantics.utils.ui.ISelectionUtils; -public class EnumerationTab extends PropertyTabContributorImpl { +public class EnumerationTab extends PropertyTabContributorImpl implements Widget { GraphExplorerComposite indexExplorer; Button showAll; - + Variable variable; Table table; @Override public void createControls(Composite body, IWorkbenchSite site, final ISessionContext context, WidgetSupport support) { + support.register(this); + Composite container = new Composite(body, SWT.None); GridDataFactory.fillDefaults().grab(true, true).applyTo(container); - GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(3).applyTo(container); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(container); TrackedText nameText = new TrackedText(container, support, SWT.BORDER); nameText.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); nameText.addModifyListener(new VariableNamePropertyModifier(context, Builtins.URIs.HasName)); nameText.setInputValidator(new VariableNameValidator(support)); - GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(nameText.getWidget()); - - - + GridDataFactory.fillDefaults().grab(true, false).span(4,1).applyTo(nameText.getWidget()); indexExplorer = new GraphExplorerComposite(ArrayMap.keys( "displaySelectors", "displayFilter").values(false, false), site, container, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI | SWT.CHECK); @@ -97,12 +98,15 @@ public class EnumerationTab extends PropertyTabContributorImpl { SimanticsUI.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - Resource enumeration = (Resource)indexExplorer.getExplorer().getRoot().getAdapter(Resource.class); - Resource configuration = graph.getSingleObject(enumeration, l0.PartOf); - SysdynModel model = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); - // update results in graphs - model.resultChanged(); + Resource modelResource = Variables.getModel(graph, variable); + if(modelResource != null) { + Resource configuration = graph.getSingleObject( + modelResource, + SimulationResource.getInstance(graph).HasConfiguration); + SysdynModel model = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); + // update results in graphs + model.resultChanged(); + } } }); } @@ -112,7 +116,7 @@ public class EnumerationTab extends PropertyTabContributorImpl { indexExplorer.finish(); - GridDataFactory.fillDefaults().grab(true, true).span(3, 1).applyTo( + GridDataFactory.fillDefaults().grab(true, true).span(4, 1).applyTo( indexExplorer); Control c = indexExplorer.getExplorerControl(); @@ -120,17 +124,21 @@ public class EnumerationTab extends PropertyTabContributorImpl { ((Tree) c).setLinesVisible(true); - Button add = new Button(container, support, SWT.None); + final Button add = new Button(container, support, SWT.None); add.setText("Add"); add.addSelectionListener(new addEnumerationIndexListener(support)); - Button remove = new Button(container, support, SWT.None); + final Button remove = new Button(container, support, SWT.None); remove.setText("Remove"); remove.addSelectionListener(new removeEnumerationIndexListener(support)); + ReplaceableIndexesWidget externalIndexes = new ReplaceableIndexesWidget(container, support, SWT.NULL); + GridDataFactory.fillDefaults().applyTo(externalIndexes.getWidget()); + showAll = new Button(container, support, SWT.CHECK); showAll.setText("Show all in charts"); showAll.addSelectionListener(new ShowAllIndexesListener(support)); + } private class addEnumerationIndexListener implements SelectionListener, Widget { @@ -143,7 +151,7 @@ public class EnumerationTab extends PropertyTabContributorImpl { @Override public void setInput(ISessionContext context, Object input) { - final Resource enumeration = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + final Resource enumeration = AdaptionUtils.adaptToSingle(input, Resource.class); try { context.getSession().syncRequest(new ReadRequest() { @@ -197,7 +205,7 @@ public class EnumerationTab extends PropertyTabContributorImpl { @Override public void setInput(ISessionContext context, Object input) { - final Resource enumeration = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + final Resource enumeration = AdaptionUtils.adaptToSingle(input, Resource.class); try { context.getSession().syncRequest(new ReadRequest() { @@ -253,7 +261,7 @@ public class EnumerationTab extends PropertyTabContributorImpl { @Override public void setInput(ISessionContext context, Object input) { - final Resource enumeration = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + final Resource enumeration = AdaptionUtils.adaptToSingle(input, Resource.class); context.getSession().asyncRequest(new Read() { @@ -319,11 +327,16 @@ public class EnumerationTab extends PropertyTabContributorImpl { } } - Resource enumeration = graph.getSingleObject(enumerationIndexes, sr.HasEnumerationIndexes_Inverse); - Resource configuration = graph.getSingleObject(enumeration, Layer0.getInstance(graph).PartOf); - SysdynModel model = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); - // update results in graphs - model.resultChanged(); + + Resource modelResource = Variables.getModel(graph, variable); + if(modelResource != null) { + Resource configuration = graph.getSingleObject( + modelResource, + SimulationResource.getInstance(graph).HasConfiguration); + SysdynModel model = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); + // update results in graphs + model.resultChanged(); + } } }); @@ -340,4 +353,9 @@ public class EnumerationTab extends PropertyTabContributorImpl { } } + + @Override + public void setInput(ISessionContext context, Object input) { + variable = AdaptionUtils.adaptToSingle(input, Variable.class); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java index 7312ff44..a3bd4b53 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java @@ -15,25 +15,65 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Tree; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; +import org.simantics.browsing.ui.swt.SingleSelectionInputSource; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; +import org.simantics.db.Resource; import org.simantics.db.management.ISessionContext; +import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys; +import org.simantics.utils.datastructures.ArrayMap; -public class ModuleTab extends PropertyTabContributorImpl { +public class ModuleTab extends PropertyTabContributorImpl implements Widget { + GraphExplorerComposite enumerationRedeclarationExplorer; + @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + support.register(this); + Composite composite = new Composite(body, SWT.NONE); GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); - GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite); + GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(composite); TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); nameText.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); nameText.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasName)); GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); + + Label label = new Label(composite, SWT.NONE); + label.setText("Replaceable enumerations"); + enumerationRedeclarationExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), site, composite, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI); + + enumerationRedeclarationExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/EnumerationReplacement"); + enumerationRedeclarationExplorer.setColumns(ColumnKeys.ENUMERATION_REDECLARATION_COLUMNS); + enumerationRedeclarationExplorer.setInputSource(new SingleSelectionInputSource( + Resource.class)); + + enumerationRedeclarationExplorer.finish(); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + enumerationRedeclarationExplorer); + + Control c = enumerationRedeclarationExplorer.getExplorerControl(); + if (c instanceof Tree) + ((Tree) c).setLinesVisible(true); + } + + + @Override + public void setInput(ISessionContext context, Object input) { + enumerationRedeclarationExplorer.setInput(context, input); + } } 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 1e07c5b1..b46e3d63 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 @@ -16,6 +16,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import org.eclipse.jface.viewers.ISelection; import org.simantics.browsing.ui.SelectionProcessor; import org.simantics.browsing.ui.swt.ComparableTabContributor; import org.simantics.db.ReadGraph; @@ -68,6 +69,7 @@ public class ResourceSelectionProcessor implements SelectionProcessor implements CustomModifier { + + private final Enumeration enumeration; + private final EnumeratedValue value; + private final List values; + private final Session session; + private Combo combo; + int select; + + public ComboBoxModifier(Session session, Enumeration enumeration, T value) { + this(session, enumeration, enumeration.find(value)); + } + + public ComboBoxModifier(Session session, Enumeration enumeration, + EnumeratedValue value) { + if (session == null) + throw new NullPointerException("null session"); + if (enumeration == null) + throw new NullPointerException("null enumeration"); + if (enumeration.size() == 0) + throw new IllegalArgumentException(""); + + this.enumeration = enumeration; + this.value = value; + this.session = session; + + select = 0; + boolean found = false; + + this.values = new ArrayList(); + for (EnumeratedValue v : enumeration.values()) { + values.add(v.getName()); + + if(v == value) + found = true; + + if(found == false) + select++; + } + } + + @Override + public Object createControl(Object parentControl, Object controlItem, + int columnIndex, NodeContext context) { + + combo = new Combo((Composite) parentControl, SWT.DROP_DOWN + | SWT.BORDER | SWT.READ_ONLY); + + for (String item : values) { + combo.add(item); + } + + combo.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + String index = ((Combo) e.getSource()).getText(); + modify(index); + } + }); + + combo.select(select); + + return combo; + + } + + @Override + public String getValue() { + return enumeration.values().get(0).getName(); + } + + @Override + public final String isValid(String label) { + if (enumeration.findByName(label) == null) + return "Value '" + label + "' is not among the enumerated values " + enumeration.values(); + return null; + } + + @Override + public void modify(String label) { + + int index = values.indexOf(label); + if (index == -1) + throw new IllegalArgumentException("Cannot modify enumeration with value '" + label + "', not among the enumerated values " + values); + + EnumeratedValue oldEnumValue = value; + EnumeratedValue newEnumValue = enumeration.values().get(index); + + final T oldObject = oldEnumValue != null ? oldEnumValue.getObject() : null; + final T newObject = newEnumValue != null ? newEnumValue.getObject() : null; + + if (ObjectUtils.objectEquals(oldObject, newObject)) + return; + + try { + session.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + modifyWithObject(graph, oldObject, newObject); + } + }); + } catch (DatabaseException e) { + handleException(e); + } + + combo.dispose(); + } + + protected abstract void modifyWithObject(WriteGraph graph, T oldEnumObject, T enumObject) throws DatabaseException; + + protected void handleException(DatabaseException e) { + throw new RuntimeException(e); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationLabeler.java index d55a10f3..bbaaa54a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationLabeler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationLabeler.java @@ -11,6 +11,7 @@ import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys; public class EnumerationLabeler extends ColumnLabelerContributorImpl{ @@ -20,9 +21,10 @@ public class EnumerationLabeler extends ColumnLabelerContributorImpl map = new HashMap(); - map.put(Keys.ENUMERATION, name); + map.put(ColumnKeys.ENUMERATION, name); SysdynResource sr = SysdynResource.getInstance(graph); + Resource enumerationIndexes = graph.getPossibleObject(input.data, sr.HasEnumerationIndexes); ListIterator indexes = OrderedSetUtils.iterator(graph, enumerationIndexes); StringBuilder sb = new StringBuilder(); @@ -34,7 +36,14 @@ public class EnumerationLabeler extends ColumnLabelerContributorImpl implements IModifiableNode { + + private Resource module; + + public RedeclarationNode(ReadGraph graph, final Resource module, Resource enumeration) { + super(enumeration); + this.module = module; + } + + /** + * + * @param graph + * @return + */ + public Resource getReplacingEnumeration(ReadGraph graph) { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource result = null; + try { + Resource redeclaration = getRedeclaration(graph); + if(redeclaration != null) { + result = graph.getSingleObject(redeclaration, sr.ReplacingEnumeration); + } + } catch(DatabaseException e) { + e.printStackTrace(); + } + + return result; + } + + + public Resource getModule() { + return module; + } + + public void setModule(Resource module) { + this.module = module; + } + + public Resource getRedeclaration(ReadGraph graph) { + try { + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource redeclaration : graph.syncRequest(new ObjectsWithType(module, sr.HasRedeclaration, sr.Redeclaration))) { + Resource replacedEnumeration = graph.getPossibleObject(redeclaration, sr.ReplacedEnumeration); + if(replacedEnumeration != null && replacedEnumeration.equals(getReplacedEnumeration())) { + return redeclaration; + } + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + return null; + } + + public void setRedeclaration(WriteGraph graph, Resource redeclaration) { + try { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource oldRedeclaration = getRedeclaration(graph); + if(oldRedeclaration != null || redeclaration == null) { + graph.deny(module, sr.HasRedeclaration, oldRedeclaration); + } + + if(redeclaration != null) + graph.claim(module, sr.HasRedeclaration, redeclaration); + + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + public Resource getReplacedEnumeration() { + return this.data; + } + + @Override + public Modifier getModifier(String columnId) { + + if(!ColumnKeys.REPLACED_WITH.equals(columnId)) + return null; + + ComboBoxModifier cbm = null; + + try { + cbm = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public ComboBoxModifier perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + ArrayList> values = new ArrayList>(); + + + Resource configuration = graph.getSingleObject(module, l0.PartOf); + + for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) { + String name = NameUtils.getSafeName(graph, enumeration); + values.add(new EnumeratedValue(name, enumeration)); + } + + if(values.size() == 0) + return null; + + values.add(0, new EnumeratedValue("", null)); + Enumeration enumeration = new Enumeration(values); + + ComboBoxModifier cbm = new ComboBoxModifier(graph.getSession(), enumeration, getReplacingEnumeration(graph)) { + + @Override + protected void modifyWithObject(WriteGraph graph, Resource oldEnumObject, + Resource enumObject) throws DatabaseException { + + if(enumObject == null) { + setRedeclaration(graph, null); + } else if(!enumObject.equals(oldEnumObject)) { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource redeclaration = GraphUtils.create2(graph, + sr.Redeclaration, + sr.ReplacedEnumeration, getReplacedEnumeration(), + sr.ReplacingEnumeration, enumObject); + setRedeclaration(graph, redeclaration); + } + + } + }; + + return cbm; + + } + }); + } catch (DatabaseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return cbm; + } + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableEnumerations.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableEnumerations.java new file mode 100644 index 00000000..91afda93 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableEnumerations.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.impl.contributor.viewpoint.ViewpointContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; + +public class ReplaceableEnumerations extends ViewpointContributorImpl { + + @Override + public Collection getContribution(ReadGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + + Resource moduleType = graph.getSingleObject(input, l0.InstanceOf); + Resource configuration = graph.getSingleObject(moduleType, sr2.IsDefinedBy); + + ArrayList result = new ArrayList(); + + for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) { + if(Boolean.TRUE.equals(graph.getRelatedValue(r, sr.IsReplaceable))) + result.add(new RedeclarationNode(graph, input, r)); + } + + return result; + } + + @Override + public String getViewpointId() { + return "Replaceable enumerations"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableEnumerationsLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableEnumerationsLabeler.java new file mode 100644 index 00000000..68a44dba --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableEnumerationsLabeler.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.HashMap; +import java.util.Map; + +import org.simantics.browsing.ui.graph.impl.contributor.labeler.ColumnLabelerContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys; + +public class ReplaceableEnumerationsLabeler extends ColumnLabelerContributorImpl{ + + @Override + public Map getLabel(ReadGraph graph, RedeclarationNode input) + throws DatabaseException { + HashMap map = new HashMap(); + + String name = NameUtils.getSafeName(graph, input.data); + map.put(ColumnKeys.ENUMERATION, name); + + Resource replacingEnumeration = input.getReplacingEnumeration(graph); + String replacingEnumerationName = ""; + if(replacingEnumeration != null) + replacingEnumerationName = NameUtils.getSafeName(graph, replacingEnumeration); + + map.put(ColumnKeys.REPLACED_WITH, replacingEnumerationName); + return map; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableIndexesWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableIndexesWidget.java new file mode 100644 index 00000000..06caf4d4 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ReplaceableIndexesWidget.java @@ -0,0 +1,117 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.utils.ui.ISelectionUtils; + +public class ReplaceableIndexesWidget implements Widget { + + Resource variable = null; + boolean selected = false; + org.simantics.browsing.ui.swt.widgets.Button isReplaceableButton; + + public ReplaceableIndexesWidget(Composite parent, WidgetSupport support, int style) { + support.register(this); + isReplaceableButton = new org.simantics.browsing.ui.swt.widgets.Button(parent, support, style |= SWT.CHECK); + isReplaceableButton.setText("Can be replaced by parent module"); + } + + @Override + public void setInput(ISessionContext context, Object input) { + if(input instanceof ISelection) { + ISelection selection = (ISelection)input; + if(selection instanceof IStructuredSelection) { + Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class); + if(resource != null) { + variable = resource; + } + } + } + + if(variable == null) return; + + try { + context.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + final Boolean replaceable = graph.getPossibleRelatedValue(variable, sr.IsReplaceable); + if(replaceable != null) + selected = replaceable; + final Button button = getWidget(); + button.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if(button.isDisposed()) return; + + if(replaceable) + button.setSelection(true); + else + button.setSelection(false); + } + }); + + + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + isReplaceableButton.addSelectionListener(new SelectionListenerImpl(context) { + + @Override + public void apply(WriteGraph graph, Resource input) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Boolean replaceable = graph.getPossibleRelatedValue(input, sr.IsReplaceable); + if(Boolean.TRUE.equals(replaceable)) { + graph.claimLiteral(input, sr.IsReplaceable, false); + selected = false; + } else { + graph.claimLiteral(input, sr.IsReplaceable, true); + selected = true; + } + } + }); + } + + public Button getWidget() { + return isReplaceableButton.getWidget(); + } + + public boolean getSelection() { + return selected; + } + + public void addSelectionListener(SelectionListener listener) { + isReplaceableButton.addSelectionListener(listener); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java index f82f517e..4425ce9c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java @@ -145,7 +145,6 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen protected Collection loadAllActive(ReadGraph g, Variable variable) throws DatabaseException { ArrayList dataSets = new ArrayList(); HashMap rvis = new HashMap(); - String rvi = Variables.getRVI(g, variable); if (rvi == null) return dataSets; diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index 802f3295..cbd4948c 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -17,10 +17,15 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", org.simantics.layer0.utils;bundle-version="0.8.0", org.simantics.layer0;bundle-version="1.0.0", org.simantics.structural.ontology;bundle-version="1.0.0", - org.simantics.sysdyn.ontology;bundle-version="1.0.0" + org.simantics.sysdyn.ontology;bundle-version="1.0.0", + org.simantics.modeling;bundle-version="1.1.1", + org.simantics.diagram;bundle-version="1.1.1", + org.simantics.diagram.ontology;bundle-version="1.1.1" Export-Package: org.simantics.sysdyn, org.simantics.sysdyn.expressionParser, org.simantics.sysdyn.manager, + org.simantics.sysdyn.mdlImport, + org.simantics.sysdyn.mdlImport.mdlElements, org.simantics.sysdyn.modelica, org.simantics.sysdyn.representation, org.simantics.sysdyn.representation.visitors, 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 9f2c93e8..d12f7b40 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -160,14 +160,14 @@ public class ModelicaWriter { if(!modules.isEmpty()) { b.append("// Module definitions\n"); for(Module m : modules) { - b.append(" " + m.getType().getName() + " " + m.getName() + ";\n"); + b.append(m.getDeclaration()); } } if(!inputs.isEmpty()) { b.append("// Input definitions\n"); for(Input i : inputs) { - b.append(" " + i.getType() + " " + i.getName() + ";\n"); + b.append(i.getDeclaration()); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java index 33dc5760..ebde6003 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; +import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @GraphType("http://www.simantics.org/Sysdyn-1.0/Enumeration") @@ -11,6 +12,9 @@ public class Enumeration extends Variable { @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasEnumerationIndexes") private EnumerationIndexes enumerationIndexes; + + @RelatedValue("http://www.simantics.org/Sysdyn-1.0/IsReplaceable") + private Boolean isReplaceable; @Override public void accept(IElementVisitorVoid v) { @@ -19,20 +23,35 @@ public class Enumeration extends Variable { public String getDeclaration() { StringBuilder sb = new StringBuilder(); - sb.append(" " + this.getType()); + sb.append(" "); + if(isReplaceable()) + sb.append("replaceable "); + sb.append(this.getType()); sb.append(" " + this.name); - sb.append(" = enumeration("); + sb.append(" = "); + sb.append(getEnumerationDefinition()); + sb.append(";\n"); + return sb.toString(); + } + + public ArrayList getEnumerationIndexes() { + return enumerationIndexes.getEnumerationIndexes(); + } + + public boolean isReplaceable() { + return Boolean.TRUE.equals(isReplaceable); + } + + public String getEnumerationDefinition() { + StringBuilder sb = new StringBuilder(); + sb.append("enumeration("); for(int i = 0; i < enumerationIndexes.getEnumerationIndexes().size(); i++) { sb.append(enumerationIndexes.getEnumerationIndexes().get(i).getName()); if(i < enumerationIndexes.getEnumerationIndexes().size() - 1) sb.append(", "); } - sb.append(");\n"); + sb.append(")"); return sb.toString(); } - - public ArrayList getEnumerationIndexes() { - return enumerationIndexes.getEnumerationIndexes(); - } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java index ceb8fef4..8a2a543c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.simantics.sysdyn.representation; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import org.simantics.objmap.annotations.GraphType; @@ -26,16 +28,58 @@ public class Input extends Variable { @RelatedElements("http://www.simantics.org/Sysdyn-1.0/IsHeadOf") private List isHeadOf; + + @Override public void accept(IElementVisitorVoid v) { v.visit(this); } public String getDefaultInputValue() { - return defaultInputValue.toString(); + if( getArrayIndexes() == null || getArrayIndexes().getEnumerations().isEmpty()) { + return defaultInputValue.toString(); + } else { + StringBuilder sb = new StringBuilder(); + sb.append("fill("); + sb.append(defaultInputValue); + sb.append(", "); + Iterator i = getArrayIndexes().getEnumerations().iterator(); + while(i.hasNext()) { + Enumeration e = i.next(); + sb.append(e.getEnumerationIndexes().size()); + if(i.hasNext()) + sb.append(", "); + } + sb.append(")"); + return sb.toString(); + } } public boolean isHeadOfDependency() { return !isHeadOf.isEmpty(); } + + public String getDeclaration() { + ArrayIndexes ai = getArrayIndexes(); + ArrayList enumerations = null; + if(ai != null) + enumerations = ai.getEnumerations(); + + String range = ""; + if(enumerations != null && enumerations.size() > 0) { + StringBuilder sb = new StringBuilder(); + sb.append("["); + Iterator iterator = enumerations.iterator(); + while(iterator.hasNext()) { + sb.append(iterator.next().getName()); + if(iterator.hasNext()) { + sb.append(", "); + } + } + sb.append("]"); + range = sb.toString(); + } + + return " " + getType() + " " + getName() + range + ";\n"; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java index 003f456e..02fa9e8d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -11,8 +11,13 @@ *******************************************************************************/ package org.simantics.sysdyn.representation; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; +import org.simantics.objmap.annotations.RelatedElements; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @@ -28,6 +33,9 @@ public class Module implements IElement { @RelatedElement("http://www.simantics.org/Layer0-1.0/InstanceOf") private ModuleType type; + @RelatedElements("http://www.simantics.org/Sysdyn-1.0/HasRedeclaration") + private List redeclarations; + @Override public void accept(IElementVisitorVoid v) { v.visit(this); @@ -40,9 +48,49 @@ public class Module implements IElement { public ModuleType getType() { return type; } + + /** + * Returns the declaration of a module with possible redeclared enumerations + * + * Module m(redeclare type E1 = enumeration(i1, i2, i3), redeclare type E2 = enumeration(i4, i5, i6)); + * + * @return Declaration of a module instance + */ + public String getDeclaration() { + StringBuilder sb = new StringBuilder(); + sb.append(" "); + sb.append(getType().getName()); + sb.append(" "); + sb.append(getName()); + if(!getRedeclarations().isEmpty()) { + sb.append("("); + Iterator i = getRedeclarations().iterator(); + while(i.hasNext()) { + Redeclaration rd = i.next(); + sb.append(rd.getRedeclaration()); + if(i.hasNext()) + sb.append(","); + } + sb.append(")"); + } + sb.append(";\n"); + return sb.toString(); + } public Configuration getParentConfiguration() { return this.parentConfiguration; } + + /** + * Return the list of the defined redeclarations for this module instance. + * + * @return List of the defined redeclarations or an empty list if redeclarations have not been set + */ + public List getRedeclarations() { + if(redeclarations == null) { + redeclarations = new ArrayList(); + } + return redeclarations; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java new file mode 100644 index 00000000..7cddf5a7 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Redeclaration.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedElement; + +@GraphType("http://www.simantics.org/Sysdyn-1.0/Redeclaration") +public class Redeclaration { + + @RelatedElement("http://www.simantics.org/Sysdyn-1.0/ReplacedEnumeration") + private Enumeration replacedEnumeration; + + @RelatedElement("http://www.simantics.org/Sysdyn-1.0/ReplacingEnumeration") + private Enumeration replacingEnumeration; + + public String getRedeclaration() { + if(replacedEnumeration == null || replacingEnumeration == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + sb.append("redeclare type "); + sb.append(replacedEnumeration.getName()); + sb.append(" = "); + sb.append(replacingEnumeration.getEnumerationDefinition()); + + return sb.toString(); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java index ed8e9f8d..29a78bad 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java @@ -49,6 +49,7 @@ public class SysdynSchema extends SimpleSchema { addLinkType(MappingSchemas.fromAnnotations(g, ConstantExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, WithLookupExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, LookupExpression.class)); + addLinkType(MappingSchemas.fromAnnotations(g, Redeclaration.class)); } catch (DatabaseException e) {