int main(int argc, char* argv[]) { UErrorCode status = U_ZERO_ERROR; const char *arg = NULL; const char *outputDir = NULL; /* NULL = no output directory, use current */ const char *inputDir = NULL; const char *encoding = ""; int i; U_MAIN_INIT_ARGS(argc, argv); argc = u_parseArgs(argc, argv, (int32_t)(sizeof(options)/sizeof(options[0])), options); /* error handling, printing usage message */ if(argc<0) { fprintf(stderr, "%s: error in command line argument \"%s\"\n", argv[0], argv[-argc]); } else if(argc<2) { argc = -1; } if(options[WRITE_POOL_BUNDLE].doesOccur && options[USE_POOL_BUNDLE].doesOccur) { fprintf(stderr, "%s: cannot combine --writePoolBundle and --usePoolBundle\n", argv[0]); argc = -1; } if(options[FORMAT_VERSION].doesOccur) { const char *s = options[FORMAT_VERSION].value; if(uprv_strlen(s) != 1 || (s[0] != '1' && s[0] != '2')) { fprintf(stderr, "%s: unsupported --formatVersion %s\n", argv[0], s); argc = -1; } else if(s[0] == '1' && (options[WRITE_POOL_BUNDLE].doesOccur || options[USE_POOL_BUNDLE].doesOccur) ) { fprintf(stderr, "%s: cannot combine --formatVersion 1 with --writePoolBundle or --usePoolBundle\n", argv[0]); argc = -1; } else { setFormatVersion(s[0] - '0'); } } if(options[VERSION].doesOccur) { fprintf(stderr, "%s version %s (ICU version %s).\n" "%s\n", argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); return U_ZERO_ERROR; } if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) { /* * Broken into chunks because the C89 standard says the minimum * required supported string length is 509 bytes. */ fprintf(stderr, "Usage: %s [OPTIONS] [FILES]\n" "\tReads the list of resource bundle source files and creates\n" "\tbinary version of reosurce bundles (.res files)\n", argv[0]); fprintf(stderr, "Options:\n" "\t-h or -? or --help this usage text\n" "\t-q or --quiet do not display warnings\n" "\t-v or --verbose print extra information when processing files\n" "\t-V or --version prints out version number and exits\n" "\t-c or --copyright include copyright notice\n"); fprintf(stderr, "\t-e or --encoding encoding of source files\n" "\t-d of --destdir destination directory, followed by the path, defaults to %s\n" "\t-s or --sourcedir source directory for files followed by path, defaults to %s\n" "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" "\t followed by path, defaults to %s\n", u_getDataDirectory(), u_getDataDirectory(), u_getDataDirectory()); fprintf(stderr, "\t-j or --write-java write a Java ListResourceBundle for ICU4J, followed by optional encoding\n" "\t defaults to ASCII and \\uXXXX format.\n"); /* This option is deprecated and should not be used ever. "\t-p or --package-name For ICU4J: package name for writing the ListResourceBundle for ICU4J,\n" "\t defaults to com.ibm.icu.impl.data\n"); */ fprintf(stderr, "\t-b or --bundle-name bundle name for writing the ListResourceBundle for ICU4J,\n" "\t defaults to LocaleElements\n" "\t-x or --write-xliff write an XLIFF file for the resource bundle. Followed by\n" "\t an optional output file name.\n" "\t-k or --strict use pedantic parsing of syntax\n" /*added by Jing*/ "\t-l or --language for XLIFF: language code compliant with BCP 47.\n"); fprintf(stderr, "\t-C or --noBinaryCollation do not generate binary collation image;\n" "\t makes .res file smaller but collator instantiation much slower;\n" "\t maintains ability to get tailoring rules\n" "\t-R or --omitCollationRules do not include collation (tailoring) rules;\n" "\t makes .res file smaller and maintains collator instantiation speed\n" "\t but tailoring rules will not be available (they are rarely used)\n"); fprintf(stderr, "\t --formatVersion write a .res file compatible with the requested formatVersion (single digit);\n" "\t for example, --formatVersion 1\n"); fprintf(stderr, "\t --writePoolBundle write a pool.res file with all of the keys of all input bundles\n" "\t --usePoolBundle [path-to-pool.res] point to keys from the pool.res keys pool bundle if they are available there;\n" "\t makes .res files smaller but dependent on the pool bundle\n" "\t (--writePoolBundle and --usePoolBundle cannot be combined)\n"); return argc < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; } if(options[VERBOSE].doesOccur) { setVerbose(TRUE); } if(options[QUIET].doesOccur) { setShowWarning(FALSE); } if(options[STRICT].doesOccur) { setStrict(TRUE); } if(options[COPYRIGHT].doesOccur){ setIncludeCopyright(TRUE); } if(options[SOURCEDIR].doesOccur) { inputDir = options[SOURCEDIR].value; } if(options[DESTDIR].doesOccur) { outputDir = options[DESTDIR].value; } /* This option is deprecated and should never be used. if(options[PACKAGE_NAME].doesOccur) { gPackageName = options[PACKAGE_NAME].value; if(!strcmp(gPackageName, "ICUDATA")) { gPackageName = U_ICUDATA_NAME; } if(gPackageName[0] == 0) { gPackageName = NULL; } }*/ if(options[ENCODING].doesOccur) { encoding = options[ENCODING].value; } if(options[ICUDATADIR].doesOccur) { u_setDataDirectory(options[ICUDATADIR].value); } /* Initialize ICU */ u_init(&status); if (U_FAILURE(status) && status != U_FILE_ACCESS_ERROR) { /* Note: u_init() will try to open ICU property data. * failures here are expected when building ICU from scratch. * ignore them. */ fprintf(stderr, "%s: can not initialize ICU. status = %s\n", argv[0], u_errorName(status)); exit(1); } status = U_ZERO_ERROR; if(options[WRITE_JAVA].doesOccur) { write_java = TRUE; outputEnc = options[WRITE_JAVA].value; } if(options[BUNDLE_NAME].doesOccur) { bundleName = options[BUNDLE_NAME].value; } if(options[WRITE_XLIFF].doesOccur) { write_xliff = TRUE; if(options[WRITE_XLIFF].value != NULL){ xliffOutputFileName = options[WRITE_XLIFF].value; } } initParser(options[NO_BINARY_COLLATION].doesOccur, options[NO_COLLATION_RULES].doesOccur); /*added by Jing*/ if(options[LANGUAGE].doesOccur) { language = options[LANGUAGE].value; } if(options[WRITE_POOL_BUNDLE].doesOccur) { newPoolBundle = bundle_open(NULL, TRUE, &status); if(U_FAILURE(status)) { fprintf(stderr, "unable to create an empty bundle for the pool keys: %s\n", u_errorName(status)); return status; } else { const char *poolResName = "pool.res"; char *nameWithoutSuffix = uprv_malloc(uprv_strlen(poolResName) + 1); if (nameWithoutSuffix == NULL) { fprintf(stderr, "out of memory error\n"); return U_MEMORY_ALLOCATION_ERROR; } uprv_strcpy(nameWithoutSuffix, poolResName); *uprv_strrchr(nameWithoutSuffix, '.') = 0; newPoolBundle->fLocale = nameWithoutSuffix; } } if(options[USE_POOL_BUNDLE].doesOccur) { const char *poolResName = "pool.res"; FileStream *poolFile; int32_t poolFileSize; int32_t indexLength; /* * TODO: Consolidate inputDir/filename handling from main() and processFile() * into a common function, and use it here as well. * Try to create toolutil functions for dealing with dir/filenames and * loading ICU data files without udata_open(). * Share code with icupkg? * Also, make_res_filename() seems to be unused. Review and remove. */ if (options[USE_POOL_BUNDLE].value!=NULL) { uprv_strcpy(theCurrentFileName, options[USE_POOL_BUNDLE].value); uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING); } else if (inputDir) { uprv_strcpy(theCurrentFileName, inputDir); uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING); } else { *theCurrentFileName = 0; } uprv_strcat(theCurrentFileName, poolResName); poolFile = T_FileStream_open(theCurrentFileName, "rb"); if (poolFile == NULL) { fprintf(stderr, "unable to open pool bundle file %s\n", theCurrentFileName); return 1; } poolFileSize = T_FileStream_size(poolFile); if (poolFileSize < 32) { fprintf(stderr, "the pool bundle file %s is too small\n", theCurrentFileName); return 1; } poolBundle.fBytes = (uint8_t *)uprv_malloc((poolFileSize + 15) & ~15); if (poolFileSize > 0 && poolBundle.fBytes == NULL) { fprintf(stderr, "unable to allocate memory for the pool bundle file %s\n", theCurrentFileName); return U_MEMORY_ALLOCATION_ERROR; } else { UDataSwapper *ds; const DataHeader *header; int32_t bytesRead = T_FileStream_read(poolFile, poolBundle.fBytes, poolFileSize); int32_t keysBottom; if (bytesRead != poolFileSize) { fprintf(stderr, "unable to read the pool bundle file %s\n", theCurrentFileName); return 1; } /* * Swap the pool bundle so that a single checked-in file can be used. * The swapper functions also test that the data looks like * a well-formed .res file. */ ds = udata_openSwapperForInputData(poolBundle.fBytes, bytesRead, U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &status); if (U_FAILURE(status)) { fprintf(stderr, "udata_openSwapperForInputData(pool bundle %s) failed: %s\n", theCurrentFileName, u_errorName(status)); return status; } ures_swap(ds, poolBundle.fBytes, bytesRead, poolBundle.fBytes, &status); udata_closeSwapper(ds); if (U_FAILURE(status)) { fprintf(stderr, "ures_swap(pool bundle %s) failed: %s\n", theCurrentFileName, u_errorName(status)); return status; } header = (const DataHeader *)poolBundle.fBytes; if (header->info.formatVersion[0]!=2) { fprintf(stderr, "invalid format of pool bundle file %s\n", theCurrentFileName); return U_INVALID_FORMAT_ERROR; } poolBundle.fKeys = (const char *)header + header->dataHeader.headerSize; poolBundle.fIndexes = (const int32_t *)poolBundle.fKeys + 1; indexLength = poolBundle.fIndexes[URES_INDEX_LENGTH] & 0xff; if (indexLength <= URES_INDEX_POOL_CHECKSUM) { fprintf(stderr, "insufficient indexes[] in pool bundle file %s\n", theCurrentFileName); return U_INVALID_FORMAT_ERROR; } keysBottom = (1 + indexLength) * 4; poolBundle.fKeys += keysBottom; poolBundle.fKeysLength = (poolBundle.fIndexes[URES_INDEX_KEYS_TOP] * 4) - keysBottom; poolBundle.fChecksum = poolBundle.fIndexes[URES_INDEX_POOL_CHECKSUM]; } for (i = 0; i < poolBundle.fKeysLength; ++i) { if (poolBundle.fKeys[i] == 0) { ++poolBundle.fKeysCount; } } T_FileStream_close(poolFile); setUsePoolBundle(TRUE); } if(options[INCLUDE_UNIHAN_COLL].doesOccur) { gIncludeUnihanColl = TRUE; } if((argc-1)!=1) { printf("genrb number of files: %d\n", argc - 1); } /* generate the binary files */ for(i = 1; i < argc; ++i) { status = U_ZERO_ERROR; arg = getLongPathname(argv[i]); if (inputDir) { uprv_strcpy(theCurrentFileName, inputDir); uprv_strcat(theCurrentFileName, U_FILE_SEP_STRING); } else { *theCurrentFileName = 0; } uprv_strcat(theCurrentFileName, arg); if (isVerbose()) { printf("Processing file \"%s\"\n", theCurrentFileName); } processFile(arg, encoding, inputDir, outputDir, gPackageName, &status); } uprv_free(poolBundle.fBytes); if(options[WRITE_POOL_BUNDLE].doesOccur) { char outputFileName[256]; bundle_write(newPoolBundle, outputDir, NULL, outputFileName, sizeof(outputFileName), &status); bundle_close(newPoolBundle, &status); if(U_FAILURE(status)) { fprintf(stderr, "unable to write the pool bundle: %s\n", u_errorName(status)); } } /* Dont return warnings as a failure */ if (U_SUCCESS(status)) { return 0; } return status; }
/* Process a file */ void processFile(const char *filename, const char *cp, const char *inputDir, const char *outputDir, const char *packageName, UErrorCode *status) { /*FileStream *in = NULL;*/ struct SRBRoot *data = NULL; UCHARBUF *ucbuf = NULL; char *rbname = NULL; char *openFileName = NULL; char *inputDirBuf = NULL; char outputFileName[256]; int32_t dirlen = 0; int32_t filelen = 0; if (status==NULL || U_FAILURE(*status)) { return; } if(filename==NULL){ *status=U_ILLEGAL_ARGUMENT_ERROR; return; }else{ filelen = (int32_t)uprv_strlen(filename); } if(inputDir == NULL) { const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR); openFileName = (char *) uprv_malloc(dirlen + filelen + 2); openFileName[0] = '\0'; if (filenameBegin != NULL) { /* * When a filename ../../../data/root.txt is specified, * we presume that the input directory is ../../../data * This is very important when the resource file includes * another file, like UCARules.txt or thaidict.brk. */ int32_t filenameSize = (int32_t)(filenameBegin - filename + 1); inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize); /* test for NULL */ if(inputDirBuf == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; goto finish; } inputDirBuf[filenameSize - 1] = 0; inputDir = inputDirBuf; dirlen = (int32_t)uprv_strlen(inputDir); } }else{ dirlen = (int32_t)uprv_strlen(inputDir); if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { openFileName = (char *) uprv_malloc(dirlen + filelen + 2); /* test for NULL */ if(openFileName == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; goto finish; } openFileName[0] = '\0'; /* * append the input dir to openFileName if the first char in * filename is not file seperation char and the last char input directory is not '.'. * This is to support : * genrb -s. /home/icu/data * genrb -s. icu/data * The user cannot mix notations like * genrb -s. /icu/data --- the absolute path specified. -s redundant * user should use * genrb -s. icu/data --- start from CWD and look in icu/data dir */ if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){ uprv_strcpy(openFileName, inputDir); openFileName[dirlen] = U_FILE_SEP_CHAR; } openFileName[dirlen + 1] = '\0'; } else { openFileName = (char *) uprv_malloc(dirlen + filelen + 1); /* test for NULL */ if(openFileName == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; goto finish; } uprv_strcpy(openFileName, inputDir); } } uprv_strcat(openFileName, filename); ucbuf = ucbuf_open(openFileName, &cp,getShowWarning(),TRUE, status); if(*status == U_FILE_ACCESS_ERROR) { fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName); goto finish; } if (ucbuf == NULL || U_FAILURE(*status)) { fprintf(stderr, "An error occured processing file %s. Error: %s\n", openFileName == NULL ? filename : openFileName,u_errorName(*status)); goto finish; } /* auto detected popular encodings? */ if (cp!=NULL && isVerbose()) { printf("autodetected encoding %s\n", cp); } /* Parse the data into an SRBRoot */ data = parse(ucbuf, inputDir, outputDir, status); if (data == NULL || U_FAILURE(*status)) { fprintf(stderr, "couldn't parse the file %s. Error:%s\n", filename,u_errorName(*status)); goto finish; } if(options[WRITE_POOL_BUNDLE].doesOccur) { int32_t newKeysLength; const char *newKeys, *newKeysLimit; bundle_compactKeys(data, status); newKeys = bundle_getKeyBytes(data, &newKeysLength); bundle_addKeyBytes(newPoolBundle, newKeys, newKeysLength, status); if(U_FAILURE(*status)) { fprintf(stderr, "bundle_compactKeys(%s) or bundle_getKeyBytes() failed: %s\n", filename, u_errorName(*status)); goto finish; } /* count the number of just-added key strings */ for(newKeysLimit = newKeys + newKeysLength; newKeys < newKeysLimit; ++newKeys) { if(*newKeys == 0) { ++newPoolBundle->fKeysCount; } } } if(options[USE_POOL_BUNDLE].doesOccur) { data->fPoolBundleKeys = poolBundle.fKeys; data->fPoolBundleKeysLength = poolBundle.fKeysLength; data->fPoolBundleKeysCount = poolBundle.fKeysCount; data->fPoolChecksum = poolBundle.fChecksum; } /* Determine the target rb filename */ rbname = make_res_filename(filename, outputDir, packageName, status); if(U_FAILURE(*status)) { fprintf(stderr, "couldn't make the res fileName for bundle %s. Error:%s\n", filename,u_errorName(*status)); goto finish; } if(write_java== TRUE){ bundle_write_java(data,outputDir,outputEnc, outputFileName, sizeof(outputFileName),packageName,bundleName,status); }else if(write_xliff ==TRUE){ bundle_write_xml(data,outputDir,outputEnc, filename, outputFileName, sizeof(outputFileName),language, xliffOutputFileName,status); }else{ /* Write the data to the file */ bundle_write(data, outputDir, packageName, outputFileName, sizeof(outputFileName), status); } if (U_FAILURE(*status)) { fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName,u_errorName(*status)); } bundle_close(data, status); finish: if (inputDirBuf != NULL) { uprv_free(inputDirBuf); } if (openFileName != NULL) { uprv_free(openFileName); } if(ucbuf) { ucbuf_close(ucbuf); } if (rbname) { uprv_free(rbname); } }
// Implement this test. int TestMergeCollections::execute() { bool lSuccess = true; if (MVTApp::startStore()) { ISession * const lSession = MVTApp::startSession(); // Declare properties. URIMap lData[2]; MVTApp::mapURIs(lSession,"TestMergeCollections.prop",2,lData); PropertyID const lPropIdVal = lData[0].uid; PropertyID const lPropIdRef = lData[1].uid; PropertyID const lPropIdPinId = PROP_SPEC_PINID; DataEventID lClassVal = STORE_INVALID_CLASSID; { char lB[100]; sprintf(lB,"TestMergeCollections.ClassVal.%d",rand()); Value lV[2]; lV[0].setVarRef(0,lPropIdVal); lV[1].setParam(0); IExprNode *expr=lSession->expr(OP_LT,2,lV); IStmt *lQ = lSession->createStmt(); lQ->addVariable(NULL,0,expr); TVERIFYRC(defineClass(lSession,lB,lQ,&lClassVal)); lQ->destroy(); } DataEventID lClassRef = STORE_INVALID_CLASSID; { char lB[100]; sprintf(lB,"TestMergeCollections.ClassRef.%d",rand()); IStmt *lQ = lSession->createStmt(); lQ->addVariable(); lQ->setPropCondition(0,&lPropIdRef,1); TVERIFYRC(defineClass(lSession,lB,lQ,&lClassRef)); lQ->destroy(); } /** * The following creates the family on reference class... **/ DataEventID lClassFamilyOnRef = STORE_INVALID_CLASSID; { char lB[100]; sprintf(lB,"TestMergeCollections.ClassFamilyOnRef.%d",rand()); Value lVFoR[2]; lVFoR[0].setVarRef(0,lPropIdRef); lVFoR[1].setParam(0); IExprNode *expr=lSession->expr(OP_IN,2,lVFoR); IStmt *lQ = lSession->createStmt(); lQ->addVariable(NULL,0,expr); TVERIFYRC(defineClass(lSession,lB,lQ,&lClassFamilyOnRef)); lQ->destroy(); } // Create a few pins mLogger.out() << "Creating " << NPINS*2 << " pins... "; PID pins[NPINS]; bool lfMember[NPINS]; PID refferedPins[NPINS]; //Creating first group of pins: // - each pin may have an interger value, or collection of interger values; // - each pin may have the same interger value within collection; // - if at least one of the values within pin < treshold, it should be picked up by the family... const int threshold = RANGE/3; int i; if(isVerbose()) mLogger.out() << " List of PINs with value less than threshold " << std::endl; for (i = 0; i < NPINS; i++) { Value lV[2]; int val = MVTRand::getRange(0, RANGE); lfMember[i] = val < threshold; SETVALUE(lV[0], lPropIdVal, val, OP_SET); CREATEPIN(lSession, &pins[i], lV, 1); IPIN *ppin = lSession->getPIN(pins[i]); int elemincollection = MVTRand::getRange(1, MAX_ELEM_INCOLLECTION); for(int jj = 0; jj < elemincollection; jj++) { int val2 = MVTRand::getRange(1, RANGE); if(!lfMember[i] && (val2 < threshold)) lfMember[i] = true; SETVALUE_C(lV[0], lPropIdVal, val2, OP_ADD, STORE_LAST_ELEMENT); TVERIFYRC(ppin->modify(lV,1)); } if(isVerbose() && lfMember[i]) mLogger.out() << std::hex << pins[i].pid << std::endl; ppin->destroy(); } // Create second group of pins: // - each pin within that group has either reference or collection of references // to a random pin(s) in the set above // - the reference to the pin, which value < threshold may be within any position within collection; // - it may be multiple references to the same pin, including the pin which value < threshold; if(isVerbose()) mLogger.out() << " List of PINs with refs whose value is less than threshold " << std::endl; int lCount = 0; int lCountDiff = 0; //counts how many pins with value < threshold has been added while creating collection... for (i = 0; i < NPINS; i++) { PID lReferPID; INITLOCALPID(lReferPID); lReferPID.pid = STORE_INVALID_PID; Value lV[2]; int idx = MVTRand::getRange(0, NPINS-1); PID id; bool willbefound = false; if (lfMember[idx]) { lCount++; willbefound = true; // The referenced pin will match the family query lReferPID = pins[idx]; } SETVALUE(lV[0], lPropIdRef, pins[idx], OP_SET); refferedPins[i] = pins[idx]; // to remember reffered pins; CREATEPIN(lSession, &id, lV, 1); IPIN *ppin = lSession->getPIN(id); int elemincollection = MVTRand::getRange(1, MAX_ELEM_INCOLLECTION); for(int jj = 0; jj < elemincollection; jj++) { int idx1 = MVTRand::getRange(0, NPINS-1); SETVALUE_C(lV[0], lPropIdRef, pins[idx1], OP_ADD, STORE_LAST_ELEMENT); TVERIFYRC(ppin->modify(lV,1)); if(!willbefound && lfMember[idx1]) { lCount++; willbefound = true; lReferPID = pins[idx1]; lCountDiff++; } } if(isVerbose() && willbefound) mLogger.out() << std::hex << id.pid << " with ref to " << lReferPID.pid << std::endl; ppin->destroy(); } mLogger.out() << std::dec << "DONE" << std::endl; // Do a simple join search. // " All pins from lClassFamilyOnRef Family, where any of the PINs in lPropIdRef has (lPropIdVal < threshhold) // "All pins, from lClassRef class, where the lPropIdRef property is equal // to the PROP_SPEC_PINID of a PIN matching the lClassVal query of (lPropIdVal < threshhold)" // IStmt *lQ = lSession->createStmt(); Value lV[4]; lV[0].setError(lPropIdRef); lV[1].setError(lPropIdRef); lV[2].setRange(&lV[0]); lV[3].set(threshold); // Family query will find all pins with lPropIdVal < threshhold SourceSpec classSpec[2]={{lClassFamilyOnRef,1,&lV[2]},{lClassVal,1,&lV[3]}}; unsigned char lVar1 = lQ->addVariable(&classSpec[0],1),lVar2 = lQ->addVariable(&classSpec[1],1); lV[0].setVarRef(lVar1,lPropIdRef); lV[1].setVarRef(lVar2,lPropIdPinId); IExprNode *expr = lSession->expr(OP_EQ,2,lV); lQ->join(lVar1,lVar2,expr); OrderSeg ord={NULL,lPropIdRef,0,0,0}; lQ->setOrder(&ord,1); uint64_t lCnt = 0; TVERIFYRC(lQ->count(lCnt,NULL,0,~0u)); TVERIFY((int)lCnt == lCount); { mLogger.out() << "Count returned = " << lCnt << "; Expected Count = " << lCount << "; lCountDiff = " << lCountDiff << ";" << std::endl; //The query below - attempt to find how many pins with at least one value < threshold were found by Family... mLogger.out() << "The query below - attempt to find how many pins with at least one value < threshold were found by Family..." << std::endl; IStmt *lQFamily = lSession->createStmt(); lQFamily->addVariable(&classSpec[1],1); uint64_t lCntFFound = 0; TVERIFYRC(lQFamily->count(lCntFFound)); mLogger.out() << "lCntFFound= " << lCntFFound << ";" << std::endl; lQFamily->destroy(); } ICursor *lR = NULL; TVERIFYRC(lQ->execute(&lR, NULL,0,~0u,0)); int lResultCount = 0; IPIN *pInResults; if (isVerbose()) mLogger.out() << "List of PINs returned " << std::endl; while((pInResults=lR->next())!=NULL) { lResultCount++; //if (isVerbose()) MVTApp::output(*pInResults,mLogger.out()); if (isVerbose()) mLogger.out() << std::hex << pInResults->getPID().pid << std::endl; const Value * refVal = pInResults->getValue(lPropIdRef); if ( refVal!=NULL ) { MvStoreEx::CollectionIterator lCollection(refVal); bool lBelongs = false; const Value *lValue; for(lValue = lCollection.getFirst(); lValue!=NULL; lValue = lCollection.getNext()) { TVERIFY(lValue->type == VT_REFID); IPIN *lPIN = lSession->getPIN(lValue->id); TVERIFY(lPIN != NULL); const Value *lVal = lPIN->getValue(lPropIdVal); MvStoreEx::CollectionIterator lValues(lVal); const Value *lVal1; for(lVal1 = lValues.getFirst(); lVal1!=NULL; lVal1 = lValues.getNext()) { TVERIFY(lVal1->type == VT_INT); if(lVal1->i < threshold) { lBelongs = true; break; } } lPIN->destroy(); if(lBelongs) break; } TVERIFY(lBelongs && "Unexpected PIN returned"); } else { TVERIFY(false && "Failed to fetch the collection"); } pInResults->destroy(); } mLogger.out() << " Total PINs returned in the result set " << std::dec << lResultCount << std::endl; TVERIFY(lResultCount == lCount); lR->destroy(); lQ->destroy(); lSession->terminate(); MVTApp::stopStore(); } else { TVERIFY(!"could not open store") ; } return lSuccess ? 0 : 1; }
int main(int argc, char* argv[]) { UErrorCode status = U_ZERO_ERROR; const char *arg = NULL; const char *outputDir = NULL; /* NULL = no output directory, use current */ const char *inputDir = NULL; const char *encoding = ""; int i; UBool illegalArg = FALSE; U_MAIN_INIT_ARGS(argc, argv); options[JAVA_PACKAGE].value = "com.ibm.icu.impl.data"; options[BUNDLE_NAME].value = "LocaleElements"; argc = u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options); /* error handling, printing usage message */ if(argc<0) { fprintf(stderr, "%s: error in command line argument \"%s\"\n", argv[0], argv[-argc]); illegalArg = TRUE; } else if(argc<2) { illegalArg = TRUE; } if(options[WRITE_POOL_BUNDLE].doesOccur && options[USE_POOL_BUNDLE].doesOccur) { fprintf(stderr, "%s: cannot combine --writePoolBundle and --usePoolBundle\n", argv[0]); illegalArg = TRUE; } if(options[FORMAT_VERSION].doesOccur) { const char *s = options[FORMAT_VERSION].value; if(uprv_strlen(s) != 1 || (s[0] < '1' && '3' < s[0])) { fprintf(stderr, "%s: unsupported --formatVersion %s\n", argv[0], s); illegalArg = TRUE; } else if(s[0] == '1' && (options[WRITE_POOL_BUNDLE].doesOccur || options[USE_POOL_BUNDLE].doesOccur) ) { fprintf(stderr, "%s: cannot combine --formatVersion 1 with --writePoolBundle or --usePoolBundle\n", argv[0]); illegalArg = TRUE; } else { setFormatVersion(s[0] - '0'); } } if((options[JAVA_PACKAGE].doesOccur || options[BUNDLE_NAME].doesOccur) && !options[WRITE_JAVA].doesOccur) { fprintf(stderr, "%s error: command line argument --java-package or --bundle-name " "without --write-java\n", argv[0]); illegalArg = TRUE; } if(options[VERSION].doesOccur) { fprintf(stderr, "%s version %s (ICU version %s).\n" "%s\n", argv[0], GENRB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); if(!illegalArg) { return U_ZERO_ERROR; } } if(illegalArg || options[HELP1].doesOccur || options[HELP2].doesOccur) { /* * Broken into chunks because the C89 standard says the minimum * required supported string length is 509 bytes. */ fprintf(stderr, "Usage: %s [OPTIONS] [FILES]\n" "\tReads the list of resource bundle source files and creates\n" "\tbinary version of resource bundles (.res files)\n", argv[0]); fprintf(stderr, "Options:\n" "\t-h or -? or --help this usage text\n" "\t-q or --quiet do not display warnings\n" "\t-v or --verbose print extra information when processing files\n" "\t-V or --version prints out version number and exits\n" "\t-c or --copyright include copyright notice\n"); fprintf(stderr, "\t-e or --encoding encoding of source files\n" "\t-d of --destdir destination directory, followed by the path, defaults to %s\n" "\t-s or --sourcedir source directory for files followed by path, defaults to %s\n" "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" "\t followed by path, defaults to %s\n", u_getDataDirectory(), u_getDataDirectory(), u_getDataDirectory()); fprintf(stderr, "\t-j or --write-java write a Java ListResourceBundle for ICU4J, followed by optional encoding\n" "\t defaults to ASCII and \\uXXXX format.\n" "\t --java-package For --write-java: package name for writing the ListResourceBundle,\n" "\t defaults to com.ibm.icu.impl.data\n"); fprintf(stderr, "\t-b or --bundle-name For --write-java: root resource bundle name for writing the ListResourceBundle,\n" "\t defaults to LocaleElements\n" "\t-x or --write-xliff write an XLIFF file for the resource bundle. Followed by\n" "\t an optional output file name.\n" "\t-k or --strict use pedantic parsing of syntax\n" /*added by Jing*/ "\t-l or --language for XLIFF: language code compliant with BCP 47.\n"); fprintf(stderr, "\t-C or --noBinaryCollation do not generate binary collation image;\n" "\t makes .res file smaller but collator instantiation much slower;\n" "\t maintains ability to get tailoring rules\n" "\t-R or --omitCollationRules do not include collation (tailoring) rules;\n" "\t makes .res file smaller and maintains collator instantiation speed\n" "\t but tailoring rules will not be available (they are rarely used)\n"); fprintf(stderr, "\t --formatVersion write a .res file compatible with the requested formatVersion (single digit);\n" "\t for example, --formatVersion 1\n"); fprintf(stderr, "\t --writePoolBundle write a pool.res file with all of the keys of all input bundles\n" "\t --usePoolBundle [path-to-pool.res] point to keys from the pool.res keys pool bundle if they are available there;\n" "\t makes .res files smaller but dependent on the pool bundle\n" "\t (--writePoolBundle and --usePoolBundle cannot be combined)\n"); return illegalArg ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; } if(options[VERBOSE].doesOccur) { setVerbose(TRUE); } if(options[QUIET].doesOccur) { setShowWarning(FALSE); } if(options[STRICT].doesOccur) { setStrict(TRUE); } if(options[COPYRIGHT].doesOccur){ setIncludeCopyright(TRUE); } if(options[SOURCEDIR].doesOccur) { inputDir = options[SOURCEDIR].value; } if(options[DESTDIR].doesOccur) { outputDir = options[DESTDIR].value; } if(options[ENCODING].doesOccur) { encoding = options[ENCODING].value; } if(options[ICUDATADIR].doesOccur) { u_setDataDirectory(options[ICUDATADIR].value); } /* Initialize ICU */ u_init(&status); if (U_FAILURE(status) && status != U_FILE_ACCESS_ERROR) { /* Note: u_init() will try to open ICU property data. * failures here are expected when building ICU from scratch. * ignore them. */ fprintf(stderr, "%s: can not initialize ICU. status = %s\n", argv[0], u_errorName(status)); exit(1); } status = U_ZERO_ERROR; if(options[WRITE_JAVA].doesOccur) { write_java = TRUE; outputEnc = options[WRITE_JAVA].value; } if(options[WRITE_XLIFF].doesOccur) { write_xliff = TRUE; if(options[WRITE_XLIFF].value != NULL){ xliffOutputFileName = options[WRITE_XLIFF].value; } } initParser(); /*added by Jing*/ if(options[LANGUAGE].doesOccur) { language = options[LANGUAGE].value; } LocalPointer<SRBRoot> newPoolBundle; if(options[WRITE_POOL_BUNDLE].doesOccur) { newPoolBundle.adoptInsteadAndCheckErrorCode(new SRBRoot(NULL, TRUE, status), status); if(U_FAILURE(status)) { fprintf(stderr, "unable to create an empty bundle for the pool keys: %s\n", u_errorName(status)); return status; } else { const char *poolResName = "pool.res"; char *nameWithoutSuffix = static_cast<char *>(uprv_malloc(uprv_strlen(poolResName) + 1)); if (nameWithoutSuffix == NULL) { fprintf(stderr, "out of memory error\n"); return U_MEMORY_ALLOCATION_ERROR; } uprv_strcpy(nameWithoutSuffix, poolResName); *uprv_strrchr(nameWithoutSuffix, '.') = 0; newPoolBundle->fLocale = nameWithoutSuffix; } } if(options[USE_POOL_BUNDLE].doesOccur) { const char *poolResName = "pool.res"; FileStream *poolFile; int32_t poolFileSize; int32_t indexLength; /* * TODO: Consolidate inputDir/filename handling from main() and processFile() * into a common function, and use it here as well. * Try to create toolutil functions for dealing with dir/filenames and * loading ICU data files without udata_open(). * Share code with icupkg? * Also, make_res_filename() seems to be unused. Review and remove. */ CharString poolFileName; if (options[USE_POOL_BUNDLE].value!=NULL) { poolFileName.append(options[USE_POOL_BUNDLE].value, status); } else if (inputDir) { poolFileName.append(inputDir, status); } poolFileName.appendPathPart(poolResName, status); if (U_FAILURE(status)) { return status; } poolFile = T_FileStream_open(poolFileName.data(), "rb"); if (poolFile == NULL) { fprintf(stderr, "unable to open pool bundle file %s\n", poolFileName.data()); return 1; } poolFileSize = T_FileStream_size(poolFile); if (poolFileSize < 32) { fprintf(stderr, "the pool bundle file %s is too small\n", poolFileName.data()); return 1; } poolBundle.fBytes = new uint8_t[(poolFileSize + 15) & ~15]; if (poolFileSize > 0 && poolBundle.fBytes == NULL) { fprintf(stderr, "unable to allocate memory for the pool bundle file %s\n", poolFileName.data()); return U_MEMORY_ALLOCATION_ERROR; } UDataSwapper *ds; const DataHeader *header; int32_t bytesRead = T_FileStream_read(poolFile, poolBundle.fBytes, poolFileSize); if (bytesRead != poolFileSize) { fprintf(stderr, "unable to read the pool bundle file %s\n", poolFileName.data()); return 1; } /* * Swap the pool bundle so that a single checked-in file can be used. * The swapper functions also test that the data looks like * a well-formed .res file. */ ds = udata_openSwapperForInputData(poolBundle.fBytes, bytesRead, U_IS_BIG_ENDIAN, U_CHARSET_FAMILY, &status); if (U_FAILURE(status)) { fprintf(stderr, "udata_openSwapperForInputData(pool bundle %s) failed: %s\n", poolFileName.data(), u_errorName(status)); return status; } ures_swap(ds, poolBundle.fBytes, bytesRead, poolBundle.fBytes, &status); udata_closeSwapper(ds); if (U_FAILURE(status)) { fprintf(stderr, "ures_swap(pool bundle %s) failed: %s\n", poolFileName.data(), u_errorName(status)); return status; } header = (const DataHeader *)poolBundle.fBytes; if (header->info.formatVersion[0] < 2) { fprintf(stderr, "invalid format of pool bundle file %s\n", poolFileName.data()); return U_INVALID_FORMAT_ERROR; } const int32_t *pRoot = (const int32_t *)( (const char *)header + header->dataHeader.headerSize); poolBundle.fIndexes = pRoot + 1; indexLength = poolBundle.fIndexes[URES_INDEX_LENGTH] & 0xff; if (indexLength <= URES_INDEX_POOL_CHECKSUM) { fprintf(stderr, "insufficient indexes[] in pool bundle file %s\n", poolFileName.data()); return U_INVALID_FORMAT_ERROR; } int32_t keysBottom = 1 + indexLength; int32_t keysTop = poolBundle.fIndexes[URES_INDEX_KEYS_TOP]; poolBundle.fKeys = (const char *)(pRoot + keysBottom); poolBundle.fKeysLength = (keysTop - keysBottom) * 4; poolBundle.fChecksum = poolBundle.fIndexes[URES_INDEX_POOL_CHECKSUM]; for (i = 0; i < poolBundle.fKeysLength; ++i) { if (poolBundle.fKeys[i] == 0) { ++poolBundle.fKeysCount; } } // 16BitUnits[] begins with strings-v2. // The strings-v2 may optionally be terminated by what looks like // an explicit string length that exceeds the number of remaining 16-bit units. int32_t stringUnitsLength = (poolBundle.fIndexes[URES_INDEX_16BIT_TOP] - keysTop) * 2; if (stringUnitsLength >= 2 && getFormatVersion() >= 3) { poolBundle.fStrings = new PseudoListResource(NULL, status); if (poolBundle.fStrings == NULL) { fprintf(stderr, "unable to allocate memory for the pool bundle strings %s\n", poolFileName.data()); return U_MEMORY_ALLOCATION_ERROR; } // The PseudoListResource constructor call did not allocate further memory. assert(U_SUCCESS(status)); const UChar *p = (const UChar *)(pRoot + keysTop); int32_t remaining = stringUnitsLength; do { int32_t first = *p; int8_t numCharsForLength; int32_t length; if (!U16_IS_TRAIL(first)) { // NUL-terminated numCharsForLength = 0; for (length = 0; length < remaining && p[length] != 0; ++length) {} } else if (first < 0xdfef) { numCharsForLength = 1; length = first & 0x3ff; } else if (first < 0xdfff && remaining >= 2) { numCharsForLength = 2; length = ((first - 0xdfef) << 16) | p[1]; } else if (first == 0xdfff && remaining >= 3) { numCharsForLength = 3; length = ((int32_t)p[1] << 16) | p[2]; } else { break; // overrun } // Check for overrun before changing remaining, // so that it is always accurate after the loop body. if ((numCharsForLength + length) >= remaining || p[numCharsForLength + length] != 0) { break; // overrun or explicitly terminated } int32_t poolStringIndex = stringUnitsLength - remaining; // Maximum pool string index when suffix-sharing the last character. int32_t maxStringIndex = poolStringIndex + numCharsForLength + length - 1; if (maxStringIndex >= RES_MAX_OFFSET) { // pool string index overrun break; } p += numCharsForLength; remaining -= numCharsForLength; if (length != 0) { StringResource *sr = new StringResource(poolStringIndex, numCharsForLength, p, length, status); if (sr == NULL) { fprintf(stderr, "unable to allocate memory for a pool bundle string %s\n", poolFileName.data()); return U_MEMORY_ALLOCATION_ERROR; } poolBundle.fStrings->add(sr); poolBundle.fStringIndexLimit = maxStringIndex + 1; // The StringResource constructor did not allocate further memory. assert(U_SUCCESS(status)); } p += length + 1; remaining -= length + 1; } while (remaining > 0); if (poolBundle.fStrings->fCount == 0) { delete poolBundle.fStrings; poolBundle.fStrings = NULL; } } T_FileStream_close(poolFile); setUsePoolBundle(TRUE); if (isVerbose() && poolBundle.fStrings != NULL) { printf("number of shared strings: %d\n", (int)poolBundle.fStrings->fCount); int32_t length = poolBundle.fStringIndexLimit + 1; // incl. last NUL printf("16-bit units for strings: %6d = %6d bytes\n", (int)length, (int)length * 2); } } if(!options[FORMAT_VERSION].doesOccur && getFormatVersion() == 3 && poolBundle.fStrings == NULL && !options[WRITE_POOL_BUNDLE].doesOccur) { // If we just default to formatVersion 3 // but there are no pool bundle strings to share // and we do not write a pool bundle, // then write formatVersion 2 which is just as good. setFormatVersion(2); } if(options[INCLUDE_UNIHAN_COLL].doesOccur) { puts("genrb option --includeUnihanColl ignored: \n" "CLDR 26/ICU 54 unihan data is small, except\n" "the ucadata-unihan.icu version of the collation root data\n" "is about 300kB larger than the ucadata-implicithan.icu version."); } if((argc-1)!=1) { printf("genrb number of files: %d\n", argc - 1); } /* generate the binary files */ for(i = 1; i < argc; ++i) { status = U_ZERO_ERROR; arg = getLongPathname(argv[i]); CharString theCurrentFileName; if (inputDir) { theCurrentFileName.append(inputDir, status); } theCurrentFileName.appendPathPart(arg, status); if (U_FAILURE(status)) { break; } gCurrentFileName = theCurrentFileName.data(); if (isVerbose()) { printf("Processing file \"%s\"\n", theCurrentFileName.data()); } processFile(arg, encoding, inputDir, outputDir, NULL, newPoolBundle.getAlias(), options[NO_BINARY_COLLATION].doesOccur, status); } poolBundle.close(); if(U_SUCCESS(status) && options[WRITE_POOL_BUNDLE].doesOccur) { char outputFileName[256]; newPoolBundle->write(outputDir, NULL, outputFileName, sizeof(outputFileName), status); if(U_FAILURE(status)) { fprintf(stderr, "unable to write the pool bundle: %s\n", u_errorName(status)); } } u_cleanup(); /* Dont return warnings as a failure */ if (U_SUCCESS(status)) { return 0; } return status; }
void MoveFinderExternEngine::setVerbose(bool verbose) { AbstractMoveFinder::setVerbose(verbose); m_externEngine.setVerbose(isVerbose()); }
uHTTP::HTTP::StatusCode FileServer::httpRequestRecieved(uHTTP::HTTPRequest *httpReq) { if (isVerbose()) { std::string firstHeader; std::cout << "> " << httpReq->getRequestLine(firstHeader) << std::endl; for (uHTTP::HTTPHeaderList::iterator header = httpReq->getHeaders().begin(); header != httpReq->getHeaders().end(); header++) { std::cout << "> " << (*header)->getName() << " : " << (*header)->getValue() << std::endl; } } if (!httpReq->isGetRequest()) { return httpReq->returnBadRequest(); } uHTTP::URI reqUri; httpReq->getURI(reqUri); std::string systemPath; systemPath.append(getRootDirectory()); systemPath.append(reqUri.getPath()); std::ifstream contentFs; contentFs.open(systemPath.c_str(), std::ifstream::in | std::ifstream::binary); if (!contentFs.is_open()) { return httpReq->returnNotFound(); } size_t fileSize = (size_t)contentFs.seekg(0, std::ios::end).tellg(); contentFs.seekg(0, std::ios::beg); bool isBinary = false; while (contentFs.good()) { unsigned char c = contentFs.get(); if (contentFs.good()) { if (c == 0) { isBinary = true; break; } } } contentFs.close(); uHTTP::HTTPResponse httpRes; httpRes.setStatusCode(uHTTP::HTTP::OK_REQUEST); httpRes.setContentType(isBinary ? "application/octet-stream" : "text/plain"); httpRes.setContentLength(fileSize); if (verboseMode) { std::cout << "< " << httpRes.getFirstLine() << std::endl; for (uHTTP::HTTPHeaderList::iterator header = httpRes.getHeaders().begin(); header != httpRes.getHeaders().end(); header++) { std::cout << "< " << (*header)->getName() << " : " << (*header)->getValue() << std::endl; } } httpReq->post(&httpRes, true); if (httpReq->isHeadRequest()) { return uHTTP::HTTP::OK_REQUEST; } uHTTP::HTTPSocket *httpSocket = httpReq->getSocket(); contentFs.open(systemPath.c_str(), std::ifstream::in | std::ifstream::binary); if (contentFs.is_open()) { while (contentFs.good()) { unsigned char c = contentFs.get(); if (contentFs.good()) { httpSocket->post(c); if (isVerbose()) std::cout << c; } } } contentFs.close(); return uHTTP::HTTP::OK_REQUEST; }
int Engine::smoothSearch() { char task[60]; std::vector<int> nbd(xBuffer_.size()); double f, dsave[29], *wa; int tr = -1, iter = 0, *iwa, isave[44], lsave[4]; int xSize = xBuffer_.size(); int m = 5; bool canstop = 0; wa = (double*) malloc( ((2 * m + 4) * xSize + 11 * m * m + 8 * m) * sizeof(double)); iwa = (int *) R_alloc(3 * xSize, sizeof(int)); if (itSoftMax_ < 100) itSoftMax_ = 100; else if (itSoftMax_ > 1000) itSoftMax_ = 1000; strcpy(task, "START"); for (int i = 0; i < xSize; ++i) { nbd[i] = 2; } L111: if (iter >= itSoftMax_) { fValue_ = f; return 0; } if (isVerbose()) { Rprintf("iter: %d itSoftMax: %d \n", iter, itSoftMax_); } Utils::setulb(xSize, m, &xBuffer_[0], &lower_[0], &upper_[0], &nbd[0], &f, &g_[0], factr_, &pgTol_, wa, iwa, task, tr, lsave, isave, dsave); iter++; if (strncmp(task, "FG", 2) == 0) { f = fObjective(xBuffer_); // Check if we have reach the threshold if (knowRealEnergy_) { canstop = f <= realEnergyThreshold_; if (canstop) { if (isVerbose()) { Rprintf( "Have got accurate energy %.10g <= %.10g in smooth search\n", f, realEnergyThreshold_); } fValue_ = f; return 0; } } gradient(); //Rprintf("LS TRACE: fValue: %.10g gradient: %.10g\n", f, g_[0]); goto L111; } if (strncmp(task, "NEW_X", 5) == 0) { goto L111; } // We should stop here fValue_ = f; if (fValue_ >= BIG_VALUE) { return -1; } return 0; }
int Engine::initialize() { // For Tracing std::vector < std::string > keys; keys.push_back("currentEnergy"); keys.push_back("minEnergy"); keys.push_back("nSteps"); keys.push_back("temperature"); tracer_.clear(); tracer_.setKeyList(keys); if (isVerbose()) { Rprintf("Initialization...\n"); } // Init x related vectors try { xRange_.resize(x_.size()); xBackup_.resize(x_.size()); xMini_.resize(x_.size()); xBuffer_.resize(x_.size()); g_.resize(x_.size()); } catch (std::length_error& le) { Rprintf("Engine: Length error: %s\n",le.what()); } itSoftMax_ = x_.size() * 6; factr_ = 1000; pgTol_ = 1.e-6; reps_ = 1.e-6; nbFctCall_ = 0; idum_ = -100377; indTrace_ = 0; // Check markov chain length if (0 != markovLength_ % x_.size()) { Rprintf( "LMarkov should be size of 'x' (recommended) or 2*n or 3*n ... since component.change is 1\n"); return -1; } // if (lsEnd_) // { // markovLength_ = 200 * x_.size(); // if (markovLength_ < 1000) // { // markovLength_ = 1000; // } // else if (markovLength_ > 10000) // { // markovLength_ = 10000; // } // } if (isVerbose()) { Rprintf("LMarkov= %i\n", markovLength_); } for (unsigned int i = 0; i < x_.size(); ++i) { xRange_[i] = upper_[i] - lower_[i]; } if (isVerbose()) { Rprintf("xrange: "); printVect(xRange_); } // Check if starting point is in constraint bool inConstraint = true; bool initError = true; unsigned int reinitCount = 0; while (initError) { if (inConstraint) { if (hasConstraint_) { inConstraint = judgeConstraint(); while (!inConstraint) { coordin(idum_, x_); inConstraint = judgeConstraint(); } } } if (isVerbose()) { Rprintf("The random intial x coordinates:\n"); printVect(x_); } energy(x_); if (isVerbose()) { Rprintf("The energy of initial x = %.10g\n", etot_); } if (etot_ >= BIG_VALUE) { if (isVerbose()) { Rprintf("x: "); printVect(x_); Rprintf( " give NaN, NA, or inf, generating new starting point\n"); } if (reinitCount >= MAX_REINIT_COUNT) { Rprintf("Stopping algorithm because function to optimize create NaN or (+/-) infinity values even with trying new random parameters"); return -1; } double rd = 0; for (unsigned int i=0; i < x_.size(); ++i) { // lower + runif(length(lower))*(upper-lower) rd = Utils::ran2(&idum_); x_[i] = lower_[i] + rd * (upper_[i] - lower_[i]); } reinitCount++; } else { initError = false; } } return 0; }
int Engine::startSearch() { if (isVerbose()) { Rprintf("Starting...\n"); } double temQa; double visit, a, b; int itNew = 0, itDev; double s1, s, t1, t2, r, pqa, pqa1; bool inConstraint = false; bool eMini_NotChanged = true; double eMiniMarkov = 0; int indexNoEminiUpdate = 0; int indexTolEminiUpdate = 1000; dVec xMiniMarkov(x_.size()); if (getIsSimpleFunction()) { indexTolEminiUpdate = x_.size(); } // if (x_.size() <= 2) // { // indexTolEminiUpdate = 3; // } // else if (x_.size() > 2 && x_.size() <= 4) // { // indexTolEminiUpdate = 4 * x_.size(); // } // else if (x_.size() > 4 && x_.size() <= 10) // { // indexTolEminiUpdate = 4 * x_.size(); // } startTime_ = clock(); eMini_ = etot_; xMini_ = x_; etot0_ = etot_; if (isVerbose()) { Rprintf("first time, ind_trace is: %i\n", indTrace_); } // Initialize etot0 and temp if (!lsEnd_) { etot_ = lsEnergy(x_); if (etot_ < eMini_) { eMini_ = etot_; xMini_ = x_; } ++indTrace_; // Do the tracing here tracer_.addValue("currentEnergy", etot0_); tracer_.addValue("minEnergy", eMini_); tracer_.addValue("nSteps", itNew); tracer_.addValue("temperature", temSta_); } if (etot_ < eMini_) { eMini_ = etot_; xMini_ = x_; } etot0_ = etot_; tem_ = temSta_; if (isVerbose()) { Rprintf("The transformed xinitial x: \n"); printVect(x_); Rprintf("The energy of transformed initial x = %.10g\n", etot_); } if (isVerbose()) { Rprintf("At the beginning, etot0= %.10g\n", etot0_); Rprintf("Emini= %.10g\n", eMini_); Rprintf("Current x: "); printVect(x_); Rprintf("Current xmini: "); printVect(xMini_); } if (checkStoping()) { stopSearch(); return 0; } if (isVerbose()) { Rprintf("Number of function call: %i\n", nbFctCall_); } int stepRecord = 0; L2435: // Main loop for (int i = 0; i < maxStep_; ++i) { itNew = i + 1; s1 = (double) itNew; s = s1 + 1.; t1 = exp((qv_ - 1.) * log(2.)) - 1.; t2 = exp((qv_ - 1.) * log(s)) - 1.; tem_ = temSta_ * t1 / t2; stepRecord += 1; if (stepRecord == maxStep_) { break; } if (tem_ < temRestart_) { //printf("*\n"); goto L2435; } temQa = tem_ / (double) itNew; indexNoEminiUpdate++; // Markov loop for (unsigned int j = 0; j < (unsigned) markovLength_; ++j) { if (j == 0) { eMini_NotChanged = true; } if (j == 0 && i == 0) { eMini_NotChanged = false; } xBackup_ = x_; inConstraint = false; while (!inConstraint) { // Loop on coordinates if (j < x_.size()) { for (unsigned int k = 0; k < x_.size(); ++k) { if (isVerbose()) { Rprintf("IDUM before visit: %d\n", idum_); } visit = visita(&qv_, &tem_, &idum_); if (visit > 1.e8) { visit = 1.e8 * Utils::ran2(&idum_); } else if (visit < -1.e8) { visit = -1.e8 * Utils::ran2(&idum_); } x_[k] = visit + xBackup_[k]; a = x_[k] - lower_[k]; b = Utils::dMod(&a, &xRange_[k]) + xRange_[k]; x_[k] = Utils::dMod(&b, &xRange_[k]) + lower_[k]; if (fabs(x_[k] - lower_[k]) < 1.e-10) { x_[k] += 1.e-10; } if (isVerbose()) { Rprintf( "visit: %.10g a: %.10g b: %.10g idum: %d x: %.10g\n", visit, a, b, idum_, x_[k]); } } // end coordinates loop } else { // Now change only one component at a time visit = visita(&qv_, &tem_, &idum_); if (visit > 1.e8) { visit = 1.e8 * Utils::ran2(&idum_); } else if (visit < -1.e8) { visit = -1.e8 * Utils::ran2(&idum_); } int index = j - x_.size(); x_[index] = visit + xBackup_[index]; a = x_[index] - lower_[index]; b = Utils::dMod(&a, &xRange_[index]) + xRange_[index]; x_[index] = Utils::dMod(&b, &xRange_[index]) + lower_[index]; if (fabs(x_[index] - lower_[index]) < 1.e-10) { x_[index] += 1.e-10; } } if (isVerbose()) { Rprintf("\ntem: %.10g temqa: %.10g itnew: %d markov j=%d\n", tem_, temQa, itNew, j); Rprintf("fx are: "); printVect(xBackup_); Rprintf("x are: "); printVect(x_); } if (hasConstraint_) { inConstraint = judgeConstraint(); } else { inConstraint = true; } if (inConstraint) { if (lsEnd_) { if (isVerbose()) { Rprintf("Calling energy\n"); } energy(x_); } else { if (isVerbose()) { Rprintf("Calling lsEnergy\n"); } etot_ = lsEnergy(x_); } if (isVerbose()) { Rprintf("Before judge, etot0= %.10g etot= %.10g\n", etot0_, etot_); } if (etot_ < etot0_) { etot0_ = etot_; if (isVerbose()) { Rprintf("etot is smaller than etot0\n"); } if (etot_ < eMini_) { eMini_ = etot_; xMini_ = x_; eMini_NotChanged = false; indexNoEminiUpdate = 0; } } else { r = Utils::ran2(&idum_); pqa1 = (qa_ - 1.) * (etot_ - etot0_) / temQa + 1.; /* write(*,*)' etot0=',etot0,', etot=',etot,', pqa1=',pqa1 ! r */ if (pqa1 < 0.) { pqa = 0.; } else { pqa = exp(log(pqa1) / (1. - qa_)); } if (isVerbose()) { Rprintf("pqa= %.10g r= %.10g \n", pqa, r); } if (r > pqa) { x_ = xBackup_; } else { etot0_ = etot_; } } // endif etot_ < eMini_ tracer_.addValue("currentEnergy", etot0_); tracer_.addValue("minEnergy", eMini_); tracer_.addValue("nSteps", itNew); tracer_.addValue("temperature", tem_); if (checkStoping()) { stopSearch(); return 0; } } // end else hasConstraint } // end while !inconstraint if (indexNoEminiUpdate >= indexTolEminiUpdate - 1) { if (j == 0) { eMiniMarkov = etot0_; std::copy(x_.begin(), x_.end(), xMiniMarkov.begin()); } else { if (etot0_ < eMiniMarkov) { eMiniMarkov = etot0_; std::copy(x_.begin(), x_.end(), xMiniMarkov.begin()); } } } } // end markov chain loop if (lsEnd_) { if (!eMini_NotChanged) { dVec temp(x_.size()); std::copy(xMini_.begin(), xMini_.end(), temp.begin()); //Rprintf("Xmini:\n"); //printVect(xMini_); // if (isUserVerbose()) // { // Rprintf("Before lsEnergy, itNew: %d eTemp: %.15g x: %.15g %15g\n", itNew, eMini_, temp[0], temp[1]); // } double eTemp = lsEnergy(temp); // if (isUserVerbose()) // { // Rprintf("lsEnergy called, itNew: %d eTemp: %.15g x: %.15g %.15g\n", itNew, eTemp, temp[0], temp[1]); // } if (eTemp < eMini_) { if (isUserVerbose()) { Rprintf("It: %d, obj value: %.10g\n", itNew, eTemp); } std::copy(temp.begin(), temp.end(), xMini_.begin()); eMini_ = eTemp; indexNoEminiUpdate = 0; tracer_.updateLastValue("currentEnergy", etot0_); tracer_.updateLastValue("minEnergy", eMini_); tracer_.updateLastValue("nSteps", itNew); tracer_.updateLastValue("temperature", tem_); } } if (indexNoEminiUpdate >= indexTolEminiUpdate) { //Rprintf("Before lsEnergy, itNew: %d x: %.15g %.15g\n", itNew, xMiniMarkov[0], xMiniMarkov[1]); eMiniMarkov = lsEnergy(xMiniMarkov); //Rprintf("lsEnergy called, itNew: %d eMiniMarkov: %.15g x: %.15g %.15g\n", itNew, eMiniMarkov, xMiniMarkov[0], xMiniMarkov[1]); if (isUserVerbose()) { Rprintf("."); } indexNoEminiUpdate = 0; indexTolEminiUpdate = x_.size(); if (eMiniMarkov < eMini_) { std::copy(xMiniMarkov.begin(), xMiniMarkov.end(), xMini_.begin()); eMini_ = eMiniMarkov; tracer_.updateLastValue("currentEnergy", etot0_); tracer_.updateLastValue("minEnergy", eMini_); tracer_.updateLastValue("nSteps", itNew); tracer_.updateLastValue("temperature", tem_); if (isUserVerbose()) { Rprintf("\nIt: %d, obj value: %.10g\n", itNew, eMini_); } if (checkStoping()) { stopSearch(); return 0; } } } } itDev = itNew % interval_; if (0 == itDev) { if (isVerbose()) { Rprintf(">After one search %d %.10g %.10g %.10g <-- Emini\n", itNew, tem_, etot0_, eMini_); Rprintf("Current x: "); printVect(x_); Rprintf("\nCurrent xmini: "); printVect(xMini_); Rprintf( "\n__________________________________________________\n"); } } } // end step loop stopSearch(); return 0; }