@Override protected Relationship generateFromTx() { Stream<? extends Thing> things = tx().admin().getMetaConcept().instances(); Optional<Relationship> relation = things.flatMap(thing -> thing.relationships()).findAny(); if (relation.isPresent()) { return relation.get(); } else { // Give up and fall back to normal generator return genFromTx(Relations.class).generate(random, status); } } }
/** * * @param thing The thing to be validated * @return An error message if the thing does not have all the required resources */ static Optional<String> validateInstancePlaysAllRequiredRoles(Thing thing) { TypeImpl<?, ?> currentConcept = (TypeImpl) thing.type(); while(currentConcept != null){ Map<Role, Boolean> plays = currentConcept.directPlays(); for (Map.Entry<Role, Boolean> playsEntry : plays.entrySet()) { if(playsEntry.getValue()){ Role role = playsEntry.getKey(); // Assert there is a relationship for this type Stream<Relationship> relationships = thing.relationships(role); if(!CommonUtil.containsOnly(relationships, 1)){ Label resourceTypeLabel = Schema.ImplicitType.explicitLabel(role.label()); return Optional.of(VALIDATION_NOT_EXACTLY_ONE_KEY.getMessage(thing.id(), resourceTypeLabel)); } } } currentConcept = (TypeImpl) currentConcept.sup(); } return Optional.empty(); }
public static ConceptMethod<Stream<? extends Concept>> getRelationshipsByRoles(Role... roles) { return ConceptMethod.builder(ConceptResponseType.CONCEPTS) .requestSetter(builder -> builder.setGetRelationshipsByRoles(convert(Stream.of(roles)))) .function(concept -> concept.asThing().relationships(roles)) .build(); }
if (required && !CommonUtil.containsOnly(thing.relationships(role), 1)) { return Optional.of(VALIDATION_REQUIRED_RELATION.getMessage(thing.id(), thing.type().label(), role.label(), thing.relationships(role).count()));