From cdbe3d1670eb0b155bc2aecb9e17188e844afeb9 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 3 Dec 2010 12:36:12 +0000 Subject: [PATCH] Variable name validation that supports range indexes git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18924 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../elements2/HoverTextElementNoBounds.java | 8 +- .../sysdyn/ui/properties/EquationTab.java | 4 +- .../ui/properties/VariableNameUtils.java | 171 +++++++++++++----- .../factories/VariableNameValidator.java | 12 +- .../simantics/sysdyn/manager/SysdynModel.java | 13 ++ .../sysdyn/representation/IndexUtils.java | 3 +- 6 files changed, 155 insertions(+), 56 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java index 3b59fa80..61deb088 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/HoverTextElementNoBounds.java @@ -80,7 +80,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { @Override public void textChanged() { TextNode node = (TextNode) e.getHint(SG_NODE); - if(!VariableNameUtils.isValid(component, node.getText())) { + if(!VariableNameUtils.isValid(component, node.getText(), false)) { node.setColor(Color.RED); } else { node.setColor(Color.BLACK); @@ -113,7 +113,7 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { public void textEditingCancelled() { TextNode node = (TextNode) e.getHint(SG_NODE); if (node != null) { - if(VariableNameUtils.isValid(component, node.getText())) + if(VariableNameUtils.isValid(component, node.getText(), false)) node.setColor(Color.BLACK); endEdit(node); } @@ -125,10 +125,10 @@ public class HoverTextElementNoBounds extends TextElementNoBounds { if (node == null) return; String text = node.getText(); - if(!VariableNameUtils.isValid(component, text)) { + if(!VariableNameUtils.isValid(component, text, false)) { text = textBeforeEdit; node.setText(text); - if(VariableNameUtils.isValid(component, text)) + if(VariableNameUtils.isValid(component, text, false)) node.setColor(Color.BLACK); } else { Object o = e.getHint(ElementHints.KEY_OBJECT); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index 6e1af6e9..a913f9ed 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -71,6 +71,7 @@ import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget; import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget; import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget; +import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNameValidator; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.AdaptionUtils; import org.simantics.utils.datastructures.Pair; @@ -106,8 +107,9 @@ public class EquationTab extends PropertyTabContributorImpl implements Widget { GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite); arrayEquationCombo = new ArrayExpressionCombo(nameComposite, support, SWT.DROP_DOWN | SWT.BORDER); + arrayEquationCombo.setInputValidator(new VariableNameValidator(support)); GridDataFactory.fillDefaults().grab(true, false).applyTo(arrayEquationCombo.getWidget()); - + deleteExpression = new Button(nameComposite, support, SWT.NONE); deleteExpression.setText("Delete"); GridDataFactory.fillDefaults().applyTo(deleteExpression.getWidget()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameUtils.java index ee10eeec..4d1b6ead 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableNameUtils.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.properties; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -26,6 +27,13 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.Enumeration; +import org.simantics.sysdyn.representation.EnumerationIndex; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.Variable; import org.simantics.ui.SimanticsUI; public class VariableNameUtils { @@ -44,7 +52,7 @@ public class VariableNameUtils { /*FIXME: * How this works when range used in equations has the same string as * the renamed variable? Should it be possible? - */ + */ Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); Resource configuration = graph.getSingleObject(variable, l0.PartOf); @@ -66,52 +74,53 @@ public class VariableNameUtils { } } } - + private static String replaceAllWords(String original, String find, String replacement) { if(!original.contains(find)) return original; - StringBuilder result = new StringBuilder(original.length()); - String delimiters = "+-*/(){}[],. "; - StringTokenizer st = new StringTokenizer(original, delimiters, true); - while (st.hasMoreTokens()) { - String w = st.nextToken(); - if (w.equals(find)) { - result.append(replacement); - } else { - result.append(w); - } - } - return result.toString(); + StringBuilder result = new StringBuilder(original.length()); + String delimiters = "+-*/(){}[],. "; + StringTokenizer st = new StringTokenizer(original, delimiters, true); + while (st.hasMoreTokens()) { + String w = st.nextToken(); + if (w.equals(find)) { + result.append(replacement); + } else { + result.append(w); + } + } + return result.toString(); } - - public static boolean nameIsTaken(final Resource variable, final String name) { - boolean result = false; - try { - result = SimanticsUI.getSession().syncRequest(new Read() { - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - return nameIsTaken(graph, variable, name); - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); + private static boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException { + SysdynModel model = getModel(graph, variable); + Configuration configuration = model.getConfiguration(); + IElement current = model.getElement(variable); + for(IElement e : configuration.getElements()) { + if(e instanceof Variable) { + Variable v = (Variable) e; + if(!v.equals(current) && v.getName().equals(name)) { + return true; + } + } } - return result; + return false; } - - public static boolean nameIsTaken(ReadGraph graph, Resource variable, String name) throws DatabaseException { + + + private static SysdynModel getModel(ReadGraph graph, Resource variable) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Resource configuration = graph.getSingleObject(variable, l0.PartOf); - for(Resource r : graph.getObjects(configuration, l0.ConsistsOf)) { - String componentName = graph.getPossibleRelatedValue(r, l0.HasName); - if(componentName != null && !r.equals(variable) && componentName.equals(name)) { - return true; - } + SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); + SysdynModel model = sdm.getModel(graph, configuration); + try { + model.update(graph); + } catch (DatabaseException e1) { + e1.printStackTrace(); } - return false; + return model; } - + /** * Checks that the syntax of the given name is valid and there * are no other variables that have the same name in the configuration @@ -122,12 +131,75 @@ public class VariableNameUtils { * @return * @throws DatabaseException */ - public static boolean isValid(ReadGraph graph, Resource variable, String name) throws DatabaseException { + public static boolean isValid(ReadGraph graph, Resource variable, String name, boolean hasRange) throws DatabaseException { + if(name.length() < 1) + return false; + if(hasRange) { + String range = null; + if(name.contains("[")) { + StringTokenizer st = new StringTokenizer(name, "[]", true); + if(st.countTokens() != 4) + return false; + name = st.nextToken(); + if(!st.nextToken().equals("[")) return false; + range = st.nextToken(); + if(!st.nextToken().equals("]")) return false; + } + if(range != null && !isRangeValid(graph, variable, range)) return false; + } if(nameIsTaken(graph, variable, name)) return false; if(!isValid(name)) return false; return true; } - + + /** + * Checks if the given range can be applied to the given variable. + * + * @param graph ReadGraph + * @param variable Resource of the variable + * @param range Range WITHOUT [ and ] brackets + * @return true if range is valid, false if not + * @throws DatabaseException + */ + private static boolean isRangeValid(ReadGraph graph, Resource variable, String range) throws DatabaseException { + String[] elements = range.split(","); + SysdynModel model = getModel(graph, variable); + IElement e = model.getElement(variable); + if(e != null && e instanceof Variable) { + Variable v = (Variable) e; + ArrayList enumerations = v.getArrayIndexes().getEnumerations(); + if(elements.length != enumerations.size()) return false; + for(int i = 0; i < enumerations.size(); i++) { + if(elements[i].trim().equals(":")) + continue; + if(elements[i].indexOf(":") != elements[i].lastIndexOf(":")) + return false; + + String[] rangeComponents = elements[i].split(":"); + if(rangeComponents.length > 2) + return false; + // Single range component, equals to the enumeration at that index + if(rangeComponents.length == 1 && rangeComponents[0].trim().equals(enumerations.get(i).getName())) + continue; + // one or two range components, they all equal to individual indexes in the enumeration + for(String r : rangeComponents) { + r = r.trim(); + boolean componentIsValid = false; + for(EnumerationIndex ei : enumerations.get(i).getEnumerationIndexes()) { + if(ei.getName().equals(r)) { + componentIsValid = true; + break; + } + } + if(!componentIsValid) + return false; + } + } + return true; + } + + return false; + } /** * Checks that the syntax of the given name is valid and there * are no other variables that have the same name in the configuration @@ -137,12 +209,23 @@ public class VariableNameUtils { * @return * @throws DatabaseException */ - public static boolean isValid(Resource variable, String name) { - if(nameIsTaken(variable, name)) return false; - if(!isValid(name)) return false; - return true; + public static boolean isValid(final Resource variable, final String name, final boolean hasRange) { + boolean result = false; + try { + result = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + return isValid(graph, variable, name, hasRange); + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return result; } - + /** * Checks that the syntax of the given name is valid * and that it is not a keyword in Modelica. @@ -159,7 +242,7 @@ public class VariableNameUtils { else return true; } - + public static final Set keywords = new HashSet(); static { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java index 5e8db8ad..879dc8f6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/VariableNameValidator.java @@ -15,13 +15,9 @@ import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.viewers.ISelection; import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; -import org.simantics.db.request.Read; import org.simantics.sysdyn.ui.properties.VariableNameUtils; -import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ISelectionUtils; public class VariableNameValidator implements IInputValidator, Widget { @@ -38,8 +34,11 @@ public class VariableNameValidator implements IInputValidator, Widget { */ @Override public String isValid(final String newText) { - if (!VariableNameUtils.isValid(newText)) - return "Sorry but spaces and special characters are not allowed for names right now"; + if (!VariableNameUtils.isValid(lastInput, newText, true)) + return "Not valid"; + else + return null; + /* if(lastInput == null) return null; String result = null; try { @@ -58,6 +57,7 @@ public class VariableNameValidator implements IInputValidator, Widget { e.printStackTrace(); } return result; + */ } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 4cc2b226..cb6fe078 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -298,6 +298,19 @@ public class SysdynModel implements IMappingListener, IModel { } } + public synchronized boolean update(ReadGraph graph) throws DatabaseException { + if(mapping.isDomainModified()) { + mapping.updateRange(graph); + for(Resource config : readModules(graph, configurationResource)) { + if(!modules.contains(config)) + modules.add((Configuration)mapping.map(graph, config)); + } + return true; + } + else + return false; + } + public synchronized boolean update() throws DatabaseException { if(mapping.isDomainModified()) { session.syncRequest(new ReadRequest() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java index d7eaf9ae..407c76ff 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndexUtils.java @@ -37,7 +37,8 @@ public class IndexUtils { sb.append(":"); } else { int i = indexOf(enumerations.get(index), rangeToken); - sb.append(i); + if(i >= 0) + sb.append(i); } } -- 2.47.1