From: villberg Date: Thu, 8 Dec 2016 13:03:33 +0000 (+0000) Subject: refs #6860 Sysdyn update site on top of Simantics Desktop X-Git-Tag: v1.29.0~18^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5a503b13e3fe40e1f2738d7765df0bebcb286d15;p=simantics%2Fsysdyn.git refs #6860 Sysdyn update site on top of Simantics Desktop git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@33407 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 8700d51b..21006429 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 ffd674d8..04c7a5d6 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -120,39 +120,41 @@ SYSDYN.Variable -- SYSDYN.Variable.value -- SYSDYN.Variable.values -- SYSDYN.Variable.times -- SYSDYN.Variable.names -- SYSDYN.Variable.activeDatasets -- SYSDYN.Variable.time -- SYSDYN.Variable.equation -- SYSDYN.Experiment.Run.time -- SYSDYN.HistoryDataset.sheet --> SHEET.Spreadsheet -- SYSDYN.Experiment.result --> SYSDYN.Result -- SYSDYN.Experiment.result SYSDYN.InitialCondition -- SYSDYN.InitialCondition.HasInitialValues --> MOD.PartialIC -- SYSDYN.ValveSymbol.textLocation --> SYSDYN.Location - - + --> - Structural model browser view for Sysdyn. - + --> @@ -143,7 +143,7 @@ relative="org.simantics.sysdyn.ui.trend.view"> @@ -152,7 +152,7 @@ id="org.simantics.diagram.symbollibrary" minimized="false" relationship="stack" - relative="org.simantics.sysdyn.ui.browser"> + relative="org.simantics.modeling.ui.browser"> + relative="org.simantics.modeling.ui.browser"> Delete the rest of the directory - try { - FileUtils.deleteAll(child); - child.delete(); - } catch (IOException e) { - - } - } - } - } - } - } @Override public void deconfigure() throws ProjectException { diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index 8bf988ce..2083aad0 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -26,7 +26,6 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", org.simantics.spreadsheet;bundle-version="1.1.0", org.eclipse.jface.text;bundle-version="3.6.1", org.simantics.spreadsheet.ontology;bundle-version="1.2.0", - org.simantics.fmu;bundle-version="1.0.0", org.simantics.issues;bundle-version="1.1.0", org.simantics.issues.common;bundle-version="1.1.0", org.simantics.jfreechart;bundle-version="1.0.0", diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/Activator.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/Activator.java index 2970af28..39ba5a16 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/Activator.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/Activator.java @@ -15,6 +15,9 @@ import java.io.File; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; +import org.simantics.sysdyn.manager.InternalGameExperimentFactory; +import org.simantics.sysdyn.solver.SolverSettings.SolverType; +import org.simantics.sysdyn.solver.Solvers; public class Activator implements BundleActivator { @@ -34,16 +37,17 @@ public class Activator implements BundleActivator { modelsDir.mkdir(); } plugin = this; + Solvers.registerGameExperimentFactory(SolverType.INTERNAL, new InternalGameExperimentFactory()); } @Override public void stop(BundleContext context) throws Exception { + Solvers.unregisterGameExperimentFactory(SolverType.INTERNAL); File modelsDir = Activator.getBundleContext().getDataFile("models"); if (modelsDir.exists()) { recursiveDelete(modelsDir); } plugin = null; - } public static Activator getDefault() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/InternalGameExperimentFactory.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/InternalGameExperimentFactory.java new file mode 100644 index 00000000..818487c3 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/InternalGameExperimentFactory.java @@ -0,0 +1,12 @@ +package org.simantics.sysdyn.manager; + +import org.simantics.db.Resource; + +public class InternalGameExperimentFactory implements SysdynExperimentFactory { + + @Override + public SysdynExperiment create(Resource experiment, Resource model) { + return new SysdynGameExperimentInternal(experiment, model); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperimentFactory.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperimentFactory.java new file mode 100644 index 00000000..b0c8cdf1 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperimentFactory.java @@ -0,0 +1,9 @@ +package org.simantics.sysdyn.manager; + +import org.simantics.db.Resource; + +public interface SysdynExperimentFactory { + + SysdynExperiment create(Resource experiment, Resource model); + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java deleted file mode 100644 index 3a396670..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperiment.java +++ /dev/null @@ -1,415 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2012, 2015 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 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.manager; - -import gnu.trove.list.array.TDoubleArrayList; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.HashMap; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.simantics.Simantics; -import org.simantics.databoard.Bindings; -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.common.utils.Logger; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.Layer0Utils; -import org.simantics.db.layer0.variable.Variable; -import org.simantics.db.layer0.variable.Variables; -import org.simantics.db.request.Read; -import org.simantics.fmu.FMUControlJNI; -import org.simantics.fmu.FMUJNIException; -import org.simantics.modelica.IModelicaMonitor; -import org.simantics.modelica.ModelicaManager; -import org.simantics.modelica.SimulationLocation; -import org.simantics.modeling.PartialIC; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.simulation.SimulationJob.HeadlessModelicaMonitor; -import org.simantics.sysdyn.solver.ISolver; -import org.simantics.sysdyn.solver.ModelicaSolver; - -/** - * Game experiment - * @author Teemu Lempinen - * - */ -public class SysdynGameExperiment extends SysdynGameExperimentBase { - - private ModelicaSolver solver; - public FMUControlJNI control; - private boolean loaded = false; - - private static boolean fortranLibrariesLoaded = false; - - public SysdynGameExperiment(Resource experiment, Resource model) { - super(experiment, model); - this.solver = new ModelicaSolver(this); - } - - public FMUControlJNI getFMUControl() { - return control; - } - - @Override - public void init(ReadGraph g) { - super.init(g); - - if(control == null) - control = new FMUControlJNI(); - - results = new TDoubleArrayList[results != null ? results.length : 0]; - - } - - @Override - protected void onExperimentDisposed() { - super.onExperimentDisposed(); - if(control!=null) { - try { - control.unloadFMU(); - loaded = false; - } catch (FMUJNIException e) { - e.printStackTrace(); - } - } - } - - private boolean isValidFMU(File file) { - if(!file.exists()) return false; - if(!file.isFile()) return false; - if(file.length() == 0) return false; - return true; - } - - - @Override - public synchronized void simulate(final IModelicaMonitor monitor, final IProgressMonitor progressMonitor, String modelName) throws IOException { - canceled = false; - - progressMonitor.subTask("Write modelica classes"); - - // Write Modelica files - String modelText = getModelicaCode(monitor, true, getOpenModelicaVersion()); - if(modelText == null) - return; - - omcVersion = ModelicaManager.getDefaultOMVersion(); - - monitor.message("Simulate " + modelName + " using OpenModelica " + omcVersion); - - progressMonitor.worked(1); - - // Write initial files and add init-parameters - progressMonitor.subTask("Write simulation files"); - HashMap inits = getExperimentParameters(monitor); - - - // add loadFile script to load all related functions and function libraries - String additionalScript = getAdditionalScripts(); - - // Create simulation files - 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 (fmu == null && (!isValidFMU(simulationLocation.executableFile) || sysdynModel.isStructureModified())) { - progressMonitor.subTask("Build model"); - buildModel(simulationLocation, monitor); - - saveModelFmu(simulationLocation); - } - - progressMonitor.worked(1); - - if(simulationLocation != null && !canceled) { - // Load fmu and initialize it for simulation - try { - if (!fortranLibrariesLoaded) { // Enable this when we update from OpenModelica 1.9.0beta4! - File omDir = ModelicaManager.getOMHome(); - File mingw = new File(omDir, "MinGW"); - File mbin = new File(mingw, "bin"); - - // Add MinGW path to java.library.path. This is a bit of a hack because - // java.library.path isn't intended to be set programmatically. - String newLibPath = System.getProperty("java.library.path") + File.pathSeparator + mbin.getAbsolutePath(); - System.setProperty("java.library.path", newLibPath); - Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); - fieldSysPath.setAccessible(true); - if (fieldSysPath != null) { - fieldSysPath.set(System.class.getClassLoader(), null); - } - - System.loadLibrary("libgfortran-3"); - System.out.println("Loaded libgfortran-3.dll"); - System.loadLibrary("pthreadGC2"); - System.out.println("Loaded pthreadGC2.dll"); - fortranLibrariesLoaded = true; - } - - control.loadFMUFile(simulationLocation.executableFile.getAbsolutePath()); // unzip and load fmu - loaded = true; - instantiate(); - - } catch (FMUJNIException e) { - System.err.println("SysdynGameExperiment initialization failed:\n\t" + e.getMessage()); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchFieldException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - process = null; - 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 rewindTo(double time) { - - if(control == null) - return; - - if(time >-0.001 && time < 0.001) { - try { - simulate(new HeadlessModelicaMonitor(), new NullProgressMonitor(), sysdynModel.getConfiguration().getLabel()); - //instantiate(); - } catch (IOException e) { - Logger.defaultLogError(e); - } - } else { - System.out.println("rewindTo"); - } - } - - @Override - public void refresh(Session session) { - try { - control.initializeSimulation(); - clearResults(); - //getInitialResultValues(); - - } catch (FMUJNIException e) { - System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage()); - } - } - - private synchronized void instantiate() { - try { - HashMap inits = getExperimentParameters(null); - - control.setStepLength(stepLength); // FIXME: fixed step lenghth - control.setTime(startTime); - control.instantiateSimulation(); // instantiate simulation - - if(!control.isInitialized()) { - control.initializeSimulation(); - } - - if(inits.get("variableFilter") == null || inits.get("variableFilter").equals(".*")) - subscription = control.getAllVariables(); - else - subscription = control.filterVariables(inits.get("variableFilter")); - - // Initialize subscription indexes map for fast result reading in getValue() - if(subscriptionIndexes == null) - subscriptionIndexes = new HashMap(); - - subscriptionIndexes.clear(); - for(int i = 0; i < subscription.length; i++) { - subscriptionIndexes.put(subscription[i], i); - } - - results = new TDoubleArrayList[subscription.length]; - - // Initialize container for current simulation results - currentValues = new double[subscription.length]; - - // subscribe all variables - control.subscribe(subscription); - - clearResults(); - //getInitialResultValues(); - - Simantics.getSession().syncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - - Resource run = Layer0Utils.getPossibleChild(graph, SysdynGameExperiment.this.experiment, getIdentifier()); - if(run == null) { - System.err.println("No run"); - return; - } - - Variable base = Variables.getVariable(graph, run); - - SysdynResource SYSDYN = SysdynResource.getInstance(graph); - - Resource ic = graph.getPossibleObject(SysdynGameExperiment.this.experiment, SYSDYN.Experiment_ic); - if(ic == null) return; - - PartialIC data = graph.getPossibleRelatedValue(ic, SYSDYN.InitialCondition_HasInitialValues, PartialIC.BINDING); - data.apply(graph, base); - - } - - }); - - } catch (FMUJNIException e) { - System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage()); - } catch (DatabaseException e) { - System.err.println("SysdynGameExperiment instantiate failed: " + e.getMessage()); - } - } - -// private synchronized void getInitialResultValues() { -// try { -// // Initialize results -// results.clear(); -// -// currentValues = control.getSubscribedResults(currentValues); -// for(int k = 0; k < subscription.length; k++) { -// results.put(subscription[k], new ArrayList()); -// results.get(subscription[k]).add(currentValues[k]); -// } -// -// ((MemoryResult)getCurrentResult()).setResult(new GameResult(this.results, this.subscription)); -// resultsChanged(); -// } catch (FMUJNIException e) { -// System.err.println("SysdynGameExperiment getInitialResultValues failed: " + e.getMessage()); -// } -// } - - @Override - public void updateSubscriptions() { - - if(!loaded) return; - - try { - if(control.isInitialized()) - currentValues = control.getSubscribedResults(currentValues); - } catch (FMUJNIException e) { - e.printStackTrace(); - } - super.updateSubscriptions(); - } - - @Override - public ISolver getSolver() { - return solver; - } - - } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java index 79f17152..797c8439 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynGameExperimentBase.java @@ -23,7 +23,6 @@ import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncListener; import org.simantics.db.request.Read; -import org.simantics.fmu.FMUJNIException; import org.simantics.simulation.experiment.ExperimentState; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.solver.ISolver; @@ -46,7 +45,7 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { public HashMap subscriptionIndexes; protected double[] currentValues; // Current values from FMU. Updated with updateSubscriptions - TDoubleArrayList[] results; + protected TDoubleArrayList[] results; public String[] subscription; @@ -117,7 +116,7 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { return null; } - public void setSubscribedResults(IProgressMonitor monitor, double time) throws FMUJNIException { + public void setSubscribedResults(IProgressMonitor monitor, double time) throws Exception { monitor.subTask("Get results (time = " + time + ")"); currentValues = getSolver().getSubscribedResults(currentValues); @@ -230,7 +229,7 @@ abstract public class SysdynGameExperimentBase extends OldSysdynExperiment { protected void onPublishResults() { try { setSubscribedResults(new NullProgressMonitor(), getSolver().getTime()); - } catch (FMUJNIException e) { + } catch (Exception e) { Logger.defaultLogError(e); } ((MemoryResult)getCurrentResult()).setResult(new GameResult(this, this.results, this.subscription)); 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 fedac97a..d4879b71 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -12,8 +12,6 @@ *******************************************************************************/ package org.simantics.sysdyn.manager; -import gnu.trove.set.hash.THashSet; - import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -71,6 +69,9 @@ import org.simantics.sysdyn.representation.expressions.ParameterExpression; import org.simantics.sysdyn.representation.expressions.StockExpression; import org.simantics.sysdyn.solver.SolverSettings; import org.simantics.sysdyn.solver.SolverSettings.SolverType; +import org.simantics.sysdyn.solver.Solvers; + +import gnu.trove.set.hash.THashSet; /** * Maintains a Java representation of system dynamic model. @@ -435,12 +436,13 @@ public class SysdynModel implements IModel, IMappingListener, VariableSubscripti if(g.isInstanceOf(experiment, sr.PlaybackExperiment)) { exp = new SysdynPlaybackExperiment(experiment, modelResource); } else if(g.isInstanceOf(experiment, sr.GameExperiment)) { - if (SolverType.INTERNAL.equals(SolverSettings.getSelectedSolverType())) { - exp = new SysdynGameExperimentInternal(experiment, modelResource); - } - else { - exp = new SysdynGameExperiment(experiment, modelResource); - } + exp = Solvers.instantiateGameExperiment(SolverSettings.getSelectedSolverType(), experiment, modelResource); +// if (SolverType.INTERNAL.equals()) { +// exp = new SysdynGameExperimentInternal(experiment, modelResource); +// } +// else { +// exp = new SysdynGameExperiment(experiment, modelResource); +// } } else if(g.isInstanceOf(experiment, sr.SensitivityAnalysisExperiment)) { exp = new SysdynSensitivityAnalysisExperiment(experiment, modelResource); } else if(g.isInstanceOf(experiment, sr.BasicExperiment)) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/ModelicaSolver.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/ModelicaSolver.java deleted file mode 100644 index 25380681..00000000 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/ModelicaSolver.java +++ /dev/null @@ -1,93 +0,0 @@ -package org.simantics.sysdyn.solver; - -import org.simantics.fmu.FMUJNIException; -import org.simantics.sysdyn.manager.SysdynGameExperiment; -import org.simantics.sysdyn.solver.SolverSettings.SolverType; - -public class ModelicaSolver implements ISolver { - - SysdynGameExperiment exp; - - public ModelicaSolver(SysdynGameExperiment exp) { - this.exp = exp; - } - - @Override - public void initialize() throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public void buildModel() throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public void runSolver() throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public void updateResults() throws Exception { - // TODO Auto-generated method stub - - } - - @Override - public SolverType getType() { - // TODO Auto-generated method stub - return null; - } - - @Override - public double[] getSubscribedResults(double[] array) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void prepareToStep() throws Exception { - - // initialize if not initialized - if(!exp.control.isInitialized()) { - exp.control.setTime(exp.startTime); - exp.control.initializeSimulation(); - exp.clearResults(); - // getInitialResultValues(); - } - - exp.control.setStepLength(exp.stepLength); // Set step length each time in case there has been changes - - } - - @Override - public void simulateStep() throws Exception { - exp.control.simulateStep(); - } - - @Override - public void setStepLength(double length) throws Exception { - exp.control.setStepLength(length); - } - - @Override - public double getTime() { - if(exp.control != null) { - try { - return exp.control.getTime(); - } catch (FMUJNIException e) { - } - } - return 0.0; - } - - @Override - public void setRealValue(String name, double value) throws Exception { - exp.control.setRealValue(name, value); - } - - -} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/Solvers.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/Solvers.java new file mode 100644 index 00000000..4a73479b --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/solver/Solvers.java @@ -0,0 +1,52 @@ +package org.simantics.sysdyn.solver; + +import java.util.HashMap; +import java.util.Map; + +import org.simantics.db.Resource; +import org.simantics.sysdyn.manager.SysdynExperiment; +import org.simantics.sysdyn.manager.SysdynExperimentFactory; + +public class Solvers { + + private Map gameExperimentFactories = new HashMap<>(); + + private static Solvers INSTANCE; + + private Solvers() { + + } + + public static synchronized Solvers getInstance() { + + if(INSTANCE == null) { + INSTANCE = new Solvers(); + } + return INSTANCE; + + } + + public static synchronized SysdynExperiment instantiateGameExperiment(SolverSettings.SolverType solver, Resource experiment, Resource model) throws Exception { + + Solvers s = getInstance(); + SysdynExperimentFactory factory = s.gameExperimentFactories.get(solver); + if(factory == null) throw new Exception("No game experiment factory registered as " + solver); + return factory.create(experiment, model); + + } + + public static synchronized void registerGameExperimentFactory(SolverSettings.SolverType solver, SysdynExperimentFactory factory) { + + Solvers s = getInstance(); + s.gameExperimentFactories.put(solver, factory); + + } + + public static synchronized void unregisterGameExperimentFactory(SolverSettings.SolverType solver) { + + Solvers s = getInstance(); + s.gameExperimentFactories.remove(solver); + + } + +}