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; }
/** * 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; }
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(); } }
/** * * @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(); }
TypeImpl<?, ?> currentConcept = (TypeImpl<?, ?>) thing.type(); 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())); }
Type type = concept.asThing().type(); output.append(colorKeyword(" isa ")).append(colorType(type));
/** * Given an thing, return a var with the type. * @param thing thing to map * @return var patterns representing given thing */ private static VarPattern base(Thing thing){ VarPattern var = var(thing.id().getValue()).isa(Graql.label(thing.type().label())); return hasResources(var, thing); }
@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); } }
@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; }