/* * VsRegistry.cpp * * Created on: Apr 30, 2010 * Author: mdurant */ #include "VsRegistry.h" #include "VsDataset.h" #include "VsGroup.h" #include "VsMesh.h" #include "VsUtils.h" #include "VsLog.h" #include "VsAttribute.h" #include "VsSchema.h" #include "VsVariable.h" #include "VsVariableWithMesh.h" #include "VsMDMesh.h" #include "VsMDVariable.h" #include "VsMesh.h" #include VsRegistry::VsRegistry() { deletingObjects = false; timeValue = -1; cycle = -1; } VsRegistry::~VsRegistry() { deleteAllObjects(); } void VsRegistry::deleteAllObjects() { deletingObjects = true; //Delete the highest-level objects first this->deleteAllMeshes(); this->deleteAllVariables(); this->deleteAllVariablesWithMesh(); //Delete the low-level objects last this->deleteAllDatasets(); this->deleteAllGroups(); deletingObjects = false; } /*********** VsGroups ***********/ void VsRegistry::add(VsGroup* group) { //check for duplicate long name VsGroup* found = this->getGroup(group->getFullName()); if (found) { VsLog::errorLog() <<"VsRegistry::add() - Group already exists with this long name: " <getFullName() <getGroup(group->getShortName()); if (found) { VsLog::warningLog() <<"VsRegistry::add() - Group already exists with this short name: " <getShortName() <getFullName()] = group; allGroupsShort[group->getShortName()] = group; } int VsRegistry::numGroups() { return allGroups.size(); } void VsRegistry::remove(VsGroup* group) { if (deletingObjects) return; allGroups.erase(group->getFullName()); allGroupsShort.erase(group->getShortName()); } VsGroup* VsRegistry::getGroup(const std::string& name) { std::string fullName = makeCanonicalName(name); std::map::iterator it = allGroups.find(fullName); if (it != allGroups.end()) { return (*it).second; } it = allGroupsShort.find(fullName); if (it != allGroupsShort.end()) { return (*it).second; } return NULL; } void VsRegistry::deleteAllGroups() { for (std::map::const_iterator it = allGroups.begin(); it != allGroups.end(); it++) { VsGroup* group = it->second; delete(group); } allGroups.clear(); allGroupsShort.clear(); } void VsRegistry::writeAllGroups() const { VsLog::debugLog() <<"********** BEGIN VsGroups *********" <::const_iterator it = allGroups.begin(); it != allGroups.end(); it++) { VsGroup* group = it->second; group->write(); } VsLog::debugLog() <<"********** END VsGroups *********" <::const_iterator it = allGroups.begin(); it != allGroups.end(); it++) { VsGroup* group = it->second; VsLog::debugLog() <<"VsRegistry::buildGroupObjects - Building object " <getFullName() <getAttribute(VsSchema::typeAtt); if (!typeAtt) { VsLog::warningLog() <<"VsRegistry::buildGroupObjects - unable to find attribute " <getFullName() <getStringValue(&type); VsLog::debugLog() <<"VsRegistry::buildGroupObjects - group is of type " <getAttribute(VsSchema::timeAtt); if (timeAtt) { std::vector in; int err = timeAtt->getFloatVectorValue(&in); if (err < 0) { VsLog::debugLog() <<"VsRegistry::loadTime(): Error " <getAttribute(VsSchema::cycleAtt); if (cycleAtt) { std::vector in; int err = cycleAtt->getIntVectorValue(&in); if (err < 0) { VsLog::debugLog() <<"VsRegistry::loadTime(): Error " <getDataset(dataset->getFullName()); if (found) { VsLog::errorLog() <<"VsRegistry::add() - Dataset already exists with this long name: " <getFullName() <getDataset(dataset->getShortName()); if (found) { VsLog::warningLog() <<"VsRegistry::add() - Dataset already exists with this short name: " <getShortName() <getFullName()] = dataset; allDatasetsShort[dataset->getShortName()] = dataset; } int VsRegistry::numDatasets() { return allDatasets.size(); } void VsRegistry::remove(VsDataset* dataset) { if (deletingObjects) return; allDatasets.erase(dataset->getFullName()); allDatasetsShort.erase(dataset->getShortName()); } VsDataset* VsRegistry::getDataset(const std::string& name) { std::string fullName = makeCanonicalName(name); std::map::iterator it = allDatasets.find(fullName); if (it != allDatasets.end()) { return (*it).second; } it = allDatasetsShort.find(fullName); if (it != allDatasetsShort.end()) { return (*it).second; } return NULL; } void VsRegistry::deleteAllDatasets() { for (std::map::const_iterator it = allDatasets.begin(); it != allDatasets.end(); it++) { VsDataset* dataset = it->second; delete(dataset); } allDatasets.clear(); allDatasetsShort.clear(); } void VsRegistry::writeAllDatasets() const { VsLog::debugLog() <<"********** BEGIN VsDatasets *********" <::const_iterator it = allDatasets.begin(); it != allDatasets.end(); it++) { VsDataset* dataset = it->second; dataset->write(); } VsLog::debugLog() <<"********** END VsDatasets *********" < varDatasets; std::vector varWithMeshDatasets; for (std::map::const_iterator it = allDatasets.begin(); it != allDatasets.end(); it++) { VsDataset* dataset = it->second; VsLog::debugLog() <<"VsRegistry::buildDatasetObjects() - looking at dataset " <getFullName() <getAttribute(VsSchema::typeAtt); if (!typeAtt) { VsLog::warningLog() <<"VsRegistry::buildDatasetObjects() - unable to find attribute " <getAttribute(VsSchema::meshAtt); if (meshAtt) { VsLog::warningLog() <<"VsRegistry::buildDatasetObjects() - Found attribute " <getFullName() <getStringValue(&type); } VsLog::debugLog() <<"VsRegistry::buildDatasetObjects() - object is of type " <::const_iterator it = varDatasets.begin(); it != varDatasets.end(); it++) { VsDataset* dataset = *it; VsVariable* var = VsVariable::buildObject(dataset); if (var && (var->getTimeGroup() != NULL)) { loadTime(var->getTimeGroup()); } } //Second pass to do all variables with mesh for (std::vector::const_iterator it = varWithMeshDatasets.begin(); it != varWithMeshDatasets.end(); it++) { VsDataset* dataset = *it; VsLog::debugLog() <<"VsRegistry::buildDatasetObjects() - looking at varWithMesh dataset " <getFullName() <getTimeGroup() != NULL)) { loadTime(var->getTimeGroup()); } } VsLog::debugLog() <<"VsRegistry::buildDatasetObjects() - Returning." <getMesh(mesh->getFullName()); if (found) { VsLog::errorLog() <<"VsRegistry::add() - Mesh already exists with this long name: " <getFullName() <getMesh(mesh->getShortName()); if (found) { VsLog::warningLog() <<"VsRegistry::add() - Mesh already exists with this short name: " <getShortName() <getFullName()] = mesh; allMeshesShort[mesh->getShortName()] = mesh; } int VsRegistry::numMeshes() { return allMeshes.size(); } void VsRegistry::remove(VsMesh* mesh) { if (deletingObjects) return; allMeshes.erase(mesh->getFullName()); allMeshesShort.erase(mesh->getShortName()); } VsMesh* VsRegistry::getMesh(const std::string& name) { std::string fullName = makeCanonicalName(name); std::map::iterator it = allMeshes.find(fullName); if (it != allMeshes.end()) { return (*it).second; } it = allMeshesShort.find(fullName); if (it != allMeshesShort.end()) { return (*it).second; } return NULL; } void VsRegistry::deleteAllMeshes() { for (std::map::iterator it = allMeshes.begin(); it != allMeshes.end(); it++) { VsMesh* mesh = it->second; delete(mesh); } allMeshes.clear(); allMeshesShort.clear(); } void VsRegistry::writeAllMeshes() const { if (allMeshes.empty()) { VsLog::debugLog() <<"*********** NO MESHES ********" <::const_iterator it = allMeshes.begin(); it != allMeshes.end(); it++) { VsMesh* mesh = it->second; mesh->write(); } VsLog::debugLog() <<"********** END VsMeshs *********" <& names) { std::map::const_iterator it; for (it = allMeshes.begin(); it != allMeshes.end(); ++it) names.push_back(it->first); } /************* MD MESHES **************/ VsMDMesh* VsRegistry::getMDMesh(const std::string& name) { // Make name fully qualified std::string fullName = makeCanonicalName(name); std::map::iterator it = allMDMeshes.find(fullName); if (it != allMDMeshes.end()) { return it->second; } return NULL; } void VsRegistry::add(VsMDMesh* mesh) { //check for duplicate name VsMDMesh* foundMesh = this->getMDMesh(mesh->getFullName()); if (foundMesh) { VsLog::errorLog() <<"VsRegistry::add() - MD Mesh already exists with this long name: " <getFullName() <getFullName()] = mesh; } void VsRegistry::remove(VsMDMesh* mesh) { allMDMeshes.erase(mesh->getFullName()); } void VsRegistry::getAllMDMeshNames(std::vector& names) { std::map::const_iterator it; for (it = allMDMeshes.begin(); it != allMDMeshes.end(); ++it) names.push_back(it->first); } void VsRegistry::writeAllMDMeshes() const { if (allMDMeshes.empty()) { VsLog::debugLog() <<"*********** NO MDMESHES ********" <::const_iterator it; for (it = allMDMeshes.begin(); it != allMDMeshes.end(); ++it) { it->second->write(); } VsLog::debugLog() <<"*********** END MDMESHES ********" <::const_iterator it = allMDMeshes.begin(); it != allMDMeshes.end(); it++) { VsMDMesh* meshMeta = it->second; for (unsigned int i = 0; i < meshMeta->getNumBlocks(); i++) { if (fullName == makeCanonicalName(meshMeta->getBlock(i)->getFullName())) { VsLog::debugLog() << "VsMDMesh::getMDParentForMesh(" <::const_iterator it = allMDMeshes.begin(); it != allMDMeshes.end(); it++) { VsMDMesh* meshMeta = it->second; for (size_t i = 0; i < meshMeta->getNumBlocks(); i++) { if (meshMeta->getBlock(i)->getFullName() == name) { VsLog::debugLog() << "VsReader::findSubordinateMDMesh(" <getBlock(i); } } } VsLog::debugLog() << "VsMDMesh::findSubordinateMDMesh(" <::const_iterator it; for (it = allMeshes.begin(); it != allMeshes.end(); it++) { VsLog::debugLog() <<"VsRegistry::buildMDMeshes() - examining mesh " <<(*it).first <getAttribute(VsSchema::mdAtt); std::string mdMeshName = ""; if (mdMeshNameAttribute) { mdMeshNameAttribute->getStringValue(&mdMeshName); } if (!mdMeshName.empty()) { VsMDMesh* mdMesh = getMDMesh(mdMeshName); //If this is a new md mesh, create it if (mdMesh == NULL) { VsLog::debugLog() <<"VsRegistry::buildMDMeshes() - MD mesh not found with name " <addBlock(mesh); if (!success) { VsLog::debugLog() <<"VsRegistry::buildMDMeshes() - failed to add block to md mesh. Will remain as a stand-alone mesh." <::const_iterator it; for (it = allVariables.begin(); it != allVariables.end(); ++it) { VsLog::debugLog() <<"VsRegistry::buildTransformedVariables() - Creating transform names for Variable " <first <second->createTransformedVariable(); } std::map::const_iterator it2; for (it2 = allVariablesWithMesh.begin(); it2 != allVariablesWithMesh.end(); ++it2) { VsLog::debugLog() <<"VsRegistry::buildTransformedVariables() - Creating transform names for Variable with Mesh " <first <second->createTransformedVariableAndMesh(); } } /*********** TRANSFORMED MESHES *******************************/ void VsRegistry::buildTransformedMeshes() { //go through all meshes looking for "vsTransform" flags VsLog::debugLog() <<"VsRegistry::buildTransformedMeshes() - Entering." <::const_iterator it; for (it = allMeshes.begin(); it != allMeshes.end(); it++) { VsLog::debugLog() <<"VsRegistry::buildTransformedMeshes() - examining mesh " <<(*it).first <hasTransform()) { std::string transformedMeshName = mesh->getTransformedMeshName(); VsLog::debugLog() <<"VsRegistry::buildTransformedMeshes() - mesh " <<(*it).first <<" has transformed mesh " <getFullName()); } } VsLog::debugLog() <<"VsRegistry::buildTransformedMeshes() - Returning." <getVariable(variable->getFullName()); if (foundVariable) { VsLog::errorLog() <<"VsRegistry::add() - Variable already exists with this long name: " <getFullName() <getVariable(variable->getShortName()); if (foundVariable) { VsLog::warningLog() <<"VsRegistry::add() - Variable already exists with this short name: " <getShortName() <getFullName()] = variable; allVariablesShort[variable->getShortName()] = variable; } int VsRegistry::numVariables() { return allVariables.size(); } void VsRegistry::remove(VsVariable* variable) { if (deletingObjects) return; allVariables.erase(variable->getFullName()); allVariablesShort.erase(variable->getShortName()); } VsVariable* VsRegistry::getVariable(const std::string& name) { std::string fullName = makeCanonicalName(name); std::map::iterator it = allVariables.find(fullName); if (it != allVariables.end()) { return (*it).second; } it = allVariablesShort.find(fullName); if (it != allVariablesShort.end()) { return (*it).second; } return NULL; } void VsRegistry::deleteAllVariables() { for (std::map::const_iterator it = allVariables.begin(); it != allVariables.end(); it++) { VsVariable* variable = it->second; delete(variable); } allVariables.clear(); allVariablesShort.clear(); } void VsRegistry::writeAllVariables() const { if (allVariables.empty()) { VsLog::debugLog() <<"*********** NO VSVARIABLES ********" <::const_iterator it = allVariables.begin(); it != allVariables.end(); it++) { VsVariable* variable = it->second; variable->write(); } VsLog::debugLog() <<"********** END VsVariables *********" <& names) { std::map::const_iterator it; for (it = allVariables.begin(); it != allVariables.end(); ++it) names.push_back(it->first); } /*********** MD VARIABLES ***************/ VsMDVariable* VsRegistry::getMDVariable(const std::string& name) { // Make name fully qualified std::string fullName = makeCanonicalName(name); std::map::iterator it = allMDVariables.find(fullName); if (it != allMDVariables.end()) { return it->second; } return NULL; } void VsRegistry::add(VsMDVariable* var) { //check for duplicate long name VsMDVariable* foundVariable = this->getMDVariable(var->getFullName()); if (foundVariable) { VsLog::errorLog() <<"VsRegistry::add() - MD Variable already exists with this long name: " <getFullName() <getFullName()] = var; } void VsRegistry::remove(VsMDVariable* var) { allMDVariables.erase(var->getFullName()); } void VsRegistry::getAllMDVariableNames(std::vector& names) { std::map::const_iterator it; for (it = allMDVariables.begin(); it != allMDVariables.end(); ++it) names.push_back(it->first); } void VsRegistry::writeAllMDVariables() const { if (allMDVariables.empty()) { VsLog::debugLog() <<"*********** NO MDVARIABLES ********" <::const_iterator it; for (it = allMDVariables.begin(); it != allMDVariables.end(); ++it) { it->second->write(); } VsLog::debugLog() <<"*********** END MDVARIABLES ********" <::const_iterator it; for (it = allVariables.begin(); it != allVariables.end(); it++) { VsVariable* var = (*it).second; VsAttribute* mdVarAtt = var->getAttribute(VsSchema::mdAtt); if (!mdVarAtt) { continue; } std::string mdVarName; mdVarAtt->getStringValue(&mdVarName); if (!mdVarName.empty()) { VsMDVariable* mdVar = getMDVariable(mdVarName); if (!mdVar) { VsLog::debugLog() <<"VsRegistry::buildMDVars() - MD variable " + mdVarName + " not found, creating." <getMeshName()); //did we find the appropriate md mesh? if (!mdMesh) { VsLog::debugLog() <<"VsRegistry::buildMDVars() - WARNING: variable " + var->getFullName() + " is declared as part of MD Var " + mdVarName + " but referenced mesh " + var->getMeshName() + " is not part of an MD mesh." <getFullName() <addBlock(var); if (!success) { VsLog::warningLog() <<"VsRegistry::buildMDVars() - unable to add var to MD var, will remain stand-alone" <::const_iterator it = allMDVariables.begin(); it != allMDVariables.end(); it++) { VsMDVariable* varMeta = it->second; for (unsigned int i = 0; i < varMeta->getNumBlocks(); i++) { if (varMeta->getBlock(i)->getFullName() == name) { VsLog::debugLog() << "VsRegistry::findSubordinateMDVar(" <getBlock(i); } } } VsLog::debugLog() << "VsRegistry::findSubordinateMDVar(" <getVariableWithMesh(variable->getFullName()); if (foundVariable) { VsLog::errorLog() <<"VsRegistry::add() - Variable with mesh already exists with this full name: " <getFullName() <getVariableWithMesh(variable->getShortName()); if (foundVariable) { VsLog::errorLog() <<"VsRegistry::add() - Variable with mesh already exists with this short name: " <getShortName() <getFullName()] = variable; allVariablesWithMeshShort[variable->getShortName()] = variable; } int VsRegistry::numVariablesWithMesh() { return allVariablesWithMesh.size(); } void VsRegistry::remove(VsVariableWithMesh* variable) { if (deletingObjects) return; allVariablesWithMesh.erase(variable->getFullName()); allVariablesWithMeshShort.erase(variable->getShortName()); } VsVariableWithMesh* VsRegistry::getVariableWithMesh(const std::string& name) { std::string fullName = makeCanonicalName(name); std::map::iterator it = allVariablesWithMesh.find(fullName); if (it != allVariablesWithMesh.end()) { return (*it).second; } it = allVariablesWithMeshShort.find(fullName); if (it != allVariablesWithMeshShort.end()) { return (*it).second; } return NULL; } void VsRegistry::deleteAllVariablesWithMesh() { for (std::map::const_iterator it = allVariablesWithMesh.begin(); it != allVariablesWithMesh.end(); it++) { VsVariableWithMesh* variable = it->second; delete (variable); } allVariablesWithMesh.clear(); allVariablesWithMeshShort.clear(); } void VsRegistry::writeAllVariablesWithMesh() const { if (allVariablesWithMesh.empty()) { VsLog::debugLog() <<"********** NO VsVariablesWithMesh *********" <::const_iterator it = allVariablesWithMesh.begin(); it != allVariablesWithMesh.end(); it++) { VsVariableWithMesh* variable = it->second; variable->write(); } VsLog::debugLog() <<"********** END VsVariablesWithMesh *********" <& names) { std::map::const_iterator it; for (it = allVariablesWithMesh.begin(); it != allVariablesWithMesh.end(); ++it) names.push_back(it->first); } /********************* TRANSFORMED MESH NAMES ************************/ bool VsRegistry::registerTransformedMeshName(std::string transformedName, std::string origName) { //first, look for a match and report failure if the name is already registered std::string oName = getOriginalMeshName(transformedName); if (!oName.empty()) { if (origName != oName) { VsLog::debugLog() << "ERROR VsRegistry::registerTransformedMeshName() - " << transformedName << " is already registered to " << oName << std::endl; return false; } else { VsLog::debugLog() << "VsRegistry::registerTransformedMeshName() - received duplicate registration for " << origName << std::endl; VsLog::debugLog() << "VsRegistry::registerTransformedMeshName() - but all info matches, so it should be ok" << std::endl; return true; } } // Ok, register the new name mapping transformedMeshNames[transformedName] = origName; VsLog::debugLog() << "VsRegistry::registerTransformedMeshName(" << transformedName << ", " << transformedMeshNames[transformedName] << ") - registration succeeded." << std::endl; return true; } std::string VsRegistry::getOriginalMeshName(std::string transformedMeshName) { // return the value if the name is registered return transformedMeshNames[transformedMeshName]; } /******************* TRANSFORMED VARS *******************/ bool VsRegistry::registerTransformedVarName(std::string transformedName, std::string origName) { //first, look for a match and report failure if the name is already registered std::string oName = getOriginalVarName(transformedName); if (!oName.empty()) { if (origName != oName) { VsLog::debugLog() << "ERROR VsRegistry::registerTransformedVarName() - " << transformedName << " is already registered to " << oName << std::endl; return false; } else { VsLog::debugLog() << "VsRegistry::registerTransformedVarName() - received duplicate registration for " << origName << std::endl; VsLog::debugLog() << "VsRegistry::registerTransformedVarName() - but all info matches, so it should be ok" << std::endl; return true; } } // Ok, register the new name mapping transformedVarNames[transformedName] = origName; VsLog::debugLog() << "VsRegistry::registerTransformedVarName(" << transformedName << ", " << transformedVarNames[transformedName] << ") - registration succeeded." << std::endl; return true; } std::string VsRegistry::getOriginalVarName(std::string transformedVarName) { // return the value if the name is registered return transformedVarNames[transformedVarName]; } /******************* EXPRESSIONS ************************/ void VsRegistry::addExpression(const std::string& name, const std::string& value) { //check for duplicates std::map::const_iterator it; it = allExpressions.find(name); if (it != allExpressions.end()) { VsLog::errorLog() <<"VsRegistry::addExpression(" <getFullName() <::const_iterator k; for (k = group->getAllAttributes().begin(); k != group->getAllAttributes().end(); ++k) { VsAttribute* att = (*k).second; if (att->getShortName() != VsSchema::typeAtt) { std::string s; int err = att->getStringValue(&s); if (err == 0) { addExpression(att->getShortName(), s); } } } VsLog::debugLog() <<"VsRegistry::buildExpressions() exiting." <::const_iterator it; for (it = allExpressions.begin(); it != allExpressions.end(); it++) { VsLog::debugLog() <<"(" <<(*it).first <<", " <<(*it).second <<")" <* VsRegistry::getAllExpressions() { return &allExpressions; } int VsRegistry::numExpressions() { return allExpressions.size(); } void VsRegistry::createComponents() { VsLog::debugLog() <<"VsRegistry::createComponents() - Entering" <::const_iterator it; for (it = allMDVariables.begin(); it != allMDVariables.end(); ++it) { VsLog::debugLog() <<"VsRegistry::createComponents() - Creating components for MD Variable " <first <second->createComponents(); } std::map::const_iterator it2; for (it2 = allVariables.begin(); it2 != allVariables.end(); ++it2) { VsLog::debugLog() <<"VsRegistry::createComponents() - Creating components for Variable " <first <second->createComponents(); } std::map::const_iterator it3; for (it3 = allVariablesWithMesh.begin(); it3 != allVariablesWithMesh.end(); ++it3) { VsLog::debugLog() <<"VsRegistry::createComponents() - Creating components for Variable With Mesh " <first <second->createComponents(); } VsLog::debugLog() <<"VsRegistry::createComponents() - Returning." < innerPair; innerPair.first = varName; innerPair.second = componentNumber; std::pair > newPair; newPair.first = componentName; newPair.second = innerPair; componentNames.push_back(newPair); //VsLog::debugLog() <<"newpair.first = " < foundPair = componentNames[i]; if (foundPair.first == componentName) { VsLog::debugLog() <<"VsReader::getComponentInfo(" <first = foundPair.second.first; namePair->second = foundPair.second.second; return; } } namePair->first = ""; namePair->second = -1; } std::string VsRegistry::getComponentName(const std::string& varName, int componentNumber) { for (unsigned int i = 0; i < componentNames.size(); i++) { std::pair foundPair = componentNames[i]; NamePair tempNamePair = foundPair.second; if ((tempNamePair.first == varName) && (tempNamePair.second == componentNumber)) { VsLog::debugLog() <<"VsReader::getComponentInfo(" < foundPair = componentNames[i]; tempNamePair = foundPair.second; if ((tempNamePair.first == varName) && (tempNamePair.second == componentNumber)) { VsLog::debugLog() <<"VsReader::getComponentInfo(" <first = tempNamePair.first; namePair->second = tempNamePair.second; return; } } VsLog::debugLog() <<"VsReader::getComponentInfo(" <first = ""; namePair->second = -1; } std::string VsRegistry::getOldComponentName(const std::string& varName, int componentIndex) { //generates an old-style name for the component //of the form "varName_index" std::string compName = varName; std::stringstream ss; ss << componentIndex; compName.append("_"); compName.append(ss.str()); return compName; }