public void CsvWriteTest() // Store schema and data to a CSV file as a result of evaluation { DcSpace space = new Space(); DcSchema schema = space.CreateSchema("My Schema", DcSchemaKind.Dc); CoreTest.CreateSampleSchema(schema); CoreTest.CreateSampleData(schema); DcTable t2 = schema.GetSubTable("Table 2"); DcColumn c21 = t2.GetColumn("Column 21"); DcColumn c22 = t2.GetColumn("Column 22"); DcColumn c23 = t2.GetColumn("Column 23"); // // Create schema for a remote db // SchemaCsv top = (SchemaCsv)space.CreateSchema("My Files", DcSchemaKind.Csv); // Create a remote file description TableCsv table = (TableCsv)space.CreateTable(DcSchemaKind.Csv, "Table_1", top.Root); table.FilePath = CsvWrite; // Manually create column to be imported (we need an automatic mechanism for appending missing columns specified in the formula) DcColumn p1 = space.CreateColumn("Column 11", table, top.GetPrimitiveType("String"), true); DcColumn p2 = space.CreateColumn("Column 12", table, top.GetPrimitiveType("String"), true); DcColumn p3 = space.CreateColumn("Custom Column 13", table, top.GetPrimitiveType("String"), true); DcColumn p4 = space.CreateColumn("Constant Column", table, top.GetPrimitiveType("String"), true); // Define export column DcColumn col = space.CreateColumn("Export", schema.GetSubTable("Table 1"), table, false); col.GetData().IsAppendData = true; col.GetData().Formula = "(( [String] [Column 11] = this.[Column 11], [String] [Column 12] = [Column 12], [String] [Custom Column 13] = [Column 13], [String] [Constant Column] = 20.02 ))"; // Tuple structure corresponds to output table col.GetData().IsAppendData = true; col.GetData().IsAppendSchema = true; table.Populate(); }
public void Resolve(DcSpace space) { if (!string.IsNullOrEmpty(SchemaName)) { // 1. Resolve schema name TypeSchema = space.GetSchema(SchemaName); if (TypeSchema == null) { return; // Cannot resolve } // 2. Resolve table name TypeTable = TypeSchema.GetSubTable(TypeName); if (TypeTable == null) { return; // Cannot resolve } } else if (!string.IsNullOrEmpty(TypeName)) // No schema name (imcomplete info) { // 1. try to find the table in the mashup DcSchema mashup = space.GetSchemas().FirstOrDefault(x => x.GetSchemaKind() == DcSchemaKind.Dc); if (mashup != null) { TypeTable = mashup.GetSubTable(TypeName); if (TypeTable != null) { TypeSchema = mashup; SchemaName = TypeSchema.Name; // We also reconstruct the name return; } } // 2. try to find the table in any other schema foreach (DcSchema schema in space.GetSchemas()) { TypeTable = schema.GetSubTable(TypeName); if (TypeTable != null) { TypeSchema = schema; SchemaName = TypeSchema.Name; // We also reconstruct the name return; } } } }
public void JsonReadTest() // Serialize/deserialize schema elements { DcSpace ds = new Space(); DcSchema schema = ds.CreateSchema("My Schema", DcSchemaKind.Dc); CoreTest.CreateSampleSchema(schema); // Add table definition DcTable t = schema.GetSubTable("Table 2"); t.GetData().WhereFormula = "[Column 22] > 20.0 && this.Super.[Column 23] < 50"; // Add column definition DcColumn c = t.GetColumn("Column 22"); c.GetData().Formula = "([Column 11]+10.0) * this.[Column 13]"; JObject space = Utils.CreateJsonFromObject(ds); ds.ToJson(space); // Serialize into json string string jsonDs = JsonConvert.SerializeObject(space, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings { }); // De-serialize from json string: http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing dynamic objDs = JsonConvert.DeserializeObject(jsonDs); //dynamic obj = JObject/JValue/JArray.Parse(json); // // Instantiate and initialize // ds = (Space)Utils.CreateObjectFromJson(objDs); ((Space)ds).FromJson(objDs, ds); Assert.AreEqual(5, ds.GetSchemas()[0].GetSubTable("Table 1").Columns.Count); Assert.AreEqual(5, ds.GetSchemas()[0].GetSubTable("Table 2").Columns.Count); Assert.AreEqual("Table 1", ds.GetSchemas()[0].GetSubTable("Table 2").GetColumn("Table 1").Output.Name); c = t.GetColumn("Column 22"); //Assert.AreEqual(DcColumnDefinitionType.ARITHMETIC, c.Definition.FormulaExpr.DefinitionType); //Assert.AreEqual(2, c.GetData().FormulaExpr.Children.Count); // // 2. Another sample schema with several schemas and inter-schema columns // string jsonDs2 = @"{ 'type': 'Space', 'schemas': [ { 'type': 'Schema', 'name': 'My Schema', 'tables': [ { 'type': 'Set', 'name': 'My Table' } ], 'columns': [ { 'type': 'Dim', 'name': 'My Column', 'lesser_table': {schema_name:'My Schema', table_name:'My Table'}, 'greater_table': {schema_name:'My Schema', table_name:'Double'} }, { 'type': 'Dim', 'name': 'Import Column', 'lesser_table': {schema_name: 'Rel Schema', table_name:'Rel Table'}, 'greater_table': {schema_name: 'My Schema', table_name: 'My Table'} } ] }, { 'type': 'SchemaCsv', 'name': 'Rel Schema', 'tables': [ { 'type': 'SetRel', 'name': 'Rel Table' } ], 'columns': [ { 'type': 'DimRel', 'name': 'My Column', 'lesser_table': {schema_name:'Rel Schema', table_name:'Rel Table'}, 'greater_table': {schema_name:'Rel Schema', table_name:'String'} }, ] } ] }"; /* * dynamic objWs2 = JsonConvert.DeserializeObject(jsonWs2); * * Workspace ws2 = Utils.CreateObjectFromJson(objWs2); * ws2.FromJson(objWs2, ws2); * * Assert.AreEqual(2, ws2.Schemas.Count); * Assert.AreEqual("My Schema", ws2.Mashup.Name); * Assert.AreEqual("My Table", ws2.Schemas[1].FindTable("Rel Table").GetGreaterDim("Import Column").Output.Name); */ }
public static void CreateSampleData(DcSchema schema) { // // Fill sample data in "Table 1" // DcTable t1 = schema.GetSubTable("Table 1"); DcColumn c11 = t1.GetColumn("Column 11"); DcColumn c12 = t1.GetColumn("Column 12"); DcColumn c13 = t1.GetColumn("Column 13"); DcColumn c14 = t1.GetColumn("Column 14"); DcColumn[] cols = new DcColumn[] { c11, c12, c13, c14 }; object[] vals = new object[4]; DcTableWriter w1 = t1.GetData().GetTableWriter(); vals[0] = 20; vals[1] = "Record 0"; vals[2] = 20.0; vals[3] = 20.0; w1.Append(cols, vals); vals[0] = 10; vals[1] = "Record 1"; vals[2] = 10.0; vals[3] = 20.0; w1.Append(cols, vals); vals[0] = 30; vals[1] = "Record 2"; vals[2] = 30.0; vals[3] = 20.0; w1.Append(cols, vals); // // Fill sample data in "Table 2" // DcTable t2 = schema.GetSubTable("Table 2"); DcColumn c21 = t2.GetColumn("Column 21"); DcColumn c22 = t2.GetColumn("Column 22"); DcColumn c23 = t2.GetColumn("Column 23"); DcColumn c24 = t2.GetColumn("Table 1"); cols = new DcColumn[] { c21, c22, c23, c24 }; vals = new object[4]; DcTableWriter w2 = t2.GetData().GetTableWriter(); vals[0] = "Value A"; vals[1] = 20; vals[2] = 40.0; vals[3] = 0; w2.Append(cols, vals); vals[0] = "Value A"; vals[1] = 30; vals[2] = 40.0; vals[3] = 1; w2.Append(cols, vals); vals[0] = "Value A"; vals[1] = 30; vals[2] = 50.0; vals[3] = 1; w2.Append(cols, vals); vals[0] = "Value B"; vals[1] = 30; vals[2] = 50.0; vals[3] = 1; w2.Append(cols, vals); }
public static object ResolveJsonRef(JObject json, DcSpace ws) // Resolve a json reference to a real object { if (json == null) { return(null); } string element_type = (string)json["element_type"]; if (element_type == null) { if (json["schema_name"] == null) { return(null); } else if (json["table_name"] == null) { element_type = "schema"; } else if (json["column_name"] == null) { element_type = "table"; } else { element_type = "column"; } } if (element_type == "schema") // Find schema { return(ws.GetSchema((string)json["schema_name"])); } else if (element_type == "table") // Find table { DcSchema schema = ws.GetSchema((string)json["schema_name"]); if (schema == null) { return(null); } return(schema.GetSubTable((string)json["table_name"])); } else if (element_type == "column") // Find column { DcSchema schema = ws.GetSchema((string)json["schema_name"]); if (schema == null) { return(null); } DcTable table = schema.GetSubTable((string)json["table_name"]); if (table == null) { return(null); } return(table.GetColumn((string)json["column_name"])); } else { throw new NotImplementedException("Unknown element type in the reference."); } }