public void AddEventTest()
        {
            var a = new Activity("A", "nameA");

            var exhaust = new ContradictionApproach(new HashSet<Activity>() {a,new Activity("B","nameB")});

            exhaust.AddEvent("A", "1000");

            Assert.IsTrue(exhaust.Graph.GetIncludedActivities().Contains(a));
        }
        public void ConditionNegativeTest()
        {
            var a = new Activity("A", "nameA");

            var b = new Activity("B", "nameB");

            var exhaust = new ContradictionApproach(new HashSet<Activity>() { a, b });

            exhaust.AddEvent("B", "1000");
            exhaust.Stop();

            HashSet<Activity> con;

            exhaust.Graph.Conditions.TryGetValue(a, out con);

            Assert.IsFalse(con.Contains(b));
        }
        public void CreateNestsTest()
        {
            var dcrGraph = new DcrGraph();

            var activityA = new Activity("A", "somename1") { Included = true, Pending = true };
            var activityB = new Activity("B", "somename2") { Included = true };
            var activityC = new Activity("C", "somename3") { Included = true };
            var activityD = new Activity("D", "somename4") { Included = true };
            var activityE = new Activity("E", "somename5") { Included = true, Pending = true };
            var activityF = new Activity("F", "somename6") { Included = true };
            var activityG = new Activity("G", "somename7") { Included = true };

            dcrGraph.AddActivities(activityA, activityB, activityC, activityD, activityE, activityF, activityG);

            dcrGraph.AddResponse(activityB.Id, activityC.Id); //inner nest condition

            //From A to all inner
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityB.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityC.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityD.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityE.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityB.Id);

            dcrGraph.AddIncludeExclude(false, activityD.Id, activityF.Id); //from in to out
            dcrGraph.AddMileStone(activityF.Id, activityG.Id);

            //From G to all inner and F
            dcrGraph.AddCondition(activityG.Id, activityB.Id);
            dcrGraph.AddCondition(activityG.Id, activityC.Id);
            dcrGraph.AddCondition(activityG.Id, activityD.Id);
            dcrGraph.AddCondition(activityG.Id, activityE.Id);
            dcrGraph.AddCondition(activityG.Id, activityF.Id);

            var exhaust = new ContradictionApproach(dcrGraph.Activities) { Graph = dcrGraph };

            exhaust.Graph = ContradictionApproach.CreateNests(exhaust.Graph);

            Assert.IsTrue(exhaust.Graph.Activities.Any(a => a.IsNestedGraph));
        }
        public void ExhaustiveWithBigDataLog()
        {
            var activities = new HashSet<Activity>();

            for (char ch = 'A'; ch <= 'Z'; ch++)
            {
                activities.Add(new Activity("" + ch, "somename " + ch));
            }

            var rnd = new Random();
            var inputLog = new Log();
            var traceId = 1000;
            var currentTrace = new LogTrace() {Id = traceId.ToString()};
            while (inputLog.Traces.Count < 100000)
            {
                currentTrace.Add(new LogEvent(activities.ElementAt(rnd.Next(activities.Count)).Id, ""));

                if (currentTrace.Events.Count == 8)
                {
                    inputLog.AddTrace(currentTrace);
                    traceId++;
                    currentTrace = (new LogTrace() {Id = traceId.ToString()});

                }

            }

            var exAl = new ContradictionApproach(activities);

            foreach (var trace in inputLog.Traces)
            {
                exAl.AddTrace(trace);
            }

            Assert.IsTrue(true);
        }
        public void TestLogParserHospital()
        {
            var watch = new Stopwatch();
            watch.Start();
            var log =
                XmlParser.ParseLog(
                                new LogStandard("http://www.xes-standard.org/", "trace",
                                    new LogStandardEntry(DataType.String, "conceptName"), "event",
                                    new LogStandardEntry(DataType.String, "ActivityCode"),
                                    new LogStandardEntry(DataType.String, "conceptName"),
                                    new LogStandardEntry(DataType.String, "org:group")), Properties.Resources.Hospital_log);
            Console.WriteLine("Finished parsing " + log.Traces.Count + " traces. Took: " + watch.Elapsed);
            Console.WriteLine("Alphabeth of size " + log.Alphabet.Count);

            int occurences = 0;

            //
            log.Traces = new List<LogTrace>(log.Traces.Where(t => t.Events.Distinct().Count() < 8));

            foreach (var character in log.Alphabet)
            {
                foreach (var other in log.Alphabet.Where(a => (a.IdOfActivity == character.IdOfActivity && a.Name != character.Name)))
                {
                    //Console.WriteLine("Name: " + character.Name + ", " + character.IdOfActivity +" is not : "+ other.IdOfActivity);
                    occurences++;
                }
            }
            Console.WriteLine("occurences of Id/Name-mismatch: " + occurences);

            var actors = new HashSet<String>();
            foreach (var name in log.Traces.SelectMany(trace => trace.Events.Select(a => a.ActorName)))
            {
                actors.Add(name);
            }

            foreach (var name in actors)
            {
                var tracesLength = log.Traces.Where(t => t.Events.Any(n => n.ActorName == name)).Select(a=> a.Events.Count);

                Console.WriteLine(name + " :  " + tracesLength.Count() + " traces, Longest trace = " + tracesLength.Max());
            }

            foreach (var trace in log.Traces.First().Events)
            {
                //Console.WriteLine("Example trace: " + log.Traces.First().Id);
                //Console.Write("ID: " + trace.IdOfActivity + ", Name: " + trace.Name + "   |   ");
            }

            Console.WriteLine("\nPlease choose department to process mine:");
            string department= Console.ReadLine();

            var newLog = log.FilterByActor(department);

            ContradictionApproach ex = new ContradictionApproach(new HashSet<Activity>(newLog.Alphabet.Select(logEvent => new Activity(logEvent.IdOfActivity,logEvent.Name))));

            Console.WriteLine(ex.Graph);

            var redundancy = new RedundancyRemover();

            foreach (var trace in newLog.Traces)
            {
                ex.AddTrace(trace);
            }
            var redundancyRemoved = redundancy.RemoveRedundancy(ex.Graph);
            Console.WriteLine(redundancyRemoved);
            Console.WriteLine(QualityDimensionRetriever.Retrieve(redundancyRemoved, newLog));

            Console.ReadLine();
        }
        public void TestOriginalLog()
        {
            var activities = new HashSet<Activity>();

            for (char ch = 'A'; ch <= 'F'; ch++)
            {
                activities.Add(new Activity("" + ch, "" + ch));
            }

            var exAl = new ContradictionApproach(activities);

            var trace1 = new LogTrace {Id = "1"};
            trace1.AddEventsWithChars('A', 'B', 'E');
            var trace2 = new LogTrace { Id = "2" };
            trace2.AddEventsWithChars('A', 'C', 'F', 'A', 'B', 'B', 'F');
            var trace3 = new LogTrace { Id = "3" };
            trace3.AddEventsWithChars('A', 'C', 'E');
            var trace4 = new LogTrace { Id = "4" };
            trace4.AddEventsWithChars('A', 'D', 'F');
            var trace5 = new LogTrace { Id = "5" };
            trace5.AddEventsWithChars('A', 'B', 'F', 'A', 'B', 'E');
            var trace6 = new LogTrace { Id = "6" };
            trace6.AddEventsWithChars('A', 'C', 'F');
            var trace7 = new LogTrace { Id = "7" };
            trace7.AddEventsWithChars('A', 'B', 'F', 'A', 'C', 'F', 'A', 'C', 'E');
            var trace8 = new LogTrace { Id = "8" };
            trace8.AddEventsWithChars('A', 'B', 'B', 'B', 'F');
            var trace9 = new LogTrace { Id = "9" };
            trace9.AddEventsWithChars('A', 'B', 'B', 'E');
            var trace10 = new LogTrace { Id = "10" };
            trace10.AddEventsWithChars('A', 'C', 'F', 'A', 'C', 'E');

            Log log = new Log() {Traces = {trace1, trace2, trace3, trace4, trace5, trace6, trace7, trace8, trace9, trace10}};

            exAl.AddLog(log);

            Console.WriteLine(exAl.Graph);
            Console.WriteLine(QualityDimensionRetriever.Retrieve(exAl.Graph,log));
            //Console.WriteLine(exAl.Graph.ExportToXml());
            Console.ReadLine();

            exAl.Graph = new RedundancyRemover().RemoveRedundancy(exAl.Graph);

            Console.WriteLine(exAl.Graph);
            Console.WriteLine(QualityDimensionRetriever.Retrieve(exAl.Graph, log));
            Console.ReadLine();

            exAl.Graph = ContradictionApproach.PostProcessing(exAl.Graph);

            Console.WriteLine(exAl.Graph);
            Console.WriteLine(QualityDimensionRetriever.Retrieve(exAl.Graph, log));

            Console.ReadLine();
            //using (StreamWriter sw = new StreamWriter("C:/Downloads/OrigGraph.xml"))
            //{
            //    sw.WriteLine(exAl.Graph.ExportToXml());
            //}
        }
        public void ExhaustiveTest()
        {
            var traceId = 1000;
            var activities = new HashSet<Activity>();

            for (char ch = 'A'; ch <= 'G'; ch++)
            {
                activities.Add(new Activity("" + ch, "somename " + ch));
            }
            var inputLog = new Log();
            var currentTrace = new LogTrace() {Id = traceId++.ToString()};
            inputLog.Traces.Add(currentTrace);
            var exAl = new ContradictionApproach(activities);

            int id = 0;

            while (true)
            {
                var input = Console.ReadLine();
                switch (input)
                {
                    case "STOP":
                        exAl.Stop();
                        currentTrace = currentTrace.Copy();
                        currentTrace = new LogTrace() { Id = traceId++.ToString() };
                        inputLog.Traces.Add(currentTrace);
                        break;
                    case "AUTOLOG":
                        Console.WriteLine("Please input a termination index between 0 - 100 : \n");
                        var logGen = new LogGenerator9001(Convert.ToInt32(Console.ReadLine()), exAl.Graph);
                        Console.WriteLine("Please input number of desired traces to generate : \n");
                        List<LogTrace> log = logGen.GenerateLog(Convert.ToInt32(Console.ReadLine()));
                        foreach (var trace in log)
                        {
                            Console.WriteLine(trace);
                        }
                        break;
                    case "REDUNDANCY":
                        exAl.Graph = new RedundancyRemover().RemoveRedundancy(exAl.Graph);
                        break;
                    case "POST":
                        exAl.Graph = ContradictionApproach.PostProcessing(exAl.Graph);
                        break;
                    case "NESTED":
                        exAl.Graph = ContradictionApproach.CreateNests(exAl.Graph);
                        break;
                    case "CHANGE TRACE":
                        inputLog.Traces.Add(currentTrace);
                        var newId = Console.ReadLine();
                        currentTrace = inputLog.Traces.Find(x => x.Id == newId) ?? new LogTrace() { Id = newId};

                        break;

                    default:
                        if (exAl.Graph.GetActivities().Any(a => a.Id == input))
                        {
                            exAl.AddEvent(input, currentTrace.Id);
                            currentTrace.Add(new LogEvent(input, "somename" + input) {EventId = "" + id++});
                        }
                        break;
                }

                Console.WriteLine("Current trace id: " + currentTrace.Id);
                Console.WriteLine(exAl.Graph);

                //the quality probably suffers because the traces contains unfinished traces.
                Console.WriteLine(QualityDimensionRetriever.Retrieve(exAl.Graph, inputLog));
            }
        }
        public void TestFlowerModel()
        {
            var graph = new DcrGraph();
            var trace = new LogTrace();

            for (char ch = 'A'; ch <= 'D'; ch++)
            {
                graph.Activities.Add(new Activity("" + ch, "somename " + ch) {Included = true});
                trace.AddEventsWithChars(ch);
            }

            var exhaustive =
                new ContradictionApproach(new HashSet<Activity>
                {
                    new Activity("A", "somenameA"),
                    new Activity("B", "somenameB"),
                    new Activity("C", "somenameC"),
                    new Activity("D", "somenameD")
                });
            exhaustive.Graph = graph;
            exhaustive.AddTrace(trace);

            Console.WriteLine(exhaustive.Graph);
            exhaustive.Graph = new RedundancyRemover().RemoveRedundancy(exhaustive.Graph);
            Console.WriteLine(exhaustive.Graph);
            Console.WriteLine(QualityDimensionRetriever.Retrieve(exhaustive.Graph, new Log() {Traces = {trace}}));
            Console.ReadLine();
        }
        public void TestQualityDimensionsRetriever()
        {
            var graph = new DcrGraph();
            graph.AddActivity("A", "somename1");
            graph.AddActivity("B", "somename2");
            graph.AddActivity("C", "somename3");
            //graph.AddActivity("D", "somename3");

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

            graph.AddIncludeExclude(true, "A", "B");
            graph.AddIncludeExclude(true, "B", "C");
            graph.AddIncludeExclude(false, "C", "B");
            graph.AddResponse("B", "C");
            // Self-excludes
            graph.AddIncludeExclude(false, "A", "A");
            graph.AddIncludeExclude(false, "B", "B");
            graph.AddIncludeExclude(false, "C", "C");
            graph.AddCondition("A", "B");
            graph.AddMileStone("A", "B");

            var someLog  =
                new Log() { Traces =
                    new List<LogTrace>
                    {
                        new LogTrace('A', 'B', 'C'),
                        new LogTrace('A', 'C')
                    }
                };
            var res = QualityDimensionRetriever.Retrieve(graph, someLog);
            Console.WriteLine(graph);
            Console.WriteLine(res);

            var graph2 = new DcrGraph();
            graph2.AddActivity("A", "somename1");
            graph2.AddActivity("B", "somename2");
            graph2.AddActivity("C", "somename3");
            graph2.SetIncluded(true, "A");
            graph2.SetIncluded(true, "B");
            graph2.SetIncluded(true, "C");

            res = QualityDimensionRetriever.Retrieve(graph2, someLog);
            Console.WriteLine(graph2);
            Console.WriteLine(res);

            var graph3 = new DcrGraph();
            graph3.AddActivity("A", "somename1");
            graph3.AddActivity("B", "somename2");
            graph3.AddActivity("C", "somename3");
            graph3.AddIncludeExclude(false, "A", "A");
            graph3.AddIncludeExclude(false, "B", "B");
            graph3.AddIncludeExclude(false, "C", "C");
            graph3.AddIncludeExclude(false, "A", "B");
            graph3.AddIncludeExclude(false, "B", "A");
            graph3.AddIncludeExclude(false, "C", "A");
            graph3.AddIncludeExclude(false, "C", "B");
            graph3.AddIncludeExclude(false, "A", "C");
            graph3.AddIncludeExclude(false, "B", "C");
            graph3.AddResponse("A", "B");
            graph3.AddResponse("A", "C");
            graph3.AddResponse("B", "A");
            graph3.AddResponse("B", "C");
            graph3.AddResponse("C", "A");
            graph3.AddResponse("C", "B");
            graph3.AddCondition("A", "B");
            graph3.AddCondition("A", "C");
            graph3.AddCondition("B", "A");
            graph3.AddCondition("B", "C");
            graph3.AddCondition("C", "A");
            graph3.AddCondition("C", "B");
            graph3.AddMileStone("A", "B");
            graph3.AddMileStone("A", "C");
            graph3.AddMileStone("B", "A");
            graph3.AddMileStone("B", "C");
            graph3.AddMileStone("C", "A");
            graph3.AddMileStone("C", "B");

            res = QualityDimensionRetriever.Retrieve(graph3, someLog);
            Console.WriteLine(graph3);
            Console.WriteLine(res);

            // "Original" test log
            var activities = new HashSet<Activity>();

            for (char ch = 'A'; ch <= 'F'; ch++)
            {
                activities.Add(new Activity("" + ch, "somename " + ch));
            }

            var exAl = new ContradictionApproach(activities);

            var originalLog = new List<LogTrace>();
            originalLog.Add(new LogTrace('A', 'B', 'E'));
            originalLog.Add(new LogTrace('A', 'C', 'F', 'A', 'B', 'B', 'F'));
            originalLog.Add(new LogTrace('A', 'C', 'E'));
            originalLog.Add(new LogTrace('A', 'D', 'F'));
            originalLog.Add(new LogTrace('A', 'B', 'F', 'A', 'B', 'E'));
            originalLog.Add(new LogTrace('A', 'C', 'F'));
            originalLog.Add(new LogTrace('A', 'B', 'F', 'A', 'C', 'F', 'A', 'C', 'E'));
            originalLog.Add(new LogTrace('A', 'B', 'B', 'B', 'F'));
            originalLog.Add(new LogTrace('A', 'B', 'B', 'E'));
            originalLog.Add(new LogTrace('A', 'C', 'F', 'A', 'C', 'E'));
            exAl.AddTrace(originalLog[0]);
            exAl.AddTrace(originalLog[1]);
            exAl.AddTrace(originalLog[2]);
            exAl.AddTrace(originalLog[3]);
            exAl.AddTrace(originalLog[4]);
            exAl.AddTrace(originalLog[5]);
            exAl.AddTrace(originalLog[6]);
            exAl.AddTrace(originalLog[7]);
            exAl.AddTrace(originalLog[8]);
            exAl.AddTrace(originalLog[9]);

            var log = new Log() {Traces = originalLog};

            res = QualityDimensionRetriever.Retrieve(exAl.Graph, log);
            Console.WriteLine(exAl.Graph);
            Console.WriteLine(res);

            Console.WriteLine("Removing redundancy::::::::::::::::::::::::::::::::::");
            exAl.Graph = new RedundancyRemover().RemoveRedundancy(exAl.Graph);

            res = QualityDimensionRetriever.Retrieve(exAl.Graph, log);
            Console.WriteLine(exAl.Graph);
            Console.WriteLine(res);

            Console.ReadLine();
        }
        public List<long> DataTimingTest(int alphabeth, int traces, int traceLength, int timesToRun)
        {
            var activities = new HashSet<Activity>();

            for (int i = 0; i < alphabeth; i++)
            {
                activities.Add(new Activity("" + i, "" + i));
            }

            var times = new List<long>();

            for (int j = 0; j < timesToRun; j++)
            {

                var rnd = new Random();

                var inputLog = new Log();
                var traceId = 1000;
                var currentTrace = new LogTrace() {Id = traceId.ToString()};
                while (inputLog.Traces.Count < traces)
                {
                    currentTrace.Add(new LogEvent(activities.ElementAt(rnd.Next(activities.Count)).Id, ""));

                    if (currentTrace.Events.Count == traceLength)
                    {
                        inputLog.AddTrace(currentTrace);
                        traceId++;
                        currentTrace = (new LogTrace() {Id = traceId.ToString()});

                    }

                }

                //
                var watch = new Stopwatch();
                watch.Start();

                var exAl = new ContradictionApproach(activities);

                foreach (var trace in inputLog.Traces)
                {
                    exAl.AddTrace(trace);
                }

                watch.Stop();

                times.Add(watch.ElapsedMilliseconds);
                Console.Write(".");
            }

            return times;
        }
        public void RetrieveFitnessOnGraphMinedFromLog()
        {
            var log =
                XmlParser.ParseLog(
                                new LogStandard("http://www.xes-standard.org/", "trace",
                                    new LogStandardEntry(DataType.String, "conceptName"), "event",
                                    new LogStandardEntry(DataType.String, "ActivityCode"),
                                    new LogStandardEntry(DataType.String, "conceptName"),
                                    new LogStandardEntry(DataType.String, "org:group")), Resources.Hospital_log);

            log = log.FilterByActor("Special lab Genetic Metabolic Diseases"); //only in 1 small trace

            ContradictionApproach ex = new ContradictionApproach(new HashSet<Activity>(log.Alphabet.Select(logEvent => new Activity(logEvent.IdOfActivity, logEvent.Name))));

            foreach (var trace in log.Traces)
            {
                ex.AddTrace(trace);
            }

            //expecting fitness = 100%
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(ex.Graph, log);
            Assert.AreEqual(100d, qd.Fitness);
        }
 private void bw_DoWork(object sender, DoWorkEventArgs e)
 {
     Dispatcher.Invoke(() =>
     {
         if (IsTraceAdditionAllowed)
         {
             // Make a copy of Exhaustive Approach where all traces are finished, to avoid potential Response cycles
             var exaustCopy = new ContradictionApproach(_contradictionApproach.Graph.GetActivities());
             exaustCopy.AddLog(new Log {Traces = _entireLog.ToList()});
             var redundancyRemovedGraph = _redundancyRemover.RemoveRedundancy(exaustCopy.Graph);
             ContradictionApproach.PostProcessing(redundancyRemovedGraph);
         }
         else // Signifies that the program was initiated with a loaded graph
         {
             var redundancyRemovedGraph = _redundancyRemover.RemoveRedundancy(GraphToDisplay);
             ContradictionApproach.PostProcessing(redundancyRemovedGraph);
         }
     });
 }
 private void SetUpWithAlphabet(int sizeOfAlphabet)
 {
     IsWaiting = true;
     var a = 'A';
     for (int i = 0; i < sizeOfAlphabet; i++)
     {
         var currId = "" + Convert.ToChar(a + i);
         Activities.Add(new Activity(currId, string.Format("Activity {0}", currId)));
     }
     foreach (var activity in Activities)
     {
         ActivityButtons.Add(new ActivityNameWrapper(activity.Id));
     }
     _contradictionApproach = new ContradictionApproach(new HashSet<Activity>(Activities));
     ContradictionApproach.PostProcessingResultEvent += UpdateGraphWithPostProcessingResult;
     UpdateGraph();
     IsWaiting = false;
 }
        private void SetUpWithLog(Log log)
        {
            IsWaiting = true;
            // TODO: _contradictionApproach.AddLog(log); - then add to GUI list etc? - test effectiveness - probably same deal
            Activities = new ObservableCollection<Activity>(log.Alphabet.Select(x => new Activity(x.IdOfActivity, x.Name){ Roles = x.ActorName }));
            foreach (var activity in Activities)
            {
                ActivityButtons.Add(new ActivityNameWrapper(activity.Id));
            }
            _contradictionApproach = new ContradictionApproach(new HashSet<Activity>(Activities));
            ContradictionApproach.PostProcessingResultEvent += UpdateGraphWithPostProcessingResult;

            foreach (var logTrace in log.Traces)
            {
                AddFinishedTraceFromLog(logTrace);
            }
            RefreshLogTraces();
            UpdateGraph();
            IsWaiting = false;
        }
        public void Init()
        {
            Activities = new ObservableCollection<Activity>();

            _contradictionApproach = new ContradictionApproach(new HashSet<Activity>(Activities));
            ContradictionApproach.PostProcessingResultEvent += UpdateGraphWithPostProcessingResult;

            _redundancyRemover = new RedundancyRemover();
            _redundancyRemover.ReportProgress += ProgressMadeInRedundancyRemover;
            
            ActivityButtons = new ObservableCollection<ActivityNameWrapper>();

            EntireLog = new TrulyObservableCollection<LogTrace>();

            PerformPostProcessing = false;

            IsTraceAdditionAllowed = true;
            
            var startOptionsViewModel = new StartOptionsWindowViewModel();
            startOptionsViewModel.AlphabetSizeSelected += SetUpWithAlphabet;
            startOptionsViewModel.LogLoaded += SetUpWithLog;
            startOptionsViewModel.DcrGraphLoaded += SetUpWithGraph;
            
            OpenStartOptionsEvent?.Invoke(startOptionsViewModel);
        }