public static Activity FromXML(DCRGraph parent, XElement labelMapping, List <XElement> markings)
        {
            Activity activity = new Activity();

            activity.ID    = labelMapping.Attribute("eventId").Value;
            activity.Label = labelMapping.Attribute("labelId").Value;
            foreach (XElement mark in markings)
            {
                List <XElement> temp = null;
                switch (mark.Name.ToString())
                {
                case "executed":
                    if ((temp = mark.Elements("event").ToList()) != null)
                    {
                        foreach (XElement eventExecuted in temp)
                        {
                            if (eventExecuted.Attribute("id").Value == activity.ID)
                            {
                                activity.IsExecuted = true;
                                break;
                            }
                        }
                    }
                    break;

                case "included":
                    if ((temp = mark.Elements("event").ToList()) != null)
                    {
                        foreach (XElement eventExecuted in temp)
                        {
                            if (eventExecuted.Attribute("id").Value == activity.ID)
                            {
                                activity.IsIncluded = true;
                                break;
                            }
                        }
                    }
                    break;

                case "pendingResponses":
                    if ((temp = mark.Elements("event").ToList()) != null)
                    {
                        foreach (XElement eventExecuted in temp)
                        {
                            if (eventExecuted.Attribute("id").Value == activity.ID)
                            {
                                activity.IsPending = true;
                                break;
                            }
                        }
                    }
                    break;

                case "globalStore":
                    if ((temp = mark.Elements("event").ToList()) != null)
                    {
                        foreach (XElement eventExecuted in temp)
                        {
                            if (eventExecuted.Attribute("id").Value == activity.ID)
                            {
                                Diagnostics.Warning("Unsupported marking found");
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            activity._isIncluded = activity.IsIncluded;
            activity._isExecuted = activity.IsExecuted;
            activity._isPending  = activity.IsPending;
            activity.Parent      = parent;
            return(activity);
        }
        public static DCRGraph FromXML(XDocument xml)
        {
            DCRGraph graph = new DCRGraph();

            graph.Title = xml.Element("dcrgraph").Attribute("title").Value;
            Diagnostics.Log("Generating graph \"" + graph.Title + "\"");

            List <XElement> relationsXML = xml
                                           .Element("dcrgraph")
                                           .Element("specification")
                                           .Element("constraints")
                                           .Descendants().ToList();

            Diagnostics.Log("Generating relations");
            List <Relation> relations = new List <Relation>();

            foreach (XElement relation in relationsXML)
            {
                foreach (XElement type in relation.Descendants())
                {
                    relations.Add(Relation.FromXML(type));
                }
            }

            List <XElement> activitiesInfo = xml
                                             .Element("dcrgraph")
                                             .Element("specification")
                                             .Element("resources")
                                             .Element("events")
                                             .Elements("event").ToList();
            List <XElement> activitiesLabels = xml
                                               .Element("dcrgraph")
                                               .Element("specification")
                                               .Element("resources")
                                               .Element("labelMappings")
                                               .Elements("labelMapping").ToList();
            List <XElement> activitiesMarking = xml
                                                .Element("dcrgraph")
                                                .Element("runtime")
                                                .Element("marking")
                                                .Elements().ToList();

            Diagnostics.Log("Generating activities");
            foreach (XElement activitiesLabel in activitiesLabels)
            {
                string   id       = activitiesLabel.Attribute("eventId").Value;
                Activity activity = Activity.FromXML(graph, activitiesLabel, activitiesMarking);
                graph.Activities.Add(id, activity);
            }

            Diagnostics.Log("Adding nested activities");
            foreach (XElement info in activitiesInfo)
            {
                if (info.Attribute("type")?.Value == "nesting")
                {
                    string id = info.Attribute("id").Value;
                    foreach (XElement nestedInfo in info.Elements("event"))
                    {
                        string nestedid = nestedInfo.Attribute("id").Value;
                        graph.Activities[id].NestedActivities.Add(graph.Activities[nestedid]);
                    }
                }
            }

            Diagnostics.Log("Adding relations to the appropriate activities");
            foreach (Relation r in relations)
            {
                //To relations
                switch (r.Type)
                {
                case Relation.RelationType.Condition:
                case Relation.RelationType.MileStone:
                    Activity target = graph.Activities[r.TargetID];
                    if (target.NestedActivities.Count > 0)
                    {
                        Queue <Activity> allNested = new Queue <Activity>();
                        foreach (Activity a in target.NestedActivities)
                        {
                            allNested.Enqueue(a);
                        }
                        while (allNested.Count > 0)
                        {
                            Activity nestedActivity = allNested.Dequeue();
                            if (nestedActivity.NestedActivities.Count > 0)
                            {
                                foreach (Activity a in nestedActivity.NestedActivities)
                                {
                                    allNested.Enqueue(a);
                                }
                            }
                            else
                            {
                                Relation r_copy = r.Clone();
                                r_copy.TargetID = nestedActivity.ID;
                                nestedActivity.AddRelation(r_copy);
                            }
                        }
                    }
                    else
                    {
                        target.AddRelation(r);
                    }
                    break;
                }
                //From relations
                switch (r.Type)
                {
                case Relation.RelationType.Response:
                case Relation.RelationType.Include:
                case Relation.RelationType.Exclude:
                case Relation.RelationType.Coresponse:
                case Relation.RelationType.Spawns:
                    Activity source = graph.Activities[r.SourceID];
                    if (source.NestedActivities.Count > 0)
                    {
                        Queue <Activity> allNested = new Queue <Activity>();
                        foreach (Activity a in source.NestedActivities)
                        {
                            allNested.Enqueue(a);
                        }
                        while (allNested.Count > 0)
                        {
                            Activity nestedActivity = allNested.Dequeue();
                            if (nestedActivity.NestedActivities.Count > 0)
                            {
                                foreach (Activity a in nestedActivity.NestedActivities)
                                {
                                    allNested.Enqueue(a);
                                }
                            }
                            else
                            {
                                Relation r_copy = r.Clone();
                                r_copy.SourceID = nestedActivity.ID;
                                nestedActivity.AddRelation(r_copy);
                            }
                        }
                    }
                    else
                    {
                        source.AddRelation(r);
                    }
                    break;
                }
            }

            return(graph);
        }