public void GetGeneralizationTest()
        {
            var dcrGraph = new DcrGraph();

            var activityA = new Activity("A", "somename1") { Included = true, Pending = true };
            var activityB = new Activity("B", "somename2") { Included = false };
            var activityC = new Activity("C", "somename3") { Included = false };

            dcrGraph.AddActivities(activityA, activityB, activityC);

            dcrGraph.AddIncludeExclude(true, activityA.Id, activityB.Id);
            dcrGraph.AddIncludeExclude(true, activityB.Id, activityC.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityA.Id);
            dcrGraph.AddIncludeExclude(false, activityB.Id, activityB.Id);

            var log = new Log();
            log.AddTrace(new LogTrace('A', 'B'));
            log.AddTrace(new LogTrace('A', 'B', 'C'));
            log.AddTrace(new LogTrace('A', 'B', 'C')); //duplicate trace should not matter
            log.AddTrace(new LogTrace('A', 'B', 'C', 'A')); //illegal execution should count down
            log.AddTrace(new LogTrace('A', 'B', 'C', 'A', 'A')); //this one should not matter, as we've already seen A been illegally executed from this state.

            var log2 = new Log();

            log2.AddTrace(new LogTrace('A', 'B'));

            var qd1 = QualityDimensionRetriever.Retrieve(dcrGraph, log);

            var qd2 = QualityDimensionRetriever.Retrieve(dcrGraph, log2);

            Assert.IsTrue(qd1.Generality > qd2.Generality);
        }
        public void FitnessOnNestedGraph()
        {
            var dcrGraph = new DcrGraph();

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

            dcrGraph.AddActivities(activityA,activityB,activityC,activityD,activityE,activityF);
            dcrGraph.AddIncludeExclude(true, activityC.Id, activityD.Id);
            dcrGraph.AddCondition(activityE.Id, activityF.Id); //outgoing relation
            //ingoing relations
            dcrGraph.AddCondition(activityA.Id, activityC.Id);
            dcrGraph.AddCondition(activityA.Id, activityD.Id);
            dcrGraph.AddCondition(activityA.Id, activityE.Id);

            dcrGraph.MakeNestedGraph(new HashSet<Activity>() { activityC, activityD, activityE });

            var log = new Log();
            log.AddTrace(new LogTrace('A', 'C','D'));
            log.AddTrace(new LogTrace('A', 'B', 'C'));
            log.AddTrace(new LogTrace('A', 'E', 'F'));
            log.AddTrace(new LogTrace('C', 'E','D')); //illegal trace should count down

            //expecting fitness = 75%
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(dcrGraph, log);

            Assert.AreEqual(75d, qd.Fitness);
        }
        public static Log ParseLog(LogStandard logStandard, string xml)
        {
            var log = new Log();

            XNamespace ns = logStandard.Namespace;

            XDocument doc = XDocument.Parse(xml);

            int eventId = 0;
            foreach (XElement traceElement in doc.Root.Elements(ns + logStandard.TraceIdentifier).Where(element => element.HasElements))
            {
                var trace = new LogTrace() {Id = traceElement.GetValue(ns, logStandard.TraceIdIdentifier) };

                foreach (XElement eventElement in traceElement.Elements(ns + logStandard.EventIdentifier).Where(element => element.HasElements))
                {
                    trace.Add(new LogEvent(eventElement.GetValue(ns, logStandard.EventIdIdentifier),
                                            eventElement.GetValue(ns, logStandard.EventNameIdentifier))
                    {
                        EventId = eventId++.ToString(),
                        ActorName = string.IsNullOrEmpty(logStandard.ActorNameIdentifier.Name) ? "" : eventElement.GetValue(ns, logStandard.ActorNameIdentifier)
                    });
                }

                log.AddTrace(trace);
            }
            return log;
        }
        public Log FilterByActor(string actorName)
        {
            var newLog = new Log();

            foreach (var trace in Traces.Where(t => t.Events.Any(e => e.ActorName == actorName)))
            {
                newLog.AddTrace(trace);
            }

            return newLog;
        }
        public Log FilterByNoOfActivities(int maxActivities)
        {
            var newLog = new Log();

            foreach (var trace in Traces.Where(t => t.Events.Distinct().Count() <= maxActivities))
            {
                newLog.AddTrace(trace);
            }

            return newLog;
        }
        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 ParseMortgageApplication()
        {
            var graph = new DcrGraph();

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

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

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

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

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

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

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

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

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

            graph.AddCondition("Collect Documents", "Irregular neighbourhood");

            graph.AddCondition("Collect Documents", "Assess loan application");

            graph.AddIncludeExclude(true, "Irregular neighbourhood", "Make appraisal appointment");

            graph.AddIncludeExclude(false, "Irregular neighbourhood", "Statistical appraisal");

            graph.AddCondition("Make appraisal appointment", "On-site appraisal");

            graph.AddIncludeExclude(true, "Appraisal audit", "On-site appraisal");

            graph.AddIncludeExclude(false, "Statistical appraisal", "On-site appraisal");
            graph.AddCondition("Statistical appraisal", "Assess loan application");

            graph.AddIncludeExclude(false,  "On-site appraisal", "Statistical appraisal");
            graph.AddCondition("On-site appraisal", "Assess loan application");
            graph.AddCondition("Budget screening approve", "Assess loan application");

            graph.AddResponse("Budget screening approve", "Assess loan application");

            graph.AddCondition("Submit budget", "Budget screening approve");
            graph.AddResponse("Submit budget", "Budget screening approve");

            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/mortgageLog.xml"))
            {
                sw.WriteLine(Log.ExportToXml(log));
            }
        }
        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 PrecisionOnNestedGraph()
        {
            var dcrGraph = new DcrGraph();

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

            dcrGraph.AddActivities(activityA, activityB, activityC, activityD, activityE, activityF);
            //ingoing relations
            dcrGraph.AddCondition(activityA.Id, activityC.Id);
            dcrGraph.AddCondition(activityA.Id, activityD.Id);
            dcrGraph.AddCondition(activityA.Id, activityE.Id);
            dcrGraph.AddCondition(activityC.Id, activityE.Id);
            dcrGraph.AddIncludeExclude(true, activityC.Id, activityD.Id);
            dcrGraph.AddIncludeExclude(true, activityD.Id, activityE.Id);
            dcrGraph.AddIncludeExclude(true, activityE.Id, activityF.Id); //outgoing relation

            dcrGraph.AddIncludeExclude(false, activityA.Id, activityA.Id);
            dcrGraph.AddIncludeExclude(false, activityC.Id, activityC.Id);
            dcrGraph.AddIncludeExclude(false, activityD.Id, activityD.Id);
            dcrGraph.AddIncludeExclude(false, activityE.Id, activityE.Id);
            //F can be run more than once

            dcrGraph.MakeNestedGraph(new HashSet<Activity>() { activityC, activityD, activityE });

            var log = new Log();
            log.AddTrace(new LogTrace('A', 'C', 'D', 'E', 'F')); // we don't execute 'F' twice

            log.AddTrace(new LogTrace('A', 'C', 'D', 'E', 'E')); //we illegally execute E

            //legal activities executed pr. state = 5
            // divided by illegal executed activities (1) + legal activities that could be executed (5 + 1) (F could be executed again.)

            //expecting precision 5/7
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(dcrGraph, log);
            Assert.AreEqual((5d/7d)*100, qd.Precision);
        }
        public void SimplicityOnNestedGraph()
        {
            var dcrGraph = new DcrGraph();

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

            dcrGraph.AddActivities(activityA, activityC, activityD, activityE, activityF);
            //ingoing relations
            dcrGraph.AddCondition(activityA.Id, activityC.Id);
            dcrGraph.AddCondition(activityA.Id, activityD.Id);
            dcrGraph.AddCondition(activityA.Id, activityE.Id);
            dcrGraph.AddCondition(activityC.Id, activityE.Id); //inside nested relation
            dcrGraph.AddIncludeExclude(true, activityC.Id, activityD.Id);
            dcrGraph.AddIncludeExclude(true, activityD.Id, activityE.Id);
            dcrGraph.AddIncludeExclude(true, activityE.Id, activityF.Id); //outgoing relation

            dcrGraph.AddIncludeExclude(false, activityA.Id, activityA.Id);
            dcrGraph.AddIncludeExclude(false, activityC.Id, activityC.Id);
            dcrGraph.AddIncludeExclude(false, activityD.Id, activityD.Id);
            dcrGraph.AddIncludeExclude(false, activityE.Id, activityE.Id);
            //F can be run more than once

            dcrGraph.MakeNestedGraph(new HashSet<Activity>() { activityC, activityD, activityE });

            var log = new Log();
            log.AddTrace(new LogTrace('A', 'C', 'D', 'E', 'F'));
            log.AddTrace(new LogTrace('A', 'C', 'D', 'E', 'E'));

            //S1 = amount of relations (9) / amount of possible relations n=6 (4n^2 - 3n = 126) = 9/126
            //S2 = amount of coupled relations (9) / possible coupled relations (n^2 = 36) = 0,25
            //S3 = amount of pending activities 1 / all activities (5) = 0,2    (pending activities should not count the nested graph)

            const double totalRelationsPart = (1.0 - 9.0 / 126.0) / 2; // 50 % weight
            const double relationCouplesPart = (1.0 - 9.0 / 21.0) / 2; // 50 % weight
            const double pendingPart = (1.0 / 5.0) * 0.1; // 10 % possible negative weight
            const double nestedPart = (1.0 / 5) * 0.2; // 20 % possible negative weight

            //expecting simplicity: 1 - ((9/126)*0,45 + 0,25*0,45 + 0,2 * 0,10)
            const double expected = (totalRelationsPart + relationCouplesPart - pendingPart - nestedPart) * 100.0;
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(dcrGraph, log);
            Assert.AreEqual(expected, qd.Simplicity);
        }
        public void RetrieveSimplicityTest()
        {
            var dcrGraph = new DcrGraph();

            var activityA = new Activity("A", "somename1") { Included = true, Pending = true };
            var activityB = new Activity("B", "somename2") { Included = false };

            dcrGraph.AddActivities(activityA, activityB);

            dcrGraph.AddIncludeExclude(true, activityA.Id, activityB.Id);

            var log = new Log(); //log does not matter for simplicity
            log.AddTrace(new LogTrace('A', 'B'));
            log.AddTrace(new LogTrace('A', 'B', 'C'));
            log.AddTrace(new LogTrace('A', 'B', 'C'));
            log.AddTrace(new LogTrace('A', 'B', 'C', 'A'));

            //S1 = amount of relations (1) / amount of possible relations (4n^2 - 3n = 10) = 0,10
            //S2 = amount of coupled relations (1) / possible coupled relations (n^2 = 4) = 0,25
            //S3 = amount of pending activities 1 / all activities (2) = 0,5

            //expecting simplicity: (1.0 - 1 / 10) / 2 + (1.0 - 1 / 3) / 2 - (1 / 2) * 0.1
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(dcrGraph, log);
            Assert.AreEqual(((1.0 - 1.0 / 10.0) / 2 + (1.0 - 1.0 / 3.0) / 2 - (1.0 / 2.0) * 0.1) * 100.0, qd.Simplicity);
        }
        public void RetrievePrecisionTest()
        {
            var dcrGraph = new DcrGraph();

            var activityA = new Activity("A", "somename1") { Included = true, Pending = true };
            var activityB = new Activity("B", "somename2") { Included = false };
            var activityC = new Activity("C", "somename3") { Included = false };

            dcrGraph.AddActivities(activityA, activityB, activityC);

            dcrGraph.AddIncludeExclude(true, activityA.Id, activityB.Id);
            dcrGraph.AddIncludeExclude(true, activityB.Id, activityC.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityA.Id);
            dcrGraph.AddIncludeExclude(false, activityB.Id, activityB.Id);

            var log = new Log();
            log.AddTrace(new LogTrace('A', 'B'));
            log.AddTrace(new LogTrace('A', 'B', 'C'));
            log.AddTrace(new LogTrace('A', 'B', 'C')); //duplicate trace should not matter
            log.AddTrace(new LogTrace('A', 'B', 'C', 'A')); //illegal execution should count down
            log.AddTrace(new LogTrace('A', 'B', 'C', 'A', 'A')); //this one should not matter, as we've already seen A been illegally executed from this state.

            //legal activities executed pr. state = 3
            // divided by illegal executed activities (1) + legal activities that could be executed (3 + 1) (c could be executed again.)

            //expecting precision 3/5 = 60%
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(dcrGraph, log);
            Assert.AreEqual(60d, qd.Precision);
        }
        public void RetrieveFitnessTest()
        {
            var dcrGraph = new DcrGraph();

            var activityA = new Activity("A", "somename1") { Included = true, Pending = true};
            var activityB = new Activity("B", "somename2") { Included = false };
            var activityC = new Activity("C", "somename3") { Included = false };

            dcrGraph.AddActivities(activityA,activityB,activityC);

            dcrGraph.AddIncludeExclude(true, activityA.Id, activityB.Id);
            dcrGraph.AddIncludeExclude(true, activityB.Id, activityC.Id);
            dcrGraph.AddIncludeExclude(false, activityA.Id, activityA.Id);
            dcrGraph.AddIncludeExclude(false, activityB.Id, activityB.Id);

            var log = new Log();
            log.AddTrace(new LogTrace('A', 'B'));
            log.AddTrace(new LogTrace('A','B','C'));
            log.AddTrace(new LogTrace('A', 'B', 'C')); //duplicate trace should still count
            log.AddTrace(new LogTrace('A', 'B', 'C', 'A')); //illegal trace should count down

            //expecting fitness = 75%
            var qd = UlrikHovsgaardAlgorithm.QualityMeasures.QualityDimensionRetriever.Retrieve(dcrGraph, log);
            Assert.AreEqual(75d,qd.Fitness);
        }