public void addRolePlayer(Role role, Thing thing) { Objects.requireNonNull(role); Objects.requireNonNull(thing); if(Schema.MetaSchema.isMetaLabel(role.label())) throw GraknTxOperationException.metaTypeImmutable(role.label()); //Do the actual put of the role and role player putRolePlayerEdge(role, thing); }
@Override public Stream<Role> toCompatibleRoles(Role entryRole) { return entryRole.subs(); } }
/** * * @param role The Role to validate * @return An error message if the relates does not have a single incoming RELATES edge */ static Optional<String> validateHasSingleIncomingRelatesEdge(Role role){ if(!role.relationships().findAny().isPresent()) { return Optional.of(VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE.getMessage(role.label())); } return Optional.empty(); }
/** * @param typedVar variable of interest * @param parentType to be checked * @return true if typing the typeVar with type is compatible with role configuration of this query */ @Override public boolean isTypeRoleCompatible(Var typedVar, Type parentType){ if (parentType == null || Schema.MetaSchema.isMetaLabel(parentType.label())) return true; Set<Type> parentTypes = parentType.subs().collect(Collectors.toSet()); return getAtoms(RelationshipAtom.class) .filter(ra -> ra.getVarNames().contains(typedVar)) .noneMatch(ra -> ra.getRoleVarMap().entries().stream() //get roles this type needs to play .filter(e -> e.getValue().equals(typedVar)) .filter(e -> !Schema.MetaSchema.isMetaLabel(e.getKey().label())) //check if it can play it .anyMatch(e -> e.getKey().players().noneMatch(parentTypes::contains))); }
superRelationType.sups().forEach(rel -> rel.roles().forEach(roleType -> allSuperRolesPlayed.add(roleType.label()))); errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(relate.label(), relationshipType.label(), "super", "super", superRelationType.label())); boolean subRoleNotFoundInRelates = superRelate.subs().noneMatch(sub -> relatesLabels.contains(sub.label())); errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.label(), superRelationType.label(), "sub", "sub", relationshipType.label()));
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); } } /**
/** * convert a given type to a map of relation types in which it can play roles * and the corresponding role types including entity type hierarchy * @param entryConcept to be converted * @return map of relation types in which it can play roles and the corresponding role types */ default Multimap<RelationshipType, Role> toRelationshipMultimap(T entryConcept){ Multimap<RelationshipType, Role> relationMap = HashMultimap.create(); toCompatibleRoles(entryConcept) .forEach(role -> role.relationships() .forEach(rel -> relationMap.put(rel, role))); return relationMap; }
@Override public Collection<PropertyExecutor> undefine(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Type type = executor.get(var).asType(); Role role = executor.get(this.role().var()).asRole(); if (!type.isDeleted() && !role.isDeleted()) { type.unplay(role); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var, role().var()).build()); }
private Set<String> validateRelationPlayers(Rule rule){ Set<String> errors = new HashSet<>(); getRelationPlayers().forEach(rp -> { VarPatternAdmin role = rp.getRole().orElse(null); if (role == null){ errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.then(), rule.label())); } else { Label roleLabel = role.getTypeLabel().orElse(null); if (roleLabel == null){ errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.then(), rule.label())); } else { if (Schema.MetaSchema.isMetaLabel(roleLabel)) { errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.then(), rule.label())); } Role roleType = tx().getRole(roleLabel.getValue()); if (roleType != null && roleType.isImplicit()) { errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_IMPLICIT_ROLE.getMessage(rule.then(), rule.label())); } } } }); return errors; }
@Override public Concept visit(OWLSubObjectPropertyOfAxiom axiom) { if (!axiom.getSubProperty().isOWLObjectProperty() || !axiom.getSuperProperty().isOWLObjectProperty()) { return null; } RelationshipType subRelation = migrator.relation(axiom.getSubProperty().asOWLObjectProperty()); RelationshipType superRelation = migrator.relation(axiom.getSuperProperty().asOWLObjectProperty()); Map<Label, Label> roleMap = new HashMap<>(); roleMap.put(migrator.namer().subjectRole(superRelation.getLabel()), migrator.namer().subjectRole(subRelation.getLabel())); roleMap.put(migrator.namer().objectRole(superRelation.getLabel()), migrator.namer().objectRole(subRelation.getLabel())); ReasonerUtils.createSubPropertyRule("Sub Property Rule Between [" + superRelation.getLabel() + "] and [" + subRelation.getLabel() + "]", superRelation, subRelation, roleMap, migrator.tx()); migrator.subjectRole(subRelation).sup(migrator.subjectRole(superRelation)); migrator.objectRole(subRelation).sup(migrator.objectRole(superRelation)); subRelation.sup(superRelation); return null; }
@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 public String innerToString(){ StringBuilder description = new StringBuilder(); description.append("ID [").append(id()).append("] Type [").append(type().label()).append("] Roles and Role Players: \n"); for (Map.Entry<Role, Set<Thing>> entry : allRolePlayers().entrySet()) { if(entry.getValue().isEmpty()){ description.append(" Role [").append(entry.getKey().label()).append("] not played by any instance \n"); } else { StringBuilder instancesString = new StringBuilder(); for (Thing thing : entry.getValue()) { instancesString.append(thing.id()).append(","); } description.append(" Role [").append(entry.getKey().label()).append("] played by ["). append(instancesString.toString()).append("] \n"); } } return description.toString(); }
public static Collection<? extends SchemaConcept> allSchemaElementsFrom(GraknTx graph) { Set<SchemaConcept> allSchemaConcepts = new HashSet<>(); allSchemaConcepts.addAll(graph.admin().getMetaConcept().subs().collect(toSet())); allSchemaConcepts.addAll(graph.admin().getMetaRole().subs().collect(toSet())); allSchemaConcepts.addAll(graph.admin().getMetaRule().subs().collect(toSet())); return allSchemaConcepts; }
/** * 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); } }
/** * Checks if the {@link Role} of the {@link Casting} has been linked to the {@link RelationshipType} of * the {@link Relationship} which the {@link Casting} connects to. * * @param role the {@link Role} which the {@link Casting} refers to * @param relationshipType the {@link RelationshipType} which should connect to the role * @param relationship the {@link Relationship} which the {@link Casting} refers to * @return an error if one is found */ private static Optional<String> roleNotLinkedToRelationShip(Role role, RelationshipType relationshipType, Relationship relationship){ boolean notFound = role.relationships(). noneMatch(innerRelationType -> innerRelationType.label().equals(relationshipType.label())); if(notFound){ return Optional.of(VALIDATION_RELATION_CASTING_LOOP_FAIL.getMessage(relationship.id(), role.label(), relationshipType.label())); } return Optional.empty(); }
/** * Add relates edges to a var, given a type * @param var var to be modified * @param type type from which metadata extracted * @return var with appropriate relates edges */ private static VarPattern relates(VarPattern var, RelationshipType type){ for(Role role:type.roles().collect(Collectors.toSet())){ var = var.relates(Graql.label(role.label())); } return var; }
private Role role() { return random.choose(tx.admin().getMetaRole().subs().collect(toSet())); }
/** * 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); } } }
/** * Add plays edges to a var, given a type * @param var var to be modified * @param type type from which metadata extracted * @return var with appropriate plays edges */ private static VarPattern plays(VarPattern var, Type type) { for(Role role:type.playing().collect(Collectors.toSet())){ var = var.plays(Graql.label(role.label())); } return var; }
/** * Get all the {@link SchemaConcept}s in a graph. * @return a stream of all {@link SchemaConcept}s with non-reserved IDs */ private Stream<? extends SchemaConcept> schemaConcepts(){ Stream<? extends Type> types = tx.admin().getMetaConcept().subs(); Stream<Role> roles = tx.admin().getMetaRole().subs(); Stream<Rule> rules = tx.admin().getMetaRule().subs(); return Stream.of(types, roles, rules) .flatMap(Function.identity()) .filter(t -> !Schema.MetaSchema.isMetaLabel(t.label())); } }