]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Ranges are verified inside expressions. Similar use cases as in expression range...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 14 Dec 2010 11:54:46 +0000 (11:54 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 14 Dec 2010 11:54:46 +0000 (11:54 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19147 ac1ea38d-2e2b-0410-8846-a27921b304fc

17 files changed:
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ArrayExpressionCombo.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ShortcutTabWidget.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionUtils.java [deleted file]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNamePropertyModifier.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameUtils.java [moved from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameUtils.java with 72% similarity]
org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java

index 61deb08809bfc631a427bc0bc5438ee6ba138908..2b583a4a7bcf4d584756871ff0fce1202d67e2c3 100644 (file)
@@ -30,7 +30,7 @@ import org.simantics.g2d.element.IElement;
 import org.simantics.g2d.utils.Alignment;\r
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.scenegraph.g2d.G2DParentNode;\r
-import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.Callback;\r
 import org.simantics.utils.datastructures.hints.IHintListener;\r
index 63eb2ce88cce8f50f0b0c383200cded2613fb4df..377019ab01571c487bfe96f8c37a95f4d6a259dc 100644 (file)
@@ -38,6 +38,7 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.sysdyn.ui.properties.widgets.factories.ComboStringPropertyModifier;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
 \r
 public class ConfigurationTab extends PropertyTabContributorImpl {\r
 \r
index a913f9ed02fe4d41ee7f65f51c80471f465e557e..4745bc3f38ad527cbc11e6e16f4e1319813a7c59 100644 (file)
@@ -289,6 +289,14 @@ public class EquationTab extends PropertyTabContributorImpl implements Widget {
 \r
        private void addListeners(ISessionContext context) {\r
 \r
+               shortcutTabWidget.addDependencyListener(new Runnable() {\r
+                       \r
+                       @Override\r
+                       public void run() {\r
+                               expressionWidget.validateFields();\r
+                       }\r
+               });\r
+               \r
                deleteExpression.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
 \r
                        @Override\r
index 2a1f6ab733815d2120c5b1618d9b72bd5741d8d0..0053e4fabe56ef0f3581b6cc72c0c1c8867a94fa 100644 (file)
@@ -28,7 +28,7 @@ import org.simantics.db.procedure.Listener;
 import org.simantics.db.service.VirtualGraphSupport;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
 \r
 public class ArrayExpressionCombo extends TrackedCombo {\r
 \r
@@ -119,6 +119,7 @@ public class ArrayExpressionCombo extends TrackedCombo {
 \r
                                @Override\r
                                public void execute(final String result) {\r
+                                       if(getWidget().isDisposed()) return;\r
                                        getWidget().getDisplay().asyncExec(new Runnable() {\r
                                                \r
                                                @Override\r
index 9aec199d984d14c614ac14dfb85b9a5ed410697c..bb14670255eaa5a0168005d0e5d2aa02fb2a9431 100644 (file)
@@ -22,8 +22,13 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Table;\r
 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.AuxiliaryExpression;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ConstantExpression;\r
@@ -34,136 +39,131 @@ import org.simantics.sysdyn.ui.properties.widgets.expressions.LookupExpression;
 import org.simantics.sysdyn.ui.properties.widgets.expressions.ParameterExpression;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.StockExpression;\r
 import org.simantics.sysdyn.ui.properties.widgets.expressions.WithLookupExpression;\r
+import org.simantics.sysdyn.ui.utils.ExpressionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.AdaptionUtils;\r
 \r
 public class ExpressionWidget implements Widget {\r
-    \r
-    private Resource expr;\r
-    private Composite parent;\r
-    private Map<String, Object> data;\r
-    private IExpression expression;\r
-    private ModifyListener modifyListener;\r
-    private FocusListener focusListener;\r
-    private Table variableTable;\r
+\r
+       private Resource expr;\r
+       private Composite parent;\r
+       private Map<String, Object> data;\r
+       private IExpression expression;\r
+       private ModifyListener modifyListener;\r
+       private FocusListener focusListener;\r
+       private Table variableTable;\r
        private VerifyKeyListener verifyKeyListener;\r
-    \r
-    \r
-    public ExpressionWidget(Composite parent, WidgetSupport support, int style) {\r
-        support.register(this);\r
-        this.parent = parent;\r
-        this.data = new HashMap<String, Object>();\r
-    }\r
-\r
-    @Override\r
-    public void setInput(ISessionContext context, Object input) {  \r
-       expr = AdaptionUtils.adaptToSingle(input, Resource.class);\r
-        ExpressionType et = ExpressionTypes.getExpressionType(expr);\r
-        displayExpression(et.toString(), true);\r
-    }\r
-    \r
-    public void displayExpression(String expressionType, boolean original) {\r
-        if(expressionType == null) {\r
+\r
+\r
+       public ExpressionWidget(Composite parent, WidgetSupport support, int style) {\r
+               support.register(this);\r
+               this.parent = parent;\r
+               this.data = new HashMap<String, Object>();\r
+       }\r
+\r
+       @Override\r
+       public void setInput(ISessionContext context, Object input) {  \r
+               expr = AdaptionUtils.adaptToSingle(input, Resource.class);\r
+               ExpressionType et = ExpressionTypes.getExpressionType(expr);\r
+               displayExpression(et.toString(), true);\r
+       }\r
+\r
+       public void displayExpression(String expressionType, boolean original) {\r
+               if(expressionType == null) {\r
                        return;\r
-        }\r
-\r
-        if(this.expression != null) expression.updateData(data);\r
-        ExpressionType et = ExpressionType.valueOf(expressionType);\r
-        IExpression exp = null;\r
-        switch (et) {\r
-            case Auxiliary: \r
-                exp = new AuxiliaryExpression(); break;\r
-            case Parameter: \r
-                exp = new ParameterExpression(); break;\r
-            case Constant: \r
-                exp = new ConstantExpression(); break;\r
-            case Lookup: \r
-                exp = new LookupExpression(); break;\r
-            case WithLookup: \r
-                exp = new WithLookupExpression(expr); break;\r
-            case Stock: \r
-                exp = new StockExpression(); break;\r
-            case Delay: \r
-                exp = new DelayExpression(); break;\r
-            default: \r
-                exp = new EmptyExpression();\r
-        }\r
-\r
-        if (exp != null) {\r
-            for(Control c : parent.getChildren()) {\r
-                c.dispose();\r
-            } \r
-\r
-            if(original) \r
-                exp.readData(expr, data);\r
-            \r
-            exp.createExpressionFields(parent, data);\r
-            if(modifyListener != null)\r
-            exp.addModifyListener(modifyListener);\r
-            if(focusListener != null)\r
-               exp.addFocusListener(focusListener);\r
-            if(verifyKeyListener != null)\r
-               exp.addVerifyKeyListener(verifyKeyListener);\r
-            this.expression = exp;\r
-            this.parent.layout();\r
-            validateFields();\r
-            \r
-            save();\r
-        }  \r
-    }\r
-    \r
-    public IExpression getExpression() {\r
-        return expression;\r
-    }\r
-    \r
-    public void setVariableTable(Table table) {\r
-        this.variableTable = table;\r
-    }\r
-    \r
-    public void validateFields() {\r
-        if(this.variableTable == null) return;\r
-        /*\r
-        TableItem[] connectedVariables = this.variableTable.getItems();\r
-        try {\r
-            final Resource configuration = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
-\r
-                @Override\r
-                public Resource perform(ReadGraph graph) throws DatabaseException {\r
-                    SysdynResource sr = SysdynResource.getInstance(graph);\r
-                    \r
-                    \r
-                    \r
-                    Resource configuration = variable;\r
-                    \r
-                    do {\r
-                    configuration = graph.getSingleObject(configuration, Layer0.getInstance(graph).PartOf);\r
-                        if(graph.isInstanceOf(configuration, sr.Configuration))\r
-                            return configuration;\r
-                    } while (configuration != null);\r
-                    \r
-                    return null;\r
-                }\r
-            });\r
-            ExpressionUtils.validateExpressionFields(expression, connectedVariables, configuration);\r
-        } catch (DatabaseException e1) {\r
-            e1.printStackTrace();\r
-        }\r
-        */\r
-    }\r
-    \r
-    public void addModifyListener(ModifyListener listener) {\r
-        this.modifyListener = listener;\r
-    }\r
-    \r
-    public void addVerifyKeyListener(VerifyKeyListener listener) {\r
-       this.verifyKeyListener = listener;\r
-    }\r
-    \r
-    public void addFocusListener(FocusListener listener) {\r
-        this.focusListener = listener;\r
-    }\r
-    \r
-    public void save() {\r
-        this.expression.save(expr, data);\r
-    }\r
+               }\r
+\r
+               if(this.expression != null) expression.updateData(data);\r
+               ExpressionType et = ExpressionType.valueOf(expressionType);\r
+               IExpression exp = null;\r
+               switch (et) {\r
+               case Auxiliary: \r
+                       exp = new AuxiliaryExpression(); break;\r
+               case Parameter: \r
+                       exp = new ParameterExpression(); break;\r
+               case Constant: \r
+                       exp = new ConstantExpression(); break;\r
+               case Lookup: \r
+                       exp = new LookupExpression(); break;\r
+               case WithLookup: \r
+                       exp = new WithLookupExpression(expr); break;\r
+               case Stock: \r
+                       exp = new StockExpression(); break;\r
+               case Delay: \r
+                       exp = new DelayExpression(); break;\r
+               default: \r
+                       exp = new EmptyExpression();\r
+               }\r
+\r
+               if (exp != null) {\r
+                       for(Control c : parent.getChildren()) {\r
+                               c.dispose();\r
+                       } \r
+\r
+                       if(original) \r
+                               exp.readData(expr, data);\r
+\r
+                       exp.createExpressionFields(parent, data);\r
+                       if(modifyListener != null)\r
+                               exp.addModifyListener(modifyListener);\r
+                       if(focusListener != null)\r
+                               exp.addFocusListener(focusListener);\r
+                       if(verifyKeyListener != null)\r
+                               exp.addVerifyKeyListener(verifyKeyListener);\r
+                       this.expression = exp;\r
+                       this.parent.layout();\r
+                       validateFields();\r
+\r
+                       save();\r
+               }  \r
+       }\r
+\r
+       public IExpression getExpression() {\r
+               return expression;\r
+       }\r
+\r
+       public void setVariableTable(Table table) {\r
+               this.variableTable = table;\r
+       }\r
+\r
+       public void validateFields() {\r
+               if(this.variableTable == null) return;\r
+\r
+               try {\r
+                       Resource variable = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
+\r
+                               @Override\r
+                               public Resource perform(ReadGraph graph) throws DatabaseException {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+                                       Resource expressionList = OrderedSetUtils.getSingleOwnerList(graph, expr);\r
+                                       Resource variable = graph.getPossibleObject(expressionList, sr.HasExpressions_Inverse);\r
+                                       return variable;\r
+                               }\r
+                       });\r
+                       if(variable != null)\r
+                               ExpressionUtils.validateExpressionFields(variable, expression, variableTable);\r
+               } catch (DatabaseException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               }\r
+\r
+       }\r
+\r
+       public void addModifyListener(ModifyListener listener) {\r
+               this.modifyListener = listener;\r
+       }\r
+\r
+       public void addVerifyKeyListener(VerifyKeyListener listener) {\r
+               this.verifyKeyListener = listener;\r
+       }\r
+\r
+       public void addFocusListener(FocusListener listener) {\r
+               this.focusListener = listener;\r
+       }\r
+\r
+       public void save() {\r
+               this.expression.save(expr, data);\r
+       }\r
 \r
 }\r
index ded5da750ee9993d234068f1296eca1cf09c845b..cbe8af81de409ce5a0ae82833ca1323efbe26275 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.properties.widgets;\r
 \r
+import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.HashSet;\r
+import java.util.concurrent.CopyOnWriteArrayList;\r
 \r
 import org.eclipse.jface.layout.GridDataFactory;\r
 import org.eclipse.jface.layout.GridLayoutFactory;\r
@@ -27,15 +29,16 @@ import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableItem;\r
 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.AsyncReadGraph;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.procedure.AsyncListener;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
 public class ShortcutTabWidget implements Widget {\r
@@ -46,6 +49,9 @@ public class ShortcutTabWidget implements Widget {
     Table variableTable;\r
     Table functionTable;\r
     Composite composite;\r
+    \r
+       CopyOnWriteArrayList<Runnable> dependencyListeners =\r
+               new CopyOnWriteArrayList<Runnable>();\r
 \r
     public ShortcutTabWidget(Composite parent, WidgetSupport support, int style) {\r
        if(support!=null)\r
@@ -99,55 +105,93 @@ public class ShortcutTabWidget implements Widget {
         if(input instanceof IStructuredSelection) {\r
             final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class);\r
             if(variable != null) {\r
-                HashSet<String> dependencies = getDependencies(variable);\r
-                TableItem item;\r
-                for(String d : dependencies) {\r
-                    item = new TableItem(variableTable, SWT.NONE);\r
-                    item.setText(d);\r
-                    item.setData(d);\r
-                }\r
+               \r
+               SimanticsUI.getSession().asyncRequest(new Read<HashSet<String>>() {\r
+\r
+                                       @Override\r
+                                       public HashSet<String> perform(ReadGraph graph)\r
+                                                       throws DatabaseException {\r
+                                               return getDependencies(graph, variable);\r
+                                       }\r
+                               }, new AsyncListener<HashSet<String>>() {\r
+\r
+                                       @Override\r
+                                       public void execute(AsyncReadGraph graph,\r
+                                                       HashSet<String> result) {\r
+                                               \r
+                                               final HashSet<String> dependencies = result;\r
+                                               variableTable.getDisplay().asyncExec(new Runnable() {\r
+                                                       \r
+                                                       @Override\r
+                                                       public void run() {\r
+                                                               if(variableTable.isDisposed()) return;\r
+                                                               \r
+                                                               TableItem[] items = variableTable.getItems();\r
+                                                               \r
+                                                               // Remove deleted dependencies and create the list of current dependencies (itemStrings)\r
+                                                               ArrayList<String> itemStrings = new ArrayList<String>();\r
+                                               for(TableItem i : items) {\r
+                                                       String text = i.getText();\r
+                                                       if(dependencies.contains(text))\r
+                                                               itemStrings.add(text);\r
+                                                       else\r
+                                                               variableTable.remove(variableTable.indexOf(i));\r
+                                               }\r
+                                               \r
+                                               // Add all new dependencies\r
+                                               TableItem item;\r
+                                               for(String d : dependencies) {\r
+                                                       if(!itemStrings.contains(d)) {\r
+                                                               item = new TableItem(variableTable, SWT.NONE);\r
+                                                               item.setText(d);\r
+                                                               item.setData(d);\r
+                                                       }\r
+                                               }\r
+                                               \r
+                                                               synchronized(dependencyListeners) {\r
+                                                                       for(Runnable listener : dependencyListeners)\r
+                                                                               listener.run();\r
+                                                               }                                                               \r
+                                                       }\r
+                                               });\r
+                                       }\r
+\r
+                                       @Override\r
+                                       public void exception(AsyncReadGraph graph,\r
+                                                       Throwable throwable) {\r
+                                               throwable.printStackTrace();\r
+                                       }\r
+\r
+                                       @Override\r
+                                       public boolean isDisposed() {\r
+                                               return variableTable.isDisposed();\r
+                                       }\r
+                               });\r
             }\r
         }\r
     }\r
 \r
     // Returns the names of the related variables (dependencies)\r
-    private HashSet<String> getDependencies(final Resource r){\r
-        HashSet<String> variables = null;\r
-        if(r != null)\r
-            try {\r
-                variables = SimanticsUI.getSession().syncRequest(new Read<HashSet<String>>() {\r
-\r
-                    @Override\r
-                    public HashSet<String> perform(ReadGraph graph)\r
-                    throws DatabaseException {\r
-                        HashSet<String> variables = new HashSet<String>();\r
-                        SysdynResource sr = SysdynResource.getInstance(graph);\r
-                        Layer0 l0 = Layer0.getInstance(graph);\r
-                        \r
-                        Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
-\r
-                        for(Resource d : dependencies) {\r
-                            if(graph.isInstanceOf(d, sr.Dependency)) {\r
-                                Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
-                                if(tail != null) {\r
-                                    Object name = graph.getPossibleRelatedValue(tail, l0.HasName);\r
-                                    if(name != null)\r
-                                        variables.add((String)name);\r
-                                }\r
-                            }\r
-                        }\r
-\r
-                        return variables;\r
-\r
-                    }\r
-\r
-\r
-                });\r
-            } catch (DatabaseException ee) {\r
-                ExceptionUtils.logAndShowError(ee);\r
-            }\r
-\r
-            return variables;\r
+    private HashSet<String> getDependencies(ReadGraph graph, Resource r) throws DatabaseException {\r
+       HashSet<String> variables = new HashSet<String>();\r
+       if(graph != null && r != null) {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+\r
+               Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
+\r
+               for(Resource d : dependencies) {\r
+                       if(graph.isInstanceOf(d, sr.Dependency)) {\r
+                               Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
+                               if(tail != null) {\r
+                                       Object name = graph.getPossibleRelatedValue(tail, l0.HasName);\r
+                                       if(name != null)\r
+                                               variables.add((String)name);\r
+                               }\r
+                       }\r
+               }\r
+        }\r
+       return variables;\r
     }\r
     \r
     public void addFocusListener(FocusListener listener) {\r
@@ -160,6 +204,18 @@ public class ShortcutTabWidget implements Widget {
         this.variableTable.addMouseListener(listener);\r
     }\r
     \r
+       public void addDependencyListener(Runnable listener) {\r
+               synchronized(dependencyListeners) {\r
+                       dependencyListeners.add(listener);\r
+               }\r
+       }\r
+\r
+       public void removeDependencyListener(Runnable listener) {\r
+               synchronized(dependencyListeners) {\r
+                       dependencyListeners.remove(listener);\r
+               }\r
+       }\r
+    \r
     public Table getVariableTable() {\r
         return variableTable;\r
     }\r
index ebf4d4205981fdeb94b1d3d7813a8790aff72cd8..66dd8622b985fe38c63d9cec3798d580732edbec 100644 (file)
@@ -31,11 +31,11 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.ExpressionUtils;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
 public class BasicExpression implements IExpression {\r
index ffa300d6d345a1b2ba7000fe5cff142aabc9366a..000db7bec563e7c9cc14bd2c254704b5be1fe28a 100644 (file)
@@ -31,7 +31,7 @@ import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.jface.text.source.SourceViewerConfiguration;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.graphics.RGB;\r
-import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
 \r
 public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
 \r
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
deleted file mode 100644 (file)
index 191960c..0000000
+++ /dev/null
@@ -1,172 +0,0 @@
-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
-\r
-import java.io.StringReader;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.StringTokenizer;\r
-\r
-import org.eclipse.jface.text.Position;\r
-import org.eclipse.swt.custom.StyledText;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.widgets.TableItem;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
-import org.simantics.sysdyn.expressionParser.ParseException;\r
-import org.simantics.sysdyn.expressionParser.Token;\r
-import org.simantics.sysdyn.expressionParser.TokenMgrError;\r
-import org.simantics.sysdyn.manager.SysdynModel;\r
-import org.simantics.sysdyn.manager.SysdynModelManager;\r
-import org.simantics.sysdyn.representation.Configuration;\r
-import org.simantics.sysdyn.representation.IElement;\r
-import org.simantics.sysdyn.representation.Variable;\r
-import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression;\r
-import org.simantics.ui.SimanticsUI;\r
-\r
-public class ExpressionUtils {\r
-       \r
-       /**\r
-        * Determines if the given expression is a parameter expression. Parameters are numbers.\r
-        * If the expression contains anything other than numbers, it is not a parameter.\r
-        * \r
-        * @param expression The expression to be checked\r
-        * @return is the expression a parameter\r
-        */\r
-       static public boolean isParameter(String expression) {\r
-               try {\r
-                       StringTokenizer st = new StringTokenizer(expression, "{}[],;");\r
-                       while(st.hasMoreTokens()) {\r
-                               Double.parseDouble(st.nextToken().trim());\r
-                       }\r
-                       return true;\r
-               } catch (NumberFormatException e) {\r
-                       return false;\r
-               }\r
-       }\r
-\r
-    static public void validateExpressionFields(IExpression expression, TableItem[] connectedVariables, Resource configuration) {\r
-        ExpressionParser parser = new ExpressionParser(new StringReader(""));\r
-        Set<String> variables = new HashSet<String>();\r
-        HashMap<ExpressionField, HashMap<String, List<Token>>> references = new HashMap<ExpressionField, HashMap<String, List<Token>>>();\r
-\r
-\r
-        // Build references and variable array\r
-        for(ExpressionField ef : expression.getExpressionFields()) {\r
-            ef.resetAnnotations();\r
-            String textString = ef.getExpression();\r
-            parser.ReInit(new StringReader(textString));\r
-            try {\r
-                parser.expr();\r
-                HashMap<String, List<Token>> cr = parser.getReferences();\r
-                references.put(ef, cr);\r
-                for(String t : cr.keySet())\r
-                    variables.add(t);\r
-            } catch (ParseException e1) {\r
-                ef.setSyntaxError(e1.currentToken, "Syntax Error");\r
-            } catch (TokenMgrError err) {\r
-               ef.setSyntaxError(0, textString.length(), "MissingLink", "Expression contains unsupported characters");\r
-            }\r
-        }\r
-\r
-        // Remove variables from variable array that don't exist in the model. Create annotations\r
-        if(!variables.isEmpty()) {\r
-            ArrayList<String> modelVariables = new ArrayList<String>();\r
-            Set<String> noSuchVariables = new HashSet<String>();\r
-            SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
-            SysdynModel model = sdm.getModel(configuration);\r
-            try {\r
-                model.update();\r
-            } catch (DatabaseException e1) {\r
-                e1.printStackTrace();\r
-            }\r
-            Configuration conf = model.getConfiguration();\r
-            ArrayList<IElement> elements = conf.getElements();\r
-            for(IElement e : elements) {\r
-                if(e instanceof Variable) {\r
-                    Variable v = (Variable) e;\r
-                    modelVariables.add(v.getName());\r
-                }\r
-            }\r
-\r
-\r
-            for(String v : variables) {\r
-                if(!modelVariables.contains(v)) {\r
-                    noSuchVariables.add(v);\r
-                }\r
-            }\r
-\r
-            if(!noSuchVariables.isEmpty()) {\r
-                // remove no such variables from variable list\r
-                for(String s : noSuchVariables)\r
-                    variables.remove(s);\r
-                // create annotations\r
-                HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, noSuchVariables);\r
-                for(ExpressionField ef : positions.keySet()) {\r
-                    ef.setNoSuchVariableAnnotations(positions.get(ef));\r
-                }\r
-            }      \r
-        }\r
-\r
-        // Check that the variables that exist have connections and the connected variables have references in the expressions\r
-        if(!(expression instanceof StockExpression)) { \r
-            for(TableItem ti : connectedVariables) {\r
-                if(!variables.contains(ti.getText())) {\r
-                    ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0));\r
-                } else {\r
-                    ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0));\r
-                    variables.remove(ti.getText());\r
-                }\r
-            }\r
-\r
-            if(!variables.isEmpty()) {\r
-                HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, variables);\r
-                for(ExpressionField ef : positions.keySet()) {\r
-                    ef.setMissingLinkAnnotations(positions.get(ef));\r
-                }\r
-\r
-            }\r
-        } \r
-\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-    static private HashMap<ExpressionField ,ArrayList<Position>> getPositionsForVariables(HashMap<ExpressionField, HashMap<String, List<Token>>> references, Set<String> variables) {\r
-        HashMap<ExpressionField ,ArrayList<Position>> result = new HashMap<ExpressionField ,ArrayList<Position>>();\r
-        for(String s : variables) {\r
-            List<Token> tlist = new ArrayList<Token>();\r
-            for(ExpressionField ef : references.keySet()) {\r
-                ArrayList<Position> positions = new ArrayList<Position>();\r
-                tlist = references.get(ef).get(s);\r
-                if(tlist != null)\r
-                    for(Token t : tlist) {\r
-                        StyledText st = ef.getSourceViewer().getTextWidget();\r
-                        int start =  st.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1;\r
-                        int offset = st.getOffsetAtLine(t.endLine - 1) + t.endColumn - start;\r
-                        positions.add(new Position(\r
-                                start,\r
-                                offset));\r
-                    }\r
-                if(result.keySet().contains(ef)) {\r
-                    result.get(ef).addAll((ArrayList<Position>)positions.clone());\r
-                } else {\r
-                    result.put(ef, (ArrayList<Position>)positions.clone());\r
-                }\r
-            }\r
-        }\r
-        return result;\r
-    }\r
-}\r
index c6320dfde55eeb0f7896fece3baeef72d3517b6e..eba5f967b32426aead39920922ad105407d748f5 100644 (file)
@@ -18,7 +18,7 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.layer0.Layer0;\r
-import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
 \r
 public class VariableNamePropertyModifier extends TextModifyListenerImpl<Resource> {\r
 \r
index 879dc8f688034a519af1649af1fdd8d233931fda..9d635e71675c7945d15a3017b7f31e54ccef9de2 100644 (file)
@@ -17,7 +17,7 @@ import org.simantics.browsing.ui.swt.widgets.impl.Widget;
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.management.ISessionContext;\r
-import org.simantics.sysdyn.ui.properties.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
 import org.simantics.utils.ui.ISelectionUtils;\r
 \r
 public class VariableNameValidator implements IInputValidator, Widget {\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java
new file mode 100644 (file)
index 0000000..137267a
--- /dev/null
@@ -0,0 +1,101 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.representation.Enumeration;\r
+import org.simantics.sysdyn.representation.EnumerationIndex;\r
+import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Variable;\r
+\r
+public class ArrayVariableUtils {\r
+\r
+\r
+       /**\r
+        * Checks if the given range elements can be applied to the given variable.\r
+        * \r
+        * @param graph ReadGraph\r
+        * @param variable Resource of the variable \r
+        * @param range Range elements in the correct order. Elements are separated in range definition by ',' \r
+        * @return true if range is valid, false if not\r
+        * @throws DatabaseException\r
+        */\r
+       public static Map<Integer, String> isRangeValid(ReadGraph graph, Variable variable, String[] elements) throws DatabaseException {\r
+               if(variable == null)\r
+                       return null;\r
+               Map<Integer, String> result = new HashMap<Integer, String>();\r
+               ArrayList<Enumeration> enumerations = variable.getArrayIndexes().getEnumerations();\r
+               // Too many elements\r
+               if(elements.length > enumerations.size()) {\r
+                       result.put( enumerations.size(), "Too many elements");\r
+               } else if(elements.length < enumerations.size()) {\r
+                       result.put( elements.length > 0 ? elements.length - 1 : 0, "Too few elements");\r
+               }\r
+               \r
+               \r
+               for(int i = 0; i < elements.length; i++) {\r
+                       if(elements[i].trim().equals(":"))\r
+                               continue;\r
+                       if(elements[i].indexOf(":") != elements[i].lastIndexOf(":")) {\r
+                               result.put( i, "Too many ':' elements");\r
+                               continue;\r
+                       }\r
+\r
+                       String[] rangeComponents = elements[i].split(":");\r
+                       if(rangeComponents.length > 2){\r
+                               result.put( i, "Too many ':' elements");\r
+                               continue;\r
+                       }\r
+                       // Single range component, equals to the enumeration at that index\r
+                       if(rangeComponents.length == 1 && rangeComponents[0].trim().equals(enumerations.get(i).getName()))\r
+                               continue;\r
+                       // one or two range components, they all equal to individual indexes in the enumeration\r
+                       for(String r : rangeComponents) {\r
+                               r = r.trim();\r
+                               boolean componentIsValid = false;\r
+                               for(EnumerationIndex ei : enumerations.get(i).getEnumerationIndexes()) {\r
+                                       if(ei.getName().equals(r)) {\r
+                                               componentIsValid = true;\r
+                                               break;\r
+                                       }\r
+                               }\r
+                               if(!componentIsValid)\r
+                                       result.put( i, "Invalid range");\r
+                       }\r
+               }\r
+               if(result.isEmpty())\r
+                       return null;\r
+               else\r
+                       return result;\r
+       }\r
+\r
+       /**\r
+        * Checks if the given range can be applied to the given variable.\r
+        * \r
+        * @param graph ReadGraph\r
+        * @param variable Resource of the variable \r
+        * @param range Range WITHOUT [ and ] brackets\r
+        * @return true if range is valid, false if not\r
+        * @throws DatabaseException\r
+        */\r
+       public static boolean isRangeValid(ReadGraph graph, Resource variable, String range) throws DatabaseException {\r
+               String[] elements = range.split(",");\r
+               SysdynModel model = ModelUtils.getModel(graph, variable);\r
+               IElement e = model.getElement(variable);\r
+               if(e != null && e instanceof Variable) {\r
+                       Variable v = (Variable) e;\r
+                       Map<Integer, String> result = isRangeValid(graph, v, elements);\r
+                       if(result == null)\r
+                               return true;\r
+                       else\r
+                               return false;\r
+               } else {\r
+                       return false;\r
+               }\r
+       }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java
new file mode 100644 (file)
index 0000000..d7372d5
--- /dev/null
@@ -0,0 +1,270 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import java.io.StringReader;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.eclipse.jface.text.Position;\r
+import org.eclipse.swt.custom.StyledText;\r
+import org.eclipse.swt.graphics.Color;\r
+import org.eclipse.swt.widgets.Table;\r
+import org.eclipse.swt.widgets.TableItem;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
+import org.simantics.sysdyn.expressionParser.ParseException;\r
+import org.simantics.sysdyn.expressionParser.Token;\r
+import org.simantics.sysdyn.expressionParser.TokenMgrError;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Variable;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.StockExpression;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class ExpressionUtils {\r
+\r
+       /**\r
+        * Determines if the given expression is a parameter expression. Parameters are numbers.\r
+        * If the expression contains anything other than numbers, it is not a parameter.\r
+        * \r
+        * @param expression The expression to be checked\r
+        * @return is the expression a parameter\r
+        */\r
+       static public boolean isParameter(String expression) {\r
+               try {\r
+                       /*\r
+                       StringTokenizer st = new StringTokenizer(expression, "{}[],;");\r
+                       while(st.hasMoreTokens()) {\r
+                               Double.parseDouble(st.nextToken().trim());\r
+                       }\r
+                        */\r
+                       Double.parseDouble(expression.trim());\r
+                       return true;\r
+               } catch (NumberFormatException e) {\r
+                       return false;\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Validates the expressionfield of a given IExpression\r
+        * \r
+        * @param expression The expression whose fields are validated\r
+        * @param connectedVariables Table items from the shortcut widget. (Items needed so that they can be coloured)\r
+        * @param configuration configuration where the variable is located\r
+        */\r
+       static public void validateExpressionFields(final Resource variable, IExpression expression, Table variableTable) {\r
+\r
+               TableItem[] connectedVariables = variableTable.getItems();\r
+\r
+\r
+               Resource configuration = null;\r
+               try {\r
+                       configuration = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
+\r
+                               @Override\r
+                               public Resource perform(ReadGraph graph) throws DatabaseException {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Resource configuration = variable;\r
+                                       while (configuration != null) {\r
+                                               configuration = graph.getPossibleObject(configuration, Layer0.getInstance(graph).PartOf);\r
+\r
+                                               if(configuration == null || graph.isInstanceOf(configuration, sr.Configuration)) {\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                                       return configuration;\r
+                               }\r
+                       });\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+                       return;\r
+               }\r
+\r
+               if(configuration == null)\r
+                       return;\r
+\r
+               ExpressionParser parser = new ExpressionParser(new StringReader(""));\r
+               Set<String> variables = new HashSet<String>();\r
+               HashMap<ExpressionField, HashMap<String, List<Token>>> references = new HashMap<ExpressionField, HashMap<String, List<Token>>>();\r
+               final HashMap<ExpressionField, HashMap<String, List<List<Token>>>> ranges = new HashMap<ExpressionField, HashMap<String, List<List<Token>>>>();\r
+\r
+\r
+               // Build references and variable array\r
+               for(ExpressionField ef : expression.getExpressionFields()) {\r
+                       ef.resetAnnotations();\r
+                       String textString = ef.getExpression();\r
+                       parser.ReInit(new StringReader(textString));\r
+                       try {\r
+                               parser.expr();\r
+                               HashMap<String, List<Token>> refs = parser.getReferences();\r
+                               references.put(ef, refs);\r
+                               variables.addAll(refs.keySet());\r
+\r
+                               HashMap<String, List<List<Token>>> rangs = parser.getRanges();\r
+                               ranges.put(ef, rangs);\r
+                       } catch (ParseException e1) {\r
+                               ef.setSyntaxError(e1.currentToken, "Syntax Error");\r
+                       } catch (TokenMgrError err) {\r
+                               ef.setSyntaxError(0, textString.length(), "MissingLink", "Expression contains unsupported characters");\r
+                       }\r
+               }\r
+\r
+\r
+               final HashMap<String, Variable> modelVariables = new HashMap<String, Variable>();\r
+\r
+               if(!variables.isEmpty()) {\r
+                       Set<String> noSuchVariables = new HashSet<String>();\r
+                       SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
+                       SysdynModel model = sdm.getModel(configuration);\r
+                       try {\r
+                               model.update();\r
+                       } catch (DatabaseException e1) {\r
+                               e1.printStackTrace();\r
+                       }\r
+\r
+                       Configuration conf = model.getConfiguration();\r
+                       ArrayList<IElement> elements = conf.getElements();\r
+                       for(IElement e : elements) {\r
+                               if(e instanceof Variable) {\r
+                                       Variable v = (Variable) e;\r
+                                       modelVariables.put(v.getName(), v);\r
+                               }\r
+                       }\r
+\r
+                       // VARIABLE NAMES\r
+\r
+                       for(String v : variables) {\r
+                               if(!modelVariables.keySet().contains(v)) {\r
+                                       noSuchVariables.add(v);\r
+                               }\r
+                       }\r
+\r
+                       if(!noSuchVariables.isEmpty()) {\r
+                               // remove no such variables from variable list\r
+                               for(String s : noSuchVariables)\r
+                                       variables.remove(s);\r
+                               // create annotations\r
+                               HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, noSuchVariables);\r
+                               for(ExpressionField ef : positions.keySet()) {\r
+                                       ef.setNoSuchVariableAnnotations(positions.get(ef));\r
+                               }\r
+                       }      \r
+               }\r
+\r
+               // Check that the variables that exist have connections and the connected variables have references in the expressions\r
+               if(!(expression instanceof StockExpression)) { \r
+                       for(TableItem ti : connectedVariables) {\r
+                               if(!variables.contains(ti.getText())) {\r
+                                       ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0));\r
+                               } else {\r
+                                       ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0));\r
+                                       variables.remove(ti.getText());\r
+                               }\r
+                       }\r
+\r
+                       if(!variables.isEmpty()) {\r
+                               HashMap<ExpressionField ,ArrayList<Position>> positions = getPositionsForVariables(references, variables);\r
+                               for(ExpressionField ef : positions.keySet()) {\r
+                                       ef.setMissingLinkAnnotations(positions.get(ef));\r
+                               }\r
+\r
+                       }\r
+               } \r
+\r
+\r
+               // RANGES\r
+               HashMap<ExpressionField, HashMap<Token, String>> errors = new HashMap<ExpressionField, HashMap<Token, String>>();\r
+               HashMap<Token, String> result = null;\r
+\r
+               for(final ExpressionField ef : ranges.keySet()) {\r
+                       try {\r
+                               result = SimanticsUI.getSession().syncRequest(new Read<HashMap<Token, String>>() {\r
+\r
+                                       @Override\r
+                                       public HashMap<Token, String> perform(ReadGraph graph) throws DatabaseException {\r
+                                               HashMap<Token, String> result = new HashMap<Token, String>();\r
+                                               for(String s : ranges.get(ef).keySet()) {\r
+                                                       if(ranges.get(ef).containsKey(s)) {\r
+                                                               for(List<Token> l : ranges.get(ef).get(s)) {\r
+                                                                       String[] ss = new String[l.size()];\r
+                                                                       for(int i = 0; i < l.size(); i++) {\r
+                                                                               ss[i] = l.get(i).image;\r
+                                                                       }\r
+                                                                       Map<Integer, String> invalidRanges = ArrayVariableUtils.isRangeValid(graph, modelVariables.get(s), ss);\r
+                                                                       if(invalidRanges != null && !invalidRanges.isEmpty()) {\r
+                                                                               for(Integer i : invalidRanges.keySet()) {\r
+                                                                                       result.put(l.get(i), invalidRanges.get(i));\r
+                                                                               }\r
+                                                                       }\r
+                                                               }\r
+                                                       }\r
+                                               }       \r
+                                               return result;\r
+                                       }\r
+                               });\r
+                       } catch (DatabaseException e) {\r
+                               e.printStackTrace();\r
+                       }\r
+                       errors.put(ef, result);\r
+\r
+               }                                       \r
+\r
+               for(ExpressionField ef : ranges.keySet()) {\r
+                       HashMap<Token, String> tokens = errors.get(ef);\r
+                       if(tokens == null || tokens.isEmpty()) continue;\r
+                       for(Token t : tokens.keySet()) {\r
+                               ef.setSyntaxError(t, tokens.get(t));\r
+                       }\r
+               }\r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       static private HashMap<ExpressionField ,ArrayList<Position>> getPositionsForVariables(HashMap<ExpressionField, HashMap<String, List<Token>>> references, Set<String> variables) {\r
+               HashMap<ExpressionField ,ArrayList<Position>> result = new HashMap<ExpressionField ,ArrayList<Position>>();\r
+               for(String s : variables) {\r
+                       List<Token> tlist = new ArrayList<Token>();\r
+                       for(ExpressionField ef : references.keySet()) {\r
+                               ArrayList<Position> positions = new ArrayList<Position>();\r
+                               tlist = references.get(ef).get(s);\r
+                               if(tlist != null)\r
+                                       for(Token t : tlist) {\r
+                                               StyledText st = ef.getSourceViewer().getTextWidget();\r
+                                               int start =  st.getOffsetAtLine(t.beginLine - 1) + t.beginColumn - 1;\r
+                                               int offset = st.getOffsetAtLine(t.endLine - 1) + t.endColumn - start;\r
+                                               positions.add(new Position(\r
+                                                               start,\r
+                                                               offset));\r
+                                       }\r
+                               if(result.keySet().contains(ef)) {\r
+                                       result.get(ef).addAll((ArrayList<Position>)positions.clone());\r
+                               } else {\r
+                                       result.put(ef, (ArrayList<Position>)positions.clone());\r
+                               }\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java
new file mode 100644 (file)
index 0000000..c309265
--- /dev/null
@@ -0,0 +1,33 @@
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class ModelUtils {\r
+\r
+       /**\r
+        * Returns the SysdynModel where the given variable is located\r
+        * \r
+        * @param graph ReadGraph\r
+        * @param variable Variable whose model is wanted\r
+        * @return SysdynModel where variable is located\r
+        * @throws DatabaseException\r
+        */\r
+       public static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException  {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               Resource configuration = graph.getSingleObject(variable, l0.PartOf);\r
+               SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
+               SysdynModel model = sdm.getModel(graph, configuration);\r
+               try {\r
+                       model.update(graph);\r
+               } catch (DatabaseException e1) {\r
+                       e1.printStackTrace();\r
+               }\r
+               return model;\r
+       }\r
+}\r
similarity index 72%
rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameUtils.java
rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/VariableNameUtils.java
index 4d1b6ead54c1511920bbd0c8ed9a68973ca0fc73..2a4f746f6f6752f5a1d88f2aaefd3ad9c56a7402 100644 (file)
@@ -9,9 +9,8 @@
  * Contributors:\r
  *     VTT Technical Research Centre of Finland - initial API and implementation\r
  *******************************************************************************/\r
-package org.simantics.sysdyn.ui.properties;\r
+package org.simantics.sysdyn.ui.utils;\r
 \r
-import java.util.ArrayList;\r
 import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Set;\r
@@ -28,10 +27,7 @@ import org.simantics.db.request.Read;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.manager.SysdynModel;\r
-import org.simantics.sysdyn.manager.SysdynModelManager;\r
 import org.simantics.sysdyn.representation.Configuration;\r
-import org.simantics.sysdyn.representation.Enumeration;\r
-import org.simantics.sysdyn.representation.EnumerationIndex;\r
 import org.simantics.sysdyn.representation.IElement;\r
 import org.simantics.sysdyn.representation.Variable;\r
 import org.simantics.ui.SimanticsUI;\r
@@ -78,7 +74,7 @@ public class VariableNameUtils {
        private static String replaceAllWords(String original, String find, String replacement) {\r
                if(!original.contains(find)) return original;\r
                StringBuilder result = new StringBuilder(original.length());\r
-               String delimiters = "+-*/(){}[],. ";\r
+               String delimiters = "+-*/(){}[],.: ";\r
                StringTokenizer st = new StringTokenizer(original, delimiters, true);\r
                while (st.hasMoreTokens()) {\r
                        String w = st.nextToken();\r
@@ -93,7 +89,9 @@ public class VariableNameUtils {
 \r
 \r
        private static boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException {\r
-               SysdynModel model = getModel(graph, variable);\r
+               if(variable == null)\r
+                       return false;\r
+               SysdynModel model = ModelUtils.getModel(graph, variable);\r
                Configuration configuration = model.getConfiguration();\r
                IElement current = model.getElement(variable);\r
                for(IElement e : configuration.getElements()) {\r
@@ -107,20 +105,6 @@ public class VariableNameUtils {
                return false;\r
        }\r
 \r
-\r
-       private static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException  {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
-               Resource configuration = graph.getSingleObject(variable, l0.PartOf);\r
-               SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
-               SysdynModel model = sdm.getModel(graph, configuration);\r
-               try {\r
-                       model.update(graph);\r
-               } catch (DatabaseException e1) {\r
-                       e1.printStackTrace();\r
-               }\r
-               return model;\r
-       }\r
-\r
        /**\r
         * Checks that the syntax of the given name is valid and there \r
         * are no other variables that have the same name in the configuration\r
@@ -145,61 +129,14 @@ public class VariableNameUtils {
                                range = st.nextToken();\r
                                if(!st.nextToken().equals("]")) return false;\r
                        }\r
-                       if(range != null && !isRangeValid(graph, variable, range)) return false;\r
+                       if(range != null && !ArrayVariableUtils.isRangeValid(graph, variable, range)) return false;\r
                }\r
                if(nameIsTaken(graph, variable, name)) return false;\r
                if(!isValid(name)) return false;\r
                return true;\r
        }\r
 \r
-       /**\r
-        * Checks if the given range can be applied to the given variable.\r
-        * \r
-        * @param graph ReadGraph\r
-        * @param variable Resource of the variable \r
-        * @param range Range WITHOUT [ and ] brackets\r
-        * @return true if range is valid, false if not\r
-        * @throws DatabaseException\r
-        */\r
-       private static boolean isRangeValid(ReadGraph graph, Resource variable, String range) throws DatabaseException {\r
-               String[] elements = range.split(",");\r
-               SysdynModel model = getModel(graph, variable);\r
-               IElement e = model.getElement(variable);\r
-               if(e != null && e instanceof Variable) {\r
-                       Variable v = (Variable) e;\r
-                       ArrayList<Enumeration> enumerations = v.getArrayIndexes().getEnumerations();\r
-                       if(elements.length != enumerations.size()) return false;\r
-                       for(int i = 0; i < enumerations.size(); i++) {\r
-                               if(elements[i].trim().equals(":"))\r
-                                       continue;\r
-                               if(elements[i].indexOf(":") != elements[i].lastIndexOf(":"))\r
-                                       return false;\r
-\r
-                               String[] rangeComponents = elements[i].split(":");\r
-                               if(rangeComponents.length > 2)\r
-                                       return false;\r
-                               // Single range component, equals to the enumeration at that index\r
-                               if(rangeComponents.length == 1 && rangeComponents[0].trim().equals(enumerations.get(i).getName()))\r
-                                       continue;\r
-                               // one or two range components, they all equal to individual indexes in the enumeration\r
-                               for(String r : rangeComponents) {\r
-                                       r = r.trim();\r
-                                       boolean componentIsValid = false;\r
-                                       for(EnumerationIndex ei : enumerations.get(i).getEnumerationIndexes()) {\r
-                                               if(ei.getName().equals(r)) {\r
-                                                       componentIsValid = true;\r
-                                                       break;\r
-                                               }\r
-                                       }\r
-                                       if(!componentIsValid)\r
-                                               return false;\r
-                               }\r
-                       }\r
-                       return true;\r
-               }\r
-\r
-               return false;\r
-       }\r
+       \r
        /**\r
         * Checks that the syntax of the given name is valid and there \r
         * are no other variables that have the same name in the configuration\r
index 023d50c10831510533e88378d3d8a7bcc4a8546f..327d3e6cda24825ed772bbd5d68d5f186b6c3088 100644 (file)
@@ -12,12 +12,20 @@ import java.util.List;
 import java.util.ArrayList;\r
 import java.util.HashMap;\r
 \r
+\r
 public class ExpressionParser {\r
+\r
        HashMap<String, List<Token>> references = new HashMap<String, List<Token>>();\r
     \r
     public HashMap<String, List<Token>> getReferences() {\r
         return references;\r
     }\r
+\r
+    HashMap<String, List<List<Token>>> ranges = new HashMap<String, List<List<Token>>>();\r
+    \r
+    public HashMap<String, List<List<Token>>> getRanges() {\r
+        return ranges;\r
+    }\r
     \r
     \r
 }\r
@@ -162,16 +170,16 @@ void name() : {
 void component_reference() : {\r
 } {\r
        //IDENT [ array_subscripts ] [ "." component_reference ]\r
-       <IDENT> { \r
-       \r
-       if(references.get(token.image) == null) {\r
-               references.put(token.image, new ArrayList<Token>());\r
+       <IDENT> {\r
+       String name = token.image;\r
+       if(references.get(name) == null) {\r
+               references.put(name, new ArrayList<Token>());\r
+       }\r
+       List<Token> list = references.get(name);\r
+       list.add(token);\r
        }\r
-       List list = references.get(token.image);\r
-       list.add(token); \r
        \r
-       } \r
-       ( "." component_reference() )?\r
+       ( array_subscripts() )? ( "." component_reference() )?\r
 }\r
 \r
 \r
@@ -208,3 +216,38 @@ void expression_list() : {
 } {\r
     expression() ( "," expression() )*\r
 }\r
+\r
+void array_subscripts() : {\r   if(ranges.get(token.image) == null) {\r
+               ranges.put(token.image, new ArrayList<List<Token>>());\r
+       }\r
+       List<List<Token>> rangesList = ranges.get(token.image);\r
+       List<Token> currentRange = new ArrayList<Token>(); \r
+} {\r
+       "[" subscript(currentRange) ( "," subscript(currentRange) )* "]"\r
+       {\r
+       rangesList.add(currentRange);\r
+       }\r
+}\r
+\r
+void subscript(List<Token> currentRange) : {\r
+  Token rangeToken = new Token(token.kind, "");\r
+  rangeToken.beginColumn = token.beginColumn;\r
+  rangeToken.beginLine = token.beginLine;\r
+} {\r   ":"\r    {\r              currentRange.add(token);\r
+       }\r
+       | rangeIndex(rangeToken, false) ( ":" rangeIndex(rangeToken, true))?    {\r
+           rangeToken.endColumn = token.endColumn;\r
+           rangeToken.endLine = token.endLine;\r
+               currentRange.add(rangeToken);\r
+       }\r
+}\r
+\r
+void rangeIndex(Token rangeToken, boolean second) : {\r
+  if(second)\r
+       rangeToken.image = rangeToken.image + ":";\r
+} {\r   <UNSIGNED_INTEGER>\r     {\r
+               rangeToken.image = rangeToken.image + token.image;  \r   }\r
+       | <IDENT>\r      {\r              rangeToken.image = rangeToken.image + token.image;\r
+       }\r
+}\r  \r
+\r
index 3acbccc63e2a40962ea0b89d3016718682cf1a38..350ff70098f0c3f9ee03e9e48df67ca805509218 100644 (file)
@@ -97,6 +97,8 @@ public class SysdynResult {
             for(DataSet ds : datasets){\r
                 // Add recording to session\r
                 Recording recording = createRecording(ds);\r
+                System.out.println(recording.nodeId);\r
+                System.out.println(recording.nodeId.getBinding());\r
                 recordings.put(Bindings.VARIANT, recording.nodeId, Bindings.VARIANT, new Variant(recordingBinding, recording));\r
             }\r
 \r
@@ -163,7 +165,7 @@ public class SysdynResult {
     public static class Recording {\r
         public Variant nodeId;\r
         public @Arguments({String.class, String.class}) TreeMap<String, String> labels;\r
-        public @Arguments({Double.class, Double.class}) TreeMap<Double, Double>[] segments;\r
+        public @Arguments({TreeMap.class, Double.class, Double.class}) TreeMap<Double, Double>[] segments;\r
     }\r
 \r
     public SysdynDataSet getDataSet(String variable) {\r