public HostDirectoryActor() : base() { var behaviors = new Behaviors(); behaviors.AddBehavior(new ActionBehavior <SerialObject>()); behaviors.AddBehavior(new ActionBehavior <IActor>()); behaviors.AddBehavior(new ActionBehavior()); behaviors.AddBehavior(new Behavior <SerialObject>(DoRouting)); Become(behaviors); }
public DirectoryActor() : base() { Console.WriteLine("Dictionary starts and autoregisters"); fDictionary.Add("Directory", this); Behaviors bhvs = new Behaviors(); bhvs.AddBehavior(new ActionBehavior <IActor>()); bhvs.AddBehavior(new ActionBehavior <IActor, string>()); Become(bhvs); }
private static IActor CreateActorFor(T aT) { // what methods ? List <MethodInfo> methods = new List <MethodInfo>(); AddMethodsToList(methods, typeof(I)); // add interface method for I // proxy to create ActorProxyModel <T> actor = new ActorProxyModel <T>(); // method to behavior Behaviors behaviors = new Behaviors(); foreach (MethodInfo methodInfo in methods) { // Get the return type and argument types Type returnType = methodInfo.ReturnType; List <Type> argumentTypes = new List <Type>(); foreach (ParameterInfo parameterInfo in methodInfo.GetParameters()) { argumentTypes.Add(parameterInfo.ParameterType); } // no return type => send message if (returnType != typeof(void)) // return type => send to a future { // add behavior future message here behaviors.AddBehavior(new Behavior <string, Future <string> >((s, f) => s == methodInfo.Name, (s, f) => { var ret = methodInfo.Invoke(aT, Type.EmptyTypes); f.SendMessage(ret); } )); } else { // add behavior send message here behaviors.AddBehavior(new Behavior <string, object>((s, t) => s == methodInfo.Name, (s, t) => methodInfo.Invoke(aT, new object[] { t }))); } } actor.AddBehaviors(behaviors); actor.SetObject(aT); return(actor); }
private void BuildFromAttributes() { Behaviors bhvs = new Behaviors(); // Launch reflexion MemberInfo[] memberInfo = GetType().GetMethods(); foreach (var mi in memberInfo) { #if NETCOREAPP1_1 BehaviorAttribute deco = (BehaviorAttribute)mi.GetType().GetTypeInfo().GetCustomAttribute(typeof(BehaviorAttribute)); #else BehaviorAttribute deco = (BehaviorAttribute)Attribute.GetCustomAttribute(mi, typeof(BehaviorAttribute)); #endif if (deco != null) { // add behavior if we can read this method var parameters = ((MethodInfo)mi).GetParameters(); if (parameters.Length == 1) { if (parameters[0].ParameterType == typeof(string)) { Behavior <string> bhv = new Behavior <string>(s => ((MethodInfo)mi).Invoke(this, new[] { s })); bhvs.AddBehavior(bhv); } } } } Become(bhvs); }
public void BehaviorsFunctionalTest() { IActor actor = new BaseActor(); IBehaviors behaviors = new Behaviors(); behaviors.LinkToActor(actor); Assert.AreEqual(actor, behaviors.LinkedActor); behaviors .AddBehavior(new Behavior <string>()) .AddBehavior(new Behavior <int>()); Assert.AreEqual(2, behaviors.AllBehaviors().Count()); Assert.IsTrue(behaviors.AllBehaviors().Any(b => b is IBehavior <string>)); Assert.IsTrue(behaviors.AllBehaviors().Any(b => b is IBehavior <int>)); foreach (var bhv in behaviors.AllBehaviors()) { Assert.IsTrue(bhv.LinkedTo == behaviors); } IBehavior bhvString = behaviors.AllBehaviors().First(b => b is IBehavior <string>); behaviors.RemoveBehavior(bhvString); Assert.IsFalse(behaviors.FindBehavior(bhvString)); Assert.IsTrue(behaviors.AllBehaviors().Count() == 1); Assert.IsFalse(bhvString.LinkedTo == behaviors); Assert.IsTrue(bhvString.LinkedActor == null); }
public DirectoryActor() : base() { Console.WriteLine(Value); _dictionary.Add(Name, this); Behaviors bhvs = new Behaviors(); bhvs.AddBehavior(new ActionBehavior <IActor>()) .AddBehavior(new ActionBehavior <IActor, string>()); Become(bhvs); }
public void BehaviorsSimpleTest() { var actor = new BaseActor(); var behaviors = new Behaviors(); behaviors.LinkToActor(actor); Assert.AreEqual(actor, behaviors.LinkedActor); var behavior1 = new Behavior <string>(); var behavior2 = new Behavior <int>(); behaviors.AddBehavior(behavior1); behaviors.AddBehavior(behavior2); behaviors.AddBehavior(null); var allBehaviors = behaviors.AllBehaviors(); Assert.AreEqual(2, allBehaviors.Count()); Assert.IsTrue(allBehaviors.Contains(behavior1)); Assert.IsTrue(allBehaviors.Contains(behavior2)); Assert.IsTrue(behaviors.FindBehavior(behavior1)); Assert.IsTrue(behaviors.FindBehavior(behavior2)); var behavior3 = new Behavior <object>(); Assert.IsFalse(behaviors.FindBehavior(behavior3)); Assert.IsFalse(behaviors.FindBehavior(null)); Assert.IsTrue(behavior1.LinkedTo == behaviors); Assert.IsTrue(behavior2.LinkedTo == behaviors); behaviors.RemoveBehavior(behavior2); Assert.IsFalse(behaviors.FindBehavior(behavior2)); Assert.IsTrue(behaviors.FindBehavior(behavior1)); Assert.IsTrue(behavior1.LinkedTo == behaviors); Assert.IsFalse(behavior2.LinkedTo == behaviors); Assert.IsTrue(behavior1.LinkedActor == actor); Assert.IsTrue(behavior2.LinkedActor == null); }
public void BehaviorsTest() { var behaviors = new Behaviors(); Assert.IsFalse(behaviors.AllBehaviors().Any()); var bhv1 = new Behavior(a => a != null, a => { }); behaviors = new Behaviors(new IBehavior[] { bhv1 }); Assert.IsTrue(behaviors.AllBehaviors().Count() == 1); Assert.IsTrue(behaviors.FindBehavior(bhv1)); Assert.IsTrue(bhv1.LinkedTo == behaviors); var bhv2 = new Behavior <string>(); behaviors.AddBehavior(bhv2); Assert.IsTrue(behaviors.AllBehaviors().Count() == 2); Assert.IsTrue(behaviors.FindBehavior(bhv2)); var actor = new BaseActor(behaviors); Assert.IsTrue(behaviors.LinkedActor == actor); }
public MapReduceActor ( Action <IActor, TData> senderKV, MapAction <TKeyMap, TValueMap> mapKV, ReduceFunction <TKeyReduce, TValueReduce> reduceKV, IActor outputActor ) : base() { // start reduce var bhvStart = new Behavior <TData>(d => { fActiveMap++; senderKV(this, d); }); // receive data to process var bhvInput = new Behavior <TKeyMap, TValueMap>( (k, v) => { // parse data var map = new MapActor <TKeyMap, TValueMap>(this, mapKV); fActiveMap++; map.SendMessage((IActor)this, k, v); } ); // end parse var bhvEndParse = new Behavior <TData, IActor>((d, a) => fActiveMap--); // receive from Map, index var bhvMap2Index = new Behavior <TKeyReduce, TValueReduce> ( (k, v) => { if (!fDict.TryGetValue(k, out List <TValueReduce> someValue)) { fDict[k] = new List <TValueReduce>(); } fDict[k].Add(v); } ); // receive end of job from Map, go to reduce var bhvMap2EndOfJov = new Behavior <MapActor <TKeyMap, TValueMap> > ( (a) => { fActiveMap--; if (fActiveMap <= 0) { // launch reduce foreach (var item in fDict) { var red = new ReduceActor <TKeyReduce, TValueReduce>(this, reduceKV); red.SendMessage(item.Key, item.Value.AsEnumerable()); } } } ); // receive from Reduce, send to output var bhvReduceToOutput = new Behavior <ReduceActor <TKeyReduce, TValueReduce>, TKeyReduce, TValueReduce> ( (r, k, v) => outputActor.SendMessage(k, v) ); Behaviors bhvs = new Behaviors(); bhvs.AddBehavior(bhvStart); bhvs.AddBehavior(bhvEndParse); bhvs.AddBehavior(bhvInput); bhvs.AddBehavior(bhvMap2Index); bhvs.AddBehavior(bhvMap2EndOfJov); bhvs.AddBehavior(bhvReduceToOutput); Become(bhvs); }
private void BuildFromAttributes() { Behaviors bhvs = new Behaviors(); // Launch reflexion MemberInfo[] memberInfo = GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance); foreach (var mi in memberInfo) { #if NETCOREAPP1_1 BehaviorAttribute deco = mi.GetCustomAttribute <BehaviorAttribute>(); #else BehaviorAttribute deco = (BehaviorAttribute)Attribute.GetCustomAttribute(mi, typeof(BehaviorAttribute)); #endif if (deco != null) { var parameters = ((MethodInfo)mi).GetParameters(); switch (parameters.Length) { case 0: { throw new ActorException(MessageCantUseDecoratedActporOnNullMessage); } case 1: { Behavior bhv = new Behavior( s => parameters[0].ParameterType.IsAssignableFrom(s.GetType()), s => ((MethodInfo)mi).Invoke(this, new[] { s })); bhvs.AddBehavior(bhv); break; } case 2: { Behavior bhv = new Behavior( s => { var ts = s.GetType(); return(ts.Name == typeof(MessageParam <,>).Name); }, s => { var ts = s.GetType(); var arg1 = ts.GetProperty("Item1").GetValue(s); var arg2 = ts.GetProperty("Item2").GetValue(s); ((MethodInfo)mi).Invoke(this, new[] { arg1, arg2 }); }); bhvs.AddBehavior(bhv); break; } case 3: { Behavior bhv = new Behavior( s => { var ts = s.GetType(); return(ts.Name == typeof(MessageParam <, ,>).Name); }, s => { var ts = s.GetType(); var arg1 = ts.GetProperty("Item1").GetValue(s); var arg2 = ts.GetProperty("Item2").GetValue(s); var arg3 = ts.GetProperty("Item3").GetValue(s); ((MethodInfo)mi).Invoke(this, new[] { arg1, arg2, arg3 }); }); bhvs.AddBehavior(bhv); break; } default: { throw new ActorException("Can't use Decorated Actor on too much arguments"); } } } } Become(bhvs); }