1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.representation;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Iterator;
\r
17 import org.simantics.objmap.annotations.RelatedElements;
\r
18 import org.simantics.sysdyn.SysdynResource;
\r
19 import org.simantics.sysdyn.representation.expressions.IExpression;
\r
20 import org.simantics.sysdyn.representation.utils.FormatUtils;
\r
23 * Representation of an independent variable
\r
25 * @author Teemu Lempinen
\r
28 public abstract class IndependentVariable extends Variable {
\r
31 value = SysdynResource.URIs.Variable_isHeadOf,
\r
33 private ArrayList<IElement> isHeadOf = new ArrayList<IElement>();
\r
36 * Get the declaration of this variable
\r
37 * @return Declaration of this variable
\r
39 public String getDeclaration() {
\r
40 Variability variability = Variability.getVariability(this);
\r
42 // [variability] type name[range]
\r
43 StringBuilder sb = new StringBuilder();
\r
45 sb.append(variability.getText().isEmpty() ? "" : variability.getText() + " ");
\r
46 sb.append(getType() + " ");
\r
47 sb.append(getName());
\r
48 sb.append(getRange());
\r
51 if(variability == Variability.PARAMETER || variability == Variability.CONSTANT) {
\r
52 // parameters and constants are guaranteed to have only one expression
\r
53 String equation = FormatUtils.formatExpressionForModelica(this, getExpressions().get(0).getExpression());
\r
54 sb.append(" = " + equation);
\r
60 // Possible additions to expressions. e.g. helper classes and variables
\r
61 if(getExpressions() != null) {
\r
63 for(IExpression e : getExpressions()) {
\r
64 addition = e.getDeclarationAddition();
\r
65 if(addition != null)
\r
66 sb.append(addition);
\r
70 return sb.toString();
\r
74 * Get the range of this variable, if it is an array variable.
\r
76 * Format: [EnumerationName.size (, Enumeration2Name.size)*]
\r
77 * @return Range of this variable, if it is an array variable. Empty string otherwise.
\r
79 public String getRange() {
\r
80 ArrayList<Enumeration> enumerations = getArrayIndexes();
\r
82 if(enumerations != null && enumerations.size() > 0) {
\r
83 StringBuilder sb = new StringBuilder();
\r
85 Iterator<Enumeration> iterator = enumerations.iterator();
\r
86 while(iterator.hasNext()) {
\r
87 sb.append(iterator.next().getName() + ".size");
\r
88 if(iterator.hasNext()) {
\r
93 range = sb.toString();
\r
99 * Get all initial equations or null, if there are no initial equations.
\r
100 * @return Initial equations as string or null.
\r
102 public String getInitialEquation() {
\r
103 StringBuilder sb = new StringBuilder();
\r
105 for(IExpression expression : getExpressions()) {
\r
106 String initialEquation = expression.getInitialEquation();
\r
107 if(initialEquation != null)
\r
108 sb.append(initialEquation);
\r
110 String result = sb.toString();
\r
111 return result.length() > 0 ? result : null;
\r
115 * Get the equation of this variable for equation block. Null is returned if this variable is not continuous.
\r
116 * @return Equation or null
\r
118 public String getEquation() {
\r
119 Variability variability = Variability.getVariability(this);
\r
120 if(variability == Variability.CONTINUOUS) {
\r
121 return getVariableEquation();
\r
129 * Combines all possible equations for this variable. Array variables can have multiple equations.
\r
130 * @return equations or null
\r
132 protected String getVariableEquation() {
\r
133 if(this.expressions == null)
\r
136 ArrayList<IExpression> expressions = getExpressions();
\r
137 ArrayList<Enumeration> enumerations = getArrayIndexes();
\r
138 IExpression firstExpression = expressions.get(0);
\r
139 if(enumerations == null || enumerations.size() < 1) {
\r
140 if(firstExpression == null)
\r
143 return firstExpression.getEquation();
\r
145 // ARRAY variable. Create all equations for the variable
\r
146 StringBuilder sb = new StringBuilder();
\r
147 for(IExpression expression : expressions) {
\r
148 sb.append(expression.getEquation());
\r
150 return sb.toString();
\r
154 public ArrayList<IElement> getIncomingDependencies() {
\r