public void Generate(IEnumerable <HelpCommand.Component> infos)
 {
     if (_xmlFilename == null)
     {
         return;
     }
     using (var ch = _host.Start("Generating XML"))
     {
         var content = new XElement("Components",
                                    from c in infos
                                    where !string.IsNullOrWhiteSpace(c.Info.UserName)
                                    select new XElement("Component",
                                                        new XAttribute("Kind", c.Kind),
                                                        new XElement("Name", c.Info.UserName),
                                                        string.IsNullOrWhiteSpace(c.Info.Summary) ? null : new XElement("Summary", c.Info.Summary),
                                                        new XElement("LoadNames",
                                                                     from l in c.Info.LoadNames
                                                                     select new XElement("LoadName", l)),
                                                        new XElement("Type", c.Info.Type.ToString()),
                                                        new XElement("SignatureTypes",
                                                                     from s in c.Info.SignatureTypes
                                                                     select new XElement("SignatureType", s.ToString())),
                                                        c.Args == null
                     ? null
                     : new XElement("Arguments",
                                    from a in CmdParser.GetArgInfo(c.Args.GetType(), c.Args).Args
                                    select new XElement("Argument",
                                                        new XElement("LongName", a.LongName),
                                                        a.ShortNames == null
                                 ? null
                                 : new XElement("ShortNames",
                                                from sn in a.ShortNames
                                                select new XElement("ShortName", sn)),
                                                        new XElement("HelpText", a.HelpText),
                                                        CreateDefaultValueElement(ch, c.Kind, a)))));
         File.WriteAllText(_xmlFilename, content.ToString());
     }
 }
Beispiel #2
0
 protected override void GenerateImplBody(IndentedTextWriter w, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         if (component.ArgType == null)
         {
             var call = GenerateCall(component);
             w.WriteLine("return {0}(env, data);", call);
         }
         else
         {
             w.WriteLine("var args = new {0}();", GetCSharpTypeName(component.ArgType));
             var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
             foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
             {
                 GenerateImplBody(w, arg, "");
             }
             var call = GenerateCall(component);
             w.WriteLine("return {0}(args, env, data);", call);
         }
     }
     w.WriteLine("}");
 }
Beispiel #3
0
        protected override void GenerateContent(IndentedTextWriter writer, string prefix,
                                                ComponentCatalog.LoadableClassInfo component, string moduleId)
        {
            writer.WriteLine("[Module(");
            _compName = prefix + component.LoadNames[0];
            var name = Name ?? PrettyPrintDisplayName(component.LoadNames[0]);

            using (writer.Nest())
            {
                writer.WriteLine("Name = \"{0}\",", name);
                writer.WriteLine("FamilyId = \"{0}\",", moduleId);
                writer.WriteLine("Owner = \"{0}\",", Owner);
                writer.WriteLine("ReleaseVersion = \"{0}\",", Version);
                writer.WriteLine("State = ModuleState.{0},", State);
                writer.WriteLine("Type = ModuleType.{0},", ModuleType);
                writer.WriteLine("Determinism = Determinism.{0},", Determinism);
                writer.WriteLine("Category = @\"{0}\")]", Category);
            }
            writer.WriteLine("[Obsolete]");
            writer.WriteLine("public static IModule Create{0}(", _compName);
            using (writer.Nest())
            {
                writer.WriteLine("[Help(Display = @\"Dataset\", ToolTip = @\"Input dataset\")]");
                writer.WriteLine("[ModuleInputPort]");
                writer.WriteLine("IDataView idataset,");
                var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                {
                    GenerateMethodSignature(writer, arg, null, null, null, "");
                }
                writer.WriteLine("[Help(Display = @\"Results dataset\", ToolTip = @\"Transformed dataset\")]");
                writer.WriteLine("[ModuleOutputPort]");
                writer.WriteLine("IDataView odataset,");
                writer.WriteLine("[Help(Display = @\"{0}\", ToolTip = @\"{0}\")]", name);
                writer.WriteLine("[ModuleOutputPort]");
                writer.WriteLine("DataTransform otransform,");
                writer.WriteLine("[Context]");
                writer.WriteLine("IContext context)");
            }
            writer.WriteLine("{");
            using (writer.Nest())
            {
                writer.WriteLine("var instance = new {0}Module();", _compName);
                writer.WriteLine();
                writer.WriteLine("var ports = new Dictionary<string, object> { { \"idataset\", idataset } };");
                writer.WriteLine("var parameters = new Dictionary<string, object>");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                    foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                    {
                        GenerateDictionaryEntry(writer, arg, "");
                    }
                }
                writer.WriteLine("};");
                writer.WriteLine();
                writer.WriteLine("instance.Context = context;");
                writer.WriteLine("instance.SetInputPorts(ports);");
                writer.WriteLine("instance.SetParameters(parameters);");
                writer.WriteLine();
                writer.WriteLine("return instance;");
            }
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("[Obsolete]");
            writer.WriteLine("public class {0}Module : ModuleBase", _compName);
            writer.WriteLine("{");
            using (writer.Nest())
            {
                writer.WriteLine("private Dictionary<string, object> parameters;");
                writer.WriteLine("private Dictionary<string, object> ports;");
                writer.WriteLine();
                writer.WriteLine("public override Dictionary<string, object> Run()");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var view = ConstructTransform((IDataView)ports[\"idataset\"]);");
                    writer.WriteLine("return new Dictionary<string, object> { { \"odataset\", view }, { \"otransform\", new DataTransform(view) } };");
                }
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override void SetParameters(Dictionary<string, object> parameters)");
                writer.WriteLine("{");
                using (writer.Nest())
                    writer.WriteLine("this.parameters = parameters;");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override void SetInputPorts(Dictionary<string, object> ports)");
                writer.WriteLine("{");
                using (writer.Nest())
                    writer.WriteLine("this.ports = ports;");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override Dictionary<string, object> ComputeSchema(Dictionary<string, object> inputports)");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var view = ConstructTransform((IDataView)inputports[\"idataset\"]);");
                    writer.WriteLine("return new Dictionary<string, object> { { \"odataset\", view.Schema } };");
                }
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("private IDataView ConstructTransform(IDataView input)");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var builder = new {0}();", _compName);
                    var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                    foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                    {
                        GenerateImplCall(writer, arg, null, null, null, "");
                    }
                    writer.WriteLine("return builder.Create{0}Impl(Host, input);", _compName);
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");
        }
        /// <summary>
        /// Returns all kinds of models.
        /// </summary>
        public static IEnumerable <ComponentDescription> EnumerateComponents(string kind)
        {
            if (kind == "argument")
            {
                foreach (var comp in EnumerateComponentsParameter(false))
                {
                    yield return(comp);
                }
            }
            else if (kind == "command")
            {
                foreach (var comp in EnumerateComponentsParameter(true))
                {
                    yield return(comp);
                }
            }
            else
            {
                var kinds = GetAllKinds();
                if (!string.IsNullOrEmpty(kind) && !kinds.Where(c => c == kind).Any())
                {
                    throw new ArgumentException($"Unable to find kind '{kind}' in\n{string.Join("\n", kinds)}.");
                }

                using (var env = new ConsoleEnvironment())
                {
                    ComponentHelper.AddStandardComponents(env);
                    var    sigs    = env.ComponentCatalog.GetAllSignatureTypes();
                    var    typeRes = typeof(object);
                    Type[] typeSigs;
                    if (string.IsNullOrEmpty(kind))
                    {
                        typeSigs = sigs.ToArray();
                    }
                    else
                    {
                        typeSigs = new[] { sigs.FirstOrDefault(t => ComponentCatalog.SignatureToString(t).ToLowerInvariant() == kind) }
                    };
                    foreach (var typeSig in typeSigs)
                    {
                        var infos = env.ComponentCatalog.GetAllDerivedClasses(typeRes, typeSig)
                                    .Where(x => !x.IsHidden)
                                    .OrderBy(x => x.LoadNames[0].ToLowerInvariant());
                        foreach (var info in infos)
                        {
                            var args = info.CreateArguments();
                            if (args == null)
                            {
                                yield return(new ComponentDescription(info, args, null, null));
                            }
                            else
                            {
                                var asse = args.GetType().Assembly;

                                var parsedArgs = CmdParser.GetArgInfo(args.GetType(), args).Args;
                                var arguments  = new List <ComponentDescription.Argument>();
                                foreach (var arg in parsedArgs)
                                {
                                    var a = new ComponentDescription.Argument()
                                    {
                                        Name         = arg.LongName,
                                        ShortName    = arg.ShortNames == null || !arg.ShortNames.Any() ? null : arg.ShortNames.First(),
                                        DefaultValue = arg.DefaultValue == null ? null : arg.DefaultValue.ToString(),
                                        Help         = arg.HelpText,
                                        Arg          = arg,
                                    };
                                    arguments.Add(a);
                                }

                                var cmp = new ComponentDescription(info, args, asse, arguments);
                                yield return(cmp);
                            }
                        }
                    }
                }
            }
        }

        #endregion
    }
        protected void GenerateImplBody(IndentingTextWriter w, CmdParser.ArgInfo.Arg arg, string argSuffix)
        {
            if (Exclude.Contains(arg.LongName))
            {
                return;
            }

            if (arg.IsSubComponentItemType)
            {
                // We need to create a tree with all the subcomponents, unless the subcomponent is a Trainer.
                Contracts.Assert(arg.ItemType.GetGenericTypeDefinition() == typeof(SubComponent <,>));
                var types    = arg.ItemType.GetGenericArguments();
                var baseType = types[0];
                var sigType  = types[1];
                if (IsTrainer(sigType))
                {
                    if (arg.IsCollection)
                    {
                        w.WriteLine("args{0}.{1} = new[] {{ new SubComponent<{2}, {3}>({4}.Item1, {4}.Item2) }};",
                                    argSuffix, arg.LongName, GetCSharpTypeName(baseType), GetCSharpTypeName(sigType),
                                    arg.LongName + argSuffix);
                    }
                    else
                    {
                        w.WriteLine("args{0}.{1} = new SubComponent<{2}, {3}>({4}.Item1, {4}.Item2);", argSuffix,
                                    arg.LongName, GetCSharpTypeName(baseType), GetCSharpTypeName(sigType),
                                    arg.LongName + argSuffix);
                    }
                    return;
                }
                if (sigType == typeof(SignatureDataLoader))
                {
                    return;
                }
                var typeName = EnumName(arg, sigType);
                w.WriteLine("switch ({0})", arg.LongName + argSuffix);
                w.WriteLine("{");
                using (w.Nest())
                {
                    if (arg.NullName != null)
                    {
                        w.WriteLine("case {0}.None:", typeName);
                        using (w.Nest())
                        {
                            w.WriteLine("args{0}.{1} = null;", argSuffix, arg.LongName);
                            w.WriteLine("break;");
                        }
                    }
                    var infos = ComponentCatalog.GetAllDerivedClasses(baseType, sigType);
                    foreach (var info in infos)
                    {
                        w.WriteLine("case {0}.{1}:", typeName, info.LoadNames[0]);
                        using (w.Nest())
                        {
                            if (info.ArgType != null)
                            {
                                var newArgSuffix = argSuffix + info.LoadNames[0];
                                w.WriteLine("var args{0} = new {1}();", newArgSuffix, GetCSharpTypeName(info.ArgType));
                                w.WriteLine("var defs{0} = new {1}();", newArgSuffix, GetCSharpTypeName(info.ArgType));
                                var args = info.CreateArguments();
                                if (args != null)
                                {
                                    var argInfo = CmdParser.GetArgInfo(args.GetType(), args);
                                    foreach (var a in argInfo.Args)
                                    {
                                        GenerateImplBody(w, a, newArgSuffix);
                                    }
                                }
                                w.WriteLine(
                                    "args{0}.{1} = new {2}(\"{3}\", CmdParser.GetSettings(args{4}, defs{4}));",
                                    argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType), info.LoadNames[0],
                                    newArgSuffix);
                            }
                            else
                            {
                                w.WriteLine("args{0}.{1} = new {2}(\"{3}\");", argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType), info.LoadNames[0]);
                            }
                            w.WriteLine("break;");
                        }
                    }
                }
                w.WriteLine("}");
            }
            else if (arg.IsCollection)
            {
                if (IsColumnType(arg))
                {
                    w.WriteLine("args{0}.{1} = {1}.Select({2}.Parse).ToArray();", argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType));
                }
                else if (IsStringColumnType(arg))
                {
                    w.WriteLine("args{0}.{1} = {2};", argSuffix, arg.LongName, arg.LongName + argSuffix);
                }
                else
                {
                    w.WriteLine("args{0}.{1} = new[] {{ {2} }};", argSuffix, arg.LongName, arg.LongName + argSuffix);
                }
            }
            else
            {
                w.WriteLine("args{0}.{1} = {2};", argSuffix, arg.LongName, arg.LongName + argSuffix);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Generate enums for subcomponents. Uses ReflectionUtils to filter only the subcomponents that match the base type and the signature.
        /// </summary>
        /// <param name="w"></param>
        /// <param name="arg"></param>
        /// <param name="seen"></param>
        protected void GenerateEnums(IndentingTextWriter w, CmdParser.ArgInfo.Arg arg, HashSet <Tuple <Type, Type> > seen)
        {
            if (Exclude.Contains(arg.LongName))
            {
                return;
            }

            var moreEnums = new List <CmdParser.ArgInfo.Arg>();

            if (arg.IsHidden || !arg.IsSubComponentItemType)
            {
                return;
            }
            Contracts.Assert(arg.ItemType.GetGenericTypeDefinition() == typeof(SubComponent <,>));
            var types    = arg.ItemType.GetGenericArguments();
            var baseType = types[0];
            var sigType  = types[1];
            var key      = new Tuple <Type, Type>(baseType, sigType);

            if (seen.Contains(key) || IsTrainer(sigType) || sigType == typeof(SignatureDataLoader))
            {
                return;
            }
            seen.Add(key);
            var typeName = EnumName(arg, sigType);

            w.WriteLine("/// <summary> Available choices for {0} </summary>", sigType);
            w.WriteLine("public enum {0}", typeName);
            w.Write("{");
            using (w.Nest())
            {
                var pre = "";
                if (arg.NullName != null)
                {
                    w.WriteLine();
                    GenerateEnumValue(w, null);
                    pre = ",";
                }
                var infos = ComponentCatalog.GetAllDerivedClasses(baseType, sigType);
                foreach (var info in infos)
                {
                    w.WriteLine(pre);
                    if (pre != "")
                    {
                        w.WriteLine();
                    }
                    pre = ",";
                    GenerateEnumValue(w, info);
                    var args = info.CreateArguments();
                    if (args == null)
                    {
                        continue;
                    }
                    var argInfo = CmdParser.GetArgInfo(args.GetType(), args);
                    moreEnums.AddRange(argInfo.Args);
                }
                w.WriteLine();
            }
            w.WriteLine("}");
            w.WriteLine();
            foreach (var argument in moreEnums)
            {
                GenerateEnums(w, argument, seen);
            }
        }