using
System;
using
System.Collections.Generic;
using
System.Text;
using
Microsoft.AnalysisServices.AdomdServer;
namespace
DMXtract
{
public
class
DMXtract
{
[SafeToPrepare(true)]
public
System.Data.DataTable
CreateMiningStructureStatements(string
miningStructureName)
{
System.Data.DataTable
results = new
System.Data.DataTable();
results.Columns.Add("Statement
Type", typeof(string));
results.Columns.Add("Statement
Object", typeof(string));
results.Columns.Add("Statement",
typeof(string));
if
(Context.ExecuteForPrepare)
return
results;
// Find
the mining structure
MiningStructure structure =
Context.MiningStructures.Find(miningStructureName);
// Add
the CREATE MINING STRUCTURE syntax
StringBuilder statement =
new
StringBuilder();
statement.Append("CREATE
MINING STRUCTURE [" + structure.Name +
"]\r\n(\r\n");
// Add
the columns
AppendStructureColumns(statement,
structure.Columns, 1);
statement.Append("\r\n)
");
results.Rows.Add("Create
Mining Structure", miningStructureName,
statement.ToString());
// Add
mining models
foreach
(MiningModel
model in
structure.MiningModels)
{
statement =
new
StringBuilder();
statement.Append("ALTER
MINING STRUCTURE [" + structure.Name +
"]\r\n ");
AppendMiningModelStatement(statement, model);
statement.Append("\r\n
");
results.Rows.Add("Add
Mining Model", model.Name,
statement.ToString());
}
return
results;
}
private
void
AppendStructureColumns(StringBuilder
statement,
MiningStructureColumnCollection columns,
int indent)
{
bool
firstRow = true;
//
Iterate columns in collection
foreach
(MiningStructureColumn
column in
columns)
{
if
(!firstRow)
statement.Append(",\r\n");
//
Indent as indicated
statement.Append('
', indent * 4);
statement.Append("["
+ column.Name +
"]\t");
if
(column.Type !=
MiningColumnType.Table)
{
// Append scalar column
statement.Append(column.Type.ToString().ToUpper());
if
(column.Distribution !=
MiningColumnDistribution.Missing)
statement.Append("
" +
column.Distribution.ToString().ToUpper());
if
(column.Flags !=
String.Empty)
statement.Append("
" + column.Flags);
statement.Append("
" + column.Content.ToString().ToUpper());
if
(column.RelatedAttribute !=
null)
statement.Append("
" + "RELATED
TO " + column.RelatedAttribute);
}
else
{
// Append nested table column
statement.Append("
TABLE");
statement.Append("\r\n");
statement.Append('
', indent * 4);
statement.Append("(\r\n");
//
Append nested columns
AppendStructureColumns(statement, column.Columns,
indent + 1);
// Close nested table definition
statement.Append("\r\n");
statement.Append('
', indent * 4);
statement.Append(")");
}
firstRow =
false;
}
}
private
void
AppendMiningModelStatement(StringBuilder
statement, MiningModel
model)
{
// Add
the ADD MINING MODEL syntax
statement.Append("ADD
MINING MODEL [" + model.Name +
"]");
statement.Append("\r\n(\r\n");
// Add
the columns
AppendModelColumns(statement,
model.Columns, 1);
// Add
the algorithm
statement.Append("\r\n)
USING ");
statement.Append(model.Algorithm);
// Add
model parameters
if
(model.Parameters.Count > 0)
{
bool
firstRow = true;
statement.Append("(");
foreach (MiningParameter
param in
model.Parameters)
{
if
(param.Name == String.Empty
| param.Value ==
String.Empty)
continue;
if
(!firstRow)
statement.Append(",
");
statement.Append(param.Name
+ "=" +
param.Value);
firstRow =
false;
}
statement.Append(")");
}
// Add
Drillthrough
if
(model.AllowDrillThrough)
statement.Append("\r\nWITH
DRILLTHROUGH");
}
private
void
AppendModelColumns(StringBuilder
statement,
MiningModelColumnCollection columns,
int indent)
{
bool
firstRow = true;
//
Iterate columns in collection
foreach
(MiningModelColumn
modelcol in
columns)
{
if
(!firstRow)
statement.Append(",\r\n");
//
Indent as indicated
statement.Append('
', indent * 4);
statement.Append("["
+ modelcol.Name +
"]");
MiningStructureColumn structcol =
modelcol.StructureColumn;
if
(modelcol.Type !=
MiningColumnType.Table)
{
// Append scalar column
// Get model column flags and remove 'NOT NULL'
since it is already specified at the model
structure
string flags =
modelcol.Flags.ToUpper().Replace("NOT
NULL", "").Replace(",,",
",").Replace(',',
' ');
if
(flags != String.Empty)
statement.Append("
" + flags);
// Add prediction flags
if
(modelcol.IsPredictable)
{
if (modelcol.IsInput)
statement.Append("
PREDICT");
else
statement.Append("
PREDICT_ONLY");
}
}
else
{
// Add prediction flags
if
(modelcol.IsPredictable)
{
if (modelcol.IsInput)
statement.Append("
PREDICT");
else
statement.Append("
PREDICT_ONLY");
}
statement.Append("\r\n");
statement.Append('
', indent * 4);
statement.Append("(\r\n");
// Append nested columns
AppendModelColumns(statement,
modelcol.Columns, indent + 1);
// Close nested table definition
statement.Append("\r\n");
statement.Append('
', indent * 4);
statement.Append(")");
}
firstRow =
false;
}
}
}
}