Skip to content

Commit

Permalink
Make getSupertypes() return all supertypes, including 'thing' (#211)
Browse files Browse the repository at this point in the history
## What is the goal of this PR?

Comply with the latest change where `thing` must be returned when invoking `getSupertypes()` on a subtype of thing such as `entity`, `attribute`, and `relation`.

## What are the changes implemented in this PR?

1. `ConceptManager::getType` has been replaced with `ConceptManager::getThingType`
2. `getSupertypes()` on `AttributeType`, `EntityType` and `RelationType` must now include `thing`, we can no longer offer specialised implementation for each type and therefore they have been replaced with a generic implementation that sits at `ThingType`.
  • Loading branch information
Ganeshwara Hananda authored Dec 31, 2020
1 parent cdb4ccf commit 861d76d
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 92 deletions.
30 changes: 15 additions & 15 deletions concept/ConceptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import grakn.client.concept.type.impl.AttributeTypeImpl;
import grakn.client.concept.type.impl.EntityTypeImpl;
import grakn.client.concept.type.impl.RelationTypeImpl;
import grakn.client.concept.type.impl.TypeImpl;
import grakn.client.concept.type.impl.ThingTypeImpl;
import grakn.client.rpc.RPCTransaction;
import grakn.protocol.ConceptProto;
import grakn.protocol.TransactionProto;
Expand All @@ -52,22 +52,22 @@ public ConceptManager(RPCTransaction rpcTransaction) {

@CheckReturnValue
public ThingType getRootThingType() {
return getType(GraqlToken.Type.THING.toString()).asThingType();
return getThingType(GraqlToken.Type.THING.toString());
}

@CheckReturnValue
public EntityType getRootEntityType() {
return getType(GraqlToken.Type.ENTITY.toString()).asEntityType();
return getEntityType(GraqlToken.Type.ENTITY.toString());
}

@CheckReturnValue
public RelationType getRootRelationType() {
return getType(GraqlToken.Type.RELATION.toString()).asRelationType();
return getRelationType(GraqlToken.Type.RELATION.toString());
}

@CheckReturnValue
public AttributeType getRootAttributeType() {
return getType(GraqlToken.Type.ATTRIBUTE.toString()).asAttributeType();
return getAttributeType(GraqlToken.Type.ATTRIBUTE.toString());
}

public EntityType putEntityType(String label) {
Expand All @@ -81,8 +81,8 @@ public EntityType putEntityType(String label) {
@Nullable
@CheckReturnValue
public EntityType getEntityType(String label) {
final Type type = getType(label);
if (type instanceof EntityType) return type.asEntityType();
final ThingType thingType = getThingType(label);
if (thingType != null && thingType.isEntityType()) return thingType.asEntityType();
else return null;
}

Expand All @@ -97,8 +97,8 @@ public RelationType putRelationType(String label) {
@Nullable
@CheckReturnValue
public RelationType getRelationType(String label) {
final Type type = getType(label);
if (type instanceof RelationType) return type.asRelationType();
final ThingType thingType = getThingType(label);
if (thingType != null && thingType.isRelationType()) return thingType.asRelationType();
else return null;
}

Expand All @@ -114,8 +114,8 @@ public AttributeType putAttributeType(String label, AttributeType.ValueType valu
@Nullable
@CheckReturnValue
public AttributeType getAttributeType(String label) {
final Type type = getType(label);
if (type instanceof AttributeType) return type.asAttributeType();
final ThingType thingType = getThingType(label);
if (thingType != null && thingType.isAttributeType()) return thingType.asAttributeType();
else return null;
}

Expand All @@ -134,13 +134,13 @@ public Thing getThing(String iid) {

@Nullable
@CheckReturnValue
public Type getType(String label) {
public ThingType getThingType(String label) {
final ConceptProto.ConceptManager.Req req = ConceptProto.ConceptManager.Req.newBuilder()
.setGetTypeReq(ConceptProto.ConceptManager.GetType.Req.newBuilder().setLabel(label)).build();
.setGetThingTypeReq(ConceptProto.ConceptManager.GetThingType.Req.newBuilder().setLabel(label)).build();

final ConceptProto.ConceptManager.Res response = execute(req);
if (response.getGetTypeRes().getResCase() == ConceptProto.ConceptManager.GetType.Res.ResCase.TYPE)
return TypeImpl.of(response.getGetTypeRes().getType());
if (response.getGetThingTypeRes().getResCase() == ConceptProto.ConceptManager.GetThingType.Res.ResCase.THING_TYPE)
return ThingTypeImpl.of(response.getGetThingTypeRes().getThingType());
else
return null;
}
Expand Down
23 changes: 5 additions & 18 deletions concept/type/AttributeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public interface AttributeType extends ThingType {

boolean isKeyable();

@Override
default boolean isAttributeType() {
return true;
}

@Override
AttributeType.Remote asRemote(Grakn.Transaction transaction);

Expand Down Expand Up @@ -120,9 +125,6 @@ interface Remote extends ThingType.Remote, AttributeType {
@Override
AttributeType getSupertype();

@Override
Stream<? extends AttributeType> getSupertypes();

@Override
Stream<? extends AttributeType> getSubtypes();

Expand Down Expand Up @@ -164,9 +166,6 @@ interface Remote extends AttributeType.Boolean, AttributeType.Remote {
@Override
AttributeType.Boolean getSupertype();

@Override
Stream<? extends AttributeType.Boolean> getSupertypes();

@Override
Stream<? extends AttributeType.Boolean> getSubtypes();

Expand All @@ -192,9 +191,6 @@ interface Remote extends AttributeType.Long, AttributeType.Remote {
@Override
AttributeType.Long getSupertype();

@Override
Stream<? extends AttributeType.Long> getSupertypes();

@Override
Stream<? extends AttributeType.Long> getSubtypes();

Expand All @@ -220,9 +216,6 @@ interface Remote extends AttributeType.Double, AttributeType.Remote {
@Override
AttributeType.Double getSupertype();

@Override
Stream<? extends AttributeType.Double> getSupertypes();

@Override
Stream<? extends AttributeType.Double> getSubtypes();

Expand All @@ -248,9 +241,6 @@ interface Remote extends AttributeType.String, AttributeType.Remote {
@Override
AttributeType.String getSupertype();

@Override
Stream<? extends AttributeType.String> getSupertypes();

@Override
Stream<? extends AttributeType.String> getSubtypes();

Expand Down Expand Up @@ -281,9 +271,6 @@ interface Remote extends AttributeType.DateTime, AttributeType.Remote {
@Override
AttributeType.DateTime getSupertype();

@Override
Stream<? extends AttributeType.DateTime> getSupertypes();

@Override
Stream<? extends AttributeType.DateTime> getSubtypes();

Expand Down
8 changes: 5 additions & 3 deletions concept/type/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

public interface EntityType extends ThingType {

@Override
default boolean isEntityType() {
return true;
}

@Override
EntityType.Remote asRemote(Grakn.Transaction transaction);

Expand All @@ -38,9 +43,6 @@ interface Remote extends ThingType.Remote, EntityType {
@Override
EntityType getSupertype();

@Override
Stream<? extends EntityType> getSupertypes();

@Override
Stream<? extends EntityType> getSubtypes();

Expand Down
8 changes: 5 additions & 3 deletions concept/type/RelationType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

public interface RelationType extends ThingType {

@Override
default boolean isRelationType() {
return true;
}

@Override
RelationType.Remote asRemote(Grakn.Transaction transaction);

Expand All @@ -50,9 +55,6 @@ interface Remote extends ThingType.Remote, RelationType {
@Override
RelationType getSupertype();

@Override
Stream<? extends RelationType> getSupertypes();

@Override
Stream<? extends RelationType> getSubtypes();

Expand Down
5 changes: 5 additions & 0 deletions concept/type/RoleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public interface RoleType extends Type {

String getScopedLabel();

@Override
default boolean isRoleType() {
return true;
}

@Override
RoleType.Remote asRemote(Grakn.Transaction transaction);

Expand Down
5 changes: 5 additions & 0 deletions concept/type/ThingType.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

public interface ThingType extends Type {

@Override
default boolean isThingType() {
return true;
}

@Override
ThingType.Remote asRemote(Grakn.Transaction transaction);

Expand Down
20 changes: 20 additions & 0 deletions concept/type/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ public interface Type extends Concept {
@CheckReturnValue
boolean isRoot();

default boolean isThingType() {
return false;
}

default boolean isEntityType() {
return false;
}

default boolean isAttributeType() {
return false;
}

default boolean isRelationType() {
return false;
}

default boolean isRoleType() {
return false;
}

@CheckReturnValue
ThingType asThingType();

Expand Down
32 changes: 2 additions & 30 deletions concept/type/impl/AttributeTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@

import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Stream;

import static grakn.client.common.exception.ErrorMessage.Concept.BAD_VALUE_TYPE;
import static grakn.client.common.exception.ErrorMessage.Concept.INVALID_CONCEPT_CASTING;
import static grakn.client.concept.proto.ConceptProtoBuilder.attributeValue;
import static grakn.common.util.Objects.className;
import static java.util.stream.Collectors.toList;

public class AttributeTypeImpl extends ThingTypeImpl implements AttributeType {

Expand Down Expand Up @@ -170,11 +172,6 @@ public AttributeTypeImpl getSupertype() {
return supertype != null ? supertype.asAttributeType() : null;
}

@Override
public Stream<? extends AttributeTypeImpl> getSupertypes() {
return super.getSupertypes().map(ThingTypeImpl::asAttributeType);
}

@Override
public Stream<? extends AttributeTypeImpl> getSubtypes() {
final Stream<AttributeTypeImpl> stream = super.getSubtypes().map(TypeImpl::asAttributeType);
Expand Down Expand Up @@ -327,11 +324,6 @@ public final AttributeTypeImpl.Boolean getSupertype() {
return supertype != null ? supertype.asBoolean() : null;
}

@Override
public final Stream<AttributeTypeImpl.Boolean> getSupertypes() {
return super.getSupertypes().map(AttributeTypeImpl::asBoolean);
}

@Override
public final Stream<AttributeTypeImpl.Boolean> getSubtypes() {
return super.getSubtypes().map(AttributeTypeImpl::asBoolean);
Expand Down Expand Up @@ -413,11 +405,6 @@ public final AttributeTypeImpl.Long getSupertype() {
return supertype != null ? supertype.asLong() : null;
}

@Override
public final Stream<AttributeTypeImpl.Long> getSupertypes() {
return super.getSupertypes().map(AttributeTypeImpl::asLong);
}

@Override
public final Stream<AttributeTypeImpl.Long> getSubtypes() {
return super.getSubtypes().map(AttributeTypeImpl::asLong);
Expand Down Expand Up @@ -499,11 +486,6 @@ public final AttributeTypeImpl.Double getSupertype() {
return supertype != null ? supertype.asDouble() : null;
}

@Override
public final Stream<AttributeTypeImpl.Double> getSupertypes() {
return super.getSupertypes().map(AttributeTypeImpl::asDouble);
}

@Override
public final Stream<AttributeTypeImpl.Double> getSubtypes() {
return super.getSubtypes().map(AttributeTypeImpl::asDouble);
Expand Down Expand Up @@ -585,11 +567,6 @@ public final AttributeTypeImpl.String getSupertype() {
return supertype != null ? supertype.asString() : null;
}

@Override
public final Stream<AttributeTypeImpl.String> getSupertypes() {
return super.getSupertypes().map(AttributeTypeImpl::asString);
}

@Override
public final Stream<AttributeTypeImpl.String> getSubtypes() {
return super.getSubtypes().map(AttributeTypeImpl::asString);
Expand Down Expand Up @@ -689,11 +666,6 @@ public final AttributeTypeImpl.DateTime getSupertype() {
return supertype != null ? supertype.asDateTime() : null;
}

@Override
public final Stream<AttributeTypeImpl.DateTime> getSupertypes() {
return super.getSupertypes().map(AttributeTypeImpl::asDateTime);
}

@Override
public final Stream<AttributeTypeImpl.DateTime> getSubtypes() {
return super.getSubtypes().map(AttributeTypeImpl::asDateTime);
Expand Down
5 changes: 0 additions & 5 deletions concept/type/impl/EntityTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ public EntityTypeImpl getSupertype() {
return supertype != null ? supertype.asEntityType() : null;
}

@Override
public final Stream<EntityTypeImpl> getSupertypes() {
return super.getSupertypes().map(ThingTypeImpl::asEntityType);
}

@Override
public final Stream<EntityTypeImpl> getSubtypes() {
return super.getSubtypes().map(ThingTypeImpl::asEntityType);
Expand Down
5 changes: 0 additions & 5 deletions concept/type/impl/RelationTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ public RelationTypeImpl getSupertype() {
return supertype != null ? supertype.asRelationType() : null;
}

@Override
public final Stream<RelationTypeImpl> getSupertypes() {
return super.getSupertypes().map(ThingTypeImpl::asRelationType);
}

@Override
public final Stream<RelationTypeImpl> getSubtypes() {
return super.getSubtypes().map(ThingTypeImpl::asRelationType);
Expand Down
4 changes: 3 additions & 1 deletion concept/type/impl/ThingTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import grakn.client.concept.type.AttributeType.ValueType;
import grakn.client.concept.type.RoleType;
import grakn.client.concept.type.ThingType;
import grakn.client.concept.type.Type;
import grakn.protocol.ConceptProto;

import java.util.stream.Stream;
Expand Down Expand Up @@ -85,7 +86,8 @@ public ThingTypeImpl getSupertype() {

@Override
public Stream<? extends ThingTypeImpl> getSupertypes() {
return super.getSupertypes().map(TypeImpl::asThingType);
final Stream<? extends TypeImpl> supertypes = super.getSupertypes();
return supertypes.map(TypeImpl::asThingType);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion concept/type/impl/TypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public final void delete() {

@Override
public final boolean isDeleted() {
return rpcTransaction.concepts().getType(label) == null;
return rpcTransaction.concepts().getThingType(label) == null;
}

final Grakn.Transaction tx() {
Expand Down
Loading

0 comments on commit 861d76d

Please sign in to comment.