@Nullable @Override public <V> AttributeType<V> getAttributeType(String label) { SchemaConcept concept = getSchemaConcept(Label.of(label)); if (concept == null || !concept.isAttributeType()) return null; return concept.asAttributeType(); }
@Override public Set<String> validateOntologically() { SchemaConcept type = getSchemaConcept(); Set<String> errors = new HashSet<>(); if (type == null) return errors; if (!type.isAttributeType()){ errors.add(ErrorMessage.VALIDATION_RULE_INVALID_ATTRIBUTE_TYPE.getMessage(type.label())); return errors; } Type ownerType = getParentQuery().getVarTypeMap().get(getVarName()); if (ownerType != null && ownerType.attributes().noneMatch(rt -> rt.equals(type.asAttributeType()))){ errors.add(ErrorMessage.VALIDATION_RULE_ATTRIBUTE_OWNER_CANNOT_HAVE_ATTRIBUTE.getMessage(type.label(), ownerType.label())); } return errors; }
/** * Map a Type to the Graql string representation * @param schemaConcept type to be mapped * @return Graql var equivalent to the given type */ public static VarPattern map(SchemaConcept schemaConcept) { VarPattern mapped = formatBase(schemaConcept); if (schemaConcept.isRelationshipType()) { mapped = map(mapped, schemaConcept.asRelationshipType()); } else if (schemaConcept.isAttributeType()) { mapped = map(mapped, schemaConcept.asAttributeType()); } return mapped; }
@Override public Stream<ConceptMap> materialise(){ ConceptMap substitution = getParentQuery().getSubstitution(); AttributeTypeImpl attributeType = AttributeTypeImpl.from(getSchemaConcept().asAttributeType()); Concept owner = substitution.get(getVarName()); Var resourceVariable = getPredicateVariable(); //if the attribute already exists, only attach a new link to the owner, otherwise create a new attribute Attribute attribute; if(this.isSpecific()){ Object value = Iterables.getOnlyElement(getMultiPredicate()).getPredicate().equalsValue().orElse(null); Attribute existingAttribute = attributeType.attribute(value); attribute = existingAttribute == null? attributeType.putAttributeInferred(value) : existingAttribute; } else { attribute = substitution.containsVar(resourceVariable)? substitution.get(resourceVariable).asAttribute() : null; } attachAttribute(owner, attribute); return Stream.of(substitution.merge(new ConceptMapImpl(ImmutableMap.of(resourceVariable, attribute)))); }
/** * 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 */ public 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.label(), superConcept); } }
/** * 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); } } }
private SchemaConcept putSchemaConcept() { SchemaConcept superConcept = use(SUPER_CONCEPT); Label label = use(LABEL); SchemaConcept concept; if (superConcept.isEntityType()) { concept = executor.tx().putEntityType(label); } else if (superConcept.isRelationshipType()) { concept = executor.tx().putRelationshipType(label); } else if (superConcept.isRole()) { concept = executor.tx().putRole(label); } else if (superConcept.isAttributeType()) { AttributeType attributeType = superConcept.asAttributeType(); AttributeType.DataType<?> dataType = useOrDefault(DATA_TYPE, attributeType.dataType()); concept = executor.tx().putAttributeType(label, dataType); } else if (superConcept.isRule()) { concept = executor.tx().putRule(label, use(WHEN), use(THEN)); } else { throw GraqlQueryException.insertMetaType(label, superConcept); } setSuper(concept, superConcept); return concept; }