args="org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode"\r
property="org.simantics.sysdyn.ui.nodeClass">\r
</test>\r
+ <test\r
+ args="org.simantics.sysdyn.ui.browser.nodes.SheetNode"\r
+ property="org.simantics.sysdyn.ui.nodeClass">\r
+ </test>\r
+ <test\r
+ args="org.simantics.sysdyn.ui.browser.nodes.BarChartNode"\r
+ property="org.simantics.sysdyn.ui.nodeClass">\r
+ </test>\r
<!--\r
<test\r
args="org.simantics.document.workbench.browser_old.DocumentationNode"\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.ui.ExceptionUtils;\r
\r
-public class EnumerationNode extends VariableNode<Resource> implements IDeletableNode {\r
+public class EnumerationNode extends VariableNode<Resource> implements /*IModifiableNode,*/ IDeletableNode {\r
\r
public EnumerationNode(Resource resource) {\r
super(resource);\r
public EnumerationNode(Variable variable, Resource represents) {\r
super(variable, represents);\r
}\r
+ /*\r
+ @Override\r
+ public Modifier getModifier(String columnId) {\r
+ \r
+ Session session = SimanticsUI.getSession();\r
+ LabelModifier modifier = new LabelModifier(session, data, session.getService(Layer0.class).HasName) {\r
+ @Override\r
+ public String isValid(String label) {\r
+ if (!new VariableNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+ };\r
+ return modifier;\r
+ }*/\r
\r
@Override\r
public void delete() throws DeleteException {\r
import org.simantics.db.layer0.util.RemoverUtil;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.FunctionLibraryNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.utils.ResourceAdaptionUtils;\r
\r
Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
@Override\r
- public String isValid(String label) {\r
- if (label.isEmpty())\r
- return "Empty label not allowed";\r
- if (label.contains(" "))\r
- return "Spaces are not allowed";\r
- return null;\r
- }\r
+ public String isValid(String label) {\r
+ if (!new FunctionLibraryNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
};\r
return modifier;\r
} catch (DatabaseException e) {\r
import org.simantics.db.layer0.util.RemoverUtil;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.FunctionNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.utils.ResourceAdaptionUtils;\r
\r
try {\r
Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
- @Override\r
- public String isValid(String label) {\r
- if (label.isEmpty())\r
- return "Empty label not allowed";\r
- if (label.contains(" "))\r
- return "Spaces are not allowed";\r
- return null;\r
- }\r
+ @Override\r
+ public String isValid(String label) {\r
+ if (!new FunctionNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
};\r
return modifier;\r
} catch (DatabaseException e) {\r
import org.simantics.layer0.Layer0;\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.ModelNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.ui.ExceptionUtils;\r
\r
LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data) {\r
@Override\r
public String isValid(String label) {\r
- if (label.isEmpty())\r
- return "Empty label not allowed";\r
- if (label.contains(" "))\r
- return "Spaces are not allowed";\r
- return null;\r
+ if (!new ModelNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
}\r
\r
@Override\r
import org.simantics.structural.stubs.StructuralResource2;\r
import org.simantics.ui.SimanticsUI;\r
\r
-public class ModuleNode extends ConfigurationNode<Resource> {\r
+public class ModuleNode extends ConfigurationNode<Resource>/* implements IModifiableNode*/ {\r
\r
Resource configuration;\r
\r
return new DefaultPasteHandler(configuration);\r
return super.getAdapter(adapter);\r
}\r
+\r
+ /*@Override\r
+ public Modifier getModifier(String columnId) {\r
+ try {\r
+ Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+ LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+ @Override\r
+ public String isValid(String label) {\r
+ if (!new VariableNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+ };\r
+ return modifier;\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return null;\r
+ }*/\r
}\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.sysdyn.ui.utils.ModuleTypeNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
\r
public class ModuleTypeNode extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode {\r
LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), type, l0.HasName) {\r
@Override\r
public String isValid(String label) {\r
- if (label.isEmpty())\r
- return "Empty name not allowed";\r
- return null;\r
+ if (!new ModuleTypeNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
}\r
};\r
\r
\r
import org.simantics.browsing.ui.common.node.DeleteException;\r
import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.utils.SharedFunctionLibraryNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.utils.ResourceAdaptionUtils;\r
\r
}\r
});\r
}\r
+\r
+ @Override\r
+ public Modifier getModifier(String columnId) {\r
+ try {\r
+ Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+ LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+ @Override\r
+ public String isValid(String label) {\r
+ if (!new SharedFunctionLibraryNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+ };\r
+ return modifier;\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return null;\r
+ }\r
\r
@Override\r
public void drop(Object data) {\r
import org.simantics.db.layer0.variable.Variable;\r
import org.simantics.db.layer0.variable.Variables;\r
import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.ui.utils.SheetNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.ui.workbench.ResourceEditorInput2;\r
import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
LabelModifier modifier = new LabelModifier(session, data, session.getService(Layer0.class).HasName) {\r
@Override\r
public String isValid(String label) {\r
- if (label.isEmpty())\r
- return "Empty label not allowed";\r
- return null;\r
+ if (!new SheetNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
}\r
};\r
return modifier;\r
import org.simantics.db.Resource;\r
import org.simantics.db.layer0.variable.Variable;\r
\r
-public class VariableNode<T> extends AbstractNode<Resource> {\r
+public class VariableNode<T> extends AbstractNode<Resource>/* implements IModifiableNode*/ {\r
\r
Variable variable;\r
\r
public Variable getVariable() {\r
return variable;\r
}\r
+\r
+ /*@Override\r
+ public Modifier getModifier(String columnId) {\r
+ try {\r
+ Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+ LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+ @Override\r
+ public String isValid(String label) {\r
+ if (!new VariableNameValidator().isValid(data, label))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+ };\r
+ return modifier;\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return null;\r
+ }*/\r
}\r
import org.simantics.diagram.handler.Paster;\r
import org.simantics.diagram.handler.Paster.NodeMap;\r
import org.simantics.modeling.ModelingResources;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
import org.simantics.utils.ui.ErrorLogger;\r
\r
/**\r
// Check for each name individually\r
for(String originalName : names.keySet()) {\r
String newName = names.get(originalName);\r
- VariableNameUtils.renameInEquations(graph, copy, originalName, newName);\r
+ new VariableNameValidator().renameInEquations(graph, copy, originalName, newName);\r
}\r
}\r
}\r
import org.simantics.layer0.Layer0;\r
import org.simantics.modeling.ModelingResources;\r
import org.simantics.scenegraph.g2d.G2DParentNode;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.datastructures.Callback;\r
import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
@Override\r
public void textChanged() {\r
TextNode node = (TextNode) e.getHint(SG_NODE);\r
- if(!VariableNameUtils.isValid(component, node.getText(), false)) {\r
+ if(!new VariableNameValidator().isValid(component, node.getText(), false)) {\r
node.setColor(Color.RED);\r
} else {\r
node.setColor(ElementUtils.getTextColor(e, Color.BLACK));\r
public void textEditingCancelled() {\r
TextNode node = (TextNode) e.getHint(SG_NODE);\r
if (node != null) {\r
- if(VariableNameUtils.isValid(component, node.getText(), false))\r
+ if(new VariableNameValidator().isValid(component, node.getText(), false))\r
node.setColor(ElementUtils.getTextColor(e, Color.BLACK));\r
endEdit(node);\r
}\r
if (node == null)\r
return;\r
String text = node.getText();\r
- if(!VariableNameUtils.isValid(component, text, false)) {\r
+ if(!new VariableNameValidator().isValid(component, text, false)) {\r
text = textBeforeEdit;\r
node.setText(text);\r
- if(VariableNameUtils.isValid(component, text, false))\r
+ if(new VariableNameValidator().isValid(component, text, false))\r
node.setColor(ElementUtils.getTextColor(e, Color.BLACK));\r
} else {\r
Object o = e.getHint(ElementHints.KEY_OBJECT);\r
@Override\r
public void perform(WriteGraph graph) throws DatabaseException {\r
Resource configuration = graph.getPossibleObject(component, Layer0.getInstance(graph).PartOf);\r
- VariableNameUtils.renameInAllEquations(graph, configuration, textBeforeEdit, textAfterEdit);\r
+ new VariableNameValidator().renameInAllEquations(graph, configuration, textBeforeEdit, textAfterEdit);\r
}\r
});\r
}\r
\r
public class ModelicaSourceViewerConfiguration extends SourceViewerConfiguration {\r
\r
- public IPresentationReconciler getPresentationReconciler(\r
+ @Override\r
+ public IPresentationReconciler getPresentationReconciler(\r
ISourceViewer sourceViewer) {\r
PresentationReconciler pr = new PresentationReconciler();\r
DefaultDamagerRepairer ddr = new DefaultDamagerRepairer(\r
|| (c >= '0' && c <= '9') || c == '_';\r
}\r
\r
- static final Set<String> keywords = new HashSet<String>();\r
- \r
- static {\r
- keywords.add("within");\r
- keywords.add("final");\r
- keywords.add("public");\r
- keywords.add("protected");\r
- keywords.add("connect");\r
- keywords.add("when");\r
- keywords.add("then");\r
- keywords.add("elsewhen");\r
- keywords.add("if");\r
- keywords.add("end");\r
- keywords.add("elseif");\r
- keywords.add("else");\r
- keywords.add("for");\r
- keywords.add("while");\r
- keywords.add("loop");\r
- keywords.add("der");\r
- keywords.add("enumeration");\r
- keywords.add("extends");\r
- keywords.add("class");\r
- keywords.add("partial");\r
- keywords.add("encapsulated");\r
- keywords.add("model");\r
- keywords.add("record");\r
- keywords.add("block");\r
- keywords.add("expandable");\r
- keywords.add("connector");\r
- keywords.add("type");\r
- keywords.add("package");\r
- keywords.add("function");\r
- keywords.add("import");\r
- keywords.add("external");\r
- keywords.add("constrainedby");\r
- keywords.add("redeclare");\r
- keywords.add("replaceable");\r
- keywords.add("flow");\r
- keywords.add("discrete");\r
- keywords.add("parameter");\r
- keywords.add("constant");\r
- keywords.add("input");\r
- keywords.add("output");\r
- keywords.add("annotation");\r
- keywords.add("false");\r
- keywords.add("true");\r
- keywords.add("each");\r
- keywords.add("initial");\r
- keywords.add("algorithm");\r
- keywords.add("equation");\r
- keywords.add("or");\r
- keywords.add("and");\r
- keywords.add("not");\r
- keywords.add("break");\r
- keywords.add("return"); \r
- }\r
+ // Includes also tokens that are strictly speaking not keywords\r
+ // but should be handled in (almost) a similar fashion.\r
+ public static final Set<String> keywords = new HashSet<String>();\r
+\r
+ static {\r
+ keywords.add("algorithm");\r
+ keywords.add("discrete");\r
+ keywords.add("false");\r
+ keywords.add("model");\r
+ keywords.add("redeclare");\r
+ keywords.add("and");\r
+ keywords.add("each");\r
+ keywords.add("final");\r
+ keywords.add("not");\r
+ keywords.add("replaceable");\r
+ keywords.add("annotation");\r
+ keywords.add("else");\r
+ keywords.add("flow");\r
+ keywords.add("operator");\r
+ keywords.add("return");\r
+ keywords.add("assert");\r
+ keywords.add("elseif");\r
+ keywords.add("for");\r
+ keywords.add("or");\r
+ keywords.add("stream");\r
+ keywords.add("block");\r
+ keywords.add("elsewhen");\r
+ keywords.add("function");\r
+ keywords.add("outer");\r
+ keywords.add("then");\r
+ keywords.add("break");\r
+ keywords.add("encapsulated");\r
+ keywords.add("if");\r
+ keywords.add("output");\r
+ keywords.add("true");\r
+ keywords.add("class");\r
+ keywords.add("end");\r
+ keywords.add("import");\r
+ keywords.add("package");\r
+ keywords.add("type");\r
+ keywords.add("connect");\r
+ keywords.add("enumeration");\r
+ keywords.add("in");\r
+ keywords.add("parameter");\r
+ keywords.add("when");\r
+ keywords.add("connector");\r
+ keywords.add("equation");\r
+ keywords.add("initial");\r
+ keywords.add("partial");\r
+ keywords.add("while");\r
+ keywords.add("constant");\r
+ keywords.add("expandable");\r
+ keywords.add("inner");\r
+ keywords.add("protected");\r
+ keywords.add("within");\r
+ keywords.add("constrainedby");\r
+ keywords.add("extends");\r
+ keywords.add("input");\r
+ keywords.add("public");\r
+ keywords.add("der");\r
+ keywords.add("external");\r
+ keywords.add("loop");\r
+ keywords.add("record");\r
+ keywords.add("time");\r
+ keywords.add("pure");\r
+ keywords.add("impure");\r
+ keywords.add("Real");\r
+ keywords.add("Boolean");\r
+ keywords.add("Integer");\r
+ keywords.add("String");\r
+ }\r
\r
static IToken modelicaKeywordToken = null;\r
public IToken getModelicaKeywordToken() {\r
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
+import org.simantics.sysdyn.ui.properties.widgets.factories.ModelNameInputValidator;\r
\r
/**\r
* Tab displaying configuration properties. Displayed for model and configuration.\r
TrackedText name = new TrackedText(composite, support, SWT.BORDER);\r
name.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasLabel));\r
name.addModifyListener(new ModelLabelModifier(context, Layer0.URIs.HasLabel));\r
- name.setInputValidator(new IInputValidator() {\r
- \r
- @Override\r
- public String isValid(String newText) {\r
- if (!VariableNameUtils.isValid(newText))\r
- return "Sorry but spaces and special characters are not allowed for names right now";\r
- return null;\r
- }\r
- });\r
+ name.setInputValidator(new ModelNameInputValidator(support));\r
\r
GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getWidget());\r
\r
import org.simantics.sysdyn.ui.properties.widgets.arrays.EnumerationIndexNode;\r
import org.simantics.sysdyn.ui.properties.widgets.arrays.ReplaceableIndexesWidget;\r
import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.datastructures.ArrayMap;\r
import org.simantics.utils.ui.AdaptionUtils;\r
TrackedText nameText = new TrackedText(container, support, SWT.BORDER);\r
nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName));\r
- nameText.setInputValidator(new VariableNameValidator(support));\r
+ nameText.setInputValidator(new VariableNameInputValidator(support));\r
GridDataFactory.fillDefaults().grab(true, false).span(4,1).applyTo(nameText.getWidget());\r
\r
indexExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
import org.simantics.sysdyn.ui.properties.widgets.arrays.NameAndArrayRangeModifyListener;\r
import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionComposite;\r
import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionWidgetInput;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
import org.simantics.ui.SimanticsUI;\r
import org.simantics.utils.datastructures.Pair;\r
import org.simantics.utils.ui.AdaptionUtils;\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite);\r
\r
arrayEquationCombo = new ArrayExpressionCombo(nameComposite, support, SWT.DROP_DOWN | SWT.BORDER);\r
- arrayEquationCombo.setInputValidator(new VariableNameValidator(support));\r
+ arrayEquationCombo.setInputValidator(new VariableNameInputValidator(support));\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(arrayEquationCombo.getWidget());\r
\r
deleteExpression = new Button(nameComposite, support, SWT.NONE);\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.FunctionLibraryNameInputValidator;\r
\r
public class FunctionLibraryTab extends LabelPropertyTabContributor {\r
\r
TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+ nameText.setInputValidator(new FunctionLibraryNameInputValidator(support));\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
\r
\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.ui.properties.widgets.FunctionLabelFactory;\r
import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.FunctionNameInputValidator;\r
import org.simantics.sysdyn.ui.properties.widgets.functions.FunctionCodeWidget;\r
\r
public class FunctionTab extends LabelPropertyTabContributor {\r
TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+ nameText.setInputValidator(new FunctionNameInputValidator(support));\r
GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(nameText.getWidget());\r
\r
\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.properties.widgets.factories.VariableNamePropertyModifier;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
import org.simantics.utils.datastructures.Pair;\r
import org.simantics.utils.ui.validators.DoubleValidator;\r
\r
TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
nameText.addModifyListener(new VariableNamePropertyModifier(context, Layer0.URIs.HasName));\r
- nameText.setInputValidator(new VariableNameValidator(support));\r
+ nameText.setInputValidator(new VariableNameInputValidator(support));\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
\r
Composite defaultValueComposite = new Composite(composite, SWT.NONE);\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameInputValidator;\r
import org.simantics.utils.datastructures.ArrayMap;\r
\r
public class ModuleTab extends LabelPropertyTabContributor implements Widget {\r
TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+ nameText.setInputValidator(new VariableNameInputValidator(support));\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
\r
Label label = new Label(composite, SWT.NONE);\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.ModuleTypeNameInputValidator;\r
\r
public class ModuleTypeTab extends LabelPropertyTabContributor {\r
\r
TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+ nameText.setInputValidator(new ModuleTypeNameInputValidator(support));\r
GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
}\r
\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.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
import org.simantics.ui.SimanticsUI;\r
\r
public class EnumerationIndexNode extends AbstractNode<Resource> implements IModifiableNode, IDoubleClickableNode {\r
\r
@Override\r
public String isValid(String label) {\r
- if(!VariableNameUtils.isValid(label))\r
+ if(!new VariableNameValidator().isValidModelica(label))\r
return "Not valid";\r
return null;\r
}\r
import java.util.LinkedHashMap;\r
import java.util.StringTokenizer;\r
\r
-import org.eclipse.jface.viewers.ISelection;\r
import org.eclipse.swt.widgets.Combo;\r
import org.simantics.Simantics;\r
import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.ui.properties.widgets.ArrayExpressionCombo;\r
import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
import org.simantics.utils.ui.ISelectionUtils;\r
\r
/**\r
this.lastInput = input;\r
} \r
\r
- public void modifyText(TrackedModifyEvent e) { \r
+ @Override\r
+ public void modifyText(TrackedModifyEvent e) { \r
Combo combo = (Combo)e.getWidget();\r
LinkedHashMap<?, ?> data = (LinkedHashMap<?, ?>) combo.getData();\r
\r
@Override\r
public Resource perform(ReadGraph graph)\r
throws DatabaseException {\r
- Resource variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
+ Resource variable = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
return graph.getPossibleObject(variable, SysdynResource.getInstance(graph).IndependentVariable_activeExpression);\r
}\r
\r
for(Object key : data.keySet()) {\r
int index = arrayExpressionCombo.getLastSelectedIndex() < 0 ? 0 : arrayExpressionCombo.getLastSelectedIndex();\r
if((Integer)combo.getData((String)key) == index) {\r
- lastExpression = (Resource) data.get((String)key);\r
+ lastExpression = (Resource) data.get(key);\r
break;\r
}\r
}\r
String originalName = graph.getRelatedValue(variable, Layer0.getInstance(graph).HasName);\r
if(!originalName.equals(newName)) {\r
Resource configuration = graph.getPossibleObject(variable, Layer0.getInstance(graph).PartOf);\r
- VariableNameUtils.renameInAllEquations(graph, configuration, originalName, newName);\r
+ new VariableNameValidator().renameInAllEquations(graph, configuration, originalName, newName);\r
graph.claimLiteral(variable, Layer0.getInstance(graph).HasName, newName);\r
}\r
\r
import org.eclipse.swt.graphics.RGB;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.swt.widgets.Table;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.modelica.ModelicaSourceViewerConfiguration;\r
\r
@SuppressWarnings("restriction")\r
public class ExpressionFieldConfiguration extends SourceViewerConfiguration {\r
}, defaultToken);\r
\r
\r
- for(String s : VariableNameUtils.keywords) {\r
+ for(String s : ModelicaSourceViewerConfiguration.keywords) {\r
reservedWord.addWord(s, reserved);\r
}\r
\r
return functions;\r
}\r
\r
+ /**\r
+ * Get all built-in Modelica functions.\r
+ * Should be used if called inside a transaction\r
+ * @return ArrayList containing all built-in functions.\r
+ */\r
+ public static ArrayList<Function> getAllBuiltInFunctions(ReadGraph graph) throws DatabaseException {\r
+ ArrayList<Function> functions = new ArrayList<Function>();\r
+ \r
+ // Add different types of functions.\r
+ functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions, Type.SYSDYN));\r
+ functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Vensim_Functions, Type.VENSIM));\r
+ functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Functions, Type.MODELICA));\r
+ functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Array_Functions, Type.MODELICA_ARRAY));\r
+ \r
+ return functions;\r
+ }\r
+ \r
/**\r
* Get all built-in Modelica functions.\r
* @return ArrayList containing all built-in functions.\r
try {\r
result = SimanticsUI.getSession().syncRequest(new Read<ArrayList<Function>>() {\r
@Override\r
- public ArrayList<Function> perform(ReadGraph graph)\r
- throws DatabaseException {\r
- ArrayList<Function> functions = new ArrayList<Function>();\r
-\r
- // Add different types of functions.\r
- functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions, Type.SYSDYN));\r
- functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Vensim_Functions, Type.VENSIM));\r
- functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Functions, Type.MODELICA));\r
- functions.addAll(getFunctionsOfType(graph, SysdynResource.URIs.Built$in_Functions_Modelica_Array_Functions, Type.MODELICA_ARRAY));\r
- \r
- return functions;\r
+ public ArrayList<Function> perform(ReadGraph graph) throws DatabaseException {\r
+ return getAllBuiltInFunctions(graph);\r
}\r
});\r
}\r
--- /dev/null
+/*******************************************************************************\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.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\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.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.FunctionLibraryNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for user defined function libraries.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class FunctionLibraryNameInputValidator implements IInputValidator, Widget {\r
+\r
+ private Resource lastInput = null;\r
+ \r
+ public FunctionLibraryNameInputValidator(WidgetSupport support) {\r
+ support.register(this);\r
+ }\r
+ \r
+ /**\r
+ * Checks that the syntax of the given name is valid and there \r
+ * are no other components that have the same name in the configuration.\r
+ */\r
+ @Override\r
+ public String isValid(final String newText) {\r
+ if (!new FunctionLibraryNameValidator().isValid(lastInput, newText))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void setInput(ISessionContext context, Object input) {\r
+ lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\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.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\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.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.FunctionNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for user defined functions.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class FunctionNameInputValidator implements IInputValidator, Widget {\r
+\r
+ private Resource lastInput = null;\r
+ \r
+ public FunctionNameInputValidator(WidgetSupport support) {\r
+ support.register(this);\r
+ }\r
+ \r
+ /**\r
+ * Checks that the syntax of the given name is valid and there \r
+ * are no other components that have the same name in the configuration.\r
+ */\r
+ @Override\r
+ public String isValid(final String newText) {\r
+ if (!new FunctionNameValidator().isValid(lastInput, newText))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void setInput(ISessionContext context, Object input) {\r
+ lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\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.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\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.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.ModelNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for model.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class ModelNameInputValidator implements IInputValidator, Widget {\r
+\r
+ private Resource lastInput = null;\r
+ \r
+ public ModelNameInputValidator(WidgetSupport support) {\r
+ support.register(this);\r
+ }\r
+ \r
+ /**\r
+ * Checks that the syntax of the given name is valid and there \r
+ * are no other components that have the same name in the configuration.\r
+ */\r
+ @Override\r
+ public String isValid(final String newText) {\r
+ if (!new ModelNameValidator().isValid(lastInput, newText))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void setInput(ISessionContext context, Object input) {\r
+ lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+ }\r
+\r
+}\r
--- /dev/null
+/*******************************************************************************\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.factories;\r
+\r
+import org.eclipse.jface.dialogs.IInputValidator;\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.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.sysdyn.ui.utils.ModuleTypeNameValidator;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * Name validator container for module types.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class ModuleTypeNameInputValidator implements IInputValidator, Widget {\r
+\r
+ private Resource lastInput = null;\r
+ \r
+ public ModuleTypeNameInputValidator(WidgetSupport support) {\r
+ support.register(this);\r
+ }\r
+ \r
+ /**\r
+ * Checks that the syntax of the given name is valid and there \r
+ * are no other components that have the same name in the configuration.\r
+ */\r
+ @Override\r
+ public String isValid(final String newText) {\r
+ if (!new ModuleTypeNameValidator().isValid(lastInput, newText))\r
+ return "Not valid";\r
+ else\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void setInput(ISessionContext context, Object input) {\r
+ lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
+ }\r
+\r
+}\r
package org.simantics.sysdyn.ui.properties.widgets.factories;\r
\r
import org.eclipse.jface.dialogs.IInputValidator;\r
-import org.eclipse.jface.viewers.ISelection;\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.Resource;\r
import org.simantics.db.management.ISessionContext;\r
-import org.simantics.sysdyn.ui.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
import org.simantics.utils.ui.ISelectionUtils;\r
\r
-public class VariableNameValidator implements IInputValidator, Widget {\r
+/**\r
+ * Name validator container for module types.\r
+ * @author Teemu Lempinen\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class VariableNameInputValidator implements IInputValidator, Widget {\r
\r
private Resource lastInput = null;\r
\r
- public VariableNameValidator(WidgetSupport support) {\r
+ public VariableNameInputValidator(WidgetSupport support) {\r
support.register(this);\r
}\r
\r
*/\r
@Override\r
public String isValid(final String newText) {\r
- if (!VariableNameUtils.isValid(lastInput, newText, true))\r
- return "Not valid";\r
+ if (!new VariableNameValidator().isValid(lastInput, newText, true))\r
+ return "Not valid";\r
else\r
return null;\r
- /*\r
- if(lastInput == null) return null;\r
- String result = null;\r
- try {\r
- result = SimanticsUI.getSession().syncRequest(new Read<String>(){\r
-\r
- @Override\r
- public String perform(ReadGraph graph) throws DatabaseException {\r
- if(VariableNameUtils.nameIsTaken(graph, lastInput, newText)) {\r
- return "Name is taken.";\r
- }\r
- return null;\r
- }\r
- \r
- }) ;\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- return result;\r
- */\r
-\r
}\r
\r
@Override\r
public void setInput(ISessionContext context, Object input) {\r
- lastInput = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
+ lastInput = ISelectionUtils.filterSingleSelection(input, Resource.class);\r
}\r
\r
}\r
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.utils.VariableNameUtils;\r
+import org.simantics.sysdyn.ui.utils.VariableNameValidator;\r
\r
public class VariableNamePropertyModifier extends TextModifyListenerImpl<Resource> {\r
\r
Layer0 L0 = Layer0.getInstance(graph);\r
String originalName = graph.getRelatedValue(variable, L0.HasName);\r
Resource configuration = graph.getPossibleObject(variable, L0.PartOf);\r
- VariableNameUtils.renameInAllEquations(graph, configuration, originalName, text);\r
+ new VariableNameValidator().renameInAllEquations(graph, configuration, originalName, text);\r
graph.claimLiteral(variable, graph.getResource(propertyURI), text, Bindings.STRING);\r
}\r
}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for function libraries.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class FunctionLibraryNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource functionLibrary, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ \r
+ if(functionLibrary == null)\r
+ return false;\r
+ \r
+ Resource containingLibrary = graph.getPossibleObject(functionLibrary, l0.PartOf);\r
+ if(containingLibrary == null)\r
+ return true;\r
+ \r
+ // Check if the function is right under the model.\r
+ if (graph.isInstanceOf(containingLibrary, sr.SysdynModel)) {\r
+ Resource configurationResource = graph.getPossibleObject(containingLibrary, SIMU.HasConfiguration);\r
+ if(configurationResource == null)\r
+ return true;\r
+ SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+ sysdynModel.update(graph);\r
+ \r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+ \r
+ // Check that the functioLibrary name != model name (== containingLibrary name)\r
+ if (NameUtils.getSafeName(graph, containingLibrary).equals(name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, containingLibrary, null, name)) return true;\r
+ \r
+ if (nameTakenBySheet(sysdynModel, name)) return true;\r
+ }\r
+\r
+ // Containing library may also be the model\r
+ if (nameTakenByItemUnderLibrary(graph, containingLibrary, functionLibrary, name)) return true;\r
+\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for functions.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class FunctionNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName) throws DatabaseException {\r
+ // TODO\r
+ }\r
+ \r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException {\r
+ // TODO\r
+ }\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource function, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ \r
+ if(function == null)\r
+ return false;\r
+ \r
+ Resource library = graph.getPossibleObject(function, l0.PartOf);\r
+ if(library == null)\r
+ return true;\r
+ \r
+ // Check if the function is right under the model.\r
+ if (graph.isInstanceOf(library, sr.SysdynModel)) {\r
+ Resource configurationResource = graph.getPossibleObject(library, SIMU.HasConfiguration);\r
+ if(configurationResource == null)\r
+ return true;\r
+ SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+ sysdynModel.update(graph);\r
+ \r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+ \r
+ // Check that the function name != model name (== library name)\r
+ if (NameUtils.getSafeName(graph, library).equals(name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, library, null, name)) return true;\r
+ \r
+ if (nameTakenBySheet(sysdynModel, name)) return true;\r
+ }\r
+\r
+ // Containing library may also be the model\r
+ if (nameTakenByItemUnderLibrary(graph, library, function, name)) return true;\r
+\r
+ return false;\r
+ }\r
+
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for model.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class ModelNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource model, String name) throws DatabaseException {\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ \r
+ if(model == null)\r
+ return false;\r
+\r
+ if (nameTakenByFunctionFunctionLibraryOrModuleType(graph, model, name)) return true;\r
+ \r
+ Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);\r
+ if(configurationResource == null)\r
+ return true;\r
+ SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+ sysdynModel.update(graph);\r
+ \r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, model, null, name)) return true;\r
+\r
+ if (nameTakenBySheet(sysdynModel, name)) return true;\r
+\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+ throws DatabaseException {\r
+\r
+ }\r
+\r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+ throws DatabaseException {\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for module types.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class ModuleTypeNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource moduleTypeSymbol, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ if(moduleTypeSymbol == null)\r
+ return false;\r
+\r
+ Resource moduleType;\r
+ if (graph.isInheritedFrom(moduleTypeSymbol, sr.Module)) {\r
+ // Renamed in model browser\r
+ moduleType = moduleTypeSymbol;\r
+ } else {\r
+ // Renamed in ModuleTypeTab\r
+ moduleType = graph.getPossibleObject(moduleTypeSymbol, mr.SymbolToComponentType);\r
+ }\r
+ if(moduleType == null)\r
+ return true;\r
+ \r
+ // All ModuleTypes are under the top level model\r
+ Resource model = graph.getPossibleObject(moduleType, l0.PartOf);\r
+ if(model == null)\r
+ return true;\r
+\r
+ Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);\r
+ if(configurationResource == null)\r
+ return true;\r
+ SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+ sysdynModel.update(graph);\r
+ \r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+ \r
+ // Check that the module name != model name\r
+ if (NameUtils.getSafeName(graph, model).equals(name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, model, null, name)) return true;\r
+ \r
+ if (nameTakenBySheet(sysdynModel, name)) return true;\r
+\r
+ if (nameTakenByItemUnderLibrary(graph, model, moduleType, name)) return true;\r
+\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import java.util.Collection;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.IElement;\r
+import org.simantics.sysdyn.representation.Module;\r
+import org.simantics.sysdyn.representation.Sheet;\r
+import org.simantics.sysdyn.representation.Variable;\r
+import org.simantics.sysdyn.ui.modelica.ModelicaSourceViewerConfiguration;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.Function;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+\r
+/**\r
+ * This class is used when renaming an element to check that there\r
+ * are no elements in the database that would cause a conflict. There\r
+ * are two kinds of different conflicts:\r
+ * 1) There is an element in the database which has the same URI.\r
+ * Adding another causes damage or even a fatal corruption in the \r
+ * database.\r
+ * 2) Multiple different elements in the database are accessed with\r
+ * identical identifiers in the generated Modelica model. This will\r
+ * prevent the simulation from running. \r
+ * \r
+ * @author Tuomas Miettinen\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public abstract class NameValidator {\r
+\r
+ /**\r
+ * This method tells the validator whether the proposed name is taken. To be more precise,\r
+ * it tells if the proposed name should not be used for any reason. E.g. similarly named\r
+ * resources cause problems in some occasions and are fine in others. In addition, there\r
+ * (at least )are two reasons to decline a name proposition: firstly, the data base might\r
+ * have a similarly named entity, and secondly, accepting a proposed name would lead in\r
+ * naming conflicts in the generated OpenModelica model.\r
+ * \r
+ * @param graph\r
+ * @param resource the Resource which is being renamed\r
+ * @param name the proposed name\r
+ * @return true iff the name proposition is denied\r
+ * @throws DatabaseException\r
+ */\r
+ protected abstract boolean nameIsTaken(ReadGraph graph, Resource resource, String name) throws DatabaseException;\r
+\r
+ /**\r
+ * Replaces variable names in all the expressions in the defined configuration. \r
+ * Replace doesn't have to replace words in other configurations than the configuration where the renamed variable is\r
+ * because input variables handle the transitions between configurations (modules).\r
+ * \r
+ * @param graph WriteGraph\r
+ * @param configuration The configuration where the renamed variable is located\r
+ * @param originalName The original name of the variable\r
+ * @param newName New name of the variable\r
+ */\r
+ public abstract void renameInEquations(WriteGraph graph, Resource resource, String originalName, String newName) throws DatabaseException;\r
+\r
+ public abstract void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException;\r
+\r
+ /**\r
+ * Checks that the syntax of the given name is valid and there \r
+ * are no other functions that have the same name in neither the \r
+ * configuration nor among built in functions\r
+ * \r
+ * @param graph ReadGraph\r
+ * @param resource The variable that is being renamed\r
+ * @param name The new name of the variable\r
+ * @return true iff the new name is valid\r
+ * @throws DatabaseException\r
+ */\r
+ public boolean isValid(ReadGraph graph, Resource resource, String name) throws DatabaseException {\r
+ // Decline empty string\r
+ if(name.length() < 1) return false;\r
+ \r
+ // Decline names based on the type of the resource\r
+ if(nameIsTaken(graph, resource, name)) return false;\r
+ \r
+ // Decline names which are against Modelica naming rules\r
+ if(!isValidModelica(name)) return false;\r
+ return true;\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
+ * \r
+ * @param variable The variable that is being renamed\r
+ * @param name The new name of the variable\r
+ * @return true iff the new name is valid\r
+ * @throws DatabaseException\r
+ */\r
+ public boolean isValid(final Resource resource, final String name) {\r
+ boolean result = false;\r
+\r
+ try {\r
+ result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ return isValid(graph, resource, name);\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Checks that the syntax of the given name is valid \r
+ * and that it is not a keyword in Modelica.\r
+ * \r
+ * @param name name that is validated\r
+ * @return true iff the syntax of the name is valid\r
+ */\r
+ public boolean isValidModelica(String name) {\r
+ String lowerCase = name.toLowerCase();\r
+ Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*+");\r
+ Matcher m = p.matcher(lowerCase);\r
+ if (!m.matches() || ModelicaSourceViewerConfiguration.keywords.contains(name))\r
+ return false;\r
+ else\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Checks that there is no similarly named spread sheet in the top level model.\r
+ * \r
+ * @param graph ReadGraph\r
+ * @param configurationResource Resource of the configuration of the model\r
+ * @param resource Resource being validated\r
+ * @param name proposed new name for resource\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected boolean nameTakenBySheet(\r
+ ReadGraph graph, \r
+ Resource configurationResource, \r
+ Resource resource, \r
+ String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);\r
+ for (Resource r : graph.getObjects(configurationResource, l0.ConsistsOf)) {\r
+ if (graph.isInstanceOf(r, SHEET.Book)) {\r
+ for (Resource s : graph.getObjects(r, l0.ConsistsOf)) {\r
+ if (NameUtils.getSafeName(graph, s).equals(name) && !s.equals(resource)) {\r
+ return true;\r
+ }\r
+ }\r
+ // Assume only one book\r
+ break;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Checks that there is no similarly named spread sheet in the top level model.\r
+ * \r
+ * @param sysdynModel the model\r
+ * @param name proposed new name for resource\r
+ * @return\r
+ */\r
+ public boolean nameTakenBySheet(\r
+ SysdynModel sysdynModel, \r
+ String name) {\r
+ Configuration configuration = sysdynModel.getConfiguration();\r
+ if (configuration == null)\r
+ return true;\r
+ for (IElement e : configuration.getElements()) {\r
+ if (e instanceof Sheet) {\r
+ if (((Sheet)e).getName().equals(name)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Check that there are no similarly named functions, module types, or any\r
+ * other items in the same library. There are actually plenty of items that have\r
+ * to be avoided, probably all of them, they are all included in this test. \r
+ * \r
+ * @param graph ReadGraph\r
+ * @param library Resource which is studied\r
+ * @param resource Resource being validated\r
+ * @param name proposed new name for resource\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected boolean nameTakenByItemUnderLibrary(\r
+ ReadGraph graph,\r
+ Resource library,\r
+ Resource resource,\r
+ String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ for (Resource r : graph.getObjects(library, l0.ConsistsOf)) {\r
+ if (NameUtils.getSafeName(graph, r).equals(name) && !r.equals(resource)) {\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Check that there is no similarly named function library among shared function libraries.\r
+ * \r
+ * @param graph ReadGraph\r
+ * @param model resource of the top level model under which the resource is\r
+ * @param resource Resource being validated\r
+ * @param name proposed new name for resource\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected boolean nameTakenBySharedFunctionLibrary(\r
+ ReadGraph graph,\r
+ Resource model, \r
+ Resource resource,\r
+ String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Collection<Resource> linkedResources = graph.getObjects(model, l0.IsLinkedTo);\r
+ for (Resource r : linkedResources) {\r
+ // Find the "Shared functions" library \r
+ if (graph.isInstanceOf(r, sr.SharedFunctionOntology)) {\r
+ if (NameUtils.getSafeName(graph, r).equals(name) && !r.equals(resource)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Check that there is no similarly named function among built-in functions\r
+ * \r
+ * @param graph ReadGraph\r
+ * @param name name that is checked\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected boolean nameTakenByBuiltInFunction(\r
+ ReadGraph graph, \r
+ String name) throws DatabaseException {\r
+ for (Function f : Function.getAllBuiltInFunctions(graph)) {\r
+ if (f.getName().equals(name)) {\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Check that the top level of the model doesn't contain \r
+ * a variable or module with the same name.\r
+ * \r
+ * @param graph ReadGraph\r
+ * @param sysdynModel the model\r
+ * @param resource Resource being validated\r
+ * @param name proposed new name for resource\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected boolean nameTakenByTopLevelVariableOrModule(\r
+ ReadGraph graph,\r
+ SysdynModel sysdynModel,\r
+ Resource resource,\r
+ String name) throws DatabaseException {\r
+ Configuration configuration = sysdynModel.getConfiguration();\r
+ if(configuration == null)\r
+ return true;\r
+ IElement current = sysdynModel.getElement(resource);\r
+ for(IElement e : configuration.getElements()) {\r
+ if(e instanceof Variable) {\r
+ Variable v = (Variable) e;\r
+ if(!v.equals(current) && v.getName() != null && v.getName().equals(name)) {\r
+ return true;\r
+ }\r
+ } else if (e instanceof Module) {\r
+ Module m = (Module)e;\r
+ if(!m.equals(current) && m.getName() != null && m.getName().equals(name)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * Check that there is no similarly named function, function library,\r
+ * or module type directly under a library\r
+ * \r
+ * This function should not be used by functions, function libraries, or module types\r
+ *\r
+ * @param graph ReadGraph\r
+ * @param library Resource which is studied\r
+ * @param name proposed new name for resource\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected boolean nameTakenByFunctionFunctionLibraryOrModuleType(\r
+ ReadGraph graph,\r
+ Resource library,\r
+ String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for (Resource r : graph.getObjects(library, l0.ConsistsOf)) {\r
+ if (graph.isInstanceOf(r, sr.SysdynModelicaFunctionLibrary)\r
+ || graph.isInstanceOf(r, sr.SysdynModelicaFunction)\r
+ || graph.isInheritedFrom(r, sr.Module)) {\r
+ if (NameUtils.getSafeName(graph, r).equals(name)) {\r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.sysdyn.manager.SysdynModel;\r
+import org.simantics.sysdyn.manager.SysdynModelManager;\r
+\r
+/**\r
+ * Name validator for shared function libraries.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class SharedFunctionLibraryNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource functionLibrary, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+ \r
+ if(functionLibrary == null)\r
+ return false;\r
+ \r
+ // Do the checking for all models that are linked to that shared function library\r
+ for (Resource model : graph.getObjects(functionLibrary, l0.IsLinkedTo_Inverse)) {\r
+ if(model == null)\r
+ return true;\r
+\r
+ Resource configurationResource = graph.getPossibleObject(model, SIMU.HasConfiguration);\r
+ if(configurationResource == null)\r
+ return true;\r
+ SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+ sysdynModel.update(graph);\r
+ \r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, null, name)) return true;\r
+\r
+ // Check that the functionLibrary name != model name\r
+ if (NameUtils.getSafeName(graph, model).equals(name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, model, functionLibrary, name)) return true;\r
+ \r
+ if (nameTakenBySheet(sysdynModel, name)) return true;\r
+ \r
+ if (nameTakenByItemUnderLibrary(graph, model, null, name)) return true;\r
+ }\r
+ \r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.NameUtils;\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
+\r
+/**\r
+ * Name validator for spread sheets.\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class SheetNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource sheet, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ \r
+ if(sheet == null)\r
+ return false;\r
+ \r
+ Resource book = graph.getPossibleObject(sheet, l0.PartOf);\r
+ if(book == null)\r
+ return true;\r
+ \r
+ Resource configurationResource = graph.getPossibleObject(book, l0.PartOf);\r
+ if(configurationResource == null)\r
+ return true;\r
+ SysdynModel sysdynModel = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configurationResource);\r
+ sysdynModel.update(graph);\r
+ \r
+ Resource model = graph.getPossibleObject(configurationResource, l0.PartOf);\r
+ if(model == null)\r
+ return true;\r
+\r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, sheet, name)) return true;\r
+ \r
+ // Check that the sheet name != model name\r
+ if (NameUtils.getSafeName(graph, model).equals(name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, model, null, name)) return true;\r
+ \r
+ if (nameTakenBySheet(graph, configurationResource, sheet, name)) return true;\r
+\r
+ if (nameTakenByItemUnderLibrary(graph, model, null, name)) return true;\r
+\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName)\r
+ throws DatabaseException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+\r
+}\r
+++ /dev/null
-/*******************************************************************************\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.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.StringTokenizer;\r
-import java.util.regex.Matcher;\r
-import java.util.regex.Pattern;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.utils.ListUtils;\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.manager.SysdynModel;\r
-import org.simantics.sysdyn.representation.Configuration;\r
-import org.simantics.sysdyn.representation.IElement;\r
-import org.simantics.sysdyn.representation.Model;\r
-import org.simantics.sysdyn.representation.Module;\r
-import org.simantics.sysdyn.representation.ModuleType;\r
-import org.simantics.sysdyn.representation.Variable;\r
-import org.simantics.ui.SimanticsUI;\r
-\r
-public class VariableNameUtils {\r
-\r
- /**\r
- * Replaces variable names in all the expressions in the defined configuration. \r
- * Replace doesn't have to replace words in other configurations than the configuration where the renamed variable is\r
- * because input variables handle the transitions between configurations (modules).\r
- * \r
- * @param graph WriteGraph\r
- * @param configuration The configuration where the renamed variable is located\r
- * @param originalName The original name of the variable\r
- * @param newName New name of the variable\r
- */\r
- public static void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName) throws DatabaseException {\r
- /*FIXME: \r
- * How this works when range used in equations has the same string as\r
- * the renamed variable? Should it be possible? \r
- */ \r
- if(originalName.equals(newName))\r
- return; // Do nothing\r
- \r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- \r
- Resource expressions = graph.getPossibleObject(variable, sr.Variable_expressionList);\r
- if(expressions != null) {\r
- List<Resource> expressionList = ListUtils.toList(graph, expressions);\r
- for(Resource s : expressionList) {\r
- for(Resource p : graph.getPredicates(s)) {\r
- Resource o = graph.getPossibleObject(s, p);\r
- if(o != null && graph.isInstanceOf(o, l0.String) && !p.equals(sr.Expression_arrayRange)) {\r
- String string = graph.getRelatedValue(s, p);\r
- String replaced = replaceAllWords(string, originalName, newName);\r
- graph.claimLiteral(s, p, replaced);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- \r
- public static void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException {\r
- if(originalName.equals(newName))\r
- return; // Do nothing\r
-\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- for(Resource r : graph.getObjects(configuration, l0.ConsistsOf)) {\r
- if(graph.isInstanceOf(r, sr.IndependentVariable)) {\r
- renameInEquations(graph, r, originalName, newName);\r
- }\r
- }\r
- }\r
-\r
- 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 = "+-*/(){}[],.: \t\n\r\f";\r
- StringTokenizer st = new StringTokenizer(original, delimiters, true);\r
- while (st.hasMoreTokens()) {\r
- String w = st.nextToken();\r
- if (w.equals(find)) {\r
- result.append(replacement);\r
- } else {\r
- result.append(w);\r
- }\r
- }\r
- return result.toString();\r
- }\r
-\r
-\r
- private static boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException {\r
- if(variable == null)\r
- return false;\r
- SysdynModel model = ModelUtils.getModel(graph, variable);\r
- if(model == null)\r
- return true;\r
- Configuration configuration = model.getConfiguration();\r
- if(configuration == null)\r
- return true;\r
- \r
- // Browse through variables and modules within the current module\r
- IElement current = model.getElement(variable);\r
- for(IElement e : configuration.getElements()) {\r
- if(e instanceof Variable) {\r
- Variable v = (Variable) e;\r
- if(!v.equals(current) && v.getName() != null && v.getName().equals(name)) {\r
- return true;\r
- }\r
- } else if (e instanceof Module) {\r
- Module m = (Module)e;\r
- if(!m.equals(current) && m.getName() != null && m.getName().equals(name)) {\r
- return true;\r
- }\r
- }\r
- }\r
- \r
- // Browse through all ModuleTypes within the whole model\r
- Model rootModel = null;\r
- ModuleType moduleType = configuration.getModuleType();\r
- if (moduleType != null) {\r
- Object o = moduleType.getParent();\r
- if (o instanceof Model) {\r
- rootModel = (Model)o;\r
- }\r
- } else {\r
- rootModel = configuration.getModel();\r
- }\r
- if (rootModel != null)\r
- return rootModel.containsModuleType(name);\r
- \r
- // We should not go here I think\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
- * \r
- * @param graph ReadGraph\r
- * @param variable The variable that is being renamed\r
- * @param name The new name of the variable\r
- * @return\r
- * @throws DatabaseException\r
- */\r
- public static boolean isValid(ReadGraph graph, Resource variable, String name, boolean hasRange) throws DatabaseException {\r
- if(name.length() < 1)\r
- return false;\r
- if(hasRange) {\r
- String range = null;\r
- if(name.contains("[")) {\r
- StringTokenizer st = new StringTokenizer(name, "[]", true);\r
- if(st.countTokens() != 4)\r
- return false;\r
- name = st.nextToken();\r
- if(!st.nextToken().equals("[")) return false;\r
- range = st.nextToken();\r
- if(!st.nextToken().equals("]")) return false;\r
- }\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
- /**\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
- * \r
- * @param variable The variable that is being renamed\r
- * @param name The new name of the variable\r
- * @return\r
- * @throws DatabaseException\r
- */\r
- public static boolean isValid(final Resource variable, final String name, final boolean hasRange) {\r
- boolean result = false;\r
- try {\r
- result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
-\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- return isValid(graph, variable, name, hasRange);\r
- }\r
-\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- return result;\r
- }\r
-\r
- /**\r
- * Checks that the syntax of the given name is valid \r
- * and that it is not a keyword in Modelica.\r
- * \r
- * @param name\r
- * @return\r
- */\r
- public static boolean isValid(String name) {\r
- String lowerCase = name.toLowerCase();\r
- Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9]*+");\r
- Matcher m = p.matcher(lowerCase);\r
- if (!m.matches() || keywords.contains(name))\r
- return false;\r
- else\r
- return true;\r
- }\r
-\r
- public static final Set<String> keywords = new HashSet<String>();\r
-\r
- static {\r
- keywords.add("algorithm");\r
- keywords.add("discrete");\r
- keywords.add("false");\r
- keywords.add("model");\r
- keywords.add("redeclare");\r
- keywords.add("and");\r
- keywords.add("each");\r
- keywords.add("final");\r
- keywords.add("not");\r
- keywords.add("replaceable");\r
- keywords.add("annotation");\r
- keywords.add("else");\r
- keywords.add("flow");\r
- keywords.add("operator");\r
- keywords.add("return");\r
- keywords.add("assert");\r
- keywords.add("elseif");\r
- keywords.add("for");\r
- keywords.add("or");\r
- keywords.add("stream");\r
- keywords.add("block");\r
- keywords.add("elsewhen");\r
- keywords.add("function");\r
- keywords.add("outer");\r
- keywords.add("then");\r
- keywords.add("break");\r
- keywords.add("encapsulated");\r
- keywords.add("if");\r
- keywords.add("output");\r
- keywords.add("true");\r
- keywords.add("class");\r
- keywords.add("end");\r
- keywords.add("import");\r
- keywords.add("package");\r
- keywords.add("type");\r
- keywords.add("connect");\r
- keywords.add("enumeration");\r
- keywords.add("in");\r
- keywords.add("parameter");\r
- keywords.add("when");\r
- keywords.add("connector");\r
- keywords.add("equation");\r
- keywords.add("initial");\r
- keywords.add("partial");\r
- keywords.add("while");\r
- keywords.add("constant");\r
- keywords.add("expandable");\r
- keywords.add("inner");\r
- keywords.add("protected");\r
- keywords.add("within");\r
- keywords.add("constrainedby");\r
- keywords.add("extends");\r
- keywords.add("input");\r
- keywords.add("public");\r
- keywords.add("der");\r
- keywords.add("external");\r
- keywords.add("loop");\r
- keywords.add("record");\r
- keywords.add("time");\r
- }\r
-}\r
--- /dev/null
+/*******************************************************************************\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.util.List;\r
+import java.util.StringTokenizer;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.ListUtils;\r
+import org.simantics.db.common.utils.NameUtils;\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.manager.SysdynModel;\r
+import org.simantics.sysdyn.representation.Configuration;\r
+import org.simantics.sysdyn.representation.Model;\r
+import org.simantics.sysdyn.representation.ModuleType;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+/**\r
+ * Name validator for variables, modules, and enumerations.\r
+ * @author Teemu Lempinen\r
+ * @author Tuomas Miettinen\r
+ * \r
+ */\r
+public class VariableNameValidator extends NameValidator {\r
+\r
+ @Override\r
+ public void renameInEquations(WriteGraph graph, Resource variable, String originalName, String newName) throws DatabaseException {\r
+ /*FIXME: \r
+ * How this works when range used in equations has the same string as\r
+ * the renamed variable? Should it be possible? \r
+ */ \r
+ if(originalName.equals(newName))\r
+ return; // Do nothing\r
+ \r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ \r
+ Resource expressions = graph.getPossibleObject(variable, sr.Variable_expressionList);\r
+ if(expressions != null) {\r
+ List<Resource> expressionList = ListUtils.toList(graph, expressions);\r
+ for(Resource s : expressionList) {\r
+ for(Resource p : graph.getPredicates(s)) {\r
+ Resource o = graph.getPossibleObject(s, p);\r
+ if(o != null && graph.isInstanceOf(o, l0.String) && !p.equals(sr.Expression_arrayRange)) {\r
+ String string = graph.getRelatedValue(s, p);\r
+ String replaced = replaceAllWords(string, originalName, newName);\r
+ graph.claimLiteral(s, p, replaced);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void renameInAllEquations(WriteGraph graph, Resource configuration, String originalName, String newName) throws DatabaseException {\r
+ if(originalName.equals(newName))\r
+ return; // Do nothing\r
+\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for(Resource r : graph.getObjects(configuration, l0.ConsistsOf)) {\r
+ if(graph.isInstanceOf(r, sr.IndependentVariable)) {\r
+ renameInEquations(graph, r, originalName, newName);\r
+ }\r
+ }\r
+ }\r
+\r
+ private 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 = "+-*/(){}[],.: \t\n\r\f";\r
+ StringTokenizer st = new StringTokenizer(original, delimiters, true);\r
+ while (st.hasMoreTokens()) {\r
+ String w = st.nextToken();\r
+ if (w.equals(find)) {\r
+ result.append(replacement);\r
+ } else {\r
+ result.append(w);\r
+ }\r
+ }\r
+ return result.toString();\r
+ }\r
+\r
+\r
+ @Override\r
+ protected boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ if(variable == null)\r
+ return false;\r
+ \r
+ SysdynModel sysdynModel = ModelUtils.getModel(graph, variable);\r
+ if(sysdynModel == null)\r
+ return true;\r
+ sysdynModel.update(graph);\r
+ \r
+ Configuration configuration = sysdynModel.getConfiguration();\r
+ if(configuration == null)\r
+ return true;\r
+ \r
+ // Get the top level model\r
+ Model rootModel = null;\r
+ ModuleType moduleType = configuration.getModuleType();\r
+ if (moduleType != null) {\r
+ Object o = moduleType.getParent();\r
+ if (o instanceof Model) {\r
+ rootModel = (Model)o;\r
+ }\r
+ } else {\r
+ rootModel = configuration.getModel();\r
+ }\r
+ \r
+ // Get the top level model (SysdynModel)\r
+ // Resource library should be used with care if it is not instance of sr.SysdynModel\r
+ Resource library = graph.getPossibleObject(variable, l0.PartOf);\r
+ if(library == null) {\r
+ return true;\r
+ } else {\r
+ library = graph.getPossibleObject(library, l0.PartOf);\r
+ if(library == null) {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ // Check if the function is right under the model.\r
+ if (graph.isInstanceOf(library, sr.SysdynModel)) {\r
+ // Check that the function name != model name (== library name)\r
+ if (NameUtils.getSafeName(graph, library).equals(name)) return true;\r
+ \r
+ if (nameTakenByBuiltInFunction(graph, name)) return true;\r
+\r
+ if (nameTakenBySharedFunctionLibrary(graph, library, null, name)) return true;\r
+ \r
+ if (nameTakenBySheet(sysdynModel, name)) return true;\r
+ }\r
+\r
+ if (nameTakenByTopLevelVariableOrModule(graph, sysdynModel, variable, name)) return true;\r
+\r
+ if (rootModel != null) {\r
+ // Browse through all ModuleTypes within the whole model\r
+ if (rootModel.containsModuleType(name))\r
+ return true;\r
+ }\r
+ \r
+ if (nameTakenByFunctionFunctionLibraryOrModuleType(graph, library, name)) return true;\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
+ * \r
+ * @param graph ReadGraph\r
+ * @param variable The variable that is being renamed\r
+ * @param name The new name of the variable\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ public boolean isValid(ReadGraph graph, Resource variable, String name, boolean hasRange) throws DatabaseException {\r
+ if(hasRange) {\r
+ String range = null;\r
+ if(name.contains("[")) {\r
+ StringTokenizer st = new StringTokenizer(name, "[]", true);\r
+ if(st.countTokens() != 4)\r
+ return false;\r
+ name = st.nextToken();\r
+ if(!st.nextToken().equals("[")) return false;\r
+ range = st.nextToken();\r
+ if(!st.nextToken().equals("]")) return false;\r
+ }\r
+ if(range != null && !ArrayVariableUtils.isRangeValid(graph, variable, range)) return false;\r
+ }\r
+ return isValid(graph, variable, name);\r
+ }\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
+ * \r
+ * @param variable The variable that is being renamed\r
+ * @param name The new name of the variable\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ public boolean isValid(final Resource variable, final String name, final boolean hasRange) {\r
+ boolean result = false;\r
+ try {\r
+ result = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+ return isValid(graph, variable, name, hasRange);\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return result;\r
+ }\r
+\r
+}\r
return false;\r
}\r
\r
+ /**\r
+ * Determine if this Model consists of a certain Function or \r
+ * FunctionLibrary ON THE TOP LEVEL.\r
+ * @param elementName The name of the element that is sought \r
+ * @return true iff this consists of element\r
+ */\r
+ public boolean containsFunctionOrLibrary(String elementName) {\r
+ if(consistsOf != null ) {\r
+ for(Object o : consistsOf) {\r
+ if(o instanceof Function) {\r
+ Function f = (Function)o;\r
+ if (f.getName().equals(elementName)) \r
+ return true;\r
+ }\r
+ else if(o instanceof FunctionLibrary) {\r
+ FunctionLibrary fl = (FunctionLibrary)o;\r
+ if (fl.getName().equals(elementName)) \r
+ return true;\r
+ }\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
/**\r
* Add built-in functions to functions map. Built-in functions do not have any library path.\r
* @param library Built-in function library\r