From 941023870ce6ae1a13b53dbc182e0116dced1cef Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Mon, 30 Jun 2014 10:54:47 +0000 Subject: [PATCH] refs #5042 refs #5019 refs #5009 Merging changes from r29718, r29733, r29763 trunk to branches/1.8 sysdyn 1.8.1 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/branches/1.8@29767 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../src/fi/semantum/sysdyn/solver/Array.java | 11 +- .../sysdyn/solver/ElementwiseProduct.java | 25 +- .../fi/semantum/sysdyn/solver/NodeClass.java | 2 + .../src/fi/semantum/sysdyn/solver/Parser.java | 45 ++- .../src/fi/semantum/sysdyn/solver/Solver.java | 80 +++++- .../sysdyn/solver/parser/ModelParser.java | 266 +++++++++--------- .../sysdyn/solver/parser/ModelicaParser.jj | 2 +- .../sysdyn/solver/parser/ModelicaParser.jjt | 2 +- org.simantics.sysdyn/scl/Sysdyn.scl | 1 + .../sysdyn/manager/OldSysdynExperiment.java | 14 +- .../sysdyn/manager/SysdynExperiments.java | 16 ++ .../sysdyn/solver/InternalSolver.java | 1 + 12 files changed, 303 insertions(+), 162 deletions(-) diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java index db001506..68a0504b 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java @@ -13,18 +13,23 @@ package fi.semantum.sysdyn.solver; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.Map; public class Array implements IExpression { public static final Array FULL = new Array(); - private ArrayList elements = new ArrayList(); + final private ArrayList elements; public Array() { - + elements = new ArrayList(); } + public Array(int size) { + elements = new ArrayList(size); + } + public Array addElement(Object element) { if(element instanceof Constant) addElement(((Constant)element).value); else elements.add(element); @@ -70,7 +75,7 @@ public class Array implements IExpression { return elements.get(index); } - public Collection elements() { + public List elements() { return elements; } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseProduct.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseProduct.java index e3198cfe..f87529c5 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseProduct.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/ElementwiseProduct.java @@ -12,6 +12,7 @@ package fi.semantum.sysdyn.solver; import java.util.Collection; import java.util.Iterator; +import java.util.List; import java.util.Map; public class ElementwiseProduct implements IExpression { @@ -36,26 +37,24 @@ public class ElementwiseProduct implements IExpression { if(left instanceof Array && right instanceof Array) { Array la = (Array)left; Array ra = (Array)right; - Collection lae = la.elements(); - Collection rae = ra.elements(); + List lae = la.elements(); + List rae = ra.elements(); if(lae.size() != rae.size()) throw new UnsupportedOperationException(); - Iterator li = lae.iterator(); - Iterator ri = rae.iterator(); - Array result = new Array(); - for(int i=0;i lae = la.elements(); - Iterator li = lae.iterator(); - Array result = new Array(); - for(int i=0;i lae = la.elements(); + Array result = new Array(lae.size()); + for(Object o : lae) { + Double ld = (Double)o; result.addElement(ld*rd); } return result; diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NodeClass.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NodeClass.java index 81b86df7..8d2e49a0 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NodeClass.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/NodeClass.java @@ -33,9 +33,11 @@ public enum NodeClass { subscript_2, component_clause, component_declaration, + component_list, array_subscripts, declaration, class_definition, + class_specifier, array, primary, component_reference, diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java index bd6fc87d..e2c722d7 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java @@ -40,6 +40,17 @@ public class Parser { static Map nodeNameMap = new HashMap(); + public String getPackagePath(SimpleNode n) { + SimpleNode parent = (SimpleNode)n.jjtGetParent(); + if(parent == null) return ""; + NodeClass nc = NodeClass.of(parent.toString()); + if (nc == NodeClass.class_specifier) { + return getPackagePath(parent) + parent.op + "."; + } else { + return getPackagePath(parent); + } + } + public Object walk_(SimpleNode n, int indent, IFrame frame) { Model model = frame instanceof Model ? (Model)frame : null; @@ -246,9 +257,20 @@ public class Parser { IExpression[] subscripts = (third instanceof IExpression[])? (IExpression[])third : null; int declarationStart = (third instanceof IExpression[]) ? 3 : 2; ArrayList clauses = new ArrayList(); - for(int i=declarationStart;i declarations2 = new ArrayList(); + if(third instanceof ArrayList) { + ArrayList decls = (ArrayList)third; + declarations2.addAll(decls); + } else { + for(int i=declarationStart;i result = new ArrayList(); + for(int i=0;i stms2 = new ArrayList(); Function function = new Function(functionName, new StatementList(stms2)); ArrayList composition = (ArrayList)walk(child, indent+2, function); @@ -392,6 +427,10 @@ public class Parser { return null; + } else if("package".equals(n.op)) { + + return walk((SimpleNode)n.jjtGetChild(0), indent+2, frame); + } else { System.err.println("class_definition " + n.op); } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java index b65afaff..e03b297d 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Solver.java @@ -42,6 +42,9 @@ public class Solver { private NodeCache cache; private boolean ready; + private boolean dirty; + private boolean started; + public Solver() { defaultStep = 0.1; start = 0; @@ -105,23 +108,27 @@ public class Solver { }); } + + private SimpleNode n; + private String codeCache = null; public void prepare(String input) throws Exception { long startNanos = System.nanoTime(); -// LineReader reader = new LineReader(input, cache); -// reader.parse(); -// -// model = reader.model; + if(!input.equals(codeCache)) { - StringReader reader = new StringReader(input); - ModelParser modelParser = new ModelParser(reader); - SimpleNode n = (SimpleNode)modelParser.stored_definition(); + StringReader reader = new StringReader(input); + ModelParser modelParser = new ModelParser(reader); + n = (SimpleNode)modelParser.stored_definition(); + + } + Parser parser = new Parser(); model = new Model(new Globals(), "", false); parser.walk(n, 0, model); - + codeCache = input; + env = new Environment(model, defaultStep, start); int size = model.prepare(); @@ -237,6 +244,8 @@ public class Solver { env.initial = false; ready = true; + started = false; + dirty = false; long endNanos = System.nanoTime(); @@ -252,15 +261,67 @@ public class Solver { return env.getValueArray(); } + public void aboutToRun() { + + if(!started && dirty) { + + // Solve all algebraic equations + Assignment[] assignments = model.assignmentArray; + for(int i=0;i String -> () deleteIC :: Variable -> String -> () + setPublishResults :: Variable -> Boolean -> () \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/OldSysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/OldSysdynExperiment.java index 326e8cbe..123bede4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/OldSysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/OldSysdynExperiment.java @@ -86,6 +86,8 @@ public class OldSysdynExperiment extends SysdynExperiment { protected static String omcHome = null; public static OldSysdynExperiment INSTANCE; + + boolean publishResults = true; public OldSysdynExperiment(Resource experiment, Resource model) { super(experiment, model); @@ -157,8 +159,10 @@ public class OldSysdynExperiment extends SysdynExperiment { @Override public void run() { if(!canceled) { - // Get and store results result.saveToFile(file, progressMonitor); - } } + // Get and store results + result.saveToFile(file, progressMonitor); + } + } }; } @@ -822,6 +826,11 @@ public class OldSysdynExperiment extends SysdynExperiment { public void resultsChanged() { resultsChanged(false); } + + public void setPublishResults(boolean value) { + publishResults = value; + if(publishResults) resultsChanged(true); + } public void resultsChanged(boolean force) { long time = System.nanoTime(); @@ -841,6 +850,7 @@ public class OldSysdynExperiment extends SysdynExperiment { */ @Override public void updateSubscriptions() { + if(!publishResults) return; for(VariableValueSubscription subscription : getListenerSnapshot()) subscription.update(); skippedVariableUpdate = false; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java index 78d7902c..aaa959a5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiments.java @@ -35,6 +35,22 @@ import org.simantics.utils.DataContainer; public class SysdynExperiments { + public static void setPublishResults(ReadGraph graph, Variable variable, boolean value) throws DatabaseException { + + final IProject project = Simantics.getProject(); + if (project == null) return; + + final IExperimentManager experimentManager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + if(experimentManager == null) return; + + IExperiment exp = experimentManager.getExperiment(variable.getName(graph)); + if(exp instanceof OldSysdynExperiment) { + OldSysdynExperiment experiment = (OldSysdynExperiment)exp; + experiment.setPublishResults(value); + } + + } + public static String activateExperiment(IProgressMonitor monitor, final IProject project, final IExperimentManager manager, final Resource experimentResource) { if(monitor == null) monitor = new NullProgressMonitor(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java index 6835e191..e7cd7bf1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/InternalSolver.java @@ -197,6 +197,7 @@ public class InternalSolver implements ISolver { @Override public void prepareToStep() throws Exception { + solver.aboutToRun(); } @Override -- 2.47.1