コード例 #1
0
ファイル: Table.cs プロジェクト: asavinov/dce-csharp
        public void Populate() 
        {
            if (DefinitionType == TableDefinitionType.FREE)
            {
                return; // Nothing to do
            }

            Length = 0;

            if (DefinitionType == TableDefinitionType.PROJECTION) // There are import dimensions so copy data from another set (projection of another set)
            {
                List<DcColumn> inColumns = InputColumns.Where(d => d.GetData().IsAppendData).ToList();

                foreach(DcColumn inColumn in inColumns)
                {
                    inColumn.GetData().Translate();
                    inColumn.GetData().Evaluate(); // Delegate to column evaluation - it will add records from column expression
                }
            }
            else if (DefinitionType == TableDefinitionType.PRODUCT) // Product of local sets (no project/de-project from another set)
            {
                // Input variable for where formula
                DcVariable thisVariable = new Variable(this.Schema.Name, this.Name, "this");
                thisVariable.TypeSchema = this.Schema;
                thisVariable.TypeTable = this;

                // Evaluator expression for where formula
                ExprNode outputExpr = this.WhereExpr;
                if(outputExpr != null)
                {
                    outputExpr.OutputVariable.SchemaName = this.Schema.Name;
                    outputExpr.OutputVariable.TypeName = "Boolean";
                    outputExpr.OutputVariable.TypeSchema = this.Schema;
                    outputExpr.OutputVariable.TypeTable = this.Schema.GetPrimitiveType("Boolean");
                    outputExpr.Resolve(this.Space, new List<DcVariable>() { thisVariable });

                    outputExpr.EvaluateBegin();
                }

                DcTableWriter tableWriter = this.GetTableWriter();
                tableWriter.Open();

                //
                // Find all local greater dimensions to be varied (including the super-dim)
                //
                DcColumn[] cols = Columns.Where(x => x.IsKey && !x.IsPrimitive).ToArray();
                int colCount = cols.Length; // Dimensionality - how many free dimensions
                object[] vals = new object[colCount]; // A record with values for each free dimension being varied

                // Prepare columns
                foreach (DcColumn col in cols)
                {
                    col.GetData().AutoIndex = true; // We need true to correctly evaluate where condition and correctly remove last element if it is wrong
                    //col.GetData().Nullify();
                }

                //
                // The current state of the search procedure
                //
                Rowid[] lengths = new Rowid[colCount]; // Size of each dimension being varied (how many offsets in each dimension)
                for (int i = 0; i < colCount; i++) lengths[i] = cols[i].Output.GetData().Length;

                Rowid[] offsets = new Rowid[colCount]; // The current point/offset for each dimensions during search
                for (int i = 0; i < colCount; i++) offsets[i] = -1;

                int top = -1; // The current level/top where we change the offset. Depth of recursion.
                do ++top; while (top < colCount && lengths[top] == 0);

                // Alternative recursive iteration: http://stackoverflow.com/questions/13655299/c-sharp-most-efficient-way-to-iterate-through-multiple-arrays-list
                while (top >= 0)
                {
                    if (top == colCount) // New element is ready. Process it.
                    {
                        // Initialize a record and append it
                        for (int i = 0; i < colCount; i++)
                        {
                            vals[i] = offsets[i];
                        }
                        Rowid input = tableWriter.Append(cols, vals);

                        //
                        // Now check if this appended element satisfies the where expression and if not then remove it
                        //
                        if (outputExpr != null)
                        {
                            // Set 'this' variable to the last elements (that has been just appended) which will be read by the expression
                            thisVariable.SetValue(this.GetData().Length - 1);

                            // Evaluate expression
                            outputExpr.Evaluate();

                            bool satisfies = (bool)outputExpr.OutputVariable.GetValue();

                            if (!satisfies)
                            {
                                Length = Length - 1; // Remove elements
                            }
                        }

                        top--;
                        while (top >= 0 && lengths[top] == 0) // Go up by skipping empty dimensions and reseting 
                        { offsets[top--] = -1; }
                    }
                    else
                    {
                        // Find the next valid offset
                        offsets[top]++;

                        if (offsets[top] < lengths[top]) // Offset chosen
                        {
                            do ++top;
                            while (top < colCount && lengths[top] == 0); // Go up (forward) by skipping empty dimensions
                        }
                        else // Level is finished. Go back.
                        {
                            do { offsets[top--] = -1; }
                            while (top >= 0 && lengths[top] == 0); // Go down (backward) by skipping empty dimensions and reseting 
                        }
                    }
                }

                // Prepare columns
                foreach (DcColumn col in cols)
                {
                    col.GetData().Reindex();
                    col.GetData().AutoIndex = true;
                }

                if (tableWriter != null)
                {
                    tableWriter.Close();
                }
                if (outputExpr != null)
                {
                    outputExpr.EvaluateEnd();
                }
            }
            else
            {
                throw new NotImplementedException("This table definition type is not implemented and cannot be populated.");
            }

        }
コード例 #2
0
ファイル: CoreTest.cs プロジェクト: asavinov/dce-csharp
        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);
        }