/// <summary> /// Method adds new attribute into existing map of all attributes (ObjectAttributes) of this CIMObject. /// <para>If MyAttributes map is null, it will be initialized and then the attribute will be added.</para> /// <para>If attribute with given FullName property already exists in the MyAttributes map, it will be added into the /// MyDuplicateAttributes list.</para> /// </summary> /// <param name="attr">object attribute</param> /// <returns>true if attribute has been successfuly added, false if there was duplicate attribute</returns> public bool AddAttribute(ObjectAttribute attr) { bool success = false; if (myAttributes == null) { myAttributes = new SortedDictionary <int, List <ObjectAttribute> >(); } List <ObjectAttribute> attrList = null; if (!myAttributes.ContainsKey(attr.Code)) { attrList = new List <ObjectAttribute>(); //// add attribute in list and in map attrList.Add(attr); myAttributes.Add(attr.Code, attrList); success = true; } else { myAttributes.TryGetValue(attr.Code, out attrList); attrList.Add(attr); success = true; } if (string.Compare(CIMConstants.AttributeNameIdentifiedObjectName, attr.FullName) == 0) { name = attr.Value; } return(success); }
/// <summary> /// Method removes the given object attribute from the MyAttributes list of /// this CIMObject. /// </summary> /// <param name="objectAttribute">attribute from MyAttributes map of this CIM object</param> public void RemoveAttribute(ObjectAttribute objectAttribute) { if ((myAttributes != null) && (objectAttribute != null)) { if (myAttributes.ContainsKey(objectAttribute.Code)) { List <ObjectAttribute> attrList = null; myAttributes.TryGetValue(objectAttribute.Code, out attrList); if ((attrList != null) && (attrList.Contains(objectAttribute))) { attrList.Remove(objectAttribute); } } } }
/// <summary> /// Method creates ObjectAttribute instance with following property values: /// <para>FullName = type + ".Member_Of_" + embeddedCategory</para> /// <para>IsReference = true</para> /// <para>Value = parentId</para> /// <para>This ObjectAttribute represents artificial attribute for representing /// info about parent inside of which some CIMObject is embedded.</para> /// </summary> /// <param name="cimModelContext">model context</param> /// <param name="type">CIM type of embedded child object</param> /// <param name="embeddedCategory">name of embedded category</param> /// <param name="parentId">parent object ID</param> /// <returns>ObjectAttribute instance or null if some argument is missing</returns> public static ObjectAttribute ConstructEmbeddedParentAttribute(CIMModelContext cimModelContext, string type, string embeddedCategory, string parentId) { ObjectAttribute specialAtt = null; if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(embeddedCategory) && !string.IsNullOrEmpty(parentId)) { if (embeddedCategory.Contains(StringManipulationManager.SeparatorColon)) { embeddedCategory = embeddedCategory.Substring(embeddedCategory.IndexOf(StringManipulationManager.SeparatorColon) + 1); } specialAtt = new ObjectAttribute(cimModelContext, string.Format("{0}.Member_Of_{1}", type, embeddedCategory)); specialAtt.IsReference = true; specialAtt.Value = parentId; } return(specialAtt); }
private void AddSimpleValueToList(CIMObject element, ref IList list, ObjectAttribute att, Type type) { if (!string.IsNullOrEmpty(att.Value)) { ////DATE AND TIME are specific - have to take care of them separately if (type.Equals(typeof(System.DateTime))) { try { ////Temporary solution 24h => 23:59:59h so that we know this is 00-24h ////this is because 24:00:00 is not by the standard but still may appear in rdf string value = att.Value; if (att.Value.Equals("24:00:00")) { value = "23:59:59"; } DateTime timeVal = DateTime.Parse(value); list.Add(timeVal); } catch { OnMessage("Invalid value format in CIM/XML of attribute (DateTime type) to be added to list " + att.FullName + ", value: " + att.Value + " elements ID: " + element.ID , MessageLevel.WARNING); } }////Everything else goes the same way else { try { list.Add(Convert.ChangeType(att.Value, type, new CultureInfo("en-US"))); } catch { OnMessage("Invalid value format in CIM/XML of attribute to be added to list " + att.FullName + ", value: " + att.Value + " elements ID: " + element.ID , MessageLevel.ERROR); } } } }
/// <summary> /// Method returns object attribute of this CIMObject with given FullName value, /// <para>or null if the requested fullName value isn't found as one of key values in the MyAttributes map.</para> /// </summary> /// <param fullName="fullName">value of FullName property of requested attribute from MyAttributes map of this CIM object</param> /// <param name="multipleAtts">in case that there are multiple attributes with same fullName, this is the list of duplicates (duplicates aren't always error - it depends on profile definition)</param> /// <returns>ObjectAttribute with given full fullName or null</returns> public ObjectAttribute GetAttribute(string fullName, out List <ObjectAttribute> multipleAtts) { int attributeCode = modelContext.ReadCodeOfAttribute(fullName); ObjectAttribute attribute = null; multipleAtts = null; if (myAttributes != null) { if (myAttributes.ContainsKey(attributeCode)) { if ((myAttributes[attributeCode] != null) && (myAttributes[attributeCode].Count > 0)) { attribute = myAttributes[attributeCode][0]; if (myAttributes[attributeCode].Count > 1) { multipleAtts = myAttributes[attributeCode].GetRange(1, myAttributes[attributeCode].Count - 1); } } } } return(attribute); }
private void SetSimpleValue(CIMObject element, object something, ObjectAttribute att, PropertyInfo prop) { if (!string.IsNullOrEmpty(att.Value)) { ////DATE AND TIME are specific - have to take care of them separately if (prop.PropertyType.Equals(typeof(System.DateTime))) { try { ////Temporary solution 24h => 23:59:59h so that we know this is 00-24h ////this is because 24:00:00 is not by the standard but still may appear in rdf string value = att.Value; if (att.Value.Equals("24:00:00")) { value = "23:59:59"; } DateTime timeVal = DateTime.Parse(value); prop.SetValue(something, timeVal, null); } catch { OnMessage("Invalid format in CIM/XML of attribute (DateTime type)" + att.FullName + ", value: " + att.Value + ", elements ID: " + element.ID , MessageLevel.WARNING); } }////Everything else goes the same way else { try { prop.SetValue(something, Convert.ChangeType(att.Value, prop.PropertyType, new CultureInfo("en-US")), null); } catch { OnMessage("Invalid format in CIM/XML of attribute (simple value)" + att.FullName + " elements ID: " + element.ID, MessageLevel.WARNING); } } } }
/// <summary> /// Sets value as data type, because in cim/xml model there is no data type as class /// only its value /// </summary> /// <param name="assembly">contains data type class</param> /// <param name="instance">object that needs property set</param> /// <param name="prop">property of instace that data type will be set to</param> /// <param name="att">attribute from model that contains value and name od data type</param> private void SetDataTypeProperty(CIMObject element, Assembly assembly, object instance, PropertyInfo prop, ObjectAttribute att) { if (!string.IsNullOrEmpty(att.Value)) { ////get Type from assembly Type dataType = assembly.GetType(prop.PropertyType.ToString()); if (dataType != null) { ////it should be only value field that can be set by value PropertyInfo dataTypePropertyInfo = dataType.GetProperty("Value"); if (dataTypePropertyInfo != null) { object dataTypeInstance = Activator.CreateInstance(dataType); prop.SetValue(instance, dataTypeInstance, null); SetSimpleValue(element, dataTypeInstance, att, dataTypePropertyInfo); } } else { OnMessage("Error occured while trying to set value to field " + prop.DeclaringType.Name + "." + prop.Name + " DataType " + prop.PropertyType.ToString() + " not found in assembly! Elements ID: " + element.ID , MessageLevel.WARNING); } } }
/// <summary> /// Sets value of enumeration to property <c>prop</c> of object <c>instance</c> /// </summary> /// <param name="assembly">contains enumeration definition</param> /// <param name="instance">object that contains property</param> /// <param name="prop">property that value will be set to</param> /// <param name="att">attribute from model that cointains value and name of enumeration</param> private void SetEnumerationProperty(CIMObject element, Assembly assembly, object instance, PropertyInfo prop, ObjectAttribute att) { if (!string.IsNullOrEmpty(att.Value)) { ////get type, that is, enumeration member that has name equal to the string value of attribute ////get the field with name casted into int and set itatt.Value Type enumType = assembly.GetType(prop.PropertyType.ToString()); FieldInfo enumVal = enumType.GetField(StringManipulationManager.ReplaceInvalidEnumerationCharacters(att.Value)); if (enumVal == null) { OnMessage("Error occured while trying to set value to field " + prop.DeclaringType.Name + "." + prop.Name + ", enumeration value <" + prop.PropertyType.ToString() + "." + StringManipulationManager.ReplaceInvalidEnumerationCharacters(att.Value) + "> not found in assembly!" + " elements ID: " + element.ID, MessageLevel.WARNING); } else { object newEnumValue = enumVal.GetValue(enumType); prop.SetValue(instance, newEnumValue, null); } } }
/// <summary> /// Reads all the "simple" values (not references) from attribute list and sets properties to the /// specified value /// </summary> /// <param name="assembly">assembly that contains class definitions</param> /// <param name="element">element being processes</param> /// <param name="classType">type of the instance</param> /// <param name="instance">instance of classType that will have properties set</param> private void ProcessAttributes(Assembly assembly, CIMObject element, Type classType, object instance) { foreach (int attKey in element.MyAttributes.Keys) { ////all properties have capital letters used for naming them string propertyName = StringManipulationManager.ExtractShortestName(element.ModelContext.ReadAttributeWithCode(attKey), StringManipulationManager.SeparatorDot); propertyName = StringManipulationManager.CreateHungarianNotation(propertyName); if (propertyName.Equals(element.CIMType)) { propertyName = propertyName + "P"; } PropertyInfo prop = classType.GetProperty(propertyName); if (prop == null) { OnMessage("Property " + propertyName + " not found in class " + element.CIMType + " (element ID:" + element.ID + ")" + " - validation of document failed!" , MessageLevel.ERROR); continue; } ////if it is a list or collection - though it always has to be a list if (prop.PropertyType.IsGenericType) { ////gets the type of the items in list Type propertyListType = prop.PropertyType.GetGenericArguments()[0]; ////get all the values for this property List <ObjectAttribute> attList = element.MyAttributes[attKey]; ////get the property as IList IList list = (IList)prop.GetValue(instance, null); List <FTN.Commands> pomComm = new List <FTN.Commands>(); List <FTN.States> pomState = new List <FTN.States>(); if (attList.Count > 0) { string[] items = attList[0].Value.Split(' '); foreach (var item in items) { if (item.Equals("Open")) { pomComm.Add(FTN.Commands.Open); } else if (item.Equals("Close")) { pomComm.Add(FTN.Commands.Close); } } foreach (var item in items) { if (item.Equals("Opened")) { pomState.Add(FTN.States.Opened); } else if (item.Equals("Closed")) { pomState.Add(FTN.States.Closed); } } } foreach (ObjectAttribute att in attList) { ////Only add a simple value to IList, enumerations and references are not needed if (IsSimpleValue(propertyListType)) { AddSimpleValueToList(element, ref list, att, propertyListType); } } if (pomComm.Count > 0) { prop.SetValue(instance, pomComm, null); } if (pomState.Count > 0) { prop.SetValue(instance, pomState, null); } } else { ////if property is not a list... List <ObjectAttribute> attList = element.MyAttributes[attKey]; ////it only has one attribute value in list then if (attList.Count <= 1) { ObjectAttribute att = attList.ElementAt(0); if (null != prop) { if (IsSimpleValue(prop.PropertyType)) { SetSimpleValue(element, instance, att, prop); } else { if (prop.PropertyType.IsEnum) { SetEnumerationProperty(element, assembly, instance, prop, att); } else { ////if it was not found up until now it has to be reference or data type ////if it is not empty and it is not any of the cases checked already it is DataType ////make instance of dataType and set value if (!IsSimpleValue(prop.PropertyType) && !string.IsNullOrEmpty(att.Value)) { SetDataTypeProperty(element, assembly, instance, prop, att); } } } } } else { OnMessage("Multiple values for attribute with multiplicity 1 on element with ID:" + element.ID + ". ATTRIBUTE: " + classType + "." + prop.Name , MessageLevel.WARNING); } } } }
private void ConnectModelElements(CIMModel CIM_Model, Assembly assembly, ref ConcreteModel concreteModel) { foreach (string type in CIM_Model.ModelMap.Keys) { SortedDictionary <string, CIMObject> objects = CIM_Model.ModelMap[type]; foreach (string objID in objects.Keys) { CIMObject element = objects[objID]; Type classType; classType = assembly.GetType(ActiveNamespace + "." + type); if (classType == null) { OnMessage("Element (" + element.ID + ") not found in assembly:" + ActiveNamespace + "." + type + " - validation of document failed!", MessageLevel.ERROR); continue; } ////aquire object from concrete model object currentObject = concreteModel.GetObjectByTypeAndID(classType, objID); if (currentObject != null) { foreach (int attKey in element.MyAttributes.Keys) { string propertyName = StringManipulationManager.ExtractShortestName(CIM_Model.ModelContext.ReadAttributeWithCode(attKey), StringManipulationManager.SeparatorDot); propertyName = StringManipulationManager.CreateHungarianNotation(propertyName); if (propertyName.Equals(type)) { propertyName = propertyName + "P"; } PropertyInfo prop = classType.GetProperty(propertyName); if (prop == null) { OnMessage("Property " + propertyName + " not found in class " + element.CIMType + ", elements ID:" + element.ID + " - validation of document failed!", MessageLevel.ERROR); continue; } ////if it is a list or collection of references if (prop.PropertyType.IsGenericType) { Type propertyListType = prop.PropertyType.GetGenericArguments()[0]; List <ObjectAttribute> attList = element.MyAttributes[attKey]; ////get the property as IList IList list = (IList)prop.GetValue(currentObject, null); foreach (ObjectAttribute att in attList) { ////this part should add a reference to IList if (!IsSimpleValue(propertyListType) && !propertyListType.IsEnum) { AddReferenceToList(element, ref list, att.Value, prop, concreteModel); } } } else { List <ObjectAttribute> attList = element.MyAttributes[attKey]; ////if its not a list... ObjectAttribute att = attList.ElementAt(0); if (null != prop && prop.CanWrite) { if (!IsSimpleValue(prop.PropertyType) && !prop.PropertyType.IsEnum) { SetReferenceToProperty(element, currentObject, att.Value, prop, concreteModel); } } } } ////embeded elements - lists if (element.GetEmbeddedChildren() != null) { foreach (string attKey in element.GetEmbeddedChildren().Keys) { ////first is the name of property string propertyName = StringManipulationManager.ExtractShortestName(attKey, StringManipulationManager.SeparatorDot); propertyName = StringManipulationManager.CreateHungarianNotation(propertyName); if (propertyName.Equals(type)) { propertyName = propertyName + "P"; } PropertyInfo prop = classType.GetProperty(propertyName); if (prop != null && prop.PropertyType.IsGenericType) { Type propertyListType = prop.PropertyType.GetGenericArguments()[0]; List <string> attList = element.GetEmbeddedChildren()[attKey]; ////get the property as IList IList list = (IList)prop.GetValue(currentObject, null); foreach (string att in attList) { ////this part should add a reference to IList if (!IsSimpleValue(propertyListType) && !propertyListType.IsEnum) { AddReferenceToList(element, ref list, att, prop, concreteModel); } } } else { List <string> attList = element.GetEmbeddedChildren()[attKey]; ////if its not a list... string att = attList.ElementAt(0); if (prop != null && prop.CanWrite) { if (!IsSimpleValue(prop.PropertyType) && !prop.PropertyType.IsEnum) { SetReferenceToProperty(element, currentObject, att, prop, concreteModel); } } } } } } else { OnMessage("Object of class:" + classType + ", with ID:" + objID + " not found in model! Unable to create concrete model." , MessageLevel.ERROR); } } } }