mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-09 17:31:45 +00:00
This commit is contained in:
@@ -55,19 +55,20 @@ filter_elements_grammar = lark.Lark(
|
||||
ifc_class: /Ifc\\w+/
|
||||
|
||||
value: special | quoted_string | regex_string | unquoted_string
|
||||
unquoted_string: /[^,.=\\s]+/
|
||||
unquoted_string: /[^,.=><*!\\s]+/
|
||||
regex_string: "/" /[^\\/]+/ "/"
|
||||
quoted_string: ESCAPED_STRING
|
||||
|
||||
special: null | true | false
|
||||
|
||||
comparison: not? equals | morethanequalto | lessthanequalto | morethan | lessthan
|
||||
comparison: not? equals | morethanequalto | lessthanequalto | morethan | lessthan | not? contains
|
||||
not: "!"
|
||||
equals: "="
|
||||
morethanequalto: ">="
|
||||
lessthanequalto: "<="
|
||||
morethan: ">"
|
||||
lessthan: "<"
|
||||
contains: "*="
|
||||
null: "NULL"
|
||||
true: "TRUE"
|
||||
false: "FALSE"
|
||||
@@ -557,18 +558,24 @@ class FacetTransformer(lark.Transformer):
|
||||
return tree
|
||||
|
||||
def comparison(self, args):
|
||||
if args[0].data == "equals":
|
||||
return "="
|
||||
elif args[0].data == "morethanequalto":
|
||||
return ">="
|
||||
elif args[0].data == "lessthanequalto":
|
||||
return "<="
|
||||
elif args[0].data == "morethan":
|
||||
return ">"
|
||||
elif args[0].data == "lessthan":
|
||||
return "<"
|
||||
if args[0].data == "not":
|
||||
comparison = args[1].data
|
||||
is_not = "!"
|
||||
else:
|
||||
return "!="
|
||||
comparison = args[0].data
|
||||
is_not = ""
|
||||
|
||||
return (
|
||||
is_not
|
||||
+ {
|
||||
"equals": "=",
|
||||
"morethanequalto": ">=",
|
||||
"lessthanequalto": "<=",
|
||||
"morethan": ">",
|
||||
"lessthan": "<",
|
||||
"contains": "*=",
|
||||
}[comparison]
|
||||
)
|
||||
|
||||
def keys(self, args):
|
||||
return self.value(args)
|
||||
@@ -595,35 +602,42 @@ class FacetTransformer(lark.Transformer):
|
||||
return False
|
||||
|
||||
def compare(self, element_value, comparison, value):
|
||||
if element_value:
|
||||
if isinstance(element_value, (list, tuple)):
|
||||
return any(self.compare(ev, comparison, value) for ev in element_value)
|
||||
elif isinstance(value, re.Pattern):
|
||||
result = bool(value.match(element_value)) if element_value is not None else False
|
||||
elif isinstance(value, str):
|
||||
if isinstance(element_value, int):
|
||||
value = int(value)
|
||||
elif isinstance(element_value, float):
|
||||
value = float(value)
|
||||
|
||||
if comparison == "=":
|
||||
result = element_value == value
|
||||
elif comparison == ">":
|
||||
result = element_value > value
|
||||
elif comparison == "<":
|
||||
result = element_value < value
|
||||
elif comparison == ">=":
|
||||
result = element_value >= value
|
||||
elif comparison == "<= ":
|
||||
result = element_value <= value
|
||||
else:
|
||||
result = element_value != value
|
||||
if isinstance(element_value, (list, tuple)):
|
||||
return any(self.compare(ev, comparison, value) for ev in element_value)
|
||||
elif isinstance(value, str):
|
||||
if isinstance(element_value, int):
|
||||
value = int(value)
|
||||
elif isinstance(element_value, float):
|
||||
value = float(value)
|
||||
|
||||
elif value in (None, True, False):
|
||||
result = element_value is value
|
||||
else:
|
||||
result = False
|
||||
return result
|
||||
if isinstance(element_value, (int, float)):
|
||||
operator = comparison.lstrip("!")
|
||||
if operator == ">=":
|
||||
result = element_value >= value
|
||||
elif operator == "<=":
|
||||
result = element_value <= value
|
||||
elif operator == ">":
|
||||
result = element_value > value
|
||||
elif operator == "<":
|
||||
result = element_value < value
|
||||
else:
|
||||
result = element_value == value # Tolerance?
|
||||
elif isinstance(element_value, str):
|
||||
operator = comparison.lstrip("!")
|
||||
if operator == "*=":
|
||||
result = value in element_value
|
||||
else:
|
||||
result = element_value == value
|
||||
else:
|
||||
result = element_value == value
|
||||
elif isinstance(value, re.Pattern):
|
||||
result = bool(value.match(element_value)) if element_value is not None else False
|
||||
elif value in (None, True, False):
|
||||
result = element_value is value
|
||||
|
||||
if comparison.startswith("!"):
|
||||
return not result
|
||||
return result
|
||||
|
||||
|
||||
class Selector:
|
||||
|
||||
Reference in New Issue
Block a user