From: lempinen Date: Thu, 21 Mar 2013 11:29:37 +0000 (+0000) Subject: Use old FMU if nothing has been changed in variables, equations or other significant... X-Git-Tag: simantics-1.10~15 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=4d975a303fbaacb13c6ae21d2f10930a397e9eda;p=simantics%2Fsysdyn.git Use old FMU if nothing has been changed in variables, equations or other significant parts. (fixes #4183) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27184 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index a7b7d77e..f2f401d6 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 30c5641a..5838fb7e 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -44,6 +44,8 @@ SYSDYN.SysdynModel -- SYSDYN.SysdynModel.tolerance --> L0.Double -- SYSDYN.SysdynModel.solver --> L0.String -- SYSDYN.SysdynModel.variableFilter --> L0.String -- SYSDYN.SysdynModel.fmuFile --> L0.ByteArray -- SYSDYN.SysdynModel.exeFile --> L0.ByteArray data) { final String currentText = this.expression.getExpression(); - if(currentText != null) { + if(currentText != null && !currentText.equals(data.get("initialEquation"))) { SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java index dd8ffe26..73cd72bb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java @@ -11,6 +11,10 @@ *******************************************************************************/ package org.simantics.sysdyn.manager; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -19,10 +23,13 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; +import org.simantics.databoard.Bindings; import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncListener; import org.simantics.db.request.Read; @@ -175,13 +182,23 @@ public class SysdynGameExperiment extends SysdynExperiment { SimulationLocation simulationLocation = createSimulationFiles(sysdynModel, modelText, inits, additionalScript, true); progressMonitor.worked(1); - + // Load precompiled fmu if structure has not changed and it has not yet been loaded + File fmu = null; + if(!sysdynModel.isStructureModified()) { + if(!simulationLocation.executableFile.isFile()) { + fmu = loadModelFmu(simulationLocation); + } else { + fmu = simulationLocation.executableFile; + } + } // Build the model and store previous model structure and inits that affect the building // If there is no exe file OR the model structure has not changed, no need to build - if (!simulationLocation.executableFile.isFile() || hasStructureChanged(modelText)) { + if (fmu == null && (!simulationLocation.executableFile.isFile() || hasStructureChanged(modelText))) { progressMonitor.subTask("Build model"); buildModel(simulationLocation, modelText, monitor); previousModelStructure = modelText; + + saveModelFmu(simulationLocation); } progressMonitor.worked(1); @@ -202,6 +219,81 @@ public class SysdynGameExperiment extends SysdynExperiment { simulate(false); } + /** + * Load fmu file from database, if it exists for the model of this experiment + * + * @param simulationLocation SimulationLocation indicating where the fmu should be loaded + * @return Loaded fmu or null if it was not loaded from database + */ + private File loadModelFmu(SimulationLocation simulationLocation) { + File fmu = null; + try { + final String fmuLocation = simulationLocation.executableFile.getAbsolutePath(); + fmu = session.syncRequest(new Read() { + @Override + public File perform(ReadGraph graph) throws DatabaseException { + File result = null; + FileOutputStream fos; + try { + fos = new FileOutputStream(fmuLocation); + byte[] fileBArray = graph.getPossibleRelatedValue( + getModel(), SysdynResource.getInstance(graph).SysdynModel_fmuFile, Bindings.BYTE_ARRAY); + + if(fileBArray != null) { + fos.write(fileBArray); + fos.close(); + result = new File(fmuLocation); + } else { + fos.close(); + return null; + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + return fmu; + } + + /** + * Save fmu file from simulationLocation to database + * @param simulationLocation Location for finding fmu + */ + private void saveModelFmu(SimulationLocation simulationLocation) { + final String fmuLocation = simulationLocation.executableFile.getAbsolutePath(); + session.asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + File file = new File(fmuLocation); + byte[] fileBArray = new byte[(int)file.length()]; + FileInputStream fis; + try { + fis = new FileInputStream(file); + + fis.read(fileBArray); + graph.claimLiteral( + getModel(), + SysdynResource.getInstance(graph).SysdynModel_fmuFile, + fileBArray, Bindings.BYTE_ARRAY); + fis.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + } + @Override public void simulateDuration(double duration) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 55a72243..29f9fc72 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -53,6 +53,7 @@ import org.simantics.sysdyn.adapter.VariableValueSubscription; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Model; import org.simantics.sysdyn.representation.Module; import org.simantics.sysdyn.representation.ParameterOverride; import org.simantics.sysdyn.representation.Sheet; @@ -271,10 +272,19 @@ public class SysdynModel implements IMappingListener, IModel, VariableSubscripti */ public synchronized boolean update(ReadGraph graph) throws DatabaseException { if(mapping.isDomainModified()) { - structureModified = true; + + try { - mapping.updateRange(graph); + Collection updated = mapping.updateRange(graph); + + for(Object o : updated) { + if(!(o instanceof Model)) { + setStructureModified(true); + break; + } + } + } catch (MappingException e) { SysdynConsole.INSTANCE.message( "Error: Mapping is broken! Find the problem, " + @@ -305,7 +315,7 @@ public class SysdynModel implements IMappingListener, IModel, VariableSubscripti } else return false; - } + } /** * Update mapping. @@ -560,7 +570,7 @@ public class SysdynModel implements IMappingListener, IModel, VariableSubscripti return structureModified; } - public void setStructureModeified(boolean structureModified) { + public void setStructureModified(boolean structureModified) { this.structureModified = structureModified; } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java index 92226cc8..db41a5bb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java @@ -11,8 +11,10 @@ *******************************************************************************/ package org.simantics.sysdyn.representation; +import gnu.trove.map.hash.THashMap; +import gnu.trove.set.hash.THashSet; + import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import org.simantics.Simantics; @@ -41,8 +43,8 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { @RelatedElement(Layer0.URIs.PartOf) protected Book book; - HashMap cells = new HashMap(); - HashSet usedRanges = new HashSet(); + THashMap cells = new THashMap(); + THashSet usedRanges = new THashSet(); Resource resource; @@ -52,12 +54,10 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { @UpdateMethod public boolean updateCells(ReadGraph g, Resource r) throws DatabaseException { - cells.clear(); - usedRanges.clear(); - this.resource = null; - if(g.hasStatement(r)) { this.resource = r; + THashMap newCells = new THashMap(); + g.getObjects(r, Layer0.getInstance(g).ConsistsOf); Variable v = g.adapt(r, Variable.class); for(Variable child : v.getChildren(g)) { @@ -65,14 +65,33 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { try { SpreadsheetUtils.decodeCellAbsolute(name); Variant value = child.getPropertyValue(g, SheetVariables.CONTENT, Bindings.VARIANT); - cells.put(name, value.getValue()); + newCells.put(name, value.getValue()); } catch (CellParseException e) { } catch (MissingVariableException e) { System.out.println("missing content for: " + name); } } + + boolean update = false; + if(newCells.size() == this.cells.size()) { + // Cells are the same size. There might not be changes. Next up: content + for(String key : this.cells.keySet()) { + if(!this.cells.get(key).equals(newCells.get(key))) { + update = true; + break; + } + } + } else { + update = true; + } + + if(update) { + this.cells = newCells; + this.usedRanges.clear(); + return true; + } } - return true; + return false; } public String getStringRepresentation() { @@ -200,7 +219,7 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { } - public HashMap getCells() { + public THashMap getCells() { return cells; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java index f61c40c1..b5736f87 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java @@ -49,9 +49,10 @@ public class SimulationJob extends Job { this.monitor.message("Simulate " + model.getConfiguration().getLabel()); try { model.update(); - if(experiment instanceof SysdynExperiment) + if(experiment instanceof SysdynExperiment) { ((SysdynExperiment)experiment).simulate(this.monitor, monitor); -// model.simulate(this.monitor, monitor, experiment); + model.setStructureModified(false); + } } catch (Exception e) { e.printStackTrace(); this.monitor.showConsole();