public void ModuleInUnreferencedAssemblyInitializedByModuleInitializer()
        {
            AssemblyCatalog assemblyCatalog = new AssemblyCatalog(GetPathToModuleDll());
            CompositionContainer compositionContainer = new CompositionContainer(assemblyCatalog);

            ModuleCatalog moduleCatalog = new ModuleCatalog();

            Mock<MefFileModuleTypeLoader> mockFileTypeLoader = new Mock<MefFileModuleTypeLoader>();

            compositionContainer.ComposeExportedValue<IModuleCatalog>(moduleCatalog);
            compositionContainer.ComposeExportedValue<MefFileModuleTypeLoader>(mockFileTypeLoader.Object);

            bool wasInit = false;
            var mockModuleInitializer = new Mock<IModuleInitializer>();
            mockModuleInitializer.Setup(x => x.Initialize(It.IsAny<ModuleInfo>())).Callback(() => wasInit = true);

            var mockLoggerFacade = new Mock<ILoggerFacade>();

            MefModuleManager moduleManager = new MefModuleManager(
                mockModuleInitializer.Object,
                moduleCatalog,
                mockLoggerFacade.Object);

            compositionContainer.SatisfyImportsOnce(moduleManager);

            moduleManager.Run();

            Assert.IsTrue(wasInit);
        }
示例#2
0
        protected override IModuleCatalog GetModuleCatalog()
        {
            var catalog = new ModuleCatalog();
            catalog.AddModule((typeof(MainModule)));

            return catalog;
        }
示例#3
0
        protected override IModuleCatalog GetModuleCatalog()
        {
            var catalog = new ModuleCatalog();
              catalog.AddModule(typeof(BugReportModule));

              return catalog;
        }
示例#4
0
 protected override IModuleCatalog GetModuleCatalog()
 {
     var catalog = new ModuleCatalog();
     catalog.AddModule(typeof (EmployeeModule));
     catalog.AddModule(typeof (TimesheetModule));
     return catalog;
 }
示例#5
0
 protected override IModuleCatalog CreateModuleCatalog()
 {
     ModuleCatalog moduleCatalog = new ModuleCatalog();
     moduleCatalog.AddModule(typeof(AdminModule));
     moduleCatalog.AddModule(typeof(ArtistsModule));
     return moduleCatalog;
 }
        public void UnknownExportedModuleIsAddedAndInitializedByModuleInitializer()
        {
            var aggregateCatalog = new AggregateCatalog();
            var compositionContainer = new CompositionContainer(aggregateCatalog);

            var moduleCatalog = new ModuleCatalog();

            var mockModuleTypeLoader = new Mock<MefXapModuleTypeLoader>(new DownloadedPartCatalogCollection());

            compositionContainer.ComposeExportedValue<IModuleCatalog>(moduleCatalog);
            compositionContainer.ComposeExportedValue<MefXapModuleTypeLoader>(mockModuleTypeLoader.Object);

            bool wasInit = false;
            var mockModuleInitializer = new Mock<IModuleInitializer>();
            mockModuleInitializer.Setup(x => x.Initialize(It.IsAny<ModuleInfo>())).Callback(() => wasInit = true);

            var mockLoggerFacade = new Mock<ILoggerFacade>();

            var moduleManager =
                new MefModuleManager(mockModuleInitializer.Object, moduleCatalog, mockLoggerFacade.Object);

            aggregateCatalog.Catalogs.Add(new TypeCatalog(typeof(TestMefModule)));

            compositionContainer.SatisfyImportsOnce(moduleManager);

            moduleManager.Run();

            Assert.IsTrue(wasInit);
            Assert.IsTrue(moduleCatalog.Modules.Any(mi => mi.ModuleName == "TestMefModule"));
        }
		protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
		{
			ModuleCatalog catalog = new ModuleCatalog();
			catalog.AddModule(typeof(Module1.Module1));
			catalog.AddModule(typeof(Module2.Module2));
			return catalog;
		}
        /// <summary>
        /// Creates the <see cref="T:Microsoft.Practices.Prism.Modularity.IModuleCatalog" /> used by Prism.
        /// </summary>
        /// <returns></returns>
        /// <remarks>The base implementation returns a new ModuleCatalog.</remarks>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog();
            catalog.AddModule(typeof(NeurochipModule));
            catalog.AddModule(typeof(GhostModule));

            return catalog;
        }
        protected override IModuleCatalog CreateModuleCatalog()
        {
            base.ConfigureModuleCatalog();

            ModuleCatalog modules = new ModuleCatalog();
            modules.AddModule(typeof(PriceGridModule));
            return modules;  
        }
 protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
 {
     ModuleCatalog catalog = new ModuleCatalog();
     catalog.AddModule(typeof(IssueLib.IssueModule));
     catalog.AddModule(typeof(MapLib.MapModule));
     catalog.AddModule(typeof(NavigationLib.NavigationModule));
     return catalog;
 }
        protected override IModuleCatalog GetModuleCatalog()
        {
            var catalog = new ModuleCatalog();

            catalog.AddModule(typeof(AngelSystemModule));

            return catalog;
        }
示例#12
0
        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog()
                .AddModule(typeof(LoginModule))
                .AddModule(typeof(PleaseWaitModule))
                .AddModule(typeof(CompaniesListModule));

            return catalog;
        }
示例#13
0
        protected override IModuleCatalog CreateModuleCatalog()
        {
            var moduleCatalog = new ModuleCatalog();
            // TODO: A more sofisticated method would 
            // be to get the modules from a Catalog.xaml file or a Web Service
            moduleCatalog.AddModule(typeof (BasicAccountModule.BasicAccountModule));

            return moduleCatalog;
        }
        /// <summary>
        /// Populates the Module Catalog.
        /// </summary>
        /// <returns>A new Module Catalog.</returns>
        /// <remarks>
        /// This method uses the Module Discovery method of populating the Module Catalog. It requires
        /// a post-build event in each module to place the module assembly in the module catalog
        /// directory.
        /// </remarks>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            var moduleCatalog = new ModuleCatalog();
            moduleCatalog.AddModule(typeof(SearchHelperModule));
            moduleCatalog.AddModule(typeof(ReportsModule));
            moduleCatalog.AddModule(typeof(AdminModule));

            return moduleCatalog;
        }
 protected override IModuleCatalog GetModuleCatalog()
 {
     var moduleCatalog = new ModuleCatalog();
     foreach (var module in Container.GetAllInstances<IModule>())
     {
         moduleCatalog.AddModule(module.GetType());
     }
     return moduleCatalog;
 }
示例#16
0
        public void CanCreateCatalogFromList()
        {
            var moduleInfo = new ModuleInfo("MockModule", "type");
            List<ModuleInfo> moduleInfos = new List<ModuleInfo> { moduleInfo };

            var moduleCatalog = new ModuleCatalog(moduleInfos);

            Assert.AreEqual(1, moduleCatalog.Modules.Count());
            Assert.AreEqual(moduleInfo, moduleCatalog.Modules.ElementAt(0));
        }
示例#17
0
        /// <summary>
        /// 4. Registers the modules for application use
        /// </summary>
        /// <param name="catalog">The catalog.</param>
        protected override void RegisterModules(ModuleCatalog catalog)
        {
            catalog
                .Add<CategoryModule>()
                .Add<PasswordModule>()
                .Add<LoginModule>() // Last module is visible
                ;

            // Register module(s) with framework
            base.RegisterModules(catalog);
        }
示例#18
0
        protected override IModuleCatalog CreateModuleCatalog()
        {
            var catalog = new ModuleCatalog();

            catalog.AddModule(typeof(MenuModule));
            catalog.AddModule(typeof(SettingsModule));
            catalog.AddModule(typeof(ManifestsModule));
            catalog.AddModule(typeof(TestResultsModule));
            catalog.AddModule(typeof(TestCollectionsModule));
            catalog.AddModule(typeof(CommandHelpModule));

            return catalog;
        }
示例#19
0
        protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog moduleCatalog = new ModuleCatalog();

            // this is the code responsible 
            // for adding Module1 to the application
            moduleCatalog.AddModule
            (
                typeof(Zieschang.Net.Projects.SLJiraClient.DashboardModule.Module)
            );

            return moduleCatalog;
        }
示例#20
0
        public ModuleCatalog GetRegisteredModules()
        {
            // Register services before loading catalogs and regions
            ServicesRegistrator();

            var catalog = new ModuleCatalog();
            catalog.AddModule(typeof(NavigationModule));
            catalog.AddModule(typeof(HomeModule));
            catalog.AddModule(typeof(ArtistModule));
            catalog.AddModule(typeof(UserModule));

            return catalog;
        }
示例#21
0
        protected override IModuleCatalog CreateModuleCatalog()
        {
            var catalog = new ModuleCatalog();

            catalog.AddModule(typeof (InfrastructureServicesModule));
            catalog.AddModule(typeof(DalModule));
            catalog.AddModule(typeof(ServicesModule));

            catalog.AddModule(typeof(DesignServicesModule));
            catalog.AddModule(typeof(DesignClientRequestModule));
            catalog.AddModule(typeof (DesignContractorsModule));
            catalog.AddModule(typeof (HangarEditModule));
            catalog.AddModule(typeof (CatalogsModule));
            return catalog;
        }
示例#22
0
        /// <summary>
        /// Populates the Module Catalog.
        /// </summary>
        /// <returns>A new Module Catalog.</returns>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            /// The most direct way to create a module catalog is to create it in code.
            var moduleCatalog = new ModuleCatalog();

            //// Add the Dashboard Module to the catalog using code.
            //Type dashboardModule = typeof(DashboardModule);
            //moduleCatalog.AddModule(new ModuleInfo() { ModuleName = dashboardModule.Name, ModuleType = dashboardModule.AssemblyQualifiedName });

            // Add the Customers Module to the catalog using code.
            Type employeeModule = typeof(EmployeesModule);
            moduleCatalog.AddModule(new ModuleInfo() { ModuleName = employeeModule.Name, ModuleType = employeeModule.AssemblyQualifiedName });

            return moduleCatalog;
        }
示例#23
0
        protected override IModuleCatalog GetModuleCatalog()
        {
            string query = App.Current.Host.Source.Query;
            var level1 = new ModuleInfo(ModuleNames.level1, string.Format("{0}.Level1Module, {0}, Version=1.0.0.0", ModuleNames.level1)) { InitializationMode = InitializationMode.OnDemand, Ref = ModuleNames.level1 + ".xap" };
            var level2 = new ModuleInfo(ModuleNames.level2, string.Format("{0}.Level2Module, {0}, Version=1.0.0.0",ModuleNames.level2)) { InitializationMode = InitializationMode.OnDemand, Ref = ModuleNames.level2 + ".xap" };
            var level3 = new ModuleInfo(ModuleNames.level3, string.Format("{0}.Level3Module, {0}, Version=1.0.0.0", ModuleNames.level3)) { InitializationMode = InitializationMode.OnDemand, Ref = ModuleNames.level3 + ".xap" };
            var level4 = new ModuleInfo(ModuleNames.level4, string.Format("{0}.Level4Module, {0}, Version=1.0.0.0", ModuleNames.level4)) { InitializationMode = InitializationMode.OnDemand, Ref = ModuleNames.level4 + ".xap" };

            ModuleCatalog cat = new ModuleCatalog();
            cat.AddModule(level1);
            cat.AddModule(level2);
            cat.AddModule(level3);
            cat.AddModule(level4);
            return cat;
        }
示例#24
0
        public RouteResolver(
            ModuleCatalog catalog,
            ModuleBuilder moduleBuilder,
            RouteResolverTrie routeTrie)
        {
            if (catalog == null)
                throw new ArgumentNullException("catalog");
            if (moduleBuilder == null)
                throw new ArgumentNullException("moduleBuilder");
            if (routeTrie == null)
                throw new ArgumentNullException("routeTrie");

            _catalog = catalog;
            _moduleBuilder = moduleBuilder;
            _routeTrie = routeTrie;
        }
        protected override IModuleCatalog GetModuleCatalog()
        {
            var moduleCatalog = new ModuleCatalog();
            if ((_modules & Modules.Security) == Modules.Security)
                moduleCatalog.AddModule(typeof(SecurityModule));
            if ((_modules & Modules.Associations) == Modules.Associations)
                moduleCatalog.AddModule(typeof(AssociationsModule));
            if ((_modules & Modules.ClientLogin) == Modules.ClientLogin)
                moduleCatalog.AddModule(typeof(ClientLoginModule));
            if ((_modules & Modules.ClientPasswordRecovery) == Modules.ClientPasswordRecovery)
                moduleCatalog.AddModule(typeof(ClientPasswordRecoveryModule));
            if ((_modules & Modules.ClientRegistration) == Modules.ClientRegistration)
                moduleCatalog.AddModule(typeof(ClientRegistrationModule));

            return moduleCatalog;
        }
示例#26
0
 protected override IModuleCatalog CreateModuleCatalog()
 {
     var version = AssemblyExtensions.ParseVersionNumber(typeof(Bootstrapper).Assembly).ToString();
     var moduleCatalog = new ModuleCatalog();
     moduleCatalog.AddModule
     (
         new ModuleInfo
         {
             InitializationMode = InitializationMode.WhenAvailable,
             Ref = "file://ModuleOne.dll",
             ModuleName = "ModuleOne",
             ModuleType = string.Format("ModuleOne.ModuleOne, ModuleOne, Version={0}, Culture=neutral, " +
                                         "PublicKeyToken=null", version)
         }
     );
     return moduleCatalog;
 }
示例#27
0
        public void CanGetDependenciesForModule()
        {
            // A <- B
            var moduleInfoA = CreateModuleInfo("A");
            var moduleInfoB = CreateModuleInfo("B", "A");
            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
                                               {
                                                   moduleInfoA
                                                   , moduleInfoB
                                               };
            var moduleCatalog = new ModuleCatalog(moduleInfos);

            IEnumerable<ModuleInfo> dependentModules = moduleCatalog.GetDependentModules(moduleInfoB);

            Assert.AreEqual(1, dependentModules.Count());
            Assert.AreEqual(moduleInfoA, dependentModules.ElementAt(0));
        }
        // In silverlight, the REF value will likely be the URL to the XAP file where you can download the file
        // For example:
        // const string refValue = "url://servername/ModuleB.xap";
        // In this quickstart, this is not needed, because the ModuleB is already present. 

        // In WPF, the REF value will likely be the location where the system can find the file, if the path 
        // is not in the normal .net Assembly probe paths. For example:
        // const string refValue = "file://c:/Modules/ModuleB.dll";
      
        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog();

            // There are two ways of adding modules to the catalog in code
            // 1: If the shell has a direct reference to the modules, you can use
            //    typeof(Module) to add the module. 
            catalog.AddModule(typeof (ModuleA), "ModuleD")
                .AddModule(typeof (ModuleD), "ModuleB")
                .AddModule(typeof (ModuleC), InitializationMode.OnDemand)
                ;

            // 2: If the shell does not have a direct reference to the module, you have to
            //    specify the Assembly qualified typename. You might also have to specify where the 
            //    file can be found by specifying a 'Ref' value. (In this example it's not needed,
            //    because the assembly is in the .net probe path)
            catalog.AddModule(new ModuleInfo("ModuleB", moduleBAssemblyQualifiedName));
                
            return catalog;
        }
示例#29
0
        protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog moduleCatalog = new ModuleCatalog();

            moduleCatalog.AddModule(new ModuleInfo
            {
                InitializationMode = InitializationMode.OnDemand,
                Ref = "CAAC.CAFM.xap",
                ModuleName = "CAFMModule",
                ModuleType = "CAAC.CAFM.CAFMModule, CAAC.CAFM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            });

            moduleCatalog.AddModule(new ModuleInfo
            {
                InitializationMode = InitializationMode.OnDemand,
                Ref = "UniCloud.UniAuth.xap",
                ModuleName = "UniAuthModule",
                ModuleType = "UniCloud.UniAuth.UniAuthModule, UniCloud.UniAuth, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            });

            return moduleCatalog;
        }
示例#30
0
        public void CanCompleteListWithTheirDependencies()
        {
            // A <- B <- C
            var moduleInfoA = CreateModuleInfo("A");
            var moduleInfoB = CreateModuleInfo("B", "A");
            var moduleInfoC = CreateModuleInfo("C", "B");
            var moduleInfoOrphan = CreateModuleInfo("X", "B");

            List<ModuleInfo> moduleInfos = new List<ModuleInfo>
                                               {
                                                   moduleInfoA
                                                   , moduleInfoB
                                                   , moduleInfoC
                                                   , moduleInfoOrphan
                                               };
            var moduleCatalog = new ModuleCatalog(moduleInfos);

            IEnumerable<ModuleInfo> dependantModules = moduleCatalog.CompleteListWithDependencies(new[] { moduleInfoC });

            Assert.AreEqual(3, dependantModules.Count());
            Assert.IsTrue(dependantModules.Contains(moduleInfoA));
            Assert.IsTrue(dependantModules.Contains(moduleInfoB));
            Assert.IsTrue(dependantModules.Contains(moduleInfoC));
        }
        public void TestRequestedLearners()
        {
            // Get datasets
            var          pathData        = GetDataPath("adult.train");
            var          pathDataTest    = GetDataPath("adult.test");
            const int    numOfSampleRows = 100;
            const string schema          =
                "sep=, col=Features:R4:0,2,4,10-12 col=workclass:TX:1 col=education:TX:3 col=marital_status:TX:5 col=occupation:TX:6 " +
                "col=relationship:TX:7 col=race:TX:8 col=sex:TX:9 col=native_country:TX:13 col=label_IsOver50K_:R4:14 header=+";
            var inputFileTrain = new SimpleFileHandle(Env, pathData, false, false);

#pragma warning disable 0618
            var datasetTrain = ImportTextData.ImportText(Env,
                                                         new ImportTextData.Input {
                InputFile = inputFileTrain, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
            var inputFileTest = new SimpleFileHandle(Env, pathDataTest, false, false);
            var datasetTest   = ImportTextData.ImportText(Env,
                                                          new ImportTextData.Input {
                InputFile = inputFileTest, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
            var requestedLearners = new[] { $"LogisticRegressionBinaryClassifier", $"FastTreeBinaryClassifier" };
#pragma warning restore 0618
            // Define entrypoint graph
            string inputGraph = @"
                {
                  'Nodes': [                                
                    {
                      'Name': 'Models.PipelineSweeper',
                      'Inputs': {
                        'TrainingData': '$TrainingData',
                        'TestingData': '$TestingData',
                        'StateArguments': {
                            'Name': 'AutoMlState',
                            'Settings': {
                                'Metric': 'Auc',
                                'Engine': {
                                    'Name': 'Rocket',
                                    'Settings' : {
                                        'TopKLearners' : 2,
                                        'SecondRoundTrialsPerLearner' : 0
                                    },
                                },
                                'TerminatorArgs': {
                                    'Name': 'IterationLimited',
                                    'Settings': {
                                        'FinalHistoryLength': 35
                                    }
                                },
                                'TrainerKind': 'SignatureBinaryClassifierTrainer',
                                'RequestedLearners' : [
                                    'LogisticRegressionBinaryClassifier',
                                    'FastTreeBinaryClassifier'
                                ]
                            }
                        },
                        'BatchSize': 5
                      },
                      'Outputs': {
                        'State': '$StateOut',
                        'Results': '$ResultsOut'
                      }
                    },
                  ]
                }";

            JObject graph   = JObject.Parse(inputGraph);
            var     catalog = ModuleCatalog.CreateInstance(Env);

            var runner = new GraphRunner(Env, catalog, graph[FieldNames.Nodes] as JArray);
            runner.SetInput("TrainingData", datasetTrain);
            runner.SetInput("TestingData", datasetTest);
            runner.RunAll();

            var autoMlState = runner.GetOutput <AutoInference.AutoMlMlState>("StateOut");
            Assert.NotNull(autoMlState);
            var space = autoMlState.GetSearchSpace();

            // Make sure only learners left are those retained.
            Assert.Equal(requestedLearners.Length, space.Item2.Length);
            Assert.True(space.Item2.All(l => requestedLearners.Any(r => r == l.LearnerName)));
        }
        public void TestRocketPipelineEngine()
        {
            // Get datasets
            var          pathData        = GetDataPath("adult.train");
            var          pathDataTest    = GetDataPath("adult.test");
            const int    numOfSampleRows = 1000;
            int          numIterations   = 35;
            const string schema          =
                "sep=, col=Features:R4:0,2,4,10-12 col=workclass:TX:1 col=education:TX:3 col=marital_status:TX:5 col=occupation:TX:6 " +
                "col=relationship:TX:7 col=ethnicity:TX:8 col=sex:TX:9 col=native_country:TX:13 col=label_IsOver50K_:R4:14 header=+";
            var inputFileTrain = new SimpleFileHandle(Env, pathData, false, false);

#pragma warning disable 0618
            var datasetTrain = ImportTextData.ImportText(Env,
                                                         new ImportTextData.Input {
                InputFile = inputFileTrain, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
            var inputFileTest = new SimpleFileHandle(Env, pathDataTest, false, false);
            var datasetTest   = ImportTextData.ImportText(Env,
                                                          new ImportTextData.Input {
                InputFile = inputFileTest, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
#pragma warning restore 0618
            // Define entrypoint graph
            string inputGraph = @"
                {
                  'Nodes': [                                
                    {
                      'Name': 'Models.PipelineSweeper',
                      'Inputs': {
                        'TrainingData': '$TrainingData',
                        'TestingData': '$TestingData',
                        'StateArguments': {
                            'Name': 'AutoMlState',
                            'Settings': {
                                'Metric': 'Auc',
                                'Engine': {
                                    'Name': 'Rocket',
                                    'Settings' : {
                                        'TopKLearners' : 2,
                                        'SecondRoundTrialsPerLearner' : 5
                                    },
                                },
                                'TerminatorArgs': {
                                    'Name': 'IterationLimited',
                                    'Settings': {
                                        'FinalHistoryLength': 35
                                    }
                                },
                                'TrainerKind': 'SignatureBinaryClassifierTrainer'
                            }
                        },
                        'BatchSize': 5
                      },
                      'Outputs': {
                        'State': '$StateOut',
                        'Results': '$ResultsOut'
                      }
                    },
                  ]
                }";

            JObject graph   = JObject.Parse(inputGraph);
            var     catalog = ModuleCatalog.CreateInstance(Env);

            var runner = new GraphRunner(Env, catalog, graph[FieldNames.Nodes] as JArray);
            runner.SetInput("TrainingData", datasetTrain);
            runner.SetInput("TestingData", datasetTest);
            runner.RunAll();

            var autoMlState = runner.GetOutput <AutoInference.AutoMlMlState>("StateOut");
            Assert.NotNull(autoMlState);
            var allPipelines = autoMlState.GetAllEvaluatedPipelines();
            var bestPipeline = autoMlState.GetBestPipeline();
            Assert.Equal(allPipelines.Length, numIterations);
            Assert.True(bestPipeline.PerformanceSummary.MetricValue > 0.1);

            var results = runner.GetOutput <IDataView>("ResultsOut");
            Assert.NotNull(results);
            var rows = PipelinePattern.ExtractResults(Env, results,
                                                      "Graph", "MetricValue", "PipelineId", "TrainingMetricValue", "FirstInput", "PredictorModel");
            Assert.True(rows.Length == numIterations);
        }
示例#33
0
        /// <summary>
        /// Builds a JSON representation of all entry points and components of the <paramref name="catalog"/>.
        /// </summary>
        /// <param name="ectx">The exception context to use</param>
        /// <param name="catalog">The module catalog</param>
        public static JObject BuildAllManifests(IExceptionContext ectx, ModuleCatalog catalog)
        {
            Contracts.CheckValueOrNull(ectx);
            ectx.CheckValue(catalog, nameof(catalog));

            var jEntryPoints    = new JArray();
            var entryPointInfos = catalog.AllEntryPoints().ToArray();

            foreach (var entryPointInfo in entryPointInfos.OrderBy(x => x.Name))
            {
                jEntryPoints.Add(BuildEntryPointManifest(ectx, entryPointInfo, catalog));
            }

            var jKinds = new JArray();

            foreach (var kind in catalog.GetAllComponentKinds())
            {
                var jKind = new JObject();
                jKind[FieldNames.Kind] = kind;
                var jComponents = new JArray();
                foreach (var component in catalog.GetAllComponents(kind))
                {
                    jComponents.Add(BuildComponentManifest(ectx, component, catalog));
                }

                jKind[FieldNames.Components] = jComponents;

                jKinds.Add(jKind);
            }

            var jepKinds = new JArray();
            var kinds    = new List <Type>();

            foreach (var entryPointInfo in entryPointInfos)
            {
                if (entryPointInfo.InputKinds != null)
                {
                    kinds.AddRange(entryPointInfo.InputKinds);
                }
                if (entryPointInfo.OutputKinds != null)
                {
                    kinds.AddRange(entryPointInfo.OutputKinds);
                }
            }

            foreach (var epKind in kinds.Distinct().OrderBy(k => k.Name))
            {
                var jepKind = new JObject();
                jepKind[FieldNames.Kind] = epKind.Name;
                var jepKindFields = new JArray();
                var propertyInfos = epKind.GetProperties().AsEnumerable();
                propertyInfos = epKind.GetInterfaces().Aggregate(propertyInfos, (current, face) => current.Union(face.GetProperties()));
                foreach (var fieldInfo in propertyInfos)
                {
                    var jField = new JObject();
                    jField[FieldNames.Name] = fieldInfo.Name;
                    var type = CSharpGeneratorUtils.ExtractOptionalOrNullableType(fieldInfo.PropertyType);
                    // Dive inside Var.
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Var <>))
                    {
                        type = type.GetGenericArguments()[0];
                    }
                    var typeEnum = TlcModule.GetDataType(type);
                    jField[FieldNames.Type] = typeEnum.ToString();
                    jepKindFields.Add(jField);
                }
                jepKind[FieldNames.Settings] = jepKindFields;
                jepKinds.Add(jepKind);
            }

            var jResult = new JObject();

            jResult[FieldNames.TopEntryPoints]     = jEntryPoints;
            jResult[FieldNames.TopComponents]      = jKinds;
            jResult[FieldNames.TopEntryPointKinds] = jepKinds;
            return(jResult);
        }
示例#34
0
 /// <summary>
 /// Configures the <see cref="T:Microsoft.Practices.Prism.Modularity.IModuleCatalog"/> used by Prism.
 /// </summary>
 protected override void ConfigureModuleCatalog()
 {
     ModuleCatalog.Initialize();
 }
示例#35
0
        public void PipelineSweeperRoles()
        {
            // Get datasets
            var          pathData        = GetDataPath("adult.train");
            var          pathDataTest    = GetDataPath("adult.test");
            const int    numOfSampleRows = 100;
            int          numIterations   = 2;
            const string schema          =
                "sep=, col=age:R4:0 col=workclass:TX:1 col=fnlwgt:R4:2 col=education:TX:3 col=education_num:R4:4 col=marital_status:TX:5 col=occupation:TX:6 " +
                "col=relationship:TX:7 col=ethnicity:TX:8 col=sex:TX:9 col=Features:R4:10-12 col=native_country:TX:13 col=IsOver50K_:R4:14 header=+";
            var inputFileTrain = new SimpleFileHandle(Env, pathData, false, false);

#pragma warning disable 0618
            var datasetTrain = ImportTextData.ImportText(Env,
                                                         new ImportTextData.Input {
                InputFile = inputFileTrain, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
            var inputFileTest = new SimpleFileHandle(Env, pathDataTest, false, false);
            var datasetTest   = ImportTextData.ImportText(Env,
                                                          new ImportTextData.Input {
                InputFile = inputFileTest, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
#pragma warning restore 0618

            // Define entrypoint graph
            string inputGraph = @"
                {
                  'Nodes': [
                    {
                      'Name': 'Models.PipelineSweeper',
                      'Inputs': {
                        'TrainingData': '$TrainingData',
                        'TestingData': '$TestingData',
                        'LabelColumns': ['IsOver50K_'],
                        'WeightColumns': ['education_num'],
                        'NameColumns': ['education'],
                        'TextFeatureColumns': ['workclass', 'marital_status', 'occupation'],
                        'StateArguments': {
                            'Name': 'AutoMlState',
                            'Settings': {
                                'Metric': 'Auc',
                                'Engine': {
                                    'Name': 'Defaults'
                                },
                                'TerminatorArgs': {
                                    'Name': 'IterationLimited',
                                    'Settings': {
                                        'FinalHistoryLength': 2
                                    }
                                },
                                'TrainerKind': 'SignatureBinaryClassifierTrainer',
                                'RequestedLearners' : [
                                    'LogisticRegressionBinaryClassifier',
                                    'FastTreeBinaryClassifier'
                                ]
                            }
                        },
                        'BatchSize': 1
                      },
                      'Outputs': {
                        'State': '$StateOut',
                        'Results': '$ResultsOut'
                      }
                    },
                  ]
                }";

            JObject graphJson = JObject.Parse(inputGraph);
            var     catalog   = ModuleCatalog.CreateInstance(Env);
            var     runner    = new GraphRunner(Env, catalog, graphJson[FieldNames.Nodes] as JArray);
            runner.SetInput("TrainingData", datasetTrain);
            runner.SetInput("TestingData", datasetTest);
            runner.RunAll();

            var autoMlState = runner.GetOutput <AutoInference.AutoMlMlState>("StateOut");
            Assert.NotNull(autoMlState);
            var allPipelines = autoMlState.GetAllEvaluatedPipelines();
            var bestPipeline = autoMlState.GetBestPipeline();
            Assert.Equal(allPipelines.Length, numIterations);

            var trainAuc = bestPipeline.PerformanceSummary.TrainingMetricValue;
            var testAuc  = bestPipeline.PerformanceSummary.MetricValue;
            Assert.True((0.94 < trainAuc) && (trainAuc < 0.95));
            Assert.True((0.83 < testAuc) && (testAuc < 0.84));

            var results = runner.GetOutput <IDataView>("ResultsOut");
            Assert.NotNull(results);
            var rows = PipelinePattern.ExtractResults(Env, results,
                                                      "Graph", "MetricValue", "PipelineId", "TrainingMetricValue", "FirstInput", "PredictorModel");
            Assert.True(rows.Length == numIterations);
            Assert.True(rows.All(r => r.TrainingMetricValue > 0.1));
        }
示例#36
0
 protected override IModuleCatalog CreateModuleCatalog()
 {
     return(ModuleCatalog.CreateFromXaml(new Uri("/Modules;component/ModuleCatalog.xaml", UriKind.Relative)));
 }
示例#37
0
        private static JObject BuildComponentManifest(IExceptionContext ectx, ModuleCatalog.ComponentInfo componentInfo, ModuleCatalog catalog)
        {
            Contracts.AssertValueOrNull(ectx);
            ectx.AssertValue(componentInfo);
            ectx.AssertValue(catalog);
            var result = new JObject();

            result[FieldNames.Name]         = componentInfo.Name;
            result[FieldNames.Desc]         = componentInfo.Description;
            result[FieldNames.FriendlyName] = componentInfo.FriendlyName;
            if (Utils.Size(componentInfo.Aliases) > 0)
            {
                result[FieldNames.Aliases] = new JArray(componentInfo.Aliases);
            }

            result[FieldNames.Settings] = BuildInputManifest(ectx, componentInfo.ArgumentType, catalog);
            return(result);
        }
示例#38
0
 protected override void ConfigureModuleCatalog()
 {
     ModuleCatalog.AddModule(new ModuleInfo(typeof(ModuleA.ModuleAModule)));
     //ModuleCatalog.AddModule(new ModuleInfo("ModuleA", typeof(ModuleA.ModuleAModule), InitializationMode.OnDemand));
 }
示例#39
0
        private static JArray BuildOutputManifest(IExceptionContext ectx, Type outputType, ModuleCatalog catalog)
        {
            Contracts.AssertValueOrNull(ectx);
            ectx.AssertValue(outputType);
            ectx.AssertValue(catalog);

            var outputs = new List <KeyValuePair <Double, JObject> >();

            if (outputType.IsGenericType && outputType.GetGenericTypeDefinition() == typeof(CommonOutputs.MacroOutput <>))
            {
                outputType = outputType.GetGenericArguments()[0];
            }

            foreach (var fieldInfo in outputType.GetFields())
            {
                var outputAttr = fieldInfo.GetCustomAttributes(typeof(TlcModule.OutputAttribute), false)
                                 .FirstOrDefault() as TlcModule.OutputAttribute;
                if (outputAttr == null)
                {
                    continue;
                }

                var jo = new JObject();
                jo[FieldNames.Name] = outputAttr.Name ?? fieldInfo.Name;
                jo[FieldNames.Type] = BuildTypeToken(ectx, fieldInfo, fieldInfo.FieldType, catalog);
                jo[FieldNames.Desc] = outputAttr.Desc;

                outputs.Add(new KeyValuePair <Double, JObject>(outputAttr.SortOrder, jo));
            }
            return(new JArray(outputs.OrderBy(x => x.Key).Select(x => x.Value).ToArray()));
        }
示例#40
0
        public void CompleteListWithDependenciesThrowsWithNull()
        {
            var catalog = new ModuleCatalog();

            catalog.CompleteListWithDependencies(null);
        }
示例#41
0
        private static object MakeDictionary <T>(IExceptionContext ectx, JObject jDict, Attributes attributes, ModuleCatalog catalog)
        {
            Contracts.AssertValue(ectx);
            ectx.AssertValue(jDict);

            var dict = new Dictionary <string, T>();

            foreach (var pair in jDict)
            {
                dict[pair.Key] = (T)GetFieldAssignableValue(ectx, typeof(T), ParseJsonValue(ectx, typeof(T), attributes, pair.Value, catalog));
            }
            return(dict);
        }
示例#42
0
        private static object MakeArray <T>(IExceptionContext ectx, JArray jArray, Attributes attributes, ModuleCatalog catalog)
        {
            Contracts.AssertValue(ectx);
            ectx.AssertValue(jArray);

            T[] array = new T[jArray.Count];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = (T)GetFieldAssignableValue(ectx, typeof(T), ParseJsonValue(ectx, typeof(T), attributes, jArray[i], catalog));
            }
            return(array);
        }
示例#43
0
        private static IComponentFactory GetComponentJson(IExceptionContext ectx, Type signatureType, string name, JObject settings, ModuleCatalog catalog)
        {
            Contracts.AssertValue(ectx);
            ectx.AssertValue(signatureType);
            ectx.AssertNonEmpty(name);
            ectx.AssertValueOrNull(settings);
            ectx.AssertValue(catalog);

            if (!catalog.TryGetComponentKind(signatureType, out string kind))
            {
                throw ectx.Except($"Component type '{signatureType}' is not a valid signature type.");
            }

            if (!catalog.TryFindComponent(kind, name, out ModuleCatalog.ComponentInfo component))
            {
                var available = catalog.GetAllComponents(kind).Select(x => $"'{x.Name}'");
                throw ectx.Except($"Component '{name}' of kind '{kind}' is not found. Available components are: {string.Join(", ", available)}");
            }

            var inputBuilder = new InputBuilder(ectx, component.ArgumentType, catalog);

            if (settings != null)
            {
                foreach (var pair in settings)
                {
                    if (!inputBuilder.TrySetValueJson(pair.Key, pair.Value))
                    {
                        throw ectx.Except($"Unexpected value for component '{name}', field '{pair.Key}': '{pair.Value}'");
                    }
                }
            }

            var missing = inputBuilder.GetMissingValues().ToArray();

            if (missing.Length > 0)
            {
                throw ectx.Except($"The following required inputs were not provided for component '{name}': {string.Join(", ", missing)}");
            }
            return(inputBuilder.GetInstance() as IComponentFactory);
        }
示例#44
0
        public void PipelineSweeperSerialization()
        {
            // Get datasets
            var          pathData        = GetDataPath("adult.train");
            var          pathDataTest    = GetDataPath("adult.test");
            const int    numOfSampleRows = 1000;
            int          numIterations   = 10;
            const string schema          =
                "sep=, col=Features:R4:0,2,4,10-12 col=workclass:TX:1 col=education:TX:3 col=marital_status:TX:5 col=occupation:TX:6 " +
                "col=relationship:TX:7 col=ethnicity:TX:8 col=sex:TX:9 col=native_country:TX:13 col=label_IsOver50K_:R4:14 header=+";
            var inputFileTrain = new SimpleFileHandle(Env, pathData, false, false);

#pragma warning disable 0618
            var datasetTrain = ImportTextData.ImportText(Env,
                                                         new ImportTextData.Input {
                InputFile = inputFileTrain, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
            var inputFileTest = new SimpleFileHandle(Env, pathDataTest, false, false);
            var datasetTest   = ImportTextData.ImportText(Env,
                                                          new ImportTextData.Input {
                InputFile = inputFileTest, CustomSchema = schema
            }).Data.Take(numOfSampleRows);
#pragma warning restore 0618

            // Define entrypoint graph
            string inputGraph = @"
                {
                  'Nodes': [
                    {
                      'Name': 'Models.PipelineSweeper',
                      'Inputs': {
                        'TrainingData': '$TrainingData',
                        'TestingData': '$TestingData',
                        'StateArguments': {
                            'Name': 'AutoMlState',
                            'Settings': {
                                'Metric': 'Auc',
                                'Engine': {
                                    'Name': 'UniformRandom'
                                },
                                'TerminatorArgs': {
                                    'Name': 'IterationLimited',
                                    'Settings': {
                                        'FinalHistoryLength': 10
                                    }
                                },
                                'TrainerKind': 'SignatureBinaryClassifierTrainer'
                            }
                        },
                        'BatchSize': 5
                      },
                      'Outputs': {
                        'State': '$StateOut',
                        'Results': '$ResultsOut'
                      }
                    },
                  ]
                }";

            JObject graphJson = JObject.Parse(inputGraph);
            var     catalog   = ModuleCatalog.CreateInstance(Env);
            var     graph     = new EntryPointGraph(Env, catalog, graphJson[FieldNames.Nodes] as JArray);
            // Test if ToJson() works properly.
            var nodes  = new JArray(graph.AllNodes.Select(node => node.ToJson()));
            var runner = new GraphRunner(Env, catalog, nodes);
            runner.SetInput("TrainingData", datasetTrain);
            runner.SetInput("TestingData", datasetTest);
            runner.RunAll();

            var results = runner.GetOutput <IDataView>("ResultsOut");
            Assert.NotNull(results);
            var rows = PipelinePattern.ExtractResults(Env, results,
                                                      "Graph", "MetricValue", "PipelineId", "TrainingMetricValue", "FirstInput", "PredictorModel");
            Assert.True(rows.Length == numIterations);
        }
示例#45
0
        private static JToken BuildTypeToken(IExceptionContext ectx, FieldInfo fieldInfo, Type type, ModuleCatalog catalog)
        {
            Contracts.AssertValueOrNull(ectx);
            ectx.AssertValue(type);
            ectx.AssertValue(catalog);

            // REVIEW: Allows newly introduced types to not break the manifest bulding process.
            // Where possible, these types should be replaced by component kinds.
            if (type == typeof(CommonInputs.IEvaluatorInput) ||
                type == typeof(CommonOutputs.IEvaluatorOutput))
            {
                var jo         = new JObject();
                var typeString = $"{type}".Replace("Microsoft.ML.Runtime.EntryPoints.", "");
                jo[FieldNames.Kind]     = "EntryPoint";
                jo[FieldNames.ItemType] = typeString;
                return(jo);
            }
            type = CSharpGeneratorUtils.ExtractOptionalOrNullableType(type);

            // Dive inside Var.
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Var <>))
            {
                type = type.GetGenericArguments()[0];
            }

            var typeEnum = TlcModule.GetDataType(type);

            switch (typeEnum)
            {
            case TlcModule.DataKind.Unknown:
                var jo = new JObject();
                if (type == typeof(JArray))
                {
                    jo[FieldNames.Kind]     = TlcModule.DataKind.Array.ToString();
                    jo[FieldNames.ItemType] = "Node";
                    return(jo);
                }
                if (type == typeof(JObject))
                {
                    return("Bindings");
                }
                var fields = BuildInputManifest(ectx, type, catalog);
                if (fields.Count == 0)
                {
                    throw ectx.Except("Unexpected parameter type: {0}", type);
                }
                jo[FieldNames.Kind]   = "Struct";
                jo[FieldNames.Fields] = fields;
                return(jo);

            case TlcModule.DataKind.Float:
            case TlcModule.DataKind.Int:
            case TlcModule.DataKind.UInt:
            case TlcModule.DataKind.Char:
            case TlcModule.DataKind.String:
            case TlcModule.DataKind.Bool:
            case TlcModule.DataKind.DataView:
            case TlcModule.DataKind.TransformModel:
            case TlcModule.DataKind.PredictorModel:
            case TlcModule.DataKind.FileHandle:
                return(typeEnum.ToString());

            case TlcModule.DataKind.Enum:
                jo = new JObject();
                jo[FieldNames.Kind] = typeEnum.ToString();
                var values = Enum.GetNames(type).Where(n => type.GetField(n).GetCustomAttribute <HideEnumValueAttribute>() == null);
                jo[FieldNames.Values] = new JArray(values);
                return(jo);

            case TlcModule.DataKind.Array:
                jo = new JObject();
                jo[FieldNames.Kind]     = typeEnum.ToString();
                jo[FieldNames.ItemType] = BuildTypeToken(ectx, fieldInfo, type.GetElementType(), catalog);
                return(jo);

            case TlcModule.DataKind.Dictionary:
                jo = new JObject();
                jo[FieldNames.Kind]     = typeEnum.ToString();
                jo[FieldNames.ItemType] = BuildTypeToken(ectx, fieldInfo, type.GetGenericArguments()[1], catalog);
                return(jo);

            case TlcModule.DataKind.Component:
                string kind;
                if (!catalog.TryGetComponentKind(type, out kind))
                {
                    throw ectx.Except("Field '{0}' is a component of unknown kind", fieldInfo.Name);
                }

                jo = new JObject();
                jo[FieldNames.Kind]          = typeEnum.ToString();
                jo[FieldNames.ComponentKind] = kind;
                return(jo);

            case TlcModule.DataKind.State:
                jo = new JObject();
                var typeString = $"{type}".Replace("Microsoft.ML.Runtime.Interfaces.", "");
                jo[FieldNames.Kind]     = "C# Object";
                jo[FieldNames.ItemType] = typeString;
                return(jo);

            default:
                ectx.Assert(false);
                throw ectx.ExceptNotSupp();
            }
        }
        public static string GetValue(ModuleCatalog catalog, Type fieldType, object fieldValue,
                                      GeneratedClasses generatedClasses, string rootNameSpace)
        {
            if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Var <>))
            {
                return($"new Var<{GetCSharpTypeName(fieldType.GetGenericTypeArgumentsEx()[0])}>()");
            }

            if (fieldType.IsArray && Var <int> .CheckType(fieldType.GetElementType()))
            {
                return($"new ArrayVar<{GetCSharpTypeName(fieldType.GetElementType())}>()");
            }

            if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Dictionary <,>) &&
                fieldType.GetGenericTypeArgumentsEx()[0] == typeof(string))
            {
                return($"new DictionaryVar<{GetCSharpTypeName(fieldType.GetGenericTypeArgumentsEx()[1])}>()");
            }

            if (Var <int> .CheckType(fieldType))
            {
                return($"new Var<{GetCSharpTypeName(fieldType)}>()");
            }

            if (fieldValue == null)
            {
                return(null);
            }

            if (!fieldType.IsInterface)
            {
                try
                {
                    var defaultFieldValue = Activator.CreateInstance(fieldType);
                    if (defaultFieldValue == fieldValue)
                    {
                        return(null);
                    }
                }
                catch (MissingMethodException)
                {
                    // No parameterless constructor, ignore.
                }
            }

            var typeEnum = TlcModule.GetDataType(fieldType);

            fieldType = ExtractOptionalOrNullableType(fieldType, out bool isNullable, out bool isOptional);
            switch (typeEnum)
            {
            case TlcModule.DataKind.Array:
                var arr = fieldValue as Array;
                if (arr != null && arr.GetLength(0) > 0)
                {
                    return($"{{ {string.Join(", ", arr.Cast<object>().Select(item => GetValue(catalog, fieldType.GetElementType(), item, generatedClasses, rootNameSpace)))} }}");
                }
                return(null);

            case TlcModule.DataKind.String:
                var strval = fieldValue as string;
                if (strval != null)
                {
                    return(Quote(strval));
                }
                return(null);

            case TlcModule.DataKind.Float:
                if (fieldValue is double d)
                {
                    if (double.IsPositiveInfinity(d))
                    {
                        return("double.PositiveInfinity");
                    }
                    if (double.IsNegativeInfinity(d))
                    {
                        return("double.NegativeInfinity");
                    }
                    if (d != 0)
                    {
                        return(d.ToString("R") + "d");
                    }
                }
                else if (fieldValue is float f)
                {
                    if (float.IsPositiveInfinity(f))
                    {
                        return("float.PositiveInfinity");
                    }
                    if (float.IsNegativeInfinity(f))
                    {
                        return("float.NegativeInfinity");
                    }
                    if (f != 0)
                    {
                        return(f.ToString("R") + "f");
                    }
                }
                return(null);

            case TlcModule.DataKind.Int:
                if (fieldValue is int i)
                {
                    if (i != 0)
                    {
                        return(i.ToString());
                    }
                }
                else if (fieldValue is long l)
                {
                    if (l != 0)
                    {
                        return(l.ToString());
                    }
                }
                return(null);

            case TlcModule.DataKind.Bool:
                return((bool)fieldValue ? "true" : "false");

            case TlcModule.DataKind.Enum:
                if (generatedClasses.IsGenerated(fieldType.FullName))
                {
                    return(generatedClasses.GetApiName(fieldType, rootNameSpace) + "." + fieldValue);
                }
                else
                {
                    return(generatedClasses.GetApiName(fieldType, "Runtime") + "." + fieldValue);
                }

            case TlcModule.DataKind.Char:
                return($"'{GetCharAsString((char)fieldValue)}'");

            case TlcModule.DataKind.Component:
                var type = fieldValue.GetType();
                ModuleCatalog.ComponentInfo componentInfo;
                if (!catalog.TryFindComponent(fieldType, type, out componentInfo))
                {
                    return(null);
                }
                object defaultComponent = null;
                try
                {
                    defaultComponent = Activator.CreateInstance(componentInfo.ArgumentType);
                }
                catch (MissingMethodException)
                {
                    // No parameterless constructor, ignore.
                }
                var propertyBag = new List <string>();
                if (defaultComponent != null)
                {
                    foreach (var fieldInfo in componentInfo.ArgumentType.GetFields())
                    {
                        var inputAttr = fieldInfo.GetCustomAttributes(typeof(ArgumentAttribute), false).FirstOrDefault() as ArgumentAttribute;
                        if (inputAttr == null || inputAttr.Visibility == ArgumentAttribute.VisibilityType.CmdLineOnly)
                        {
                            continue;
                        }
                        if (fieldInfo.FieldType == typeof(JArray) || fieldInfo.FieldType == typeof(JObject))
                        {
                            continue;
                        }

                        var propertyValue        = GetValue(catalog, fieldInfo.FieldType, fieldInfo.GetValue(fieldValue), generatedClasses, rootNameSpace);
                        var defaultPropertyValue = GetValue(catalog, fieldInfo.FieldType, fieldInfo.GetValue(defaultComponent), generatedClasses, rootNameSpace);
                        if (propertyValue != defaultPropertyValue)
                        {
                            propertyBag.Add($"{Capitalize(inputAttr.Name ?? fieldInfo.Name)} = {propertyValue}");
                        }
                    }
                }
                var properties = propertyBag.Count > 0 ? $" {{ {string.Join(", ", propertyBag)} }}" : "";
                return($"new {GetComponentName(componentInfo)}(){properties}");

            case TlcModule.DataKind.Unknown:
                return($"new {generatedClasses.GetApiName(fieldType, rootNameSpace)}()");

            default:
                return(fieldValue.ToString());
            }
        }
示例#47
0
        /// <inheritdoc/>
        protected override void ConfigureModuleCatalog()
        {
            ModuleCatalog catalog = (ModuleCatalog)this.ModuleCatalog;

            catalog.AddModule(typeof(MainModule));
        }
示例#48
0
 protected override void ConfigureModuleCatalog()
 {
     ModuleCatalog catalog = (ModuleCatalog)ModuleCatalog;
 }
示例#49
0
        public void PipelineSweeperMultiClassClassification()
        {
            // Get datasets
            // TODO (agoswami) : For now we use the same dataset for train and test since the repo does not have a separate test file for the iris dataset.
            // In the future the PipelineSweeper Macro will have an option to take just one dataset as input, and do the train-test split internally.
            var          pathData       = GetDataPath(@"iris.txt");
            var          pathDataTest   = GetDataPath(@"iris.txt");
            int          numIterations  = 2;
            const string schema         = "col=Species:R4:0 col=SepalLength:R4:1 col=SepalWidth:R4:2 col=PetalLength:R4:3 col=PetalWidth:R4:4";
            var          inputFileTrain = new SimpleFileHandle(Env, pathData, false, false);

#pragma warning disable 0618
            var datasetTrain = ImportTextData.ImportText(Env, new ImportTextData.Input {
                InputFile = inputFileTrain, CustomSchema = schema
            }).Data;
            var inputFileTest = new SimpleFileHandle(Env, pathDataTest, false, false);
            var datasetTest   = ImportTextData.ImportText(Env, new ImportTextData.Input {
                InputFile = inputFileTest, CustomSchema = schema
            }).Data;
#pragma warning restore 0618

            // Define entrypoint graph
            string inputGraph = @"
                {
                  'Nodes': [
                    {
                      'Name': 'Models.PipelineSweeper',
                      'Inputs': {
                        'TrainingData': '$TrainingData',
                        'TestingData': '$TestingData',
                        'LabelColumns': ['Species'],
                        'StateArguments': {
                            'Name': 'AutoMlState',
                            'Settings': {
                                'Metric': 'AccuracyMicro',
                                'Engine': {
                                    'Name': 'Defaults'
                                },
                                'TerminatorArgs': {
                                    'Name': 'IterationLimited',
                                    'Settings': {
                                        'FinalHistoryLength': 2
                                    }
                                },
                                'TrainerKind': 'SignatureMultiClassClassifierTrainer',
                                'RequestedLearners' : [
                                    'LogisticRegressionClassifier',
                                    'StochasticDualCoordinateAscentClassifier'
                                ]
                            }
                        },
                        'BatchSize': 1
                      },
                      'Outputs': {
                        'State': '$StateOut',
                        'Results': '$ResultsOut'
                      }
                    },
                  ]
                }";

            JObject graphJson = JObject.Parse(inputGraph);
            var     catalog   = ModuleCatalog.CreateInstance(Env);
            var     runner    = new GraphRunner(Env, catalog, graphJson[FieldNames.Nodes] as JArray);
            runner.SetInput("TrainingData", datasetTrain);
            runner.SetInput("TestingData", datasetTest);
            runner.RunAll();

            var autoMlState = runner.GetOutput <AutoInference.AutoMlMlState>("StateOut");
            Assert.NotNull(autoMlState);
            var allPipelines = autoMlState.GetAllEvaluatedPipelines();
            var bestPipeline = autoMlState.GetBestPipeline();
            Assert.Equal(allPipelines.Length, numIterations);

            var bestMicroAccuracyTrain = bestPipeline.PerformanceSummary.TrainingMetricValue;
            var bestMicroAccuracyTest  = bestPipeline.PerformanceSummary.MetricValue;
            Assert.True((0.97 < bestMicroAccuracyTrain) && (bestMicroAccuracyTrain < 0.99));
            Assert.True((0.97 < bestMicroAccuracyTest) && (bestMicroAccuracyTest < 0.99));

            var results = runner.GetOutput <IDataView>("ResultsOut");
            Assert.NotNull(results);
            var rows = PipelinePattern.ExtractResults(Env, results,
                                                      "Graph", "MetricValue", "PipelineId", "TrainingMetricValue", "FirstInput", "PredictorModel");
            Assert.True(rows.Length == numIterations);
            Assert.True(rows.All(r => r.MetricValue > 0.9));
        }
示例#50
0
        protected override void ConfigureModuleCatalog()
        {
            ModuleCatalog catalog = (ModuleCatalog)ModuleCatalog;

            catalog.AddModule(typeof(AppModule));
        }
示例#51
0
 protected override void ConfigureModuleCatalog()
 {
     ModuleCatalog.AddModule(new ModuleInfo(typeof(ModuleA)));
 }
示例#52
0
        /// <summary>
        /// Build a token for component default value. This will look up the component in the catalog, and if it finds an entry, it will
        /// build a JSON structure that would be parsed into the default value.
        ///
        /// This is an inherently fragile setup in case when the factory is not trivial, but it will work well for 'property bag' factories
        /// that we are currently using.
        /// </summary>
        private static JToken BuildComponentToken(IExceptionContext ectx, IComponentFactory value, ModuleCatalog catalog)
        {
            Contracts.AssertValueOrNull(ectx);
            ectx.AssertValue(value);
            ectx.AssertValue(catalog);

            var type = value.GetType();

            ModuleCatalog.ComponentInfo componentInfo;
            if (!catalog.TryFindComponent(type, out componentInfo))
            {
                // The default component is not in the catalog. This is, technically, allowed, but it means that there's no JSON representation
                // for the default value. We will emit the one the won't parse back.
                return(new JValue("(custom component)"));
            }

            ectx.Assert(componentInfo.ArgumentType == type);

            // Try to invoke default ctor for the factory to obtain defaults.
            object defaults;

            try
            {
                defaults = Activator.CreateInstance(type);
            }
            catch (MissingMemberException ex)
            {
                // There was no default constructor found.
                // This should never happen, since ModuleCatalog would error out if there is no default ctor.
                ectx.Assert(false);
                throw ectx.Except(ex, "Couldn't find default constructor");
            }

            var jResult   = new JObject();
            var jSettings = new JObject();

            jResult[FieldNames.Name] = componentInfo.Name;

            // Iterate over all fields of the factory object, and compare the values with the defaults.
            // If the value differs, insert it into the settings object.
            bool anyValue = false;

            foreach (var fieldInfo in type.GetFields())
            {
                var attr = fieldInfo.GetCustomAttributes(typeof(ArgumentAttribute), false).FirstOrDefault()
                           as ArgumentAttribute;
                if (attr == null || attr.Visibility == ArgumentAttribute.VisibilityType.CmdLineOnly)
                {
                    continue;
                }
                ectx.Assert(!fieldInfo.IsStatic && !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral);

                bool   needValue   = false;
                object actualValue = fieldInfo.GetValue(value);
                if (attr.IsRequired)
                {
                    needValue = true;
                }
                else
                {
                    object defaultValue = fieldInfo.GetValue(defaults);
                    needValue = !Equals(actualValue, defaultValue);
                }
                if (!needValue)
                {
                    continue;
                }
                jSettings[attr.Name ?? fieldInfo.Name] = BuildValueToken(ectx, actualValue, fieldInfo.FieldType, catalog);
                anyValue = true;
            }

            if (anyValue)
            {
                jResult[FieldNames.Settings] = jSettings;
            }
            return(jResult);
        }
        public static string GetInputType(ModuleCatalog catalog, Type inputType, GeneratedClasses generatedClasses, string rootNameSpace)
        {
            if (inputType.IsGenericType && inputType.GetGenericTypeDefinition() == typeof(Var <>))
            {
                return($"Var<{GetCSharpTypeName(inputType.GetGenericTypeArgumentsEx()[0])}>");
            }

            if (inputType.IsArray && Var <int> .CheckType(inputType.GetElementType()))
            {
                return($"ArrayVar<{GetCSharpTypeName(inputType.GetElementType())}>");
            }

            if (inputType.IsGenericType && inputType.GetGenericTypeDefinition() == typeof(Dictionary <,>) &&
                inputType.GetGenericTypeArgumentsEx()[0] == typeof(string))
            {
                return($"DictionaryVar<{GetCSharpTypeName(inputType.GetGenericTypeArgumentsEx()[1])}>");
            }

            if (Var <int> .CheckType(inputType))
            {
                return($"Var<{GetCSharpTypeName(inputType)}>");
            }

            var type     = ExtractOptionalOrNullableType(inputType, out bool isNullable, out bool isOptional);
            var typeEnum = TlcModule.GetDataType(type);

            switch (typeEnum)
            {
            case TlcModule.DataKind.Float:
            case TlcModule.DataKind.Int:
            case TlcModule.DataKind.UInt:
            case TlcModule.DataKind.Char:
            case TlcModule.DataKind.String:
            case TlcModule.DataKind.Bool:
            case TlcModule.DataKind.DataView:
            case TlcModule.DataKind.TransformModel:
            case TlcModule.DataKind.PredictorModel:
            case TlcModule.DataKind.FileHandle:
                return(GetCSharpTypeName(inputType));

            case TlcModule.DataKind.Array:
                return(GetInputType(catalog, inputType.GetElementType(), generatedClasses, rootNameSpace) + "[]");

            case TlcModule.DataKind.Component:
                string kind;
                bool   success = catalog.TryGetComponentKind(type, out kind);
                Contracts.Assert(success);
                return($"{kind}");

            case TlcModule.DataKind.Enum:
                var enumName = generatedClasses.GetApiName(type, rootNameSpace);
                if (isNullable)
                {
                    return($"{enumName}?");
                }
                if (isOptional)
                {
                    return($"Optional<{enumName}>");
                }
                return($"{enumName}");

            default:
                if (isNullable)
                {
                    return(generatedClasses.GetApiName(type, rootNameSpace) + "?");
                }
                if (isOptional)
                {
                    return($"Optional<{generatedClasses.GetApiName(type, rootNameSpace)}>");
                }
                return(generatedClasses.GetApiName(type, rootNameSpace));
            }
        }
示例#54
0
        protected override void ConfigureModuleCatalog()
        {
            ModuleCatalog moduleCatalog = new ModuleCatalog();

            //moduleCatalog.AddModule(typeof(CustomersModule));
        }
示例#55
0
 protected override void ConfigureModuleCatalog()
 {
     base.ConfigureModuleCatalog();
     ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
 }
示例#56
0
        private static JArray BuildInputManifest(IExceptionContext ectx, Type inputType, ModuleCatalog catalog)
        {
            Contracts.AssertValueOrNull(ectx);
            ectx.AssertValue(inputType);
            ectx.AssertValue(catalog);

            // Instantiate a value of the input, to pull defaults out of.
            var defaults = Activator.CreateInstance(inputType);

            var inputs = new List <KeyValuePair <Double, JObject> >();

            foreach (var fieldInfo in inputType.GetFields())
            {
                var inputAttr = fieldInfo.GetCustomAttributes(typeof(ArgumentAttribute), false).FirstOrDefault() as ArgumentAttribute;
                if (inputAttr == null || inputAttr.Visibility == ArgumentAttribute.VisibilityType.CmdLineOnly)
                {
                    continue;
                }
                var jo = new JObject();
                jo[FieldNames.Name] = inputAttr.Name ?? fieldInfo.Name;
                jo[FieldNames.Type] = BuildTypeToken(ectx, fieldInfo, fieldInfo.FieldType, catalog);
                jo[FieldNames.Desc] = inputAttr.HelpText;
                if (inputAttr.Aliases != null)
                {
                    jo[FieldNames.Aliases] = new JArray(inputAttr.Aliases);
                }

                jo[FieldNames.Required]   = inputAttr.IsRequired;
                jo[FieldNames.SortOrder]  = inputAttr.SortOrder;
                jo[FieldNames.IsNullable] = fieldInfo.FieldType.IsGenericType && (fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(Nullable <>));

                var defaultValue = fieldInfo.GetValue(defaults);
                var dataType     = TlcModule.GetDataType(fieldInfo.FieldType);
                if (!inputAttr.IsRequired || (dataType != TlcModule.DataKind.Unknown && defaultValue != null))
                {
                    jo[FieldNames.Default] = BuildValueToken(ectx, defaultValue, fieldInfo.FieldType, catalog);
                }

                if (fieldInfo.FieldType.IsGenericType &&
                    fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(Optional <>))
                {
                    var val = fieldInfo.GetValue(defaults) as Optional;
                    if (val == null && !inputAttr.IsRequired)
                    {
                        throw ectx.Except("Field '{0}' is an Optional<> type but is null by default, instead of set to a constructed implicit default.", fieldInfo.Name);
                    }
                    if (val != null && val.IsExplicit)
                    {
                        throw ectx.Except("Field '{0}' is an Optional<> type with a non-implicit default value.", fieldInfo.Name);
                    }
                }

                var rangeAttr = fieldInfo.GetCustomAttributes(typeof(TlcModule.RangeAttribute), false).FirstOrDefault() as TlcModule.RangeAttribute;
                if (rangeAttr != null)
                {
                    if (!TlcModule.IsNumericKind(TlcModule.GetDataType(fieldInfo.FieldType)))
                    {
                        throw ectx.Except("Field '{0}' has a range but is of a non-numeric type.", fieldInfo.Name);
                    }

                    if (!rangeAttr.Type.Equals(fieldInfo.FieldType))
                    {
                        throw ectx.Except("Field '{0}' has a range attribute that uses a type which is not equal to the field's FieldType.", fieldInfo.Name);
                    }

                    var jRange = new JObject();
                    if (rangeAttr.Sup != null)
                    {
                        jRange[FieldNames.Range.Sup] = JToken.FromObject(rangeAttr.Sup);
                    }
                    if (rangeAttr.Inf != null)
                    {
                        jRange[FieldNames.Range.Inf] = JToken.FromObject(rangeAttr.Inf);
                    }
                    if (rangeAttr.Max != null)
                    {
                        jRange[FieldNames.Range.Max] = JToken.FromObject(rangeAttr.Max);
                    }
                    if (rangeAttr.Min != null)
                    {
                        jRange[FieldNames.Range.Min] = JToken.FromObject(rangeAttr.Min);
                    }
                    jo[FieldNames.Range.Type] = jRange;
                }

                // Handle deprecated/obsolete attributes, passing along the message to the manifest.
                if (fieldInfo.GetCustomAttributes(typeof(ObsoleteAttribute), false).FirstOrDefault() is ObsoleteAttribute obsAttr)
                {
                    var jParam = new JObject
                    {
                        [FieldNames.Deprecated.Message] = JToken.FromObject(obsAttr.Message),
                    };
                    jo[FieldNames.Deprecated.ToString()] = jParam;
                }

                if (fieldInfo.GetCustomAttributes(typeof(TlcModule.SweepableLongParamAttribute), false).FirstOrDefault() is TlcModule.SweepableLongParamAttribute slpAttr)
                {
                    var jParam = new JObject
                    {
                        [FieldNames.SweepableLongParam.RangeType] = JToken.FromObject("Long"),
                        [FieldNames.SweepableLongParam.Min]       = JToken.FromObject(slpAttr.Min),
                        [FieldNames.SweepableLongParam.Max]       = JToken.FromObject(slpAttr.Max)
                    };
                    if (slpAttr.StepSize != null)
                    {
                        jParam[FieldNames.SweepableLongParam.StepSize] = JToken.FromObject(slpAttr.StepSize);
                    }
                    if (slpAttr.NumSteps != null)
                    {
                        jParam[FieldNames.SweepableLongParam.NumSteps] = JToken.FromObject(slpAttr.NumSteps);
                    }
                    if (slpAttr.IsLogScale)
                    {
                        jParam[FieldNames.SweepableLongParam.IsLogScale] = JToken.FromObject(true);
                    }
                    jo[FieldNames.SweepableLongParam.ToString()] = jParam;
                }

                if (fieldInfo.GetCustomAttributes(typeof(TlcModule.SweepableFloatParamAttribute), false).FirstOrDefault() is TlcModule.SweepableFloatParamAttribute sfpAttr)
                {
                    var jParam = new JObject
                    {
                        [FieldNames.SweepableFloatParam.RangeType] = JToken.FromObject("Float"),
                        [FieldNames.SweepableFloatParam.Min]       = JToken.FromObject(sfpAttr.Min),
                        [FieldNames.SweepableFloatParam.Max]       = JToken.FromObject(sfpAttr.Max)
                    };
                    if (sfpAttr.StepSize != null)
                    {
                        jParam[FieldNames.SweepableFloatParam.StepSize] = JToken.FromObject(sfpAttr.StepSize);
                    }
                    if (sfpAttr.NumSteps != null)
                    {
                        jParam[FieldNames.SweepableFloatParam.NumSteps] = JToken.FromObject(sfpAttr.NumSteps);
                    }
                    if (sfpAttr.IsLogScale)
                    {
                        jParam[FieldNames.SweepableFloatParam.IsLogScale] = JToken.FromObject(true);
                    }
                    jo[FieldNames.SweepableFloatParam.ToString()] = jParam;
                }

                if (fieldInfo.GetCustomAttributes(typeof(TlcModule.SweepableDiscreteParamAttribute), false).FirstOrDefault() is TlcModule.SweepableDiscreteParamAttribute sdpAttr)
                {
                    var jParam = new JObject
                    {
                        [FieldNames.SweepableDiscreteParam.RangeType] = JToken.FromObject("Discrete"),
                        [FieldNames.SweepableDiscreteParam.Options]   = JToken.FromObject(sdpAttr.Options)
                    };
                    jo[FieldNames.SweepableDiscreteParam.ToString()] = jParam;
                }

                inputs.Add(new KeyValuePair <Double, JObject>(inputAttr.SortOrder, jo));
            }
            return(new JArray(inputs.OrderBy(x => x.Key).Select(x => x.Value).ToArray()));
        }
示例#57
0
        public static JObject BuildEntryPointManifest(IExceptionContext ectx, ModuleCatalog.EntryPointInfo entryPointInfo, ModuleCatalog catalog)
        {
            Contracts.CheckValueOrNull(ectx);
            ectx.CheckValue(entryPointInfo, nameof(entryPointInfo));
            ectx.CheckValue(catalog, nameof(catalog));

            var result = new JObject();

            result[FieldNames.Name]         = entryPointInfo.Name;
            result[FieldNames.Desc]         = entryPointInfo.Description;
            result[FieldNames.FriendlyName] = entryPointInfo.FriendlyName;
            result[FieldNames.ShortName]    = entryPointInfo.ShortName;

            // There supposed to be 2 parameters, env and input.
            result[FieldNames.Inputs]  = BuildInputManifest(ectx, entryPointInfo.InputType, catalog);
            result[FieldNames.Outputs] = BuildOutputManifest(ectx, entryPointInfo.OutputType, catalog);

            if (entryPointInfo.InputKinds != null)
            {
                var jInputKinds = new JArray();
                foreach (var kind in entryPointInfo.InputKinds)
                {
                    jInputKinds.Add(kind.Name);
                }
                result[FieldNames.InputKind] = jInputKinds;
            }

            if (entryPointInfo.OutputKinds != null)
            {
                var jOutputKinds = new JArray();
                foreach (var kind in entryPointInfo.OutputKinds)
                {
                    jOutputKinds.Add(kind.Name);
                }
                result[FieldNames.OutputKind] = jOutputKinds;
            }
            return(result);
        }
        public void EntryPointPipelineSweep()
        {
            // Get datasets
            var       pathData        = GetDataPath("adult.tiny.with-schema.txt");
            var       pathDataTest    = GetDataPath("adult.tiny.with-schema.txt");
            const int numOfSampleRows = 1000;
            int       numIterations   = 4;
            var       inputFileTrain  = new SimpleFileHandle(Env, pathData, false, false);

#pragma warning disable 0618
            var datasetTrain = ImportTextData.ImportText(Env,
                                                         new ImportTextData.Input {
                InputFile = inputFileTrain
            }).Data.Take(numOfSampleRows);
            var inputFileTest = new SimpleFileHandle(Env, pathDataTest, false, false);
            var datasetTest   = ImportTextData.ImportText(Env,
                                                          new ImportTextData.Input {
                InputFile = inputFileTest
            }).Data.Take(numOfSampleRows);
#pragma warning restore 0618
            // Define entrypoint graph
            string inputGraph = @"
                {
                  'Nodes': [                                
                    {
                      'Name': 'Models.PipelineSweeper',
                      'Inputs': {
                        'TrainingData': '$TrainingData',
                        'TestingData': '$TestingData',
                        'StateArguments': {
                            'Name': 'AutoMlState',
                            'Settings': {
                                'Metric': 'Auc',
                                'Engine': {
                                    'Name': 'UniformRandom'
                                },
                                'TerminatorArgs': {
                                    'Name': 'IterationLimited',
                                    'Settings': {
                                        'FinalHistoryLength': 4
                                    }
                                },
                                'TrainerKind': 'SignatureBinaryClassifierTrainer'
                            }
                        },
                        'BatchSize': 2
                      },
                      'Outputs': {
                        'State': '$StateOut',
                        'Results': '$ResultsOut'
                      }
                    },
                  ]
                }";

            JObject graph   = JObject.Parse(inputGraph);
            var     catalog = ModuleCatalog.CreateInstance(Env);

            var runner = new GraphRunner(Env, catalog, graph[FieldNames.Nodes] as JArray);
            runner.SetInput("TrainingData", datasetTrain);
            runner.SetInput("TestingData", datasetTest);
            runner.RunAll();

            var autoMlState = runner.GetOutput <AutoInference.AutoMlMlState>("StateOut");
            Assert.NotNull(autoMlState);
            var allPipelines = autoMlState.GetAllEvaluatedPipelines();
            var bestPipeline = autoMlState.GetBestPipeline();
            Assert.Equal(allPipelines.Length, numIterations);
            Assert.True(bestPipeline.PerformanceSummary.MetricValue > 0.1);

            var results = runner.GetOutput <IDataView>("ResultsOut");
            Assert.NotNull(results);
            var rows = PipelinePattern.ExtractResults(Env, results,
                                                      "Graph", "MetricValue", "PipelineId", "TrainingMetricValue", "FirstInput", "PredictorModel");
            Assert.True(rows.Length == numIterations);
            Assert.True(rows.All(r => r.TrainingMetricValue > 0.1));
        }
示例#59
0
        private static object ParseJsonValue(IExceptionContext ectx, Type type, Attributes attributes, JToken value, ModuleCatalog catalog)
        {
            Contracts.AssertValue(ectx);
            ectx.AssertValue(type);
            ectx.AssertValueOrNull(value);
            ectx.AssertValue(catalog);

            if (value == null)
            {
                return(null);
            }

            if (value is JValue val && val.Value == null)
            {
                return(null);
            }

            if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Optional <>) || type.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                if (type.GetGenericTypeDefinition() == typeof(Optional <>) && value.HasValues)
                {
                    value = value.Values().FirstOrDefault();
                }
                type = type.GetGenericArguments()[0];
            }

            if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Var <>)))
            {
                string varName = value.Value <string>();
                ectx.Check(VariableBinding.IsBindingToken(value), "Variable name expected.");
                var variable   = Activator.CreateInstance(type) as IVarSerializationHelper;
                var varBinding = VariableBinding.Create(ectx, varName);
                variable.VarName = varBinding.VariableName;
                return(variable);
            }

            if (type == typeof(JArray) && value is JArray)
            {
                return(value);
            }

            TlcModule.DataKind dt = TlcModule.GetDataType(type);

            try
            {
                switch (dt)
                {
                case TlcModule.DataKind.Bool:
                    return(value.Value <bool>());

                case TlcModule.DataKind.String:
                    return(value.Value <string>());

                case TlcModule.DataKind.Char:
                    return(value.Value <char>());

                case TlcModule.DataKind.Enum:
                    if (!Enum.IsDefined(type, value.Value <string>()))
                    {
                        throw ectx.Except($"Requested value '{value.Value<string>()}' is not a member of the Enum type '{type.Name}'");
                    }
                    return(Enum.Parse(type, value.Value <string>()));

                case TlcModule.DataKind.Float:
                    if (type == typeof(double))
                    {
                        return(value.Value <double>());
                    }
                    else if (type == typeof(float))
                    {
                        return(value.Value <float>());
                    }
                    else
                    {
                        ectx.Assert(false);
                        throw ectx.ExceptNotSupp();
                    }

                case TlcModule.DataKind.Array:
                    var ja = value as JArray;
                    ectx.Check(ja != null, "Expected array value");
                    Func <IExceptionContext, JArray, Attributes, ModuleCatalog, object> makeArray = MakeArray <int>;
                    return(Utils.MarshalInvoke(makeArray, type.GetElementType(), ectx, ja, attributes, catalog));

                case TlcModule.DataKind.Int:
                    if (type == typeof(long))
                    {
                        return(value.Value <long>());
                    }
                    if (type == typeof(int))
                    {
                        return(value.Value <int>());
                    }
                    ectx.Assert(false);
                    throw ectx.ExceptNotSupp();

                case TlcModule.DataKind.UInt:
                    if (type == typeof(ulong))
                    {
                        return(value.Value <ulong>());
                    }
                    if (type == typeof(uint))
                    {
                        return(value.Value <uint>());
                    }
                    ectx.Assert(false);
                    throw ectx.ExceptNotSupp();

                case TlcModule.DataKind.Dictionary:
                    ectx.Check(value is JObject, "Expected object value");
                    Func <IExceptionContext, JObject, Attributes, ModuleCatalog, object> makeDict = MakeDictionary <int>;
                    return(Utils.MarshalInvoke(makeDict, type.GetGenericArguments()[1], ectx, (JObject)value, attributes, catalog));

                case TlcModule.DataKind.Component:
                    var jo = value as JObject;
                    ectx.Check(jo != null, "Expected object value");
                    // REVIEW: consider accepting strings alone.
                    var jName = jo[FieldNames.Name];
                    ectx.Check(jName != null, "Field '" + FieldNames.Name + "' is required for component.");
                    ectx.Check(jName is JValue, "Expected '" + FieldNames.Name + "' field to be a string.");
                    var name = jName.Value <string>();
                    ectx.Check(jo[FieldNames.Settings] == null || jo[FieldNames.Settings] is JObject,
                               "Expected '" + FieldNames.Settings + "' field to be an object");
                    return(GetComponentJson(ectx, type, name, jo[FieldNames.Settings] as JObject, catalog));

                default:
                    var settings = value as JObject;
                    ectx.Check(settings != null, "Expected object value");
                    var inputBuilder = new InputBuilder(ectx, type, catalog);

                    if (inputBuilder._fields.Length == 0)
                    {
                        throw ectx.Except($"Unsupported input type: {dt}");
                    }

                    if (settings != null)
                    {
                        foreach (var pair in settings)
                        {
                            if (!inputBuilder.TrySetValueJson(pair.Key, pair.Value))
                            {
                                throw ectx.Except($"Unexpected value for component '{type}', field '{pair.Key}': '{pair.Value}'");
                            }
                        }
                    }

                    var missing = inputBuilder.GetMissingValues().ToArray();
                    if (missing.Length > 0)
                    {
                        throw ectx.Except($"The following required inputs were not provided for component '{type}': {string.Join(", ", missing)}");
                    }
                    return(inputBuilder.GetInstance());
                }
            }
            catch (FormatException ex)
            {
                if (ex.IsMarked())
                {
                    throw;
                }
                throw ectx.Except(ex, $"Failed to parse JSON value '{value}' as {type}");
            }
        }
示例#60
0
        private static JToken BuildValueToken(IExceptionContext ectx, object value, Type valueType, ModuleCatalog catalog)
        {
            Contracts.AssertValueOrNull(ectx);
            ectx.AssertValueOrNull(value);
            ectx.AssertValue(valueType);
            ectx.AssertValue(catalog);

            if (value == null)
            {
                return(null);
            }

            // Dive inside Nullable.
            if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                valueType = valueType.GetGenericArguments()[0];
            }

            // Dive inside Optional.
            if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Optional <>))
            {
                valueType = valueType.GetGenericArguments()[0];
                value     = ((Optional)value).GetValue();
            }

            var dataType = TlcModule.GetDataType(valueType);

            switch (dataType)
            {
            case TlcModule.DataKind.Bool:
            case TlcModule.DataKind.Int:
            case TlcModule.DataKind.UInt:
            case TlcModule.DataKind.Float:
            case TlcModule.DataKind.String:
                return(new JValue(value));

            case TlcModule.DataKind.Char:
                return(new JValue(value.ToString()));

            case TlcModule.DataKind.Array:
                var valArray = value as Array;
                var ja       = new JArray();
                foreach (var item in valArray)
                {
                    ja.Add(BuildValueToken(ectx, item, item.GetType(), catalog));
                }
                return(ja);

            case TlcModule.DataKind.Enum:
                return(value.ToString());

            case TlcModule.DataKind.Dictionary:
                // REVIEW: need to figure out how to represent these.
                throw ectx.ExceptNotSupp("Dictionary and component default values are not supported");

            case TlcModule.DataKind.Component:
                var factory = value as IComponentFactory;
                ectx.AssertValue(factory);
                return(BuildComponentToken(ectx, factory, catalog));

            default:
                throw ectx.ExceptNotSupp("Encountered a default value for unsupported type {0}", dataType);
            }
        }