Data Model > Extending the UNICOM Intelligence Data Model > Creating a Metadata Source Component (MDSC) > Populating the MDM Document > Visual C++ examples
 
Visual C++ examples
This topic provides some fragments of Visual C++ that illustrate populating the MDM Document.
Creating the document
MDM::IDocumentPtr pDoc;
pDoc.CreateInstance(__uuidof(MDM::Document));
pDoc->Contexts->Current = L"QUESTION"; // set context to question
pDoc->Name = MetadataFileName; // new file name
// later when you want to save the doc
pDoc->Save(MetadataPath, false);
Creating basic variables
MDM::IVariablePtr mdmVar;
mdmVar = pDoc->CreateVariable(origVarName);
if (mdmVar == 0) {
return false;
}
// -1 indicates that the Variable should be added at the end of the list
pDoc->Fields->Add(mdmVar, -1);

mdmVar->Label = origVarQuestionText; // default contexts are Question, LABEL
// populating another context
mdmVar->Labels[L"ShortName"]->Text[L"Analysis"][L"ENU"] = origVarAnalysisShortname;
switch (origVarType)
{
case VAR_CATEGORICAL:
mdmVar->DataType = MDM::mtCategorical;
// Add the elements
MDM::IElementsPtr pElements = mdmVar->Elements;
for (short i = 0; i < origVarElementCount; ++i) {
// Generate the Name from the Text but remove invalid characters
_bstr_t bsElemName = MakeElemName(origElemText);
mdmElem = pDoc->CreateElement(bsElemName, origElemText);
if (mdmElem == 0) {
return false;
}
// -1 indicates that the Element should be added at the end of the list
pElements->Add(m_mdmElem, -1);
mdmElem->Flag = MDM::flNone; // add a flag if it is appropriate
mdmElem->Type = MDM::mtCategory;
}
mdmVar->MinValue = 1L;
mdmVar->MaxValue = (long) OrigVarMaxValue;
break;
case VAR_NUM:
mdmVar->DataType= MDM::mtLong;
mdmVar->MinValue = (long) origVarMinVal;
mdmVar->MaxValue = (long) origVarMaxVal;
break;
case VAR_REAL:
mdmVar->DataType= MDM::mtDouble;
// Note: Min,MaxValue are of type double here
mdmVar->MinValue = (double) origVarMinVal;
mdmVar->MaxValue = (double) origVarMaxVal;
break;
case VAR_TEXT:
mdmVar->DataType = MDM::mtText;
mdmVar->MinValue = (long) origVarMinVal;
mdmVar->MaxValue = (long) origVarMaxVal;
break;
case VAR_GRID
// See grid information in the Complex question types section
// ...
default:
break;
}
Adding variables to the Paper routing
// pDoc is a pointer to an MDM document
MDM::IRoutingPtr pRouting = pDoc->Routing;
MDM::ITypesPtr pFields = pDoc->Fields;
long lCount = pFields->Count;
for (long lIndex = 0; lIndex < lCount; ++lIndex)
{
MDM::IRoutingItemPtr pRoutingItem(__uuidof(MDM::RoutingItem));
pRoutingItem->Item = pFields[lIndex];
pRouting->Add(pRoutingItem, L"Paper", -1);
}
Creating a grid
// creating and adding the grid container
mdmGrid = pDoc->CreateGrid(origVarName);
if (mdmGrid == 0) {
return false;
}
pDoc->Fields->Add(mdmGrid, -1);

// creating and adding the categorical into the grid
mdmVar = pDoc->CreateVariable(origVarName, origVarQuestionText);
if (mdmVar == 0) {
return false;
}
MDM::ITypesPtr pGridFields = mdmGrid->Fields;
pGridFields->Add(mdmVar, -1);
mdmVar->DataType = MDM::mtCategorical;
mdmVar->MinValue = 1L;
mdmVar->MaxValue = (long) OrigVarMaxValue;

// Add the top elements
MDM::IElementsPtr pGridElements = mdmGrid->Elements;
pGridElements->Name = L"side"; // give the elements for the grid a name
for (short i = 0; i < origVarElementCount; ++i) {
_bstr_t bsElemText = origElemText[i];
mdmElem = pDoc->CreateElement(MakeElemName(bsElemText), bsElemText);
if (mdmElem == 0) {
return false;
}
pGridElements->Add(m_mdmElem, -1);
mdmElem->Flag = MDM::flNone; // add a flag if it is appropriate
mdmElem->Type = MDM::mtCategory;
}

// Add the routing information
MDM::RoutingPtr pRouting = mdmGrid->Class->Routing;
for (long j = 0; j < pGridFields->Count; ++j)
{
IVariablePtr pVar = pGridFields->Item[j];
if (pVar) {
IRoutingItemPtr pRoutingItem(__uuidof(MDM::RoutingItem));
pRoutingItem->Item = pVar;
pRouting->Add(pRoutingItem, L"Paper", -1);
}
}
See also
Setting up the Document
Setting up basic questions
Setting up elements
Setting up helper fields
Setting up shared category lists
Setting up complex questions
Setting up the routing
Setting up custom properties
Versions
Using the Alias Map component
Populating the MDM Document