@Override protected void buildInstances(GraknTx tx) { colonelWest = putEntityWithResource(tx, "colonelWest", person, key.label()); Nono = putEntityWithResource(tx, "Nono", country, key.label()); America = putEntityWithResource(tx, "America", country, key.label()); Tomahawk = putEntityWithResource(tx, "Tomahawk", rocket, key.label()); putResource(colonelWest, nationality, "American"); putResource(Tomahawk, propulsion, "gsp"); }
@Override public void buildInstances(GraknTx tx) { Europe = putEntityWithResource(tx, "Europe", continent, key.label()); Poland = putEntityWithResource(tx, "Poland", country, key.label()); Masovia = putEntityWithResource(tx, "Masovia", region, key.label()); Silesia = putEntityWithResource(tx, "Silesia", region, key.label()); Warsaw = putEntityWithResource(tx, "Warsaw", city, key.label()); Wroclaw = putEntityWithResource(tx, "Wroclaw", city, key.label()); UW = putEntityWithResource(tx, "University-of-Warsaw", university, key.label()); PW = putEntityWithResource(tx, "Warsaw-Polytechnics", university, key.label()); England = putEntityWithResource(tx, "England", country, key.label()); GreaterLondon = putEntityWithResource(tx, "GreaterLondon", region, key.label()); London = putEntityWithResource(tx, "London", city, key.label()); Imperial = putEntityWithResource(tx, "Imperial College London", university, key.label()); UCL = putEntityWithResource(tx, "University College London", university, key.label()); Germany = putEntityWithResource(tx, "Germany", country, key.label()); Bavaria = putEntityWithResource(tx, "Bavaria", region, key.label()); Munich = putEntityWithResource(tx, "Munich", city, key.label()); putEntityWithResource(tx, "University of Munich", university, key.label()); France = putEntityWithResource(tx, "France", country, key.label()); IleDeFrance = putEntityWithResource(tx, "IleDeFrance", region, key.label()); Paris = putEntityWithResource(tx, "Paris", city, key.label()); Italy = putEntityWithResource(tx, "Italy", country, key.label()); Lombardy = putEntityWithResource(tx, "Lombardy", region, key.label()); Milan = putEntityWithResource(tx, "Milan", city, key.label()); }
@Override Iterable<? super MatchableConcept> transform(MatchableConcept item) { return item.get().asThing().attributes() .filter(resource -> MatchableConcept.NAME_TYPES.contains(resource.type().label())) .map(MatchableConcept::of) .collect(toSet()); }
/** * Check if the given {@link Attribute} conforms to the has syntax and structural requirements * @param attribute {@link Attribute} to check * @return true if the {@link Attribute} is target of has relation */ private static boolean isHasResourceResource(Attribute attribute){ AttributeType attributeType = attribute.type(); // TODO: Make sure this is tested boolean plays = attributeType.playing().map(Role::label) .allMatch(c -> c.equals(HAS_VALUE.getLabel(attributeType.label()))); return attribute.owners().findAny().isPresent() && plays; } }
/** * Thrown when trying to set a {@code value} on the {@code resource} which does not conform to it's regex */ public static GraknTxOperationException regexFailure(AttributeType attributeType, String value, String regex){ return create(ErrorMessage.REGEX_INSTANCE_FAILURE.getMessage(regex, attributeType.label(), value)); }
/** * Thrown when attempting to set a regex on a {@link Attribute} whose type {@link AttributeType} is not of the * data type {@link AttributeType.DataType#STRING} */ public static GraknTxOperationException cannotSetRegex(AttributeType attributeType){ return create(REGEX_NOT_STRING.getMessage(attributeType.label())); }
/** * Thrown when {@code type} has {@code attributeType} as a {@link Type#key(AttributeType)} and a {@link Type#has(AttributeType)} */ public static GraknTxOperationException duplicateHas(Type type, AttributeType attributeType){ return create(ErrorMessage.CANNOT_BE_KEY_AND_ATTRIBUTE.getMessage(type.label(), attributeType.label())); }
/** * Add the resources of an entity * @param var var representing the entity * @param thing thing containing resource information * @return var pattern with resources */ private static VarPattern hasResources(VarPattern var, Thing thing){ for(Attribute attribute : thing.attributes().collect(Collectors.toSet())){ var = var.has(attribute.type().label(), var().val(attribute.value())); } return var; }
/** * Helper method to delete a {@link AttributeType} which is possible linked to this {@link Type}. * The link to {@link AttributeType} is removed if <code>attributeToRemove</code> is in the candidate list * <code>attributeTypes</code> * * @param implicitType the {@link Schema.ImplicitType} which specifies which implicit {@link Role} should be removed * @param attributeTypes The list of candidate which potentially contains the {@link AttributeType} to remove * @param attributeToRemove the {@link AttributeType} to remove * @return the {@link Type} itself */ private T deleteAttribute(Schema.ImplicitType implicitType, Stream<AttributeType> attributeTypes, AttributeType attributeToRemove){ if(attributeTypes.anyMatch(a -> a.equals(attributeToRemove))){ Label label = implicitType.getLabel(attributeToRemove.label()); Role role = vertex().tx().getSchemaConcept(label); if(role != null) unplay(role); } return getThis(); }
@Override public T unhas(Attribute attribute){ Role roleHasOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().label())); Role roleKeyOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_OWNER.getLabel(attribute.type().label())); Role roleHasValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().label())); Role roleKeyValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_VALUE.getLabel(attribute.type().label())); Stream<Relationship> relationships = relationships(filterNulls(roleHasOwner, roleKeyOwner)); relationships.filter(relationship -> { Stream<Thing> rolePlayers = relationship.rolePlayers(filterNulls(roleHasValue, roleKeyValue)); return rolePlayers.anyMatch(rolePlayer -> rolePlayer.equals(attribute)); }).forEach(Concept::delete); return getThis(); }
/** * Thrown when a {@link Thing} is not allowed to have {@link Attribute} of that {@link AttributeType} */ public static GraknTxOperationException hasNotAllowed(Thing thing, Attribute attribute){ return create(HAS_INVALID.getMessage(thing.type().label(), attribute.type().label())); }
/** * Helper method to validate that the target types are of one data type, and get that data type * * @return the DataType of the target types */ @Nullable private AttributeType.DataType<?> validateAndGetTargetDataType() { AttributeType.DataType<?> dataType = null; for (Type type : targetTypes()) { // check if the selected type is a attribute type if (!type.isAttributeType()) throw GraqlQueryException.mustBeAttributeType(type.label()); AttributeType<?> attributeType = type.asAttributeType(); if (dataType == null) { // check if the attribute type has data-type LONG or DOUBLE dataType = attributeType.dataType(); if (!dataType.equals(AttributeType.DataType.LONG) && !dataType.equals(AttributeType.DataType.DOUBLE)) { throw GraqlQueryException.attributeMustBeANumber(dataType, attributeType.label()); } } else { // check if all the attribute types have the same data-type if (!dataType.equals(attributeType.dataType())) { throw GraqlQueryException.attributesWithDifferentDataTypes(query.of().get()); } } } return dataType; }
public static <D> AttributeImpl<D> create(VertexElement vertexElement, AttributeType<D> type, Object value) { Object persistenceValue = castValue(type.dataType(), value); AttributeImpl<D> attribute = new AttributeImpl<>(vertexElement, type, persistenceValue); //Generate the index again. Faster than reading String index = Schema.generateAttributeIndex(type.label(), value.toString()); vertexElement.property(Schema.VertexProperty.INDEX, index); //Track the attribute by index vertexElement.tx().txCache().addNewAttribute(index, attribute.id()); return attribute; }
@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); } }
@Override protected void buildInstances(GraknTx tx) { Thing Alice = putEntityWithResource(tx, "Alice", applicant, key.label()); Thing Bob = putEntityWithResource(tx, "Bob", applicant, key.label()); Thing Charlie = putEntityWithResource(tx, "Charlie", applicant, key.label()); Thing Denis = putEntityWithResource(tx, "Denis", applicant, key.label()); Thing Eva = putEntityWithResource(tx, "Eva", applicant, key.label()); Thing Frank = putEntityWithResource(tx, "Frank", applicant, key.label());
public Pattern pattern(Attribute concept) { Var owner = Graql.var().asUserDefined(); VarPattern resourceVar = Graql.var().asUserDefined().val(concept.value()); return owner .has(concept.type().label(),resourceVar) .id(concept.owner().id()); } }
private Relationship attributeRelationship(Attribute attribute, boolean isInferred) { Schema.ImplicitType has = Schema.ImplicitType.HAS; Schema.ImplicitType hasValue = Schema.ImplicitType.HAS_VALUE; Schema.ImplicitType hasOwner = Schema.ImplicitType.HAS_OWNER; //Is this attribute a key to me? if(type().keys().anyMatch(rt -> rt.equals(attribute.type()))){ has = Schema.ImplicitType.KEY; hasValue = Schema.ImplicitType.KEY_VALUE; hasOwner = Schema.ImplicitType.KEY_OWNER; } Label label = attribute.type().label(); RelationshipType hasAttribute = vertex().tx().getSchemaConcept(has.getLabel(label)); Role hasAttributeOwner = vertex().tx().getSchemaConcept(hasOwner.getLabel(label)); Role hasAttributeValue = vertex().tx().getSchemaConcept(hasValue.getLabel(label)); if(hasAttribute == null || hasAttributeOwner == null || hasAttributeValue == null || type().playing().noneMatch(play -> play.equals(hasAttributeOwner))){ throw GraknTxOperationException.hasNotAllowed(this, attribute); } EdgeElement attributeEdge = addEdge(AttributeImpl.from(attribute), Schema.EdgeLabel.ATTRIBUTE); if(isInferred) attributeEdge.property(Schema.EdgeProperty.IS_INFERRED, true); return vertex().tx().factory().buildRelation(attributeEdge, hasAttribute, hasAttributeOwner, hasAttributeValue); }
private void updateAttributeRelationHierarchy(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, Role ownerRole, Role valueRole, RelationshipType relationshipType){ AttributeType attributeTypeSuper = attributeType.sup(); Label superLabel = attributeTypeSuper.label(); Role ownerRoleSuper = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(superLabel)); Role valueRoleSuper = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(superLabel)); RelationshipType relationshipTypeSuper = vertex().tx().putRelationTypeImplicit(has.getLabel(superLabel)). relates(ownerRoleSuper).relates(valueRoleSuper); //Create the super type edges from sub role/relations to super roles/relation ownerRole.sup(ownerRoleSuper); valueRole.sup(valueRoleSuper); relationshipType.sup(relationshipTypeSuper); if (!Schema.MetaSchema.ATTRIBUTE.getLabel().equals(superLabel)) { //Make sure the supertype attribute is linked with the role as well ((AttributeTypeImpl) attributeTypeSuper).plays(valueRoleSuper); updateAttributeRelationHierarchy(attributeTypeSuper, has, hasValue, hasOwner, ownerRoleSuper, valueRoleSuper, relationshipTypeSuper); } } /**
/** * Creates a relation type which allows this type and a {@link ai.grakn.concept.Attribute} type to be linked. * @param attributeType The {@link AttributeType} which instances of this type should be allowed to play. * @param has the implicit relation type to build * @param hasValue the implicit role type to build for the {@link AttributeType} * @param hasOwner the implicit role type to build for the type * @param required Indicates if the {@link ai.grakn.concept.Attribute} is required on the entity * @return The {@link Type} itself */ private T has(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, boolean required){ Label attributeLabel = attributeType.label(); Role ownerRole = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(attributeLabel)); Role valueRole = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(attributeLabel)); RelationshipType relationshipType = vertex().tx().putRelationTypeImplicit(has.getLabel(attributeLabel)). relates(ownerRole). relates(valueRole); //this plays ownerRole; this.play(ownerRole, required); //TODO: Use explicit cardinality of 0-1 rather than just false //attributeType plays valueRole; ((AttributeTypeImpl) attributeType).play(valueRole, false); updateAttributeRelationHierarchy(attributeType, has, hasValue, hasOwner, ownerRole, valueRole, relationshipType); return getThis(); }