/** * Checks if a {@link Relationship} has at least one role player. * @param relationship The {@link Relationship} to check */ static Optional<String> validateRelationshipHasRolePlayers(Relationship relationship) { if(!relationship.rolePlayers().findAny().isPresent()){ return Optional.of(ErrorMessage.VALIDATION_RELATIONSHIP_WITH_NO_ROLE_PLAYERS.getMessage(relationship.id(), relationship.type().label())); } return Optional.empty(); } }
/** * Add the roleplayers of a {@link Relationship} to the relationship var * @param var var representing the relationship * @param relationship {@link Relationship} that contains roleplayer data * @return var pattern with roleplayers */ private static VarPattern roleplayers(VarPattern var, Relationship relationship){ for(Map.Entry<Role, Set<Thing>> entry: relationship.rolePlayersMap().entrySet()){ Role role = entry.getKey(); for (Thing thing : entry.getValue()) { var = var.rel(Graql.label(role.label()), thing.id().getValue()); } } return var; }
/** * Deletes the concept as an Thing */ @Override public void delete() { //Remove links to relationships and return them Set<Relationship> relationships = castingsInstance().map(casting -> { Relationship relationship = casting.getRelationship(); Role role = casting.getRole(); relationship.unassign(role, this); return relationship; }).collect(toSet()); vertex().tx().txCache().removedInstance(type().id()); deleteNode(); relationships.forEach(relation -> { if(relation.type().isImplicit()){//For now implicit relationships die relation.delete(); } else { RelationshipImpl rel = (RelationshipImpl) relation; rel.cleanUp(); } }); }
public Pattern pattern(Relationship concept) { VarPattern relationPattern = Graql.var(); Set<Pattern> idPatterns = new HashSet<>(); for (Map.Entry<Role, Set<Thing>> entry : concept.rolePlayersMap().entrySet()) { for (Thing var : entry.getValue()) { Var rolePlayer = Graql.var(); relationPattern = relationPattern.rel(Graql.label(entry.getKey().label()), rolePlayer); idPatterns.add(rolePlayer.asUserDefined().id(var.id())); } } relationPattern = relationPattern.isa(Graql.label(concept.type().label())); Pattern pattern = relationPattern; for (Pattern idPattern : idPatterns) { pattern = pattern.and(idPattern); } return pattern; } }
@Override public Concept visit(OWLAnnotationAssertionAxiom axiom) { if (! (axiom.getSubject() instanceof OWLNamedIndividual) ) { return null; } Optional<OWLLiteral> value = axiom.getValue().asLiteral(); if (!value.isPresent()) { return null; } @SuppressWarnings("unchecked") AttributeType<String> attributeType = (AttributeType<String>)visit(axiom.getProperty()); Entity entity = migrator.entity((OWLNamedIndividual)axiom.getSubject()); Attribute<String> attribute = attributeType.putAttribute(value.get().getLiteral()); RelationshipType propertyRelation = migrator.relation(axiom.getProperty()); return propertyRelation.addRelationship() .addRolePlayer(migrator.entityRole(entity.type(), attribute.type()), entity) .addRolePlayer(migrator.resourceRole(attribute.type()), attribute); }
/** * This method checks if the plays edge has been added between the roleplayer's {@link Type} and * the {@link Role} being played. * * It also 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. * * @return Specific errors if any are found */ static Set<String> validatePlaysAndRelatesStructure(Casting casting) { Set<String> errors = new HashSet<>(); //Gets here to make sure we traverse/read only once Thing thing = casting.getRolePlayer(); Role role = casting.getRole(); Relationship relationship = casting.getRelationship(); //Actual checks roleNotAllowedToBePlayed(role, thing).ifPresent(errors::add); roleNotLinkedToRelationShip(role, relationship.type(), relationship).ifPresent(errors::add); return errors; }
public static ConceptMethod<Stream<? extends Concept>> getRolePlayersByRoles(Role... roles) { return ConceptMethod.builder(ConceptResponseType.CONCEPTS) .requestSetter(builder -> builder.setGetRolePlayersByRoles(convert(Stream.of(roles)))) .function(concept -> concept.asRelationship().rolePlayers(roles)) .build(); }
/** * 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(); }
public static ConceptMethod<Void> removeRolePlayer(RolePlayer rolePlayer) { return ConceptMethod.builder(ConceptResponseType.UNIT) .requestSetter(builder -> builder.setUnsetRolePlayer(convert(rolePlayer))) .functionVoid(concept -> { concept.asRelationship().removeRolePlayer(rolePlayer.role(), rolePlayer.player()); }) .build(); }
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; }
/** * 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; }
@Override public T unhas(Attribute attribute){ Role roleHasOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().label())); Role roleKeyOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_OWNER.getLabel(attribute.type().label())); Role roleHasValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().label())); Role roleKeyValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_VALUE.getLabel(attribute.type().label())); Stream<Relationship> relationships = relationships(filterNulls(roleHasOwner, roleKeyOwner)); relationships.filter(relationship -> { Stream<Thing> rolePlayers = relationship.rolePlayers(filterNulls(roleHasValue, roleKeyValue)); return rolePlayers.anyMatch(rolePlayer -> rolePlayer.equals(attribute)); }).forEach(Concept::delete); return getThis(); }
@Override public Collection<PropertyExecutor> insert(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Attribute attributeConcept = executor.get(attribute().var()).asAttribute(); Thing thing = executor.get(var).asThing(); ConceptId relationshipId = thing.relhas(attributeConcept).id(); executor.builder(relationship().var()).id(relationshipId); }; PropertyExecutor executor = PropertyExecutor.builder(method) .produces(relationship().var()) .requires(var, attribute().var()) .build(); return ImmutableSet.of(executor); }
public static ConceptMethod<Void> setRolePlayer(RolePlayer rolePlayer) { return ConceptMethod.builder(ConceptResponseType.UNIT) .requestSetter(builder -> builder.setSetRolePlayer(convert(rolePlayer))) .functionVoid(concept -> concept.asRelationship().addRolePlayer(rolePlayer.role(), rolePlayer.player())) .build(); }
for (Map.Entry<Role, Set<Thing>> rolePlayers : concept.asRelationship().rolePlayersMap().entrySet()) { Role role = rolePlayers.getKey(); Set<Thing> things = rolePlayers.getValue();
@Override public Concept visit(OWLObjectPropertyAssertionAxiom axiom) { if (!axiom.getSubject().isNamed() || !axiom.getObject().isNamed() || !axiom.getProperty().isOWLObjectProperty()) { return null; } Entity subject = migrator.entity(axiom.getSubject().asOWLNamedIndividual()); Entity object = migrator.entity(axiom.getObject().asOWLNamedIndividual()); RelationshipType relationshipType = migrator.relation(axiom.getProperty().asOWLObjectProperty()); return relationshipType.addRelationship() .addRolePlayer(migrator.subjectRole(relationshipType), subject) .addRolePlayer(migrator.objectRole(relationshipType), object); }
@Override protected void buildRelations() { //Enemy(Nono, America) isEnemyOf.create() .assign(enemySource, Nono) .assign(enemyTarget, America); //Owns(Nono, Missile) owns.create() .assign(owner, Nono) .assign(ownedItem, Tomahawk); //isPaidBy(West, Nono) isPaidBy.create() .assign(payee, colonelWest) .assign(payer, Nono); }