From: miettinen Date: Tue, 14 Jan 2014 10:42:30 +0000 (+0000) Subject: Solving problems in custom integral equation in stocks (refs #4657). X-Git-Tag: 1.8.1~165 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=613935f0a6521d12f598b47e2a6a0d1445aafcda;p=simantics%2Fsysdyn.git Solving problems in custom integral equation in stocks (refs #4657). git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28628 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 29f035b1..27edbee8 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 7aacf9fe..b14b917b 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -264,6 +264,8 @@ SYSDYN.DelayExpression -- SYSDYN.DelayExpression.initialValue --> L0.String -- SYSDYN.StockExpression.useCustomIntegral --> SYSDYN.StockExpression -- SYSDYN.StockExpression.integralEquation --> L0.String -- SYSDYN.StockExpression.initialEquation --> L0.String 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") : ""; + String integralEquation = data.get("integral") != null ? (String)data.get("integral") : getDefaultIntegral(input.expression); Label label = new Label(parent, SWT.NONE); @@ -102,7 +102,8 @@ public class StockExpression implements IExpression { // Delete the possible integral expression from the database to note // that we are in the "default" mode. SysdynResource sr = SysdynResource.getInstance(graph); - graph.deny(input.expression, sr.StockExpression_integralEquation); + graph.deny(input.expression, sr.StockExpression_useCustomIntegral); + graph.claimLiteral(input.expression, sr.StockExpression_integralEquation, getDefaultIntegral(graph, input.expression)); } }); break; @@ -151,7 +152,7 @@ public class StockExpression implements IExpression { 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 : ""; + initialEquation = (initialEquation != null) ? initialEquation : ""; return new Pair(integralEquation, initialEquation); } else { return new Pair(null, ""); @@ -166,6 +167,21 @@ public class StockExpression implements IExpression { } if (equations.first == null) { + try { + SimanticsUI.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)); + } + + }); + } catch (DatabaseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + data.put("integral", getDefaultIntegral(expression)); } else { data.put("integral", equations.first); @@ -191,10 +207,12 @@ public class StockExpression implements IExpression { public void save(final Resource expression, final Map data) { final String currentInitial = this.initialExpression.getExpression(); final String currentIntegral = this.integralExpression.getExpression(); - if(currentInitial == null - || !currentInitial.equals(data.get("initialEquation")) - || (currentIntegral != null - && !currentIntegral.equals(data.get("integral")))) { + if (currentIntegral == null) { + this.integralExpression.setExpression(getDefaultIntegral(expression)); + } + if(!currentInitial.equals(data.get("initialEquation")) + || currentIntegral == null + || !currentIntegral.equals(data.get("integral"))) { SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override @@ -222,17 +240,17 @@ public class StockExpression implements IExpression { } g.claimLiteral(expression, sr.StockExpression_initialEquation, currentInitial); - // If the value is same as default, do not save to database. - if (currentIntegral != null && !currentIntegral.equals(data.get("integral"))) { - g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral); + if (!currentIntegral.equals(data.get("integral"))) { + // 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); } }); } this.initialExpression.setExpression(currentInitial); - if (currentIntegral != null) - this.integralExpression.setExpression(currentIntegral); + this.integralExpression.setExpression(currentIntegral); } @Override @@ -243,7 +261,42 @@ public class StockExpression implements IExpression { data.put("integral", this.integralExpression.getExpression()); } - private String getDefaultIntegral(final Resource expression) { + + 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) @@ -253,36 +306,7 @@ public class StockExpression implements IExpression { @Override public String perform(ReadGraph graph) 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(); + return getDefaultIntegral(graph, expression); } }); 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 96c1c981..d90fd960 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Association for Decentralized Information Management in + * Copyright (c) 2007, 2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -12,7 +12,6 @@ package org.simantics.sysdyn.ui.validation; import java.io.StringReader; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -127,17 +126,8 @@ public class ValidationUtils { SysdynResource sr = SysdynResource.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); + // Add incoming dependencies and flows Collection dependencies = graph.getObjects(r, sr.Variable_isHeadOf); - - // Add also the outgoing flows - Collection flows = graph.getObjects(r, sr.Variable_isTailOf); - ArrayList outgoingFlows = new ArrayList(); - for (Resource f : flows) { - if (graph.isInstanceOf(f, sr.Flow)) - outgoingFlows.add(f); - } - dependencies.addAll(outgoingFlows); - for(Resource d : dependencies) { if(graph.isInstanceOf(d, sr.Dependency) || (graph.isInstanceOf(d, sr.Flow) && graph.isInstanceOf(r, sr.Stock))) { @@ -156,6 +146,20 @@ public class ValidationUtils { } } } + + // Add also the outgoing flows + Collection outgoingFlows = graph.getObjects(r, sr.Variable_isTailOf); + for(Resource f : outgoingFlows) { + if(graph.isInstanceOf(f, sr.Flow) && graph.isInstanceOf(r, sr.Stock)) { + Resource head = graph.getPossibleObject(f, sr.Variable_HasHead); + if(head != null) { + + Object name = graph.getPossibleRelatedValue(head, l0.HasName); + if(name != null) + variables.add((String)name); + } + } + } } return variables; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java index ba6916cc..c96d01e4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Association for Decentralized Information Management in + * Copyright (c) 2010, 2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -42,12 +42,15 @@ import org.simantics.sysdyn.representation.utils.UnitUtils; @GraphType(SysdynResource.URIs.StockExpression) public class StockExpression extends Expression { - @RelatedValue(SysdynResource.URIs.StockExpression_initialEquation) + @RelatedValue(SysdynResource.URIs.StockExpression_initialEquation) private String initialEquation; + @RelatedValue(SysdynResource.URIs.StockExpression_integralEquation) + private String integralEquation; + @Override public String getExpression() { - return initialEquation; + return "This + is + not + a + parameter + at + any + time"; } @Override @@ -67,33 +70,39 @@ public class StockExpression extends Expression { .append(parent.getModelicaName() + range) .append(") ="); - // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows) - ArrayList incoming = ((Stock)parent).getIncomingValves(); - ArrayList outgoing = ((Stock)parent).getOutgoingValves(); - if(incoming.isEmpty() && outgoing.isEmpty()) { - // No connections, add 0 for each array index if any. - ArrayList enumerations = parent.getArrayIndexes(); - if(enumerations == null || enumerations.isEmpty()) { - b.append(" 0.0"); - } else { - b.append(" zeros("); - for(int i = 0; i < enumerations.size(); i++) { - b.append(enumerations.get(i).getModelicaName() + ".size"); - if(i != enumerations.size() - 1) - b.append(", "); - } - b.append(")"); - } - + if (integralEquation != null) { + String equation = FormatUtils.replaceWhitespace(this.integralEquation); + equation = FormatUtils.formatExpressionForModelica(parent, equation); + b.append(equation).append(";\n"); } else { - // incoming valves add and outgoing valves reduce the stock - for(Valve valve : outgoing) - b.append("\n - ").append(valve.getModelicaName() + range); - for(Valve valve : incoming) - b.append("\n + ").append(valve.getModelicaName() + range); + // Stock equation is formed automatically using incoming and outgoing flows (actually the nearest valves in those flows) + ArrayList incoming = ((Stock)parent).getIncomingValves(); + ArrayList outgoing = ((Stock)parent).getOutgoingValves(); + if(incoming.isEmpty() && outgoing.isEmpty()) { + // No connections, add 0 for each array index if any. + ArrayList enumerations = parent.getArrayIndexes(); + if(enumerations == null || enumerations.isEmpty()) { + b.append(" 0.0"); + } else { + b.append(" zeros("); + for(int i = 0; i < enumerations.size(); i++) { + b.append(enumerations.get(i).getModelicaName() + ".size"); + if(i != enumerations.size() - 1) + b.append(", "); + } + b.append(")"); + } + + } else { + // incoming valves add and outgoing valves reduce the stock + for(Valve valve : outgoing) + b.append("\n - ").append(valve.getModelicaName() + range); + for(Valve valve : incoming) + b.append("\n + ").append(valve.getModelicaName() + range); + } + b.append(";\n"); } - b.append(";\n"); - return b.toString(); + return b.toString(); } /** @@ -188,7 +197,7 @@ public class StockExpression extends Expression { public String validateUnits(ReadGraph graph, SysdynModel model) { String result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), initialEquation); if(result == null) { - String integralEquation = getEquation(); + String integralEquation = getEquation(); if(integralEquation.contains("=")) result = UnitUtils.matchUnits( graph,