Exemplo n.º 1
0
        public static void PerformChanges(IRepository pcmRepository, ISystem0 system, IResourceEnvironment resourceEnvironment, Allocation allocation, Model model, string target)
        {
            var last = false;
            var i    = 0;
            ModelChangeRecorder recorder = null;

            StartNextRecorder(model, target, last, ref i, ref recorder);

            var container = new ResourceContainer
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "A random new container",
            };

            resourceEnvironment.ResourceContainer_ResourceEnvironment.Add(container);

            StartNextRecorder(model, target, last, ref i, ref recorder);
            // next, we allocate an assembly context to this container
            var allocationCtx = allocation.AllocationContexts_Allocation[0];
            var oldContainer  = allocationCtx.ResourceContainer_AllocationContext;

            allocationCtx.ResourceContainer_AllocationContext = container;
            StartNextRecorder(model, target, last, ref i, ref recorder);
            // to repair the situation, we create a link between the old container and the new one
            var link = new LinkingResource
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "SnailConnection"
            };

            link.ConnectedResourceContainers_LinkingResource.Add(oldContainer);
            link.ConnectedResourceContainers_LinkingResource.Add(container);
            resourceEnvironment.LinkingResources__ResourceEnvironment.Add(link);
            StartNextRecorder(model, target, last, ref i, ref recorder);
            // create a new dummy interface
            var dummyInterface = new OperationInterface
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "Foobar"
            };

            pcmRepository.Interfaces__Repository.Add(dummyInterface);

            StartNextRecorder(model, target, last, ref i, ref recorder);

            // create a dummy component
            var dummyComponent = new BasicComponent
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "Dummy"
            };
            var realAssembly  = allocationCtx.AssemblyContext_AllocationContext;
            var realComponent = realAssembly.EncapsulatedComponent__AssemblyContext;
            var realProvided  = realComponent.ProvidedRoles_InterfaceProvidingEntity[0] as IOperationProvidedRole;
            var realInterface = realProvided.ProvidedInterface__OperationProvidedRole;
            var requireReal   = new OperationRequiredRole
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "Required",
                RequiredInterface__OperationRequiredRole = realInterface
            };

            dummyComponent.RequiredRoles_InterfaceRequiringEntity.Add(requireReal);

            var requireFake = new OperationRequiredRole
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "Fake",
                RequiredInterface__OperationRequiredRole = dummyInterface
            };

            dummyComponent.RequiredRoles_InterfaceRequiringEntity.Add(requireFake);
            pcmRepository.Components__Repository.Add(dummyComponent);

            StartNextRecorder(model, target, last, ref i, ref recorder);

            // create an assembly for the dummy
            var dummyAssembly = new AssemblyContext
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "DummyAssembly",
                EncapsulatedComponent__AssemblyContext = dummyComponent
            };

            system.AssemblyContexts__ComposedStructure.Add(dummyAssembly);

            // create a connector from the dummy to a real assembly
            var connector = new AssemblyConnector
            {
                Id         = Guid.NewGuid().ToString(),
                EntityName = "A connector",
                ProvidedRole_AssemblyConnector             = realProvided,
                ProvidingAssemblyContext_AssemblyConnector = realAssembly,
                RequiredRole_AssemblyConnector             = requireFake,
                RequiringAssemblyContext_AssemblyConnector = dummyAssembly
            };

            system.Connectors__ComposedStructure.Add(connector);

            StartNextRecorder(model, target, last, ref i, ref recorder);
            // fix the connector
            connector.RequiredRole_AssemblyConnector = requireReal;
            last = true;
            StartNextRecorder(model, target, last, ref i, ref recorder);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 解析代码并格式化,返回出错行,返回-1则为无错
        /// </summary>
        int ParsingCode(string codeFilePath)
        {
            Regex  regex        = new Regex(@"\r*\n* *#+.*\r*\n");
            string noCommentStr = regex.Replace(codeContent, "\n");

            string[] lineStr = Regex.Split(noCommentStr, @"\n");
            for (int i = 0; i < lineStr.Length; i++)
            {
                lineStr[i] = new Regex(@"\s*#+.*\r*\n*").Replace(lineStr[i], "\n");
                Console.WriteLine(lineStr[i]);
            }
            //删除空字符串数组
            lineStr = lineStr.Where(s => !string.IsNullOrEmpty(s.Trim())).ToArray();

            Stack <char> stack = new Stack <char>();

            for (int i = 0; i < lineStr.Length; i++)
            {
                //去掉尾部空行
                lineStr[i] = lineStr[i].TrimEnd();
                lineStr[i] = Regex.Replace(lineStr[i], @" *= *{ *\n*", " = {\n");
                lineStr[i] = Regex.Replace(lineStr[i], @" *\n*} *\n*", "\n}\n");
                //获取匹配字符串数组,复制下来加上回车,删除之前的旧字符串不就好了

                for (int stackIndex = 0; stackIndex < lineStr[i].Length; stackIndex++)
                {
                    switch (lineStr[i][stackIndex])
                    {
                    case '{':
                        stack.Push(lineStr[i][stackIndex]);
                        //找到括号且无开始行,设置开始行
                        if (codeStartLine == -1)
                        {
                            codeStartLine = i;
                        }
                        break;

                    case '}':
                        if (stack.Count > 0 && stack.Pop() == '{')
                        {
                            //完全匹配,且有开始行
                            if (stack.Count == 0 && codeStartLine > -1)
                            {
                                codeEndLine = i;
                                string ec = "";
                                while (codeStartLine <= codeEndLine)
                                {
                                    ec += lineStr[codeStartLine] + "\n";
                                    codeStartLine++;
                                }
                                countryEvent.Add(ec);
                                codeStartLine = -1;
                                codeEndLine   = -1;
                            }
                            break;
                        }
                        else
                        {
                            return(i);
                        }

                    default:
                        continue;
                    }
                }

                //当前行没有括号且正在匹配括号为0
                if (!Regex.IsMatch(lineStr[i], @"{+") && !Regex.IsMatch(lineStr[i], @"}+"))
                {
                    //该行没有括号且正在匹配括号为0
                    if (stack.Count == 0)
                    {
                        countryEvent.Add(lineStr[i] + "\n");
                    }
                }
            }

            if (stack.Count == 0)
            {
                editorManager.Clear();
                string str = "";
                foreach (var item in countryEvent)
                {
                    str += item + "\n";
                }
                editorManager.LoadDataString(str, codeFilePath);

                //解析为YAML
                OperationInterface.SetStatusText("解析代码中,请稍后……");
                var yaml = TxtResolveToYaml(codeFilePath);
                //editorManager.LoadDataString(yaml, codeFilePath);
                OperationInterface.SetStatusText("解码为YAML成功!");

                mainWindow.modClassTree.Items.Clear();
                new YAMLForm(Path.GetDirectoryName(codeFilePath) + "\\" + Path.GetFileNameWithoutExtension(codeFilePath) + Util.extension, mainWindow, editorManager);

                return(-1);
            }
            else
            {
                //缺少右括号
                return(-2);
            }
        }