Ejemplo n.º 1
1
        public object ToJsonR(IList items, Style style = Style.Full)
        {
            var jsonR = new JsonR();

            Type itemType = GetListItemType(items.GetType());
            jsonR.Type    = itemType.Name;

            SplitKeyValues(items, jsonR.Keys, jsonR.Values);
            switch (style)
            {
                case Style.Keys:
                    return jsonR.Keys;

                case Style.Values:
                    return jsonR.Values;

                case Style.Type:
                    return jsonR.Type;

                case Style.Hint:
                    return new {jsonR.Type, jsonR.Values };
            }

            return jsonR;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write the summary report to the specified writer.
        /// </summary>
        /// <param name="storage">The data store to query</param>
        /// <param name="simulationName">The simulation name to produce a summary report for</param>
        /// <param name="writer">Text writer to write to</param>
        /// <param name="apsimSummaryImageFileName">The file name for the logo. Can be null</param>
        /// <param name="outtype">Indicates the format to be produced</param>
        /// <param name="darkTheme">Whether or not the dark theme should be used.</param>
        public static void WriteReport(
            IDataStore storage,
            string simulationName,
            TextWriter writer,
            string apsimSummaryImageFileName,
            OutputType outtype,
            bool darkTheme)
        {
            Document            document = null;
            RtfDocumentRenderer renderer = null;

            if (outtype == OutputType.html)
            {
                writer.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                writer.WriteLine("<html>");
                writer.WriteLine("<head>");
                writer.WriteLine("<meta content='text/html; charset=UTF-8; http-equiv='content-type'>");
                writer.WriteLine("<style>");
                if (darkTheme)
                {
                    writer.WriteLine("h2 { color:white; } ");
                    writer.WriteLine("h3 { color:white; } ");
                    writer.WriteLine("table { border:1px solid white; }");
                }
                else
                {
                    writer.WriteLine("h2 { color:darkblue; } ");
                    writer.WriteLine("h3 { color:darkblue; } ");
                    writer.WriteLine("table { border:1px solid black; }");
                    writer.WriteLine("th { background-color: palegoldenrod}");
                    writer.WriteLine("tr.total { color:darkorange; }");
                }
                writer.WriteLine("table { border-collapse:collapse; width:100%; table-layout:fixed; text-align:left; }");
                writer.WriteLine("table.headered {text-align:right; }");
                writer.WriteLine("tr.total { font-weight:bold; }");
                writer.WriteLine("table.headered td.col1 { text-align:left; font-weight:bold; }");
                writer.WriteLine("td { border:1px solid; }");
                writer.WriteLine("th { border:1px solid; text-align:right; }");
                writer.WriteLine("th.col1 { text-align:left; }");
                writer.WriteLine("</style>");
                writer.WriteLine("</head>");
                writer.WriteLine("<body>");
                writer.WriteLine("<a href=\"#log\">Simulation log</a>");
            }
            else if (outtype == OutputType.rtf)
            {
                document = new Document();
                renderer = new RtfDocumentRenderer();

                // Get the predefined style Normal.
                Style style = document.Styles["Normal"];

                // Because all styles are derived from Normal, the next line changes the
                // font of the whole document. Or, more exactly, it changes the font of
                // all styles and paragraphs that do not redefine the font.
                style.Font.Name = "Arial";

                // Heading1 to Heading9 are predefined styles with an outline level. An outline level
                // other than OutlineLevel.BodyText automatically creates the outline (or bookmarks)
                // in PDF.
                style            = document.Styles["Heading2"];
                style.Font.Size  = 14;
                style.Font.Bold  = true;
                style.Font.Color = Colors.DarkBlue;
                style.ParagraphFormat.PageBreakBefore = false;
                style.ParagraphFormat.SpaceAfter      = 3;
                style.ParagraphFormat.SpaceBefore     = 16;

                style            = document.Styles["Heading3"];
                style.Font.Size  = 12;
                style.Font.Bold  = true;
                style.Font.Color = Colors.DarkBlue;
                style.ParagraphFormat.SpaceBefore = 10;
                style.ParagraphFormat.SpaceAfter  = 2;

                // Create a new style called Monospace based on style Normal
                style = document.Styles.AddStyle("Monospace", "Normal");
                System.Drawing.FontFamily monoFamily = new System.Drawing.FontFamily(System.Drawing.Text.GenericFontFamilies.Monospace);
                style.Font.Name = monoFamily.Name;
                Section section = document.AddSection();
            }

            // Get the initial conditions table.
            DataTable initialConditionsTable = storage.Reader.GetData(simulationName: simulationName, tableName: "_InitialConditions");

            if (initialConditionsTable != null)
            {
                // Convert the '_InitialConditions' table in the DataStore to a series of
                // DataTables for each model.
                List <DataTable> tables = new List <DataTable>();
                ConvertInitialConditionsToTables(initialConditionsTable, tables);

                // Now write all tables to our report.
                for (int i = 0; i < tables.Count; i += 2)
                {
                    // Only write something to the summary file if we have something to write.
                    if (tables[i].Rows.Count > 0 || tables[i + 1].Rows.Count > 0)
                    {
                        string heading = tables[i].TableName;
                        WriteHeading(writer, heading, outtype, document);

                        // Write the manager script.
                        if (tables[i].Rows.Count == 1 && tables[i].Rows[0][0].ToString() == "Script code: ")
                        {
                            WriteScript(writer, tables[i].Rows[0], outtype, document);
                        }
                        else
                        {
                            // Write the properties table if we have any properties.
                            if (tables[i].Rows.Count > 0)
                            {
                                WriteTable(writer, tables[i], outtype, "PropertyTable", document);
                            }

                            // Write the general data table if we have any data.
                            if (tables[i + 1].Rows.Count > 0)
                            {
                                WriteTable(writer, tables[i + 1], outtype, "ApsimTable", document);
                            }
                        }

                        if (outtype == OutputType.html)
                        {
                            writer.WriteLine("<br/>");
                        }
                    }
                }
            }

            // Write out all messages.
            WriteHeading(writer, "Simulation log:", outtype, document, "log");
            DataTable messageTable = GetMessageTable(storage, simulationName);

            WriteMessageTable(writer, messageTable, outtype, false, "MessageTable", document);

            if (outtype == OutputType.html)
            {
                writer.WriteLine("</body>");
                writer.WriteLine("</html>");
            }
            else if (outtype == OutputType.rtf)
            {
                string rtf = renderer.RenderToString(document, Path.GetTempPath());
                writer.Write(rtf);
            }
        }