Beispiel #1
0
        public void TestInCSharp()
        {
            var eventA = new NameChanged {
                EntityName = "1234", Name = "Bob", OccurredOn = DateTime.UtcNow
            };
            var eventB = new NameChanged {
                EntityName = "1234", Name = "Jimmy", OccurredOn = DateTime.UtcNow.AddDays(1)
            };

            var markovA = MarkovEvent.Build("NameChanged", eventA, eventA.OccurredOn);
            var markovB = MarkovEvent.Build("NameChanged", eventB, eventB.OccurredOn);

            var trunk = new Trunk();

            var braidDiscriminators = new BraidLinkDiscriminators();

            braidDiscriminators.AddDiscriminator(x => BraidLinkDiscriminationResult.ISaidYes);

            var chainLongitudinalEvaluators = LongitudinalEvaluators.Build();

            chainLongitudinalEvaluators.AddEvaluator(LongitudinalEvaluator.Build("Identity", () => JsonConvert.SerializeObject(0), (a, s) =>
            {
                var state = JsonConvert.DeserializeObject <int>(s);

                Console.WriteLine(state);

                state++;

                s = JsonConvert.SerializeObject(state);

                var result = new EvaluationResult();

                result.Value.X = JsonConvert.SerializeObject(a);
                result.Value.Y = JsonConvert.SerializeObject(a.OccurredOn);

                result.State = s;

                return(result);
            }, (a, b, e, s) => JsonConvert.SerializeObject(a.OccurredOn - b.OccurredOn)));

            var chainBuilder = ChainBuilder.Build(x => ChainIdentity.Build(x.Payload["EntityName"].ToString()), chainLongitudinalEvaluators);

            var braidBuilder = BraidBuilder.Build("Initial", braidDiscriminators, chainBuilder, LongitudinalEvaluators.Build());

            trunk.AddBraid(braidBuilder);

            trunk.Accept(markovA);
            trunk.Accept(markovB);
        }
Beispiel #2
0
        public void TestInJavaScript()
        {
            var eventAOccurredOn = DateTime.UtcNow;
            var eventBOccurredOn = DateTime.UtcNow.AddDays(1);

            var eventA = "{ \"EntityName\" : \"1234\", \"Name\" : \"Bob\", \"OccurredOn\" : \"" + eventAOccurredOn + "\"}";
            var eventB = "{ \"EntityName\" : \"1234\", \"Name\" : \"Jimmy\", \"OccurredOn\" : \"" + eventBOccurredOn + "\"}";

            var markovA = MarkovEvent.BuildFromJson("NameChanged", eventA, eventAOccurredOn);
            var markovB = MarkovEvent.BuildFromJson("NameChanged", eventB, eventBOccurredOn);

            var trunk = new Trunk();

            var braidDiscriminators = new BraidLinkDiscriminators();

            braidDiscriminators.AddDiscriminator(x => BraidLinkDiscriminationResult.ISaidYes);

            var chainLongitudinalEvaluators = LongitudinalEvaluators.Build();

            string jsInitialize = "function Initialize() {return 0;}";

            string jsEvaluator = " function Evaluate(n, e, s) {" +
                                 " " +
                                 " s += 1;" +
                                 " var result = {};" +
                                 " result.Value = {};" +
                                 " result.Value.X = JSON.stringify(e);" +
                                 " result.Value.Y = e.OccurredOn;" +
                                 " result.State = s;" +
                                 " return result;" +

                                 " }";

            string jsDistance = " function Distance(eventA, eventB, evaluator, state) { return eventA.OccurredOn - eventB.OccurredOn; } ";

            chainLongitudinalEvaluators.AddEvaluator(LongitudinalEvaluator.Build("Identity", jsInitialize, jsEvaluator, jsDistance));

            var chainBuilder = ChainBuilder.Build(x => ChainIdentity.Build(x.Payload["EntityName"].ToString()), chainLongitudinalEvaluators);

            var braidBuilder = BraidBuilder.Build("Initial", braidDiscriminators, chainBuilder, LongitudinalEvaluators.Build());

            trunk.AddBraid(braidBuilder);

            trunk.Accept(markovA);
            trunk.Accept(markovB);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var trunk = new Trunk();

            var discriminators = new BraidLinkDiscriminators();

            var rawEvents = EventManager.DeserializeEventsFromFiles("financialaid");

            var markovEvents = new List <MarkovEvent>();

            var engine     = new Engine().Execute("function parse(o){ return JSON.parse(o);}");
            var parseEvent = engine.GetValue("parse");

            for (int i = 0; i < rawEvents.Item1.Count; i++)
            {
                var rawEvent    = rawEvents.Item1[i];
                var rawMetadata = rawEvents.Item2[i];

                var jsEvent    = parseEvent.Invoke(rawEvent).AsObject();
                var jsMetadata = parseEvent.Invoke(rawMetadata).AsObject();

                Func <JsValue, object> parseValue = value =>
                {
                    switch (value.Type)
                    {
                    case Jint.Runtime.Types.String:
                    default:
                        return(value.ToString());

                    case Jint.Runtime.Types.Boolean:
                        return(Boolean.Parse(value.ToString()));

                    case Jint.Runtime.Types.Number:
                        return(Decimal.Parse(value.ToString()));

                    case Jint.Runtime.Types.None:
                    case Jint.Runtime.Types.Undefined:
                    case Jint.Runtime.Types.Null:
                    case Jint.Runtime.Types.Object:
                        throw new ArgumentOutOfRangeException(value.Type.ToString());
                    }
                };

                var eventProperties    = new Dictionary <string, object>(jsEvent.GetOwnProperties().Select(x => new KeyValuePair <string, object>(x.Key, parseValue(x.Value.Value))));
                var metadataProperties = new Dictionary <string, string>(jsMetadata.GetOwnProperties().Select(x => new KeyValuePair <string, string>(x.Key, (string)x.Value.Value.ToObject())));

                var occurredOn = DateTime.Parse(eventProperties["OccurredOn"].ToString());
                var eventNames = metadataProperties["CLRType"].Split(',')[0].Split('.');
                var eventName  = eventNames[eventNames.Length - 1];

                var markovEvent = MarkovEvent.Build(eventName, eventProperties, occurredOn, rawEvent);

                markovEvents.Add(markovEvent);
            }

            discriminators.AddDiscriminator(x =>
            {
                switch (x)
                {
                case ChainLink l when l.MarkovEvent.EventType == "Blb": return(BraidLinkDiscriminationResult.HardNo);

                default: return(BraidLinkDiscriminationResult.Yes);
                }
            });

            var chainBuilder = ChainBuilder.Build(x =>
            {
                var value = string.Empty;

                //switch (x)
                //{
                //        default:
                //            value = x.Payload["EntityId"].ToString();
                //}
                value = x.Payload["EntityName"].ToString();

                return(ChainIdentity.Build(value));
            }, LongitudinalEvaluators.Build());

            var braidBuilder = BraidBuilder.Build("test", discriminators, chainBuilder, LongitudinalEvaluators.Build());

            trunk.AddBraid(braidBuilder);
            Console.WriteLine(markovEvents.Count);
            foreach (var markovEvent in markovEvents)
            {
                trunk.Accept(markovEvent);
            }

            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }