From 2dc41a246c29759666b9a24ef5695f1137ee9aa4 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 11 Jul 2011 13:01:02 +0000 Subject: [PATCH] Valid spreadsheet cells are recognized by expression parser git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@21467 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../widgets/expressions/ExpressionField.java | 20 +++-- .../sysdyn/ui/utils/ArrayVariableUtils.java | 4 +- .../sysdyn/ui/utils/ExpressionUtils.java | 76 +++++++++++++++++-- .../expressionParser/ExpressionParser.jj | 22 +++--- .../simantics/sysdyn/representation/Book.java | 5 +- .../sysdyn/representation/Configuration.java | 7 ++ .../sysdyn/representation/Sheet.java | 36 +++++++-- .../sysdyn/representation/Variable.java | 10 ++- 8 files changed, 142 insertions(+), 38 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java index aac8c3b9..38f0c16d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java @@ -45,6 +45,10 @@ public class ExpressionField extends Composite { protected IDocument _document; protected AnnotationModel _annotationModel; + public static final String MISSING_LINK = "MissingLink"; + public static final String NO_SUCH_VARIABLE = "NoSuchVariable"; + public static final String SYNTAX_ERROR = "SyntaxError"; + String oldExpression; ColorManager cManager = new ColorManager(); @@ -78,10 +82,12 @@ public class ExpressionField extends Composite { AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess); _sourceViewer.addPainter(painter); - painter.addAnnotationType("MissingLink"); - painter.setAnnotationTypeColor("MissingLink", new Color(this.getDisplay(), 255,0,0)); - painter.addAnnotationType("NoSuchVariable"); - painter.setAnnotationTypeColor("NoSuchVariable", new Color(this.getDisplay(), 255,0,0)); + painter.addAnnotationType(MISSING_LINK); + painter.setAnnotationTypeColor(MISSING_LINK, new Color(this.getDisplay(), 255,215,0)); + painter.addAnnotationType(NO_SUCH_VARIABLE); + painter.setAnnotationTypeColor(NO_SUCH_VARIABLE, new Color(this.getDisplay(), 255,0,0)); + painter.addAnnotationType(SYNTAX_ERROR); + painter.setAnnotationTypeColor(SYNTAX_ERROR, new Color(this.getDisplay(), 255,0,0)); _sourceViewer.setDocument(_document, _annotationModel); @@ -122,7 +128,7 @@ public class ExpressionField extends Composite { public void setMissingLinkAnnotations(List positions){ for(Position p : positions) { Annotation annotation = new Annotation(false); - annotation.setType("MissingLink"); + annotation.setType(MISSING_LINK); annotation.setText("No link to this variable"); _annotationModel.addAnnotation(annotation, p); } @@ -131,7 +137,7 @@ public class ExpressionField extends Composite { public void setNoSuchVariableAnnotations(List positions){ for(Position p : positions) { Annotation annotation = new Annotation(false); - annotation.setType("NoSuchVariable"); + annotation.setType(NO_SUCH_VARIABLE); annotation.setText("No such variable in model"); _annotationModel.addAnnotation(annotation, p); } @@ -156,7 +162,7 @@ public class ExpressionField extends Composite { e.printStackTrace(); } } - setSyntaxError(start, offset, "MissingLink", message == null ? "Syntax Error" : message); + setSyntaxError(start, offset, SYNTAX_ERROR, message == null ? "Syntax Error" : message); } public void setSyntaxError(int start, int offset, String type, String text) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java index 834e90cc..574f71b8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ArrayVariableUtils.java @@ -30,7 +30,9 @@ public class ArrayVariableUtils { return null; Map result = new HashMap(); // Not an array variable - if(variable.getArrayIndexes() == null) { + if(variable.getArrayIndexes() == null || + variable.getArrayIndexes().getEnumerations() == null || + variable.getArrayIndexes().getEnumerations().size() == 0) { for(int i = 0; i < elements.length ; i++) result.put(i, "Variable is not an array variable"); return result; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java index 904378ba..c35cd3a2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ExpressionUtils.java @@ -41,6 +41,8 @@ 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.Module; +import org.simantics.sysdyn.representation.Sheet; import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField; import org.simantics.sysdyn.ui.properties.widgets.expressions.IExpression; @@ -137,12 +139,13 @@ public class ExpressionUtils { } catch (ParseException e1) { ef.setSyntaxError(e1.currentToken, "Syntax Error"); } catch (TokenMgrError err) { - ef.setSyntaxError(0, textString.length(), "MissingLink", "Expression contains unsupported characters"); + ef.setSyntaxError(0, textString.length(), ExpressionField.SYNTAX_ERROR, "Expression contains unsupported characters"); } } final HashMap modelVariables = new HashMap(); + HashSet ignoreVariables = new HashSet(); if(!variables.isEmpty()) { Set noSuchVariables = new HashSet(); @@ -160,6 +163,8 @@ public class ExpressionUtils { if(e instanceof Variable) { Variable v = (Variable) e; modelVariables.put(v.getName(), v); + } else if(e instanceof Module) { + ignoreVariables.add(((Module)e).getName()); } } @@ -169,12 +174,30 @@ public class ExpressionUtils { variables.remove("time"); for(String v : variables) { - if(!modelVariables.keySet().contains(v)) { + + if(modelVariables.get(v) instanceof Enumeration || modelVariables.get(v) instanceof Sheet) { + ignoreVariables.add(v); + } + + // Reference to some other module, spreadsheet or enumeration + if(v.contains(".")) { + String[] parts = v.split("\\."); + Object parent = conf; + for(int i = 0; i < parts.length && parent != null; i++) { + parent = getElement(parent, parts[i]); + } + if(parent == null) { + noSuchVariables.add(v); + } else { + ignoreVariables.add(v); + } + } else if(!modelVariables.keySet().contains(v)) { noSuchVariables.add(v); } } if(!noSuchVariables.isEmpty()) { + noSuchVariables.removeAll(ignoreVariables); // remove no such variables from variable list for(String s : noSuchVariables) variables.remove(s); @@ -201,12 +224,8 @@ public class ExpressionUtils { } } - // Remove all enumerations, they cannot have connections - for(String name : modelVariables.keySet()) { - if(modelVariables.get(name) instanceof Enumeration && variables.contains(name)) { - variables.remove(name); - } - } + // Remove all enumerations and sheets, they cannot have connections + variables.removeAll(ignoreVariables); if(!variables.isEmpty()) { HashMap> positions = getPositionsForVariables(references, variables); @@ -335,6 +354,47 @@ public class ExpressionUtils { } } + + static private Object getElement(Object parent, String name) { + Configuration c = null; + if(parent instanceof Module) { + Module m = (Module)parent; + c = m.getType().getConfiguration(); + } else if (parent instanceof Configuration) { + c = (Configuration)parent; + } + + if(c != null) { + for(IElement e : c.getElements()) { + if(e instanceof Variable && ((Variable)e).getName().equals(name)) { + return e; + } else if(e instanceof Module && ((Module)e).getName().equals(name)) { + return e; + } + } + } else if(parent instanceof Sheet) { + Sheet s = (Sheet)parent; + for(String key : s.getCells().keySet()) { + if(key.equals(name)) { + return Boolean.TRUE; + } + } + + } else if(parent instanceof Enumeration) { + Enumeration e = (Enumeration)parent; + if(name.equals("size") || name.equals("elements")) + return Boolean.TRUE; + + for(EnumerationIndex ei : e.getEnumerationIndexes()) { + if(ei.getName().equals(name)) { + return Boolean.TRUE; + } + } + + } + + return null; + } @SuppressWarnings("unchecked") static private HashMap> getPositionsForVariables(HashMap>> references, Set variables) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj index 57cbe036..f1173c1b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -202,37 +202,37 @@ void name() : { ( "." name() )? } -void component_reference(Token prevToken) : { +void component_reference(String prevToken) : { } { //IDENT [ array_subscripts ] [ "." component_reference ] { - String name = token.image; - + String name = token.image; // prevToken != null => this is the second part of an enumeration if(forIndex == true && prevToken != null) { - if(enumerationReferences.get(prevToken.image) == null) { enumerationReferences.put(prevToken.image, new ArrayList()); + if(enumerationReferences.get(prevToken) == null) { enumerationReferences.put(prevToken, new ArrayList()); } - List list = enumerationReferences.get(prevToken.image); + List list = enumerationReferences.get(prevToken); list.add(token); // forIndex == true, prevToken == null => this is the enumeration } else if(forIndex == true && prevToken == null) { - if(enumerationReferences.get(token.image) == null) { - enumerationReferences.put(token.image, new ArrayList()); + if(enumerationReferences.get(name) == null) { + enumerationReferences.put(name, new ArrayList()); } - List list = enumerationReferences.get(token.image); + List list = enumerationReferences.get(name); list.add(token); - } else { if(references.get(name) == null) { + } else { + if(prevToken != null) + name = prevToken + "." + name; if(references.get(name) == null) { references.put(name, new ArrayList()); } List list = references.get(name); list.add(token); } - Token curToken = token; } - ( array_subscripts() )? ( "." component_reference(curToken) )? + ( array_subscripts() )? ( "." component_reference(name) )? } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java index 17e1baa9..7a306f4e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Book.java @@ -3,13 +3,12 @@ package org.simantics.sysdyn.representation; import java.util.ArrayList; import java.util.List; -import org.simantics.objmap.IMapping; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElements; import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @GraphType("http://www.simantics.org/Spreadsheet-1.0/Book") -public class Book implements IElement { +public class Book extends Variable { @RelatedElements( value = "http://www.simantics.org/Layer0-1.0/ConsistsOf", @@ -28,7 +27,7 @@ public class Book implements IElement { public String getBook() { StringBuilder book = new StringBuilder(); for(Sheet sheet : sheets) - book.append(sheet.getCells()); + book.append(sheet.getStringRepresentation()); return book.toString(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java index 208759bd..298aada1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java @@ -41,6 +41,13 @@ public class Configuration { public ArrayList getElements() { + ArrayList elements = new ArrayList(); + elements.addAll(this.elements); + for(IElement e : this.elements) { + if(e instanceof Book) { + elements.addAll(((Book)e).getSheets()); + } + } return elements; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java index e33b0f50..542df9df 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java @@ -4,25 +4,28 @@ import java.util.HashMap; import org.simantics.databoard.Bindings; import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.primitives.MutableString; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.exception.MissingVariableException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; +import org.simantics.layer0.Layer0; import org.simantics.objmap.annotations.GraphType; -import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.UpdateMethod; import org.simantics.spreadsheet.Range; import org.simantics.spreadsheet.SheetVariables; import org.simantics.spreadsheet.common.exception.CellParseException; import org.simantics.spreadsheet.util.SpreadsheetUtils; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @GraphType("http://www.simantics.org/Spreadsheet-1.0/Spreadsheet") -public class Sheet { +public class Sheet extends org.simantics.sysdyn.representation.Variable { - @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") - protected String name; + @RelatedElement("http://www.simantics.org/Layer0-1.0/PartOf") + protected Book book; HashMap cells = new HashMap(); @@ -32,7 +35,9 @@ public class Sheet { @UpdateMethod public boolean updateCells(ReadGraph g, Resource r) throws DatabaseException { - System.out.println("updating cells"); + + g.getObjects(r, Layer0.getInstance(g).ConsistsOf); + Variable v = g.adapt(r, Variable.class); cells.clear(); for(Variable child : v.browseChildren(g)) { @@ -49,7 +54,7 @@ public class Sheet { return true; } - public String getCells() { + public String getStringRepresentation() { StringBuilder clazz = new StringBuilder(); clazz.append(" class " + name + "_class\n "); @@ -57,6 +62,14 @@ public class Sheet { int counter = 0; for(String key : cells.keySet()) { Object value = cells.get(key); + if(value instanceof String || value instanceof MutableString) { + try { + Double d = Double.parseDouble(value.toString()); + value = d; + } catch (NumberFormatException e) { + } + } + if(value instanceof Double) { Double d = (Double)value; clazz.append("constant Real " + key + " = " + d + "; "); @@ -71,5 +84,16 @@ public class Sheet { clazz.append(" " + name + "_class " + name + ";\n"); return clazz.toString(); } + + public HashMap getCells() { + return cells; + } + + @Override + public void accept(IElementVisitorVoid v) { + + } + + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java index 4ad6ab41..6ec5f47f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -24,7 +24,7 @@ public abstract class Variable implements IElement { protected String name; @RelatedElement("http://www.simantics.org/Layer0-1.0/PartOf") - protected Configuration configuration; + protected Object parent; @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasArrayIndexes") protected ArrayIndexes arrayIndexes; @@ -38,7 +38,13 @@ public abstract class Variable implements IElement { } public Configuration getParentConfiguration() { - return this.configuration; + if(parent instanceof Configuration) { + return (Configuration)parent; + } else if(parent instanceof Book) { + return (Configuration)((Book)parent).getParentConfiguration(); + } else { + return null; + } } public String getType() { -- 2.47.1