/// <summary> /// /// </summary> /// <param name="emlfile"></param> /// <param name="output"></param> public static void Convert(string emlfile, string output) { if (!File.Exists(emlfile)) throw new EcellException(string.Format(MessageResources.ErrFindFile, emlfile)); WrappedSimulator sim = new WrappedSimulator(Util.GetDMDirs()); EcellModel model = EmlReader.Parse(emlfile, sim); SaveSBML(model, output); sim.Dispose(); }
/// <summary> /// Stores the "EcellObject" /// </summary> /// <param name="simulator">The "simulator"</param> /// <param name="dmm">The "DMDescriptorKeeper"</param> /// <param name="ecellObject">The stored "EcellObject"</param> /// <param name="initialCondition">The initial condition.</param> internal static void DataStored( WrappedSimulator simulator, DMDescriptorKeeper dmm, EcellObject ecellObject, Dictionary<string, double> initialCondition) { if (ecellObject.Type.Equals(Constants.xpathStepper)) { DataStored4Stepper(simulator, dmm, ecellObject); } else if (ecellObject.Type.Equals(Constants.xpathSystem)) { DataStored4System( simulator, ecellObject, initialCondition); } else if (ecellObject.Type.Equals(Constants.xpathProcess)) { DataStored4Process( simulator, dmm, ecellObject, initialCondition); } else if (ecellObject.Type.Equals(Constants.xpathVariable)) { DataStored4Variable( simulator, ecellObject, initialCondition); } // // 4 children // if (ecellObject.Children != null) { foreach (EcellObject childEcellObject in ecellObject.Children) DataStored(simulator, dmm, childEcellObject, initialCondition); } }
/// <summary> /// Unload the current simulator. /// </summary> public void UnloadSimulator() { if (m_simulator == null) return; try { m_simulator.Dispose(); } catch (Exception e) { m_env.Console.WriteLine(e.ToString()); m_env.Console.Flush(); } m_simulator = null; }
/// <summary> /// GetVariableValue /// </summary> /// <param name="simulator">the current simulator.</param> /// <param name="name">the variable name</param> /// <param name="entityPath">the entity name.</param> /// <returns></returns> private static EcellValue GetVariableValue(WrappedSimulator simulator, string name, string entityPath) { EcellValue value = null; try { object property = simulator.GetEntityProperty(entityPath); value = new EcellValue(property); } catch (Exception ex) { Trace.WriteLine(ex); if (name.Equals(Constants.xpathFixed)) { value = new EcellValue(0); } else { value = new EcellValue(0.0); } } return value; }
/// <summary> /// Creates the dummy simulator 4 property lists. /// </summary> /// <param name="simulator">The dummy simulator</param> /// <param name="defaultProcess">The dm name of "Process"</param> /// <param name="defaultStepper">The dm name of "Stepper"</param> private static void BuildDefaultSimulator( WrappedSimulator simulator, string defaultProcess, string defaultStepper) { try { // Set DefaultProcess if null if (defaultProcess == null) defaultProcess = Constants.DefaultProcessName; // Set DefaultStepper if null if (defaultStepper == null) defaultStepper = Constants.DefaultStepperName; // simulator.CreateStepper(defaultStepper, Constants.textKey); simulator.CreateEntity( Constants.xpathVariable, Constants.xpathVariable + Constants.delimiterColon + Constants.delimiterPath + Constants.delimiterColon + Constants.xpathSize.ToUpper() ); simulator.CreateEntity( defaultProcess, Constants.xpathProcess + Constants.delimiterColon + Constants.delimiterPath + Constants.delimiterColon + Constants.xpathSize.ToUpper() ); simulator.LoadEntityProperty( Constants.xpathSystem + "::/:" + Constants.xpathStepperID, new string[] { Constants.textKey } ); simulator.LoadEntityProperty( Util.BuildFullPN( Constants.xpathVariable, Constants.delimiterPath, Constants.xpathSize.ToUpper(), Constants.xpathValue ), new string[] { "0.1" } ); simulator.Initialize(); } catch (Exception ex) { throw new EcellException( MessageResources.ErrCombiStepProc, ex); } }
/// <summary> /// Stores the "EcellObject" 4 the "Process". /// </summary> /// <param name="simulator">The simulator</param> /// <param name="dmm">The "DMDescriptorKeeper"</param> /// <param name="ecellObject">The stored "Process"</param> /// <param name="initialCondition">The initial condition.</param> internal static void DataStored4Process( WrappedSimulator simulator, DMDescriptorKeeper dmm, EcellObject ecellObject, Dictionary<string, double> initialCondition) { string key = ecellObject.FullID; IList<string> wrappedPolymorph = null; try { wrappedPolymorph = simulator.GetEntityPropertyList(key); } catch (Exception ex) { Trace.WriteLine(ex); return; } // // Checks the stored "EcellData" // List<EcellData> processEcellDataList = new List<EcellData>(); Dictionary<string, EcellData> storedEcellDataDic = new Dictionary<string, EcellData>(); if (ecellObject.Value != null && ecellObject.Value.Count > 0) { foreach (EcellData storedEcellData in ecellObject.Value) { storedEcellDataDic[storedEcellData.Name] = storedEcellData; processEcellDataList.Add(storedEcellData); } } // // Stores the "EcellData" // foreach (string name in wrappedPolymorph) { string entityPath = Util.BuildFullPN(key, name); PropertyAttributes flag = simulator.GetEntityPropertyAttributes(entityPath); if (!flag.Gettable) { continue; } EcellValue value = null; if (name == Constants.xpathVRL) { // Won't restore the variable reference list from the simulator's corresponding // object. if (storedEcellDataDic.ContainsKey(name)) value = storedEcellDataDic[name].Value; else value = new EcellValue(new List<object>()); } else if (name == Constants.xpathActivity || name == Constants.xpathMolarActivity) { value = new EcellValue(0.0); } else { try { value = new EcellValue(simulator.GetEntityProperty(entityPath)); if (dmm.ContainsDescriptor(ecellObject.Type, ecellObject.Classname)) { DMDescriptor desc = dmm.GetDMDescriptor(ecellObject.Type, ecellObject.Classname); if (desc.ContainsProperty(name)) { PropertyDescriptor prop = desc[name]; if (prop.DefaultValue.Type == EcellValueType.List && !value.IsList) value = new EcellValue(new List<EcellValue>()); } } } catch (Exception ex) { Trace.WriteLine(ex); value = GetValueFromDMM(dmm, ecellObject.Type, ecellObject.Classname, name); } } EcellData ecellData = CreateEcellData(name, value, entityPath, flag); if (ecellData.Value != null) { ecellData.Logable = ecellData.Value.IsDouble && (ecellData.Settable == false || ecellData.Saveable == false); } if (storedEcellDataDic.ContainsKey(name)) { ecellData.Logged = storedEcellDataDic[name].Logged; processEcellDataList.Remove(storedEcellDataDic[name]); } processEcellDataList.Add(ecellData); } ecellObject.SetEcellDatas(processEcellDataList); }
/// <summary> /// Stores the "EcellObject" 4 the "System". /// </summary> /// <param name="simulator">The simulator</param> /// <param name="ecellObject">The stored "System"</param> /// <param name="initialCondition">The initial condition.</param> internal static void DataStored4System( WrappedSimulator simulator, EcellObject ecellObject, Dictionary<string, double> initialCondition) { // Creates an entityPath. string parentPath = ecellObject.ParentSystemID; string childPath = ecellObject.LocalID; string key = Constants.xpathSystem + Constants.delimiterColon + parentPath + Constants.delimiterColon + childPath; // Property List IList<string> wrappedPolymorph = simulator.GetEntityPropertyList(key); // // Checks the stored "EcellData" // List<EcellData> systemEcellDataList = new List<EcellData>(); Dictionary<string, EcellData> storedEcellDataDic = new Dictionary<string, EcellData>(); if (ecellObject.Value != null && ecellObject.Value.Count > 0) { foreach (EcellData storedEcellData in ecellObject.Value) { storedEcellDataDic[storedEcellData.Name] = storedEcellData; systemEcellDataList.Add(storedEcellData); } } foreach (string name in wrappedPolymorph) { string entityPath = key + Constants.delimiterColon + name; PropertyAttributes flag = simulator.GetEntityPropertyAttributes(entityPath); if (!flag.Gettable) { continue; } object value = null; if (name.Equals(Constants.xpathSize)) { value = new EcellValue(EcellSystem.DefaultSize); } else { try { value = new EcellValue(simulator.GetEntityProperty(entityPath)); } catch (WrappedException ex) { Trace.WriteLine(ex); if (storedEcellDataDic.ContainsKey(name)) { IEnumerable val = storedEcellDataDic[name].Value as IEnumerable; object firstItem = null; { IEnumerator i = val.GetEnumerator(); if (i.MoveNext()) firstItem = i.Current; } if (firstItem is IEnumerable) { value = val; } else { value = firstItem; } } else { value = ""; } } } EcellData ecellData = CreateEcellData(name, new EcellValue(value), entityPath, flag); if (ecellData.Value != null) { ecellData.Logable = ecellData.Value.IsDouble; } if (storedEcellDataDic.ContainsKey(name)) { ecellData.Logged = storedEcellDataDic[name].Logged; systemEcellDataList.Remove(storedEcellDataDic[name]); } systemEcellDataList.Add(ecellData); } ecellObject.SetEcellDatas(systemEcellDataList); }
/// <summary> /// Loads the eml formatted file and returns the model ID. /// </summary> public void LoadModel() { WrappedSimulator simulator = null; try { foreach (string filename in m_info.Models) { simulator = CreateSimulatorInstance(); // Load model string modelID = null; EcellModel modelObj = null; try { modelObj = EmlReader.Parse(filename, simulator); } catch (EcellException e) { throw new EcellException(string.Format(MessageResources.ErrLoadModel, filename), e); } catch (Exception e) { string msg = string.Format(MessageResources.ErrLoadModel, filename) + "\n" + e.Message; Util.ShowErrorDialog(msg); continue; } // If file is not Eml, return. if (modelObj.Children == null || modelObj.Children.Count <= 0) continue; if (!string.IsNullOrEmpty(modelObj.ErrMsg) && m_env.PluginManager.DockPanel != null) { Util.ShowWarningDialog(MessageResources.WarnLoadDM); m_env.Console.Write(modelObj.ErrMsg); m_env.Console.Flush(); } // If this project is template. if (m_info.Type == ProjectType.Template) modelObj.ModelID = m_info.Name; modelID = modelObj.ModelID; // Initialize try { simulator.Initialize(); } catch (Exception e) { // Error Message // [VariableReference [S0] not found in this Process] // MichaelisUniUniFluxprocess // DecayFluxProcess // [Only first or second order scheme is allowed] // PingPongBiBiFluxProcess // TauLeapProcess if (m_env.PluginManager.DockPanel != null) Util.ShowWarningDialog(MessageResources.WarnInvalidData + "\n" + e.Message); Trace.WriteLine(e.ToString()); } // Sets initial conditions. InitializeModel(modelObj, simulator); try { string leml = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(filename) + Constants.FileExtLEML; Leml.LoadLEML(m_env, modelObj, leml); } catch (Exception e) { Trace.WriteLine(e.StackTrace); } } // If this project has no model. if (m_modelList.Count <= 0) throw new EcellException(string.Format(MessageResources.ErrNoSet, "Model")); // Stores the "LoggerPolicy" string simParam = m_info.SimulationParam; if (!m_loggerPolicyDic.ContainsKey(simParam)) { m_loggerPolicyDic[simParam] = new LoggerPolicy(); } } catch (Exception ex) { throw new EcellException(string.Format(MessageResources.ErrLoadModel, ""), ex); } m_simulator = simulator; }
public void TestGetDescription() { string[] paths = Util.GetDMDirs(); string desc = ""; WrappedSimulator sim = new WrappedSimulator(paths); try { desc = sim.GetDescription("HogeProcess"); Assert.AreEqual("", desc, "GetDescription method returns unexpected value."); } catch(Exception) { } desc = sim.GetDescription("ExpressionFluxProcess"); Assert.IsNotEmpty(desc, "GetDescription method returns unexpected value."); }
/// <summary> /// Initialize the model /// </summary> /// <param name="modelObject">the model object.</param> /// <param name="simulator">the loaded simulator.</param> public static void InitializeModel(EcellModel modelObject, WrappedSimulator simulator) { bool isWarn = false; string errMsg = MessageResources.WarnLoadDM + "\n"; Dictionary<string, object> processPropertyDic = new Dictionary<string, object>(); // Initialize object foreach (EcellObject obj in modelObject.Children) { // Initialize Stepper if (obj is EcellStepper) { try { simulator.CreateStepper(obj.Classname, obj.Key); } catch (Exception e) { errMsg += obj.FullID + ":" + e.Message + "\n"; Trace.WriteLine(e.ToString()); isWarn = true; } foreach (EcellData data in obj.Value) { simulator.LoadStepperProperty( obj.Key, data.Name, data.Value.Value); } } else if (obj is EcellSystem) { // Initialize System if (!obj.Key.Equals(Constants.delimiterPath)) { try { simulator.CreateEntity( obj.Classname, obj.FullID); } catch (Exception e) { throw new EmlParseException("Failed to create a System entity", e); } } foreach (EcellData data in obj.Value) { simulator.LoadEntityProperty( data.EntityPath, data.Value.Value); } // Initialize Entity foreach (EcellObject entity in obj.Children) { if (entity.Type.Equals(EcellObject.TEXT)) continue; bool isCreated = true; // 4 "EcellCoreLib" try { simulator.CreateEntity( entity.Classname, entity.FullID); } catch (Exception e) { errMsg += entity.FullID + ":" + e.Message + "\n"; Trace.WriteLine(e.ToString()); isCreated = false; isWarn = true; } foreach (EcellData data in entity.Value) { string entityPath = data.EntityPath; if (obj.Type.Equals(Constants.xpathVariable)) { if (isCreated == true) simulator.LoadEntityProperty(entityPath, data.Value.Value); } else { processPropertyDic[entityPath] = data.Value.Value; } } } } } // List<string> removeList = new List<string>(); foreach (KeyValuePair<string, object> pair in processPropertyDic) { try { simulator.LoadEntityProperty(pair.Key, pair.Value); } catch (WrappedException e) { isWarn = true; errMsg += pair.Key + ":" + e.Message + "\n"; } if (pair.Key.EndsWith(Constants.xpathVRL)) removeList.Add(pair.Key); } foreach (string entityPath in removeList) { processPropertyDic.Remove(entityPath); } if (isWarn) { modelObject.ErrMsg = errMsg; } }
/// <summary> /// Parses the "eml" formatted file. /// </summary> /// <param name="fileName">The "eml" formatted file</param> /// <param name="sim">Simulator instance</param> public static EcellModel Parse(string fileName, WrappedSimulator sim) { EmlReader reader = new EmlReader(fileName, sim); EcellModel model = reader.Parse(); InitializeModel(model, sim); return model; }
/// <summary> /// CreateLogger /// </summary> /// <param name="fullPathID">The FullPathID to set the logger.</param> /// <param name="isInitalize">The flag whether this logger is initialized.</param> /// <param name="sim">The current simulator.</param> /// <param name="loggerPolicy">The current logger policy.</param> private void CreateLogger(string fullPathID, bool isInitalize, WrappedSimulator sim, LoggerPolicy loggerPolicy) { if (m_loggerEntry.Contains(fullPathID)) return; if (m_currentProject.SimulationStatus == SimulationStatus.Run || m_currentProject.SimulationStatus == SimulationStatus.Suspended || isInitalize) { sim.CreateLogger(fullPathID, loggerPolicy.ReloadStepCount, loggerPolicy.ReloadInterval, Convert.ToBoolean((int)loggerPolicy.DiskFullAction), loggerPolicy.MaxDiskSpace); m_currentProject.LogableEntityPathDic[fullPathID] = m_currentProject.Model.FullID; } m_loggerEntry.Add(fullPathID); }
/// <summary> /// Creates a new "Eml" instance with no argument. /// </summary> /// <param name="filename">the EML file name.</param> /// <param name="sim">The current simulator.</param> public EmlReader(string filename, WrappedSimulator sim) { m_doc = new XmlDocument(); m_doc.Load(filename); m_modelID = Path.GetFileNameWithoutExtension(filename); }
/// <summary> /// Loads the "System" 2 the "ECellCoreLib". /// </summary> /// <param name="simulator">The simulator</param> /// <param name="systemList">The list of "System"</param> /// <param name="loggerList">The list of the "Logger"</param> /// <param name="initialCondition">The dictionary of initial condition.</param> /// <param name="setPropertyDic">The dictionary of simulation library.</param> private static void LoadSystem( WrappedSimulator simulator, List<EcellObject> systemList, List<string> loggerList, Dictionary<string, double> initialCondition, Dictionary<string, object> setPropertyDic) { Debug.Assert(systemList != null && systemList.Count > 0); bool existSystem = false; Dictionary<string, object> processPropertyDic = new Dictionary<string, object>(); foreach (EcellObject system in systemList) { if (system == null) continue; existSystem = true; if (!system.Key.Equals(Constants.delimiterPath)) { simulator.CreateEntity( system.Classname, system.FullID); } // 4 property if (system.Value == null || system.Value.Count <= 0) continue; foreach (EcellData ecellData in system.Value) { if (ecellData.Name == null || ecellData.Name.Length <= 0 || ecellData.Value == null) { continue; } EcellValue value = ecellData.Value; if (ecellData.Saveable) { simulator.LoadEntityProperty( ecellData.EntityPath, value.Value); } else if (ecellData.Settable) { setPropertyDic[ecellData.EntityPath] = value.Value; } if (ecellData.Logged) { loggerList.Add(ecellData.EntityPath); } } // 4 children if (system.Children == null || system.Children.Count <= 0) continue; LoadEntity( simulator, system.Children, loggerList, processPropertyDic, initialCondition, setPropertyDic); } if (processPropertyDic.Count > 0) { // The "VariableReferenceList" is previously loaded. string[] keys = null; processPropertyDic.Keys.CopyTo(keys = new string[processPropertyDic.Keys.Count], 0); foreach (string entityPath in keys) { if (entityPath.EndsWith(Constants.xpathVRL)) { simulator.LoadEntityProperty(entityPath, processPropertyDic[entityPath]); processPropertyDic.Remove(entityPath); } } foreach (string entityPath in processPropertyDic.Keys) { if (!entityPath.EndsWith("Fixed")) { simulator.LoadEntityProperty(entityPath, processPropertyDic[entityPath]); } } } Debug.Assert(existSystem); }
/// <summary> /// Loads the "Stepper" 2 the "EcellCoreLib". /// </summary> /// <param name="simulator">The simulator</param> /// <param name="stepperList">The list of the "Stepper"</param> /// <param name="setStepperDic">The list of stepper.</param> private static void LoadStepper( WrappedSimulator simulator, List<EcellObject> stepperList, Dictionary<string, Dictionary<string, object>> setStepperDic) { Debug.Assert(stepperList != null && stepperList.Count > 0); foreach (EcellObject stepper in stepperList) { if (stepper == null) continue; simulator.CreateStepper(stepper.Classname, stepper.Key); // 4 property if (stepper.Value == null || stepper.Value.Count <= 0) continue; foreach (EcellData ecellData in stepper.Value) { if (ecellData.Name == null || ecellData.Name.Length <= 0 || ecellData.Value == null) continue; else if (!ecellData.Value.IsDouble && !ecellData.Value.IsInt) continue; // 4 MaxStepInterval == Double.MaxValue EcellValue value = ecellData.Value; try { if ((double)value == Double.PositiveInfinity || (double)value == Double.MaxValue) continue; XmlConvert.ToDouble(value); } catch (Exception ex) { Trace.WriteLine(ex); continue; } if (value.IsDouble && (Double.IsInfinity((double)value) || Double.IsNaN((double)value))) continue; if (ecellData.Saveable) { simulator.LoadStepperProperty( stepper.Key, ecellData.Name, value.Value); } else if (ecellData.Settable) { if (!setStepperDic.ContainsKey(stepper.Key)) { setStepperDic[stepper.Key] = new Dictionary<string, object>(); } setStepperDic[stepper.Key][ecellData.Name] = value.Value; } } } }
/// <summary> /// Loads the "Process" and the "Variable" to the "EcellCoreLib". /// </summary> /// <param name="entityList">The list of the "Process" and the "Variable"</param> /// <param name="simulator">The simulator</param> /// <param name="loggerList">The list of the "Logger"</param> /// <param name="processPropertyDic">The dictionary of the process property</param> /// <param name="initialCondition">The dictionary of the initial condition</param> /// <param name="setPropertyDic">The dictionary of property.</param> private static void LoadEntity( WrappedSimulator simulator, List<EcellObject> entityList, List<string> loggerList, Dictionary<string, object> processPropertyDic, Dictionary<string, double> initialCondition, Dictionary<string, object> setPropertyDic) { if (entityList == null || entityList.Count <= 0) return; foreach (EcellObject entity in entityList) { if (entity is EcellText) continue; simulator.CreateEntity( entity.Classname, entity.FullID); if (entity.Value == null || entity.Value.Count <= 0) continue; foreach (EcellData ecellData in entity.Value) { EcellValue value = ecellData.Value; if (string.IsNullOrEmpty(ecellData.Name) || value == null || (value.IsString && ((string)value).Length == 0)) { continue; } if (ecellData.Logged) { loggerList.Add(ecellData.EntityPath); } if (value.IsDouble && (Double.IsInfinity((double)value) || Double.IsNaN((double)value))) { continue; } if (ecellData.Saveable) { if (ecellData.EntityPath.EndsWith(Constants.xpathVRL)) { processPropertyDic[ecellData.EntityPath] = value.Value; } else { if (ecellData.EntityPath.EndsWith("FluxDistributionList")) continue; simulator.LoadEntityProperty( ecellData.EntityPath, value.Value); } } else if (ecellData.Settable) { setPropertyDic[ecellData.EntityPath] = value.Value; } } } }
/// <summary> /// InitializeModel /// </summary> /// <param name="ecellObject">The model object.</param> /// <param name="simulator">The simulator.</param> internal void InitializeModel(EcellObject ecellObject, WrappedSimulator simulator) { // Sets the "EcellObject". string modelID = ecellObject.ModelID; string simParam = m_info.SimulationParam; if (ecellObject.Type.Equals(Constants.xpathModel)) { SetSimParams(modelID); m_modelList.Add((EcellModel)ecellObject); DataStorer.DataStored( simulator, m_env.DMDescriptorKeeper, ecellObject, m_initialCondition[simParam][modelID]); } else if (ecellObject.Type.Equals(Constants.xpathSystem)) { if (!m_systemDic.ContainsKey(modelID)) { m_systemDic[modelID] = new List<EcellObject>(); } m_systemDic[modelID].Add(ecellObject); } else if (ecellObject.Type.Equals(Constants.xpathStepper)) { if (!m_stepperDic.ContainsKey(modelID)) { m_stepperDic[modelID] = new List<EcellObject>(); } m_stepperDic[modelID].Add(ecellObject); } foreach (EcellObject childEcellObject in ecellObject.Children) { InitializeModel(childEcellObject, simulator); } }
/// <summary> /// Check DynamicProperty /// </summary> /// <param name="sim">The current simulator.</param> /// <param name="id">The proerty name.</param> /// <param name="pdescs">the list of proerty.</param> /// <returns></returns> private static bool CheckDynamicProperty(WrappedSimulator sim, string id, Dictionary<string, PropertyDescriptor> pdescs) { // Check DynamicProperty bool dynamic = true; try { string randomID = null; do { randomID = Util.GenerateRandomID(32); } while (pdescs.ContainsKey(randomID)); // Test Set property. if (id.EndsWith(EcellObject.STEPPER)) { sim.SetStepperProperty(id, randomID, 0.0); } else { string fullId = Util.BuildFullPN(id, randomID); sim.SetEntityProperty(fullId, 0.0); } } catch (Exception) { dynamic = false; } return dynamic; }
/// <summary> /// Creates the new "Project" instance with ProjectInfo. /// </summary> /// <param name="info">the project information object.</param> /// <param name="env">ApplicationEnvironment</param> public Project(ProjectInfo info, ApplicationEnvironment env) { if (info == null) throw new EcellException(string.Format(MessageResources.ErrInvalidParam, "ProjectInfo")); if (env == null) throw new EcellException(string.Format(MessageResources.ErrInvalidParam, "ApplicationEnvironment")); m_info = info; m_env = env; m_stepperDic = new Dictionary<string, List<EcellObject>>(); m_modelList = new List<EcellModel>(); m_systemDic = new Dictionary<string, List<EcellObject>>(); m_logableEntityPathDic = new Dictionary<string, string>(); m_loggerPolicyDic = new Dictionary<string, LoggerPolicy>(); // Loads the model. if (info.Type != ProjectType.Model) info.FindModels(); SetDMList(); m_simulator = CreateSimulatorInstance(); // Lock(); }
/// <summary> /// Get PropertyDescriptors /// </summary> /// <param name="sim">the current simulator.</param> /// <param name="fullID">the entity fullID.</param> /// <returns></returns> private static Dictionary<string, PropertyDescriptor> GetEntityPropertyDescriptors(WrappedSimulator sim, string fullID) { // Get default property values. Dictionary<string, PropertyDescriptor> pdescs = new Dictionary<string, PropertyDescriptor>(); foreach (string propName in sim.GetEntityPropertyList(fullID)) { string fullPN = Util.BuildFullPN(fullID, propName); PropertyAttributes attrs = sim.GetEntityPropertyAttributes(fullPN); EcellValue defaultValue = null; if (attrs.Gettable) { if (propName.Equals(Constants.xpathMolarActivity) || propName.Equals(Constants.xpathActivity)) defaultValue = new EcellValue(0.0); if (propName.Equals(Constants.xpathMolarConc) || propName.Equals(Constants.xpathNumberConc)) defaultValue = new EcellValue(0.0); try { object obj = sim.GetEntityProperty(fullPN); defaultValue = new EcellValue(obj); } catch (Exception) { Trace.WriteLine(string.Format(" Failed to get default value of property, {0}:{1}.", fullID, propName)); } } // Set default value. bool isMatrix = false; if (defaultValue == null && !propName.Contains("Matrix")) defaultValue = new EcellValue(0.0d); else if(propName.Contains("Matrix")) { defaultValue = new EcellValue(""); isMatrix =true; } bool logable = defaultValue.IsDouble; if (fullID.StartsWith(EcellObject.PROCESS)) logable = logable && (!attrs.Settable || !attrs.Savable); pdescs[propName] = new PropertyDescriptor( propName, attrs.Settable && !isMatrix, // settable attrs.Gettable, // gettable attrs.Loadable && !isMatrix, // loadable attrs.Savable && !isMatrix, // saveable attrs.Dynamic, // dynamic logable, // logable defaultValue ); } return pdescs; }
/// <summary> /// Reload the current simulator. /// </summary> public void ReloadSimulator() { UnloadSimulator(); m_simulator = CreateSimulatorInstance(); }
/// <summary> /// Get PropertyDescriptors /// </summary> /// <param name="sim">the curremt simulator.</param> /// <param name="stepper">the stepper name.</param> /// <returns></returns> private static Dictionary<string, PropertyDescriptor> GetStepperPropertyDescriptors(WrappedSimulator sim, string stepper) { // Get default property values. Dictionary<string, PropertyDescriptor> pdescs = new Dictionary<string, PropertyDescriptor>(); foreach (string propName in sim.GetStepperPropertyList(stepper)) { PropertyAttributes attrs = sim.GetStepperPropertyAttributes(stepper, propName); EcellValue defaultValue = null; if (attrs.Gettable) { try { defaultValue = new EcellValue(sim.GetStepperProperty(stepper, propName)); } catch (Exception) { Trace.WriteLine(string.Format(" Failed to get default value of property, {0}:{1}.", stepper, propName)); } } // Set default value. if (defaultValue == null) defaultValue = new EcellValue(0.0d); bool logable = defaultValue.IsDouble; pdescs[propName] = new PropertyDescriptor( propName, attrs.Settable, // settable attrs.Gettable, // gettable attrs.Loadable, // loadable attrs.Savable, // saveable attrs.Dynamic, // dynamic logable, // logable defaultValue ); } return pdescs; }
public void SetUp() { s = new WrappedSimulator( new string[] { GetDMDirectory() }); }
/// <summary> /// Load the entity DM. /// </summary> /// <param name="sim">the loaded simulator.</param> /// <param name="info">the DM information/</param> /// <param name="type">the type of DM.</param> /// <returns>DMDescriptor</returns> private static DMDescriptor LoadEntityDM(WrappedSimulator sim, DMModuleInfo info, string type) { DMDescriptor desc = null; try { Trace.WriteLine(" Checking properties for " + info.ModuleName); string id = Util.BuildFullID(type, "/", info.ModuleName); sim.CreateEntity(info.ModuleName, id); // Get PropertyDescriptors Dictionary<string, PropertyDescriptor> pdescs = GetEntityPropertyDescriptors(sim, id); // Check DynamicProperty bool dynamic = CheckDynamicProperty(sim, id, pdescs); desc = new DMDescriptor(info.ModuleName, info.Path, type, dynamic, pdescs); desc.Description = info.Description; } catch (Exception) { Trace.WriteLine("Failed to load " + info.ModuleName); //Trace.WriteLine(e.StackTrace); } return desc; }
/// <summary> /// Stores the "EcellObject" 4 the "Stepper". /// </summary> /// <param name="simulator">The simulator</param> /// <param name="dmm">The "DynamicModuleManager"</param> /// <param name="ecellObject">The stored "Stepper"</param> internal static void DataStored4Stepper( WrappedSimulator simulator, DMDescriptorKeeper dmm, EcellObject ecellObject) { string key = Constants.xpathStepper + Constants.delimiterColon + Constants.delimiterColon + ecellObject.Key; List<EcellData> stepperEcellDataList = new List<EcellData>(); IList<string> wrappedPolymorph = null; // // Property List // try { wrappedPolymorph = simulator.GetStepperPropertyList(ecellObject.Key); } catch (Exception ex) { Trace.WriteLine(ex.StackTrace); return; } // // Sets the class name. // if (string.IsNullOrEmpty(ecellObject.Classname)) { ecellObject.Classname = simulator.GetStepperClassName(ecellObject.Key); } // // Checks the stored "EcellData" // Dictionary<string, EcellData> storedEcellDataDic = new Dictionary<string, EcellData>(); if (ecellObject.Value != null && ecellObject.Value.Count > 0) { foreach (EcellData storedEcellData in ecellObject.Value) { storedEcellDataDic[storedEcellData.Name] = storedEcellData; stepperEcellDataList.Add(storedEcellData); } } // // Stores the "EcellData" // foreach (string name in wrappedPolymorph) { string entityPath = key + Constants.delimiterColon + name; PropertyAttributes flag = simulator.GetStepperPropertyAttributes(ecellObject.Key, name); if (!flag.Gettable) { continue; } EcellValue value = null; try { object property = simulator.GetStepperProperty(ecellObject.Key, name); value = new EcellValue(property); } catch (Exception ex) { Trace.WriteLine(ex); value = GetValueFromDMM(dmm, ecellObject.Type, ecellObject.Classname, name); } EcellData ecellData = CreateEcellData(name, value, entityPath, flag); if (storedEcellDataDic.ContainsKey(name)) { if (value.IsString && value.ToString().Equals("")) { continue; } else { stepperEcellDataList.Remove(storedEcellDataDic[name]); } } stepperEcellDataList.Add(ecellData); } ecellObject.SetEcellDatas(stepperEcellDataList); }
/// <summary> /// Parses the simulation parameter file. /// </summary> /// <param name="fileName">The simulation parameter file name</param> /// <param name="sim">The simulator.</param> public static SimulationParameter Parse(string fileName, WrappedSimulator sim) { XmlDocument doc = new XmlDocument(); doc.Load(fileName); return new SimulationParameterReader(doc, sim, Path.GetFileNameWithoutExtension(fileName)).Parse(); }
/// <summary> /// Stores the "EcellObject" 4 the "Variable". /// </summary> /// <param name="simulator">The simulator</param> /// <param name="ecellObject">The stored "Variable"</param> /// <param name="initialCondition">The initial condition.</param> internal static void DataStored4Variable( WrappedSimulator simulator, EcellObject ecellObject, Dictionary<string, double> initialCondition) { string fullID = ecellObject.FullID; IList<string> wrappedPolymorph = simulator.GetEntityPropertyList(fullID); // // Checks the stored "EcellData" // List<EcellData> variableEcellDataList = new List<EcellData>(); Dictionary<string, EcellData> storedEcellDataDic = new Dictionary<string, EcellData>(); if (ecellObject.Value != null && ecellObject.Value.Count > 0) { foreach (EcellData storedEcellData in ecellObject.Value) { storedEcellDataDic[storedEcellData.Name] = storedEcellData; variableEcellDataList.Add(storedEcellData); } } foreach (string name in wrappedPolymorph) { string entityPath = fullID + Constants.delimiterColon + name; PropertyAttributes flag = simulator.GetEntityPropertyAttributes(entityPath); if (!flag.Gettable) { continue; } EcellValue value = GetVariableValue(simulator, name, entityPath); EcellData ecellData = CreateEcellData(name, value, entityPath, flag); if (ecellData.Value != null) { ecellData.Logable = ecellData.Value.IsDouble; } if (storedEcellDataDic.ContainsKey(name)) { ecellData.Logged = storedEcellDataDic[name].Logged; variableEcellDataList.Remove(storedEcellDataDic[name]); } variableEcellDataList.Add(ecellData); } ecellObject.SetEcellDatas(variableEcellDataList); }
/// <summary> /// Load DMDescriptions. /// </summary> /// <param name="dmPaths">DM path.</param> private void LoadDMs(string[] dmPaths) { m_dmPaths = dmPaths; // Set dictionary. Dictionary<string, Dictionary<string, List<DMModuleInfo>>> maps = new Dictionary<string, Dictionary<string, List<DMModuleInfo>>>(); Dictionary<string, Dictionary<string, List<DMModuleInfo>>> modulesToLookup = new Dictionary<string, Dictionary<string, List<DMModuleInfo>>>(); maps[Constants.xpathSystem] = new Dictionary<string, List<DMModuleInfo>>(); maps[Constants.xpathStepper] = new Dictionary<string, List<DMModuleInfo>>(); maps[Constants.xpathProcess] = new Dictionary<string, List<DMModuleInfo>>(); maps[Constants.xpathVariable] = new Dictionary<string, List<DMModuleInfo>>(); // Look for built-in modules { const string dmPath = "<BUILTIN>"; Dictionary<string, List<DMModuleInfo>> perDirectoryModuleList = new Dictionary<string, List<DMModuleInfo>>(); perDirectoryModuleList[Constants.xpathSystem] = new List<DMModuleInfo>(); perDirectoryModuleList[Constants.xpathStepper] = new List<DMModuleInfo>(); perDirectoryModuleList[Constants.xpathProcess] = new List<DMModuleInfo>(); perDirectoryModuleList[Constants.xpathVariable] = new List<DMModuleInfo>(); modulesToLookup[dmPath] = perDirectoryModuleList; using (WrappedSimulator sim = new WrappedSimulator(new string[] { "" })) { foreach (DMInfo entry in sim.GetDMInfo()) { perDirectoryModuleList[entry.TypeName].Add( new DMModuleInfo(dmPath, entry)); } } } // Searches the DM paths foreach (string dmPath in m_dmPaths) { if (!Directory.Exists(dmPath)) continue; string[] modulePaths = Directory.GetFiles( dmPath, Constants.delimiterWildcard + Constants.FileExtDM ); if (modulePaths.Length == 0) continue; Dictionary<string, List<DMModuleInfo>> perDirectoryModuleList = new Dictionary<string, List<DMModuleInfo>>(); perDirectoryModuleList[Constants.xpathSystem] = new List<DMModuleInfo>(); perDirectoryModuleList[Constants.xpathStepper] = new List<DMModuleInfo>(); perDirectoryModuleList[Constants.xpathProcess] = new List<DMModuleInfo>(); perDirectoryModuleList[Constants.xpathVariable] = new List<DMModuleInfo>(); modulesToLookup[dmPath] = perDirectoryModuleList; using (WrappedSimulator sim = new WrappedSimulator(new string[] { dmPath })) { foreach (string modulePath in modulePaths) { string moduleName = Path.GetFileNameWithoutExtension(modulePath); string moduleType = GetModuleType(moduleName); if (moduleType == null) continue; // XXX: what are we supposed to do here? List<DMModuleInfo> infoList = null; maps[moduleType].TryGetValue(moduleName, out infoList); if (infoList == null) { infoList = new List<DMModuleInfo>(); maps[moduleType][moduleName] = infoList; } string description = null; try { description = sim.GetDescription(moduleName); } catch (Exception e) { Trace.WriteLine(e); } DMModuleInfo info = new DMModuleInfo(modulePath, moduleName, description); infoList.Add(info); perDirectoryModuleList[moduleType].Add(info); } } } Dictionary<string, Dictionary<string, DMDescriptor>> descs = new Dictionary<string, Dictionary<string, DMDescriptor>>(); descs[Constants.xpathSystem] = new Dictionary<string, DMDescriptor>(); descs[Constants.xpathProcess] = new Dictionary<string, DMDescriptor>(); descs[Constants.xpathVariable] = new Dictionary<string, DMDescriptor>(); descs[Constants.xpathStepper] = new Dictionary<string, DMDescriptor>(); foreach (KeyValuePair<string, Dictionary<string, List<DMModuleInfo>>> kv in modulesToLookup) { using (WrappedSimulator sim = new WrappedSimulator(new string[] { kv.Key })) { { sim.CreateStepper("PassiveStepper", "tmp"); string id = Util.BuildFullPN(Constants.xpathSystem, "", "/", "StepperID"); sim.SetEntityProperty(id, "tmp"); } Trace.WriteLine("Checking DMs in " + kv.Key); // Test System DMs. foreach (string kind in new String[] { Constants.xpathSystem, Constants.xpathProcess, Constants.xpathVariable }) { foreach (DMModuleInfo info in kv.Value[kind]) { descs[kind][info.ModuleName] = LoadEntityDM(sim, info, kind); } } // Test Stepper DMs. foreach (DMModuleInfo info in kv.Value[Constants.xpathStepper]) { descs[Constants.xpathStepper][info.ModuleName] = LoadStepperDM(sim, info); } } } m_descs = descs; }
/// <summary> /// Load the stepper DM. /// </summary> /// <param name="sim">the loaded simulator.</param> /// <param name="info">DM information.</param> /// <returns>DMDescriptor.</returns> private static DMDescriptor LoadStepperDM(WrappedSimulator sim, DMModuleInfo info) { DMDescriptor desc = null; try { Trace.WriteLine(" Checking properties for " + info.ModuleName); string stepper = info.ModuleName; sim.CreateStepper(stepper, stepper); // Get PropertyDescriptors Dictionary<string, PropertyDescriptor> pdescs = GetStepperPropertyDescriptors(sim, stepper); // Check DynamicProperty bool dynamic = CheckDynamicProperty(sim, stepper, pdescs); desc = new DMDescriptor(stepper, info.Path, Constants.xpathStepper, dynamic, pdescs); desc.Description = info.Description; } catch (Exception) { Trace.WriteLine("Failed to load " + info.ModuleName); //Trace.WriteLine(e.StackTrace); } return desc; }
/// <summary> /// LoadSBML /// </summary> /// <param name="filename">the SBML file name.</param> public void LoadSBML(string filename) { WrappedSimulator sim = null; try { CloseProject(); m_env.PluginManager.ChangeStatus(ProjectStatus.Loading); // Load model EcellModel model = SBML2EML.Convert(filename); // Initialize try { sim = new WrappedSimulator(Util.GetDMDirs()); EmlReader.InitializeModel(model, sim); sim.Initialize(); } catch (Exception e) { if (m_env.PluginManager.DockPanel != null) Util.ShowWarningDialog(MessageResources.WarnInvalidData + "\n" + e.Message); Trace.WriteLine(e.ToString()); } // Create Project string projectID = model.ModelID; string comment = "Convert from SBML: " + Path.GetFileName(filename); ProjectInfo info = new ProjectInfo(projectID, comment, DateTime.Now.ToString(), Constants.defaultSimParam); info.Type = ProjectType.SBML; Project project = new Project(info, m_env); project.LoggerPolicyDic[Constants.defaultSimParam] = new LoggerPolicy(); m_currentProject = project; project.Simulator = sim; project.InitializeModel(model, sim); // Load model List<EcellObject> passList = new List<EcellObject>(); List<EcellData> ecellDataList = new List<EcellData>(); ecellDataList.Add(new EcellData(Constants.textComment, new EcellValue(comment), null)); passList.Add(EcellObject.CreateObject(projectID, "", Constants.xpathProject, "", ecellDataList)); passList.Add(model); passList.AddRange(model.Children); m_env.PluginManager.DataAdd(passList); m_env.PluginManager.ParameterSet(projectID, Constants.defaultSimParam); m_env.PluginManager.ChangeStatus(ProjectStatus.Loaded); } catch (Exception e) { CloseProject(); m_env.Console.WriteLine("Failed to convert SBML: " + filename); m_env.Console.WriteLine(e.ToString()); throw new EcellException("Failed to convert SBML. " + filename, e); } }