Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a swig out typemap for Json::Value + openstudio::path typemap in/out for Python + add toJSON() for Workfow files and BCLMeasure #4917

Merged
merged 11 commits into from
Jun 30, 2023

Conversation

jmarrec
Copy link
Collaborator

@jmarrec jmarrec commented Jun 14, 2023

Pull request overview

  • Converts the Json::Value
    • Ruby: to a hash, with symbols as keys (not sure that's what we really want or not)
    • Python: a dict.
    • CSharp: ignored (for now at least)

Context: As part of the rewrite of Measure Manager to C++, I'm going to be using Json::Value outputs a lot more than currently. I'm just trying to isolate changes for easier review.

Test code:

ruby:

Products/openstudio labs -e "m = OpenStudio::Model::Model.new; ft = OpenStudio::EnergyPlus::ForwardTranslator.new; ft.setExcludeLCCObjects(true); w = ft.translateModel(m); d = OpenStudio::EPJSON::toJSON(w); puts d.class; require 'json'; puts JSON.pretty_generate(d)"

python:

Products/openstudio labs -c "m = openstudio.model.Model(); z = openstudio.model.ThermalZone(m); ft = openstudio.energyplus.ForwardTranslator(); ft.setExcludeLCCObjects(True); w = ft.translateModel(m); d = openstudio.epjson.toJSON(w); print(type(d)); import json; print(json.dumps(d, indent=2))"

Pull Request Author

  • Model API Changes / Additions
  • Any new or modified fields have been implemented in the EnergyPlus ForwardTranslator (and ReverseTranslator as appropriate)
  • Model API methods are tested (in src/model/test)
  • EnergyPlus ForwardTranslator Tests (in src/energyplus/Test)
  • If a new object or method, added a test in NREL/OpenStudio-resources: Add Link
  • If needed, added VersionTranslation rules for the objects (src/osversion/VersionTranslator.cpp)
  • Verified that C# bindings built fine on Windows, partial classes used as needed, etc.
  • All new and existing tests passes
  • If methods have been deprecated, update rest of code to use the new methods

Labels:

  • If change to an IDD file, add the label IDDChange
  • If breaking existing API, add the label APIChange
  • If deemed ready, add label Pull Request - Ready for CI so that CI builds your PR

Review Checklist

This will not be exhaustively relevant to every PR.

  • Perform a Code Review on GitHub
  • Code Style, strip trailing whitespace, etc.
  • All related changes have been implemented: model changes, model tests, FT changes, FT tests, VersionTranslation, OS App
  • Labeling is ok
  • If defect, verify by running develop branch and reproducing defect, then running PR and reproducing fix
  • If feature, test running new feature, try creative ways to break it
  • CI status: all green or justified

@jmarrec jmarrec added Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge. component - Ruby bindings component - Python bindings labels Jun 14, 2023
@jmarrec jmarrec self-assigned this Jun 14, 2023
Comment on lines 17 to 55
%fragment("JsonToDict","header", fragment="SWIG_FromCharPtrAndSize") {
PyObject* SWIG_From_JsonValue(const Json::Value& value) {

if (value.isBool()) {
return value.asBool() ? Py_True : Py_False;
} else if (value.isIntegral()) {
return PyLong_FromLongLong(value.asInt64());
} else if (value.isNumeric()) {
return PyFloat_FromDouble(value.asDouble());
} else if (value.isString()) {
// return PyUnicode_FromString(value.asCString());
const auto str = value.asString();
return SWIG_FromCharPtrAndSize(str.data(), str.size());
} else if (value.isArray()) {
PyObject* result = PyList_New(value.size());
Py_ssize_t idx = 0;
for( const auto& arrayElement : value) {
auto val = SWIG_From_JsonValue(arrayElement);
PyList_SetItem(result, idx++, val); // This steals the reference so no decref
}
return result;

} else if (value.isObject()) {
PyObject* result = PyDict_New();
for( const auto& id : value.getMemberNames()) {
auto val = SWIG_From_JsonValue(value[id]);
PyDict_SetItemString(result, id.c_str(), val); // Does **not** steal so decref
Py_DECREF(val);
}
return result;
}

return PyDict_New();
}
}
%typemap(out, fragment="JsonToDict") Json::Value {
$result = SWIG_From_JsonValue($1);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fragment and typemap for python

Comment on lines 208 to 239
%fragment("JsonToDict", "header", fragment="SWIG_FromCharPtrAndSize") {
VALUE SWIG_From_JsonValue(const Json::Value& value) {
if (value.isBool()) {
return value.asBool() ? Qtrue : Qfalse;
} else if (value.isIntegral()) {
return INT2NUM(value.asInt64());
} else if (value.isNumeric()) {
return DOUBLE2NUM(value.asDouble());
} else if (value.isString()) {
const auto str = value.asString();
return SWIG_FromCharPtrAndSize(str.data(), str.size());
} else if (value.isArray()) {
VALUE result = rb_ary_new2(value.size());
for( const auto& arrayElement : value) {
rb_ary_push(result, SWIG_From_JsonValue(arrayElement));
}
return result;

} else if (value.isObject()) {
VALUE result = rb_hash_new();
for( const auto& id : value.getMemberNames()) {
rb_hash_aset(result, ID2SYM(rb_intern(id.data())), SWIG_From_JsonValue(value[id]));
}
return result;
}

return rb_hash_new();
}
}
%typemap(out, fragment="JsonToDict") Json::Value {
$result = SWIG_From_JsonValue($1);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fragment and typemap for ruby

} else if (value.isObject()) {
VALUE result = rb_hash_new();
for( const auto& id : value.getMemberNames()) {
rb_hash_aset(result, ID2SYM(rb_intern(id.data())), SWIG_From_JsonValue(value[id]));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the symbolization of hash keys, could replace with

Suggested change
rb_hash_aset(result, ID2SYM(rb_intern(id.data())), SWIG_From_JsonValue(value[id]));
rb_hash_aset(result, SWIG_FromCharPtrAndSize(id.data(), id.size()), SWIG_From_JsonValue(value[id]));


#if defined SWIGPYTHON
%fragment("JsonToDict","header", fragment="SWIG_FromCharPtrAndSize") {
inline PyObject* SWIG_From_JsonValue(const Json::Value& value) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up moving the fragments defined in xxx/LanguageSpecific.i to a single jsoncpp.i file... Because I was getting a "multiple defined symbol SWIG_From_JsonValue" when linking the ruby libs into a single lib. I'm not 100% sure but really I think the only think that fixed it is inline here. I'm fine leaving it in jsoncpp.i though

Comment on lines +14 to +16
#ifdef SWIGCSHARP
%ignore openstudio::epJSON::toJSON;
#endif
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want that global?

@jmarrec jmarrec requested a review from kbenne June 15, 2023 14:44
@jmarrec jmarrec changed the title Add a swig out typemap for Json::Value Add a swig out typemap for Json::Value + openstudio::path typemap in/out for Python Jun 19, 2023
@jmarrec jmarrec force-pushed the json_value_typemap_swig branch 2 times, most recently from 1f7b61d to b11a6b7 Compare June 29, 2023 14:53
@jmarrec jmarrec changed the title Add a swig out typemap for Json::Value + openstudio::path typemap in/out for Python Add a swig out typemap for Json::Value + openstudio::path typemap in/out for Python + add toJSON() for Workfow files and BCLMeasure Jun 29, 2023
@jmarrec jmarrec merged commit 470e0ef into develop Jun 30, 2023
@jmarrec jmarrec deleted the json_value_typemap_swig branch June 30, 2023 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component - Python bindings component - Ruby bindings Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants