/** * 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; }
@Override public Collection<PropertyExecutor> undefine(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { AttributeType<Object> attributeType = executor.get(var).asAttributeType(); if (!attributeType.isDeleted() && regex().equals(attributeType.regex())) { attributeType.regex(null); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var).build()); }
/** * 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; } }
/** * * @return The data type of this {@link Attribute}'s {@link AttributeType}. */ @Override public AttributeType.DataType<D> dataType() { return type().dataType(); }
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); } } /**
/** * Checks if all the regex's of the types of this resource conforms to the value provided. * * @throws GraknTxOperationException when the value does not conform to the regex of its types * @param value The value to check the regexes against. */ private void checkConformsToRegexes(D value){ //Not checking the datatype because the regex will always be null for non strings. this.sups().forEach(sup -> { String regex = sup.regex(); if (regex != null && !Pattern.matches(regex, (String) value)) { throw GraknTxOperationException.regexFailure(this, (String) value, regex); } }); }
public static <T> void putResource(Thing thing, AttributeType<T> attributeType, T resource) { Attribute attributeInstance = attributeType.create(resource); thing.has(attributeInstance); }
/** * Make the second argument the super of the first argument * * @throws GraqlQueryException if the types are different, or setting the super to be a meta-type */ private static void setSuper(SchemaConcept subConcept, SchemaConcept superConcept) { if (superConcept.isEntityType()) { subConcept.asEntityType().sup(superConcept.asEntityType()); } else if (superConcept.isRelationshipType()) { subConcept.asRelationshipType().sup(superConcept.asRelationshipType()); } else if (superConcept.isRole()) { subConcept.asRole().sup(superConcept.asRole()); } else if (superConcept.isAttributeType()) { subConcept.asAttributeType().sup(superConcept.asAttributeType()); } else if (superConcept.isRule()) { subConcept.asRule().sup(superConcept.asRule()); } else { throw GraqlQueryException.insertMetaType(subConcept.getLabel(), superConcept); } } }
title.has(title); releaseDate = tx.putAttributeType("release-date", AttributeType.DataType.DATE); runtime = tx.putAttributeType("runtime", AttributeType.DataType.LONG); gender = tx.putAttributeType("gender", AttributeType.DataType.STRING).regex("(fe)?male"); realName = tx.putAttributeType("real-name", AttributeType.DataType.STRING); name = tx.putAttributeType("name", AttributeType.DataType.STRING);
public static ConceptMethod<Concept> putAttribute(Object value) { return ConceptMethod.builder(ConceptResponseType.CONCEPT) .requestSetter(builder -> builder.setPutAttribute(convertValue(value))) .function(concept -> concept.asAttributeType().putAttribute(value)) .build(); }
String valueAsString = axiom.getObject().getLiteral(); Object value = valueAsString; if (attributeType.getDataType() == AttributeType.DataType.BOOLEAN) { value = Boolean.parseBoolean(valueAsString); } else if (attributeType.getDataType() == AttributeType.DataType.LONG) { value = Long.parseLong(valueAsString); } else if (attributeType.getDataType() == AttributeType.DataType.DOUBLE) { value = Double.parseDouble(valueAsString); Attribute attribute = attributeType.putAttribute(value); RelationshipType propertyRelation = migrator.relation(axiom.getProperty().asOWLDataProperty()); Role entityRole = migrator.entityRole(entity.type(), attribute.type());
@Nullable public <T> Entity getEntity(T id, AttributeType<T> rtype){ Attribute<T> iri = rtype.getAttribute(id); Thing inst = iri != null? iri.ownerInstances().findFirst().orElse(null) : null; return inst != null? inst.asEntity() : null; }
private AttributeType resourceType() { return random.choose((Collection<AttributeType>) tx.admin().getMetaAttributeType().subs().collect(toSet())); }
private Attribute resource() { return chooseOrThrow((Stream<Attribute>) tx.admin().getMetaAttributeType().instances()); }
@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 final AttributeType.DataType<D> dataType() { return type().dataType(); }
/** * This method is overridden so that we can check that the regex of the new super type (if it has a regex) * can be applied to all the existing instances. */ @Override public AttributeType<D> sup(AttributeType<D> superType){ ((AttributeTypeImpl<D>) superType).sups().forEach(st -> checkInstancesMatchRegex(st.regex())); return super.sup(superType); }
/** * Logs a new {@link Keyspace} to the {@link KeyspaceStore}. * * @param keyspace The new {@link Keyspace} we have just created */ @Override public void addKeyspace(Keyspace keyspace){ if(containsKeyspace(keyspace)) return; try (EmbeddedGraknTx<?> tx = systemKeyspaceSession.transaction(GraknTxType.WRITE)) { AttributeType<String> keyspaceName = tx.getSchemaConcept(KEYSPACE_RESOURCE); if (keyspaceName == null) { throw GraknBackendException.initializationException(keyspace); } Attribute<String> attribute = keyspaceName.create(keyspace.getValue()); if (attribute.owner() == null) { tx.<EntityType>getSchemaConcept(KEYSPACE_ENTITY).create().has(attribute); } tx.commit(); // add to cache existingKeyspaces.add(keyspace); } catch (InvalidKBException e) { throw new RuntimeException("Could not add keyspace [" + keyspace + "] to system graph", e); } }