>-- SYSDYN.DelayExpression.initialValue --> L0.String <R SYSDYN.HasEquationOrEmpty : L0.FunctionalRelation
SYSDYN.StockExpression <T SYSDYN.Expression
+ >-- SYSDYN.StockExpression.useCustomIntegral --> SYSDYN.StockExpression <R L0.IsRelatedTo : L0.Tag
+ @L0.symmetric
>-- SYSDYN.StockExpression.integralEquation --> L0.String <R SYSDYN.Expression.equation : L0.FunctionalRelation
>-- SYSDYN.StockExpression.initialEquation --> L0.String <R SYSDYN.Expression.equation : L0.FunctionalRelation
public final Resource StockExpression_initialEquation_Inverse;\r
public final Resource StockExpression_integralEquation;\r
public final Resource StockExpression_integralEquation_Inverse;\r
+ public final Resource StockExpression_useCustomIntegral;\r
public final Resource StockSymbol;\r
public final Resource SymbolReferences;\r
public final Resource SymbolReferences_BasicSymbols;\r
public static final String StockExpression_initialEquation_Inverse = "http://www.simantics.org/Sysdyn-1.1/StockExpression/initialEquation/Inverse";\r
public static final String StockExpression_integralEquation = "http://www.simantics.org/Sysdyn-1.1/StockExpression/integralEquation";\r
public static final String StockExpression_integralEquation_Inverse = "http://www.simantics.org/Sysdyn-1.1/StockExpression/integralEquation/Inverse";\r
+ public static final String StockExpression_useCustomIntegral = "http://www.simantics.org/Sysdyn-1.1/StockExpression/useCustomIntegral";\r
public static final String StockSymbol = "http://www.simantics.org/Sysdyn-1.1/StockSymbol";\r
public static final String SymbolReferences = "http://www.simantics.org/Sysdyn-1.1/SymbolReferences";\r
public static final String SymbolReferences_BasicSymbols = "http://www.simantics.org/Sysdyn-1.1/SymbolReferences/BasicSymbols";\r
StockExpression_initialEquation_Inverse = getResourceOrNull(graph, URIs.StockExpression_initialEquation_Inverse);\r
StockExpression_integralEquation = getResourceOrNull(graph, URIs.StockExpression_integralEquation);\r
StockExpression_integralEquation_Inverse = getResourceOrNull(graph, URIs.StockExpression_integralEquation_Inverse);\r
+ StockExpression_useCustomIntegral = getResourceOrNull(graph, URIs.StockExpression_useCustomIntegral);\r
StockSymbol = getResourceOrNull(graph, URIs.StockSymbol);\r
SymbolReferences = getResourceOrNull(graph, URIs.SymbolReferences);\r
SymbolReferences_BasicSymbols = getResourceOrNull(graph, URIs.SymbolReferences_BasicSymbols);\r
/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management in\r
+ * Copyright (c) 2007, 2014 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
public void createExpressionFields(Composite parent, Map<String, Object> data, Table allowedVariables) {\r
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(parent);\r
String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
- String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
+ String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression);\r
\r
\r
Label label = new Label(parent, SWT.NONE);\r
// Delete the possible integral expression from the database to note \r
// that we are in the "default" mode.\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
- graph.deny(input.expression, sr.StockExpression_integralEquation);\r
+ graph.deny(input.expression, sr.StockExpression_useCustomIntegral);\r
+ graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
}\r
});\r
break;\r
if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
String initialEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_initialEquation);\r
String integralEquation = graph.getPossibleRelatedValue(expression, sr.StockExpression_integralEquation);\r
- initialEquation = (initialEquation != null) ? initialEquation : "";\r
+ initialEquation = (initialEquation != null) ? initialEquation : "";\r
return new Pair<String, String>(integralEquation, initialEquation);\r
} else {\r
return new Pair<String, String>(null, "");\r
}\r
\r
if (equations.first == null) {\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression));\r
+ }\r
+\r
+ });\r
+ } catch (DatabaseException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ \r
data.put("integral", getDefaultIntegral(expression));\r
} else {\r
data.put("integral", equations.first);\r
public void save(final Resource expression, final Map<String, Object> data) {\r
final String currentInitial = this.initialExpression.getExpression();\r
final String currentIntegral = this.integralExpression.getExpression();\r
- if(currentInitial == null \r
- || !currentInitial.equals(data.get("initialEquation"))\r
- || (currentIntegral != null\r
- && !currentIntegral.equals(data.get("integral")))) {\r
+ if (currentIntegral == null) {\r
+ this.integralExpression.setExpression(getDefaultIntegral(expression));\r
+ }\r
+ if(!currentInitial.equals(data.get("initialEquation"))\r
+ || currentIntegral == null\r
+ || !currentIntegral.equals(data.get("integral"))) {\r
SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
\r
@Override\r
}\r
g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial);\r
\r
- // If the value is same as default, do not save to database.\r
- if (currentIntegral != null && !currentIntegral.equals(data.get("integral"))) {\r
- g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
+ if (!currentIntegral.equals(data.get("integral"))) {\r
+ // If the value is not same as default, enable the custom tag\r
+ g.claim(expression, sr.StockExpression_useCustomIntegral, expression);\r
}\r
+ g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
}\r
\r
});\r
}\r
this.initialExpression.setExpression(currentInitial);\r
- if (currentIntegral != null)\r
- this.integralExpression.setExpression(currentIntegral);\r
+ this.integralExpression.setExpression(currentIntegral);\r
}\r
\r
@Override\r
data.put("integral", this.integralExpression.getExpression());\r
}\r
\r
- private String getDefaultIntegral(final Resource expression) {\r
+ \r
+ private static String getDefaultIntegral(ReadGraph graph, Resource expression) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ \r
+ // find the variable\r
+ Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
+ if(variable == null)\r
+ return "";\r
+ \r
+ SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
+ SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
+ model.update(graph);\r
+ \r
+ Stock stock = (Stock)model.getElement(variable);\r
+ \r
+ String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\r
+ if(range == null)\r
+ range = "";\r
+ \r
+ StringBuilder builder = new StringBuilder();\r
+ builder.append("");\r
+ for(Valve in : stock.getIncomingValves()) {\r
+ builder.append(" + " + in.getName() + range);\r
+ }\r
+ for(Valve out : stock.getOutgoingValves()) {\r
+ builder.append(" - " + out.getName() + range);\r
+ }\r
+ if (builder.indexOf(" + ") == 0)\r
+ builder.delete(0, 3);\r
+ \r
+ return builder.toString().trim(); \r
+ }\r
+ \r
+ \r
+ private static String getDefaultIntegral(final Resource expression) {\r
\r
String integral = "";\r
if(expression == null)\r
\r
@Override\r
public String perform(ReadGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- \r
- // find the variable\r
- Resource variable = graph.getPossibleObject(expression, l0.PartOf);\r
- if(variable == null)\r
- return "";\r
- \r
- SysdynModelManager sdm = SysdynModelManager.getInstance(graph.getSession());\r
- SysdynModel model = sdm.getModel(graph, graph.getSingleObject(variable, l0.PartOf));\r
- model.update(graph);\r
- \r
- Stock stock = (Stock)model.getElement(variable);\r
- \r
- String range = graph.getPossibleRelatedValue(expression, sr.Expression_arrayRange);\r
- if(range == null)\r
- range = "";\r
- \r
- StringBuilder builder = new StringBuilder();\r
- builder.append("");\r
- for(Valve in : stock.getIncomingValves()) {\r
- builder.append(" + " + in.getName() + range);\r
- }\r
- for(Valve out : stock.getOutgoingValves()) {\r
- builder.append(" - " + out.getName() + range);\r
- }\r
- if (builder.indexOf(" + ") == 0)\r
- builder.delete(0, 3);\r
- \r
- return builder.toString().trim();\r
+ return getDefaultIntegral(graph, expression);\r
}\r
\r
});\r
/*******************************************************************************\r
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2007, 2014 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
package org.simantics.sysdyn.ui.validation;\r
\r
import java.io.StringReader;\r
-import java.util.ArrayList;\r
import java.util.Collection;\r
import java.util.HashMap;\r
import java.util.HashSet;\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
Layer0 l0 = Layer0.getInstance(graph);\r
\r
+ // Add incoming dependencies and flows\r
Collection<Resource> dependencies = graph.getObjects(r, sr.Variable_isHeadOf);\r
- \r
- // Add also the outgoing flows \r
- Collection<Resource> flows = graph.getObjects(r, sr.Variable_isTailOf);\r
- ArrayList<Resource> outgoingFlows = new ArrayList<Resource>();\r
- for (Resource f : flows) {\r
- if (graph.isInstanceOf(f, sr.Flow))\r
- outgoingFlows.add(f);\r
- }\r
- dependencies.addAll(outgoingFlows);\r
-\r
for(Resource d : dependencies) {\r
if(graph.isInstanceOf(d, sr.Dependency)\r
|| (graph.isInstanceOf(d, sr.Flow) && graph.isInstanceOf(r, sr.Stock))) {\r
}\r
}\r
}\r
+ \r
+ // Add also the outgoing flows \r
+ Collection<Resource> outgoingFlows = graph.getObjects(r, sr.Variable_isTailOf);\r
+ for(Resource f : outgoingFlows) {\r
+ if(graph.isInstanceOf(f, sr.Flow) && graph.isInstanceOf(r, sr.Stock)) {\r
+ Resource head = graph.getPossibleObject(f, sr.Variable_HasHead);\r
+ if(head != null) {\r
+\r
+ Object name = graph.getPossibleRelatedValue(head, l0.HasName);\r
+ if(name != null)\r
+ variables.add((String)name);\r
+ }\r
+ }\r
+ }\r
}\r
return variables;\r
}\r
/*******************************************************************************\r
- * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
+ * Copyright (c) 2010, 2014 Association for Decentralized Information Management in\r
* Industry THTH ry.\r
* All rights reserved. This program and the accompanying materials\r
* are made available under the terms of the Eclipse Public License v1.0\r
@GraphType(SysdynResource.URIs.StockExpression)\r
public class StockExpression extends Expression {\r
\r
- @RelatedValue(SysdynResource.URIs.StockExpression_initialEquation)\r
+ @RelatedValue(SysdynResource.URIs.StockExpression_initialEquation)\r
private String initialEquation;\r
\r
+ @RelatedValue(SysdynResource.URIs.StockExpression_integralEquation)\r
+ private String integralEquation;\r
+\r
@Override\r
public String getExpression() {\r
- return initialEquation;\r
+ return "This + is + not + a + parameter + at + any + time";\r
}\r
\r
@Override\r
.append(parent.getModelicaName() + range)\r
.append(") =");\r
\r
- // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows)\r
- ArrayList<Valve> incoming = ((Stock)parent).getIncomingValves();\r
- ArrayList<Valve> outgoing = ((Stock)parent).getOutgoingValves();\r
- if(incoming.isEmpty() && outgoing.isEmpty()) {\r
- // No connections, add 0 for each array index if any.\r
- ArrayList<Enumeration> enumerations = parent.getArrayIndexes();\r
- if(enumerations == null || enumerations.isEmpty()) {\r
- b.append(" 0.0");\r
- } else {\r
- b.append(" zeros(");\r
- for(int i = 0; i < enumerations.size(); i++) {\r
- b.append(enumerations.get(i).getModelicaName() + ".size");\r
- if(i != enumerations.size() - 1)\r
- b.append(", ");\r
- }\r
- b.append(")");\r
- }\r
-\r
+ if (integralEquation != null) {\r
+ String equation = FormatUtils.replaceWhitespace(this.integralEquation);\r
+ equation = FormatUtils.formatExpressionForModelica(parent, equation);\r
+ b.append(equation).append(";\n");\r
} else {\r
- // incoming valves add and outgoing valves reduce the stock\r
- for(Valve valve : outgoing)\r
- b.append("\n - ").append(valve.getModelicaName() + range);\r
- for(Valve valve : incoming)\r
- b.append("\n + ").append(valve.getModelicaName() + range);\r
+ // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows)\r
+ ArrayList<Valve> incoming = ((Stock)parent).getIncomingValves();\r
+ ArrayList<Valve> outgoing = ((Stock)parent).getOutgoingValves();\r
+ if(incoming.isEmpty() && outgoing.isEmpty()) {\r
+ // No connections, add 0 for each array index if any.\r
+ ArrayList<Enumeration> enumerations = parent.getArrayIndexes();\r
+ if(enumerations == null || enumerations.isEmpty()) {\r
+ b.append(" 0.0");\r
+ } else {\r
+ b.append(" zeros(");\r
+ for(int i = 0; i < enumerations.size(); i++) {\r
+ b.append(enumerations.get(i).getModelicaName() + ".size");\r
+ if(i != enumerations.size() - 1)\r
+ b.append(", ");\r
+ }\r
+ b.append(")");\r
+ }\r
+ \r
+ } else {\r
+ // incoming valves add and outgoing valves reduce the stock\r
+ for(Valve valve : outgoing)\r
+ b.append("\n - ").append(valve.getModelicaName() + range);\r
+ for(Valve valve : incoming)\r
+ b.append("\n + ").append(valve.getModelicaName() + range);\r
+ }\r
+ b.append(";\n");\r
}\r
- b.append(";\n");\r
- return b.toString();\r
+ return b.toString();\r
}\r
\r
/** \r
public String validateUnits(ReadGraph graph, SysdynModel model) {\r
String result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), initialEquation);\r
if(result == null) {\r
- String integralEquation = getEquation();\r
+ String integralEquation = getEquation();\r
if(integralEquation.contains("="))\r
result = UnitUtils.matchUnits(\r
graph, \r