1818#
1919
2020from datetime import datetime
21+ from typing import Optional
2122
2223import graknprotocol .protobuf .concept_pb2 as concept_proto
2324
25+ from grakn .common .exception import GraknClientException
2426from grakn .concept .proto import concept_proto_builder , concept_proto_reader
2527from grakn .concept .type .thing_type import ThingType , RemoteThingType
2628from grakn .concept .type .value_type import ValueType
@@ -32,6 +34,8 @@ def is_keyable(value_type: ValueType):
3234
3335class AttributeType (ThingType ):
3436
37+ ROOT_LABEL = "attribute"
38+
3539 def as_remote (self , transaction ):
3640 return RemoteAttributeType (transaction , self .get_label (), self .is_root ())
3741
@@ -59,11 +63,36 @@ def is_string(self):
5963 def is_datetime (self ):
6064 return False
6165
66+ def as_boolean (self ):
67+ if self .is_root ():
68+ return BooleanAttributeType (self .ROOT_LABEL , is_root = True )
69+ raise GraknClientException ("Invalid conversion to " + BooleanAttributeType .__name__ )
70+
71+ def as_long (self ):
72+ if self .is_root ():
73+ return LongAttributeType (self .ROOT_LABEL , is_root = True )
74+ raise GraknClientException ("Invalid conversion to " + LongAttributeType .__name__ )
75+
76+ def as_double (self ):
77+ if self .is_root ():
78+ return DoubleAttributeType (self .ROOT_LABEL , is_root = True )
79+ raise GraknClientException ("Invalid conversion to " + DoubleAttributeType .__name__ )
80+
81+ def as_string (self ):
82+ if self .is_root ():
83+ return StringAttributeType (self .ROOT_LABEL , is_root = True )
84+ raise GraknClientException ("Invalid conversion to " + StringAttributeType .__name__ )
85+
86+ def as_datetime (self ):
87+ if self .is_root ():
88+ return DateTimeAttributeType (self .ROOT_LABEL , is_root = True )
89+ raise GraknClientException ("Invalid conversion to " + DateTimeAttributeType .__name__ )
90+
6291 def __eq__ (self , other ):
6392 if other is self :
6493 return True
6594 # root "attribute" should always be equal to itself regardless of which value class it holds
66- if not other or not isinstance (AttributeType , other ):
95+ if not other or not isinstance (other , AttributeType ):
6796 return False
6897 return self .get_label () == other .get_label ()
6998
@@ -73,6 +102,8 @@ def __hash__(self):
73102
74103class RemoteAttributeType (RemoteThingType ):
75104
105+ ROOT_LABEL = "attribute"
106+
76107 def get_value_type (self ):
77108 return ValueType .OBJECT
78109
@@ -82,6 +113,13 @@ def is_keyable(self):
82113 def as_remote (self , transaction ):
83114 return RemoteAttributeType (transaction , self .get_label (), self .is_root ())
84115
116+ def get_subtypes (self ):
117+ stream = super (RemoteAttributeType , self ).get_subtypes ()
118+ if self .is_root () and self .get_value_type () is not ValueType .OBJECT :
119+ return [subtype for subtype in stream if subtype .get_value_type () is self .get_value_type () or subtype .get_label () == self .get_label ()]
120+ else :
121+ return stream
122+
85123 def get_owners (self , only_key = False ):
86124 method = concept_proto .Type .Req ()
87125 get_owners_req = concept_proto .AttributeType .GetOwners .Req ()
@@ -122,6 +160,31 @@ def is_string(self):
122160 def is_datetime (self ):
123161 return False
124162
163+ def as_boolean (self ):
164+ if self .is_root ():
165+ return BooleanAttributeType (self .ROOT_LABEL , is_root = True )
166+ raise GraknClientException ("Invalid conversion to " + BooleanAttributeType .__name__ )
167+
168+ def as_long (self ):
169+ if self .is_root ():
170+ return LongAttributeType (self .ROOT_LABEL , is_root = True )
171+ raise GraknClientException ("Invalid conversion to " + LongAttributeType .__name__ )
172+
173+ def as_double (self ):
174+ if self .is_root ():
175+ return DoubleAttributeType (self .ROOT_LABEL , is_root = True )
176+ raise GraknClientException ("Invalid conversion to " + DoubleAttributeType .__name__ )
177+
178+ def as_string (self ):
179+ if self .is_root ():
180+ return StringAttributeType (self .ROOT_LABEL , is_root = True )
181+ raise GraknClientException ("Invalid conversion to " + StringAttributeType .__name__ )
182+
183+ def as_datetime (self ):
184+ if self .is_root ():
185+ return DateTimeAttributeType (self .ROOT_LABEL , is_root = True )
186+ raise GraknClientException ("Invalid conversion to " + DateTimeAttributeType .__name__ )
187+
125188 def __eq__ (self , other ):
126189 if other is self :
127190 return True
@@ -148,6 +211,9 @@ def as_remote(self, transaction):
148211 def is_boolean (self ):
149212 return True
150213
214+ def as_boolean (self ):
215+ return self
216+
151217
152218class RemoteBooleanAttributeType (RemoteAttributeType ):
153219
@@ -166,6 +232,9 @@ def get(self, value: bool):
166232 def is_boolean (self ):
167233 return True
168234
235+ def as_boolean (self ):
236+ return self
237+
169238
170239class LongAttributeType (AttributeType ):
171240
@@ -182,6 +251,9 @@ def as_remote(self, transaction):
182251 def is_long (self ):
183252 return True
184253
254+ def as_long (self ):
255+ return self
256+
185257
186258class RemoteLongAttributeType (RemoteAttributeType ):
187259
@@ -200,6 +272,9 @@ def get(self, value: int):
200272 def is_long (self ):
201273 return True
202274
275+ def as_long (self ):
276+ return self
277+
203278
204279class DoubleAttributeType (AttributeType ):
205280
@@ -216,6 +291,9 @@ def as_remote(self, transaction):
216291 def is_double (self ):
217292 return True
218293
294+ def as_double (self ):
295+ return self
296+
219297
220298class RemoteDoubleAttributeType (RemoteAttributeType ):
221299
@@ -234,6 +312,9 @@ def get(self, value: float):
234312 def is_double (self ):
235313 return True
236314
315+ def as_double (self ):
316+ return self
317+
237318
238319class StringAttributeType (AttributeType ):
239320
@@ -250,6 +331,9 @@ def as_remote(self, transaction):
250331 def is_string (self ):
251332 return True
252333
334+ def as_string (self ):
335+ return self
336+
253337
254338class RemoteStringAttributeType (RemoteAttributeType ):
255339
@@ -272,7 +356,9 @@ def get_regex(self):
272356 regex = self ._execute (method ).attribute_type_get_regex_res .regex
273357 return None if len (regex ) == 0 else regex
274358
275- def set_regex (self , regex : str ):
359+ def set_regex (self , regex : Optional [str ]):
360+ if regex is None :
361+ regex = ""
276362 method = concept_proto .Type .Req ()
277363 set_regex_req = concept_proto .AttributeType .SetRegex .Req ()
278364 set_regex_req .regex = regex
@@ -282,6 +368,9 @@ def set_regex(self, regex: str):
282368 def is_string (self ):
283369 return True
284370
371+ def as_string (self ):
372+ return self
373+
285374
286375class DateTimeAttributeType (AttributeType ):
287376
@@ -298,6 +387,9 @@ def as_remote(self, transaction):
298387 def is_datetime (self ):
299388 return True
300389
390+ def as_datetime (self ):
391+ return self
392+
301393
302394class RemoteDateTimeAttributeType (RemoteAttributeType ):
303395
@@ -315,3 +407,6 @@ def get(self, value: datetime):
315407
316408 def is_datetime (self ):
317409 return True
410+
411+ def as_datetime (self ):
412+ return self
0 commit comments