From 62c120e4bbbccc4c97804d21d0ffcc8eccc1225c Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Thu, 20 Jul 2023 19:22:15 +1000 Subject: [PATCH] New facet based selector syntax. Proposed to supersede existing selector syntax. --- .../ifcopenshell/util/selector.py | 254 ++++++++++++++++++ .../test/util/test_selector.py | 100 +++++++ 2 files changed, 354 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/util/selector.py b/src/ifcopenshell-python/ifcopenshell/util/selector.py index 95dd6dbfc3..1e3227872e 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/selector.py +++ b/src/ifcopenshell-python/ifcopenshell/util/selector.py @@ -21,6 +21,7 @@ import lark import ifcopenshell.util import ifcopenshell.util.fm import ifcopenshell.util.element +import ifcopenshell.util.classification def get_element_value(element, query): @@ -48,6 +49,259 @@ def get_element_value(element, query): return Selector.get_element_value(element, filter_query["keys"], filter_query["is_regex"]) +def filter_elements(ifc_file, query, elements=None): + l = lark.Lark( + """start: filter_group + filter_group: facet_list ("+" facet_list)* + facet_list: facet ("," facet)* + + facet: entity | attribute | type | material | property | classification | location + + entity: not? ifc_class + attribute: attribute_name comparison value + type: "type" comparison value + material: "material" comparison value + property: pset "." prop comparison value + classification: "classification" comparison value + location: "location" comparison value + + pset: quoted_string | unquoted_string | regex_string + prop: quoted_string | unquoted_string | regex_string + + attribute_name: /[A-Z]\\w+/ + ifc_class: /Ifc\\w+/ + + value: special | quoted_string | unquoted_string | regex_string + unquoted_string: /[^.=\\s]+/ + quoted_string: ESCAPED_STRING + regex_string: "/" /[^\\/]+/ "/" + + special: null | true | false + + comparison: not? equals + not: "!" + equals: "=" + null: "NULL" + true: "TRUE" + false: "FALSE" + + // Embed common.lark for packaging + DIGIT: "0".."9" + HEXDIGIT: "a".."f"|"A".."F"|DIGIT + INT: DIGIT+ + SIGNED_INT: ["+"|"-"] INT + DECIMAL: INT "." INT? | "." INT + _EXP: ("e"|"E") SIGNED_INT + FLOAT: INT _EXP | DECIMAL _EXP? + SIGNED_FLOAT: ["+"|"-"] FLOAT + NUMBER: FLOAT | INT + SIGNED_NUMBER: ["+"|"-"] NUMBER + _STRING_INNER: /.*?/ + _STRING_ESC_INNER: _STRING_INNER /(?