creating and selecting via LINQ works now

This commit is contained in:
Michael Becker 2024-07-22 08:24:07 -04:00
parent d69e83c974
commit 8e1a5a7438
Signed by: beckermj
GPG Key ID: 24F8DAA73DCB2C8F
4 changed files with 131 additions and 45 deletions

View File

@ -243,4 +243,28 @@ public class OmsDatabase
}
}
}
private Dictionary<InstanceHandle, OmsClass> _classesByHandle = new Dictionary<InstanceHandle, OmsClass>();
private Dictionary<OmsClass, InstanceHandle> _handlesByClass = new Dictionary<OmsClass, InstanceHandle>();
public OmsClass GetClass(InstanceHandle handle)
{
if (_classesByHandle.ContainsKey(handle))
{
return _classesByHandle[handle];
}
return null;
}
internal void CreateClass(OmsClass item, Guid localClassGuid, Guid localInstanceGuid)
{
InstanceHandle ih = Oms.CreateInstanceOf(Oms.GetInstance(localClassGuid), localInstanceGuid);
RegisterClass(item, ih);
}
private void RegisterClass(OmsClass item, InstanceHandle ih)
{
_classesByHandle[ih] = item;
_handlesByClass[item] = ih;
}
}

View File

@ -19,15 +19,58 @@ public class OmsInstanceList<T> : OmsInstanceList, IList<T> where T : OmsClass
private OmsDatabase omsdb;
private Guid classGuid;
public class Enumerator : IEnumerator<T>
{
protected T GetCurrent()
{
InstanceHandle _Current = _listEnumerator.Current;
if (_Current == InstanceHandle.Empty)
{
return null;
}
return (T) omsdb.GetClass(_Current);
}
public T Current => GetCurrent();
object IEnumerator.Current => GetCurrent();
private IEnumerable<InstanceHandle> _list;
private IEnumerator<InstanceHandle> _listEnumerator;
private OmsDatabase omsdb;
public Enumerator(OmsDatabase omsdb, IEnumerable<InstanceHandle> list)
{
this.omsdb = omsdb;
_list = list;
_listEnumerator = _list.GetEnumerator();
}
public void Dispose()
{
_listEnumerator.Dispose();
}
public bool MoveNext()
{
return _listEnumerator.MoveNext();
}
public void Reset()
{
_listEnumerator.Reset();
}
}
public OmsInstanceList(OmsDatabase omsdb, Guid classGuid)
{
this.omsdb = omsdb;
this.classGuid = classGuid;
}
public T this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public T this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public int Count
public int Count
{
get
{
@ -36,61 +79,62 @@ public class OmsInstanceList<T> : OmsInstanceList, IList<T> where T : OmsClass
}
}
public bool IsReadOnly => throw new NotImplementedException();
public bool IsReadOnly => throw new NotImplementedException();
public void Add(T item)
{
public void Add(T item)
{
if (item.GlobalIdentifier == Guid.Empty)
{
Guid localClassGuid = OmsDatabase.GetGlobalIdentifierForClass(item.GetType());
Guid localInstanceGuid = Guid.NewGuid();
omsdb.Oms.CreateInstanceOf(omsdb.Oms.GetInstance(localClassGuid), localInstanceGuid);
omsdb.CreateClass(item, localClassGuid, localInstanceGuid);
}
}
}
public void Clear()
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
IReadOnlyCollection<InstanceHandle> ihs = omsdb.Oms.GetInstancesOf(omsdb.Oms.GetInstance(classGuid));
return new Enumerator(omsdb, ihs);
}
public int IndexOf(T item)
{
throw new NotImplementedException();
}
public int IndexOf(T item)
{
throw new NotImplementedException();
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}

View File

@ -628,4 +628,10 @@ public abstract class Oms
//!FIXME: remove when we have sibling relationships
AssignRelationship(inheritsFrom, GetInstance(KnownRelationshipGuids.Class__has_sub__Class), classInstance);
}
public IReadOnlyCollection<InstanceHandle> GetInstancesOf(InstanceHandle classInstance)
{
IReadOnlyCollection<InstanceHandle> insts = GetRelatedInstances(classInstance, GetInstance(KnownRelationshipGuids.Class__has__Instance));
return insts;
}
}

View File

@ -85,12 +85,24 @@ public class VehicleForHireTests : OmsModelingTestsBase
}
[Test]
public void RelationshipsCreated() {
public void RelationshipsCreated()
{
CheckRelationshipExists(new Guid("{867126b9-294e-454f-b9c5-88df44353014}"), new Guid("{4b6f186c-babe-4c42-840b-3840373f68c2}"), "has", new Guid("{38a492b9-901f-4074-af45-642e812b51f5}"));
}
[Test]
public void CreateBusinessAndTestLinq()
{
VehicleForHireDB db = (VehicleForHireDB) GetDatabase();
VehicleForHireDB.Business busn = new VehicleForHireDB.Business("Ryde Rentals Transportation");
db.Businesses.Add(busn);
var query2 = from b in db.Businesses where b.Name == "test" select b;
Assert.That(query2.Count(), Is.EqualTo(0));
var query3 = from b in db.Businesses where b.Name == "Ryde Rentals Transportation" select b;
var busn3 = query3.First();
Assert.That(busn3.GlobalIdentifier, Is.EqualTo(busn.GlobalIdentifier));
}
}