private String parseSwitch(Queue <IXamlElement> token) { FlowSwitch swtch = new FlowSwitch(); // [Start] FlowDecision IXamlElement elem = token.Dequeue(); // Reference String reference; elem.Attributes.TryGetValue("x:Name", out reference); swtch.Id = reference; // Default String def; if (elem.Attributes.TryGetValue("Default", out def)) { swtch.Default = extractReference(def); } while (!token.Peek().QName.Equals(createQName("FlowSwitch"))) { if (token.Peek().QName.Equals(createQName("FlowSwitch.Default"))) { token.Dequeue(); swtch.Default = parseNode(token); token.Dequeue(); } else { swtch.Branches.Add(parseNode(token)); } } // [End] FlowDecision token.Dequeue(); steps.Add(swtch); return(swtch.Id); }
public PetriNet Compile(PetriNet phylum) { String prefix = phylum.ActivityCount + ".internal."; Place p1 = phylum.NewPlace(prefix + "initialized"); Place p2 = phylum.NewPlace(prefix + "closed"); if (startNode == null) { // New Activity phylum.ActivityCount += 1; int currentID = phylum.ActivityCount; // Compile new Empty().Compile(phylum); // Merge phylum.Merge(p1, currentID + ".internal.initialized"); phylum.Merge(p2, currentID + ".internal.closed"); return(phylum); } // Create Steps foreach (FlowNode node in steps) { if (node is FlowStep) { FlowStep step = (FlowStep)node; String prefixStep = prefix + step.Id + "."; Place stp1 = phylum.NewPlace(prefixStep + "start"); Place stp2 = phylum.NewPlace(prefixStep + "next"); phylum.ActivityCount += 1; int currentID = phylum.ActivityCount; // Action Activity step.Action.Compile(phylum); // Connect phylum.Merge(stp1, currentID + ".internal.initialized"); phylum.Merge(stp2, currentID + ".internal.closed"); } else if (node is FlowDecision) { FlowDecision dec = (FlowDecision)node; String prefixStep = prefix + dec.Id + "."; Place dp1 = phylum.NewPlace(prefixStep + "start"); Place condition = phylum.NewPlace(prefixStep + "condition"); Place right = phylum.NewPlace(prefixStep + "true"); Place wrong = phylum.NewPlace(prefixStep + "false"); Transition evalCondition = phylum.NewTransition(prefixStep + "evalcondition"); Transition startTrue = phylum.NewTransition(prefixStep + "starttrue"); Transition startFalse = phylum.NewTransition(prefixStep + "startfalse"); // Connect phylum.NewArc(dp1, evalCondition); phylum.NewArc(evalCondition, condition); phylum.NewArc(condition, startTrue); phylum.NewArc(condition, startFalse); phylum.NewArc(startTrue, right); phylum.NewArc(startFalse, wrong); } else if (node is FlowSwitch) { FlowSwitch swtch = (FlowSwitch)node; String prefixStep = prefix + swtch.Id + "."; Place sp1 = phylum.NewPlace(prefixStep + "start"); Place sdefault = phylum.NewPlace(prefixStep + "default"); // Inner Petri Net Place lastState = sp1; for (int i = 1; i <= swtch.Branches.Count + 1; i++) { if (i <= swtch.Branches.Count) { Place cond = phylum.NewPlace(prefixStep + "condition" + i); Place state = phylum.NewPlace(prefixStep + "case" + i); Transition evalCase = phylum.NewTransition(prefixStep + "evalcase" + i); Transition initCase = phylum.NewTransition(prefixStep + "initcase" + i); phylum.NewArc(lastState, evalCase); phylum.NewArc(evalCase, cond); phylum.NewArc(cond, initCase); phylum.NewArc(initCase, state); // LastState lastState = cond; } else { Transition initDefault = phylum.NewTransition(prefixStep + "initdefault"); phylum.NewArc(lastState, initDefault); phylum.NewArc(initDefault, sdefault); } } } } // Merge // StartNode phylum.Merge(p1, prefix + steps.First(e => e.Id.Equals(startNode)).Id + ".start"); foreach (FlowNode node in steps) { if (node is FlowStep) { FlowStep step = (FlowStep)node; String prefixStep = prefix + step.Id + "."; if (step.Next != null) { phylum.Merge(prefixStep + "next", prefix + step.Next + ".start"); } else { phylum.Merge(p2, prefixStep + "next"); } } else if (node is FlowDecision) { FlowDecision dec = (FlowDecision)node; String prefixDecision = prefix + dec.Id + "."; if (dec.Right != null) { phylum.Merge(prefixDecision + "true", prefix + dec.Right + ".start"); } else { phylum.Merge(p2, prefixDecision + "true"); } if (dec.Wrong != null) { phylum.Merge(prefixDecision + "false", prefix + dec.Wrong + ".start"); } else { phylum.Merge(p2, prefixDecision + "false"); } } else if (node is FlowSwitch) { FlowSwitch swtch = (FlowSwitch)node; String prefixSwitch = prefix + swtch.Id + "."; //Default if (swtch.Default != null) { phylum.Merge(prefixSwitch + "default", prefix + swtch.Default + ".start"); } else { phylum.Merge(p2, prefixSwitch + "default"); } // Branches for (int i = 1; i <= swtch.Branches.Count; i++) { phylum.Merge(prefixSwitch + "case" + i, prefix + swtch.Branches[i - 1] + ".start"); } } } return(phylum); }