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

namespace angeldnd.dap {
    public struct ContextConsts {
        public const string TypeContext = "Context";

        public const string PathVars = "_vars";
        public const string PathProperties = "_properties";
        public const string PathChannels = "_channels";
        public const string PathHandlers = "_handlers";

        public const string ChannelTick = "_tick";

        public const string SuffixHandlerAsync = "~";
        public const string SuffixChannelResponse = ">";

        public static string GetAsyncHandlerPath(string handlerPath) {
            return handlerPath + SuffixHandlerAsync;
        }

        public static string GetResponseChannelPath(string handlerPath) {
            return handlerPath + SuffixChannelResponse;
        }
    }

    public class Context : Entity {
        public override string Type {
            get { return ContextConsts.TypeContext; }
        }

        public readonly Vars Vars;
        public readonly Properties Properties;
        public readonly Channels Channels;
        public readonly Handlers Handlers;

        public Context() {
            Vars = Add<Vars>(ContextConsts.PathVars);
            Properties = Add<Properties>(ContextConsts.PathProperties);
            Channels = Add<Channels>(ContextConsts.PathChannels);
            Handlers = Add<Handlers>(ContextConsts.PathHandlers);

            AddChannel(ContextConsts.ChannelTick);
        }

        public Data Dump() {
            Data data = Properties.Encode();
            if (data != null) {
                return data.GetData(EntityConsts.KeyAspects);
            }
            return null;
        }

        public bool Load(Data data) {
            return Properties.DecodeAspects(data) > 0;
        }

        public bool FireEvent(string channelPath, Data evt) {
            return Channels.FireEvent(channelPath, evt);
        }

        public Channel GetChannel(string channelPath) {
            return Channels.GetChannel(channelPath);
        }

        public Channel AddChannel(string channelPath) {
            return Channels.AddChannel(channelPath);
        }

        public Data HandleRequest(string handlerPath, Data req) {
            return Handlers.HandleRequest(handlerPath, req);
        }

        public Handler GetHandler(string handlerPath) {
            return Handlers.GetHandler(handlerPath);
        }

        public Handler AddHandler(string handlerPath) {
            return Handlers.AddHandler(handlerPath);
        }

        public bool SetVarValue<T>(string varPath, T v) {
            if (Vars.HasVar<T>(varPath)) {
                return Vars.SetValue<T>(varPath, v);
            } else {
                return Vars.AddVar<T>(varPath, v) != null;
            }
        }

        public T GetVarValue<T>(string varPath, T defaultValue) {
            return Vars.GetValue<T>(varPath, defaultValue);<commit_msg>add a couple helper for Context Var usage<commit_after>using System;
using System.Collections.Generic;

namespace angeldnd.dap {
    public struct ContextConsts {
        public const string TypeContext = "Context";

        public const string PathVars = "_vars";
        public const string PathProperties = "_properties";
        public const string PathChannels = "_channels";
        public const string PathHandlers = "_handlers";

        public const string ChannelTick = "_tick";

        public const string SuffixHandlerAsync = "~";
        public const string SuffixChannelResponse = ">";

        public static string GetAsyncHandlerPath(string handlerPath) {
            return handlerPath + SuffixHandlerAsync;
        }

        public static string GetResponseChannelPath(string handlerPath) {
            return handlerPath + SuffixChannelResponse;
        }
    }

    public class Context : Entity {
        public override string Type {
            get { return ContextConsts.TypeContext; }
        }

        public readonly Vars Vars;
        public readonly Properties Properties;
        public readonly Channels Channels;
        public readonly Handlers Handlers;

        public Context() {
            Vars = Add<Vars>(ContextConsts.PathVars);
            Properties = Add<Properties>(ContextConsts.PathProperties);
            Channels = Add<Channels>(ContextConsts.PathChannels);
            Handlers = Add<Handlers>(ContextConsts.PathHandlers);

            AddChannel(ContextConsts.ChannelTick);
        }

        public Data Dump() {
            Data data = Properties.Encode();
            if (data != null) {
                return data.GetData(EntityConsts.KeyAspects);
            }
            return null;
        }

        public bool Load(Data data) {
            return Properties.DecodeAspects(data) > 0;
        }

        public bool FireEvent(string channelPath, Data evt) {
            return Channels.FireEvent(channelPath, evt);
        }

        public Channel GetChannel(string channelPath) {
            return Channels.GetChannel(channelPath);
        }

        public Channel AddChannel(string channelPath) {
            return Channels.AddChannel(channelPath);
        }

        public Data HandleRequest(string handlerPath, Data req) {
            return Handlers.HandleRequest(handlerPath, req);
        }

        public Handler GetHandler(string handlerPath) {
            return Handlers.GetHandler(handlerPath);
        }

        public Handler AddHandler(string handlerPath) {
            return Handlers.AddHandler(handlerPath);
        }

        public bool HasVar(string varPath) {
            return Vars.Has(varPath);
        }

        public bool SetVarValue<T>(string varPath, T v) {
            if (Vars.HasVar<T>(varPath)) {
                return Vars.SetValue<T>(varPath, v);
            } else {
                return Vars.AddVar<T>(varPath, v) != null;
            }
        }

        public T GetVarValue<T>(string varPath, T defaultValue) {
            return Vars.GetValue<T>(varPath, defaultValue);
        }

        public T GetVarValue<T>(string varPath) {
            return GetVarValue<T>(varPath, default(T));
        }

        public bool HasVar(string varsPath, string varPath) {
            Vars vars = Get<Vars>(varsPath);
            if (vars == null) {
                return vars.Has(varPath);
            }
            return false;
{'fraction_non_alphanumeric': 0.07996251756988912, 'fraction_numerical': 0.00031235358425737937, 'mean_word_length': 2.102713178294574, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 30, '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': '3933822', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1565, 'n_words': 411}