public RelationshipType relation(OWLDataProperty property) { RelationshipType relType = tx.putRelationshipType(namer.resourceRelation(property.getIRI())); AttributeType<?> attributeType = resourceType(property); relType.relates(entityRole(owlThingEntityType(), attributeType)); relType.relates(resourceRole(attributeType)); return relType; }
RelationshipTypeImpl superRelationType = (RelationshipTypeImpl) relationshipType.sup(); if(Schema.MetaSchema.isMetaLabel(superRelationType.label()) || superRelationType.isAbstract()){ //If super type is a meta type no validation needed return Collections.emptySet(); Collection<Role> relates = relationshipType.roles().collect(Collectors.toSet()); Set<Label> relatesLabels = relates.stream().map(SchemaConcept::label).collect(Collectors.toSet()); 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())); errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.label(), superRelationType.label(), "sub", "sub", relationshipType.label()));
.sorted(Comparator.comparing(e -> e.getKey().roles().count() != getRelationPlayers().size())) Set<Role> rs = e.getKey().roles().collect(toSet()); rs.removeAll(e.getValue()); return new Pair<>( .sorted(Comparator.comparing(p -> -p.getValue())) .sorted(Comparator.comparing(e -> e.getKey().isImplicit())) .map(Pair::getKey)
/** * * @param relationshipType The {@link RelationshipType} to validate * @return An error message if the relationTypes does not have at least 1 role */ static Optional<String> validateHasMinimumRoles(RelationshipType relationshipType) { if(relationshipType.isAbstract() || relationshipType.roles().iterator().hasNext()){ return Optional.empty(); } else { return Optional.of(VALIDATION_RELATION_TYPE.getMessage(relationshipType.label())); } }
/** * @return a map of relationships and corresponding roles that could be played by this atom */ private Multimap<RelationshipType, Role> inferPossibleRelationConfigurations(ConceptMap sub){ Set<Role> roles = getExplicitRoles().filter(r -> !Schema.MetaSchema.isMetaLabel(r.label())).collect(toSet()); Map<Var, Type> varTypeMap = getParentQuery().getVarTypeMap(sub); Set<Type> types = getRolePlayers().stream().filter(varTypeMap::containsKey).map(varTypeMap::get).collect(toSet()); if (roles.isEmpty() && types.isEmpty()){ RelationshipType metaRelationType = tx().admin().getMetaRelationType(); Multimap<RelationshipType, Role> compatibleTypes = HashMultimap.create(); metaRelationType.subs() .filter(rt -> !rt.equals(metaRelationType)) .forEach(rt -> compatibleTypes.putAll(rt, rt.roles().collect(toSet()))); return compatibleTypes; } //intersect relation types from roles and types Multimap<RelationshipType, Role> compatibleTypes; Multimap<RelationshipType, Role> compatibleTypesFromRoles = compatibleRelationTypesWithRoles(roles, new RoleConverter()); Multimap<RelationshipType, Role> compatibleTypesFromTypes = compatibleRelationTypesWithRoles(types, new TypeConverter()); if (roles.isEmpty()){ compatibleTypes = compatibleTypesFromTypes; } //no types from roles -> roles correspond to mutually exclusive relations else if(compatibleTypesFromRoles.isEmpty() || types.isEmpty()){ compatibleTypes = compatibleTypesFromRoles; } else { compatibleTypes = multimapIntersection(compatibleTypesFromTypes, compatibleTypesFromRoles); } return compatibleTypes; }
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); } } /**
@Override public Concept visit(OWLSubDataPropertyOfAxiom axiom) { if (!axiom.getSubProperty().isOWLDataProperty() || !axiom.getSuperProperty().isOWLDataProperty()) { return null; } RelationshipType subRelation = migrator.relation(axiom.getSubProperty().asOWLDataProperty()); RelationshipType superRelation = migrator.relation(axiom.getSuperProperty().asOWLDataProperty()); subRelation.sup(superRelation); return null; }
@Override protected Relationship newInstance(RelationshipType type) { return type.create(); } }
@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; }
public Entity putEntity(String id, EntityType type) { Entity current = getEntity(id, owlIriResource()); if(current != null) return current; Label hasIriResourceId = Label.of(OwlModel.IRI.owlname()); AttributeType<String> iriResource = owlIriResource(); Role hasIriOwner = entityRole(type, iriResource); Role hasIriValue = resourceRole(iriResource); RelationshipType hasIriRelation = tx.putRelationshipType(namer.resourceRelation(hasIriResourceId)) .relates(hasIriOwner).relates(hasIriValue); Entity entity = type.addEntity(); Attribute attributeInstance = iriResource.putAttribute(id); hasIriRelation.addRelationship() .addRolePlayer(hasIriOwner, entity) .addRolePlayer(hasIriValue, attributeInstance); return entity; }
/** * infer {@link RelationshipType}s that this {@link RelationshipAtom} can potentially have * NB: {@link EntityType}s and link {@link Role}s are treated separately as they behave differently: * {@link EntityType}s only play the explicitly defined {@link Role}s (not the relevant part of the hierarchy of the specified {@link Role}) and the {@link Role} inherited from parent * @return list of {@link RelationshipType}s this atom can have ordered by the number of compatible {@link Role}s */ private Set<Type> inferPossibleEntityTypePlayers(ConceptMap sub){ return inferPossibleRelationConfigurations(sub).asMap().entrySet().stream() .flatMap(e -> { Set<Role> rs = e.getKey().roles().collect(toSet()); rs.removeAll(e.getValue()); return rs.stream().flatMap(Role::players); }).collect(Collectors.toSet()); }
/** * Helper method to get the types to be included in the query scope * * @return stream of Concept Types */ private Stream<Type> scopeTypes() { // Get all types if query.inTypes() is empty, else get all scoped types of each meta type. // Only include attributes and implicit "has-xxx" relationships when user specifically asked for them. if (!query.in().isPresent() || query.in().get().isEmpty()) { ImmutableSet.Builder<Type> typeBuilder = ImmutableSet.builder(); if (scopeIncludesAttributes()) { tx.admin().getMetaConcept().subs().forEach(typeBuilder::add); } else { tx.admin().getMetaEntityType().subs().forEach(typeBuilder::add); tx.admin().getMetaRelationType().subs() .filter(relationshipType -> !relationshipType.isImplicit()).forEach(typeBuilder::add); } return typeBuilder.build().stream(); } else { Stream<Type> subTypes = query.in().get().stream().map(label -> { Type type = tx.getType(label); if (type == null) throw GraqlQueryException.labelNotFound(label); return type; }).flatMap(Type::subs); if (!scopeIncludesAttributes()) { subTypes = subTypes.filter(relationshipType -> !relationshipType.isImplicit()); } return subTypes; } }
/** * 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(); }
/** * Map a {@link Relationship} to a var, along with all of the roleplayers * Exclude any relations that are mapped to an encountered resource * @param relationship {@link Relationship} to be mapped * @return var patterns representing the given instance */ //TODO resources on relations private static VarPattern map(Relationship relationship){ if(relationship.type().isImplicit()){ return var(); } VarPattern var = base(relationship); var = roleplayers(var, relationship); return var; }
private Relationship relation() { return chooseOrThrow(tx.admin().getMetaRelationType().instances()); }
private RelationshipType relationType() { return random.choose(tx.admin().getMetaRelationType().subs().collect(toSet())); }
work = tx.putRole("work"); author = tx.putRole("author"); authoredBy = tx.putRelationshipType("authored-by").relates(work).relates(author); directedBy = tx.putRelationshipType("directed-by").sup(authoredBy) .relates(productionBeingDirected).relates(director); characterBeingPlayed = tx.putRole("character-being-played"); hasCast = tx.putRelationshipType("has-cast") .relates(productionWithCast).relates(actor).relates(characterBeingPlayed); .relates(genreOfProduction).relates(productionWithGenre); .relates(clusterOfProduction).relates(productionWithCluster);
/** * 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 static void hasCluster(Thing cluster, Thing... movies) { Relationship relationship = hasCluster.create().assign(clusterOfProduction, cluster); for (Thing movie : movies) { relationship.assign(productionWithCluster, movie); } } }
@Override public Map<Role, Set<Thing>> allRolePlayers() { HashMap<Role, Set<Thing>> roleMap = new HashMap<>(); //We add the role types explicitly so we can return them when there are no roleplayers type().roles().forEach(roleType -> roleMap.put(roleType, new HashSet<>())); //All castings are used here because we need to iterate over all of them anyway castingsRelation().forEach(rp -> roleMap.computeIfAbsent(rp.getRole(), (k) -> new HashSet<>()).add(rp.getRolePlayer())); return roleMap; }