Exemplo n.º 1
0
        private static Activity ParseActivity(DcrGraph graph, XDocument doc, XElement eve)
        {
            var nestedEvents = eve.Descendants("event").Where(element => element.HasElements).ToList(); //Only takes event elements in events!

            bool isNestedGraph = nestedEvents.Count > 0;

            // Retrieve Id
            var id = eve.Attribute("id").Value;

            // Retrieve Name:
            var name = (from labelMapping in doc.Descendants("labelMapping")
                        where labelMapping.Attribute("eventId").Value.Equals(id)
                        select labelMapping.Attribute("labelId").Value).FirstOrDefault();

            // Check to see if Activity was already parsed
            if (graph.GetActivities().ToList().Exists(x => x.Id == id && x.Name == name)) return null;

            Activity activityToReturn;

            if (isNestedGraph)
            {
                var nestedActivities = new HashSet<Activity>();
                foreach (var nestedEvent in nestedEvents)
                {
                    nestedActivities.Add(ParseActivity(graph, doc, nestedEvent));
                }

                activityToReturn = graph.MakeNestedGraph(id, name, nestedActivities);
            }
            else // Not a nested graph --> Treat as single activity
            {
                // Add activity to graph
                activityToReturn = graph.AddActivity(id, name);

                // Assigning Roles:
                var roles = eve.Descendants("role");
                var rolesList = new List<string>();
                foreach (var role in roles)
                {
                    if (role.Value != "") graph.AddRolesToActivity(id,role.Value);
                }

                // Mark Included
                if ((from includedEvent in doc.Descendants("included").Elements()
                     select includedEvent.FirstAttribute.Value).Contains(id)) graph.SetIncluded(true, id);

                // Mark Pending:
                if ((from pendingEvent in doc.Descendants("pendingResponses").Elements()
                     select pendingEvent.FirstAttribute.Value).Contains(id)) graph.SetPending(true, id);

                // Mark Executed:
                if ((from executedEvent in doc.Descendants("executed").Elements()
                     select executedEvent.FirstAttribute.Value).Contains(id)) graph.SetExecuted(true, id);
            }

            return activityToReturn;
        }
        public DcrGraph RemoveRedundancy(DcrGraph inputGraph, BackgroundWorker worker = null)
        {
            _worker = worker;
            #if DEBUG
            Console.WriteLine("Started redundancy removal:");
            #endif

            //TODO: use an algorithm to check if the graph is connected and if not then recursively remove redundancy on the subgraphs.
            //temporarily remove flower activities.
            var copy = inputGraph.Copy();

            var removedActivities =
                copy.GetActivities().Where(x => (x.Included && !copy.ActivityHasRelations(x))).ToList();

            foreach (var a in removedActivities)
            {
                copy.RemoveActivity(a.Id);
            }

            var byteDcrGraph = new ByteDcrGraph(copy);

            _uniqueTraceFinder = new UniqueTraceFinder(byteDcrGraph);

            _originalInputDcrGraph = copy.Copy();
            OutputDcrGraph = copy;

            // Remove relations and see if the unique traces acquired are the same as the original. If so, the relation is clearly redundant and is removed immediately
            // All the following calls potentially alter the OutputDcrGraph

            RemoveRedundantRelations(RelationType.Response);

            RemoveRedundantRelations(RelationType.Condition);

            RemoveRedundantRelations(RelationType.InclusionExclusion);

            RemoveRedundantRelations(RelationType.Milestone);

            foreach (var activity in OutputDcrGraph.GetActivities())
            {
                var graphCopy = new ByteDcrGraph(byteDcrGraph);

                graphCopy.RemoveActivity(activity.Id);

                ReportProgress?.Invoke("Removing Activity " + activity.Id);

                // Compare unique traces - if equal activity is redundant
                if (_uniqueTraceFinder.CompareTraces(graphCopy))
                {
                    // The relation is redundant, replace  copy with current copy (with the relation removed)
                    OutputDcrGraph.RemoveActivity(activity.Id);
                }

            }

            foreach (var a in removedActivities)
            {
                OutputDcrGraph.AddActivity(a.Id, a.Name);
                OutputDcrGraph.SetIncluded(true, a.Id);
                OutputDcrGraph.SetPending(a.Pending, a.Id);
            }
            var nested = OutputDcrGraph.ExportToXml();

            return OutputDcrGraph;
        }