Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concept API clean up, refactors, and bug fixes #163

Merged
merged 8 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ java_library(
checkstyle_test(
name = "checkstyle",
targets = [":client-java"],
license_type = "apache"
license_type = "apache",
)


Expand Down
2 changes: 1 addition & 1 deletion Grakn.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import graql.lang.query.GraqlCompute;
import graql.lang.query.GraqlDefine;
import graql.lang.query.GraqlDelete;
import graql.lang.query.GraqlMatch;
import graql.lang.query.GraqlInsert;
import graql.lang.query.GraqlMatch;
import graql.lang.query.GraqlQuery;
import graql.lang.query.GraqlUndefine;

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.2
2.0.0
8 changes: 4 additions & 4 deletions rpc/RPCProtoBuilder.java → common/ProtoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package grakn.client.rpc;
package grakn.client.common;

import grabl.tracing.client.GrablTracingThreadStatic;
import grakn.client.GraknOptions;
Expand All @@ -30,17 +30,17 @@
import static grabl.tracing.client.GrablTracingThreadStatic.currentThreadTrace;
import static grabl.tracing.client.GrablTracingThreadStatic.isTracingEnabled;

abstract class RPCProtoBuilder {
public abstract class ProtoBuilder {

static OptionsProto.Options options(final GraknOptions options) {
public static OptionsProto.Options options(final GraknOptions options) {
final OptionsProto.Options.Builder builder = OptionsProto.Options.newBuilder();
options.infer().ifPresent(builder::setInfer);
options.explain().ifPresent(builder::setExplain);
options.batchSize().ifPresent(builder::setBatchSize);
return builder.build();
}

static Map<String, String> tracingData() {
public static Map<String, String> tracingData() {
if (isTracingEnabled()) {
final GrablTracingThreadStatic.ThreadTrace threadTrace = currentThreadTrace();
if (threadTrace == null) {
Expand Down
7 changes: 3 additions & 4 deletions common/exception/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ private ErrorMessage(String codePrefix, int codeNumber, String messagePrefix, St
public static class ClientInternal extends ErrorMessage {
public static final ClientInternal UNRECOGNISED_VALUE =
new ClientInternal(1, "Unrecognised schema value!");
public static final ClientInternal ILLEGAL_ARGUMENT_NULL =
new ClientInternal(2, "'%s' can not be null.");
public static final ClientInternal ILLEGAL_ARGUMENT_NULL_OR_EMPTY =
new ClientInternal(3, "'%s' can not be null or empty.");
// TODO: variable error messages like this (below) is not good as the error code no longer uniquely identifies the problem for users
public static final ClientInternal MISSING_ARGUMENT =
new ClientInternal(2, "'%s' can not be null or empty.");

private static final String codePrefix = "CIN";
private static final String messagePrefix = "Invalid Internal State (Client)";
Expand Down
118 changes: 14 additions & 104 deletions concept/Concept.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,166 +19,76 @@

package grakn.client.concept;

import grakn.client.Grakn;
import grakn.client.common.exception.GraknException;
import grakn.client.concept.thing.Thing;
import grakn.client.concept.thing.impl.ThingImpl;
import grakn.client.concept.type.Rule;
import grakn.client.concept.type.Type;
import grakn.client.concept.type.impl.TypeImpl;
import grakn.protocol.ConceptProto;

import javax.annotation.CheckReturnValue;

import static grakn.client.common.exception.ErrorMessage.Concept.INVALID_CONCEPT_CASTING;

/**
* The base concept implementation.
* A concept which can be every object in the graph.
* This class forms the basis of assuring the graph follows the Grakn object model.
* It provides methods to retrieve information about the Concept, and determine if it is a Type
* (EntityType, RoleType, RelationType, Rule or AttributeType)
* or an Thing (Entity, Relation, Attribute).
*/
public interface Concept {

/**
* Return as a Type if the Concept is a Type.
*
* @return A Type if the Concept is a Type
*/
@CheckReturnValue
Type asType();

/**
* Return as a Thing if the Concept is a Thing.
*
* @return A Thing if the Concept is a Thing
*/
@CheckReturnValue
Thing asThing();

/**
* Return as a Rule if the Concept is a Rule.
*
* @return A Rule if the Concept is a Rule
*/
@CheckReturnValue
Rule asRule();

/**
* Return a Concept.Remote for this Concept.
*
* @param concepts The Concept API to use for the RPCs.
* @return A remote concept using the given Concept API to enable RPCs.
*/
Remote asRemote(Concepts concepts);

/**
* Determine if the Concept is remote.
*
* @return true if the Concept is remote.
*/
Remote asRemote(Grakn.Transaction transaction);

default boolean isRemote() {
return false;
}

interface Local extends Concept {

static Concept.Local of(ConceptProto.Concept concept) {
if (concept.hasThing()) {
return Thing.Local.of(concept.getThing());
} else {
return Type.Local.of(concept.getType());
}
}

/**
* Return as a Type if the Concept is a Type.
*
* @return A Type if the Concept is a Type
*/
@Override
default Type.Local asType() {
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Type.class.getCanonicalName()));
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Type.class.getSimpleName()));
haikalpribadi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Return as a Thing if the Concept is a Thing.
*
* @return A Thing if the Concept is a Thing
*/
@Override
default Thing.Local asThing() {
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Thing.class.getCanonicalName()));
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Thing.class.getSimpleName()));
}

/**
* Return as a Rule if the Concept is a Rule.
*
* @return A Rule if the Concept is a Rule
*/
@Override
default Rule.Local asRule() {
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Rule.class.getCanonicalName()));
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Rule.class.getSimpleName()));
}
}

/**
* The base remote concept implementation.
*
* Provides the basic RPCs to delete a concept and check if it is deleted.
*/
interface Remote extends Concept {

static Concept.Remote of(final Concepts concepts, ConceptProto.Concept concept) {
static Concept.Remote of(final Grakn.Transaction transaction, ConceptProto.Concept concept) {
if (concept.hasThing()) {
return Thing.Remote.of(concepts, concept.getThing());
return ThingImpl.Remote.of(transaction, concept.getThing());
} else {
return Type.Remote.of(concepts, concept.getType());
return TypeImpl.Remote.of(transaction, concept.getType());
}
}

/**
* Delete the Concept
*/
void delete();

/**
* Return whether the concept has been deleted.
*/
boolean isDeleted();

/**
* Return as a Type if the Concept is a Type.
*
* @return A Type if the Concept is a Type
*/
@Override
default Type.Remote asType() {
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Type.class.getCanonicalName()));
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Type.class.getSimpleName()));
}

/**
* Return as a Thing if the Concept is a Thing.
*
* @return A Thing if the Concept is a Thing
*/
@Override
default Thing.Remote asThing() {
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Thing.class.getCanonicalName()));
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Thing.class.getSimpleName()));
}

/**
* Return as a Rule if the Concept is a Rule.
*
* @return A Rule if the Concept is a Rule
*/
@Override
default Rule.Remote asRule() {
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Rule.class.getCanonicalName()));
}

@Override
default Remote asRemote(Concepts concepts) {
return this;
throw new GraknException(INVALID_CONCEPT_CASTING.message(this, Rule.class.getSimpleName()));
}

@Override
Expand Down
Loading