protected void GenerateCodeForWorkflow(IFlowSharpCodeService codeService, StringBuilder sb, GraphicElement el, int indent)
        {
            string strIndent = new String('\t', indent);

            while (el != null)
            {
                if ((el is IIfBox))
                {
                    // True clause
                    var elTrue = codeService.GetTruePathFirstShape((IIfBox)el);
                    // False clause
                    var elFalse = codeService.GetFalsePathFirstShape((IIfBox)el);

                    if (elTrue != null)
                    {
                        sb.AppendLine(strIndent + "bool " + el.Text.ToLower() + " = workflow." + el.Text + "(packet);");
                        sb.AppendLine();
                        sb.AppendLine(strIndent + "if (" + el.Text.ToLower() + ")");
                        sb.AppendLine(strIndent + "{");
                        GenerateCodeForWorkflow(codeService, sb, elTrue, indent + 1);
                        sb.AppendLine(strIndent + "}");

                        if (elFalse != null)
                        {
                            sb.AppendLine(strIndent + "else");
                            sb.AppendLine(strIndent + "{");
                            GenerateCodeForWorkflow(codeService, sb, elFalse, indent + 1);
                            sb.AppendLine(strIndent + "}");
                        }
                    }
                    else if (elFalse != null)
                    {
                        sb.AppendLine(strIndent + "bool " + el.Text.ToLower() + " = workflow." + el.Text + "(packet);");
                        sb.AppendLine();
                        sb.AppendLine(strIndent + "if (!" + el.Text.ToLower() + ")");
                        sb.AppendLine(strIndent + "{");
                        GenerateCodeForWorkflow(codeService, sb, elFalse, indent + 1);
                        sb.AppendLine(strIndent + "}");
                    }

                    // TODO: How to join back up with workflows that rejoin from if-then-else?
                    break;
                }
                else
                {
                    sb.AppendLine(strIndent + "workflow." + el.Text + "(packet);");
                }

                el = codeService.NextElementInWorkflow(el);
            }
        }
Пример #2
0
        // ===================================================

        // DRAKON workflow

        public GraphicElement ParseDrakonWorkflow(DrakonCodeTree dcg, IFlowSharpCodeService codeService, BaseController canvasController, GraphicElement el, bool inCondition = false)
        {
            while (el != null)
            {
                // If we're in a conditional and we encounter a shape with multiple "merge" connections, then we assume (I think rightly so)
                // that this is the end of the conditional branch, and that code should continue at this point outside of the "if-else" statement.
                if (inCondition)
                {
                    var connections = el.Connections.Where(c => c.ElementConnectionPoint.Type == GripType.TopMiddle);

                    if (connections.Count() > 1)
                    {
                        return(el);
                    }
                }

                // All these if's.  Yuck.
                if (el is IBeginForLoopBox)
                {
                    var drakonLoop = new DrakonLoop()
                    {
                        Code = ParseCode(el)
                    };
                    dcg.AddInstruction(drakonLoop);
                    var nextEl = codeService.NextElementInWorkflow(el);

                    if (nextEl != null)
                    {
                        el = ParseDrakonWorkflow(drakonLoop.LoopInstructions, codeService, canvasController, nextEl);
                    }
                    else
                    {
                        // TODO: error -- there are no further elements after the beginning for loop box!
                        ServiceManager.Get <IFlowSharpCodeOutputWindowService>().WriteLine("Error: Drakon start 'for' loop does not have any statements!");
                        return(el);
                    }
                }
                else if (el is IEndForLoopBox)
                {
                    return(el);
                }
                else if (el is IIfBox)
                {
                    var drakonIf = new DrakonIf()
                    {
                        Code = ParseCode(el)
                    };
                    dcg.AddInstruction(drakonIf);

                    var elTrue  = codeService.GetTruePathFirstShape((IIfBox)el);
                    var elFalse = codeService.GetFalsePathFirstShape((IIfBox)el);

                    if (elTrue != null)
                    {
                        ParseDrakonWorkflow(drakonIf.TrueInstructions, codeService, canvasController, elTrue, true);
                    }

                    if (elFalse != null)
                    {
                        ParseDrakonWorkflow(drakonIf.FalseInstructions, codeService, canvasController, elFalse, true);
                    }

                    // dcg.AddInstruction(new DrakonEndIf());
                }
                else if (el is IOutputBox)
                {
                    dcg.AddInstruction(new DrakonOutput()
                    {
                        Code = ParseCode(el)
                    });
                }
                else
                {
                    dcg.AddInstruction(new DrakonStatement()
                    {
                        Code = ParseCode(el)
                    });
                }

                el = codeService.NextElementInWorkflow(el);
            }

            return(null);
        }