/// <summary> /// Converts an activity diagram to a finite state machine. /// </summary> /// <param name="diagram">Diagram to be converted</param> /// <param name="model">Parent model of diagram, used to get sub-diagrams</param> /// <returns>a FSM of diagram</returns> public static FiniteStateMachine ActivityDiagramToFsm(UmlActivityDiagram diagram, UmlModel model) { List <UmlTransition> transitions = diagram.UmlObjects.OfType <UmlTransition>().ToList(); FiniteStateMachine fsm = new FiniteStateMachine(diagram.Name); State source = null; State target = null; String input = ""; String output = ""; Boolean haveHyperlinks = true; List <UmlTransition> newTransitions; while (haveHyperlinks) { newTransitions = new List <UmlTransition>(); foreach (UmlTransition t in transitions) { UmlTransition aux = t; if (t.Source.TaggedValues.ContainsKey("jude.hyperlink")) { newTransitions.AddRange(GetTransitionsOfDiagram(model, ref aux, hyperLinkType.Source)); } if (t.Target.TaggedValues.ContainsKey("jude.hyperlink")) { newTransitions.AddRange(GetTransitionsOfDiagram(model, ref aux, hyperLinkType.Target)); } } transitions.AddRange(newTransitions); transitions = transitions.Distinct().ToList(); haveHyperlinks = transitions.Where(x => x.Source.TaggedValues.ContainsKey("jude.hyperlink") || x.Target.TaggedValues.ContainsKey("jude.hyperlink")).Count() > 0; } RemoveForks(ref diagram, ref transitions); RemoveDecisions(ref diagram, ref transitions); foreach (UmlTransition t in transitions) { input = t.GetTaggedValue("TDACTION"); source = new State(t.Source.Name); source.Id = t.Source.Id; if (input != null) { target = new State(t.Target.Name); target.Id = t.Target.Id; output = ""; if (t.GetTaggedValue("TDPARAMETERS") != null) { output = t.GetTaggedValue("TDPARAMETERS"); } fsm.AddTransition(new Transition(source, target, input, output)); } if (t.Target is UmlFinalState) { fsm.CheckAsFinal(source); } } foreach (Transition t in fsm.Transitions) { State s = fsm.States .Where(x => x.Name.Equals(t.SourceState.Name)) .FirstOrDefault(); s.Transitions.Add(t); } fsm = WipeOutOutermost(diagram, fsm); fsm.InitialState = getInitial(fsm); return(fsm); }
/// <summary> /// Converts an activity diagram to a finite state machine. /// </summary> /// <param name="diagram">Diagram to be converted</param> /// <param name="model">Parent model of diagram, used to get sub-diagrams</param> /// <returns>a FSM of diagram</returns> public FiniteStateMachine ActivityDiagramToFsm(UmlActivityDiagram diagram, UmlModel model) { List <UmlTransition> transitions = diagram.UmlObjects.OfType <UmlTransition> ().ToList(); List <UmlTransition> newTransitions; //FiniteStateMachine fsm = new FiniteStateMachine(diagram.Name); FiniteStateMachine fsm = new FiniteStateMachine(); State source = null; State target = null; String input = ""; String output = ""; Boolean haveHiperlinks = true; while (haveHiperlinks) { newTransitions = new List <UmlTransition> (); foreach (UmlTransition t in transitions) { UmlTransition aux = new UmlTransition(); aux = t; List <UmlTransition> hyperlinkTrans = null; if (t.Source.TaggedValues.ContainsKey("jude.hyperlink")) { hyperlinkTrans = GetTransitionsOfDiagram(model, ref aux, hyperLinkType.Source); } if (hyperlinkTrans != null) { newTransitions.AddRange(hyperlinkTrans); } hyperlinkTrans = null; if (t.Target.TaggedValues.ContainsKey("jude.hyperlink")) { hyperlinkTrans = GetTransitionsOfDiagram(model, ref aux, hyperLinkType.Target); } if (hyperlinkTrans != null) { newTransitions.AddRange(hyperlinkTrans); } } #region new UmlDecision ID - unsuccessful List <UmlDecision> ignoreList = new List <UmlDecision> (); foreach (UmlTransition newT in newTransitions) { UmlElement src = newT.Source; UmlElement trg = newT.Target; if (src is UmlDecision) { if (!ignoreList.Contains(src)) { List <UmlDecision> decs = (from t in newTransitions where t.Source.Name.Equals(src.Name) select(UmlDecision) t.Source).Distinct().ToList(); decs.AddRange((from t in newTransitions where t.Target.Name.Equals(src.Name) select(UmlDecision) t.Target).Distinct().ToList()); String decID = Guid.NewGuid().ToString(); foreach (UmlDecision d in decs) { d.Id = decID; } ignoreList.AddRange(decs); } } if (trg is UmlDecision) { if (!ignoreList.Contains(trg)) { List <UmlDecision> decs = (from t in newTransitions where t.Target.Name.Equals(trg.Name) select(UmlDecision) t.Target).Distinct().ToList(); decs.AddRange((from t in newTransitions where t.Source.Name.Equals(trg.Name) select(UmlDecision) t.Source).Distinct().ToList()); String decID = Guid.NewGuid().ToString(); foreach (UmlDecision d in decs) { d.Id = decID; } ignoreList.AddRange(decs); } } } #endregion transitions.AddRange(newTransitions); transitions = transitions.Distinct().ToList(); haveHiperlinks = transitions.Where(x => x.Source.TaggedValues.ContainsKey("jude.hyperlink") || x.Target.TaggedValues.ContainsKey("jude.hyperlink")).Count() > 0; } RemoveForks(ref diagram, ref transitions); RemoveDecisions(ref diagram, ref transitions); UmlTransition auxTran = transitions.Where(x => x.Source is UmlInitialState).FirstOrDefault(); List <UmlTransition> auxList = new List <UmlTransition> (); auxList.Add(auxTran); for (int i = 0; i < transitions.Count; i++) { auxTran = transitions.Where(x => x.Source.Equals(auxTran.Target)).FirstOrDefault(); if (auxTran != null) { auxList.Add(auxTran); } } transitions.Clear(); transitions.AddRange(auxList); foreach (UmlTransition t in transitions) { input = t.GetTaggedValue("TDACTION"); source = new State(t.Source.Name); source.Id = t.Source.Id; if (input != null) { target = new State(t.Target.Name); target.Id = t.Target.Id; if ((((UmlActionState)t.Target).ParentLane != null) && !String.IsNullOrEmpty(((UmlActionState)t.Target).ParentLane.Name)) { target.TaggedValues.Add("Lane", ((UmlActionState)t.Target).ParentLane.Name); } output = ""; if (t.GetTaggedValue("TDEXPECTEDRESULT") != null) { output = t.GetTaggedValue("TDEXPECTEDRESULT"); } #region Cycles bool cycleTran = false; if (t.GetTaggedValue("TDCYCLETRAN") != null) { cycleTran = (t.GetTaggedValue("TDCYCLETRAN").Equals("true") ? true : false); } bool lastCycleTrans = false; if (t.GetTaggedValue("TDLASTCYCLETRANS") != null) { //lastCycleTrans = (t.GetTaggedValue("TDCYCLETRAN").Equals("true") ? true : false); lastCycleTrans = (t.GetTaggedValue("TDLASTCYCLETRANS").Equals("true") ? true : false); } Transition trans = new Transition(source, target, input, output, cycleTran, lastCycleTrans); if (t.GetTaggedValue("TDLASTCYCLETRANS") != null) { trans.TaggedValues.Add("TDCYCLETRAN", t.GetTaggedValue("TDCYCLETRAN")); } #endregion foreach (KeyValuePair <String, String> pair in t.TaggedValues) { trans.TaggedValues.Add(pair.Key, pair.Value); } //trans.TaggedValues.Add("TDACTION", t.GetTaggedValue("TDACTION")); //trans.TaggedValues.Add("TDEXPECTEDRESULT", t.GetTaggedValue("TDEXPECTEDRESULT") + ""); fsm.AddTransition(trans); } if (t.Target is UmlFinalState) { fsm.CheckAsFinal(source); } } fsm = WipeOutOutermost(diagram, fsm); fsm.InitialState = GetFsmInitialState(fsm); fsm.Name = diagram.Name; return(fsm); }