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

Method compute_arguments

src/cli/MeasureManager.cpp:787–849  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

785}
786
787MeasureManagerServer::ResponseType MeasureManagerServer::compute_arguments(const web::json::value& body) {
788 openstudio::path measureDir;
789 if (boost::optional<openstudio::path> p_ = get_field<openstudio::path>(body, "measure_dir")) { // Not passing a default value => optional
790 measureDir = std::move(*p_);
791 } else {
792 return {web::http::status_codes::BadRequest, toWebJSON("The 'measure_dir' (string) must be in the post data")};
793 }
794
795 // NOTE: this endpoint expects an OSM (even if it's an EnergyPlusMeasure), NOT an OSM or IDF like the CLI --compute_arguments flag does
796 const openstudio::path osmPath = get_field<openstudio::path>(body, "osm_path", {}); // defaults to an empty path
797 bool has_valid_osm_path = false;
798 if (!osmPath.empty()) {
799 if (osmPath.extension() != ".osm") {
800 auto msg =
801 fmt::format("For /compute_arguments endpoint, parameter 'osm_path' must always be an '.osm' file, not '{}'", osmPath.generic_string());
802 fmt::print(stderr, "{}\n", msg);
803 return {web::http::status_codes::BadRequest, toWebJSON(msg)};
804 }
805 has_valid_osm_path = true;
806 }
807
808 const bool force_reload = get_field<bool>(body, "force_reload", false);
809
810 auto measure_ = m_measureManager.getMeasure(measureDir, force_reload);
811 if (!measure_) {
812 auto msg = fmt::format("Cannot load measure at '{}'", measureDir.generic_string());
813 fmt::print(stderr, "{}\n", msg);
814 return {web::http::status_codes::BadRequest, toWebJSON(msg)};
815 }
816
817 boost::optional<model::Model> model_;
818 boost::optional<Workspace> workspace_;
819
820 if (has_valid_osm_path) {
821 if (auto osmInfo_ = m_measureManager.getModel(osmPath, force_reload)) {
822 // Clone and keep handles
823 model_ = osmInfo_->model.clone(true).cast<openstudio::model::Model>();
824 workspace_ = osmInfo_->workspace.clone(true);
825 } else {
826 auto msg = fmt::format("Cannot load model at '{}'", osmPath.generic_string());
827 fmt::print(stderr, "{}\n", msg);
828 return {web::http::status_codes::BadRequest, toWebJSON(msg)};
829 }
830 }
831
832 const openstudio::measure::OSMeasureInfo info = m_measureManager.getMeasureInfo(measureDir, *measure_, osmPath, model_, workspace_);
833 if (auto errorString_ = info.error()) {
834 return {web::http::status_codes::OK, toWebJSON(*errorString_)};
835 }
836
837 // TODO: maybe I should write an OSMeasureInfo::toJSON() method, but that'd be duplicating the code in BCLMeasure (BCLXML to be exact).
838 // So since the only thing that's different is the OSArgument (OSMeasureInfo) versus BCLMeasureArgument (BCLMeasure), we just override
839 auto result = measure_->toJSON();
840 if (has_valid_osm_path) {
841 auto& arguments = result["arguments"];
842 arguments.clear();
843 for (const measure::OSArgument& argument : info.arguments()) {
844 arguments.append(argument.toJSON());

Callers

nothing calls this directly

Calls 11

toWebJSONFunction · 0.85
extensionMethod · 0.80
getModelMethod · 0.80
getMeasureInfoMethod · 0.80
emptyMethod · 0.45
getMeasureMethod · 0.45
cloneMethod · 0.45
errorMethod · 0.45
toJSONMethod · 0.45
clearMethod · 0.45
argumentsMethod · 0.45

Tested by

no test coverage detected