| 1 |
# BIMTester - OpenBIM Auditing Tool |
| 2 |
# Copyright (C) 2021 Dion Moult <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of BIMTester. |
| 5 |
# |
| 6 |
# BIMTester is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# BIMTester is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public License |
| 17 |
# along with BIMTester. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from behave import step |
| 20 |
from behave import use_step_matcher |
| 21 |
|
| 22 |
from bimtester import util |
| 23 |
from bimtester.ifc import IfcStore |
| 24 |
from bimtester.lang import _ |
| 25 |
|
| 26 |
|
| 27 |
@step('All "{ifc_class}" elements have an "{aproperty}" property in the "{pset}" pset') |
| 28 |
def step_impl(context, ifc_class, aproperty, pset): |
| 29 |
eleclass_has_property_in_pset(context, ifc_class, aproperty, pset) |
| 30 |
|
| 31 |
|
| 32 |
# ------------------------------------------------------------------------ |
| 33 |
# STEPS with Regular Expression Matcher ("re") |
| 34 |
# ------------------------------------------------------------------------ |
| 35 |
use_step_matcher("re") |
| 36 |
|
| 37 |
|
| 38 |
@step(r"All (?P<ifc_class>.*) elements have an? (?P<property_path>.*\..*) property") |
| 39 |
def step_impl(context, ifc_class, property_path): |
| 40 |
import re |
| 41 |
|
| 42 |
pset, aproperty = property_path.split(".") |
| 43 |
eleclass_has_property_in_pset(context, ifc_class, aproperty, pset) |
| 44 |
|
| 45 |
|
| 46 |
@step( |
| 47 |
r'All (?P<ifc_class>.*) elements have an? (?P<property_path>.*\..*) property value matching the pattern "(?P<pattern>.*)"' |
| 48 |
) |
| 49 |
def step_impl(context, ifc_class, property_path, pattern): |
| 50 |
import re |
| 51 |
from ifcopenshell.util.element import get_psets |
| 52 |
|
| 53 |
pset_name, property_name = property_path.split(".") |
| 54 |
elements = IfcStore.file.by_type(ifc_class) |
| 55 |
for element in elements: |
| 56 |
|
| 57 |
psets = get_psets(element) |
| 58 |
|
| 59 |
if not pset_name in psets: |
| 60 |
assert False |
| 61 |
|
| 62 |
pset = psets[pset_name] |
| 63 |
if not property_name in pset: |
| 64 |
assert False |
| 65 |
|
| 66 |
prop = pset[property_name] |
| 67 |
# get_psets returns just strings |
| 68 |
|
| 69 |
if not re.search(pattern, prop): |
| 70 |
assert False |
| 71 |
|
| 72 |
|
| 73 |
def eleclass_has_property_in_pset(context, ifc_class, aproperty, pset): |
| 74 |
context.falseelems = [] |
| 75 |
context.falseguids = [] |
| 76 |
context.falseprops = {} |
| 77 |
from ifcopenshell.util.element import get_psets |
| 78 |
|
| 79 |
elements = IfcStore.file.by_type(ifc_class) |
| 80 |
for elem in elements: |
| 81 |
psets = get_psets(elem) |
| 82 |
if not (pset in psets and aproperty in psets[pset]): |
| 83 |
context.falseelems.append(str(elem)) |
| 84 |
context.falseguids.append(elem.GlobalId) |
| 85 |
context.falseprops[elem.id()] = str(psets) |
| 86 |
|
| 87 |
context.elemcount = len(elements) |
| 88 |
context.falsecount = len(context.falseelems) |
| 89 |
util.assert_elements( |
| 90 |
ifc_class, |
| 91 |
context.elemcount, |
| 92 |
context.falsecount, |
| 93 |
context.falseelems, |
| 94 |
# TODO: Translate these messages into other languages |
| 95 |
message_all_falseelems=_( |
| 96 |
"All {elemcount} {ifc_class} elements are missing the property {parameter} in the pset." |
| 97 |
), |
| 98 |
message_some_falseelems=_( |
| 99 |
"The following {falsecount} of {elemcount} {ifc_class} elements are missing the property {parameter} in the pset: {falseelems}" |
| 100 |
), |
| 101 |
message_no_elems=_("There are no {ifc_class} elements in the IFC file."), |
| 102 |
parameter=aproperty, |
| 103 |
) |
| 104 |
# the pset name is missing in the failing message, but it is in the step test name |