TEST_F(AnalysisDriverFixture,SimpleProject_InsertMeasure) { // create a new project SimpleProject project = getCleanSimpleProject("SimpleProject_InsertMeasure"); // open a measure openstudio::path dir = resourcesPath() / toPath("/utilities/BCL/Measures/SetWindowToWallRatioByFacade"); BCLMeasure measure = BCLMeasure::load(dir).get(); // insert it into the project BCLMeasure projectMeasure = project.insertMeasure(measure); // verify that there is a project-specific copy EXPECT_NE(toString(measure.directory()),toString(projectMeasure.directory())); openstudio::path scriptsDir = project.projectDir() / toPath("scripts"); EXPECT_EQ(toString(completeAndNormalize(scriptsDir)), toString(completeAndNormalize(projectMeasure.directory().parent_path()))); EXPECT_EQ(measure.uuid(),projectMeasure.uuid()); EXPECT_EQ(measure.versionUUID(),projectMeasure.versionUUID()); // DLM: should this be not equal? // verify that it can be retrieved from the project, but its arguments cannot ASSERT_TRUE(project.getMeasureByUUID(measure.uuid())); BCLMeasure temp = project.getMeasureByUUID(measure.uuid()).get(); EXPECT_EQ(toString(projectMeasure.directory()),toString(temp.directory())); EXPECT_EQ(projectMeasure.uuid(),temp.uuid()); EXPECT_EQ(projectMeasure.versionUUID(),temp.versionUUID()); EXPECT_EQ(1u,project.measures().size()); // use embedded Ruby to extract the arguments // (see AnalysisDriverFixture.cpp for interpreter set-up) OSArgumentVector args = argumentGetter->getArguments(projectMeasure, project.seedModel(), project.seedIdf()); EXPECT_FALSE(args.empty()); // register and subsequently retrieve those arguments project.registerArguments(measure,args); // doesn't matter which copy of the measure we use EXPECT_TRUE(project.hasStoredArguments(temp)); OSArgumentVector argsCopy = project.getStoredArguments(projectMeasure); ASSERT_EQ(args.size(),argsCopy.size()); for (int i = 0, n = args.size(); i < n; ++i) { EXPECT_EQ(args[i].name(),argsCopy[i].name()); EXPECT_EQ(args[i].type(),argsCopy[i].type()); } // use this measure to create a new variable/ruby measure // now it is important to use the copy in the project MeasureGroup dv("New Measure Group",MeasureVector()); Problem problem = project.analysis().problem(); problem.push(dv); // here, expect this test to pass. // in pat, if it fails, let user know and call problem.erase(dv). EXPECT_TRUE(problem.fileTypesAreCompatible(dv, projectMeasure.inputFileType(), projectMeasure.outputFileType())); EXPECT_TRUE(dv.push(RubyMeasure(projectMeasure))); }
boost::optional<BCLMeasure> WorkflowJSON_Impl::addMeasure(const BCLMeasure& bclMeasure) { boost::optional<BCLMeasure> existingMeasure = getBCLMeasureByUUID(bclMeasure.uuid()); boost::optional<openstudio::path> existingMeasureDirName; if (existingMeasure){ // TODO: check that measure type has not changed? existingMeasureDirName = existingMeasure->directory(); boost::filesystem::remove_all(*existingMeasureDirName); } std::vector<openstudio::path> paths = absoluteMeasurePaths(); OS_ASSERT(!paths.empty()); // Get the name of the directory (=last level directory name), eg: /path/to/measure_folder => measure_folder openstudio::path lastLevelDirectoryName = getLastLevelDirectoryName( bclMeasure.directory() ); if (!toUUID(toString(lastLevelDirectoryName)).isNull()){ // directory name is convertible to uuid, use the class name lastLevelDirectoryName = toPath(bclMeasure.className()); } int i = 1; while (boost::filesystem::exists(paths[0] / lastLevelDirectoryName)){ std::stringstream ss; ss << toString(lastLevelDirectoryName) << " " << i; lastLevelDirectoryName = toPath(ss.str()); } openstudio::path newMeasureDirName = paths[0] / lastLevelDirectoryName; // If we have an existing measure if( existingMeasureDirName) { openstudio::path lastLevelExistingDirectoryName = getLastLevelDirectoryName(*existingMeasureDirName); // And the previous directory name isn't the same as the new one if( lastLevelExistingDirectoryName != lastLevelDirectoryName ) { // update steps for (auto& step : m_steps){ if (auto measureStep = step.optionalCast<MeasureStep>()){ if (measureStep->measureDirName() == toString(lastLevelExistingDirectoryName) ) { measureStep->setMeasureDirName(toString(lastLevelDirectoryName)); } } } } } onUpdate(); return bclMeasure.clone(newMeasureDirName); }
bool RubyMeasure_Impl::setMeasure(const BCLMeasure& bclMeasure) { FileReferenceType inputFileType = bclMeasure.inputFileType(); FileReferenceType outputFileType = bclMeasure.outputFileType(); if (!fileTypesAreCompatible(inputFileType,outputFileType)) { return false; } m_bclMeasure = bclMeasure; m_bclMeasureDirectory = bclMeasure.directory(); m_bclMeasureUUID = bclMeasure.uuid(); m_bclMeasureVersionUUID = bclMeasure.versionUUID(); m_perturbationScript.reset(); m_inputFileType = inputFileType; m_outputFileType = outputFileType; m_isUserScript = false; clearArguments(); // emits onChange(AnalysisObject_Impl::InvalidatesResults) return true; }
bool RubyMeasure_Impl::updateMeasure(const BCLMeasure& newVersion, std::vector<ruleset::OSArgument> newArguments) { if (!usesBCLMeasure() || (newVersion.uuid() != m_bclMeasureUUID.get())) { return false; } FileReferenceType inputFileType = newVersion.inputFileType(); FileReferenceType outputFileType = newVersion.outputFileType(); if (!fileTypesAreCompatible(inputFileType,outputFileType)) { return false; } m_bclMeasure = newVersion; m_bclMeasureDirectory = newVersion.directory(); OS_ASSERT(m_bclMeasureUUID.get() == newVersion.uuid()); m_bclMeasureVersionUUID = newVersion.versionUUID(); m_inputFileType = inputFileType; m_outputFileType = outputFileType; m_isUserScript = false; // preserve current argument values // otherwise, prefer newArguments definitions and order OSArgumentVector argsToSet; OSArgumentVector currentArgs = arguments(); BOOST_FOREACH(const OSArgument& newArg,newArguments) { OSArgument newArgClone = newArg.clone(); // look for current value NameFinder<OSArgument> finder(newArgClone.name(),true); ruleset::OSArgumentVector::iterator it = std::find_if(currentArgs.begin(), currentArgs.end(), finder); if (it != currentArgs.end()) { // already exists--try to preserve value if (it->hasValue()) { bool setResult(false); switch(it->type().value()) { case OSArgumentType::Boolean : setResult = newArgClone.setValue(it->valueAsBool()); break; case OSArgumentType::Double : setResult = newArgClone.setValue(it->valueAsDouble()); break; case OSArgumentType::Quantity : setResult = newArgClone.setValue(it->valueAsQuantity()); break; case OSArgumentType::Integer : setResult = newArgClone.setValue(it->valueAsInteger()); break; default : break; } if (!setResult) { // try string conversion by default or if failed setResult = newArgClone.setValue(it->valueAsString()); if (!setResult) { LOG(Info,"Unable to preserve argument value for " << *it << " during updateMeasure operation on RubyMeasure named " << name() << " with measure " << newVersion.name() << " and new argument " << newArgClone); } } } } argsToSet.push_back(newArgClone); }