import org.simantics.db.common.request.WriteRequest;\r
import org.simantics.db.common.utils.ListUtils;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
import org.simantics.db.procedure.Listener;\r
import org.simantics.db.request.Read;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.tableParser.TableParser;\r
import org.simantics.sysdyn.tableParser.Token;\r
import org.simantics.sysdyn.ui.utils.SyntaxError;\r
+import org.simantics.sysdyn.utils.SheetUtils;\r
import org.simantics.ui.SimanticsUI;\r
\r
public class WithLookupExpression implements IExpression {\r
\r
@Override\r
public void run() {\r
- lookup.setExpression(result); \r
+ lookup.setExpression(result); \r
+ updateChart();\r
}\r
});\r
- updateChart();\r
}\r
\r
@Override\r
\r
if(oldExpression == null || oldLookupTable == null ||\r
(currentExpression != null && currentLookupTable != null\r
- && (!currentExpression.equals(oldExpression) || \r
- !currentLookupTable.equals(oldLookupTable)))) {\r
+ && (!currentExpression.equals(oldExpression) || \r
+ !currentLookupTable.equals(oldLookupTable)))) {\r
data.putAll(data);\r
data.put("equation", currentExpression);\r
data.put("lookup", currentLookupTable);\r
SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
@Override\r
public void perform(WriteGraph g)\r
- throws DatabaseException {\r
+ throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(g);\r
Layer0 l0 = Layer0.getInstance(g);\r
- \r
+\r
if(!g.isInstanceOf(expr, sr.WithLookupExpression)) {\r
- \r
- \r
- final Resource newExpression = GraphUtils.create2(g, sr.WithLookupExpression,\r
- sr.WithLookupExpression_minX, 0.0,\r
- sr.WithLookupExpression_maxX, 10.0,\r
- sr.WithLookupExpression_minY, 0.0,\r
- sr.WithLookupExpression_maxY, 10.0);\r
- String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);\r
- if(arrayRange != null)\r
- g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);\r
- \r
+\r
+\r
+ final Resource newExpression = GraphUtils.create2(g, sr.WithLookupExpression,\r
+ sr.WithLookupExpression_minX, 0.0,\r
+ sr.WithLookupExpression_maxX, 10.0,\r
+ sr.WithLookupExpression_minY, 0.0,\r
+ sr.WithLookupExpression_maxY, 10.0);\r
+ String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);\r
+ if(arrayRange != null)\r
+ g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);\r
+\r
final Resource variable = g.getSingleObject(expression, l0.PartOf);\r
- Resource expressions = g.getPossibleObject(variable, sr.Variable_expressionList);\r
- Resource node = ListUtils.getNode(g, expressions, expression);\r
- g.deny(node, l0.List_Element);\r
- g.claim(node, l0.List_Element, newExpression);\r
- \r
+ Resource expressions = g.getPossibleObject(variable, sr.Variable_expressionList);\r
+ Resource node = ListUtils.getNode(g, expressions, expression);\r
+ g.deny(node, l0.List_Element);\r
+ g.claim(node, l0.List_Element, newExpression);\r
+\r
g.deny(expression, l0.PartOf);\r
g.claim(newExpression, l0.PartOf, variable);\r
- \r
- VirtualGraph runtime = g.getService(VirtualGraph.class);\r
- g.syncRequest(new WriteRequest(runtime) {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
- graph.deny(variable, sr.IndependentVariable_activeExpression);\r
- graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);\r
- }\r
- }\r
- );\r
- expr = newExpression;\r
- \r
+\r
+ VirtualGraph runtime = g.getService(VirtualGraph.class);\r
+ g.syncRequest(new WriteRequest(runtime) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))\r
+ graph.deny(variable, sr.IndependentVariable_activeExpression);\r
+ graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);\r
+ }\r
+ }\r
+ );\r
+ expr = newExpression;\r
+\r
}\r
g.claimLiteral(expr, sr.WithLookupExpression_expression, currentExpression);\r
g.claimLiteral(expr, sr.WithLookupExpression_lookup, currentLookupTable);\r
this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);\r
this.lookup.getSourceViewer().getTextWidget().addKeyListener(listener);\r
}\r
- \r
- @Override\r
- public void addVerifyKeyListener(VerifyKeyListener listener) {\r
- this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
- this.lookup.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
- }\r
+\r
+ @Override\r
+ public void addVerifyKeyListener(VerifyKeyListener listener) {\r
+ this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
+ this.lookup.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
+ }\r
\r
@Override\r
public void addModifyListener(ModifyListener listener) {\r
true,\r
true,\r
false\r
- );\r
+ );\r
chart.removeLegend();\r
chart.getXYPlot().getDomainAxis().setTickLabelsVisible(true);\r
chart.getXYPlot().getDomainAxis().setAxisLineVisible(false);\r
Double.parseDouble(yTokens.get(i).image)));\r
}\r
} catch (ParseException e1) {\r
- this.lookup.setSyntaxError(new SyntaxError(e1.currentToken, "Syntax Error"));\r
- System.out.println("MESSAGE: " + e1.getMessage());\r
- return;\r
+ if(lookup.getExpression().matches("[a-zA-Z0-9]*\\([a-zA-Z0-9:]*\\)")) {\r
+ // Might be a sheet reference\r
+ try {\r
+ final String name = lookup.getExpression().substring(0, lookup.getExpression().indexOf("("));\r
+ final String range = lookup.getExpression().substring(lookup.getExpression().indexOf("(") + 1, lookup.getExpression().indexOf(")"));\r
+ String possibleTable = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph) throws DatabaseException {\r
+ Variable variable = input.variable;\r
+ Resource model = Variables.getModel(graph, variable);\r
+ return SheetUtils.getStringRepresentation(graph, model, name, range);\r
+ }\r
+\r
+ });\r
+ \r
+\r
+ try {\r
+ if(possibleTable == null) {\r
+ ParseException e = new ParseException(e1.getMessage());\r
+ e.currentToken = e1.currentToken;\r
+ throw e;\r
+ }\r
+ \r
+ parser.ReInit(new StringReader(possibleTable));\r
+ parser.table();\r
+ ArrayList<Token> xTokens = parser.getXTokens();\r
+ ArrayList<Token> yTokens = parser.getYTokens();\r
+ for(int i = 0; i < xTokens.size(); i++) {\r
+ dataPoints.add(new Point2D.Double(\r
+ Double.parseDouble(xTokens.get(i).image), \r
+ Double.parseDouble(yTokens.get(i).image)));\r
+ }\r
+ } catch (ParseException e2) {\r
+ this.lookup.setSyntaxError(new SyntaxError(e2.currentToken, "Syntax Error"));\r
+ System.out.println("MESSAGE: " + e2.getMessage());\r
+ return;\r
+ }\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
}\r
\r
XYSeries series = new XYSeries("Lookup Table");\r