From: Jani Mäkinen Date: Wed, 19 Aug 2020 12:39:06 +0000 (+0300) Subject: Cleanup code in EquationTab and fix StockExpression class X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=f59a0f99a09894753b1351863cce1f2f5d7f8664;p=simantics%2Fsysdyn.git Cleanup code in EquationTab and fix StockExpression class gitlab #49 Change-Id: I4bac3bc28865f02951d480087af50964994d6310 --- diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index 8b62d3ae..3ba3a3a6 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -259,9 +259,9 @@ public class EquationTab extends AdjustableTab implements Widget { return; DataContainer isListenerActive = lastActiveInput; - Resource expression = null; + try { - expression = Simantics.getSession().syncRequest(new ResourceRead(variable) { + Simantics.getSession().syncRequest(new ResourceRead(variable) { @Override public Resource perform(ReadGraph graph) throws DatabaseException { @@ -280,83 +280,18 @@ public class EquationTab extends AdjustableTab implements Widget { @Override public void exception(Throwable t) { - LOGGER.error("getActiveExpression failed", t); + LOGGER.error("Error while reading active expression", t); } @Override public boolean isDisposed() { - return !isListenerActive.get(); + return !isListenerActive.get() || composite.isDisposed(); }}); + } catch (DatabaseException e) { LOGGER.error("getActiveExpression failed", e); } - // The variable has no expressions -> creating a new ordered set expressions and the active expression - if(expression == null && variable != null) { - try { - expression = Simantics.getSession().syncRequest(new WriteResultRequest() { - - @Override - public Resource perform(WriteGraph graph) - throws DatabaseException { - if(!graph.hasStatement(variable)) { - /* Trying to create empty experiment for removed variable due to - * async setInput - */ - return null; - } - - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - Resource expressions = ListUtils.create(graph, Collections.emptyList()); - graph.claim(variable, sr.Variable_expressionList, expressions); - final Resource expression = graph.newResource(); - - if(graph.isInstanceOf(variable, sr.Auxiliary) || - graph.isInstanceOf(variable, sr.Valve)) { - graph.claim(expression, l0.InstanceOf, null, sr.NormalExpression); - graph.claimLiteral(expression, sr.Expression_equation, ""); - } - else if(graph.isInstanceOf(variable, sr.Stock)) { - graph.claim(expression, l0.InstanceOf, null, sr.StockExpression); - graph.claimLiteral(expression, sr.StockExpression_initialEquation, ""); - } - ArrayList addition = new ArrayList(1); - addition.add(expression); - ListUtils.insertBack(graph, expressions, addition); - - graph.claim(variable, l0.ConsistsOf, expression); - - VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); - final Session session = graph.getSession(); - session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - VirtualGraph runtime = graph.getService(VirtualGraph.class); - session.asyncRequest(new WriteRequest(runtime) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) - graph.deny(variable, sr.IndependentVariable_activeExpression); - graph.claim(variable, sr.IndependentVariable_activeExpression, expression); - } - } - ); - } - }); - return expression; - } - }); - } catch (DatabaseException e) { - LOGGER.error("getActiveExpression failed", e); - } - - if(expression == null) - return; - } - - // Now the variable should have an expression Simantics.getSession().asyncRequest(new Read>() { /** @@ -398,7 +333,8 @@ public class EquationTab extends AdjustableTab implements Widget { @Override public void execute(AsyncReadGraph graph, final Pair result) { - newExpression.getWidget().getDisplay().asyncExec(new Runnable() { + + site.getShell().getDisplay().asyncExec(new Runnable() { @Override public void run() { @@ -413,18 +349,78 @@ public class EquationTab extends AdjustableTab implements Widget { @Override public void exception(AsyncReadGraph graph, Throwable throwable) { - LOGGER.error("Unexpected failure", throwable); + LOGGER.error("Error while reading sysdyn variable", throwable); } @Override public boolean isDisposed() { - return newExpression.getWidget().isDisposed() || deleteExpression.getWidget().isDisposed(); + return !isListenerActive.get() || newExpression.getWidget().isDisposed() || deleteExpression.getWidget().isDisposed(); } }); - // Set input to widgets using expressionSupport - StructuredSelection ss = new StructuredSelection(new ExpressionWidgetInput(var, expression)); - expressionSupport.fireInput(context, ss); + } + + private Resource ensureExpression(Resource variable) { + // The variable has no expressions -> creating a new ordered set expressions and the active expression + try { + return Simantics.getSession().syncRequest(new WriteResultRequest() { + + @Override + public Resource perform(WriteGraph graph) + throws DatabaseException { + if(!graph.hasStatement(variable)) { + /* Trying to create empty experiment for removed variable due to + * async setInput + */ + return null; + } + + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource expressions = ListUtils.create(graph, Collections.emptyList()); + graph.claim(variable, sr.Variable_expressionList, expressions); + final Resource expression = graph.newResource(); + + if(graph.isInstanceOf(variable, sr.Auxiliary) || + graph.isInstanceOf(variable, sr.Valve)) { + graph.claim(expression, l0.InstanceOf, null, sr.NormalExpression); + graph.claimLiteral(expression, sr.Expression_equation, ""); + } + else if(graph.isInstanceOf(variable, sr.Stock)) { + graph.claim(expression, l0.InstanceOf, null, sr.StockExpression); + graph.claimLiteral(expression, sr.StockExpression_initialEquation, ""); + } + ArrayList addition = new ArrayList(1); + addition.add(expression); + ListUtils.insertBack(graph, expressions, addition); + + graph.claim(variable, l0.ConsistsOf, expression); + + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + final Session session = graph.getSession(); + session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + VirtualGraph runtime = graph.getService(VirtualGraph.class); + session.asyncRequest(new WriteRequest(runtime) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) + graph.deny(variable, sr.IndependentVariable_activeExpression); + graph.claim(variable, sr.IndependentVariable_activeExpression, expression); + } + } + ); + } + }); + return expression; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return null; } /** @@ -749,137 +745,14 @@ public class EquationTab extends AdjustableTab implements Widget { } } - private void updateExpression(Resource expression, Resource variable, ISessionContext context, Variable var) { + private void updateExpression(Resource currentExpression, Resource variable, ISessionContext context, Variable var) { // The variable has no expressions -> creating a new ordered set expressions and the active expression + Resource expression = currentExpression; if(expression == null && variable != null) { - try { - expression = Simantics.getSession().syncRequest(new WriteResultRequest() { - - @Override - public Resource perform(WriteGraph graph) - throws DatabaseException { - if(!graph.hasStatement(variable)) { - /* Trying to create empty experiment for removed variable due to - * async setInput - */ - return null; - } - - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - Resource expressions = ListUtils.create(graph, Collections.emptyList()); - graph.claim(variable, sr.Variable_expressionList, expressions); - final Resource expression = graph.newResource(); - - if(graph.isInstanceOf(variable, sr.Auxiliary) || - graph.isInstanceOf(variable, sr.Valve)) { - graph.claim(expression, l0.InstanceOf, null, sr.NormalExpression); - graph.claimLiteral(expression, sr.Expression_equation, ""); - } - else if(graph.isInstanceOf(variable, sr.Stock)) { - graph.claim(expression, l0.InstanceOf, null, sr.StockExpression); - graph.claimLiteral(expression, sr.StockExpression_initialEquation, ""); - } - ArrayList addition = new ArrayList(1); - addition.add(expression); - ListUtils.insertBack(graph, expressions, addition); - - graph.claim(variable, l0.ConsistsOf, expression); - - VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); - final Session session = graph.getSession(); - session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("expressions")) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - VirtualGraph runtime = graph.getService(VirtualGraph.class); - session.asyncRequest(new WriteRequest(runtime) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) - graph.deny(variable, sr.IndependentVariable_activeExpression); - graph.claim(variable, sr.IndependentVariable_activeExpression, expression); - } - } - ); - } - }); - return expression; - } - }); - } catch (DatabaseException e) { - LOGGER.error("Unexpected failure", e); - } - - if(expression == null) - return; + expression = ensureExpression(variable); } - - // Now the variable should have an expression - Simantics.getSession().asyncRequest(new Read>() { - - /** - * Find out if user can add a new expression or delete the current expression - */ - @Override - public Pair perform(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource expressions = graph.getPossibleObject(variable, sr.Variable_expressionList); - if(expressions == null) { - return new Pair(false, false); - } - List expressionList = ListUtils.toList(graph, expressions); - if(expressionList.isEmpty()) { - return new Pair(false, false); - } - - boolean canAdd = true; - boolean canDelete = false; - // If there are multiple expressions, one can be removed - if(expressionList.size() > 1) - canDelete = true; - String defaultRange = ArrayExpressionCombo.getDefaultRange(graph, variable); - - /* If the variable is an array variable, a range has been set to all expressions and none of - * the ranges is the default range, an expression can be added - */ - for(Resource expression : expressionList) { - String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange); - if(range == null || range.equals("") || range.equals(defaultRange)) { - canAdd = false; - break; - } - } - return new Pair(canAdd, canDelete); - } - }, new AsyncListener>() { - - @Override - public void execute(AsyncReadGraph graph, - final Pair result) { - newExpression.getWidget().getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - if(!newExpression.getWidget().isDisposed()) - newExpression.getWidget().setEnabled(result.first); - if(!deleteExpression.getWidget().isDisposed()) - deleteExpression.getWidget().setEnabled(result.second); - } - }); - - } - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - LOGGER.error("Unexpected failure", throwable); - } - - @Override - public boolean isDisposed() { - return newExpression.getWidget().isDisposed() || deleteExpression.getWidget().isDisposed(); - } - }); + if(expression == null) + return; // Set input to widgets using expressionSupport StructuredSelection ss = new StructuredSelection(new ExpressionWidgetInput(var, expression)); diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java index 4b38a52a..81616660 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionWidget.java @@ -232,7 +232,8 @@ public class ExpressionWidget implements Widget { */ public void validateFields() { if(this.variableTable == null) return; - + if(expr == null) return; + try { // Find the variable for this experession Resource variable = Simantics.getSession().syncRequest(new Read() { diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java index 14f87e96..f64b138d 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java @@ -42,6 +42,7 @@ import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.CommentMetadata; import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.ResourceRead; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; @@ -54,155 +55,177 @@ import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; +import org.simantics.sysdyn.ui.properties.EquationTab; import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.datastructures.Triple; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class StockExpression implements IExpression { - private final ExpressionWidgetInput input; - private ExpressionField integralExpression, initialExpression; - private ExpressionField lastSelectedText; - private TextViewerUndoManager undoManager; - - public StockExpression(ExpressionWidgetInput input) { - this.input = input; - } - - @Override - public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { - GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); - String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : ""; - String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression); - - - Label label = new Label(parent, SWT.NONE); - label.setText("Integral"); - - Composite integralComposite = new Composite(parent, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite); - GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite); - - integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input); - integralExpression.setExpression(integralEquation); - GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression); - integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { - - @Override - public void focusLost(FocusEvent e) { - lastSelectedText = integralExpression; - } - }); - - undoManager = new TextViewerUndoManager(100); - integralExpression.getSourceViewer().setUndoManager(undoManager); - undoManager.connect(integralExpression.getSourceViewer()); - - Button defaultButton = new Button(integralComposite, SWT.PUSH); - defaultButton.setText("Use default"); - defaultButton.addListener(SWT.Selection, new Listener() { - - @Override - public void handleEvent(Event event) { - switch (event.type) { - case SWT.Selection: - StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression)); - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) - throws DatabaseException { - // Delete the possible integral expression from the database to note - // that we are in the "default" mode. - graph.markUndoPoint(); - SysdynResource sr = SysdynResource.getInstance(graph); - graph.deny(input.expression, sr.StockExpression_useCustomIntegral); - graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression)); - - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add("Use default")); - - } - }); - break; - } - } - - }); - - label = new Label(parent, SWT.NONE); - label.setText("Initial\nValue"); - - initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input); - initialExpression.setExpression(initialEquation); - GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression); - initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { - - @Override - public void focusLost(FocusEvent e) { - lastSelectedText = initialExpression; - } - }); - - lastSelectedText = initialExpression; - } - - @Override - public void focus() { - this.lastSelectedText.focus(); - } - - @Override - public List getExpressionFields() { - return Arrays.asList(this.integralExpression, this.initialExpression); - } - - @Override - public void readData(final Resource expression, final Map data) { - Pair equations = null; - if (expression != null && data.get("initialEquation") == null) { - try { - equations = Simantics.getSession().syncRequest(new Read>() { - - @Override - public Pair perform(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - if (graph.isInstanceOf(expression, sr.StockExpression)) { - String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation); - String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation); - initialEquation = (initialEquation != null) ? initialEquation : ""; - return new Pair(integralEquation, initialEquation); - } else { - return new Pair(null, ""); - } - } - - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - data.put("initialEquation", equations.second); - } - - if (equations.first == null) { - try { + private static final Logger LOGGER = LoggerFactory.getLogger(StockExpression.class); + private final ExpressionWidgetInput input; + private ExpressionField integralExpression, initialExpression; + private ExpressionField lastSelectedText; + private TextViewerUndoManager undoManager; + + public StockExpression(ExpressionWidgetInput input) { + this.input = input; + } + + @Override + public void createExpressionFields(Composite parent, Map data, Table allowedVariables) { + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent); + String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : ""; + String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression); + + + Label label = new Label(parent, SWT.NONE); + label.setText("Integral"); + + Composite integralComposite = new Composite(parent, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(integralComposite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(integralComposite); + + integralExpression = new ExpressionField(integralComposite, SWT.BORDER, allowedVariables, true, input); + integralExpression.setExpression(integralEquation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(integralExpression); + integralExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelectedText = integralExpression; + } + }); + + undoManager = new TextViewerUndoManager(100); + integralExpression.getSourceViewer().setUndoManager(undoManager); + undoManager.connect(integralExpression.getSourceViewer()); + + Button defaultButton = new Button(integralComposite, SWT.PUSH); + defaultButton.setText("Use default"); + defaultButton.addListener(SWT.Selection, new Listener() { + + @Override + public void handleEvent(Event event) { + switch (event.type) { + case SWT.Selection: + StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression)); + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) + throws DatabaseException { + // Delete the possible integral expression from the database to note + // that we are in the "default" mode. + graph.markUndoPoint(); + SysdynResource sr = SysdynResource.getInstance(graph); + graph.deny(input.expression, sr.StockExpression_useCustomIntegral); + graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression)); + + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add("Use default")); + + } + }); + break; + } + } + + }); + + label = new Label(parent, SWT.NONE); + label.setText("Initial\nValue"); + + initialExpression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true, input); + initialExpression.setExpression(initialEquation); + GridDataFactory.fillDefaults().grab(true, true).applyTo(initialExpression); + initialExpression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() { + + @Override + public void focusLost(FocusEvent e) { + lastSelectedText = initialExpression; + } + }); + + lastSelectedText = initialExpression; + } + + @Override + public void focus() { + this.lastSelectedText.focus(); + } + + @Override + public List getExpressionFields() { + return Arrays.asList(this.integralExpression, this.initialExpression); + } + + @Override + public void readData(final Resource expression, final Map data) { + if(expression == null) { + LOGGER.warn("StockExpression is null"); + return; + } + if(!expression.equals(data.get("expression"))) { + data.clear(); + } + + Object getInitialEquation = data.get("initialEquation"); + Object integralEquation = data.get("integral"); + Boolean useCustomIntegral = (Boolean) data.get("useCustomIntegral"); + if (getInitialEquation == null || integralEquation == null || useCustomIntegral == null) { + try { + Triple equations = Simantics.getSession().syncRequest(new ResourceRead>(expression) { + + @Override + public Triple perform(ReadGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + boolean useCustomIntegral = graph.hasStatement(resource, sr.StockExpression_useCustomIntegral); + if (graph.isInstanceOf(expression, sr.StockExpression)) { + String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation); + String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation); + initialEquation = (initialEquation != null) ? initialEquation : ""; + return new Triple(integralEquation, initialEquation, useCustomIntegral); + } else { + return new Triple(null, "", useCustomIntegral); + } + } + }); + data.put("integral", equations.first); + data.put("initialEquation", equations.second); + data.put("useCustomIntegral", equations.third); + integralEquation = equations.first; + getInitialEquation = equations.second; + useCustomIntegral = equations.third; + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + } + if (integralEquation == null) { + try { Simantics.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression)); - graph.markUndoPoint(); - CommentMetadata cm = graph.getMetadata(CommentMetadata.class); - graph.addMetadata(cm.add("Set expression")); + graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression)); + graph.markUndoPoint(); + CommentMetadata cm = graph.getMetadata(CommentMetadata.class); + graph.addMetadata(cm.add("Set expression")); } - }); } catch (DatabaseException e) { // TODO Auto-generated catch block e.printStackTrace(); } - - data.put("integral", getDefaultIntegral(expression)); - } else { + + data.put("integral", getDefaultIntegral(expression)); + } else if(!useCustomIntegral) { + data.put("integral", getDefaultIntegral(expression)); + } + /* + try { final String integral = equations.first; Simantics.getSession().syncRequest(new ReadRequest() { @@ -222,172 +245,172 @@ public class StockExpression implements IExpression { data.put("integral", equations.first); e.printStackTrace(); } - } - - } - - @Override - public void replaceSelection(String var) { - if(lastSelectedText != null) { - IDocument doc = lastSelectedText.getDocument(); - try { - Point selection = lastSelectedText.getSelection(); - doc.replace(selection.x, selection.y, var); - lastSelectedText.setSelection(selection.x + var.length()); - } catch (BadLocationException e) { - e.printStackTrace(); - } - } - } - - @Override - public void save(final Resource expression, final Map data) { - final String currentInitial = this.initialExpression.getExpression(); - final String currentIntegral = this.integralExpression.getExpression(); - if (currentIntegral == null) { - this.integralExpression.setExpression(getDefaultIntegral(expression)); - } - if(!currentInitial.equals(data.get("initialEquation")) - || currentIntegral == null - || !currentIntegral.equals(data.get("integral"))) { - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph g) - throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(g); - Layer0 l0 = Layer0.getInstance(g); - - if(!g.isInstanceOf(expression, sr.StockExpression)) { - Resource variable = g.getPossibleObject(expression, l0.PartOf); - Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList); - Resource temp = g.newResource(); - ListUtils.replace(g, expressionList, expression, temp); - - for(Resource predicate : g.getPredicates(expression)) { - g.deny(expression, predicate); - } - g.claim(expression, l0.InstanceOf, null, sr.StockExpression); - - ListUtils.replace(g, expressionList, temp, expression); - - RemoverUtil.remove(g, temp); - - g.claim(expression, l0.PartOf, variable); - } - g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial); - - if (!currentIntegral.equals(g.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation, Bindings.STRING))) { - // If the value is not same as default, enable the custom tag - g.claim(expression, sr.StockExpression_useCustomIntegral, expression); - } - g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral); - - updateData(data); - - g.markUndoPoint(); - - Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial); - } - - }); - } - this.initialExpression.setExpression(currentInitial); - this.integralExpression.setExpression(currentIntegral); - } - - @Override - public void updateData(Map data) { - if(this.initialExpression != null && this.initialExpression.getExpression() != null) - data.put("initialEquation", this.initialExpression.getExpression()); - if(this.integralExpression != null && this.integralExpression.getExpression() != null) - data.put("integral", this.integralExpression.getExpression()); - } - - - private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - - // find the variable - Resource variable = graph.getPossibleObject(expression, l0.PartOf); - if(variable == null) - return ""; - + */ + data.put("expression", expression); + } + + @Override + public void replaceSelection(String var) { + if(lastSelectedText != null) { + IDocument doc = lastSelectedText.getDocument(); + try { + Point selection = lastSelectedText.getSelection(); + doc.replace(selection.x, selection.y, var); + lastSelectedText.setSelection(selection.x + var.length()); + } catch (BadLocationException e) { + e.printStackTrace(); + } + } + } + + @Override + public void save(final Resource expression, final Map data) { + final String currentInitial = this.initialExpression.getExpression(); + final String currentIntegral = this.integralExpression.getExpression(); + if (currentIntegral == null) { + this.integralExpression.setExpression(getDefaultIntegral(expression)); + } + if(!currentInitial.equals(data.get("initialEquation")) + || currentIntegral == null + || !currentIntegral.equals(data.get("integral"))) { + Simantics.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) + throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + Layer0 l0 = Layer0.getInstance(g); + + if(!g.isInstanceOf(expression, sr.StockExpression)) { + Resource variable = g.getPossibleObject(expression, l0.PartOf); + Resource expressionList = g.getPossibleObject(variable, sr.Variable_expressionList); + Resource temp = g.newResource(); + ListUtils.replace(g, expressionList, expression, temp); + + for(Resource predicate : g.getPredicates(expression)) { + g.deny(expression, predicate); + } + g.claim(expression, l0.InstanceOf, null, sr.StockExpression); + + ListUtils.replace(g, expressionList, temp, expression); + + RemoverUtil.remove(g, temp); + + g.claim(expression, l0.PartOf, variable); + } + g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial); + + if (!currentIntegral.equals(g.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation, Bindings.STRING))) { + // If the value is not same as default, enable the custom tag + g.claim(expression, sr.StockExpression_useCustomIntegral, expression); + } + g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral); + + updateData(data); + + g.markUndoPoint(); + + Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial); + } + + }); + } + this.initialExpression.setExpression(currentInitial); + this.integralExpression.setExpression(currentIntegral); + } + + @Override + public void updateData(Map data) { + if(this.initialExpression != null && this.initialExpression.getExpression() != null) + data.put("initialEquation", this.initialExpression.getExpression()); + if(this.integralExpression != null && this.integralExpression.getExpression() != null) + data.put("integral", this.integralExpression.getExpression()); + } + + + private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + // find the variable + Resource variable = graph.getPossibleObject(expression, l0.PartOf); + if(variable == null) + return ""; + SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession()); SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf)); model.update(graph); - + Stock stock = (Stock)model.getElement(variable); - + String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange); if(range == null) range = ""; - - StringBuilder builder = new StringBuilder(); - builder.append(""); - for(Valve in : stock.getIncomingValves()) { - builder.append(" + " + in.getName() + range); - } - for(Valve out : stock.getOutgoingValves()) { - builder.append(" - " + out.getName() + range); - } - if (builder.indexOf(" + ") == 0) - builder.delete(0, 3); - - return builder.toString().trim(); - } - - - private static String getDefaultIntegral(final Resource expression) { - - String integral = ""; - if(expression == null) - return integral; - try { - integral = Simantics.getSession().syncRequest(new Read() { - - @Override - public String perform(ReadGraph graph) throws DatabaseException { - return getDefaultIntegral(graph, expression); - } - - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - return integral; - } - - @Override - public void addKeyListener(KeyListener listener) { - this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener); - this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener); - - } - - @Override - public void addModifyListener(ModifyListener listener) { - this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener); - this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener); - - } - - @Override - public void addFocusListener(FocusListener listener) { - this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener); - this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener); - } - + + StringBuilder builder = new StringBuilder(); + builder.append(""); + for(Valve in : stock.getIncomingValves()) { + builder.append(" + " + in.getName() + range); + } + for(Valve out : stock.getOutgoingValves()) { + builder.append(" - " + out.getName() + range); + } + if (builder.indexOf(" + ") == 0) + builder.delete(0, 3); + + return builder.toString().trim(); + } + + + private static String getDefaultIntegral(final Resource expression) { + + String integral = ""; + if(expression == null) + return integral; + try { + integral = Simantics.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + return getDefaultIntegral(graph, expression); + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + return integral; + } + + @Override + public void addKeyListener(KeyListener listener) { + this.initialExpression.getSourceViewer().getTextWidget().addKeyListener(listener); + this.integralExpression.getSourceViewer().getTextWidget().addKeyListener(listener); + + } + + @Override + public void addModifyListener(ModifyListener listener) { + this.initialExpression.getSourceViewer().getTextWidget().addModifyListener(listener); + this.integralExpression.getSourceViewer().getTextWidget().addModifyListener(listener); + + } + + @Override + public void addFocusListener(FocusListener listener) { + this.initialExpression.getSourceViewer().getTextWidget().addFocusListener(listener); + this.integralExpression.getSourceViewer().getTextWidget().addFocusListener(listener); + } + @Override public void addVerifyKeyListener(VerifyKeyListener listener) { this.initialExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); } - @Override - public IUndoManager getUndoManager() { - return undoManager; - } + @Override + public IUndoManager getUndoManager() { + return undoManager; + } }