public IVersionedObject Add(IVersionedObject @object, bool ignoreDublicates = false)
        {
            if (_transactionState != TransactionState.Running)
            {
                throw new OperationCanceledException($"Cannot add objects when transaction state = {_transactionState.ToString() }");
            }

            if (_objectManager.GetObject(@object.Id) != null)
            {
                if (ignoreDublicates)
                {
                    return(@object);
                }
                else
                {
                    throw new OperationCanceledException($"An object with id: {@object.Id} is already present in the object manager. If you want to update an existing object, please use Update instead.");
                }
            }

            if (_addedObjects.ContainsKey(@object.Id))
            {
                if (ignoreDublicates)
                {
                    return(@object);
                }
                else
                {
                    throw new OperationCanceledException($"An object with id: {@object.Id} is already added in this transaction.");
                }
            }

            // Check if pointer to same object has been added before
            if (_objectManager._objectAdditionStates.ContainsKey(@object.Id))
            {
                var objVersions = _objectManager._objectAdditionStates[@object.Id];

                if (objVersions.Contains(@object))
                {
                    throw new OperationCanceledException($"An object with id: {@object.Id} referencing the same object already exists inside the object manager. You cannot add the same object instance twice. You need to create a new object instance everytime you add an object.");
                }
            }

            _addedObjects.Add(@object.Id, @object);

            return(@object);
        }
Example #2
0
        public void AddingObject_TestGetObject()
        {
            var manager = new InMemoryObjectManager();

            var firstTransaction = manager.CreateTransaction("custom version id");

            var objToAdd = new TestObjectA(Guid.NewGuid(), "hej");

            firstTransaction.Add(objToAdd);
            firstTransaction.Commit();

            // Asset that GetObject(guid id) workd
            Assert.Equal(objToAdd, manager.GetObject(objToAdd.Id));

            // Asset that GetObject(guid id, long version id) works
            Assert.Equal(objToAdd, manager.GetObject(objToAdd.Id, 1));

            // Asset that GetObject(custom, string version id ) works
            Assert.Equal(objToAdd, manager.GetObject(objToAdd.Id, "custom version id"));
        }
        public IRouteNetworkElement?GetRouteNetworkElement(Guid id)
        {
            if (_loadMode && _loadModeTransaction != null)
            {
                return(_loadModeTransaction.GetObject(id) as IRouteNetworkElement);
            }
            else if (_cmdTransaction != null)
            {
                var transObj = _cmdTransaction.GetObject(id);

                if (transObj != null)
                {
                    return(transObj as IRouteNetworkElement);
                }
                else
                {
                    return(_objectManager.GetObject(id) as IRouteNetworkElement);
                }
            }
            else
            {
                return(GetRouteNetworkElement(id, GetLatestCommitedVersion()));
            }
        }
        public void CreateTransactionWithCustomId_TestGetObject()
        {
            var manager = new InMemoryObjectManager();

            var myCustomId = "my custom version id 1";

            var firstTransaction = manager.CreateTransaction(myCustomId);

            var objToAdd = new TestObjectA(Guid.NewGuid(), "hej");

            firstTransaction.Add(objToAdd);
            firstTransaction.Commit();


            // Asser that GetObjects work with custom ids
            var objects = manager.GetObjects(myCustomId);

            Assert.Single(objects);

            var obj = manager.GetObject(objToAdd.Id, myCustomId);

            Assert.Equal(objToAdd, obj);
        }
        public IVersionedObject?GetObject(Guid id)
        {
            if (_loadMode && _loadModeTransaction != null)
            {
                return(_loadModeTransaction.GetObject(id));
            }
            else if (_cmdTransaction != null)
            {
                var transObj = _cmdTransaction.GetObject(id);

                if (transObj != null)
                {
                    return(transObj);
                }
                else
                {
                    return(_objectManager.GetObject(id));
                }
            }
            else
            {
                return(null);
            }
        }
        internal void ApplySegmentConnect(SpanEquipment spanEquipment, SpanSegmentToSimpleTerminalConnectInfo spanSegmentToConnect)
        {
            if (!_graphElementsById.TryGetValue(spanSegmentToConnect.SegmentId, out var existingSegmentGraphElement))
            {
                throw new ApplicationException($"Cannot find span segment graph element with id: {spanSegmentToConnect.SegmentId} processing segment to terminal connection.");
            }

            if (!spanEquipment.TryGetSpanSegment(spanSegmentToConnect.SegmentId, out var spanSegmentWithIndexInfo))
            {
                throw new ApplicationException($"Cannot find span segment with id: {spanSegmentToConnect.SegmentId} in span equipment with id: {spanEquipment.Id} ");
            }

            var version = _objectManager.GetLatestCommitedVersion();

            var trans = _objectManager.CreateTransaction();

            try
            {
                // Find or create terminal
                var terminalToConnect = _objectManager.GetObject(spanSegmentToConnect.TerminalId, version) as UtilityGraphConnectedTerminal;

                if (terminalToConnect == null)
                {
                    var terminalNodeOfInterestId = spanSegmentToConnect.ConnectionDirection == SpanSegmentToTerminalConnectionDirection.FromSpanSegmentToTerminal ? spanEquipment.NodesOfInterestIds[spanSegmentWithIndexInfo.SpanSegment.ToNodeOfInterestIndex] : spanEquipment.NodesOfInterestIds[spanSegmentWithIndexInfo.SpanSegment.FromNodeOfInterestIndex];
                    terminalToConnect = new UtilityGraphConnectedTerminal(spanSegmentToConnect.TerminalId, terminalNodeOfInterestId);
                    trans.Add(terminalToConnect);
                }

                // If segment has never been connected before
                if (existingSegmentGraphElement is UtilityGraphDisconnectedSegment)
                {
                    var fromNodeToConnect = spanSegmentToConnect.ConnectionDirection == SpanSegmentToTerminalConnectionDirection.FromTerminalToSpanSegment ? terminalToConnect : null;
                    var toNodeToConnect   = spanSegmentToConnect.ConnectionDirection == SpanSegmentToTerminalConnectionDirection.FromSpanSegmentToTerminal ? terminalToConnect : null;

                    var newSegmentGraphElement = new UtilityGraphConnectedSegment(spanSegmentToConnect.SegmentId, fromNodeToConnect, toNodeToConnect, spanEquipment.Id, spanSegmentWithIndexInfo.StructureIndex, spanSegmentWithIndexInfo.SegmentIndex);

                    trans.Add(newSegmentGraphElement);

                    UpdateDict(spanSegmentToConnect.SegmentId, newSegmentGraphElement);
                }
                // We're dealing with update to a segment already connected
                else
                {
                    if (spanSegmentToConnect.ConnectionDirection == SpanSegmentToTerminalConnectionDirection.FromSpanSegmentToTerminal)
                    {
                        var existingFromTerminal = (UtilityGraphConnectedTerminal)((UtilityGraphConnectedSegment)existingSegmentGraphElement).InV(version);

                        var newSegmentGraphElement = new UtilityGraphConnectedSegment(spanSegmentToConnect.SegmentId, existingFromTerminal, terminalToConnect, spanEquipment.Id, spanSegmentWithIndexInfo.StructureIndex, spanSegmentWithIndexInfo.SegmentIndex);
                        trans.Update(newSegmentGraphElement);

                        UpdateDict(spanSegmentToConnect.SegmentId, newSegmentGraphElement);
                    }
                    else
                    {
                        var existingToTerminal = (UtilityGraphConnectedTerminal)((UtilityGraphConnectedSegment)existingSegmentGraphElement).OutV(version);

                        var newSegmentGraphElement = new UtilityGraphConnectedSegment(spanSegmentToConnect.SegmentId, terminalToConnect, existingToTerminal, spanEquipment.Id, spanSegmentWithIndexInfo.StructureIndex, spanSegmentWithIndexInfo.SegmentIndex);
                        trans.Update(newSegmentGraphElement);

                        UpdateDict(spanSegmentToConnect.SegmentId, newSegmentGraphElement);
                    }
                }
            }
            finally
            {
                trans.Commit();
            }
        }