public byte[] ExportArrowGraphToDiagram(DiagramArrowGraphDto diagramArrowGraphDto)
        {
            if (diagramArrowGraphDto == null)
            {
                throw new ArgumentNullException(nameof(diagramArrowGraphDto));
            }
            graphml graphML = GraphMLBuilder.ToGraphML(diagramArrowGraphDto);

            byte[] output = null;
            using (var ms = new MemoryStream())
            {
                var xmlSerializer = new XmlSerializer(typeof(graphml));
                xmlSerializer.Serialize(ms, graphML);
                output = ms.ToArray();
            }
            return(output);
        }
Exemplo n.º 2
0
        private StringBuilder ParseModel(string model, string className)
        {
            Dictionary<string, string> states = new Dictionary<string, string>();
            ArrayList acceptingState = new ArrayList();
            string endStateID = null;

            string modifier = "static";
            string modelType = "model";

            StringReader strModel = new StringReader(model);
            // Get the graph tab of the xml into the graphmlGraph object 
            XmlSerializer graphmlSerializer = new XmlSerializer(typeof(graphml));
            graphml Model = (graphml)graphmlSerializer.Deserialize(strModel);
            graphmlGraph graph = null;
            foreach (object item in Model.Items)
            {
                if (item.GetType().Name.Equals("graphmlGraph"))
                {
                    graph = (graphmlGraph)item;
                    break;
                }
            }

            // Start creating the C# code in a StringBuilder object
            codeModel = new StringBuilder();

            codeModel.AppendLine("using System;");
            codeModel.AppendLine("using System.Collections;");
            codeModel.AppendLine("using System.Collections.Generic;");
            codeModel.AppendLine("using NModel.Attributes;");
            codeModel.AppendLine("using NModel;");
            codeModel.AppendLine("using NModel.Execution;");
            codeModel.AppendLine("");
            codeModel.AppendLine("namespace " + _namespace);
            codeModel.AppendLine("{");

            codeModel.AppendLine("");
            
            // Make sure that Model_Config is parsed first
            foreach (graphmlGraphEdge edge in graph.edge)
            {
                foreach (data dt in edge.data)
                {
                    if ((dt.PolyLineEdge != null) && (dt.PolyLineEdge.EdgeLabel != null))
                    {
                        foreach (PolyLineEdgeEdgeLabel edgeLabel in dt.PolyLineEdge.EdgeLabel)
                        {
                            if (edgeLabel.Value != null)
                            {
                                string[] strCode = edgeLabel.Value.Split('\n');

                                string methodName = getBlock(strCode, "name")[0].ToLower();

                                if (methodName.Equals("model_config"))
                                {
                                    string[] configBlock = getBlock(strCode, "config");
                                    for (int curLine = 0; curLine < configBlock.Length; ++curLine)
                                    {
                                        if (configBlock[curLine].Trim().StartsWith("modifier"))
                                            modifier = (configBlock[curLine].Split(':')[1].Trim());
                                        if(configBlock[curLine].Trim().StartsWith("model_type"))
                                            modelType = (configBlock[curLine].Split(':')[1].Trim());
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (modelType == "model")
                ModelStatesEnum(graph, ref states, ref endStateID);

            if (modelType == "feature")
                codeModel.AppendLine("    [Feature]");

            // Class modifier and name:
            codeModel.AppendLine("    " + modifier + " class " + className);
            codeModel.AppendLine("    {");

            // Make sure that Model_Init is parsed second
            foreach (graphmlGraphEdge edge in graph.edge)
            {
                foreach (data dt in edge.data)
                {
                    if ((dt.PolyLineEdge != null) && (dt.PolyLineEdge.EdgeLabel != null))
                    {
                        foreach (PolyLineEdgeEdgeLabel edgeLabel in dt.PolyLineEdge.EdgeLabel)
                        {
                            if (edgeLabel.Value != null)
                            {
                                string[] strCode = edgeLabel.Value.Split('\n');
                                string methodName = getBlock(strCode, "name")[0].ToLower();
                                if (methodName.Equals("model_init"))
                                {
                                    if (modelType == "model")
                                        codeModel.AppendLine("        " + modifier + " ModelStates _curState = ModelStates." + states[edge.target].Trim() + ";");
                                    string[] initBlock = getBlock(strCode, "init");
                                    for (int curLine = 0; curLine < initBlock.Length; ++curLine)
                                    {
                                        if (initBlock[curLine].Trim() != "")
                                            codeModel.AppendLine("        " + initBlock[curLine].Trim());
                                    }
                                    codeModel.AppendLine("");                                    
                                }
                            }
                        }
                    }
                }
            }


            foreach (graphmlGraphEdge edge in graph.edge)
            {
                foreach (data dt in edge.data)
                {
                    if ((dt.PolyLineEdge != null) && (dt.PolyLineEdge.EdgeLabel != null))
                    {
                        foreach (PolyLineEdgeEdgeLabel edgeLabel in dt.PolyLineEdge.EdgeLabel)
                        {
                            if (edgeLabel.Value != null)
                            {                                
                                string[] strCode = edgeLabel.Value.Split('\n');

                                string methodName = getBlock(strCode, "name")[0].ToLower();
                                string[] parametersBlock = getBlock(strCode, "parameters");
                                string[] reqBlock = getBlock(strCode, "req");
                                string[] guardBlock = getBlock(strCode, "guard");
                                string[] actionBlock = getBlock(strCode, "action");

                                if ((!methodName.Equals("model_config")) && (!methodName.Equals("model_init")))
                                {
                                    codeModel.AppendLine("        [Action]");
                                    for (int curLine = 0; curLine < reqBlock.Length; ++curLine)
                                    {
                                        if (reqBlock[curLine].Trim() != "")
                                            codeModel.AppendLine("        [Requirement(" + reqBlock[curLine].Trim() + ")]");
                                    }                                    

                                    codeModel.AppendLine("        " + modifier + " void " + methodName + "(");
                                    for (int curLine = 0; curLine < parametersBlock.Length; ++curLine)
                                    {
                                        if (parametersBlock[curLine].Trim() != "")
                                            codeModel.AppendLine("                " + parametersBlock[curLine].Trim());
                                    }
                                    codeModel.AppendLine("        )");

                                    codeModel.AppendLine("        {");

                                    for (int curLine = 0; curLine < actionBlock.Length; ++curLine)
                                    {
                                        if (actionBlock[curLine].Trim() != "")
                                            codeModel.AppendLine("            " + actionBlock[curLine].Trim());
                                    }

                                    if (states.ContainsKey(edge.target))
                                        codeModel.AppendLine("            _curState = ModelStates." + states[edge.target].Trim() + ";");

                                    codeModel.AppendLine("        }");

                                    codeModel.AppendLine("        " + modifier + " bool " + methodName.Trim() + "Enabled" + "(");
                                    for (int curLine = 0; curLine < parametersBlock.Length; ++curLine)
                                    {
                                        if (parametersBlock[curLine].Trim() != "")
                                            codeModel.AppendLine("                " + parametersBlock[curLine].Trim());
                                    }
                                    codeModel.AppendLine("        )");

                                    codeModel.AppendLine("        {");
                                    AppendCommentsInBlock(guardBlock);
                                    SetBooleanBlock(guardBlock);
                                    if ( (modelType == "model") && (states.ContainsKey(edge.source)) ) // No key for Start and Model_Configured
                                        codeModel.AppendLine("            (_curState == ModelStates." + states[edge.source].Trim() + ")");
                                    else if (modelType == "feature")
                                        codeModel.AppendLine("            true");
                                    codeModel.AppendLine("            );");
                                    codeModel.AppendLine("        }");
                                    codeModel.AppendLine("");
                                }                                
                            }
                            else if ( (modelType == "model") && edge.target.Equals(endStateID) )
                            {
                                acceptingState.Add("_curState == ModelStates." + states[edge.source].Trim());
                            }
                        }
                    }
                }
            }

            // Set the accepting state
            if ( (modelType == "model") && (acceptingState.Count > 0) )
            {
                codeModel.AppendLine("        [AcceptingStateCondition]");
                codeModel.AppendLine("        " + modifier + " bool IsAcceptingState()");
                codeModel.AppendLine("        {");
                codeModel.AppendLine("            return(");

                string[] acceptingStateArray = new string[acceptingState.Count];
                acceptingState.CopyTo(acceptingStateArray, 0);
                for (int curLine = 0; curLine < acceptingStateArray.Length; ++curLine)
                {                    
                    codeModel.AppendLine("            (" + acceptingStateArray[curLine].Trim() + ") ||");
                }
                codeModel.AppendLine("            false);");
                codeModel.AppendLine("        }");
                codeModel.AppendLine("");
            }


            codeModel.AppendLine("    }");
            codeModel.AppendLine("}");
            return (codeModel);
        }