Esempio n. 1
0
        public void TestTweetExample()
        {
            Type  tweeterType          = typeof(Tweeter);
            ITang tang                 = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples });

            IConfiguration conf = cb.BindImplementation(GenericType <ITweetFactory> .Class, GenericType <MockTweetFactory> .Class)
                                  .BindImplementation(GenericType <ISMS> .Class, GenericType <MockSMS> .Class)
                                  .BindNamedParameter <Tweeter.PhoneNumber, long>(GenericType <Tweeter.PhoneNumber> .Class, "8675309")
                                  .Build();
            IInjector injector = tang.NewInjector(conf);
            var       tweeter  = (Tweeter)injector.GetInstance(tweeterType);

            tweeter.sendMessage();

            var sms     = (ISMS)injector.GetInstance(typeof(ISMS));
            var factory = (ITweetFactory)injector.GetInstance(typeof(ITweetFactory));

            Assert.NotNull(sms);
            Assert.NotNull(factory);
        }
Esempio n. 2
0
        public void TestMultiLayersWithBindImpl()
        {
            TangImpl.Reset();

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindImplementation(typeof(IH), typeof(L));
            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
            var       o = i.GetInstance <IH>();

            Assert.IsTrue(o is L);

            ICsConfigurationBuilder cb2 = TangFactory.GetTang().NewConfigurationBuilder();

            cb2.BindImplementation(typeof(IH), typeof(M));
            cb2.BindImplementation(typeof(M), typeof(L)); //construcotr of L is explicitly bound
            IInjector i2 = TangFactory.GetTang().NewInjector(cb2.Build());
            var       o2 = i2.GetInstance <IH>();

            Assert.IsTrue(o2 is L);
        }
        public void TestLegacyConstructor()
        {
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder();

            IList <string> constructorArg = new List <string>();

            constructorArg.Add(ReflectionUtilities.GetAssemblyQualifiedName(typeof(int)));
            constructorArg.Add(ReflectionUtilities.GetAssemblyQualifiedName(typeof(string)));
            cb.RegisterLegacyConstructor(ReflectionUtilities.GetAssemblyQualifiedName(typeof(LegacyConstructor)), constructorArg);
            //cb.Bind(typeof(LegacyConstructor), typeof(LegacyConstructor));
            cb.BindImplementation(GenericType <LegacyConstructor> .Class, GenericType <LegacyConstructor> .Class);

            IInjector i = tang.NewInjector(cb.Build());

            i.BindVolatileInstance(GenericType <int> .Class, 42);
            i.BindVolatileInstance(GenericType <string> .Class, "The meaning of life is ");
            LegacyConstructor l = i.GetInstance <LegacyConstructor>();

            Assert.AreEqual(42, l.X);
            Assert.AreEqual("The meaning of life is ", l.Y);
        }
Esempio n. 4
0
        public void TestActivityConfigWithSeparateAssembly()
        {
            Type  activityInterfaceType = typeof(ITask);
            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Common, FileNames.Tasks });

            cb.BindImplementation(GenericType <ITask> .Class, GenericType <HelloTask> .Class);
            IConfiguration conf = cb.Build();

            ConfigurationFile.WriteConfigurationFile(conf, "TaskConf1.txt");
            IDictionary <string, string> p = ConfigurationFile.FromFile("TaskConf1.txt");

            IInjector injector    = tang.NewInjector(new string[] { FileNames.Common, FileNames.Tasks }, "TaskConf1.txt");
            var       activityRef = (ITask)injector.GetInstance(activityInterfaceType);

            // combined line sample
            var o = (ITask)TangFactory.GetTang()
                    .NewInjector(new string[] { FileNames.Common, FileNames.Tasks }, "TaskConf1.txt")
                    .GetInstance(typeof(ITask));

            Assert.NotNull(activityRef);
        }
Esempio n. 5
0
        public void TestActivityConfig()
        {
            Type activityInterfaceType = typeof(com.microsoft.reef.activity.IActivity);
            var  a            = Assembly.Load(@"com.microsoft.reef.activity");
            Type activityType = a.GetType("com.microsoft.reef.activity.HelloActivity");

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { file, file2, file3 });

            cb.BindImplementation(activityInterfaceType, activityType);
            IConfiguration conf = cb.Build();

            ConfigurationFile.WriteConfigurationFile(conf, "activityConf.txt");
            IDictionary <string, string> p = ReadFromFile("activityConf.txt");

            IInjector injector    = tang.NewInjector(new string[] { file2, file3 }, "activityConf.txt");
            var       activityRef = (com.microsoft.reef.activity.IActivity)injector.GetInstance(activityInterfaceType);

            Assert.IsNotNull(activityRef);
            //byte[] b = new byte[10];
            //activityRef.Call(b);
        }
Esempio n. 6
0
        public void TestDeserializedConfigMerge()
        {
            Type  activityInterfaceType = typeof(ITask);
            ITang tang = TangFactory.GetTang();

            ICsConfigurationBuilder cb1 = tang.NewConfigurationBuilder();

            cb1.BindImplementation(GenericType <ITask> .Class, GenericType <HelloTask> .Class);
            cb1.BindNamedParameter <TaskConfigurationOptions.Identifier, string>(
                GenericType <TaskConfigurationOptions.Identifier> .Class, "Hello Task");
            IConfiguration conf1      = cb1.Build();
            var            serializer = new AvroConfigurationSerializer();

            serializer.ToFile(conf1, "task.config");

            ProtocolBufferClassHierarchy.Serialize("Task.bin", conf1.GetClassHierarchy());
            IClassHierarchy ns1 = ProtocolBufferClassHierarchy.DeSerialize("Task.bin");

            ICsConfigurationBuilder cb2 = tang.NewConfigurationBuilder();

            cb2.BindNamedParameter <Timer.Seconds, int>(GenericType <Timer.Seconds> .Class, "2");
            IConfiguration conf2 = cb2.Build();

            serializer.ToFile(conf2, "timer.config");

            ProtocolBufferClassHierarchy.Serialize("Timer.bin", conf2.GetClassHierarchy());
            IClassHierarchy ns2 = ProtocolBufferClassHierarchy.DeSerialize("Timer.bin");

            AvroConfiguration taskAvroconfiguration = serializer.AvroDeserializeFromFile("task.config");
            IConfiguration    taskConfiguration     = serializer.FromAvro(taskAvroconfiguration, ns1);

            AvroConfiguration timerAvroconfiguration = serializer.AvroDeserializeFromFile("timer.config");
            IConfiguration    timerConfiguration     = serializer.FromAvro(timerAvroconfiguration, ns2);

            IConfiguration merged = Configurations.MergeDeserializedConfs(taskConfiguration, timerConfiguration);

            var b = merged.newBuilder().Build();
        }
Esempio n. 7
0
        public void TestActivityWithBinding()
        {
            Type activityInterfaceType = typeof(com.microsoft.reef.activity.IActivity);
            var  a            = Assembly.Load(@"com.microsoft.reef.activity");
            var  a1           = Assembly.Load(@"com.microsoft.reef.activityInterface");
            Type activityType = a.GetType("com.microsoft.reef.activity.HelloActivity");

            ITang tang = TangFactory.GetTang();
            ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { file2, file3 });

            cb.BindImplementation(activityInterfaceType, activityType);
            Type namedParameter = a1.GetType(@"com.microsoft.reef.driver.activity.ActivityConfigurationOptions+Identifier");

            cb.BindNamedParameter(namedParameter, "Hello Activity");

            IConfiguration conf        = cb.Build();
            IInjector      injector    = tang.NewInjector(conf);
            var            activityRef = (com.microsoft.reef.activity.IActivity)injector.GetInstance(activityInterfaceType);

            Assert.IsNotNull(activityRef);

            byte[] b = new byte[10];
            //activityRef.Call(b);
        }
Esempio n. 8
0
        /// <summary>
        /// Gets the task configuration for the operator topology.
        /// </summary>
        /// <param name="taskId">The task identifier</param>
        /// <returns>The task configuration</returns>
        public IConfiguration GetTaskConfiguration(string taskId)
        {
            if (taskId == null)
            {
                throw new ArgumentException("TaskId is null when GetTaskConfiguration");
            }

            TaskNode selfTaskNode = GetTaskNode(taskId);

            if (selfTaskNode == null)
            {
                throw new ArgumentException("Task has not been added to the topology");
            }

            string   parentId;
            TaskNode parent = selfTaskNode.Parent;

            if (parent == null)
            {
                parentId = selfTaskNode.TaskId;
            }
            else
            {
                parentId = parent.TaskId;
            }

            //add parentid, if no parent, add itself
            ICsConfigurationBuilder confBuilder = TangFactory.GetTang().NewConfigurationBuilder()
                                                  .BindNamedParameter <GroupCommConfigurationOptions.TopologyRootTaskId, string>(
                GenericType <GroupCommConfigurationOptions.TopologyRootTaskId> .Class,
                parentId);

            //add all its children
            foreach (TaskNode childNode in selfTaskNode.GetChildren())
            {
                confBuilder.BindSetEntry <GroupCommConfigurationOptions.TopologyChildTaskIds, string>(
                    GenericType <GroupCommConfigurationOptions.TopologyChildTaskIds> .Class,
                    childNode.TaskId);
            }

            if (OperatorSpec is BroadcastOperatorSpec)
            {
                var broadcastSpec = OperatorSpec as BroadcastOperatorSpec;
                if (taskId.Equals(broadcastSpec.SenderId))
                {
                    confBuilder.BindImplementation(GenericType <IGroupCommOperator <T> > .Class, GenericType <BroadcastSender <T> > .Class);
                    SetMessageType(typeof(BroadcastSender <T>), confBuilder);
                }
                else
                {
                    confBuilder.BindImplementation(GenericType <IGroupCommOperator <T> > .Class, GenericType <BroadcastReceiver <T> > .Class);
                    SetMessageType(typeof(BroadcastReceiver <T>), confBuilder);
                }
            }
            else if (OperatorSpec is ReduceOperatorSpec)
            {
                var reduceSpec = OperatorSpec as ReduceOperatorSpec;
                if (taskId.Equals(reduceSpec.ReceiverId))
                {
                    confBuilder.BindImplementation(GenericType <IGroupCommOperator <T> > .Class, GenericType <ReduceReceiver <T> > .Class);
                    SetMessageType(typeof(ReduceReceiver <T>), confBuilder);
                }
                else
                {
                    confBuilder.BindImplementation(GenericType <IGroupCommOperator <T> > .Class, GenericType <ReduceSender <T> > .Class);
                    SetMessageType(typeof(ReduceSender <T>), confBuilder);
                }
            }
            else if (OperatorSpec is ScatterOperatorSpec)
            {
                ScatterOperatorSpec scatterSpec = OperatorSpec as ScatterOperatorSpec;
                if (taskId.Equals(scatterSpec.SenderId))
                {
                    confBuilder.BindImplementation(GenericType <IGroupCommOperator <T> > .Class, GenericType <ScatterSender <T> > .Class);
                    SetMessageType(typeof(ScatterSender <T>), confBuilder);
                }
                else
                {
                    confBuilder.BindImplementation(GenericType <IGroupCommOperator <T> > .Class, GenericType <ScatterReceiver <T> > .Class);
                    SetMessageType(typeof(ScatterReceiver <T>), confBuilder);
                }
            }
            else
            {
                throw new NotSupportedException("Spec type not supported");
            }

            return(Configurations.Merge(confBuilder.Build(), OperatorSpec.Configiration));
        }