Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 9bff44d

Browse files
author
Alex Walker
authored
Add 'get_type' to local Thing concepts (#200)
## What is the goal of this PR? Previously, `get_type` was only available on remote `Thing` concepts. Now, we include type information whenever a Graql query or Concept API method returns a `Thing`, enabling users to use it without an additional network roundtrip per concept. ## What are the changes implemented in this PR? Add 'get_type' to local Thing concepts
1 parent 4dcb709 commit 9bff44d

File tree

17 files changed

+87
-99
lines changed

17 files changed

+87
-99
lines changed

.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
build --incompatible_strict_action_env
2121
run --incompatible_strict_action_env
22-
test --incompatible_strict_action_env --test_env=PATH
22+
test --incompatible_strict_action_env --test_env=PATH --cache_test_results=no
2323

2424
try-import /opt/credentials/bazel-remote-cache.rc

.grabl/automation.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ build:
106106
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
107107
bazel test //tests/behaviour/graql/language/insert/... --test_output=errors
108108
bazel test //tests/behaviour/graql/language/delete/... --test_output=errors
109-
bazel test //tests/behaviour/graql/language/update:checkstyle --test_output=errors
110-
bazel test //tests/behaviour/graql/language/update:test-core --test_output=errors
109+
bazel test //tests/behaviour/graql/language/update/... --test_output=errors
111110
test-behaviour-definable:
112111
image: graknlabs-ubuntu-20.04
113112
type: foreground
@@ -121,10 +120,8 @@ build:
121120
export ARTIFACT_USERNAME=$REPO_GRAKN_USERNAME
122121
export ARTIFACT_PASSWORD=$REPO_GRAKN_PASSWORD
123122
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
124-
bazel test //tests/behaviour/graql/language/define:checkstyle --test_output=errors
125-
bazel test //tests/behaviour/graql/language/define:test-core --test_output=errors
126-
bazel test //tests/behaviour/graql/language/undefine:checkstyle --test_output=errors
127-
bazel test //tests/behaviour/graql/language/undefine:test-core --test_output=errors
123+
bazel test //tests/behaviour/graql/language/define/... --test_output=errors
124+
bazel test //tests/behaviour/graql/language/undefine/... --test_output=errors
128125
test-cluster-failover:
129126
machine: 4-core-8-gb
130127
image: graknlabs-ubuntu-20.04
@@ -139,7 +136,7 @@ build:
139136
export ARTIFACT_USERNAME=$REPO_GRAKN_USERNAME
140137
export ARTIFACT_PASSWORD=$REPO_GRAKN_PASSWORD
141138
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
142-
bazel test //tests:test_cluster_failover --test_output=streamed
139+
bazel test //tests:test_cluster_failover --test_output=errors
143140
deploy-pip-snapshot:
144141
image: graknlabs-ubuntu-20.04
145142
dependencies: [build, test-behaviour-connection, test-behaviour-concept, test-behaviour-match, test-behaviour-writable, test-behaviour-definable, test-cluster-failover]

dependencies/graknlabs/artifacts.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def graknlabs_grakn_core_artifacts():
2727
artifact_name = "grakn-core-server-{platform}-{version}.{ext}",
2828
tag_source = deployment["artifact.release"],
2929
commit_source = deployment["artifact.snapshot"],
30-
commit = "c04b5f105914c690cbd54e6bdc6a4eb9dda2b3e7",
30+
commit = "8a556849a42a54a0f69277da212540f00b9a783c",
3131
)
3232

3333
def graknlabs_grakn_cluster_artifacts():
@@ -37,5 +37,5 @@ def graknlabs_grakn_cluster_artifacts():
3737
artifact_name = "grakn-cluster-all-{platform}-{version}.{ext}",
3838
tag_source = deployment_private["artifact.release"],
3939
commit_source = deployment_private["artifact.snapshot"],
40-
commit = "ae8aeb4919887d96aca24aa9029e9827b6437b35",
40+
commit = "e1e96cdb1d2c5cfd4309221c5003e7d54a341960",
4141
)

grakn/concept/proto/concept_proto_builder.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ def iid(iid_: str):
3333
def thing(thing_):
3434
proto_thing = concept_proto.Thing()
3535
proto_thing.iid = iid(thing_.get_iid())
36-
proto_thing.encoding = thing_encoding(thing_)
3736
return proto_thing
3837

3938

4039
def type_(_type):
4140
proto_type = concept_proto.Type()
4241
proto_type.label = _type.get_label()
43-
proto_type.encoding = type_encoding(_type)
42+
proto_type.encoding = encoding(_type)
4443

4544
if _type.is_role_type():
4645
proto_type.scope = _type.get_scope()
@@ -99,18 +98,7 @@ def value_type(value_type_: ValueType):
9998
raise GraknClientException("Unrecognised value type: " + str(value_type_))
10099

101100

102-
def thing_encoding(thing_):
103-
if thing_.is_entity():
104-
return concept_proto.Thing.Encoding.Value("ENTITY")
105-
elif thing_.is_relation():
106-
return concept_proto.Thing.Encoding.Value("RELATION")
107-
elif thing_.is_attribute():
108-
return concept_proto.Thing.Encoding.Value("ATTRIBUTE")
109-
else:
110-
raise GraknClientException("Unrecognised thing encoding: " + str(thing_))
111-
112-
113-
def type_encoding(_type):
101+
def encoding(_type):
114102
if _type.is_entity_type():
115103
return concept_proto.Type.Encoding.Value("ENTITY_TYPE")
116104
elif _type.is_relation_type():

grakn/concept/proto/concept_proto_reader.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,29 @@ def concept(con_proto: concept_proto.Concept):
4646

4747

4848
def thing(thing_proto: concept_proto.Thing):
49-
if thing_proto.encoding == concept_proto.Thing.Encoding.Value("ENTITY"):
49+
if thing_proto.type.encoding == concept_proto.Type.Encoding.Value("ENTITY_TYPE"):
5050
return Entity._of(thing_proto)
51-
elif thing_proto.encoding == concept_proto.Thing.Encoding.Value("RELATION"):
51+
elif thing_proto.type.encoding == concept_proto.Type.Encoding.Value("RELATION_TYPE"):
5252
return Relation._of(thing_proto)
53-
elif thing_proto.encoding == concept_proto.Thing.Encoding.Value("ATTRIBUTE"):
53+
elif thing_proto.type.encoding == concept_proto.Type.Encoding.Value("ATTRIBUTE_TYPE"):
5454
return attribute(thing_proto)
5555
else:
56-
raise GraknClientException("The encoding " + thing_proto.encoding + " was not recognised.")
56+
raise GraknClientException("The encoding " + thing_proto.type.encoding + " was not recognised.")
5757

5858

5959
def attribute(thing_proto: concept_proto.Thing):
60-
if thing_proto.value_type == concept_proto.AttributeType.ValueType.Value("BOOLEAN"):
60+
if thing_proto.type.value_type == concept_proto.AttributeType.ValueType.Value("BOOLEAN"):
6161
return BooleanAttribute._of(thing_proto)
62-
elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value("LONG"):
62+
elif thing_proto.type.value_type == concept_proto.AttributeType.ValueType.Value("LONG"):
6363
return LongAttribute._of(thing_proto)
64-
elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value("DOUBLE"):
64+
elif thing_proto.type.value_type == concept_proto.AttributeType.ValueType.Value("DOUBLE"):
6565
return DoubleAttribute._of(thing_proto)
66-
elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value("STRING"):
66+
elif thing_proto.type.value_type == concept_proto.AttributeType.ValueType.Value("STRING"):
6767
return StringAttribute._of(thing_proto)
68-
elif thing_proto.value_type == concept_proto.AttributeType.ValueType.Value("DATETIME"):
68+
elif thing_proto.type.value_type == concept_proto.AttributeType.ValueType.Value("DATETIME"):
6969
return DateTimeAttribute._of(thing_proto)
7070
else:
71-
raise GraknClientException("The value type " + str(thing_proto.value_type) + " was not recognised.")
71+
raise GraknClientException("The value type " + str(thing_proto.type.value_type) + " was not recognised.")
7272

7373

7474
def type_(type_proto: concept_proto.Type):

grakn/concept/thing/attribute.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# specific language governing permissions and limitations
1717
# under the License.
1818
#
19-
2019
from datetime import datetime
2120

2221
import grakn_protocol.protobuf.concept_pb2 as concept_proto
@@ -77,13 +76,13 @@ def is_datetime(self):
7776

7877
class BooleanAttribute(Attribute):
7978

80-
def __init__(self, iid: str, value: bool):
81-
super(BooleanAttribute, self).__init__(iid)
79+
def __init__(self, iid: str, type_, value: bool):
80+
super(BooleanAttribute, self).__init__(iid, type_)
8281
self._value = value
8382

8483
@staticmethod
8584
def _of(thing_proto: concept_proto.Thing):
86-
return BooleanAttribute(concept_proto_reader.iid(thing_proto.iid), thing_proto.value.boolean)
85+
return BooleanAttribute(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.attribute_type(thing_proto.type), thing_proto.value.boolean)
8786

8887
def get_value(self):
8988
return self._value
@@ -92,13 +91,13 @@ def is_boolean(self):
9291
return True
9392

9493
def as_remote(self, transaction):
95-
return RemoteBooleanAttribute(transaction, self.get_iid(), self.get_value())
94+
return RemoteBooleanAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
9695

9796

9897
class RemoteBooleanAttribute(RemoteAttribute):
9998

100-
def __init__(self, transaction, iid: str, value: bool):
101-
super(RemoteBooleanAttribute, self).__init__(transaction, iid)
99+
def __init__(self, transaction, iid: str, type_, value: bool):
100+
super(RemoteBooleanAttribute, self).__init__(transaction, iid, type_)
102101
self._value = value
103102

104103
def get_value(self):
@@ -108,18 +107,18 @@ def is_boolean(self):
108107
return True
109108

110109
def as_remote(self, transaction):
111-
return RemoteBooleanAttribute(transaction, self.get_iid(), self.get_value())
110+
return RemoteBooleanAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
112111

113112

114113
class LongAttribute(Attribute):
115114

116-
def __init__(self, iid: str, value: int):
117-
super(LongAttribute, self).__init__(iid)
115+
def __init__(self, iid: str, type_, value: int):
116+
super(LongAttribute, self).__init__(iid, type_)
118117
self._value = value
119118

120119
@staticmethod
121120
def _of(thing_proto: concept_proto.Thing):
122-
return LongAttribute(concept_proto_reader.iid(thing_proto.iid), thing_proto.value.long)
121+
return LongAttribute(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.attribute_type(thing_proto.type), thing_proto.value.long)
123122

124123
def get_value(self):
125124
return self._value
@@ -128,13 +127,13 @@ def is_long(self):
128127
return True
129128

130129
def as_remote(self, transaction):
131-
return RemoteLongAttribute(transaction, self.get_iid(), self.get_value())
130+
return RemoteLongAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
132131

133132

134133
class RemoteLongAttribute(RemoteAttribute):
135134

136-
def __init__(self, transaction, iid: str, value: int):
137-
super(RemoteLongAttribute, self).__init__(transaction, iid)
135+
def __init__(self, transaction, iid: str, type_, value: int):
136+
super(RemoteLongAttribute, self).__init__(transaction, iid, type_)
138137
self._value = value
139138

140139
def get_value(self):
@@ -144,18 +143,18 @@ def is_long(self):
144143
return True
145144

146145
def as_remote(self, transaction):
147-
return RemoteLongAttribute(transaction, self.get_iid(), self.get_value())
146+
return RemoteLongAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
148147

149148

150149
class DoubleAttribute(Attribute):
151150

152-
def __init__(self, iid: str, value: float):
153-
super(DoubleAttribute, self).__init__(iid)
151+
def __init__(self, iid: str, type_, value: float):
152+
super(DoubleAttribute, self).__init__(iid, type_)
154153
self._value = value
155154

156155
@staticmethod
157156
def _of(thing_proto: concept_proto.Thing):
158-
return DoubleAttribute(concept_proto_reader.iid(thing_proto.iid), thing_proto.value.double)
157+
return DoubleAttribute(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.attribute_type(thing_proto.type), thing_proto.value.double)
159158

160159
def get_value(self):
161160
return self._value
@@ -164,13 +163,13 @@ def is_double(self):
164163
return True
165164

166165
def as_remote(self, transaction):
167-
return RemoteDoubleAttribute(transaction, self.get_iid(), self.get_value())
166+
return RemoteDoubleAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
168167

169168

170169
class RemoteDoubleAttribute(RemoteAttribute):
171170

172-
def __init__(self, transaction, iid: str, value: float):
173-
super(RemoteDoubleAttribute, self).__init__(transaction, iid)
171+
def __init__(self, transaction, iid: str, type_, value: float):
172+
super(RemoteDoubleAttribute, self).__init__(transaction, iid, type_)
174173
self._value = value
175174

176175
def get_value(self):
@@ -180,18 +179,18 @@ def is_double(self):
180179
return True
181180

182181
def as_remote(self, transaction):
183-
return RemoteDoubleAttribute(transaction, self.get_iid(), self.get_value())
182+
return RemoteDoubleAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
184183

185184

186185
class StringAttribute(Attribute):
187186

188-
def __init__(self, iid: str, value: str):
189-
super(StringAttribute, self).__init__(iid)
187+
def __init__(self, iid: str, type_, value: str):
188+
super(StringAttribute, self).__init__(iid, type_)
190189
self._value = value
191190

192191
@staticmethod
193192
def _of(thing_proto: concept_proto.Thing):
194-
return StringAttribute(concept_proto_reader.iid(thing_proto.iid), thing_proto.value.string)
193+
return StringAttribute(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.attribute_type(thing_proto.type), thing_proto.value.string)
195194

196195
def get_value(self):
197196
return self._value
@@ -200,13 +199,13 @@ def is_string(self):
200199
return True
201200

202201
def as_remote(self, transaction):
203-
return RemoteStringAttribute(transaction, self.get_iid(), self.get_value())
202+
return RemoteStringAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
204203

205204

206205
class RemoteStringAttribute(RemoteAttribute):
207206

208-
def __init__(self, transaction, iid: str, value: str):
209-
super(RemoteStringAttribute, self).__init__(transaction, iid)
207+
def __init__(self, transaction, iid: str, type_, value: str):
208+
super(RemoteStringAttribute, self).__init__(transaction, iid, type_)
210209
self._value = value
211210

212211
def get_value(self):
@@ -216,18 +215,18 @@ def is_string(self):
216215
return True
217216

218217
def as_remote(self, transaction):
219-
return RemoteStringAttribute(transaction, self.get_iid(), self.get_value())
218+
return RemoteStringAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
220219

221220

222221
class DateTimeAttribute(Attribute):
223222

224-
def __init__(self, iid: str, value: datetime):
225-
super(DateTimeAttribute, self).__init__(iid)
223+
def __init__(self, iid: str, type_, value: datetime):
224+
super(DateTimeAttribute, self).__init__(iid, type_)
226225
self._value = value
227226

228227
@staticmethod
229228
def _of(thing_proto: concept_proto.Thing):
230-
return DateTimeAttribute(concept_proto_reader.iid(thing_proto.iid), datetime.fromtimestamp(float(thing_proto.value.date_time) / 1000.0))
229+
return DateTimeAttribute(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.attribute_type(thing_proto.type), datetime.fromtimestamp(float(thing_proto.value.date_time) / 1000.0))
231230

232231
def get_value(self):
233232
return self._value
@@ -236,13 +235,13 @@ def is_datetime(self):
236235
return True
237236

238237
def as_remote(self, transaction):
239-
return RemoteDateTimeAttribute(transaction, self.get_iid(), self.get_value())
238+
return RemoteDateTimeAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())
240239

241240

242241
class RemoteDateTimeAttribute(RemoteAttribute):
243242

244-
def __init__(self, transaction, iid: str, value: datetime):
245-
super(RemoteDateTimeAttribute, self).__init__(transaction, iid)
243+
def __init__(self, transaction, iid: str, type_, value: datetime):
244+
super(RemoteDateTimeAttribute, self).__init__(transaction, iid, type_)
246245
self._value = value
247246

248247
def get_value(self):
@@ -252,4 +251,4 @@ def is_datetime(self):
252251
return True
253252

254253
def as_remote(self, transaction):
255-
return RemoteDateTimeAttribute(transaction, self.get_iid(), self.get_value())
254+
return RemoteDateTimeAttribute(transaction, self.get_iid(), self.get_type(), self.get_value())

grakn/concept/thing/entity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class Entity(Thing):
2727

2828
@staticmethod
2929
def _of(thing_proto: concept_proto.Thing):
30-
return Entity(concept_proto_reader.iid(thing_proto.iid))
30+
return Entity(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.type_(thing_proto.type))
3131

3232
def as_remote(self, transaction):
33-
return RemoteEntity(transaction, self._iid)
33+
return RemoteEntity(transaction, self._iid, self.get_type())
3434

3535
def is_entity(self):
3636
return True
@@ -39,7 +39,7 @@ def is_entity(self):
3939
class RemoteEntity(RemoteThing):
4040

4141
def as_remote(self, transaction):
42-
return RemoteEntity(transaction, self._iid)
42+
return RemoteEntity(transaction, self._iid, self.get_type())
4343

4444
def is_entity(self):
4545
return True

grakn/concept/thing/relation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class Relation(Thing):
2828

2929
@staticmethod
3030
def _of(thing_proto: concept_proto.Thing):
31-
return Relation(concept_proto_reader.iid(thing_proto.iid))
31+
return Relation(concept_proto_reader.iid(thing_proto.iid), concept_proto_reader.type_(thing_proto.type))
3232

3333
def as_remote(self, transaction):
34-
return RemoteRelation(transaction, self.get_iid())
34+
return RemoteRelation(transaction, self.get_iid(), self.get_type())
3535

3636
def is_relation(self):
3737
return True
@@ -40,7 +40,7 @@ def is_relation(self):
4040
class RemoteRelation(RemoteThing):
4141

4242
def as_remote(self, transaction):
43-
return RemoteRelation(transaction, self.get_iid())
43+
return RemoteRelation(transaction, self.get_iid(), self.get_type())
4444

4545
def get_players_by_role_type(self):
4646
method = concept_proto.Thing.Req()

0 commit comments

Comments
 (0)