/** * When a relation is deleted this cleans up any solitary casting and resources. */ void cleanUp() { Stream<Thing> rolePlayers = rolePlayers(); boolean performDeletion = rolePlayers.noneMatch(thing -> thing != null && thing.id() != null); if(performDeletion) delete(); }
/** * * @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(); }
@Override Iterable<? super MatchableConcept> transform(MatchableConcept item) { return item.get().asThing().attributes() .filter(resource -> MatchableConcept.NAME_TYPES.contains(resource.type().label())) .map(MatchableConcept::of) .collect(toSet()); }
/** * Map an Thing to the equivalent Graql representation * @param thing thing to be mapped * @return Graql representation of given thing */ public static VarPattern map(Thing thing){ if(thing.isEntity()){ return map(thing.asEntity()); } else if(thing.isAttribute()){ return map(thing.asAttribute()); } else if(thing.isRelationship()){ return map(thing.asRelationship()); } else { throw CommonUtil.unreachableStatement("Unrecognised thing " + thing); } }
private String summaryFormat(Object object) { if (object instanceof SchemaConcept) { return ((SchemaConcept) object).label().getValue().replaceAll("-", "_"); } else if (object instanceof Thing) { Thing thing = (Thing) object; return summaryFormat(thing.type()) + thing.id().getValue(); } else if (object instanceof Label) { return valueToString(((Label) object).getValue()); } else { return object.toString(); } }
rolePlayerList.add(colorType(role) + ": id " + idToString(thing.id())); Type type = concept.asThing().type(); output.append(colorKeyword(" isa ")).append(colorType(type)); concept.asThing().attributes(attributeTypes).forEach(resource -> { String resourceType = colorType(resource.type()); String value = StringUtil.valueToString(resource.value());
@Override public String toString() { if (concept.isAttribute()) { return "hasValue(" + valueToString(concept.asAttribute().value()) + ")"; } else if (concept.isThing()) { Thing thing = concept.asThing(); Stream<Attribute<?>> resources = thing.attributes(); Optional<?> value = resources .filter(resource -> NAME_TYPES.contains(resource.type().label())) .map(Attribute::value).findFirst(); return "instance(" + value.map(StringUtil::valueToString).orElse("") + ") isa " + thing.type().label(); } else if (concept.isType()) { return "type(" + concept.asType().label() + ")"; } else if (concept.isRole()) { return "role(" + concept.asRole().label() + ")"; } else if (concept.isRule()) { return "rule(" + concept.asRule().label() + ")"; } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } }
private static Set<Type> getTypes(Thing thing) { Set<Type> types = Sets.newHashSet(); Type type = thing.type(); while (type != null) { types.add(type); type = type.sup(); } return types; }
@Override protected final T generateFromTx() { T thing; S type = genFromTx(generatorClass).makeExcludeAbstractTypes().excludeMeta().generate(random, status); //noinspection unchecked Collection<T> instances = (Collection<T> ) type.instances().collect(toSet()); if (instances.isEmpty()) { thing = newInstance(type); } else { thing = random.choose(instances); } if(withResource && !thing.attributes().findAny().isPresent()){ //A new attribute type is created every time a attribute is lacking. //Existing attribute types and resources of those types are not used because we end up mutating the // the schema in strange ways. This approach is less complex but ensures everything has a attribute // without conflicting with the schema //Create a new attribute type AttributeType.DataType<?> dataType = gen(AttributeType.DataType.class); Label label = genFromTx(Labels.class).mustBeUnused().generate(random, status); AttributeType attributeType = tx().putAttributeType(label, dataType); //Create new attribute Attribute attribute = newResource(attributeType); //Link everything together type.has(attributeType); thing.has(attribute); } return thing; }
@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); } } }
@Nullable public <T> Entity getEntity(T id, AttributeType<T> rtype){ Attribute<T> iri = rtype.getAttribute(id); Thing inst = iri != null? iri.ownerInstances().findFirst().orElse(null) : null; return inst != null? inst.asEntity() : null; }
/** * Helper method to check if concept instances exist in the query scope * * @param ids * @return true if they exist, false if they don't */ private boolean scopeContainsInstances(ConceptId... ids) { for (ConceptId id : ids) { Thing thing = tx.getConcept(id); if (thing == null || !scopeTypeLabels().contains(thing.type().label())) return false; } return true; }
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(); }
@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(); }
TypeImpl<?, ?> currentConcept = (TypeImpl<?, ?>) thing.type(); 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())); return Optional.empty(); } else { return Optional.of(VALIDATION_CASTING.getMessage(thing.type().label(), thing.id(), role.label()));
/** * Thrown when a {@link Thing} is not allowed to have {@link Attribute} of that {@link AttributeType} */ public static GraknTxOperationException hasNotAllowed(Thing thing, Attribute attribute){ return create(HAS_INVALID.getMessage(thing.type().label(), attribute.type().label())); }
/** * Add the resources of an entity * @param var var representing the entity * @param thing thing containing resource information * @return var pattern with resources */ private static VarPattern hasResources(VarPattern var, Thing thing){ for(Attribute attribute : thing.attributes().collect(Collectors.toSet())){ var = var.has(attribute.type().label(), var().val(attribute.value())); } return var; }
/** * Thrown when an thing does not have a type */ public static GraknTxOperationException noType(Thing thing){ return create(NO_TYPE.getMessage(thing.id())); }
@Override protected Json concept(Concept concept) { Json json = Json.object("id", concept.id().getValue()); if (concept.isSchemaConcept()) { json.set("name", concept.asSchemaConcept().label().getValue()); SchemaConcept superConcept = concept.asSchemaConcept().sup(); if (superConcept != null) json.set("sub", superConcept.label().getValue()); } else if (concept.isThing()) { json.set("isa", concept.asThing().type().label().getValue()); } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } if (concept.isAttribute()) { json.set("value", concept.asAttribute().value()); } if (concept.isRule()) { Pattern when = concept.asRule().when(); if (when != null) { json.set("when", when.toString()); } Pattern then = concept.asRule().then(); if (then != null) { json.set("then", then.toString()); } } return json; }