public static void ToGraphviz(string DotPath, string name, string path, HSMResults items, bool showHeritages, State startup, int level, State stateToHighlight = null) { StringBuilder b = new StringBuilder(); b.AppendLine("digraph G {"); b.AppendLine("rankdir= LR;"); b.AppendLine("node [shape=plaintext];"); foreach (var st in items.States) { bool isStitHilightTarget = false; if (stateToHighlight != null) { isStitHilightTarget = items.Transitions.Any(p => p.From.Equals(stateToHighlight) && p.To != null && p.To.Equals(st)); } //bool non_accessible = isStitHilightTarget || (stateToHighlight != null && st.Equals(stateToHighlight)); //bool accessible = true; //b.AppendLine(st.Label + " [fontcolor="+( non_accessible==false?"gray":"black" )+", style=filled, fillcolor=" + (highLoght != null && st.Equals(highLoght) ? "red" : (isStitHilightTarget ==true? "orange" : "white")) + "]; "); string foreColor = (stateToHighlight != null && st.Equals(stateToHighlight) ? "#" + Colors.CurrentStateForeColor : (isStitHilightTarget == true ? "#" + Colors.AccessibleStatesForeColor : "#" + Colors.InAccessibleStatesForeColor)); string backColor = (stateToHighlight != null && st.Equals(stateToHighlight) ? "#" + Colors.CurrentStateBackColor : (isStitHilightTarget == true ? "#" + Colors.AccessibleStatesBackColor : "#" + Colors.InAccessibleStatesBackColor)); b.AppendLine(st.Name + " [label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"5\"><TR><TD CELLPADDING=\"5\" BGCOLOR=\"" + backColor + "\" ><FONT FACE=\"Arial\" POINT-SIZE=\"12\" COLOR=\"" + foreColor + "\">" + st.Name + "</FONT></TD></TR></TABLE>>]; "); } //if (highLoght!=null) // b.AppendLine(highLoght.Label + " [style=filled, fillcolor=yellow] "); b.AppendLine("_ [shape=point];"); b.AppendLine("_-> " + startup.Name + ";"); foreach (var t in items.Transitions) { var edgeLabel = t.Method.Name + "(" + string.Join(",", t.Method.Parameters.Select(j => j.Name)) + ")"; if (level > 0) { edgeLabel += (t.Condition != null ? "\\n[" + t.Condition + "]" : ""); } if (level > 1) { edgeLabel += (t.Action != null ? "\\n(" + t.Action + ")" : ""); } if (t.IsLocal) { b.AppendLine(t.From.Name + " -> " + t.From.Name + " " + "[label=\"" + edgeLabel + "\",style=dotted,color=\"magenta\"];"); } else { if (stateToHighlight != null && t.From.Name.Equals(stateToHighlight.Name)) { b.AppendLine(t.From.Name + " -> " + t.To.Name + " " + "[label=\"" + edgeLabel + "\",color=\"#" + Colors.OperationPossibleColor + "\", fontcolor =\"#" + Colors.OperationPossibleColor + "\"];"); } else { b.AppendLine(t.From.Name + " -> " + t.To.Name + " " + "[label=\"" + edgeLabel + "\",color=\"#" + Colors.OperationImpossibleColor + "\", fontcolor =\"#" + Colors.OperationImpossibleColor + "\"];"); } } } if (showHeritages) { foreach (var e in items.States) { State ei = e; if (ei.Parent != null) { b.AppendLine(ei.Name + " -> " + ei.Parent.Name + " " + "[color=\"red\"];"); } } } b.AppendLine("}"); string dotfile = path + @"\" + name + (stateToHighlight != null ? "_" + stateToHighlight.Name:"") + ".dot"; File.WriteAllText(dotfile, b.ToString()); ProcessStartInfo pi = new ProcessStartInfo(DotPath, " -Tpng -O " + dotfile); pi.UseShellExecute = false; Process proc = Process.Start(pi); proc.WaitForExit(); }
public void BuildPseudo(IEnumerable <Transition> _userTransitions) { #region Record States StoreState(Startup); foreach (var tr in _userTransitions) { StoreState(tr.From); StoreState(tr.To); } foreach (var etat in states.ToList()) // ToList to create a copy of states from transition { State et = etat; while (et.Parent != null) { StoreState(et.Parent); et = et.Parent; } } // nomage des etats non deja nomes int serieNum = 0; while (states.Any(p => String.IsNullOrWhiteSpace(p.Name))) { var x = states.First(p => String.IsNullOrWhiteSpace(p.Name)); while (states.Where(p => !String.IsNullOrWhiteSpace(p.Name)).Any(p => p.Name.Equals(string.Format(StateNamePattern, serieNum)))) { serieNum++; } x.Name = string.Format(StateNamePattern, serieNum); } #endregion #region Version non reduite level0 = new HSMResults(); level0.States = states.ToList(); // copies level0.Transitions = _userTransitions.ToList(); // copies #endregion #region Reduction / Normalisation des transitions List <Transition> transitions = new List <Transition>(); foreach (var st in states) { List <Transition> stTransitions = new List <Transition>(); State i = st; while (i != null) { var transitionsPossibles = _userTransitions.Where(p => p.From.Equals(i)).ToList(); var methodesPossibles = transitionsPossibles.Select(p => p.Method).Distinct(); List <Method> mthsToAdd = new List <Method>(); foreach (var mp in methodesPossibles) { if (!stTransitions.Any(p => p.Method.Equals(mp))) { mthsToAdd.Add(mp); } } foreach (var mtiToAdd in mthsToAdd) { stTransitions.AddRange(transitionsPossibles.Where(p => p.Method.Equals(mtiToAdd)).Select(h => new Transition() { From = st, To = h.To, Action = h.Action, Condition = h.Condition, Index = h.Index, Method = h.Method })); } i = i.Parent; } transitions.AddRange(stTransitions); } VisitAllStates(transitions, Startup); //suppression des states inutiles { var statesToDelete = states.Where(p => p.Visited == false).ToList(); while (statesToDelete.Count > 0) { //Console.WriteLine("\tsuppresion etat {0}", statesToDelete[0].Label); var trToDelete = transitions.Where(p => p.From.Equals(statesToDelete[0]) || (!p.IsLocal && p.To.Equals(statesToDelete[0]))).ToList(); while (trToDelete.Count > 0) { transitions.Remove(trToDelete[0]); trToDelete.RemoveAt(0); } states.Remove(statesToDelete[0]); statesToDelete.RemoveAt(0); } } //Numerotation des etats for (int i = 0; i < states.Count; i++) { states[i].UniqueId = i; } level1 = new HSMResults(); level1.States = states.ToList(); // copies level1.Transitions = transitions.ToList(); // copies #endregion }