示例#1
0
        private void WriteResultToAnswerTable(int worldNo, DataTable content, double probability)
        {
            DataTable answerTable = new DataTable();

            answerTable.Columns.Add(new DataColumn("worldNo", typeof(int)));
            var attCols  = content.Columns;
            var colNames = new List <string>();

            foreach (DataColumn col in attCols)
            {
                answerTable.Columns.Add(col.ColumnName, typeof(string));
                colNames.Add(col.ColumnName);
            }
            answerTable.Columns.Add(new DataColumn("p", typeof(double)));

            var distinctRows = content.DefaultView.ToTable(true, colNames.ToArray());

            foreach (DataRow row in distinctRows.Rows)
            {
                var newRow = answerTable.NewRow();
                newRow.SetField("worldNo", worldNo);
                newRow.SetField("p", probability);

                foreach (DataColumn column in content.Columns)
                {
                    var columnName = column.ColumnName;
                    newRow.SetField(columnName, row.Field <string>(columnName));
                }
                answerTable.Rows.Add(newRow);
            }
            if (underlineDatabase.CheckIsTableAlreadyExist(answerTableName))
            {
                underlineDatabase.WriteTableBacktoDatabase(answerTableName, answerTable);
            }
            else
            {
                underlineDatabase.CreateNewTableWithDataTable(answerTableName, answerTable);
            }
        }
        /// <summary>
        /// Find out all states that a new random variable i can has
        /// for each i's state, duplicate the existing possibleWorld table
        /// and insert this state as a row in every world
        /// </summary>
        /// <param name="query"></param>
        /// <param name="randomVariable"></param>
        private void PopulatePossibleWorlds(SqlInsertQuery query, int randomVariable)
        {
            var tableName = query.TableName;

            var statesTable = underlineDatabase.ExecuteSqlWithResult("select * from " + tableName + "_PossibleStates");

            IEnumerable <DataRow> newlyAddedPossibleStates = from newRow in statesTable.AsEnumerable()
                                                             where newRow.Field <int>("var") == randomVariable
                                                             select newRow;

            var newVarStates = newlyAddedPossibleStates.Count();

            var existingWorldsTable = underlineDatabase.ExecuteSqlWithResult("select * from " + tableName + "_PossibleWorlds");
            var result = existingWorldsTable.Copy();

            // replicate existingWorldTable, number of copy depends on number of state in new variable.
            var worldNumbers = (from dataRow in existingWorldsTable.AsEnumerable()
                                select dataRow.Field <int>("worldNo")).Distinct().ToList();

            // if old worlds is empty, for each new state create a world
            if (!worldNumbers.Any())
            {
                int index = 0;
                foreach (var eachPossibleState in newlyAddedPossibleStates)
                {
                    index++;
                    insertVariableState(index, eachPossibleState, ref result);
                }
            }
            else
            {
                for (int i = 1; i <= newVarStates; i++)
                {
                    var eachNewState = newlyAddedPossibleStates.ElementAt(i - 1);
                    // for each new variable state, duplicate existing worlds
                    var duplicatedWorldNumbers = generateNewRandomVariables(worldNumbers, i, existingWorldsTable, ref result);

                    // current new state i is assign to each world
                    foreach (var worldNumber in duplicatedWorldNumbers)
                    {
                        insertVariableState(worldNumber, eachNewState, ref result);
                    }
                }
            }

            underlineDatabase.WriteTableBacktoDatabase(tableName + "_PossibleWorlds", result);
        }