Ejemplo n.º 1
0
    public static DirectedEdgedGraph <string, string> ToDirectedGraph()
    {
        DirectedEdgedGraph <string, string> result = new DirectedEdgedGraph <string, string>();

        void Add(string from, string to, OperationSymbol key)
        {
            Dictionary <string, string> dic = result.TryRelatedTo(from);

            if (dic == null || !dic.ContainsKey(to))
            {
                result.Add(from, to, key.ToString());
            }
            else
            {
                result.Add(from, to, dic[to] + ", " + key.ToString());
            }
        }

        foreach (var item in OperationLogic.GraphOperations <T, S>())
        {
            switch (item.OperationType)
            {
            case OperationType.Execute:
            {
                Execute gOp = (Execute)item;

                foreach (var f in gOp.FromStates)
                {
                    foreach (var t in gOp.ToStates)
                    {
                        Add(f !.ToString() !, t !.ToString() !, item.OperationSymbol);
                    }
                }
            } break;

            case OperationType.Delete:
            {
                Delete dOp = (Delete)item;
                foreach (var f in dOp.FromStates)
                {
                    Add(f !.ToString() !, "[Deleted]", item.OperationSymbol);
                }
            } break;

            case OperationType.Constructor:
            case OperationType.ConstructorFrom:
            case OperationType.ConstructorFromMany:
            {
                string from = item.OperationType == OperationType.Constructor ? "[New]" :
                              item.OperationType == OperationType.ConstructorFrom ? "[From {0}]".FormatWith(item.GetType().GetGenericArguments()[2].TypeName()) :
                              item.OperationType == OperationType.ConstructorFromMany ? "[FromMany {0}]".FormatWith(item.GetType().GetGenericArguments()[2].TypeName()) : "";

                var dtoState = (IGraphToStateOperation)item;
                foreach (var t in dtoState.ToStates)
                {
                    Add(from, t !.ToString() !, item.OperationSymbol);
                }
            } break;
            }
        }

        return(result);
    }