Example #1
0
        /// <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);
        }
Example #2
0
        /// <summary>
        /// Method first tries to find the ProfileElementStereotype object with given fullName isInside the
        /// (static) StereotypeList, and if it doesn't find it, it creates new stereotype objects
        /// and adds it to this list.
        /// <remarks>If the fullStereotypeName is null, method will return null.</remarks>
        /// </summary>
        /// <param fullName="fullStereotypeName">full fullName of stereotype which is being searched</param>
        /// <returns>ProfileElementStereotype object with given fullName founded (or added) in StereotypeList</returns>
        public static ProfileElementStereotype FindOrCreateStereotypeForName(string fullStereotypeName)
        {
            ProfileElementStereotype stereotype = null;

            if (!string.IsNullOrEmpty(fullStereotypeName))
            {
                string shortName = StringManipulationManager.ExtractShortestName(fullStereotypeName, StringManipulationManager.SeparatorSharp);

                foreach (ProfileElementStereotype existingStereotype in Profile.StereotypeList)
                {
                    if (existingStereotype.Name.Equals(fullStereotypeName) || existingStereotype.Name.Equals(shortName))
                    {
                        stereotype = existingStereotype;
                        break;
                    }
                }

                if (stereotype == null)
                {
                    stereotype = new ProfileElementStereotype(Profile.StereotypeList.Count, fullStereotypeName);
                    Profile.StereotypeList.Add(stereotype);
                }
            }
            return(stereotype);
        }
Example #3
0
 private string ExtractSimpleNameFromResourceURI(string resourceUri)
 {
     return(StringManipulationManager.ExtractShortestName(resourceUri, separator));
 }
Example #4
0
        public override string ToString()
        {
            StringBuilder toStringBuilder = new StringBuilder("Profile: \n");

            if (profileMap != null)
            {
                if (profileMap.ContainsKey(ProfileElementTypes.ClassCategory))
                {
                    foreach (ProfileElement package in profileMap[ProfileElementTypes.ClassCategory])
                    {
                        toStringBuilder.Append("* members of ");
                        toStringBuilder.AppendLine(package.UniqueName);
                        List <ProfileElement> list = (package as ClassCategory).MembersOfClassCategory;
                        if (list != null)
                        {
                            foreach (ProfileElement elem in list)
                            {
                                if (elem is Class)
                                {
                                    toStringBuilder.Append("\t ").AppendLine(elem.URI);
                                    toStringBuilder.Append("\t\t type = ").AppendLine(elem.Type);
                                    toStringBuilder.Append("\t\t label = ").AppendLine(elem.Label);
                                    if (elem.IsEnumeration)
                                    {
                                        toStringBuilder.AppendLine("\t\t Enumeration class");
                                    }

                                    if ((elem as Class).Stereotypes != null)
                                    {
                                        toStringBuilder.AppendLine("\t\t has Stereotypes : ");
                                        foreach (ProfileElementStereotype stereotype in (elem as Class).Stereotypes)
                                        {
                                            toStringBuilder.Append("\t\t\t").AppendLine(stereotype.ToString());
                                        }
                                    }

                                    toStringBuilder.Append("\t\t subClassOf = ").AppendLine((elem as Class).SubClassOf);
                                    toStringBuilder.Append("\t\t belongsToCategory = ").AppendLine((elem as Class).BelongsToCategory);
                                    if ((elem as Class).MyProperties != null)
                                    {
                                        toStringBuilder.AppendLine("\t\t has Properties : ");
                                        foreach (ProfileElement property in (elem as Class).MyProperties)
                                        {
                                            toStringBuilder.Append("\t\t\t").AppendLine(property.UniqueName);
                                            toStringBuilder.Append("\t\t\t\t label = ").AppendLine(property.Label);
                                            toStringBuilder.Append("\t\t\t\t dataType = ").AppendLine((property as Property).DataType);
                                            toStringBuilder.Append("\t\t\t\t range = ").AppendLine((property as Property).Range);
                                            toStringBuilder.Append("\t\t\t\t multiplicity = ").AppendLine(StringManipulationManager.ExtractShortestName(property.MultiplicityAsString, StringManipulationManager.SeparatorSharp));

                                            if (property.Stereotypes != null)
                                            {
                                                toStringBuilder.AppendLine("\t\t\t\t has Stereotypes : ");
                                                foreach (ProfileElementStereotype stereotype in property.Stereotypes)
                                                {
                                                    toStringBuilder.Append("\t\t\t\t\t").AppendLine(stereotype.ToString());
                                                }
                                            }
                                            toStringBuilder.AppendLine();
                                        }
                                    }

                                    if ((elem as Class).MyEnumerationMembers != null)
                                    {
                                        toStringBuilder.AppendLine("\t\t\t has enum members : ");
                                        foreach (ProfileElement enumMember in (elem as Class).MyEnumerationMembers)
                                        {
                                            toStringBuilder.Append("\t\t\t\t").AppendLine(enumMember.UniqueName);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(toStringBuilder.ToString());
        }
        /// <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);
                    }
                }
            }
        }