Esempio n. 1
0
int TestRefVal::execute()
{
	bool lSuccess = true;
	if (MVTApp::startStore())
	{
		ISession * const lSession =	MVTApp::startSession();
		const static int lNumProps = 25;
		PropertyID lPropIDs[lNumProps];
		MVTApp::mapURIs(lSession,"TestRefVal.prop",lNumProps,lPropIDs);

		// Create a few pins with properties, collections,
		// and references to other properties or values.
		typedef std::set<StoreReference, SortByReferenced> TReferences;
		TReferences lReferences;
		int i, j, k;
		PID lPIDs[100];
		for (i = 0; i < 100; i++)
		{
			CREATEPIN(lSession, &lPIDs[i], NULL, 0);
			IPIN * const lPIN = lSession->getPIN(lPIDs[i]);
			int const lNumProps = MVTRand::getRange(1, 20);
			for (j = 0; j < lNumProps; j++)
			{
				int const lNumVals = MVTRand::getRange(1, 5);
				for (k = 0; k < lNumVals; k++)
				{
					bool lDoReference = i > 0 && MVTRand::getRange(0, 10) > 3;

					// Either create a reference to some other property/value created earlier...
					if (lDoReference)
					{
						StoreReference lReference;
						lReference.mReferencing.id = lPIDs[i];
						lReference.mReferencing.pid = lPropIDs[j];
						lReference.mReferencing.eid = 0;

						// Choose a PIN to reference.
						int const lPINReferencedIndex = MVTRand::getRange(0, i-1);
						lReference.mReferenced.id = lPIDs[lPINReferencedIndex];

						// Choose a property to reference.
						IPIN * lPINReferenced;
						bool lOLMethod = MVTRand::getRange(0, 100) > 33;
						if(lOLMethod){
							lPINReferenced = lSession->getPIN(lReference.mReferenced.id);
						}
						else{
							Value lVal;
							lVal.set(lReference.mReferenced.id);
							lPINReferenced = lSession->getPIN(lVal);	
						}
						int lNumPINProps = (int)lPINReferenced->getNumberOfProperties();
						unsigned int const lPropReferencedIndex = (unsigned int)MVTRand::getRange(0, lNumPINProps-1);
						Value const * const lPropReferenced = lPINReferenced->getValue(lPropIDs[lPropReferencedIndex]);
						lReference.mReferenced.pid = lPropReferenced->property;

						// Choose a value to reference, if relevant.
						if (lPropReferenced->type == VT_COLLECTION && !lPropReferenced->isNav())
						{
							int const lValueReferencedIndex = MVTRand::getRange(0, (int)lPropReferenced->length-1);
							lReference.mReferenced.eid = lPropReferenced->varray[lValueReferencedIndex].eid;
						}
						else
						{
							lReference.mReferenced.eid = STORE_COLLECTION_ID;
							if(lPropReferenced->type == VT_COLLECTION) 
								TVERIFY(false && "navigator returned even after setting session interface mode to ITF_COLLECTIONS_AS_ARRAYS");
						}

						// Unless this exact same reference already exists, create it.
						if (lReferences.end() != lReferences.find(lReference))
							lDoReference = false;
						else
						{
							Value lPV;
							SETVALUE(lPV, lPropIDs[j], lReference.mReferenced, OP_ADD);
							if (RC_OK != lPIN->modify(&lPV, 1))
							{
								lSuccess = false;
								assert(false);
							}
							else
								lReferences.insert(lReference);
						}
					}

					// Or create a new property.
					if (!lDoReference)
					{
						Value lPV;
						Tstring lString;
						MVTRand::getString(lString, 20, 0, false);
						SETVALUE_C(lPV, lPropIDs[j], lString.c_str(), OP_ADD, STORE_LAST_ELEMENT);
						if (RC_OK != lPIN->modify(&lPV, 1))
						{
							#if 0
								lSuccess = false;
								assert(false);
							#endif
						}
					}
				}
			}
			lPIN->destroy();
		}

		#if 0 // Note: Queries for VT_REFIDELT not supported yet...
		// Query to see if all relationships are found.
		TReferences::iterator lItRef;
		for (lItRef = lReferences.begin(); lReferences.end() != lItRef; lItRef++)
		{
			// Execute the mvstore query.
			IStmt * const lQ = lSession->createStmt();
			unsigned char const lVar = lQ->addVariable();
			Value lV[2];
			PropertyID lPropIds[] = {(*lItRef).mReferencing.pid};
			lV[0].setVarRef(0, *lPropIds);
			lV[1].set((*lItRef).mReferenced);
			TExprTreePtr lExprTree = EXPRTREEGEN(lSession)(OP_EQ, 2, lV, 0);
			lQ->addCondition(lExprTree);
			ICursor * const lQR = lQ->execute();

			// Check that all objects found by the mvstore query are correct.
			IPIN * lQRIter;
			int lNumFound = 0;
			for (lQRIter = lQR->next(); NULL != lQRIter; lQRIter = lQR->next(), lNumFound++)
			{
				StoreReference lRef;
				lRef.mReferencing.id = lQRIter->getPID();
				lRef.mReferencing.pid = (*lItRef).mReferencing.pid;
				lRef.mReferenced = (*lItRef).mReferenced;
				if (lReferences.end() == lReferences.find(lRef))
				{
					lSuccess = false;
					mLogger.out() << "Query found a reference that did not exist!" << std::endl;
				}
			}

			// Check that the mvstore query found exactly the right number of objects.
			int lNumExpected = 0;
			TReferences::iterator lItRef2;
			StoreReference lRef;
			lRef.mReferenced = (*lItRef).mReferenced;
			INITLOCALPID(lRef.mReferencing.id);
			LOCALPID(lRef.mReferencing) = 0;
			lRef.mReferencing.pid = 0;
			lRef.mReferencing.eid = 0;
			for (lItRef2 = lReferences.lower_bound(lRef); lReferences.end() != lItRef2 && equalRefVIDs((*lItRef2).mReferenced, lRef.mReferenced); lItRef2++)
				if ((*lItRef2).mReferencing.pid == (*lItRef).mReferencing.pid)
					lNumExpected++;
			if (lNumFound != lNumExpected)
			{
				lSuccess = false;
				mLogger.out() << "Query found " << lNumFound << " referencing objects instead of " << lNumExpected << std::endl;
			}

			// Cleanup.
			lQR->destroy();
			lQ->destroy();
		}
		#endif

		lSession->terminate();
		MVTApp::stopStore();
	}
	else { TVERIFY(!"Unable to start store"); }
	return lSuccess	? 0	: 1;
}
string uLanguageCPP::createDeclarationFileContent(uInheritable * aClass, std::string const& base)
{
    stringstream fileContent;

    // start with multiple inclusion protection
    string includeGuard = aClass->getName() + "_H";
    transform(includeGuard.begin(), includeGuard.end(), includeGuard.begin(), ::toupper);
    fileContent << "#ifndef " << includeGuard << endl;
    fileContent << "#define " << includeGuard << endl << endl;

    // add include files
    // TODO

    // class name - inheritance
    if (base == "")
        fileContent << "class " << aClass->getName() << " {" << endl << endl;
    else
        fileContent << "class " << aClass->getName() << " : public " << base << "{" << endl << endl;


    string publicSection = "";
    string protectedSection = "";
    string privateSection = "";

    TMethods methods = aClass->getMethods();
    for (TMethodsIter iter = methods.begin(); iter < methods.end(); ++iter) {
        if ((*iter)->getAccess() == uPublic) {
            publicSection += "\t" + createMethodDeclaration(*iter) + "\n";
        }
        else if ((*iter)->getAccess() == uPrivate) {
            privateSection += "\t" + createMethodDeclaration(*iter) + "\n";
        }
        else if ((*iter)->getAccess() == uProtected) {
            protectedSection += "\t" + createMethodDeclaration(*iter) + "\n";
        }
    }

    TParameters attributes = aClass->getAttributes();
    for (TParametersIter iter = attributes.begin(); iter < attributes.end(); ++iter) {
        if ((*iter)->getAccess() == uPublic) {
            publicSection += "\t" + createAttributeDeclaration(*iter) + "\n";
        }
        else if ((*iter)->getAccess() == uPrivate) {
            privateSection += "\t" + createAttributeDeclaration(*iter) + "\n";
        }
        else if ((*iter)->getAccess() == uProtected) {
            protectedSection += "\t" + createAttributeDeclaration(*iter) + "\n";
        }
    }

    TReferences references = aClass->getReferences();
    for (TReferencesIter iter = references.begin(); iter < references.end(); ++iter) {
        if ((*iter)->getAccess() == uPublic) {
            publicSection += "\t" + createReferenceDeclaration(*(*iter)) + "\n";
        }
        else if ((*iter)->getAccess() == uPrivate) {
            privateSection += "\t" + createReferenceDeclaration(*(*iter)) + "\n";
        }
        else if ((*iter)->getAccess() == uProtected) {
            protectedSection += "\t" + createReferenceDeclaration(*(*iter)) + "\n";
        }
    }


    // public section
    fileContent << "public: " << endl;
    fileContent << publicSection << endl;

    // protected section
    fileContent << "protected: " << endl;
    fileContent << protectedSection << endl;

    // private section
    fileContent << "private: " << endl;
    fileContent << privateSection << endl;

    fileContent << "};" << endl << endl;

    fileContent << "#endif" << endl;

    return fileContent.str();
}