forked from IfcOpenShell/IfcOpenShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
175 lines (145 loc) · 6.56 KB
/
nodes.py
File metadata and controls
175 lines (145 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
###############################################################################
# #
# This file is part of IfcOpenShell. #
# #
# IfcOpenShell is free software: you can redistribute it and/or modify #
# it under the terms of the Lesser GNU General Public License as published by #
# the Free Software Foundation, either version 3.0 of the License, or #
# (at your option) any later version. #
# #
# IfcOpenShell is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# Lesser GNU General Public License for more details. #
# #
# You should have received a copy of the Lesser GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
import string
import collections
class Node:
def __init__(self, tokens):
self.tokens = tokens
self.init()
def tokens_of_type(self, cls):
return [t for t in self.tokens if isinstance(t, cls)]
def single_token_of_type(self, cls, k = None, v = None):
ts = [t for t in self.tokens if isinstance(t, cls) and (k is None or getattr(t, k) == v)]
return ts[0] if len(ts) == 1 else None
class TypeDeclaration(Node):
name = property(lambda self: self.tokens[1])
type = property(lambda self: self.tokens[3])
def init(self):
assert self.tokens[0] == 'type'
assert isinstance(self.type, UnderlyingType)
def __repr__(self):
return "%s = TypeDeclaration(%s)" % (self.name, self.type)
class EntityDeclaration(Node):
name = property(lambda self: self.tokens[1])
attributes = property(lambda self: self.tokens_of_type(ExplicitAttribute))
def init(self):
assert self.tokens[0] == 'entity'
s = self.single_token_of_type(SubtypeExpression)
self.inverse = self.single_token_of_type(AttributeList, 'type', 'inverse')
self.derive = self.single_token_of_type(AttributeList, 'type', 'derive')
self.supertypes = s.types if s else []
def __repr__(self):
builder = ""
builder += "Entity(%s)" % (self.name)
if len(self.supertypes):
builder += "\n Supertypes: %s"%(",".join(self.supertypes))
if len(self.attributes):
builder += "\n Attributes: %s"%("".join(["\n %s"%a for a in self.attributes]))
if self.derive:
builder += "\n Derive:"
builder += str(self.derive)
if self.inverse:
builder += "\n Inverse:"
builder += str(self.inverse)
builder += "\n"
return builder
class UnderlyingType(Node):
type = property(lambda self: self.tokens[0])
def init(self):
pass
def __repr__(self):
return repr(self.type)
class EnumerationType(Node):
type = property(lambda self: self.tokens[0])
values = property(lambda self: self.tokens[3::2])
def init(self):
assert self.type == 'enumeration'
def __repr__(self):
return ",".join(self.values)
class AggregationType(Node):
aggregate_type = property(lambda self: self.tokens[0])
bounds = property(lambda self: None if self.tokens[1] == 'of' else self.tokens[1])
type = property(lambda self: self.tokens[-1])
def init(self):
assert self.bounds is None or isinstance(self.bounds, BoundSpecification)
def __repr__(self):
return "%s%s of %s"%(self.aggregate_type, self.bounds, self.type)
class SelectType(Node):
type = property(lambda self: self.tokens[0])
values = property(lambda self: self.tokens[2::2])
def init(self):
assert self.type == 'select'
def __repr__(self):
return ",".join(self.values)
class SubSuperTypeExpression(Node):
type = property(lambda self: self.tokens[0])
types = property(lambda self: self.tokens[3::2])
def init(self):
assert self.type == self.class_type
class SubtypeExpression(SubSuperTypeExpression):
class_type = 'subtype'
class AttributeList(Node):
elements = property(lambda self: self.tokens[1:])
def __init__(self, ty, toks):
self.type = ty
Node.__init__(self, toks)
def init(self):
assert self.type == self.tokens[0]
def __repr__(self):
return "".join(["\n %s"%s for s in self.elements])
class InverseAttribute(Node):
name = property(lambda self: self.tokens[0])
type = property(lambda self: self.tokens[2])
bounds = property(lambda self: None if len(self.tokens) == 6 else self.tokens[3])
entity = property(lambda self: self.tokens[-4])
attribute = property(lambda self: self.tokens[-2])
def init(self):
assert self.bounds is None or isinstance(self.bounds, BoundSpecification)
def __repr__(self):
return "%s = %s.%s (%s%s)"%(self.name, self.entity, self.attribute, self.type, self.bounds or "")
class DerivedAttribute(Node):
def init(self):
name_index = list(self.tokens).index(':') - 1
self.name = self.tokens[name_index]
def __repr__(self):
return str(self.name)
class BinaryType(Node):
def init(self):
pass
def __repr__(self):
return "BINARY"
class BoundSpecification(Node):
lower = property(lambda self: self.tokens[1])
upper = property(lambda self: self.tokens[3])
def init(self):
# assert self.lower in string.digits or self.lower == '?'
# assert self.upper in string.digits or self.upper == '?'
pass
def __repr__(self):
return "[%s:%s]"%(self.lower, self.upper)
class ExplicitAttribute(Node):
name = property(lambda self: self.tokens[0])
type = property(lambda self: self.tokens[-2])
optional = property(lambda self: len(self.tokens) == 5 and self.tokens[-3] == 'optional')
def init(self):
# NB: This assumes a single name per attribute
# definition, which is not necessarily the case.
assert self.tokens[1] == ':'
def __repr__(self):
return "%s : %s%s" % (self.name, self.type, " ?" if self.optional else "")