<commit_before>﻿using System;
using System.IO;
using System.Collections.Generic;

namespace Symbooglix
{
    public class ExecutionTreeLogger : IExecutorEventHandler
    {
        string Directory;
        bool AnnotateWithContextChanges = false;
        List<Tuple<ExecutionTreeNode,ExecutionTreeNode>> ContextChanges=null;


        public ExecutionTreeLogger(string directory, bool annotateWithContextChanges=false)
        {
            this.Directory = directory;
            this.AnnotateWithContextChanges = annotateWithContextChanges;

            if (this.AnnotateWithContextChanges)
                ContextChanges = new List<Tuple<ExecutionTreeNode, ExecutionTreeNode>>();
        }

        public void Connect(Executor e)
        {
            e.ExecutorTerminated += HandleExecutorTerminated;

            if (AnnotateWithContextChanges)
                e.ContextChanged += HandleContextChanged;
        }

        void HandleContextChanged (object sender, Executor.ContextChangeEventArgs e)
        {
            ContextChanges.Add(Tuple.Create(e.Previous.TreeNode, e.Next.TreeNode));
        }

        void HandleExecutorTerminated(object sender, Executor.ExecutorTerminatedArgs e)
        {
            var path = Path.Combine(this.Directory, "execution_tree.dot");
            var executor = (Executor) sender;
            using (var SW = new StreamWriter(path))
            {
                ExecutionTreePrinter etp = null;

                if (AnnotateWithContextChanges)
                    etp = new ExecutionTreePrinterWithContextChanges(executor.TreeRoot, 2, this.ContextChanges);
                else
                    etp = new ExecutionTreePrinter(executor.TreeRoot);
                etp.Print(SW);
            }
        }

        public void Disconnect(Executor e)
        {
            e.ExecutorTerminated -= HandleExecutorTerminated;

            if (AnnotateWithContextChanges)
                e.ContextChanged -= HandleContextChanged;
        }
    }

    class ExecutionTreePrinterWithContextChanges : ExecutionTreePrinter
    {
        IList<Tuple<ExecutionTreeNode,ExecutionTreeNode>> ContextChanges;
        public ExecutionTreePrinterWithContextChanges(ExecutionTreeNode root, int indent, IList<Tuple<ExecutionTreeNode,ExecutionTreeNode>> contextChanges ) : base(root, indent)
        {
            this.ContextChanges = contextChanges;
        }

        protected override void PrintAdditionalEdges(TextWriter TW, ExecutionTreeNode root)
        {
            TW.WriteLine("/* Context changes */");
            foreach (var pair in this.ContextChanges)
            {
                TW.WriteLine("{0} -> {1} [color=red];", GetNodeID(pair.Item1.State.TreeNode), GetNodeID(pair.Item2.State.TreeNode));
            }
        }
    }
}
<commit_msg>Number context changes in ExecutionTree ExecutionTreePrinterWithContextChanges<commit_after>﻿using System;
using System.IO;
using System.Collections.Generic;

namespace Symbooglix
{
    public class ExecutionTreeLogger : IExecutorEventHandler
    {
        string Directory;
        bool AnnotateWithContextChanges = false;
        List<Tuple<ExecutionTreeNode,ExecutionTreeNode>> ContextChanges=null;


        public ExecutionTreeLogger(string directory, bool annotateWithContextChanges=false)
        {
            this.Directory = directory;
            this.AnnotateWithContextChanges = annotateWithContextChanges;

            if (this.AnnotateWithContextChanges)
                ContextChanges = new List<Tuple<ExecutionTreeNode, ExecutionTreeNode>>();
        }

        public void Connect(Executor e)
        {
            e.ExecutorTerminated += HandleExecutorTerminated;

            if (AnnotateWithContextChanges)
                e.ContextChanged += HandleContextChanged;
        }

        void HandleContextChanged (object sender, Executor.ContextChangeEventArgs e)
        {
            ContextChanges.Add(Tuple.Create(e.Previous.TreeNode, e.Next.TreeNode));
        }

        void HandleExecutorTerminated(object sender, Executor.ExecutorTerminatedArgs e)
        {
            var path = Path.Combine(this.Directory, "execution_tree.dot");
            var executor = (Executor) sender;
            using (var SW = new StreamWriter(path))
            {
                ExecutionTreePrinter etp = null;

                if (AnnotateWithContextChanges)
                    etp = new ExecutionTreePrinterWithContextChanges(executor.TreeRoot, 2, this.ContextChanges);
                else
                    etp = new ExecutionTreePrinter(executor.TreeRoot);
                etp.Print(SW);
            }
        }

        public void Disconnect(Executor e)
        {
            e.ExecutorTerminated -= HandleExecutorTerminated;

            if (AnnotateWithContextChanges)
                e.ContextChanged -= HandleContextChanged;
        }
    }

    class ExecutionTreePrinterWithContextChanges : ExecutionTreePrinter
    {
        IList<Tuple<ExecutionTreeNode,ExecutionTreeNode>> ContextChanges;
        public ExecutionTreePrinterWithContextChanges(ExecutionTreeNode root, int indent, IList<Tuple<ExecutionTreeNode,ExecutionTreeNode>> contextChanges ) : base(root, indent)
        {
            this.ContextChanges = contextChanges;
        }

        protected override void PrintAdditionalEdges(TextWriter TW, ExecutionTreeNode root)
        {
            int counter = 0;
            TW.WriteLine("/* Context changes */");
            foreach (var pair in this.ContextChanges)
            {
                TW.WriteLine("{0} -> {1} [color=red, label=\"{2}\"];", GetNodeID(pair.Item1.State.TreeNode), GetNodeID(pair.Item2.State.TreeNode), counter);
                ++counter;
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.07865757734661773, 'fraction_numerical': 0.002097535395909806, 'mean_word_length': 2.3325567850902735, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6742972', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1406, 'n_words': 273}