public static async Task<DrawingImage> Retrieve(DcrGraph graph)
        {
            var body = "src=" + graph.ExportToXml();

            var tempFilePath = Path.Combine(Path.GetTempPath(), "SaveFile.svg");

            using (WebClient wc = new WebClient()) 
            {
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                //var encodedBody = SharpVectors.HttpUtility.UrlEncode(body);
                //if (encodedBody == null)
                //{
                //    return null;
                //}
                    
                var encodedBody = Regex.Replace(body, @"[^\w\s<>/""=]", "");
                //var encodedBody = body.Replace(" & ", "and"); // TODO: Replace all illegal characters...?
                if (encodedBody.Contains("_")) encodedBody = encodedBody.Replace("_", "");

                var result = await wc.UploadStringTaskAsync("http://dcr.itu.dk:8023/trace/dcr", encodedBody);
                    
                //TODO: don't save it as a file
                System.IO.File.WriteAllText(tempFilePath, result);
                    
            }


            //conversion options
            WpfDrawingSettings settings = new WpfDrawingSettings();
            settings.IncludeRuntime = true;
            settings.TextAsGeometry = true;

            FileSvgReader converter = new FileSvgReader(settings);

            var xamlFile = converter.Read(tempFilePath);


            return new DrawingImage(xamlFile);
        }
        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;
        }
        public void TestExportDcrGraphToXml()
        {
            var activities = new HashSet<Activity> { new Activity("A", "somename1"), new Activity("B", "somename2"), new Activity("C", "somename3") };
            var graph = new DcrGraph();

            foreach (var a in activities)
            {
                graph.AddActivity(a.Id, a.Name);
            }

            graph.SetIncluded(true, "A"); // Start at A

            graph.AddIncludeExclude(true, "A", "B");
            graph.AddIncludeExclude(true, "A", "C");
            graph.AddIncludeExclude(true, "B", "C");

            graph.AddIncludeExclude(false, "C", "B");
            graph.AddIncludeExclude(false, "A", "A");
            graph.AddIncludeExclude(false, "B", "B");
            graph.AddIncludeExclude(false, "C", "C");

            Console.WriteLine(graph);

            var xml = graph.ExportToXml();
            Console.WriteLine(xml);

            File.WriteAllText("E:/DCR2XML.xml", xml);

            Console.ReadLine();
        }
        public void ParseMortgageApplication2()
        {
            var graph = new DcrGraph();

            graph.AddActivities(new Activity("Collect Documents", "Collect Documents") { Included = true, Roles = "Caseworker" });
            graph.AddIncludeExclude(false, "Collect Documents", "Collect Documents");

            graph.AddActivities(new Activity("Irregular neighbourhood", "Irregular neighbourhood") { Included = true, Roles = "it" });
            graph.AddIncludeExclude(false, "Irregular neighbourhood", "Irregular neighbourhood");

            graph.AddActivities(new Activity("Make appraisal appointment", "Make appraisal appointment") { Included = true, Roles = "Mobile consultant" });
            graph.AddIncludeExclude(false, "Make appraisal appointment", "Make appraisal appointment");

            graph.AddActivities(new Activity("Appraisal audit", "Appraisal audit") { Included = true, Roles = "Auditor" });
            graph.AddIncludeExclude(false, "Appraisal audit", "Appraisal audit");

            graph.AddActivities(new Activity("On-site appraisal", "On-site appraisal") { Included = true, Roles = "Mobile consulant" });
            graph.AddIncludeExclude(false, "On-site appraisal", "On-site appraisal");

            graph.AddActivities(new Activity("Submit budget", "Submit budget") { Included = true, Roles = "Customer" });
            graph.AddIncludeExclude(false, "Submit budget", "Submit budget");

            graph.AddActivities(new Activity("Budget screening approve", "Budget screening approve") { Included = true, Pending = true, Roles = "Intern" });
            graph.AddIncludeExclude(false, "Budget screening approve", "Budget screening approve");

            graph.AddActivities(new Activity("Statistical appraisal", "Statistical appraisal") { Included = true, Roles = "Caseworker" });
            graph.AddIncludeExclude(false, "Statistical appraisal", "Statistical appraisal");

            graph.AddActivities(new Activity("Assess loan application", "Assess loan application") { Included = true, Pending = true, Roles = "Caseworker" });
            graph.AddIncludeExclude(false, "Assess loan application", "Assess loan application");

            graph.AddCondition("Collect Documents", "Irregular neighbourhood");
            graph.AddCondition("Collect Documents", "Make appraisal appointment");
            graph.AddCondition("Collect Documents", "On-site appraisal");
            graph.AddCondition("Collect Documents", "Statistical appraisal");

            graph.AddIncludeExclude(false, "Statistical appraisal", "Irregular neighbourhood");
            graph.AddIncludeExclude(false, "Statistical appraisal", "Make appraisal appointment");
            graph.AddIncludeExclude(false, "Statistical appraisal", "On-site appraisal");

            graph.AddIncludeExclude(false, "Irregular neighbourhood","Statistical appraisal");
            graph.AddIncludeExclude(false, "Make appraisal appointment","Statistical appraisal" );
            graph.AddIncludeExclude(false, "On-site appraisal","Statistical appraisal");

            graph.AddCondition("Irregular neighbourhood", "Make appraisal appointment");
            graph.AddCondition("Make appraisal appointment", "On-site appraisal");
            graph.AddCondition("On-site appraisal", "Submit budget");
            graph.AddCondition("Submit budget", "Budget screening approve");
            graph.AddCondition("Budget screening approve", "Assess loan application");
            graph.AddCondition("Assess loan application","Appraisal audit");

            graph.AddCondition("Statistical appraisal", "Submit budget");

            var nested = new HashSet<Activity>();
            nested.Add(graph.GetActivity("Irregular neighbourhood"));
            nested.Add(graph.GetActivity("Make appraisal appointment"));
            nested.Add(graph.GetActivity("On-site appraisal"));

            graph.MakeNestedGraph(nested);

            //LogGenerator9001 logGenerator9001 = new LogGenerator9001(20, graph);

            //Log log = new Log();

            //foreach (var trace in logGenerator9001.GenerateLog(500))
            //{
            //    log.AddTrace(trace);
            //}

            using (StreamWriter sw = new StreamWriter("C:/Downloads/mortgageStrict.xml"))
            {
                sw.WriteLine(graph.ExportToXml());
            }
        }