From: lempinen Date: Thu, 1 Dec 2011 10:33:09 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23384 ac1ea38d-2e2b... X-Git-Tag: simantics-1.6~83 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=08b1e460af4d8b8d95bff57c592ef314c1c25db2;p=simantics%2Fsysdyn.git git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23384 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index dd36a3ee..613fa022 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index c19156a4..8e329777 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -208,6 +208,9 @@ SYSDYN.RefersTo -- SYSDYN.DelayExpression.expression --> L0.String -- SYSDYN.DelayExpression.order --> L0.Integer -- SYSDYN.DelayExpression.delayTime --> L0.String -- SYSDYN.DelayExpression.initialValue --> L0.String -- SYSDYN.DelayExpression.initialValue --> L0.String creating a new ordered set expressions and the active expression + // The variable has no expressions -> creating a new ordered set expressions and the active expression if(expression == null && variable != null) { try { expression = SimanticsUI.getSession().syncRequest(new WriteResultRequest() { @@ -226,9 +220,12 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } } - + // Now the variable should have an expression SimanticsUI.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); @@ -243,10 +240,14 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { 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.HasArrayRange); if(range == null || range.equals("") || range.equals(defaultRange)) { @@ -284,13 +285,20 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { return newExpression.getWidget().isDisposed() || deleteExpression.getWidget().isDisposed(); } }); - + + // Set input to widgets using expressionSupport StructuredSelection ss = new StructuredSelection(expression); expressionSupport.fireInput(context, ss); } + /** + * Adds listeners to widgets in this tab + * + * @param context ISessionContext + */ private void addListeners(ISessionContext context) { + // Validate expression fields when a dependency has been added or removed shortcutTabWidget.addDependencyListener(new Runnable() { @Override @@ -299,6 +307,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Deletes a selected expression deleteExpression.addSelectionListener(new SelectionListenerImpl(context) { @Override @@ -308,6 +317,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); + // Currently active expressin should be located in (Model sr.HasActiveExpression expression) Resource activeExpression = graph.getPossibleObject(input, sr.HasActiveExpression); if(activeExpression == null) return; @@ -316,15 +326,19 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { if(OrderedSetUtils.toList(graph, expressionList).size() <= 1) return; + // Get the previous expression in expression list to be activated Resource prev = OrderedSetUtils.prev(graph, expressionList, activeExpression); OrderedSetUtils.remove(graph, expressionList, activeExpression); graph.deny(input, l0.ConsistsOf, activeExpression); if(prev.equals(expressionList)) { + // If the removed expression was last on the list, prev returns the list. + // Get a proper list element to be activated Iterator iterator = OrderedSetUtils.iterator(graph, expressionList); prev = iterator.next(); } + // Set prev as active in virtual graph final Resource newActive = prev; VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); final Session session = graph.getSession(); @@ -347,6 +361,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Creates a new expression newExpression.addSelectionListener(new SelectionListenerImpl(context) { @Override @@ -359,14 +374,17 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { if(expressions == null) { return; } + // Get the currently active expression Resource activeExpression = graph.getPossibleObject(input, sr.HasActiveExpression); Resource newExpression = graph.newResource(); if(activeExpression != null) { + // Create a new expression based on the old expression graph.claim(newExpression, l0.InstanceOf, graph.getSingleObject(activeExpression, l0.InstanceOf)); if(graph.isInstanceOf(newExpression, sr.StockExpression)) { graph.claimLiteral(newExpression, sr.HasInitialEquation, ""); } } else { + // If there was no active expression, create a normal expression graph.claim(newExpression, l0.InstanceOf, sr.NormalExpression); } OrderedSetUtils.add(graph, expressions, newExpression); @@ -374,6 +392,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Item factory for expression type combo expressionTypeCombo.setItemFactory(new ReadFactoryImpl>() { @Override @@ -382,6 +401,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { Map map = new HashMap(); SysdynResource sr = SysdynResource.getInstance(graph); + // Select expression types based on the type of the variable final ExpressionType[] expressionTypes; if(graph.isInstanceOf(input, sr.Auxiliary)) expressionTypes = ExpressionTypes.auxiliaryExpressions; @@ -399,6 +419,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Initial selection to the combo from active expression expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl() { @Override @@ -410,6 +431,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Modify listener for selecting expression type expressionTypeCombo.addModifyListener(new TextModifyListener() { @Override @@ -420,6 +442,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { }); + // Add all units used in the model to the unit combo unitCombo.setItemFactory(new ReadFactoryImpl>() { @Override @@ -442,6 +465,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { return map; } }); + + // Set initial selection of unit combo unitCombo.setSelectionFactory(new ReadFactoryImpl() { @Override @@ -454,6 +479,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Modify unit unitCombo.addModifyListener(new ComboModifyListenerImpl() { @Override @@ -463,6 +489,11 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { }); + /* + * Double-clicking something in shortcut tab widget + * writes the clicked element into expression widget, + * sets focus on expression widget and validates its fields + */ shortcutTabWidget.addMouseListener(new MouseListener(){ @Override @@ -476,8 +507,8 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { @Override public void run() { if(expressionWidget!= null) { + expressionWidget.getExpression().focus(); expressionWidget.getExpression().replaceSelection(var); - expressionWidget.getExpression().focus(); expressionWidget.validateFieldsTimed(); } } @@ -495,6 +526,10 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + + /* + * Shortcut tab widget should not maintain any selections for visual reasons + */ shortcutTabWidget.addFocusListener(new FocusListener() { @Override @@ -508,6 +543,9 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { }); + /* Modifying an expression sets a timed validation. The timer is + * reset after each modification + */ expressionWidget.addModifyListener(new ModifyListener() { @Override @@ -516,6 +554,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Pressing return without shift key triggers saving the expression expressionWidget.addVerifyKeyListener(new VerifyKeyListener() { @Override @@ -530,6 +569,7 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { } }); + // Triggers save when equation tab loses focus if(focusLostListener == null) { focusLostListener = new org.eclipse.ui.IPartListener2() { @@ -565,6 +605,15 @@ public class EquationTab extends LabelPropertyTabContributor implements Widget { super.dispose(); } + /** + * Get the currently active expression of the first expression in expression list if + * no expression has been set to active + * + * @param graph ReadGraph + * @param variable Variable + * @return active expression or the first expression in variables expression list + * @throws DatabaseException + */ private Resource getActiveExpression(ReadGraph graph, Resource variable) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); Resource expression = graph.getPossibleObject(variable, sr.HasActiveExpression); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java index 18b2e8cd..8494a8c9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java @@ -144,12 +144,12 @@ public class DelayExpression implements IExpression { @Override public void focus() { - equation.setFocus(); + lastSelectedText.setFocus(); } @Override public List getExpressionFields() { - return Arrays.asList(equation, delayTime); + return Arrays.asList(equation, delayTime, initialValue); } @Override 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 38f0c16d..2904c0d0 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 @@ -29,6 +29,8 @@ import org.eclipse.jface.text.source.MatchingCharacterPainter; import org.eclipse.jface.text.source.SourceViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.graphics.Color; @@ -39,6 +41,14 @@ import org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess; import org.eclipse.swt.graphics.Point; import org.simantics.sysdyn.expressionParser.Token; +/** + * Field for displaying a part of an expression. Expression field uses SourceViewer + * to display annotations and other visual elements just like any other + * source viewer in eclipse. + * + * @author Teemu Lempinen + * + */ public class ExpressionField extends Composite { protected SourceViewer _sourceViewer; @@ -55,6 +65,11 @@ public class ExpressionField extends Composite { IAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess(); + /** + * Create a new expression field + * @param parent + * @param style + */ public ExpressionField(Composite parent, int style) { super(parent, style); @@ -77,11 +92,13 @@ public class ExpressionField extends Composite { null, true, styles); + + // Configuration for color management _sourceViewer.configure(new ExpressionFieldConfiguration(cManager)); - AnnotationPainter painter = new AnnotationPainter(_sourceViewer, annotationAccess); _sourceViewer.addPainter(painter); + // Annotation types painter.addAnnotationType(MISSING_LINK); painter.setAnnotationTypeColor(MISSING_LINK, new Color(this.getDisplay(), 255,215,0)); painter.addAnnotationType(NO_SUCH_VARIABLE); @@ -92,11 +109,8 @@ public class ExpressionField extends Composite { _sourceViewer.setDocument(_document, _annotationModel); GridDataFactory.fillDefaults().grab(true, true).applyTo(_sourceViewer.getControl()); -// _sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL, -// SWT.FILL, -// true, -// true)); + // Parenthesis matching PaintManager paintManager = new PaintManager(_sourceViewer); MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(_sourceViewer, new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} )); @@ -104,6 +118,7 @@ public class ExpressionField extends Composite { paintManager.addPainter(matchingCharacterPainter); + // Listener for canceling editing. ESC -> revert back to original text _sourceViewer.getTextWidget().addKeyListener(new KeyListener() { @Override @@ -119,12 +134,39 @@ public class ExpressionField extends Composite { } }); + /* Focus listener saving and restoring selections + * When focus is lost, current selection is saved, but the selection is removed. + * When focus is gained back, the selection is restored + */ + _sourceViewer.getTextWidget().addFocusListener(new FocusListener() { + + Point selection = null; + @Override + public void focusLost(FocusEvent e) { + selection = ((StyledText)e.widget).getSelection(); + ((StyledText)e.widget).setSelection(0); + } + + @Override + public void focusGained(FocusEvent e) { + if(selection != null) + ((StyledText)e.widget).setSelection(selection); + } + }); } + /** + * Returns the {@link SourceViewer} of this ExpressionField + * @return Returns the {@link SourceViewer} of this ExpressionField + */ public SourceViewer getSourceViewer() { return this._sourceViewer; } + /** + * Sets missing link annotations to given positions + * @param positions Positions for missing link annotations + */ public void setMissingLinkAnnotations(List positions){ for(Position p : positions) { Annotation annotation = new Annotation(false); @@ -134,6 +176,10 @@ public class ExpressionField extends Composite { } } + /** + * Sets no such variable annotations to given positions + * @param positions Positions for no such variable annotations + */ public void setNoSuchVariableAnnotations(List positions){ for(Position p : positions) { Annotation annotation = new Annotation(false); @@ -143,14 +189,33 @@ public class ExpressionField extends Composite { } } + /** + * Sets syntax error for the given token + * @param token Token with syntax error + * @param message Message to be displayed in tool tips + */ public void setSyntaxError(Token token, String message){ setSyntaxError(token.image, message, token.beginLine, token.beginColumn, token.endLine, token.endColumn); } + /** + * Sets syntax error for the given token + * @param token Token with syntax error + * @param message Message to be displayed in tool tips + */ public void setSyntaxError(org.simantics.sysdyn.tableParser.Token token, String message){ setSyntaxError(token.image, message, token.beginLine, token.beginColumn, token.endLine, token.endColumn); } + /** + * Sets syntax error to given location + * @param image Token image + * @param message Message to be displayed in tool tips + * @param beginLine Begin line + * @param beginColumn Begin column + * @param endLine End line + * @param endColumn End column + */ public void setSyntaxError(String image, String message, int beginLine, int beginColumn, int endLine, int endColumn) { int start = 0; int offset = this._document.getLength(); @@ -165,6 +230,13 @@ public class ExpressionField extends Composite { setSyntaxError(start, offset, SYNTAX_ERROR, message == null ? "Syntax Error" : message); } + /** + * Sets syntax error to given start and offset + * @param start Start location + * @param offset Offset + * @param type Error type (SYNTAX_ERROR, MISSING_LINK, NO_SUCH_VARIABLE) + * @param text Message to be displayedin tool tips + */ public void setSyntaxError(int start, int offset, String type, String text) { Annotation annotation = new Annotation(false); annotation.setType(type); @@ -173,22 +245,42 @@ public class ExpressionField extends Composite { _annotationModel.addAnnotation(annotation, p); } + /** + * Resets all annotations + */ public void resetAnnotations() { _annotationModel.removeAllAnnotations(); } + + /** + * Sets an expression to this expression field + * @param expression + */ public void setExpression(String expression) { _document.set(expression); this.oldExpression = expression; } + /** + * Returns the expression of this expression field + * @return + */ public String getExpression() { return this._document.get(); } + /** + * Returns the current selection + * @return current selection + */ public Point getSelection() { return _sourceViewer.getSelectedRange(); } + /** + * Set selection for this expression field. The length of the selection is 0 + * @param selection Selection location + */ public void setSelection(int selection) { this._sourceViewer.setSelectedRange(selection, 0); } @@ -197,6 +289,9 @@ public class ExpressionField extends Composite { return _document; } + /** + * Focus to this expression field + */ public void focus() { this._sourceViewer.getTextWidget().forceFocus(); } 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 bb0a96ac..6abe38f2 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 @@ -257,7 +257,7 @@ public class ExpressionUtils { TableItem[] connectedVariables = variableTable.getItems(); for(TableItem ti : connectedVariables) { if(!variables.contains(ti.getText())) { - ti.setForeground(new Color(ti.getDisplay(), 255, 0, 0)); + ti.setForeground(new Color(ti.getDisplay(), 255,125,0)); } else { ti.setForeground(new Color(ti.getDisplay(), 0, 0, 0)); variables.remove(ti.getText()); @@ -381,11 +381,6 @@ public class ExpressionUtils { } - - - - - for(ExpressionField ef : ranges.keySet()) { HashMap tokens = errors.get(ef); if(tokens == null || tokens.isEmpty()) continue; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java index 8ad6013b..8a550544 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/DependencyFunction.java @@ -63,15 +63,15 @@ public class DependencyFunction { Set dependencies = ValidationUtils.getDependencies(graph, component); Set references = null; + ArrayList result = new ArrayList(); + + // Find all references in equations of component try { references = ValidationUtils.getReferences(graph, component); - } catch (SyntaxErrorException e) { - } catch (UnsupportedCharactersException e) { - } catch (UndefinedExpressionException e) { - } - - ArrayList result = new ArrayList(); + } catch (Exception e) { + return result; + } // Check that all arrow dependencies are used in equations if (dependencies != null) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java index 11eb5f04..5ad0123a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java @@ -19,6 +19,7 @@ import java.util.List; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Statement; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; @@ -34,7 +35,7 @@ import org.simantics.sysdyn.expressionParser.TokenMgrError; * */ public class ValidationUtils { - + /** * Find all variables that are referred to in the expressions of variable r * @param graph ReadGraph @@ -50,19 +51,25 @@ public class ValidationUtils { ExpressionParser parser = new ExpressionParser(new StringReader("")); SysdynResource sr = SysdynResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); List expressionList = getExpressions(graph, r); if(expressionList == null || expressionList.isEmpty()) throw new UndefinedExpressionException(); for(Resource expression : expressionList) { - Collection equations = graph.syncRequest(new ObjectsWithType(expression, sr.HasEquation, l0.String)); - if(equations.isEmpty()) + Collection statements = graph.getStatements(expression, sr.HasEquation); + if(statements.isEmpty()) throw new UndefinedExpressionException(); - - for(Resource s : equations) { - String value = graph.getValue(s, Bindings.STRING); - if(value.length() == 0) - throw new UndefinedExpressionException(); + + for(Statement statement : statements) { + Object v = graph.getValue(statement.getObject()); + String value = v.toString(); + + if(value.length() == 0) { + // Empty might be allowed + if(graph.isSubrelationOf(statement.getPredicate(), sr.HasEquationOrEmpty)) + return references; + else + throw new UndefinedExpressionException(); + } parser.ReInit(new StringReader(value)); try { @@ -77,7 +84,7 @@ public class ValidationUtils { } return references; } - + /** * Get all expressions of a variable r * @param graph ReadGraph @@ -94,7 +101,7 @@ public class ValidationUtils { return null; } - + /** * Returns the names of the related variables (dependencies) * @@ -124,7 +131,7 @@ public class ValidationUtils { } return variables; } - + /** * Is reference reachable from variable * @@ -140,7 +147,7 @@ public class ValidationUtils { else return false; } - + /** * Find a resource starting from variable and using reference path * @@ -162,5 +169,5 @@ public class ValidationUtils { } return null; } - + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java index 1ef05738..e3d02a2c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java @@ -77,6 +77,8 @@ public class FunctionUtils { SysdynResource sr = SysdynResource.getInstance(graph); SimulationResource simu = SimulationResource.getInstance(graph); Resource modelResource = graph.getPossibleObject(model.getConfigurationResource(), simu.IsConfigurationOf); + if(modelResource == null) + return parents; parents.add(modelResource); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java index 6aea3fc5..852ea14c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java @@ -146,7 +146,7 @@ public class DelayExpression extends Expression { sb.append("\tReal" + range + " initialValue = " + (range.length() > 0 ? "fill(-137543," + range.substring(1, range.length() - 1) + ")" : -137543) + ";\n"); else // Initial value - sb.append("\tReal" + range + " initialValue = " + initialValue + ";\n"); + sb.append("\tReal" + range + " initialValue = " + FormatUtils.formatExpressionForModelica(variable, this.initialValue) + ";\n"); // First valve sb.append("\tReal" + range + " delay0;\n"); @@ -160,13 +160,11 @@ public class DelayExpression extends Expression { sb.append("initial equation\n"); // "Generic" structure selection. If the "random" number is not used in initial value, use delay0 as initial value - sb.append("\tLV" + n +" = DL * " + - (range.length() > 0 ? "(if max(initialValue) < -137543 or max(initialValue) > -137543 then initialValue else delay0)" : - "(if initialValue < -137543 or initialValue > -137543 then initialValue else delay0)") + ";\n"); - // Each stock gets the same initial value - for(int i = 1; i < n; i++) - sb.append("\tLV" + i + " = LV" + n + ";\n"); + for(int i = 1; i <= n; i++) + sb.append("\tLV" + i +" = DL * " + + (range.length() > 0 ? "(if max(initialValue) < -137543 or max(initialValue) > -137543 then initialValue else delay0)" : + "(if initialValue < -137543 or initialValue > -137543 then initialValue else delay0)") + ";\n"); sb.append("equation\n"); sb.append("\tDL = delayTime/" + n + ";\n"); @@ -184,6 +182,7 @@ public class DelayExpression extends Expression { @Override public String getEquation(IndependentVariable variable) { String equation = FormatUtils.formatExpressionForModelica(variable, this.equation); + String delayTime = FormatUtils.formatExpressionForModelica(variable, this.delayTime); // Set delay properties StringBuilder sb = new StringBuilder();