/// <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> /// 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> /// 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> /// 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); }
public void TestWrappedSimulator() { _unitUnderTest.UnloadSimulator(); // When you do not carry out Initialize, Dispose is available. WrappedSimulator sim1 = new WrappedSimulator(_unitUnderTest.GetDMDirs()); EmlReader.Parse(TestConstant.Model_RBC, sim1); Console.WriteLine(sim1.GetEntityProperty("System::/:Name")); sim1.Dispose(); // when I carry out Initialize, Dispose returns error. WrappedSimulator sim2 = new WrappedSimulator(_unitUnderTest.GetDMDirs()); EmlReader.Parse(TestConstant.Model_RBC, sim2); Console.WriteLine(sim2.GetEntityProperty("System::/:Name")); try { sim2.Initialize(); sim2.Dispose(); } catch (Exception e) { Trace.WriteLine(e.ToString()); Assert.Fail(e.Message); } }