/// <summary>
        /// Creates ConcreteModel from CIMModel using classes from assembly
        /// </summary>
        /// <param name="CIM_Model">model of cim/xml that is used to create ConcreteModel</param>
        /// <param name="assembly">contains classes for model</param>
        public ConcreteModelBuildingResult GenerateModel(CIMModel CIM_Model, Assembly assembly, string type, ref ConcreteModel model)
        {
            //model = new ConcreteModel();
            result          = new ConcreteModelBuildingResult();
            activeNamespace = type;

            if (CIM_Model != null && assembly != null)
            {
                try
                {
                    //two iterations
                    //- first - only simple value/enumerations and instantiate the class
                    //- second - create connections
                    InstantiateClassesWithSimpleValues(CIM_Model, assembly, ref model);

                    //second iteration for setting up references (excluding dataTypes)GOES HERE
                    ConnectModelElements(CIM_Model, assembly, ref model);

                    result.MissingValues = GenerateMissingMandatoryValuesReport(model, assembly);
                }
                catch (Exception ex)
                {
                    OnMessage(ex.Message, MessageLevel.ERROR);
                }
            }
            else
            {
                OnMessage("Model or assembly are not selected.", MessageLevel.ERROR);
            }

            return(result);
        }
Ejemplo n.º 2
0
 public void StartDocument(string filePath)
 {
     currentElement   = null;
     currentAttribute = null;
     if (model == null)
     {
         model            = new CIMModel();
         model.SourcePath = filePath;
     }
     inLevel = Level.Root;
 }
Ejemplo n.º 3
0
 public override void StartDocument()
 {
     currentElement   = null;
     currentAttribute = null;
     embeddedElement  = null;
     if (model == null)
     {
         model            = new CIMModel();
         model.SourcePath = this.filePath;
     }
     childrenOf = new Dictionary <string, Stack <CIMObject> >();
     inLevel    = Level.Root;
 }
Ejemplo n.º 4
0
        private bool LoadModelFromExtractFile(Stream extract, ref ConcreteModel concreteModelResult, out string errorLog)
        {
            bool valid = false;

            errorLog = string.Empty;

            System.Globalization.CultureInfo culture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            try
            {
                Assembly assembly;
                LoadAssembly(".\\" + ProfileName + ".dll", out assembly);

                if (assembly != null)
                {
                    CIMModel             cimModel        = new CIMModel();
                    CIMModelLoaderResult modelLoadResult = CIMModelLoader.LoadCIMXMLModel(extract, Namespace, out cimModel);
                    if (modelLoadResult.Success)
                    {
                        concreteModelResult = new ConcreteModel();
                        ConcreteModelBuilder        builder          = new ConcreteModelBuilder();
                        ConcreteModelBuildingResult modelBuildResult = builder.GenerateModel(cimModel, assembly, Namespace, ref concreteModelResult);

                        if (modelBuildResult.Success)
                        {
                            valid = true;
                        }
                        else
                        {
                            errorLog = modelBuildResult.Report.ToString();
                        }
                    }
                    else
                    {
                        errorLog = modelLoadResult.Report.ToString();
                    }
                }

                Thread.CurrentThread.CurrentCulture = culture;
            }
            catch (Exception e)
            {
                Thread.CurrentThread.CurrentCulture = culture;
                errorLog = e.Message;
            }

            return(valid);
        }
Ejemplo n.º 5
0
        private bool LoadModelFromExtractFile(Stream extract, SupportedProfiles extractType, ref ConcreteModel concreteModelResult, ref Assembly assembly, out string log)
        {
            bool valid = false;

            log = string.Empty;

            System.Globalization.CultureInfo culture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            try
            {
                ProfileManager.LoadAssembly(extractType, out assembly);
                if (assembly != null)
                {
                    CIMModel             cimModel        = new CIMModel();
                    CIMModelLoaderResult modelLoadResult = CIMModelLoader.LoadCIMXMLModel(extract, ProfileManager.Namespace, out cimModel);
                    if (modelLoadResult.Success)
                    {
                        concreteModelResult = new ConcreteModel();
                        ConcreteModelBuilder        builder          = new ConcreteModelBuilder();
                        ConcreteModelBuildingResult modelBuildResult = builder.GenerateModel(cimModel, assembly, ProfileManager.Namespace, ref concreteModelResult);

                        if (modelBuildResult.Success)
                        {
                            valid = true;
                        }
                        log = modelBuildResult.Report.ToString();
                    }
                    else
                    {
                        log = modelLoadResult.Report.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                log = e.Message;
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = culture;
            }
            return(valid);
        }
        /// <summary>
        /// Method instantiates all classes from <c>CIM_Model</c> using class definitions from <c>assembly</c>
        /// All instances will have ID, and properties, that are not references, set and will be stored in model
        /// </summary>
        /// <param name="CIM_Model">model representing cim/xml</param>
        /// <param name="assembly">assembly that contains classes used in model</param>
        private void InstantiateClassesWithSimpleValues(CIMModel CIM_Model, Assembly assembly, ref ConcreteModel concreteModel)
        {
            ////FOREACH TYPE IN MODEL
            foreach (string type in CIM_Model.ModelMap.Keys)
            {
                SortedDictionary <string, CIMObject> objects = CIM_Model.ModelMap[type];
                ////FOREACH OBJECT WITH ID THAT BELONGS TO TYPE
                foreach (string objID in objects.Keys)
                {
                    ////GET THE OBJECT
                    CIMObject element = objects[objID];
                    Type      classType;
                    ////AQUIRE TYPE FROM ASSEMBLY
                    classType = assembly.GetType(ActiveNamespace + "." + type);
                    if (classType == null)
                    {
                        OnMessage("Class for element (" + element.ID + ") not found in assembly:" + ActiveNamespace + "." + type + "  - validation of document failed!"
                                  , MessageLevel.ERROR);
                        continue;
                    }

                    ////WITH TYPE MAKE AN INSTANCE
                    object instance = Activator.CreateInstance(classType);

                    ////AND SET ID TO THAT INSTANCE
                    PropertyInfo propID = classType.GetProperty("ID");
                    propID.SetValue(instance, objID, null);

                    ////SET SIMPLE VALUE ATTRIBUTES
                    ProcessAttributes(assembly, element, classType, instance);

                    ////SAVE OBJECT IN MODEL
                    string insertEl = concreteModel.InsertObjectInModelMap(instance, classType);
                    if (!string.IsNullOrEmpty(insertEl))
                    {
                        OnMessage(string.Format("Inserting in model error on element ID {0} . Message: {1}", objID, insertEl), MessageLevel.WARNING);
                    }
                }
            }
        }
 private void CheckMissingValues(CIMModel CIM_Model, Assembly assembly)
 {
     ////FOREACH TYPE IN MODEL
     foreach (string type in CIM_Model.ModelMap.Keys)
     {
         SortedDictionary <string, CIMObject> objects = CIM_Model.ModelMap[type];
         ////FOREACH OBJECT WITH ID THAT BELONGS TO TYPE
         foreach (string objID in objects.Keys)
         {
             ////GET THE OBJECT
             CIMObject element = objects[objID];
             Type      classType;
             ////AQUIRE TYPE FROM ASSEMBLY
             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;
             }
         }
     }
 }
Ejemplo n.º 8
0
        public static CIMModelLoaderResult LoadCIMXMLModel(Stream stream, string fileName, out CIMModel model, string enumMappingFilePath = "")
        {
            CIMModelLoaderResult result = new CIMModelLoaderResult();

            try
            {
                bool                success;
                TimeSpan            durationOfParsing = new TimeSpan(0);
                CIMXMLReaderHandler handler           = new CIMXMLReaderHandler();

                handler = (CIMXMLReaderHandler)XMLParser.DoParse(handler, stream, fileName, out success, out durationOfParsing);

                if (success)
                {
                    model = handler.Model;
                }
                else
                {
                    result.Report.AppendLine("Loading CIM model was unsuccessful.");
                    model          = null;
                    result.Success = false;
                }
            }
            catch (Exception e)
            {
                model          = null;
                result.Success = false;
                result.Report.AppendLine("XML FORMAT ERROR! XML FILE: " + Path.GetFileName(fileName) + "\n\t Description: " + e.Message);
            }
            return(result);
        }
        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);
                    }
                }
            }
        }