private static boolean mayHaveEdgeInstances(SchemaConcept concept) { return concept.isRelationshipType() && concept.isImplicit(); } }
@Nullable @Override public RelationshipType getRelationshipType(String label) { SchemaConcept concept = getSchemaConcept(Label.of(label)); if (concept == null || !concept.isRelationshipType()) return null; return concept.asRelationshipType(); }
@Override public boolean requiresMaterialisation() { return isUserDefined() && getSchemaConcept() != null && getSchemaConcept().isRelationshipType(); }
@Override public void checkValidProperty(GraknTx graph, VarPatternAdmin var) throws GraqlQueryException { Set<Label> roleTypes = relationPlayers().stream() .map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream) .map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream) .collect(toSet()); Optional<Label> maybeLabel = var.getProperty(IsaProperty.class).map(IsaProperty::type).flatMap(VarPatternAdmin::getTypeLabel); maybeLabel.ifPresent(label -> { SchemaConcept schemaConcept = graph.getSchemaConcept(label); if (schemaConcept == null || !schemaConcept.isRelationshipType()) { throw GraqlQueryException.notARelationType(label); } }); // Check all role types exist roleTypes.forEach(roleId -> { SchemaConcept schemaConcept = graph.getSchemaConcept(roleId); if (schemaConcept == null || !schemaConcept.isRole()) { throw GraqlQueryException.notARoleType(roleId); } }); }
public boolean isAppendRule(){ Atom headAtom = getHead().getAtom(); SchemaConcept headType = headAtom.getSchemaConcept(); if (headType.isRelationshipType() && headAtom.getVarName().isUserDefinedName()) { RelationshipAtom bodyAtom = getBody().getAtoms(RelationshipAtom.class) .filter(at -> Objects.nonNull(at.getSchemaConcept())) .filter(at -> at.getSchemaConcept().equals(headType)) .filter(at -> at.getVarName().isUserDefinedName()) .findFirst().orElse(null); return bodyAtom != null; } return false; }
@Override public Set<String> validateOntologically() { Set<String> errors = new HashSet<>(); SchemaConcept type = getSchemaConcept(); if (type != null && !type.isRelationshipType()){ errors.add(ErrorMessage.VALIDATION_RULE_INVALID_RELATION_TYPE.getMessage(type.label())); return errors; } //check role-type compatibility Map<Var, Type> varTypeMap = getParentQuery().getVarTypeMap(); for (Map.Entry<Role, Collection<Var>> e : getRoleVarMap().asMap().entrySet() ){ Role role = e.getKey(); if (!Schema.MetaSchema.isMetaLabel(role.label())) { //check whether this role can be played in this relation if (type != null && type.asRelationshipType().roles().noneMatch(r -> r.equals(role))) { errors.add(ErrorMessage.VALIDATION_RULE_ROLE_CANNOT_BE_PLAYED.getMessage(role.label(), type.label())); } //check whether the role player's type allows playing this role for (Var player : e.getValue()) { Type playerType = varTypeMap.get(player); if (playerType != null && playerType.playing().noneMatch(plays -> plays.equals(role))) { errors.add(ErrorMessage.VALIDATION_RULE_TYPE_CANNOT_PLAY_ROLE.getMessage(playerType.label(), role.label(), type == null? "" : type.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; }
/** * 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); } } }
/** * 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); } }
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; }