private void attachAttribute(Concept owner, Attribute attribute){ if (owner.isEntity()){ EntityImpl.from(owner.asEntity()).attributeInferred(attribute); } else if (owner.isRelationship()){ RelationshipImpl.from(owner.asRelationship()).attributeInferred(attribute); } else if (owner.isAttribute()){ AttributeImpl.from(owner.asAttribute()).attributeInferred(attribute); } }
/** * * @param concept The element to be later validated */ public void trackForValidation(Concept concept) { if (concept.isThing()) { modifiedThings.add(concept.asThing()); } else if (concept.isRole()) { modifiedRoles.add(concept.asRole()); } else if (concept.isRelationshipType()) { modifiedRelationshipTypes.add(concept.asRelationshipType()); } else if (concept.isRule()){ modifiedRules.add(concept.asRule()); } } public void trackForValidation(Casting casting) {
@Override final Type asCurrentBaseType(Concept other) { return other.asType(); }
public static GrpcConcept.BaseType getBaseType(Concept concept) { if (concept.isEntityType()) { return GrpcConcept.BaseType.EntityType; } else if (concept.isRelationshipType()) { return GrpcConcept.BaseType.RelationshipType; } else if (concept.isAttributeType()) { return GrpcConcept.BaseType.AttributeType; } else if (concept.isEntity()) { return GrpcConcept.BaseType.Entity; } else if (concept.isRelationship()) { return GrpcConcept.BaseType.Relationship; } else if (concept.isAttribute()) { return GrpcConcept.BaseType.Attribute; } else if (concept.isRole()) { return GrpcConcept.BaseType.Role; } else if (concept.isRule()) { return GrpcConcept.BaseType.Rule; } else if (concept.isType()) { return GrpcConcept.BaseType.MetaType; } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } }
@Override protected Json concept(Concept concept) { Json json = Json.object("id", concept.id().getValue()); if (concept.isSchemaConcept()) { json.set("name", concept.asSchemaConcept().label().getValue()); SchemaConcept superConcept = concept.asSchemaConcept().sup(); if (superConcept != null) json.set("sub", superConcept.label().getValue()); } else if (concept.isThing()) { json.set("isa", concept.asThing().type().label().getValue()); } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } if (concept.isAttribute()) { json.set("value", concept.asAttribute().value()); } if (concept.isRule()) { Pattern when = concept.asRule().when(); if (when != null) { json.set("when", when.toString()); } Pattern then = concept.asRule().then(); if (then != null) { json.set("then", then.toString()); } } return json; }
@Override public String toString() { if (concept.isAttribute()) { return "hasValue(" + valueToString(concept.asAttribute().value()) + ")"; } else if (concept.isThing()) { Thing thing = concept.asThing(); Stream<Attribute<?>> resources = thing.attributes(); Optional<?> value = resources .filter(resource -> NAME_TYPES.contains(resource.type().label())) .map(Attribute::value).findFirst(); return "instance(" + value.map(StringUtil::valueToString).orElse("") + ") isa " + thing.type().label(); } else if (concept.isType()) { return "type(" + concept.asType().label() + ")"; } else if (concept.isRole()) { return "role(" + concept.asRole().label() + ")"; } else if (concept.isRule()) { return "rule(" + concept.asRule().label() + ")"; } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } }
/** * * @param concept The concept to no longer track */ @SuppressWarnings("SuspiciousMethodCalls") public void remove(Concept concept){ modifiedThings.remove(concept); modifiedRoles.remove(concept); modifiedRelationshipTypes.remove(concept); modifiedRules.remove(concept); if(concept.isAttribute()) { newAttributes.removeAll(AttributeImpl.from(concept.asAttribute()).getIndex()); } if(concept.isRelationship()){ newRelationships.remove(concept.asRelationship()); } conceptCache.remove(concept.id()); if (concept.isSchemaConcept()) { Label label = concept.asSchemaConcept().label(); schemaConceptCache.remove(label); labelCache.remove(label); } }
@Override final Attribute<D> asCurrentBaseType(Concept other) { return other.asAttribute(); } }
return GET_ATTRIBUTE_TYPES; case SETATTRIBUTETYPE: return setAttributeType(converter.convert(conceptMethod.getSetAttributeType()).asAttributeType()); case UNSETATTRIBUTETYPE: return unsetAttributeType(converter.convert(conceptMethod.getUnsetAttributeType()).asAttributeType()); case GETKEYTYPES: return GET_KEY_TYPES; case SETDIRECTSUPERCONCEPT: GrpcConcept.Concept setDirectSuperConcept = conceptMethod.getSetDirectSuperConcept(); SchemaConcept schemaConcept = converter.convert(setDirectSuperConcept).asSchemaConcept(); return setDirectSuperConcept(schemaConcept); case UNSETROLEPLAYER: return getRolePlayersByRoles(roles); case SETKEYTYPE: return setKeyType(converter.convert(conceptMethod.getSetKeyType()).asAttributeType()); case UNSETKEYTYPE: return unsetKeyType(converter.convert(conceptMethod.getUnsetKeyType()).asAttributeType()); case SETABSTRACT: return setAbstract(conceptMethod.getSetAbstract()); case SETROLEPLAYEDBYTYPE: return setRolePlayedByType(converter.convert(conceptMethod.getSetRolePlayedByType()).asRole()); case UNSETROLEPLAYEDBYTYPE: return unsetRolePlayedByType(converter.convert(conceptMethod.getUnsetRolePlayedByType()).asRole()); case ADDENTITY: return ADD_ENTITY; case SETRELATEDROLE: return setRelatedRole(converter.convert(conceptMethod.getSetRelatedRole()).asRole());
@Override protected final Thing asInstance(Concept concept) { return concept.asThing(); } }
@Override public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Concept concept = executor.get(var); if (concept.isType()) { concept.asType().isAbstract(true); } else { throw GraqlQueryException.insertAbstractOnNonType(concept.asSchemaConcept()); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var).build()); }
@Override final Relationship asSelf(Concept concept) { return concept.asRelationship(); } }
@Override public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Role role = executor.get(this.role().var()).asRole(); executor.get(var).asType().plays(role); }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var, role().var()).build()); }
@Override public Collection<PropertyExecutor> undefine(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { RelationshipType relationshipType = executor.get(var).asRelationshipType(); Role role = executor.get(this.role().var()).asRole(); if (!relationshipType.isDeleted() && !role.isDeleted()) { relationshipType.unrelate(role); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var, role().var()).build()); }
@Override final RelationshipType asCurrentType(Concept concept) { return concept.asRelationshipType(); }
@Override public <V> Collection<Attribute<V>> getAttributesByValue(V value) { if (value == null) return Collections.emptySet(); //Make sure you trying to retrieve supported data type if (!AttributeType.DataType.SUPPORTED_TYPES.containsKey(value.getClass().getName())) { throw GraknTxOperationException.unsupportedDataType(value); } HashSet<Attribute<V>> attributes = new HashSet<>(); AttributeType.DataType dataType = AttributeType.DataType.SUPPORTED_TYPES.get(value.getClass().getTypeName()); //noinspection unchecked getConcepts(dataType.getVertexProperty(), dataType.getPersistenceValue(value)).forEach(concept -> { if (concept != null && concept.isAttribute()) { //noinspection unchecked attributes.add(concept.asAttribute()); } }); return attributes; }