From: lempinen Date: Wed, 20 Jan 2010 15:54:43 +0000 (+0000) Subject: Comparison of dependencies and expression texts X-Git-Tag: simantics-1.0~102 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a9d605d5516c7adb3e12613cfa121edebe07c90d;p=simantics%2Fsysdyn.git Comparison of dependencies and expression texts git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13570 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 1819e6ff..742ae5a3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -1,6 +1,10 @@ package org.simantics.sysdyn.ui.equation; +import java.io.StringReader; +import java.util.HashSet; +import java.util.Set; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ISelection; @@ -8,10 +12,13 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.RowLayout; @@ -30,6 +37,8 @@ import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; +import org.simantics.sysdyn.expressionParser.ExpressionParser; +import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.ui.SimanticsUI; public class EquationView extends ViewPart implements ISelectionListener { @@ -54,7 +63,6 @@ public class EquationView extends ViewPart implements ISelectionListener { Composite unitsAndRange; Composite emptyComposite; - @Override public void createPartControl(Composite parent) { // Listeners @@ -202,15 +210,15 @@ public class EquationView extends ViewPart implements ISelectionListener { unitSelector.updateUnits(variable); } + if(shortcutTabs != null) shortcutTabs.updateTables(variable); + String expressionType = expressionComposite.resetExpressionView(variable); + addExpressionFieldListeners(); + validateExpressionFields(); expressionController.setExpressionTypes(expressionComposite.getExpressionTypes()); expressionController.select(expressionType); nameText.setText(result.name); - - if(shortcutTabs != null) { - shortcutTabs.updateTables(variable); - } } }); @@ -247,6 +255,8 @@ public class EquationView extends ViewPart implements ISelectionListener { @Override public void widgetSelected(SelectionEvent e) { expressionComposite.displayExpressionView(expressionController.getSelecetedType()); + addExpressionFieldListeners(); + validateExpressionFields(); } @Override @@ -300,7 +310,46 @@ public class EquationView extends ViewPart implements ISelectionListener { }); } - - + private void addExpressionFieldListeners() { + if(expressionComposite.getExpressionViewFactor() != null) + for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) { + text.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + validateExpressionFields(); + } + }); + } + } + + private void validateExpressionFields() { + ExpressionParser parser = new ExpressionParser(new StringReader("")); + Set variables = new HashSet(); + + if(expressionComposite.getExpressionViewFactor() != null) + for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) { + String textString = text.getText(); + parser.ReInit(new StringReader(textString)); + try { + parser.expr(); + variables.addAll(parser.getReferences()); + text.setBackground(new Color(text.getDisplay(), 255, 255, 255)); + } catch (ParseException e1) { + text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); + return; + } + } + + for(TableItem ti : this.shortcutTabs.getVariableTable().getItems()) { + if(!variables.contains(ti.getText())) { + for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) + text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); + ti.setBackground(new Color(ti.getDisplay(), 255, 230, 230)); + } else { + ti.setBackground(new Color(ti.getDisplay(), 255, 255, 255)); + } + } + } } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java index bb5bc09e..339bd000 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/ExpressionComposite.java @@ -69,8 +69,8 @@ public class ExpressionComposite extends Composite { } public String resetExpressionView(Resource variable) { - data.clear(); - origData.clear(); + this.data.clear(); + this.origData.clear(); this.variable = variable; this.expressionViewFactor = null; String origExpressionType = getOriginalExpressionType().toString(); @@ -166,6 +166,10 @@ public class ExpressionComposite extends Composite { this.expressionViewFactor.replaceSelection(var); } + public IExpressionViewFactor getExpressionViewFactor() { + return this.expressionViewFactor; + } + private ExpressionType getOriginalExpressionType() { ExpressionType et = null; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java index 4d523cd1..0a6810a5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/AuxiliaryExpressionViewFactor.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Arrays; +import java.util.List; import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; @@ -47,13 +49,17 @@ public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor { expression.addFocusListener(new FocusAdapter() { + @Override + public void focusGained(FocusEvent e){ + + } + @Override public void focusLost(FocusEvent e) { lastSelection = expression.getSelection(); } }); - expression.addKeyListener(new ExpressionFieldKeyListener(equation)); } @Override @@ -129,4 +135,9 @@ public class AuxiliaryExpressionViewFactor implements IExpressionViewFactor { if(this.expression != null && this.expression.getText() != null) data.put("equation", this.expression.getText()); } + + @Override + public List getExpressionFields() { + return Arrays.asList(this.expression); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java index a3c24fec..def62261 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ConstantExpressionViewFactor.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Arrays; +import java.util.List; import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; @@ -32,7 +34,6 @@ public class ConstantExpressionViewFactor implements IExpressionViewFactor { final String equation = data.get("equation") != null ? (String)data.get("equation") : ""; - GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); equationLabel = new Label(parent, SWT.NONE); @@ -54,9 +55,6 @@ public class ConstantExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new ExpressionFieldKeyListener(equation)); - - } @Override @@ -133,4 +131,9 @@ public class ConstantExpressionViewFactor implements IExpressionViewFactor { data.put("equation", this.expression.getText()); } + @Override + public List getExpressionFields() { + return Arrays.asList(this.expression); + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java index fbb6392d..75abce65 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/DelayExpressionViewFactor.java @@ -1,8 +1,10 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.List; import java.util.Map; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; import org.simantics.db.Resource; public class DelayExpressionViewFactor implements IExpressionViewFactor { @@ -43,4 +45,10 @@ public class DelayExpressionViewFactor implements IExpressionViewFactor { } + @Override + public List getExpressionFields() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java index a199f75b..e9452789 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java @@ -12,10 +12,13 @@ import org.simantics.sysdyn.expressionParser.ParseException; public class ExpressionFieldKeyListener implements KeyListener { - String originalText; + private String originalText; + private ExpressionParser parser; - public ExpressionFieldKeyListener(String originalText) { - this.originalText = originalText; + public ExpressionFieldKeyListener(ExpressionParser parser, Text text) { + this.originalText = text.getText(); + this.parser = parser; + this.handleKeyReleased(text); } @Override public void keyPressed(KeyEvent e) { @@ -29,17 +32,19 @@ public class ExpressionFieldKeyListener implements KeyListener { public void keyReleased(KeyEvent e) { if(e.widget instanceof Text) { Text text = (Text)e.widget; - - ExpressionParser parser = new ExpressionParser( - new StringReader(text.getText()) - ); - try { - parser.expr(); - text.setBackground(new Color(text.getDisplay(), 255, 255, 255)); - } catch (ParseException e1) { - text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); - } + this.handleKeyReleased(text); + } + } + + private void handleKeyReleased(Text text) { + String textString = text.getText(); + this.parser.ReInit(new StringReader(textString)); + + try { + parser.expr(); + text.setBackground(new Color(text.getDisplay(), 255, 255, 255)); + } catch (ParseException e1) { + text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); } } - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java index 76ca7170..e7d37515 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/IExpressionViewFactor.java @@ -1,10 +1,12 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.List; import java.util.Map; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; import org.simantics.db.Resource; public interface IExpressionViewFactor { @@ -49,4 +51,10 @@ public interface IExpressionViewFactor { * @param data */ void updateData(Map data); + + /** + * @return all individual expression fields for modelica validation + */ + List getExpressionFields(); + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java index 98571aa9..df077d60 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/LookupExpressionViewFactor.java @@ -1,8 +1,10 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.List; import java.util.Map; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; import org.simantics.db.Resource; public class LookupExpressionViewFactor implements IExpressionViewFactor { @@ -43,4 +45,10 @@ public class LookupExpressionViewFactor implements IExpressionViewFactor { } + @Override + public List getExpressionFields() { + // TODO Auto-generated method stub + return null; + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java index aee64437..66a00b3a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ParameterExpressionViewFactor.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Arrays; +import java.util.List; import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; @@ -54,9 +56,6 @@ public class ParameterExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new ExpressionFieldKeyListener(equation)); - - } @Override @@ -133,5 +132,10 @@ public class ParameterExpressionViewFactor implements IExpressionViewFactor { data.put("equation", this.expression.getText()); } + @Override + public List getExpressionFields() { + return Arrays.asList(this.expression); + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java index 3b2e036f..4070348b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/StockExpressionViewFactor.java @@ -1,6 +1,8 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; @@ -24,18 +26,18 @@ import org.simantics.ui.SimanticsUI; public class StockExpressionViewFactor implements IExpressionViewFactor { - Label integralLabel; - Text integral; - Label equationLabel; - Text expression; - Point lastSelection = new Point(0,0); + private Label integralLabel; + private Text integral; + private Label equationLabel; + private Text expression; + private Point lastSelection = new Point(0,0); @Override public void createView(Composite parent, Map data) { final String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : ""; final String integralEquation = data.get("integral") != null ? (String)data.get("integral") : ""; - + GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent); @@ -69,8 +71,6 @@ public class StockExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new ExpressionFieldKeyListener(initialEquation)); - } @Override @@ -198,4 +198,9 @@ public class StockExpressionViewFactor implements IExpressionViewFactor { if(this.integral != null && this.integral.getText() != null) data.put("integral", this.integral.getText()); } + + @Override + public List getExpressionFields() { + return Arrays.asList(this.expression); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java index e0c6212a..e9b9e0ae 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/WithLookupExpressionViewFactor.java @@ -1,5 +1,7 @@ package org.simantics.sysdyn.ui.equation.expressions; +import java.util.Arrays; +import java.util.List; import java.util.Map; import org.eclipse.jface.layout.GridDataFactory; @@ -24,11 +26,10 @@ import org.simantics.ui.SimanticsUI; public class WithLookupExpressionViewFactor implements IExpressionViewFactor { - Label expressionLabel; - Text expression; - Label lookupLabel; - Text lookup; - + private Label expressionLabel; + private Text expression; + private Label lookupLabel; + private Text lookup; private Point lastSelection = new Point(0,0); private Text lastSelectedText = expression; @@ -61,8 +62,6 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { } }); - expression.addKeyListener(new ExpressionFieldKeyListener(equation)); - lookupLabel = new Label(parent, SWT.NONE); lookupLabel.setFont(FONT); lookupLabel.setText("Lookup\ntable"); @@ -83,8 +82,6 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { } }); - lookup.addKeyListener(new ExpressionFieldKeyListener(lookupTable)); - } @Override @@ -171,4 +168,9 @@ public class WithLookupExpressionViewFactor implements IExpressionViewFactor { if(this.lookup != null && this.lookup.getText() != null) data.put("lookup", this.lookup.getText()); } + + @Override + public List getExpressionFields() { + return Arrays.asList(this.expression, this.lookup); + } } \ No newline at end of file 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 285bfafb..b9fd2b45 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/ExpressionParser.jj @@ -6,13 +6,13 @@ options { PARSER_BEGIN(ExpressionParser) package org.simantics.sysdyn.expressionParser; -import java.util.List; -import java.util.ArrayList; +import java.util.Set; +import java.util.HashSet; public class ExpressionParser { - List> references = new ArrayList>(); + Set references = new HashSet(); - public List> getReferences() { + public Set getReferences() { return references; } } @@ -56,6 +56,7 @@ TOKEN: // { add_op term } -> ( add_op() term() )* void expr() : { + references = new HashSet(); } { simple_expression() | "if" expression() "then" expression() ( "elseif" expression() "then" expression() )* @@ -132,9 +133,7 @@ void primary() : { | "false" | "true" | LOOKAHEAD( name() "(" ) name() function_call_args() - | { List reference = new ArrayList(); } - component_reference(reference) - { references.add(reference); } + | component_reference() // | "(" output_expression_list() ")" // | "[" expression_list() { ";" expression_list() } "]" //| "{" function_arguments() "}" @@ -146,11 +145,11 @@ void name() : { ( "." name() )? } -void component_reference(List reference) : { +void component_reference() : { } { //IDENT [ array_subscripts ] [ "." component_reference ] - { reference.add(token.image); } - ( "." component_reference(reference) )? + { references.add(token.image); } + ( "." component_reference() )? } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java index 91a3d247..bed43adb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/expressionParser/TestExpressionParser.java @@ -1,17 +1,16 @@ package org.simantics.sysdyn.expressionParser; import java.io.StringReader; -import java.util.List; public class TestExpressionParser { + static private ExpressionParser parser; + public static void parse(String string) { - ExpressionParser parser = new ExpressionParser( - new StringReader(string) - ); + parser.ReInit(new StringReader(string)); try { parser.expr(); - for(List ref : parser.getReferences()) { + for(String ref : parser.getReferences()) { System.out.println(ref); } } catch (ParseException e) { @@ -22,8 +21,13 @@ public class TestExpressionParser { } public static void main(String[] args) { - parse("1 + m2ma +"); + parser = new ExpressionParser( + new StringReader("") + ); + parse("1 + m2ma"); + System.out.println("##"); parse("ter2e + moro"); - parse("moro * sqr(4.0) + min(3, 2)"); + System.out.println("##"); + parse("moro * sqr(4.0) + min(m2ma, moi)"); } }