/// <summary> /// Method creates constant field and its property which contains prefix of the property /// </summary> /// <param name="file">CodeTypeDeclaration class that field and its property are been added to</param> /// <param name="att">CodeMemberField member attribute that prefix field and its property are made for</param> /// <param name="field">Property which is being processing</param> private void CreateFieldPrefix(CodeTypeDeclaration file, CodeMemberField att, Property field) { CodeMemberField fieldPrefix = new CodeMemberField(); fieldPrefix.Attributes = MemberAttributes.Private | MemberAttributes.Const; fieldPrefix.Type = new CodeTypeReference(typeof(string)); fieldPrefix.Name = "_" + att.Name.Substring(4) + "Prefix"; //set prefix if (field.GetUndefinedStereotypes() != null && field.GetUndefinedStereotypes().Count > 0) { fieldPrefix.InitExpression = new CodePrimitiveExpression(StringManipulationManager.ExtractShortestName(field.GetUndefinedStereotypes().ElementAt(0).Name, separator)); } else { fieldPrefix.InitExpression = new CodePrimitiveExpression("cim"); } file.Members.Add(fieldPrefix); CodeMemberProperty propFieldPrefix = new CodeMemberProperty(); propFieldPrefix.Attributes = MemberAttributes.Public | MemberAttributes.Static; propFieldPrefix.Type = new CodeTypeReference(typeof(string)); propFieldPrefix.Name = StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)) + "Prefix"; propFieldPrefix.HasGet = true; propFieldPrefix.HasSet = false; propFieldPrefix.GetStatements.Add(new CodeSnippetExpression("return " + fieldPrefix.Name)); file.Members.Add(propFieldPrefix); }
private void CreatePropertyForField(CodeTypeDeclaration file, string dataType, CodeMemberField att, bool get, bool set) { CodeMemberProperty prop = new CodeMemberProperty(); prop.Attributes = MemberAttributes.Public; prop.Type = new CodeTypeReference(dataType); prop.Name = StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)); if (prop.Name.Equals(file.Name)) { prop.Name = prop.Name + "P"; } if (get) { prop.HasGet = true; prop.GetStatements.Add(new CodeSnippetExpression("return this." + att.Name + ((String.Compare(dataType, "system.string", true) == 0)?"":".GetValueOrDefault()"))); } if (set) { prop.HasSet = true; prop.SetStatements.Add(new CodeSnippetExpression("this." + att.Name + " = value")); } file.Members.Add(prop); CreateHasValueProperty(file, att); }
/// <summary> /// Method creates property field for member field <c>att</c> inside <c>file</c> class /// </summary> /// <param name="file">CodeTypeDeclaration class that property is added to</param> /// <param name="att">CodeMemberField member field that property is made for</param> /// <param name="get">bool value - if <c>true</c> get will be added to property field</param> /// <param name="set">bool value - if <c>true</c> set will be added to property field</param> private static void CreatePropertyForField(CodeTypeDeclaration file, CodeMemberField att, bool get, bool set) { CodeMemberProperty prop = new CodeMemberProperty(); prop.Attributes = MemberAttributes.Public; prop.Type = att.Type; prop.Name = StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)); if (prop.Name.Equals(file.Name)) { prop.Name = prop.Name + "P"; } if (get) { prop.HasGet = true; prop.GetStatements.Add(new CodeSnippetExpression("return this." + att.Name)); } if (set) { prop.HasSet = true; prop.SetStatements.Add(new CodeSnippetExpression("this." + att.Name + " = value")); } file.Members.Add(prop); CreateHasValueProperty(file, att); }
private void CreateIsMandatoryFieldAndProperty(CodeTypeDeclaration file, CodeMemberField att, Property field) { CodeMemberField fieldIsMandatory = new CodeMemberField(); fieldIsMandatory.Attributes = MemberAttributes.Private | MemberAttributes.Const; fieldIsMandatory.Type = new CodeTypeReference(typeof(bool)); fieldIsMandatory.Name = "is" + StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)) + "Mandatory"; //switch case and set true or false if (ExtractSimpleNameFromResourceURI(field.MultiplicityAsString).Equals(MultiplicityExactlyOneString1) || ExtractSimpleNameFromResourceURI(field.MultiplicityAsString).Equals(MultiplicityExactlyOneString2) || ExtractSimpleNameFromResourceURI(field.MultiplicityAsString).Equals(MultiplicityOneOrMoreString)) { fieldIsMandatory.InitExpression = new CodePrimitiveExpression(true); } else { fieldIsMandatory.InitExpression = new CodePrimitiveExpression(false); } file.Members.Add(fieldIsMandatory); CodeMemberProperty propIsMandatory = new CodeMemberProperty(); propIsMandatory.Attributes = MemberAttributes.Public | MemberAttributes.Static; propIsMandatory.Type = new CodeTypeReference(typeof(bool)); propIsMandatory.Name = "Is" + StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)) + "Mandatory"; propIsMandatory.HasGet = true; propIsMandatory.HasSet = false; propIsMandatory.GetStatements.Add(new CodeSnippetExpression("return " + fieldIsMandatory.Name)); file.Members.Add(propIsMandatory); }
private void CreateIsMandatoryFieldAndProperty(CodeTypeDeclaration file, CodeMemberField att, EAPAttribute attribute) { CodeMemberField fieldIsMandatory = new CodeMemberField(); fieldIsMandatory.Attributes = MemberAttributes.Private | MemberAttributes.Const; fieldIsMandatory.Type = new CodeTypeReference(typeof(bool)); fieldIsMandatory.Name = "is" + StringManipulationManager.CreateHungarianNotation(att.Name) + "Mandatory"; //switch case and set true or false if (int.Parse(attribute.Min) == 1) { fieldIsMandatory.InitExpression = new CodePrimitiveExpression(true); } else { fieldIsMandatory.InitExpression = new CodePrimitiveExpression(false); } file.Members.Add(fieldIsMandatory); CodeMemberProperty propIsMandatory = new CodeMemberProperty(); propIsMandatory.Attributes = MemberAttributes.Public | MemberAttributes.Static; propIsMandatory.Type = new CodeTypeReference(typeof(bool)); propIsMandatory.Name = "Is" + StringManipulationManager.CreateHungarianNotation(att.Name) + "Mandatory"; propIsMandatory.HasGet = true; propIsMandatory.HasSet = false; propIsMandatory.GetStatements.Add(new CodeSnippetExpression("return " + fieldIsMandatory.Name)); file.Members.Add(propIsMandatory); }
private static void CreateHasValueProperty(CodeTypeDeclaration file, CodeMemberField att) { CodeMemberProperty propHasValue = new CodeMemberProperty(); propHasValue.Attributes = MemberAttributes.Public; propHasValue.Type = new CodeTypeReference(typeof(bool)); propHasValue.Name = StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)) + "HasValue"; propHasValue.HasGet = true; propHasValue.HasSet = false; propHasValue.GetStatements.Add(new CodeSnippetExpression("return this." + att.Name + " != null")); file.Members.Add(propHasValue); }
private void CreatePropertyForField(CodeTypeDeclaration file, EAPAttribute attribute, CodeMemberField att, bool get, bool set) { CodeMemberProperty prop = new CodeMemberProperty(); prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; if (attribute.TypeCode != "") { if (attribute.TypeCode.Equals("Enum") || attribute.TypeCode.Equals("Class")) { prop.Type = new CodeTypeReference(attribute.MeasurementType); } } else { if (attribute.MeasurementType != "") { string dataType = StringManipulationManager.GetSystemType(attribute.MeasurementType); prop.Type = new CodeTypeReference(dataType); } } prop.Name = StringManipulationManager.CreateHungarianNotation(att.Name); if (prop.Name.Equals(file.Name)) { prop.Name = prop.Name + "P"; } if (get) { prop.HasGet = true; prop.GetStatements.Add(new CodeSnippetExpression("return this." + att.Name)); } if (set) { prop.HasSet = true; prop.SetStatements.Add(new CodeSnippetExpression("this." + att.Name + " = value")); } file.Members.Add(prop); }
/// <summary> /// Method creates predefined class that contains ID and other needed fields /// </summary> private void CreateParentClass() { CodeCompileUnit unit = new CodeCompileUnit(); //namespace CodeNamespace nameSpace = new CodeNamespace(defaultNS); unit.Namespaces.Add(nameSpace); //namespace imports nameSpace.Imports.Add(new CodeNamespaceImport("System")); //class CodeTypeDeclaration file = new CodeTypeDeclaration(); file.IsClass = true; file.Name = "IDClass"; file.TypeAttributes = TypeAttributes.Public; //create field string fieldName = "cim_ID"; CodeMemberField att = new CodeMemberField(typeof(string), fieldName); att.Attributes = MemberAttributes.Private; att.Comments.Add(new CodeCommentStatement("ID used for reference purposes", true)); file.Members.Add(att); //create property CodeMemberProperty prop = new CodeMemberProperty(); prop.Attributes = MemberAttributes.Public; prop.Type = new CodeTypeReference(typeof(string)); prop.Name = "ID"; prop.HasGet = true; prop.GetStatements.Add(new CodeSnippetExpression("return this." + fieldName)); prop.HasSet = true; prop.SetStatements.Add(new CodeSnippetExpression("this." + fieldName + " = value")); file.Members.Add(prop); CodeMemberField fieldIsMandatory = new CodeMemberField(); fieldIsMandatory.Attributes = MemberAttributes.Private | MemberAttributes.Const; fieldIsMandatory.Type = new CodeTypeReference(typeof(bool)); fieldIsMandatory.Name = "is" + StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)) + "Mandatory"; //switch case and set true or false fieldIsMandatory.InitExpression = new CodePrimitiveExpression(true); file.Members.Add(fieldIsMandatory); CodeMemberProperty propIsMandatory = new CodeMemberProperty(); propIsMandatory.Attributes = MemberAttributes.Public; propIsMandatory.Type = new CodeTypeReference(typeof(bool)); propIsMandatory.Name = "Is" + StringManipulationManager.CreateHungarianNotation(att.Name.Substring(4)) + "Mandatory"; propIsMandatory.HasGet = true; propIsMandatory.HasSet = false; propIsMandatory.GetStatements.Add(new CodeSnippetExpression("return " + fieldIsMandatory.Name)); file.Members.Add(propIsMandatory); nameSpace.Types.Add(file); Files.Add(unit); }
/// <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); } } } }