Skip to content

Commit 490fa92

Browse files
committed
Rework equality and get_info_2
1 parent cf3393b commit 490fa92

3 files changed

Lines changed: 107 additions & 44 deletions

File tree

‎src/ifcopenshell-python/ifcopenshell/entity_instance.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ def get_info_2(
394394
* ignore: `()` (empty tuple)
395395
"""
396396

397-
assert recursive
398397
assert return_type is dict
399398
assert len(ignore) == 0
400-
return ifcopenshell_wrapper.get_info_cpp(self, include_identifier)
399+
return ifcopenshell_wrapper.get_info_cpp(self, recursive, include_identifier)

‎src/ifcopenshell-python/test/test_entity_instance.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@ def test_exclude_identifier(self):
6767
"Outer": {"CfsFaces": None, "type": "IfcClosedShell"},
6868
"type": "IfcFacetedBrep",
6969
}
70+
71+
def test_equality():
72+
f = ifcopenshell.file()
73+
g = ifcopenshell.file()
74+
f.createIfcCartesianPoint((0., 0.))
75+
g.createIfcCartesianPoint((0., 0.))
76+
assert f[1] == g[1]
77+
g[1].Coordinates = (1., 0.)
78+
assert f[1] != g[1]

‎src/ifcwrap/IfcParseWrapper.i‎

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
%ignore IfcParse::IfcFile::type_iterator;
6767

6868
%ignore express::Base::is;
69+
%ignore express::Base::operator==;
6970

7071
%rename("by_id") instance_by_id;
7172
%rename("by_guid") instance_by_guid;
@@ -466,29 +467,61 @@ private:
466467
}
467468

468469
bool __eq__(const express::Base& other) const {
469-
return $self->identity() == other.identity();
470+
// This logic is not perfect and is not fully consistent with __hash__
471+
// Should there also be a distinction between entity instances and type declarations?
472+
if ($self->identity() == other.identity()) {
473+
return true;
474+
} else if ($self->file() != other.file()) {
475+
// PyGILState_STATE gil = PyGILState_Ensure();
476+
// @todo get_info_2 is actually implemented in C++, so we try to call it directly
477+
bool result = false;
478+
PyObject *py_self = SWIG_NewPointerObj(SWIG_as_voidptr($self),
479+
SWIGTYPE_p_express__Base, 0);
480+
PyObject *py_other = SWIG_NewPointerObj(SWIG_as_voidptr(&other),
481+
SWIGTYPE_p_express__Base, 0);
482+
if (py_self && py_other) {
483+
PyObject *args = PyTuple_New(0);
484+
485+
PyObject *kwargs = PyDict_New();
486+
PyDict_SetItemString(kwargs, "recursive", Py_True);
487+
PyDict_SetItemString(kwargs, "include_identifier", Py_False);
488+
489+
PyObject *m1 = PyObject_GetAttrString(py_self, "get_info_2");
490+
PyObject *m2 = PyObject_GetAttrString(py_other, "get_info_2");
491+
492+
PyObject *i1 = (m1 ? PyObject_Call(m1, args, kwargs) : nullptr);
493+
PyObject *i2 = (m2 ? PyObject_Call(m2, args, kwargs) : nullptr);
494+
495+
if (i1 && i2) {
496+
int eq = PyObject_RichCompareBool(i1, i2, Py_EQ);
497+
result = (eq == 1);
498+
} else {
499+
// get_info_2 missing or threw; treat as not equal (and clear Python error).
500+
PyErr_Clear();
501+
}
502+
503+
Py_XDECREF(i1);
504+
Py_XDECREF(i2);
505+
Py_XDECREF(m1);
506+
Py_XDECREF(m2);
507+
Py_DECREF(kwargs);
508+
Py_DECREF(args);
509+
} else {
510+
PyErr_Clear();
511+
}
512+
513+
Py_XDECREF(py_self);
514+
Py_XDECREF(py_other);
515+
// PyGILState_Release(gil);
516+
return result;
517+
} else {
518+
return false;
519+
}
470520
}
471521

472522
size_t __hash__() const {
473-
return boost::hash<std::tuple<uint32_t, void*>>{}({self->identity(), self->file()});
474-
// if (self->declaration().as_entity()) {
475-
// }
476-
}
477-
478-
/*
479-
else {
480-
return self->get_attribute_value(0).apply_visitor([&](const auto& val){
481-
using U = std::decay_t<decltype(val)>;
482-
if constexpr (std::is_same_v<U, Blank> || std::is_same_v<U, Derived> || std::is_same_v<U, boost::logic::tribool> || std::is_same_v<U, EnumerationReference> || std::is_same_v<U, empty_aggregate_t> || std::is_same_v<U, empty_aggregate_of_aggregate_t>) {
483-
// @todo
484-
return boost::hash<std::tuple<size_t, size_t, void*>>{}({self->declaration().index_in_schema(), 0, self->file()});
485-
} else {
486-
return boost::hash<std::tuple<size_t, decltype(val), void*>>{}({self->declaration().index_in_schema(), val, self->file()});
487-
}
488-
});
489-
}
523+
return std::hash<uint32_t>{}(self->identity());
490524
}
491-
*/
492525

493526
std::string __repr__() const {
494527
std::ostringstream oss;
@@ -1104,16 +1137,14 @@ object = _old_object
11041137
%}
11051138

11061139
%{
1107-
PyObject* get_info_cpp(const express::Base& v, bool include_identifier);
1140+
PyObject* get_info_cpp(const express::Base& v, bool recursive, bool include_identifier);
11081141

11091142
// @todo refactor this to remove duplication with the typemap.
11101143
// except this is calls the above function in case of instances.
1111-
PyObject* convert_cpp_attribute_to_python(const express::Base& instance, size_t attribute_index, bool include_identifier = true) {
1112-
return instance.get_attribute_value(attribute_index).apply_visitor([include_identifier](const auto& v){
1144+
PyObject* convert_cpp_attribute_to_python(const express::Base& instance, size_t attribute_index, bool recursive, bool include_identifier) {
1145+
return instance.get_attribute_value(attribute_index).apply_visitor([recursive, include_identifier](const auto& v){
11131146
using U = std::decay_t<decltype(v)>;
1114-
if constexpr (is_std_vector_v<U>) {
1115-
return pythonize_vector(v);
1116-
} else if constexpr (std::is_same_v<U, EnumerationReference>) {
1147+
if constexpr (std::is_same_v<U, EnumerationReference>) {
11171148
return pythonize(std::string(v.value()));
11181149
} else if constexpr (std::is_same_v<U, Derived>) {
11191150
if (feature_use_attribute_value_derived) {
@@ -1123,18 +1154,52 @@ object = _old_object
11231154
return static_cast<PyObject*>(Py_None);
11241155
}
11251156
} else if constexpr (std::is_same_v<U, express::Base>) {
1126-
return get_info_cpp(v, include_identifier);
1157+
if (recursive) {
1158+
return get_info_cpp(v, recursive, include_identifier);
1159+
} else {
1160+
return pythonize(v);
1161+
}
1162+
} else if constexpr (std::is_same_v<U, std::vector<express::Base>>) {
1163+
if (recursive) {
1164+
PyObject* t = PyTuple_New(v.size());
1165+
for (size_t i = 0; i < v.size(); ++i) {
1166+
PyObject* item = get_info_cpp(v[i], recursive, include_identifier);
1167+
PyTuple_SET_ITEM(t, i, item);
1168+
}
1169+
return t;
1170+
} else {
1171+
return pythonize_vector(v);
1172+
}
1173+
} else if constexpr (std::is_same_v<U, std::vector<std::vector<express::Base>>>) {
1174+
if (recursive) {
1175+
PyObject* outer = PyTuple_New(v.size());
1176+
for (size_t i = 0; i < v.size(); ++i) {
1177+
const auto& inner_vec = v[i];
1178+
PyObject* inner = PyTuple_New(inner_vec.size());
1179+
for (size_t j = 0; j < inner_vec.size(); ++j) {
1180+
PyObject* item = get_info_cpp(inner_vec[j], recursive, include_identifier);
1181+
PyTuple_SET_ITEM(inner, j, item);
1182+
}
1183+
PyTuple_SET_ITEM(outer, i, inner);
1184+
}
1185+
return outer;
1186+
} else {
1187+
return pythonize_vector(v);
1188+
}
11271189
} else if constexpr (std::is_same_v<U, empty_aggregate_t> || std::is_same_v<U, empty_aggregate_of_aggregate_t> || std::is_same_v<U, Blank>) {
11281190
Py_INCREF(Py_None);
11291191
return static_cast<PyObject*>(Py_None);
1192+
} else if constexpr (is_std_vector_v<U>) {
1193+
// only for non-entity-instance vectors
1194+
return pythonize_vector(v);
11301195
} else {
11311196
return pythonize(v);
11321197
}
11331198
});
11341199
}
11351200
%}
11361201
%inline %{
1137-
PyObject* get_info_cpp(const express::Base& v, bool include_identifier = true) {
1202+
PyObject* get_info_cpp(const express::Base& v, bool recursive, bool include_identifier) {
11381203
PyObject *d = PyDict_New();
11391204

11401205
if (v.declaration().as_entity()) {
@@ -1147,35 +1212,25 @@ object = _old_object
11471212
auto attr_type = *dit
11481213
? IfcUtil::Argument_DERIVED
11491214
: IfcUtil::from_parameter_type((*it)->type_of_attribute());
1150-
auto value_py = convert_cpp_attribute_to_python(v, std::distance(attrs.begin(), it), include_identifier);
1215+
auto value_py = convert_cpp_attribute_to_python(v, std::distance(attrs.begin(), it), recursive, include_identifier);
11511216
PyDict_SetItem(d, name_py, value_py);
11521217
Py_DECREF(name_py);
11531218
Py_DECREF(value_py);
11541219
}
11551220
if (include_identifier) {
1156-
const std::string& id_cpp = "id";
1157-
auto id_py = pythonize(id_cpp);
11581221
auto id_v_py = pythonize(v.id());
1159-
PyDict_SetItem(d, id_py, id_v_py);
1160-
Py_DECREF(id_py);
1222+
PyDict_SetItemString(d, "id", id_v_py);
11611223
Py_DECREF(id_v_py);
11621224
}
11631225
} else {
1164-
const std::string& name_cpp = "wrappedValue";
1165-
auto name_py = pythonize(name_cpp);
1166-
auto value_py = convert_cpp_attribute_to_python(v, 0, include_identifier);
1167-
PyDict_SetItem(d, name_py, value_py);
1168-
Py_DECREF(name_py);
1226+
auto value_py = convert_cpp_attribute_to_python(v, 0, recursive, include_identifier);
1227+
PyDict_SetItemString(d, "wrappedValue", value_py);
11691228
Py_DECREF(value_py);
11701229
}
11711230

1172-
// @todo type and id can be static?
1173-
const std::string& type_cpp = "type";
1174-
auto type_py = pythonize(type_cpp);
11751231
const std::string& type_v_cpp = v.declaration().name();
11761232
auto type_v_py = pythonize(type_v_cpp);
1177-
PyDict_SetItem(d, type_py, type_v_py);
1178-
Py_DECREF(type_py);
1233+
PyDict_SetItemString(d, "type", type_v_py);
11791234
Py_DECREF(type_v_py);
11801235

11811236
return d;

0 commit comments

Comments
 (0)