/// <summary> /// Test which should be called for all settable properties, /// in order to verify functionality of IAccess methods. /// </summary> /// <param name="propertyId">property ID of settable property</param> public virtual void SettablePropertiesTest(ModelCode propertyId) { Assert.IsTrue(entity.HasProperty(propertyId)); Property initialValue = generator.Generate(propertyId); Assert.NotNull(initialValue); entity.SetProperty(initialValue); Property currentValue = entity.GetProperty(propertyId); Assert.AreEqual(initialValue, currentValue); }
public static T ConvertTo <T>(ResourceDescription rd) where T : IdentifiedObject { IdentifiedObject io = CreateEntity(rd.Id); foreach (Property property in rd.Properties) { if (property.Id == ModelCode.IDOBJ_GID) { continue; } if (property.Type == PropertyType.ReferenceVector) { continue; } io.SetProperty(property); } return((T)io); }
/// <summary> /// Update entiteta u kopiji /// </summary> /// <param name="rd">Resource description</param> private void UpdateEntity(ResourceDescription rd) { if (rd == null || rd.Properties == null && rd.Properties.Count == 0) { CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Update entity is not done because update operation is empty."); return; } try { long globalId = rd.Id; CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Updating entity with GID ({0:x16}).", globalId); if (!this.EntityExists(original, globalId)) { string message = String.Format("Failed to update entity because entity with specified GID ({0:x16}) does not exist in network model.", globalId); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } //Dobijam tip objekta koji se ubacuje iz njegovog globalnog identifikatora DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); //Pravim Shallow kopiju kontejnera tako sto napravim novi kontejner i u njega prevezem sve reference iz originala //Ideja: Sve sto dodirnem kopiram Container containerCopy = ConatinerShallowCopy(type); //Dobijam postojeci objekat koji je isti i za kopiju i za original IdentifiedObject io = GetEntity(copy, globalId); //Vrsi se deep kopi tog objekta IdentifiedObject copyIo = io.DeepCopy(); //Na mesto original objekta u copy kontejneru ubacujem kopiju tog objekta copy[type].Entities[io.GlobalId] = copyIo; //Prolazimo kroz sve properties i radimo njihov update foreach (Property property in rd.Properties) { //Prvo proveravamo da li je property referenca if (property.Type == PropertyType.Reference) { long oldTargetGlobalId = copyIo.GetProperty(property.Id).AsReference(); if (oldTargetGlobalId != 0) { //Pronadjem objekat sa kojim je objekat povezan IdentifiedObject oldTargetEntity = GetEntity(copy, oldTargetGlobalId); //Pravi se kopija target entiteta IdentifiedObject copyOldTargetEntity = oldTargetEntity.DeepCopy(); //Dobijem kontejner target elementa uradim ShallowCopy kontejnera DMSType typeTarget = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(oldTargetEntity.GlobalId); ConatinerShallowCopy(typeTarget); //Zamena originalnog entiteta sa kopijom u novom kontejneru copy[typeTarget].Entities[copyOldTargetEntity.GlobalId] = copyOldTargetEntity; //Ukolonim tu referencu copyOldTargetEntity.RemoveReference(property.Id, globalId); } //Postavim id nove reference long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { if (!EntityExists(copy, targetGlobalId)) { string message = string.Format("Failed to get target entity with GID: 0x{0:X16}.", targetGlobalId); throw new Exception(message); } //Pronadjem novi obejak na koji je povezan i setujem mu referencu IdentifiedObject targetEntity = GetEntity(copy, targetGlobalId); //Pravii se kopija target entiteta IdentifiedObject copyTargetEntity = targetEntity.DeepCopy(); //Dobijem kontejner target elementa uradim ShallowCopy kontejnera DMSType typeTarget = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(targetEntity.GlobalId); ConatinerShallowCopy(typeTarget); //Zamena originalnog entiteta sa kopijom u novom kontejneru copy[typeTarget].Entities[targetEntity.GlobalId] = copyTargetEntity; copyTargetEntity.AddReference(property.Id, globalId); } //Odradi update ostalih properties copyIo.SetProperty(property); } else { //Odradi update ostalih properties copyIo.SetProperty(property); } } CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Updating entity with GID ({0:x16}) successfully finished.", globalId); } catch (Exception ex) { string message = String.Format("Failed to update entity (GID = 0x{0:x16}) in model. {1} ", rd.Id, ex.Message); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } }
/// <summary> /// Insert entiteta u kopiju /// </summary> /// <param name="rd">Resource description</param> private void InsertEntity(ResourceDescription rd) { string message = ""; if (rd == null) { message = String.Format("Insert entity is not done because update operation is empty."); LogHelper.Log(LogTarget.File, LogService.NMSSmartContainer, " ERROR - SmartContainer.cs - " + message); throw new Exception(message); } long globalId = rd.Id; CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Inserting entity with GID ({0:x16}).", globalId); //Proveravam da li postoji entitet koji zelim da dodam u originalnom modelu ako postoji, bacam izuzetak if (this.EntityExists(original, globalId)) { message = String.Format("Failed to insert entity because entity with specified GID ({0:x16}) already exists in network model.", globalId); LogHelper.Log(LogTarget.File, LogService.NMSSmartContainer, " ERROR - SmartContainer.cs - " + message); throw new Exception(message); } try { //Dobijam tip objekta koji se ubacuje iz njegovog globalnog identifikatora DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); //Pravim Shallow kopiju kontejnera tako sto napravim novi kontejner i u njega prevezem sve reference iz originala //Ideja: Sve sto dodirnem kopiram Container containerCopy = ConatinerShallowCopy(type); //Pravim objekat DMS tipa, to moze biti region, subregion, substation u zavisnosti sta ubacujem -> ne podesavam propertije //Taj objekat unutar metode CreateEntity ubacujem u kopiju kontejnera //Sada kopija ima veze prema svim entitetima iz originalnog kontejnera plus novi objekat IdentifiedObject io = containerCopy.CreateEntity(globalId); //Ako nemamo properties onda smo zavrsili i napustamo i napustamo metodu InsertEntity if (rd.Properties == null) { return; } foreach (Property property in rd.Properties) { //GID se podesi kroz konstruktor tako da ga sada ne podesavamo if (property.Id == ModelCode.IDOBJ_GID) { continue; } //Provera da li postoji entitet sa istim mrid-om, ako postoji ne prolazi test if (property.Id == ModelCode.IDOBJ_MRID) { if (this.ContainsMrid(original, globalId, property.PropertyValue.StringValue)) { message = String.Format("Failed to insert entity because entity with specified GID ({0:x16}) already exists in network model.", globalId); LogHelper.Log(LogTarget.File, LogService.NMSSmartContainer, " ERROR - SmartContainer.cs - " + message); throw new Exception(message); } } //Provera da li je property referenca, ako nije postavi property i uzmi sledeci if (property.Type != PropertyType.Reference) { io.SetProperty(property); continue; } //Ako je referenca //Uzmemo id objekta na koga se property odnosi long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { //Provera da li postoji objekat koji je referenciran od strane trenutnog propertija //Ako ne postoji onda je to greska if (!EntityExists(copy, targetGlobalId)) { message = string.Format("Failed to get target entity with GID: 0x{0:X16}. {1}", targetGlobalId); LogHelper.Log(LogTarget.File, LogService.NMSSmartContainer, " ERROR - SmartContainer.cs - " + message); throw new Exception(message); } //Ako postoji IdentifiedObject targetEntity = GetEntity(copy, targetGlobalId); targetEntity.AddReference(property.Id, io.GlobalId); } io.SetProperty(property); } message = String.Format("Inserting entity with GID ({0:x16}) successfully finished.", globalId); LogHelper.Log(LogTarget.File, LogService.NMSSmartContainer, " INFO - SmartContainer.cs - " + message); } catch (Exception ex) { message = String.Format("Failed to insert entity (GID = 0x{0:x16}) into model. {1}", rd.Id, ex.Message); LogHelper.Log(LogTarget.File, LogService.NMSSmartContainer, " ERROR - SmartContainer.cs - " + message); throw new Exception(message); } }
/// <summary> /// Updates entity in block model. /// </summary> /// <param name="rd">Description of the resource that should be updated</param> private void UpdateEntity(ResourceDescription rd) { if (rd == null || rd.Properties == null && rd.Properties.Count == 0) { CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Update entity is not done because update operation is empty."); return; } try { long globalId = rd.Id; CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Updating entity with GID ({0:x16}).", globalId); if (!this.EntityExists(globalId)) { string message = String.Format("Failed to update entity because entity with specified GID ({0:x16}) does not exist in network model.", globalId); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } IdentifiedObject io = GetEntity(globalId); // updating properties of entity foreach (Property property in rd.Properties) { if (property.Type == PropertyType.Reference) { long oldTargetGlobalId = io.GetProperty(property.Id).AsReference(); if (oldTargetGlobalId != 0) { IdentifiedObject oldTargetEntity = GetEntity(oldTargetGlobalId); oldTargetEntity.RemoveReference(property.Id, globalId); } // updating reference of entity long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { if (!EntityExists(targetGlobalId)) { string message = string.Format("Failed to get target entity with GID: 0x{0:X16}.", targetGlobalId); throw new Exception(message); } IdentifiedObject targetEntity = GetEntity(targetGlobalId); targetEntity.AddReference(property.Id, globalId); } // update value of the property in specified entity io.SetProperty(property); } else { // update value of the property in specified entity io.SetProperty(property); } } CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Updating entity with GID ({0:x16}) successfully finished.", globalId); } catch (Exception ex) { string message = String.Format("Failed to update entity (GID = 0x{0:x16}) in model. {1} ", rd.Id, ex.Message); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } }
/// <summary> /// Inserts entity into the network model. /// </summary> /// <param name="rd">Description of the resource that should be inserted</param> private void InsertEntity(ResourceDescription rd) { if (rd == null) { CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Insert entity is not done because update operation is empty."); return; } long globalId = rd.Id; CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Inserting entity with GID ({0:x16}).", globalId); // check if mapping for specified global id already exists if (this.EntityExists(globalId)) { string message = String.Format("Failed to insert entity because entity with specified GID ({0:x16}) already exists in network model.", globalId); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } try { // find type DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); Container container = null; // get container or create container if (ContainerExists(type)) { container = GetContainer(type); } else { container = new Container(); networkDataModel.Add(type, container); } // create entity and add it to container IdentifiedObject io = container.CreateEntity(globalId); // apply properties on created entity if (rd.Properties != null) { foreach (Property property in rd.Properties) { // globalId must not be set as property if (property.Id == ModelCode.IDOBJ_GID) { continue; } if (property.Type == PropertyType.Reference) { // if property is a reference to another entity long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { if (!EntityExists(targetGlobalId)) { string message = string.Format("Failed to get target entity with GID: 0x{0:X16}. {1}", targetGlobalId); throw new Exception(message); } // get referenced entity for update IdentifiedObject targetEntity = GetEntity(targetGlobalId); targetEntity.AddReference(property.Id, io.GlobalId); } io.SetProperty(property); } else { io.SetProperty(property); } } } CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Inserting entity with GID ({0:x16}) successfully finished.", globalId); } catch (Exception ex) { string message = String.Format("Failed to insert entity (GID = 0x{0:x16}) into model. {1}", rd.Id, ex.Message); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } }
private void InsertEntity(ResourceDescription rd, out ResourceDescription newRd) { newRd = rd; if (rd == null) { CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Insert entity is not done because update operation is empty."); throw new Exception(); } long globalId = rd.Id; CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Inserting entity with GID ({0:x16}).", globalId); // check if mapping for specified global id already exists if (this.EntityExists(globalId)) { string message = string.Format("Failed to insert entity because entity with specified GID ({0:x16}) already exists in network model.", globalId); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } try { // find type DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); //check mrid already exist // if exist made mapping old-new mrids if (CheckMridExist(type, rd)) { var typedCollection = networkDataModelCopy[type]; var objMrid = rd.Properties.Single(x => x.Id == ModelCode.IDOBJ_MRID); var result = typedCollection.Entities.Values.SingleOrDefault(x => x.MRID == objMrid.PropertyValue.StringValue); GidHelper[globalId] = result.GID; return; } Container container = null; // get container or create container if (ContainerExists(type)) { container = GetContainer(type); } else { container = new Container(); networkDataModelCopy.Add(type, container); } // create entity and add it to container IdentifiedObject io = container.CreateEntity(globalId); // -------------------------------------------------- affectedEntities.Add(globalId, DeltaOpType.Insert); // -------------------------------------------------- // apply properties on created entity if (rd.Properties != null) { foreach (Property property in rd.Properties) { // globalId must not be set as property if (property.Id == ModelCode.IDOBJ_GID) { continue; } if (property.Type == PropertyType.Reference) { // if property is a reference to another entity long targetGlobalId = property.AsReference(); if (GidHelper.ContainsKey(targetGlobalId)) { //Update old-new mrids targetGlobalId = GidHelper[targetGlobalId]; newRd.Properties.Single(x => x.Id == property.Id).SetValue(targetGlobalId); } if (targetGlobalId != 0) { if (!EntityExists(targetGlobalId)) { string message = string.Format("Failed to get target entity with GID: 0x{0:X16}. {1}", targetGlobalId); throw new Exception(message); } // get referenced entity for update IdentifiedObject targetEntity = GetEntity(targetGlobalId); targetEntity.AddReference(property.Id, io.GID); } io.SetProperty(property); } else { io.SetProperty(property); } } } CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Inserting entity with GID ({0:x16}) successfully finished.", globalId); } catch (Exception ex) { string message = string.Format("Failed to insert entity (GID = 0x{0:x16}) into model. {1}", rd.Id, ex.Message); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } }
private void InsertEntity(ResourceDescription rd) { if (rd == null) { string message = String.Format("MODBUS SIMULATOR - Insert entity is not done because update operation is empty."); CommonTrace.WriteTrace(CommonTrace.TraceVerbose, message); throw new Exception(message); } long globalId = rd.Id; string mrid = rd.Properties[0].PropertyValue.StringValue; CommonTrace.WriteTrace(CommonTrace.TraceInfo, "MODBUS SIMULATOR - Insertion of entity with GID ({0:x16}).", globalId); if (EntityExists(globalId, mrid)) { string message = String.Format("Failed to insert analog value because entity already exists in modbus simulator model."); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } try { //Gets object type depending on its Global ID DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); //New DMSType Object IdentifiedObject io = CreateEntity(globalId); //If object has no properties the method is finished if (rd.Properties == null) { return; } foreach (Property property in rd.Properties) { //No point in setting the GlobalID since it is already set if (property.Id == ModelCode.IDOBJ_GID) { continue; } //Check if property is a Reference, set it if it is not if (property.Type != PropertyType.Reference) { io.SetProperty(property); continue; } //If it is, get an ID of the object which is a reference long targetGlobalId = property.AsReference(); // // wa DMSType tip = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); if (tip == DMSType.SUBSTATION || tip == DMSType.SYNCMACHINE) { continue; } if (targetGlobalId != 0) { //if it exists IdentifiedObject targetEntity = GetEntity(targetGlobalId); if (targetEntity == null) { CommonTrace.WriteTrace(CommonTrace.TraceInfo, "MODBUS SIMULATOR - Target entity is Substation.", globalId); continue; } targetEntity.AddReference(property.Id, io.GlobalId); } io.SetProperty(property); } CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "MODBUS SIMULATOR - Insertion of entity with GID ({0:x16}) successfully finished.", globalId); } catch (Exception ex) { string message = String.Format("MODBUS SIMULATOR - Failed to insert entity (GID = 0x{0:x16}) into model. {1}", rd.Id, ex.Message); CommonTrace.WriteTrace(CommonTrace.TraceError, message); throw new Exception(message); } }
private void InsertEntity(ResourceDescription rd) { if (rd == null) { string message = String.Format("Insert entity is not done because update operation is empty."); LogHelper.Log(LogTarget.File, LogService.CETwoPhaseCommit, " ERROR - CalculationEngineModel.cs - " + message); throw new Exception(message); } long globalId = rd.Id; string mrid = rd.Properties[0].PropertyValue.StringValue; LogHelper.Log(LogTarget.File, LogService.CETwoPhaseCommit, " INFO - CalculationEngineModel.cs - Insert entity with {" + mrid + "} mrid."); if (EntityExists(globalId, mrid)) { string message = String.Format("Failed to insert analog value because entity already exists in calculation engine model."); LogHelper.Log(LogTarget.File, LogService.CETwoPhaseCommit, " ERROR - CalculationEngineModel.cs - " + message); throw new Exception(message); } DMSType type; try { //Dobijam tip objekta koji se ubacuje iz njegovog globalnog identifikatora, u ovom slucaju DER, ANALOG ili DISCRETE type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); //Pravim objekat DMS tipa, to moze biti der, analog ili discrete IdentifiedObject io = CreateEntity(globalId); //Ako nemamo properties onda smo zavrsili i napustamo i napustamo metodu InsertEntity if (rd.Properties == null) { return; } foreach (Property property in rd.Properties) { //GID se podesi kroz konstruktor tako da ga sada ne podesavamo if (property.Id == ModelCode.IDOBJ_GID) { continue; } //Provera da li je property referenca, ako nije postavi property i uzmi sledeci if (property.Type != PropertyType.Reference) { io.SetProperty(property); continue; } //wa DMSType tip = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); if (tip == DMSType.SUBSTATION || tip == DMSType.SYNCMACHINE) { continue; } //Ako je referenca //Uzmemo id objekta na koga se property odnosi long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { //Ako postoji IdentifiedObject targetEntity = GetEntity(targetGlobalId); if (targetEntity == null) { string message1 = String.Format("Target entity is " + type.ToString() + " (GID = 0x{0:x16})", globalId); // bilo je substation LogHelper.Log(LogTarget.File, LogService.CETwoPhaseCommit, " ERROR - CalculationEngineModel.cs - " + message1); continue; } targetEntity.AddReference(property.Id, io.GlobalId); } io.SetProperty(property); } string message = String.Format("Inserting entity with (GID = 0x{0:x16}) successfully finished.", globalId); LogHelper.Log(LogTarget.File, LogService.CETwoPhaseCommit, " INFO - CalculationEngineModel.cs - " + message); } catch (Exception ex) { string message = String.Format("CALCULATION ENGINE - Failed to insert entity (GID = 0x{0:x16}) into model. {1}", rd.Id, ex.Message); LogHelper.Log(LogTarget.File, LogService.CETwoPhaseCommit, " ERROR - CalculationEngineModel.cs - " + message); throw new Exception(message); } }
Tuple <IdentifiedObject, IdentifiedObject> UpdateEntity(ResourceDescription rd) { if (rd == null) { return(null); } IdentifiedObject oldIO; Container container; if (!TryGetEntity(rd.Id, out oldIO, out container)) { return(null); } IdentifiedObject io = oldIO.Clone(); foreach (Property prop in rd.Properties.Values) { if (prop.Type == PropertyType.Reference) { long oldTargetGID = ((ReferenceProperty)io.GetProperty(prop.Id)).Value; if (oldTargetGID != 0) { IdentifiedObject oldTarget; Container oldTargetContainer; if (TryGetEntity(oldTargetGID, out oldTarget, out oldTargetContainer)) { oldTarget = oldTarget.Clone(); oldTarget.RemoveTargetReference(prop.Id, io.GID); oldTargetContainer.Set(oldTarget); } } long targetGID = ((ReferenceProperty)prop).Value; if (targetGID != 0) { IdentifiedObject target; Container targetContainer; if (TryGetEntity(targetGID, out target, out targetContainer)) { target = target.Clone(); target.AddTargetReference(prop.Id, io.GID); targetContainer.Set(target); } } } if (!io.SetProperty(prop)) { return(null); } } container.Set(io); return(new Tuple <IdentifiedObject, IdentifiedObject>(oldIO, io)); }