MCPcopy Create free account
hub / github.com/NatLabRockies/OpenStudio / updateMeasureScript

Method updateMeasureScript

src/utilities/bcl/BCLMeasure.cpp:817–874  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

815}
816
817bool BCLMeasure::updateMeasureScript(const MeasureType& oldMeasureType, const MeasureType& newMeasureType, const MeasureLanguage& oldMeasureLanguage,
818 const MeasureLanguage& newMeasureLanguage, const std::string& oldClassName, const std::string& newClassName,
819 const std::string& name, const std::string& description, const std::string& modelerDescription) {
820 if (oldMeasureLanguage != newMeasureLanguage) {
821 LOG_AND_THROW("Cannot update a measure after a Language Change from " << oldMeasureLanguage.valueName() << " to "
822 << newMeasureLanguage.valueName());
823 }
824 // TODO: deal with Python
825 boost::optional<openstudio::path> rubyScriptPath_ = primaryRubyScriptPath();
826 if (rubyScriptPath_ && exists(*rubyScriptPath_)) {
827
828 openstudio::filesystem::ifstream file(*rubyScriptPath_, std::ios_base::binary);
829 if (file.is_open()) {
830
831 // Replacement paterns
832 std::string nameFunction = "$1def name\n$1 return \"" + name + "\"\n$1end";
833 std::string descriptionFunction = "$1def description\n$1 return \"" + description + "\"\n$1end";
834 std::string modelerDescriptionFunction = "$1def modeler_description\n$1 return \"" + modelerDescription + "\"\n$1end";
835
836 std::string fileString = openstudio::filesystem::read_as_string(file);
837
838 boost::regex re;
839
840 if (oldMeasureType != newMeasureType) {
841 boost::replace_all(fileString, oldMeasureType.valueName(), newMeasureType.valueName());
842 }
843
844 if (!oldClassName.empty() && !newClassName.empty() && oldClassName != newClassName) {
845 // DLM: might also want to check that oldClassName is greater than 3 characters long?
846 // should we be doing a more selective replace (like require leading space and trailing space, ., or :)?
847 boost::replace_all(fileString, oldClassName, newClassName);
848 }
849
850 // Note JM 2018-11-06: Use the iterator overload of regex_replace for inplace replacement? Actually don't.
851 // There's undefined behavior when you end up replacing with something that doesn't have the same length
852 re.set_expression("^([\t ]*?)def\\s+name(.*?)end[\\s#]?$");
853 fileString = boost::regex_replace(fileString, re, nameFunction);
854
855 re.set_expression("^([\t ]*?)def\\s+description(.*?)end[\\s#]?$");
856 fileString = boost::regex_replace(fileString, re, descriptionFunction);
857
858 re.set_expression("^([\t ]*?)def\\s+modeler_description(.*?)end[\\s#]?$");
859 fileString = boost::regex_replace(fileString, re, modelerDescriptionFunction);
860
861 file.close();
862
863 openstudio::filesystem::ofstream ofile(*rubyScriptPath_, std::ios_base::binary);
864 if (ofile.is_open()) {
865 ofile << fileString;
866 // openstudio::filesystem::write(ofile, fileString);
867 ofile.close();
868 return true;
869 }
870 }
871 }
872
873 return false;
874}

Callers 2

do_POSTMethod · 0.80
duplicate_measureMethod · 0.80

Calls 4

read_as_stringFunction · 0.85
valueNameMethod · 0.80
emptyMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected