From 9b501f29f6ed0e250a82be0a7ada82a1025fedd4 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 30 Dec 2024 23:23:07 -0500 Subject: [PATCH] improve the .NET-to-Mocha object modeling system --- .../src/lib/Mocha.Core/Modeling/OmsClass.cs | 28 +++++++++++++-- .../Mocha.Core/Modeling/OmsInstanceList.cs | 36 ++++++++++++++++--- .../SampleDatabases/VehicleForHireTests.cs | 3 ++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsClass.cs b/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsClass.cs index f92a4b2..8f8a658 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsClass.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsClass.cs @@ -15,6 +15,7 @@ // You should have received a copy of the GNU General Public License // along with Mocha.NET. If not, see . +using System.ComponentModel; using System.Diagnostics; using System.Reflection; using MBS.Core.Reflection; @@ -23,9 +24,15 @@ namespace Mocha.Core.Modeling; public class OmsClass { - internal OmsDatabase omsdb { get; set; } - public Guid GlobalIdentifier { get; internal set; } = Guid.Empty; - + internal OmsDatabase omsdb { get; private set; } + private Guid _GlobalIdentifier = Guid.Empty; + public Guid GlobalIdentifier + { + get + { + return _GlobalIdentifier; + } + } private Dictionary _attributeValuesTemp = new Dictionary(); protected object GetAttributeValue(object defaultValue = null) @@ -99,4 +106,19 @@ public class OmsClass } throw new InvalidOperationException(); } + + internal void Initialize(OmsDatabase omsdb, Guid localInstanceGuid) + { + this.omsdb = omsdb; + this._GlobalIdentifier = localInstanceGuid; + + InstanceHandle inst = omsdb.Oms.GetInstance(_GlobalIdentifier); + if (inst != InstanceHandle.Empty) + { + foreach (KeyValuePair kvp in _attributeValuesTemp) + { + omsdb.Oms.SetAttributeValue(inst, omsdb.Oms.GetInstance(kvp.Key), kvp.Value); + } + } + } } \ No newline at end of file diff --git a/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsInstanceList.cs b/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsInstanceList.cs index 1a39492..eef5f29 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsInstanceList.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/Modeling/OmsInstanceList.cs @@ -85,7 +85,28 @@ public class OmsInstanceList : OmsInstanceList, IList where T : OmsClass this.classGuid = classGuid; } - public T this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public T this[int index] + { + get + { + IEnumerable insts = omsdb.Oms.GetInstancesOf(omsdb.Oms.GetInstance(classGuid)); + + int i = 0; + foreach (InstanceHandle ih in insts) + { + if (i == index) + { + return (T)omsdb.GetClass(ih); + } + i++; + } + throw new IndexOutOfRangeException(); + } + set + { + + } + } public int Count { @@ -104,10 +125,9 @@ public class OmsInstanceList : OmsInstanceList, IList where T : OmsClass { Guid localClassGuid = OmsDatabase.GetGlobalIdentifierForClass(item.GetType()); Guid localInstanceGuid = Guid.NewGuid(); - item.GlobalIdentifier = localInstanceGuid; - + omsdb.CreateClass(item, localClassGuid, localInstanceGuid); - item.omsdb = omsdb; + item.Initialize(omsdb, localInstanceGuid); } } @@ -123,7 +143,13 @@ public class OmsInstanceList : OmsInstanceList, IList where T : OmsClass public void CopyTo(T[] array, int arrayIndex) { - throw new NotImplementedException(); + IEnumerable insts = omsdb.Oms.GetInstancesOf(omsdb.Oms.GetInstance(classGuid)); + int i = 0; + foreach (InstanceHandle ih in insts) + { + array[i + arrayIndex] = (T)omsdb.GetClass(ih); + i++; + } } public IEnumerator GetEnumerator() diff --git a/mocha-dotnet/tests/Mocha.Core.Tests/SampleDatabases/VehicleForHireTests.cs b/mocha-dotnet/tests/Mocha.Core.Tests/SampleDatabases/VehicleForHireTests.cs index 1203e73..27414a6 100644 --- a/mocha-dotnet/tests/Mocha.Core.Tests/SampleDatabases/VehicleForHireTests.cs +++ b/mocha-dotnet/tests/Mocha.Core.Tests/SampleDatabases/VehicleForHireTests.cs @@ -115,6 +115,9 @@ public class VehicleForHireTests : OmsModelingTestsBase VehicleForHireDB.Business busn = new VehicleForHireDB.Business("Ryde Rentals Transportation"); db.Businesses.Add(busn); + var query1 = from b in db.Businesses select b; + Assert.That(query1.Count(), Is.EqualTo(1)); + var query2 = from b in db.Businesses where b.Name == "test" select b; Assert.That(query2.Count(), Is.EqualTo(0));