improve the .NET-to-Mocha object modeling system

This commit is contained in:
Michael Becker 2024-12-30 23:23:07 -05:00
parent c268310f39
commit 9b501f29f6
3 changed files with 59 additions and 8 deletions

View File

@ -15,6 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
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<Guid, object> _attributeValuesTemp = new Dictionary<Guid, object>();
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<Guid, object> kvp in _attributeValuesTemp)
{
omsdb.Oms.SetAttributeValue(inst, omsdb.Oms.GetInstance(kvp.Key), kvp.Value);
}
}
}
}

View File

@ -85,7 +85,28 @@ public class OmsInstanceList<T> : OmsInstanceList, IList<T> 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<InstanceHandle> 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<T> : OmsInstanceList, IList<T> 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<T> : OmsInstanceList, IList<T> where T : OmsClass
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
IEnumerable<InstanceHandle> 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<T> GetEnumerator()

View File

@ -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));