From: lempinen Date: Thu, 2 Dec 2010 12:58:11 +0000 (+0000) Subject: Changed UI to support new ontology with array variables and multiple expressions. X-Git-Tag: 2011-04-05-db-merge-trunk~65 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=76f63f59d61e826c1b50492dc98c008d965920f8;p=simantics%2Fsysdyn.git Changed UI to support new ontology with array variables and multiple expressions. Some UI components added to support using array variables, but they are incomplete and buggy. MODEL CANNOT BE SIMULATED IF ARRAYS ARE USED! If array variables are not used, the software should function the way it did before. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18898 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index f0384018..c9117399 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -106,6 +106,61 @@ name="System dynamics"> + + + + + + + + + + + + + + + + + + + + + + + - - - - - - Simple System Dynamics Tutorials - - - + + + + + + + + @@ -635,6 +687,20 @@ + + + + + + + + @@ -679,11 +745,39 @@ - - + point="org.eclipse.ui.cheatsheets.cheatSheetContent"> + + + + + Simple System Dynamics Tutorials + + + + + + + + + + @@ -694,53 +788,6 @@ - - - - - - - - - - - - - - - - - - - @@ -774,22 +821,4 @@ label="System Dynamics ontology dependencies"> - - - - - - - - diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java index b4ff4c41..265f2a8a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/SysdynComponentCopyAdvisor.java @@ -35,7 +35,7 @@ public class SysdynComponentCopyAdvisor extends ComponentCopyAdvisor{ SysdynResource sr = SysdynResource.getInstance(graph); try { if(graph.isInstanceOf(statement.getSubject(), sr.IndependentVariable)) { - if(statement.getPredicate().equals(sr.HasExpression)) + if(statement.getPredicate().equals(sr.HasExpressions)) return true; } } catch (ServiceException e) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java index 19011dfa..54608589 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/DefaultVariable.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.project; +import java.util.List; + import org.simantics.databoard.binding.Binding; import org.simantics.databoard.binding.java.DoubleBindingDefault; import org.simantics.databoard.binding.java.StringBindingDefault; @@ -20,6 +22,7 @@ import org.simantics.databoard.type.DoubleType; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.ResourceVariable; import org.simantics.layer0.Layer0; @@ -38,7 +41,11 @@ public class DefaultVariable extends ResourceVariable { SysdynResource sr = SysdynResource.getInstance(graph); Resource represents = graph.getPossibleObject(resource, b.Represents); if(represents == null) return null; - Resource expression = graph.getPossibleObject(represents, sr.HasExpression); + //FIXME: doesn't support multiple expressions + Resource expressions = graph.getPossibleObject(represents, sr.HasExpressions); + if(expressions == null) return null; + List expressionList = OrderedSetUtils.toList(graph, expressions); + Resource expression = expressionList.get(0); if(expression == null) return null; if(!graph.isInstanceOf(expression, sr.ParameterExpression)) return null; String text = graph.getPossibleRelatedValue(expression, sr.HasEquation); @@ -61,7 +68,11 @@ public class DefaultVariable extends ResourceVariable { // if(!MutableDoubleBinding.INSTANCE.equals(binding)) return; Resource represents = graph.getPossibleObject(resource, l0.Represents); if(represents == null) return; - Resource expression = graph.getPossibleObject(represents, sr.HasExpression); + //FIXME: doesn't support multiple expressions + Resource expressions = graph.getPossibleObject(represents, sr.HasExpressions); + if(expressions == null) return; + List expressionList = OrderedSetUtils.toList(graph, expressions); + Resource expression = expressionList.get(0); if(expression == null) return; if(!graph.isInstanceOf(expression, sr.ParameterExpression)) return; Double value = new Double(((MutableDouble)object).doubleValue()); 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 new file mode 100644 index 00000000..40572edb --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java @@ -0,0 +1,220 @@ +package org.simantics.sysdyn.ui.properties; + +import java.util.ArrayList; +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; +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.NodeContext; +import org.simantics.browsing.ui.swt.AdaptableHintContext; +import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; +import org.simantics.browsing.ui.swt.SingleSelectionInputSource; +import org.simantics.browsing.ui.swt.widgets.Button; +import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +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.utils.datastructures.ArrayMap; + +public class ArrayIndexesTab extends PropertyTabContributorImpl { + + GraphExplorerComposite availableEnumerationsExplorer; + GraphExplorerComposite usedEnumerationsExplorer; + + @Override + public void createControls(Composite body, IWorkbenchSite site, + ISessionContext context, WidgetSupport support) { + + GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body); + + + Composite available = new Composite(body, SWT.NONE); + GridLayoutFactory.fillDefaults().applyTo(available); + GridDataFactory.fillDefaults().grab(true, true).applyTo(available); + Label label = new Label(available, SWT.None); + label.setText("Available Enumerations"); + availableEnumerationsExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), site, available, support, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI); + + availableEnumerationsExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/AvailableVariableIndexes"); + availableEnumerationsExplorer.setColumns(Keys.ENUMERATION_TABLE_COLUMNS); + availableEnumerationsExplorer.setInputSource(new SingleSelectionInputSource( + Resource.class)); + + availableEnumerationsExplorer.finish(); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + availableEnumerationsExplorer); + + Control c = availableEnumerationsExplorer.getExplorerControl(); + if (c instanceof Tree) + ((Tree) c).setLinesVisible(true); + + + Button add = new Button(body, support, SWT.NONE); + add.setText(" -> "); + + add.addSelectionListener(new SelectionListenerImpl(context) { + + @Override + public void apply(WriteGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource arrayIndexes = graph.getPossibleObject(input, sr.HasArrayIndexes); + + List enumerationResources = getSelectedResources(availableEnumerationsExplorer); + + if(arrayIndexes == null) { + arrayIndexes = OrderedSetUtils.create(graph, sr.ArrayIndexes, enumerationResources); + graph.claim(input, sr.HasArrayIndexes, arrayIndexes); + } else { + OrderedSetUtils.addAll(graph, arrayIndexes, enumerationResources); + } + + + } + }); + + Composite used = new Composite(body, SWT.NONE); + GridLayoutFactory.fillDefaults().applyTo(used); + GridDataFactory.fillDefaults().grab(true, true).applyTo(used); + label = new Label(used, SWT.None); + label.setText("Used Enumerations"); + + usedEnumerationsExplorer = new GraphExplorerComposite(ArrayMap.keys( + "displaySelectors", "displayFilter").values(false, false), site, used, support, SWT.FULL_SELECTION | SWT.BORDER); + + usedEnumerationsExplorer + .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/UsedVariableIndexes"); + usedEnumerationsExplorer.setColumns(Keys.ENUMERATION_TABLE_COLUMNS); + usedEnumerationsExplorer.setInputSource(new SingleSelectionInputSource( + Resource.class)); + + usedEnumerationsExplorer.finish(); + + GridDataFactory.fillDefaults().grab(true, true).applyTo( + usedEnumerationsExplorer); + + Control c2 = usedEnumerationsExplorer.getExplorerControl(); + if (c2 instanceof Tree) + ((Tree) c2).setLinesVisible(true); + + Composite buttons = new Composite(body, SWT.None); + GridLayoutFactory.fillDefaults().applyTo(buttons); + + Button up = new Button(buttons, support, SWT.NONE); + up.setText("Up"); + up.addSelectionListener(new SelectionListenerImpl(context) { + @Override + public void apply(WriteGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource arrayIndexes = graph.getPossibleObject(input, sr.HasArrayIndexes); + if(arrayIndexes == null) + return; + + List enumerationResources = getSelectedResources(usedEnumerationsExplorer); + if(enumerationResources.size() != 1) + return; + Resource enumeration = enumerationResources.get(0); + Resource prev = OrderedSetUtils.prev(graph, arrayIndexes, enumeration); + if(prev.equals(arrayIndexes)) + return; + + OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + OrderedSetUtils.addBefore(graph, arrayIndexes, prev, enumeration); + } + }); + + Button down = new Button(buttons, support, SWT.NONE); + down.setText("Down"); + down.addSelectionListener(new SelectionListenerImpl(context) { + @Override + public void apply(WriteGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource arrayIndexes = graph.getPossibleObject(input, sr.HasArrayIndexes); + if(arrayIndexes == null) + return; + + List enumerationResources = getSelectedResources(usedEnumerationsExplorer); + if(enumerationResources.size() != 1) + return; + Resource enumeration = enumerationResources.get(0); + Resource next = OrderedSetUtils.next(graph, arrayIndexes, enumeration); + if(next.equals(arrayIndexes)) + return; + OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + OrderedSetUtils.addAfter(graph, arrayIndexes, next, enumeration); + } + }); + + + Button remove = new Button(buttons, support, SWT.NONE); + remove.setText("Remove"); + remove.addSelectionListener(new SelectionListenerImpl(context) { + @Override + public void apply(WriteGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource arrayIndexes = graph.getPossibleObject(input, sr.HasArrayIndexes); + if(arrayIndexes == null) + return; + + List enumerationResources = getSelectedResources(usedEnumerationsExplorer); + if(enumerationResources.size() != 1) + return; + Resource enumeration = enumerationResources.get(0); + OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + } + }); + } + + + private List getSelectedResources(GraphExplorerComposite explorer) { + List result = new ArrayList(); + + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return result; + IStructuredSelection iss = (IStructuredSelection) selection; + @SuppressWarnings("unchecked") + List selections = iss.toList(); + for(AdaptableHintContext ahc : selections) { + Resource resource = (Resource) ahc.getAdapter(Resource.class); + result.add(resource); + } + return result; + } + + private NodeContext getSelectedNodeContext(GraphExplorerComposite explorer) { + ISelection selection = ((ISelectionProvider) explorer + .getAdapter(ISelectionProvider.class)).getSelection(); + if (selection == null) + return null; + IStructuredSelection iss = (IStructuredSelection) selection; + AdaptableHintContext ahc = (AdaptableHintContext)iss.getFirstElement(); + if(ahc == null) + return null; + NodeContext ctx = (NodeContext)ahc.getAdapter(NodeContext.class); + return ctx; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index 67cd6ffa..6e1af6e9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -13,10 +13,13 @@ package org.simantics.sysdyn.ui.properties; import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.VerifyKeyListener; @@ -36,75 +39,319 @@ import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.platform.PropertyPageView; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; -import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.Button; import org.simantics.browsing.ui.swt.widgets.TrackedCombo; -import org.simantics.browsing.ui.swt.widgets.TrackedText; import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener; import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl; +import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.VirtualGraph; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.AsyncListener; +import org.simantics.db.request.Read; +import org.simantics.db.service.VirtualGraphSupport; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.ArrayExpressionCombo; import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes; +import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType; import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget; +import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget; import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget; -import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType; -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.Pair; -public class EquationTab extends PropertyTabContributorImpl { +public class EquationTab extends PropertyTabContributorImpl implements Widget { - TrackedCombo expressionTypeCombo, unitCombo; + TrackedCombo expressionTypeCombo, unitCombo, arrayEquationCombo; ShortcutTabWidget shortcutTabWidget; ExpressionWidget expressionWidget; org.eclipse.ui.IPartListener2 focusLostListener; IWorkbenchSite site; + WidgetSupport support; + Button deleteExpression, newExpression; + + WidgetSupportImpl expressionSupport = new WidgetSupportImpl(); @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { this.site = site; + this.support = 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); - TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite); + + Composite nameAndEquations = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(nameAndEquations); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(nameAndEquations); + + Composite nameComposite = new Composite(nameAndEquations, SWT.NONE); + GridLayoutFactory.fillDefaults().numColumns(3).applyTo(nameComposite); + GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite); + + arrayEquationCombo = new ArrayExpressionCombo(nameComposite, support, SWT.DROP_DOWN | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(arrayEquationCombo.getWidget()); + + deleteExpression = new Button(nameComposite, support, SWT.NONE); + deleteExpression.setText("Delete"); + GridDataFactory.fillDefaults().applyTo(deleteExpression.getWidget()); + + newExpression = new Button(nameComposite, support, SWT.NONE); + newExpression.setText("New"); + GridDataFactory.fillDefaults().applyTo(newExpression.getWidget()); + /* + TrackedText nameText = new TrackedText(nameAndEquations, support, SWT.BORDER); nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName)); nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName)); nameText.setInputValidator(new VariableNameValidator(support)); - GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); + GridDataFactory.fillDefaults().grab(true, false).span(2,1).applyTo(nameText.getWidget()); + */ + shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE); + GridDataFactory.fillDefaults().span(1, 3).grab(false, true).applyTo(shortcutTabWidget.getWidget()); - Label label = new Label(composite, SWT.SINGLE ); + + Composite TypeAndUnit = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(TypeAndUnit); + GridLayoutFactory.fillDefaults().numColumns(5).applyTo(TypeAndUnit); + + Label label = new Label(TypeAndUnit, SWT.SINGLE ); label.setText("Type:"); GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); - expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); - + expressionTypeCombo = new TrackedCombo(TypeAndUnit, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); + GridDataFactory.fillDefaults().applyTo(expressionTypeCombo.getWidget()); - label = new Label(composite, SWT.SINGLE ); + label = new Label(TypeAndUnit, SWT.SINGLE ); label.setText("Unit:"); GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); - unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER); + unitCombo = new TrackedCombo(TypeAndUnit, support, SWT.DROP_DOWN | SWT.BORDER); + GridDataFactory.fillDefaults().applyTo(unitCombo.getWidget()); - shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE); + IsOutputWidget isOutput = new IsOutputWidget(TypeAndUnit, support, SWT.NULL); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.END, SWT.FILL).applyTo(isOutput.getWidget()); Composite expressionComposite = new Composite(composite, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite); - expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(expressionComposite); + expressionWidget = new ExpressionWidget(expressionComposite, expressionSupport, SWT.NONE); expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable()); - addListeners(); + addListeners(context); + } + + @Override + public void setInput(ISessionContext context, final Object input) { + final Resource variable = AdaptionUtils.adaptToSingle(input, Resource.class); + Resource expression = null; + try { + expression = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + return getActiveExpression(graph, AdaptionUtils.adaptToSingle(input, Resource.class)); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + // No expressions -> creating a new ordered set expressions and the active expression + if(expression == null && variable != null) { + try { + expression = SimanticsUI.getSession().syncRequest(new WriteResultRequest() { + + @Override + public Resource perform(WriteGraph graph) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource expressions = OrderedSetUtils.create(graph, sr.Expressions); + graph.claim(variable, sr.HasExpressions, expressions); + final Resource expression = graph.newResource(); + + if(graph.isInstanceOf(variable, sr.Auxiliary) || + graph.isInstanceOf(variable, sr.Valve)) { + graph.claim(expression, l0.InstanceOf, null, sr.NormalExpression); + graph.claimLiteral(expression, sr.HasEquation, ""); + } + else if(graph.isInstanceOf(variable, sr.Stock)) { + graph.claim(expression, l0.InstanceOf, null, sr.StockExpression); + graph.claimLiteral(expression, sr.HasInitialEquation, ""); + } + OrderedSetUtils.add(graph, expressions, expression); + + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + final Session session = graph.getSession(); + session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + VirtualGraph runtime = graph.getService(VirtualGraph.class); + session.asyncRequest(new WriteRequest(runtime) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + graph.claim(variable, sr.HasActiveExpression, expression); + } + } + ); + } + }); + return expression; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + + SimanticsUI.getSession().asyncRequest(new Read>() { + + @Override + public Pair perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expressions = graph.getPossibleObject(variable, sr.HasExpressions); + if(expressions == null) { + return new Pair(false, false); + } + List expressionList = OrderedSetUtils.toList(graph, expressions); + if(expressionList.isEmpty()) { + return new Pair(false, false); + } + + boolean canAdd = true; + boolean canDelete = false; + if(expressionList.size() > 1) + canDelete = true; + String defaultRange = ArrayExpressionCombo.getDefaultRange(graph, variable); + + for(Resource expression : expressionList) { + String range = graph.getPossibleRelatedValue(expression, sr.HasArrayRange); + if(range == null || range.equals("") || range.equals(defaultRange)) { + canAdd = false; + break; + } + } + return new Pair(canAdd, canDelete); + } + }, new AsyncListener>() { + + @Override + public void execute(AsyncReadGraph graph, + final Pair result) { + newExpression.getWidget().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if(!newExpression.getWidget().isDisposed()) + newExpression.getWidget().setEnabled(result.first); + if(!deleteExpression.getWidget().isDisposed()) + deleteExpression.getWidget().setEnabled(result.second); + } + }); + + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + throwable.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return newExpression.getWidget().isDisposed() || deleteExpression.getWidget().isDisposed(); + } + }); + + StructuredSelection ss = new StructuredSelection(expression); + expressionSupport.fireInput(context, ss); } - private void addListeners() { + private void addListeners(ISessionContext context) { + + deleteExpression.addSelectionListener(new SelectionListenerImpl(context) { + + @Override + public void apply(WriteGraph graph, final Resource input) + throws DatabaseException { + + SysdynResource sr = SysdynResource.getInstance(graph); + Resource activeExpression = graph.getPossibleObject(input, sr.HasActiveExpression); + if(activeExpression == null) + return; + + Resource expressionList = OrderedSetUtils.getSingleOwnerList(graph, activeExpression); + if(OrderedSetUtils.toList(graph, expressionList).size() <= 1) + return; + + Resource prev = OrderedSetUtils.prev(graph, expressionList, activeExpression); + OrderedSetUtils.remove(graph, expressionList, activeExpression); + if(prev.equals(expressionList)) { + Iterator iterator = OrderedSetUtils.iterator(graph, expressionList); + prev = iterator.next(); + } + + final Resource newActive = prev; + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + final Session session = graph.getSession(); + session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + VirtualGraph runtime = graph.getService(VirtualGraph.class); + session.asyncRequest(new WriteRequest(runtime) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.hasStatement(input, sr.HasActiveExpression)) + graph.deny(input, sr.HasActiveExpression); + graph.claim(input, sr.HasActiveExpression, newActive); + } + } + ); + } + }); + } + }); + + newExpression.addSelectionListener(new SelectionListenerImpl(context) { + + @Override + public void apply(WriteGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Resource expressions = graph.getPossibleObject(input, sr.HasExpressions); + if(expressions == null) { + return; + } + Resource activeExpression = graph.getPossibleObject(input, sr.HasActiveExpression); + Resource newExpression = graph.newResource(); + if(activeExpression != null) + graph.claim(newExpression, l0.InstanceOf, graph.getSingleObject(activeExpression, l0.InstanceOf)); + else + graph.claim(newExpression, l0.InstanceOf, sr.NormalExpression); + OrderedSetUtils.add(graph, expressions, newExpression); + } + }); expressionTypeCombo.setItemFactory(new ReadFactoryImpl>() { @@ -135,7 +382,10 @@ public class EquationTab extends PropertyTabContributorImpl { @Override public String perform(ReadGraph graph, final Resource input) throws DatabaseException { - return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString(); + Resource activeExpression = getActiveExpression(graph, input); + if(activeExpression == null) + return null; + return ExpressionTypes.getExpressionType(graph, activeExpression).toString(); } }); @@ -293,4 +543,21 @@ public class EquationTab extends PropertyTabContributorImpl { site.getPage().removePartListener(focusLostListener); super.dispose(); } + + private Resource getActiveExpression(ReadGraph graph, Resource variable) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource expression = graph.getPossibleObject(variable, sr.HasActiveExpression); + if(expression == null) { + Resource expressions = graph.getPossibleObject(variable, sr.HasExpressions); + if(expressions == null) { + return null; + } + List expressionList = OrderedSetUtils.toList(graph, expressions); + if(expressionList.isEmpty()) { + return null; + } + expression = expressionList.get(0); + } + return expression; + } } 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 1bb77cb0..57ac1c52 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 @@ -14,11 +14,14 @@ package org.simantics.sysdyn.ui.properties; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; import org.simantics.browsing.ui.SelectionProcessor; import org.simantics.browsing.ui.swt.ComparableTabContributor; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.diagram.stubs.DiagramResource; @@ -51,19 +54,40 @@ public class ResourceSelectionProcessor implements SelectionProcessor expressionList = OrderedSetUtils.toList(backend, expressions); + if(expressionList.isEmpty()) { + System.err.println("expressionList is empty for " + r); + return Collections.emptyList(); + } + expression = expressionList.get(0); + } tabs.add(new ComparableTabContributor( new EquationTab(), - 2, + 3, r, "Equation")); if(expression != null && backend.isInstanceOf(expression, sr.WithLookupExpression)) { tabs.add(new ComparableTabContributor( new LookupTableTab(), - 1, + 2, expression, "Lookup Table")); } + + tabs.add(new ComparableTabContributor( + new ArrayIndexesTab(), + 1, + r, + "Indexes")); + tabs.add(new ComparableTabContributor( new VariableInformationTab(), 0, @@ -164,7 +188,9 @@ public class ResourceSelectionProcessor implements SelectionProcessor expressionList = OrderedSetUtils.toList(graph, expressions); + for(Resource s : expressionList) { + for(Resource p : graph.getPredicates(s)) { + Resource o = graph.getPossibleObject(s, p); + if(o != null && graph.isInstanceOf(o, l0.String) && !p.equals(sr.HasArrayRange)) { + String string = graph.getRelatedValue(s, p); + String replaced = replaceAllWords(string, originalName, newName); + graph.claimLiteral(s, p, replaced); + } } } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java new file mode 100644 index 00000000..2a1f6ab7 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java @@ -0,0 +1,269 @@ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.LinkedHashMap; + +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; +import org.simantics.browsing.ui.swt.widgets.TrackedCombo; +import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; +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.Session; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.Listener; +import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.VariableNameUtils; + +public class ArrayExpressionCombo extends TrackedCombo { + + int lastSelectedIndex = -2; + + public ArrayExpressionCombo(Composite parent, WidgetSupport support, + int style) { + super(parent, support, style); + + /* + this.setInputValidator(new VariableNameValidator(support)); + */ + + this.setItemFactory(new ReadFactoryImpl>() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + Map map = new LinkedHashMap(); + if(input == null) { + return map; + } + + Layer0 l0 = Layer0.getInstance(graph); + String name = graph.getPossibleRelatedValue(input, l0.HasName); + + SysdynResource sr = SysdynResource.getInstance(graph); + + String defaultRange = getDefaultRange(graph, input); + for(Resource expression : getExpressions(graph, input)) { + String arrayRange = graph.getPossibleRelatedValue(expression, sr.HasArrayRange); + if(arrayRange != null) { + map.put(name + arrayRange, expression); + } else if(defaultRange != null) { + map.put(name + defaultRange, expression); + } else { + map.put(name, expression); + } + } + if(map.isEmpty()) { + map.put(name, input); + } + return map; + } + + }); + + + this.setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + String name = graph.getPossibleRelatedValue(input, Layer0.getInstance(graph).HasName); + + String defaultRange = getDefaultRange(graph, input); + if(defaultRange == null) + return name; + + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource activeExpression = graph.getPossibleObject(input, sr.HasActiveExpression); + Resource expression; + if(activeExpression == null) { + ArrayList expressions = getExpressions(graph, input); + if(expressions == null || expressions.isEmpty()) + return name; + expression = expressions.get(0); + } else { + expression = activeExpression; + } + String range = graph.getPossibleRelatedValue(expression, sr.HasArrayRange); + if(range != null) + return name + range; + else + return name + defaultRange; + } + }); + + this.addModifyListener(new NameAndArrayRangeModifyListener(support)); + } + + + @Override + public void setInput(ISessionContext context, Object input) { + super.setInput(context, input); + + if(selectionFactory != null) { + selectionFactory.listen(context, input, new Listener() { + + @Override + public void execute(final String result) { + getWidget().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + Combo combo = getWidget(); + if(combo != null && !combo.isDisposed()) { + Object o = getWidget().getData(result); + if(o != null) + lastSelectedIndex = (Integer)o; + } + + } + }); + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return getWidget().isDisposed(); + } + + }); + } + } + + private ArrayList getExpressions(ReadGraph graph, Resource variable) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource hasExpressions = graph.getPossibleObject(variable, sr.HasExpressions); + if(hasExpressions == null) + return new ArrayList(); + else + return new ArrayList(OrderedSetUtils.toList(graph, hasExpressions)); + } + + public static String getDefaultRange(ReadGraph graph, Resource variable) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource hasArrayIndexes = graph.getPossibleObject(variable, sr.HasArrayIndexes); + + if(hasArrayIndexes == null) + return null; + + Iterator iterator = OrderedSetUtils.iterator(graph, hasArrayIndexes); + if(!iterator.hasNext()) + return null; + + StringBuilder sb = new StringBuilder(); + sb.append("["); + + while(iterator.hasNext()) { + sb.append(NameUtils.getSafeName(graph, iterator.next())); + if(iterator.hasNext()) { + sb.append(", "); + } + } + sb.append("]"); + return sb.toString(); + } + + private class NameAndArrayRangeModifyListener extends ComboModifyListenerImpl implements Widget { + + Resource lastExpression; + + public NameAndArrayRangeModifyListener(WidgetSupport support) { + support.register(this); + } + + @Override + public void setInput(ISessionContext context, Object input) { + super.setInput(context, input); + } + + @Override + public void modifyText(TrackedModifyEvent e) { + + Combo combo = (Combo)e.getWidget(); + LinkedHashMap data = (LinkedHashMap) combo.getData(); + + Resource expression = (Resource) data.get(combo.getText()); + if(expression != null) { + lastExpression = expression; + lastSelectedIndex = combo.getSelectionIndex(); + } else { + for(Object key : data.keySet()) { + int index = lastSelectedIndex < 0 ? 0 : lastSelectedIndex; + if((Integer)combo.getData((String)key) == index) { + lastExpression = (Resource) data.get((String)key); + break; + } + } + } + + super.modifyText(e); + } + + @Override + public void applyText(WriteGraph graph, final Resource variable, String text) + throws DatabaseException { + StringTokenizer st = new StringTokenizer(text, "[]"); + final String newName = st.nextToken(); + String range = null; + if(st.hasMoreTokens()) { + range = st.nextToken(); + } + String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName); + if(!originalName.equals(newName)) { + VariableNameUtils.renameInEquations(graph, variable, originalName, newName); + graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName); + } + + SysdynResource sr = SysdynResource.getInstance(graph); + + if(range != null && lastExpression != null) { + String oldRange = graph.getPossibleRelatedValue(lastExpression, sr.HasArrayRange); + if(oldRange == null || !range.equals(oldRange)) { + graph.claimLiteral(lastExpression, sr.HasArrayRange, "[" + range + "]"); + } + } + + Resource activeExpression = graph.getPossibleObject(variable, sr.HasActiveExpression); + + if(lastExpression != null && !lastExpression.equals(activeExpression)) { + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + final Session session = graph.getSession(); + session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + VirtualGraph runtime = graph.getService(VirtualGraph.class); + session.asyncRequest(new WriteRequest(runtime) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.hasStatement(variable, sr.HasActiveExpression)) + graph.deny(variable, sr.HasActiveExpression); + graph.claim(variable, sr.HasActiveExpression, lastExpression); + } + } + ); + } + }); + } + } + } +} + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java index 4ae9bdce..0a5c8bc2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartTableWidget.java @@ -167,8 +167,9 @@ public class ChartTableWidget implements Widget { @Override public String perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - String lookup = graph.getRelatedValue(expression, sr.HasLookup); - return lookup; + if(!graph.isInstanceOf(expression, sr.WithLookupExpression)) + return null; + return graph.getRelatedValue(expression, sr.HasLookup); } }, new Listener() { @@ -179,6 +180,7 @@ public class ChartTableWidget implements Widget { @Override public void execute(String lookup) { + if(lookup == null) return; TableParser parser = new TableParser(new StringReader("")); parser.ReInit(new StringReader(lookup)); table.clearTable(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java index 0cbcbd0a..990bb3de 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ChartWidget.java @@ -98,8 +98,10 @@ public class ChartWidget implements Widget { @Override public Auxiliary perform(ReadGraph graph) throws DatabaseException { - Auxiliary auxiliary = new Auxiliary(); - SysdynResource sr = SysdynResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + if(!graph.isInstanceOf(expression, sr.WithLookupExpression)) + return null; + Auxiliary auxiliary = new Auxiliary(); auxiliary.minX = graph.getRelatedValue(expression, sr.HasMinX); auxiliary.maxX = graph.getRelatedValue(expression, sr.HasMaxX); auxiliary.minY = graph.getRelatedValue(expression, sr.HasMinY); @@ -116,7 +118,7 @@ public class ChartWidget implements Widget { @Override public void execute(Auxiliary result) { - + if(result == null) return; XYDataset dataset = createDataset(result.table); chartPanel.resetChart(dataset); chartPanel.addSeriesChangeListener(new _SeriesChangeListener(expression)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java index 70210fa9..967b1ff4 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java @@ -11,8 +11,11 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.properties.widgets; +import java.util.List; + import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.sysdyn.SysdynResource; @@ -20,80 +23,61 @@ import org.simantics.ui.SimanticsUI; public class ExpressionTypes { - public static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay, Empty}; - - public static ExpressionType[] auxiliaryExpressions = new ExpressionType[] { - ExpressionType.Auxiliary, - ExpressionType.Parameter, - ExpressionType.Constant, - // ExpressionType.Lookup, - ExpressionType.WithLookup}; - - public static ExpressionType[] valveExpressions = new ExpressionType[] { - ExpressionType.Auxiliary, - ExpressionType.Parameter, - ExpressionType.Constant, - ExpressionType.WithLookup}; + public static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay, Empty}; - public static ExpressionType[] stockExpressions = new ExpressionType[] { - ExpressionType.Stock}; + public static ExpressionType[] auxiliaryExpressions = new ExpressionType[] { + ExpressionType.Auxiliary, + ExpressionType.Parameter, + ExpressionType.Constant, + // ExpressionType.Lookup, + ExpressionType.WithLookup}; - public static ExpressionType getExpressionTypeForVariable(final Resource variable) { - try { - return SimanticsUI.getSession().syncRequest(new Read() { + public static ExpressionType[] valveExpressions = new ExpressionType[] { + ExpressionType.Auxiliary, + ExpressionType.Parameter, + ExpressionType.Constant, + ExpressionType.WithLookup}; - @Override - public ExpressionType perform(ReadGraph graph) throws DatabaseException { - return getExpressionTypeForVariable(graph, variable); - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - return null; - } + public static ExpressionType[] stockExpressions = new ExpressionType[] { + ExpressionType.Stock}; - } + public static ExpressionType getExpressionType(final Resource expression) { + try { + return SimanticsUI.getSession().syncRequest(new Read() { - public static ExpressionType getExpressionTypeForVariable(ReadGraph graph, final Resource variable) { - ExpressionType et = null; - try { - SysdynResource sr = SysdynResource.getInstance(graph); + @Override + public ExpressionType perform(ReadGraph graph) throws DatabaseException { + return getExpressionType(graph, expression); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + return null; + } - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if(expression == null) { - if(graph.isInstanceOf(variable, sr.Auxiliary)) { - et = ExpressionType.Auxiliary; - } - else if(graph.isInstanceOf(variable, sr.Valve)) { - et = ExpressionType.Auxiliary; - } - else if(graph.isInstanceOf(variable, sr.Stock)) { - et = ExpressionType.Stock; - } else - et = ExpressionType.Empty; - } else { - if(graph.isInstanceOf(expression, sr.NormalExpression)) { - et = ExpressionType.Auxiliary; - } else if (graph.isInstanceOf(expression, sr.StockExpression)) { - et = ExpressionType.Stock; - } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) { - et = ExpressionType.Parameter; - } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) { - et = ExpressionType.Constant; - } else if (graph.isInstanceOf(expression, sr.DelayExpression)) { - et = ExpressionType.Delay; - } else if (graph.isInstanceOf(expression, sr.LookupExpression)) { - et = ExpressionType.Lookup; - } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) { - et = ExpressionType.WithLookup; - } else { - et = ExpressionType.Empty; - } - } - } catch (DatabaseException e) { - e.printStackTrace(); - } - return et; - } + } + + public static ExpressionType getExpressionType(ReadGraph graph, final Resource expression) throws DatabaseException { + ExpressionType et = null; + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.isInstanceOf(expression, sr.NormalExpression)) { + et = ExpressionType.Auxiliary; + } else if (graph.isInstanceOf(expression, sr.StockExpression)) { + et = ExpressionType.Stock; + } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) { + et = ExpressionType.Parameter; + } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) { + et = ExpressionType.Constant; + } else if (graph.isInstanceOf(expression, sr.DelayExpression)) { + et = ExpressionType.Delay; + } else if (graph.isInstanceOf(expression, sr.LookupExpression)) { + et = ExpressionType.Lookup; + } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) { + et = ExpressionType.WithLookup; + } else { + et = ExpressionType.Empty; + } + return et; + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java index fa4c88a3..9aec199d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java @@ -14,41 +14,31 @@ package org.simantics.sysdyn.ui.properties.widgets; import java.util.HashMap; import java.util.Map; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Table; -import org.eclipse.swt.widgets.TableItem; 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.exception.DatabaseException; import org.simantics.db.management.ISessionContext; -import org.simantics.db.request.Read; -import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType; import org.simantics.sysdyn.ui.properties.widgets.expressions.AuxiliaryExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.ConstantExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.DelayExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.EmptyExpression; -import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionUtils; import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.LookupExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.ParameterExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.StockExpression; import org.simantics.sysdyn.ui.properties.widgets.expressions.WithLookupExpression; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.ISelectionUtils; +import org.simantics.ui.utils.AdaptionUtils; public class ExpressionWidget implements Widget { - private Resource variable; + private Resource expr; private Composite parent; private Map data; private IExpression expression; @@ -65,27 +55,16 @@ public class ExpressionWidget implements Widget { } @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; - } - } - } - - ExpressionType et = ExpressionTypes.getExpressionTypeForVariable(variable); - + public void setInput(ISessionContext context, Object input) { + expr = AdaptionUtils.adaptToSingle(input, Resource.class); + ExpressionType et = ExpressionTypes.getExpressionType(expr); displayExpression(et.toString(), true); - } - public void displayExpression(String expressionType, boolean original) { - if(expressionType == null) - return; + if(expressionType == null) { + return; + } if(this.expression != null) expression.updateData(data); ExpressionType et = ExpressionType.valueOf(expressionType); @@ -100,7 +79,7 @@ public class ExpressionWidget implements Widget { case Lookup: exp = new LookupExpression(); break; case WithLookup: - exp = new WithLookupExpression(variable); break; + exp = new WithLookupExpression(expr); break; case Stock: exp = new StockExpression(); break; case Delay: @@ -115,7 +94,7 @@ public class ExpressionWidget implements Widget { } if(original) - exp.readData(variable, data); + exp.readData(expr, data); exp.createExpressionFields(parent, data); if(modifyListener != null) @@ -142,6 +121,7 @@ public class ExpressionWidget implements Widget { public void validateFields() { if(this.variableTable == null) return; + /* TableItem[] connectedVariables = this.variableTable.getItems(); try { final Resource configuration = SimanticsUI.getSession().syncRequest(new Read() { @@ -149,6 +129,9 @@ public class ExpressionWidget implements Widget { @Override public Resource perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); + + + Resource configuration = variable; do { @@ -164,6 +147,7 @@ public class ExpressionWidget implements Widget { } catch (DatabaseException e1) { e1.printStackTrace(); } + */ } public void addModifyListener(ModifyListener listener) { @@ -179,7 +163,7 @@ public class ExpressionWidget implements Widget { } public void save() { - this.expression.save(variable, data); + this.expression.save(expr, data); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java index fb61ce7c..ded5da75 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java @@ -45,14 +45,15 @@ public class ShortcutTabWidget implements Widget { TabItem functions; Table variableTable; Table functionTable; + Composite composite; public ShortcutTabWidget(Composite parent, WidgetSupport support, int style) { - support.register(this); + if(support!=null) + support.register(this); - Composite composite = new Composite(parent, style); - GridDataFactory.fillDefaults().span(1, 2).grab(false, true).applyTo(composite); + composite = new Composite(parent, style); GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite); - + tabFolder = new TabFolder (composite, SWT.NONE); GridDataFactory.fillDefaults().grab(false, true).applyTo(tabFolder); GridLayoutFactory.fillDefaults().applyTo(tabFolder); @@ -89,6 +90,10 @@ public class ShortcutTabWidget implements Widget { functions.setControl(functionTable); } + public Composite getWidget() { + return composite; + } + @Override public void setInput(ISessionContext context, Object input) { if(input instanceof IStructuredSelection) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java new file mode 100644 index 00000000..9b2c37e2 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java @@ -0,0 +1,39 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +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.sysdyn.SysdynResource; + +public class AvailableEnumerations extends ViewpointContributorImpl { + + @Override + public Collection getContribution(ReadGraph graph, Resource input) + throws DatabaseException { + if(input == null) return null; + Layer0 l0 = Layer0.getInstance(graph); + Resource configuration = graph.getPossibleObject(input, l0.PartOf); + ArrayList> result = new ArrayList>(); + SysdynResource sr = SysdynResource.getInstance(graph); + if(configuration == null) + return result; + for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) { + result.add(new EnumerationNode(r)); + } + return result; + } + + @Override + public String getViewpointId() { + return "Used enumerations"; + } + + +} 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 new file mode 100644 index 00000000..d55a10f3 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationLabeler.java @@ -0,0 +1,43 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.HashMap; +import java.util.ListIterator; +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.common.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; + +public class EnumerationLabeler extends ColumnLabelerContributorImpl{ + + @Override + public Map getLabel(ReadGraph graph, EnumerationNode input) + throws DatabaseException { + + String name = NameUtils.getSafeName(graph, input.data); + HashMap map = new HashMap(); + map.put(Keys.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(); + sb.append("["); + while(indexes.hasNext()) { + Resource i = indexes.next(); + sb.append(NameUtils.getSafeName(graph, i)); + if(indexes.hasNext()) + sb.append(", "); + } + sb.append("]"); + map.put(Keys.INDEXES, sb.toString()); + return map; + } + + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationNode.java new file mode 100644 index 00000000..2d960058 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/EnumerationNode.java @@ -0,0 +1,12 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.db.Resource; + +public class EnumerationNode extends AbstractNode { + + public EnumerationNode(Resource resource) { + super(resource); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/Keys.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/Keys.java new file mode 100644 index 00000000..57dd8e2b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/Keys.java @@ -0,0 +1,18 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import org.simantics.browsing.ui.Column; +import org.simantics.browsing.ui.Column.Align; + +public class Keys { + + public static final String ENUMERATION = "Enumeration"; + public static final String INDEXES = "Indexes"; + + public static String[] ENUMERATION_COLUMNS_KEYS = { ENUMERATION, INDEXES }; + public static Column[] ENUMERATION_TABLE_COLUMNS = new Column[] { + new Column(ENUMERATION, Align.LEFT, 100, "Enumeration", false), + new Column(INDEXES, Align.LEFT, 100, "Indexes", true), + }; + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java new file mode 100644 index 00000000..35d094f0 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java @@ -0,0 +1,37 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +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.utils.OrderedSetUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; + +public class UsedEnumerations extends ViewpointContributorImpl { + + @Override + public Collection getContribution(ReadGraph graph, Resource input) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + + ArrayList> result = new ArrayList>(); + + Resource arrayIndexes = graph.getPossibleObject(input, sr.HasArrayIndexes); + if(arrayIndexes == null) { + return result; + } + for(Resource r : OrderedSetUtils.toList(graph, arrayIndexes)) { + result.add(new EnumerationNode(r)); + } + return result; + } + + @Override + public String getViewpointId() { + return "Available enumerations"; + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java index a805d3be..ebf4d420 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java @@ -27,13 +27,14 @@ import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; -import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; 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.DatabaseException; import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; @@ -68,16 +69,15 @@ public class BasicExpression implements IExpression { } @Override - public void readData(final Resource variable, Map data) { + public void readData(final Resource expression, Map data) { String equation = null; - if (variable != null && data.get("equation") == null) { + if (expression != null && data.get("equation") == null) { try { equation = SimanticsUI.getSession().syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); if (expression != null) { String equation = graph.getPossibleRelatedValue(expression, sr.HasEquation); if(equation != null) @@ -111,8 +111,8 @@ public class BasicExpression implements IExpression { } @Override - public void save(final Resource variable, Map data) { - final String currentText = expression.getExpression(); + public void save(final Resource expression, Map data) { + final String currentText = this.expression.getExpression(); final String oldEquation = (String)data.get("equation"); if(oldEquation == null || (currentText != null && expressionType != null)) { @@ -122,7 +122,6 @@ public class BasicExpression implements IExpression { public void perform(WriteGraph g) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); - Resource expression = g.getPossibleObject(variable, sr.HasExpression); if(ExpressionUtils.isParameter(currentText)) { if(!expressionType.equals(sr.ConstantExpression)) expressionType = sr.ParameterExpression; @@ -135,16 +134,13 @@ public class BasicExpression implements IExpression { && currentText.equals(oldEquation)) { return; } - Builtins b = g.getBuiltins(); - if(expression != null && !g.isInstanceOf(expression, expressionType)) { - g.deny(variable, sr.HasExpression); - expression = null; - } - if(expression == null) { - expression = g.newResource(); - g.claim(expression, b.InstanceOf, null, expressionType); - g.claim(variable, sr.HasExpression, expression); - } + + Layer0 l0 = Layer0.getInstance(g); + + g.deny(expression, l0.InstanceOf); + g.deny(expression, sr.HasEquation); + + g.claim(expression, l0.InstanceOf, null, expressionType); g.claimLiteral(expression, sr.HasEquation, currentText); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java index cbf98765..191960cb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java @@ -17,6 +17,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.StringTokenizer; import org.eclipse.jface.text.Position; import org.eclipse.swt.custom.StyledText; @@ -47,7 +48,10 @@ public class ExpressionUtils { */ static public boolean isParameter(String expression) { try { - Double.parseDouble(expression); + StringTokenizer st = new StringTokenizer(expression, "{}[],;"); + while(st.hasMoreTokens()) { + Double.parseDouble(st.nextToken().trim()); + } return true; } catch (NumberFormatException e) { return false; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java index e36371ff..1491cd32 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java @@ -25,9 +25,9 @@ public interface IExpression { public void createExpressionFields(Composite parent, Map data); - public void readData(final Resource variable, Map data); + public void readData(final Resource expression, Map data); - public void save(final Resource variable, Map data); + public void save(final Resource expression, Map data); public void focus(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java index dd03e20c..9357b4cf 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2007, 2010 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 @@ -33,6 +33,7 @@ import org.simantics.db.ReadGraph; 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.DatabaseException; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; @@ -81,18 +82,17 @@ public class StockExpression implements IExpression { } @Override - public void readData(final Resource variable, Map data) { + public void readData(final Resource expression, Map data) { String initialEquation = null; - if (variable != null && data.get("initialEquation") == null) { + if (expression != null && data.get("initialEquation") == null) { try { initialEquation = SimanticsUI.getSession().syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if (expression != null && graph.isInstanceOf(expression, sr.StockExpression)) { + if (graph.isInstanceOf(expression, sr.StockExpression)) { return graph.getRelatedValue(expression, sr.HasInitialEquation); } else { return ""; @@ -106,7 +106,7 @@ public class StockExpression implements IExpression { data.put("initialEquation", initialEquation); } - data.put("integral", getIntegral(variable)); + data.put("integral", getIntegral(expression)); } @@ -125,32 +125,34 @@ public class StockExpression implements IExpression { } @Override - public void save(final Resource variable, Map data) { - final String currentText = expression.getExpression(); + public void save(final Resource expression, Map data) { + final String currentText = this.expression.getExpression(); if(currentText != null) { SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { + System.out.println("write stock"); SysdynResource sr = SysdynResource.getInstance(g); - Resource expression = g.getPossibleObject(variable, sr.HasExpression); Layer0 l0 = Layer0.getInstance(g); - if(expression != null && !g.isInstanceOf(expression, sr.StockExpression)) { - g.deny(variable, sr.HasExpression); - expression = null; + + if(!g.isInstanceOf(expression, sr.StockExpression)) { + Resource expressionList = g.getSingleObject(expression, l0.HasNext); + Resource temp = g.newResource(); + OrderedSetUtils.replace(g, expressionList, expression, temp); + for(Resource predicate : g.getPredicates(expression)) { + g.deny(expression, predicate); + } + g.claim(expression, l0.InstanceOf, null, sr.StockExpression); + OrderedSetUtils.replace(g, expressionList, temp, expression); } - if(expression == null) { - expression = g.newResource(); - g.claim(expression, l0.InstanceOf, null, sr.StockExpression); - g.claim(variable, sr.HasExpression, expression); - } g.claimLiteral(expression, sr.HasInitialEquation, currentText); } }); } - expression.setExpression(currentText); + this.expression.setExpression(currentText); } @Override @@ -162,8 +164,10 @@ public class StockExpression implements IExpression { } - private String getIntegral(final Resource variable) { + private String getIntegral(final Resource expression) { String integral = ""; + if(expression == null) + return integral; try { integral = SimanticsUI.getSession().syncRequest(new Read() { @@ -171,6 +175,10 @@ public class StockExpression implements IExpression { public String perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); + + // find the variable + Resource expressionList = OrderedSetUtils.getSingleOwnerList(graph, expression); + Resource variable = graph.getSingleObject(expressionList, sr.HasExpressions_Inverse); Collection heads = graph.getObjects(variable, sr.IsHeadOf); Collection tails = graph.getObjects(variable, sr.IsTailOf); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java index 96eecd76..d01c6ce0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java @@ -51,6 +51,7 @@ import org.simantics.db.ReadGraph; 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.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; @@ -72,10 +73,10 @@ public class WithLookupExpression implements IExpression { private ChartPanel smallPanel; private Frame smallFrame; - private Resource variable; + private Resource expr; - public WithLookupExpression(Resource variable) { - this.variable = variable; + public WithLookupExpression(Resource expression) { + this.expr = expression; } @Override @@ -125,7 +126,7 @@ public class WithLookupExpression implements IExpression { @Override public void focusLost(FocusEvent e) { lastSelectedText = lookup; - save(variable, data); + save(expr, data); } }); @@ -146,10 +147,9 @@ public class WithLookupExpression implements IExpression { @Override public String perform(ReadGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); String result = ""; - if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) { - result = graph.getRelatedValue(expression, sr.HasLookup); + if (expr != null && graph.isInstanceOf(expr, sr.WithLookupExpression)) { + result = graph.getRelatedValue(expr, sr.HasLookup); } return result; } @@ -196,7 +196,7 @@ public class WithLookupExpression implements IExpression { } @Override - public void readData(final Resource variable, Map data) { + public void readData(final Resource expression, Map data) { class Auxiliary { String equation, lookup; @@ -204,7 +204,7 @@ public class WithLookupExpression implements IExpression { Auxiliary results = null; - if (variable != null && data.get("equation") == null) { + if (data.get("equation") == null) { try { results = SimanticsUI.getSession().syncRequest(new Read() { @@ -212,7 +212,6 @@ public class WithLookupExpression implements IExpression { public Auxiliary perform(ReadGraph graph) throws DatabaseException { Auxiliary results = new Auxiliary(); SysdynResource sr = SysdynResource.getInstance(graph); - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) { results.equation = graph.getRelatedValue(expression, sr.HasEquation); results.lookup = graph.getRelatedValue(expression, sr.HasLookup); @@ -265,24 +264,23 @@ public class WithLookupExpression implements IExpression { public void perform(WriteGraph g) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); - Resource expression = g.getPossibleObject(variable, sr.HasExpression); Layer0 l0 = Layer0.getInstance(g); - if(expression != null && !g.isInstanceOf(expression, sr.WithLookupExpression)) { - g.deny(variable, sr.HasExpression); - expression = null; + if(!g.isInstanceOf(expr, sr.WithLookupExpression)) { + Resource expressionList = g.getSingleObject(expr, l0.HasNext); + Resource temp = g.newResource(); + OrderedSetUtils.replace(g, expressionList, expr, temp); + for(Resource predicate : g.getPredicates(expr)) { + g.deny(expr, predicate); + } + g.claim(expr, l0.InstanceOf, null, sr.WithLookupExpression); + g.claimLiteral(expr, sr.HasMinX, 0.0); + g.claimLiteral(expr, sr.HasMaxX, 10.0); + g.claimLiteral(expr, sr.HasMinY, 0.0); + g.claimLiteral(expr, sr.HasMaxY, 10.0); + OrderedSetUtils.replace(g, expressionList, temp, expr); } - if(expression == null) { - expression = g.newResource(); - g.claim(expression, l0.InstanceOf, null, sr.WithLookupExpression); - g.claim(variable, sr.HasExpression, expression); - g.claimLiteral(expression, sr.HasMinX, 0.0); - g.claimLiteral(expression, sr.HasMaxX, 10.0); - g.claimLiteral(expression, sr.HasMinY, 0.0); - g.claimLiteral(expression, sr.HasMaxY, 10.0); - - } - g.claimLiteral(expression, sr.HasEquation, currentExpression); - g.claimLiteral(expression, sr.HasLookup, currentLookupTable); + g.claimLiteral(expr, sr.HasEquation, currentExpression); + g.claimLiteral(expr, sr.HasLookup, currentLookupTable); } }); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNamePropertyModifier.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNamePropertyModifier.java index 09363bb8..4025beb8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNamePropertyModifier.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNamePropertyModifier.java @@ -33,6 +33,10 @@ public class VariableNamePropertyModifier extends TextModifyListenerImpl enumerations = new ArrayList(); + + public ArrayList getEnumerations() { + return enumerations; + } + +} 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 4176277f..96aed4cc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Enumeration.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.representation; +import java.util.ArrayList; + import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @@ -28,4 +30,20 @@ public class Enumeration extends Variable { sb.append(");\n"); return sb.toString(); } + + public ArrayList getEnumerationIndexes() { + return enumerationIndexes.getEnumerationIndexes(); + } + + public int indexOf(String index) { + int result = -1; + ArrayList indexes = getEnumerationIndexes(); + for(int i = 0; i < indexes.size(); i++) { + if(indexes.get(i).equals(index)) { + result = i +1; + break; + } + } + return result; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java index cdc559ee..50fed1bb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java @@ -11,15 +11,73 @@ *******************************************************************************/ package org.simantics.sysdyn.representation; -import org.simantics.objmap.annotations.RelatedElement; +import java.util.ArrayList; +import java.util.Iterator; + import org.simantics.sysdyn.representation.expressions.IExpression; +import org.simantics.sysdyn.representation.expressions.ParameterExpression; public abstract class IndependentVariable extends Variable { - - @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasExpression") - protected IExpression expression; - public IExpression getExpression() { - return this.expression; + public String getDeclaration() { + ArrayList expressions = this.expressions.getExpressions(); + ArrayIndexes ai = this.getArrayIndexes(); + ArrayList enumerations = null; + if(ai != null) + enumerations = ai.getEnumerations(); + if(enumerations == null || enumerations.size() < 1) { + // NOT an array variable, get declaration from the only expression. + if(expressions.get(0) == null) + return null; + else + return expressions.get(0).getDeclaration(this); + } else { + // ARRAY variable. Create declaration according to the type, name and indexes + //TODO: check if all of them are parameters -> form a parameter + StringBuilder sb = new StringBuilder(); + sb.append(" "); + sb.append(this.getType()); + sb.append(" "); + sb.append(this.getName()); + sb.append("["); + Iterator iterator = enumerations.iterator(); + while(iterator.hasNext()) { + sb.append(iterator.next().getName()); + if(iterator.hasNext()) { + sb.append(", "); + } + } + sb.append("];\n"); + return sb.toString(); + } + } + + public String getInitialEquation() { + return expressions.getExpressions().get(0).getInitialEquation(this); + } + + public String getEquation() { + ArrayList expressions = this.expressions.getExpressions(); + ArrayIndexes ai = this.getArrayIndexes(); + ArrayList enumerations = null; + if(ai != null) + enumerations = ai.getEnumerations(); + if(enumerations == null || enumerations.size() < 1) { + // NOT an array variable, get equation from the only expression. + IExpression expression = expressions.get(0); + if(expression == null || expression instanceof ParameterExpression) + return null; + else + return expressions.get(0).getEquation(this); + } else { + // ARRAY variable. Create all equations for the variable + //TODO: check that it has not been a parameter + StringBuilder sb = new StringBuilder(); + for(IExpression expression : expressions) { + sb.append(expression.getEquation(this)); + } + 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 3c0888e0..ed8e9f8d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java @@ -16,6 +16,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.schema.MappingSchemas; import org.simantics.objmap.schema.SimpleSchema; import org.simantics.sysdyn.representation.expressions.ConstantExpression; +import org.simantics.sysdyn.representation.expressions.Expressions; import org.simantics.sysdyn.representation.expressions.LookupExpression; import org.simantics.sysdyn.representation.expressions.NormalExpression; import org.simantics.sysdyn.representation.expressions.ParameterExpression; @@ -37,9 +38,11 @@ public class SysdynSchema extends SimpleSchema { addLinkType(MappingSchemas.fromAnnotations(g, Input.class)); addLinkType(MappingSchemas.fromAnnotations(g, ModuleType.class)); addLinkType(MappingSchemas.fromAnnotations(g, Model.class)); + addLinkType(MappingSchemas.fromAnnotations(g, Expressions.class)); addLinkType(MappingSchemas.fromAnnotations(g, Enumeration.class)); addLinkType(MappingSchemas.fromAnnotations(g, EnumerationIndex.class)); addLinkType(MappingSchemas.fromAnnotations(g, EnumerationIndexes.class)); + addLinkType(MappingSchemas.fromAnnotations(g, ArrayIndexes.class)); addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, ParameterExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, StockExpression.class)); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java index 1b9c1990..4ad6ab41 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -13,15 +13,25 @@ package org.simantics.sysdyn.representation; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.representation.expressions.Expressions; public abstract class Variable implements IElement { @RelatedValue("http://www.simantics.org/Layer0-1.0/HasType") protected String type; + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") protected String name; + @RelatedElement("http://www.simantics.org/Layer0-1.0/PartOf") protected Configuration configuration; + + @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasArrayIndexes") + protected ArrayIndexes arrayIndexes; + + @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasExpressions") + protected Expressions expressions; + public String getName() { return this.name; @@ -34,4 +44,12 @@ public abstract class Variable implements IElement { public String getType() { return this.type; } + + public ArrayIndexes getArrayIndexes() { + return this.arrayIndexes; + } + + public Expressions getExpressions() { + return this.expressions; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java index 64ac5ce2..1fd149ef 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java @@ -11,10 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.expressions; +import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.representation.IndependentVariable; public abstract class Expression implements IExpression { + @RelatedValue("http://www.simantics.org/Sysdyn-1.0/HasArrayRange") + private String range; + @Override public String getDeclaration(IndependentVariable variable) { return null; @@ -29,5 +33,13 @@ public abstract class Expression implements IExpression { public String getInitialEquation(IndependentVariable variable) { return null; } + + @Override + public String getArrayRange() { + if(range == null) + return ""; + else + return range; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expressions.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expressions.java new file mode 100644 index 00000000..d92336d8 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expressions.java @@ -0,0 +1,17 @@ +package org.simantics.sysdyn.representation.expressions; + +import java.util.ArrayList; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedOrderedSetElements; + +@GraphType("http://www.simantics.org/Sysdyn-1.0/Expressions") +public class Expressions { + + @RelatedOrderedSetElements + private ArrayList expressions = new ArrayList(); + + public ArrayList getExpressions() { + return expressions; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java index 5bf4d102..54431347 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java @@ -18,5 +18,6 @@ public interface IExpression { String getDeclaration(IndependentVariable variable); String getInitialEquation(IndependentVariable variable); String getEquation(IndependentVariable variable); + String getArrayRange(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java index 1d8be4bb..09949c58 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/NormalExpression.java @@ -28,7 +28,7 @@ public class NormalExpression extends Expression { @Override public String getEquation(IndependentVariable variable) { - return " " + variable.getName() + " = " + equation + ";\n"; + return " " + variable.getName() + this.getArrayRange() + " = " + equation + ";\n"; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java index fc5c7f69..91ec0c01 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/ParameterExpression.java @@ -29,7 +29,16 @@ public class ParameterExpression extends Expression { return b.toString(); } - public double getValue() { - return Double.parseDouble(equation); + + /** + * Used when the expression is a part of an array variable. Then it is like a normal auxiliary. + */ + @Override + public String getEquation(IndependentVariable variable) { + return " " + variable.getName() + this.getArrayRange() + " = " + equation + ";\n"; + }; + + public Double getValue() { + return Double.parseDouble(equation); } }